File: Net.k.in

package info (click to toggle)
kaya 0.4.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,448 kB
  • ctags: 1,694
  • sloc: cpp: 9,536; haskell: 7,461; sh: 3,013; yacc: 910; makefile: 816; perl: 90
file content (567 lines) | stat: -rw-r--r-- 23,868 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004, 2005 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/
"<summary>Networking functions</summary>
<prose>This module contains functions for setting up TCP network connections and sending and receiving data, and listening on TCP ports. If <code>libgnutls</code> was available when Kaya was configured, then <moduleref>TLS</moduleref> may be used to set up encrypted connections.</prose>"
module Net;

import Builtins;
import System;
import Prelude;
// TODO: TLS currently available for client only, server support needed
import TLS;
import Binary;

%include "network_glue.h";
%include "unistd.h";
%imported "network_glue";
@NETLIB@%link "wsock32";

foreign "network_glue.o" {
    Void do_net_init(Ptr vm) = net_init;
    String net_recv(Ptr vm, Ptr handle, a len) = net_recv;
    Ptr net_recvchars(Ptr vm, Ptr handle, a len) = net_recvchars;
    Int net_recvbyte(Ptr vm, Ptr handle) = net_recvbyte;
    Ptr net_connect(Ptr vm, Int proto, String server, Int port) = net_connect;
    Bool net_connwaiting(Ptr vm, Int socket, Int timeout) = net_connwaiting;
    Int net_listen(Ptr vm, Int proto, Int port, Int backlog) = net_listen;
    Ptr net_accept(Ptr vm, Int socket) = net_accept;
    Void net_shutdown(Ptr h) = net_shutdown;
    Void net_close(Ptr vm, Ptr h) = net_close;
    Void close(Int x) = close;
    Void net_send(Ptr vm, Ptr h, String d) = net_send;
    Void net_sendchars(Ptr vm, Ptr h, Ptr d, Int len) = net_sendchars;
    Void net_sendbyte(Ptr vm, Ptr h, Int byte) = net_sendbyte;
    Bool net_pending(Ptr vm, Ptr h, Int timeout) = net_pending;
    String net_getaddr(Ptr h) = net_getaddr;
}

"<summary>Can't bind to port</summary>
<prose>This Exception is thrown when it is not possible to listen on a TCP port.</prose>"
Exception CantBind();
"<summary>Can't listen on port</summary>
<prose>This Exception is thrown when it is not possible to listed on a TCP port, but binding to the port was successful.</prose>"
Exception CantListen();
"<summary>Failed to accept connection</summary>
<prose>This Exception is thrown when a listening socket fails to accept a connection.</prose>"
Exception AcceptFailed();
"<summary>Sending data failed</summary>
<prose>This Exception is thrown when sending data to a network connection fails.</prose>"
Exception SendFailed();
"<summary>No data to receive</summary>
<prose>This Exception is thrown when no data is receivable on a network connection.</prose>"
Exception NothingToReceive();
"<summary>Testing connection failed</summary>
<prose>This Exception is thrown when an attempt to check if there is data waiting on a connection fails.</prose>"
Exception SelectError();
"<summary>Initialisation failed</summary>
<prose>This Exception is thrown if initialisation of the networking libraries failed.</prose>"
Exception CantInit();
"<summary>Protocol not supported</summary>
<prose>This Exception is thrown if the protocol chosen was not supported. Currently IPv6 is not supported.</prose>"
Exception ProtocolNotSupported();
"<summary>Host not known</summary>
<prose>The system was unable to look up the host in DNS.</prose>"
Exception CantGetHost();
"<summary>Connection failed</summary>
<prose>The connection to the remote host failed.</prose>"
Exception ConnectError();
"<summary>Close failed</summary>
<prose>An error occurred when trying to close a connection.</prose>"
Exception CloseError();
"<summary>Negative Length</summary>
<prose>A negative length was specified when receiving data.</prose>"
Exception NegativeLength();

Void netError(Int code)
{
    case code of {
	1 -> throw(CantBind);
	| 2 -> throw(CantListen);
	| 3 -> throw(AcceptFailed);
	| 4 -> throw(SendFailed);
	| 5 -> throw(NothingToReceive);
	| 6 -> throw(SelectError);
	| 7 -> throw(CantInit);
	| 8 -> throw(ProtocolNotSupported);
	| 9 -> throw(CantGetHost);
	| 10 -> throw(ConnectError);
	| 11 -> throw(CloseError);
	| default -> throw(InternalError(code));
    }
}

"<summary>A network connection</summary>
<prose>A network connection in use.</prose>"
abstract data NetHandle = NetH(Ptr handle, Maybe<TLSsession> tls, String hostname);

Int MAXCONN = 128;

"<summary>The protocol to use</summary>
<prose>The network protocol to use for connections. IPv6 is not currently supported.</prose>"
public data Protocol = TCP | TCP_IPv4 | TCP_IPv6;

globals {
    Bool initRun = netInit();
}

"<summary>Initialises networking libraries.</summary>
<prose>Initialises the networking libraries. This is called automatically when this module is imported, but is harmless to call again. On operating systems other than Windows, this function does nothing.</prose>"
public Bool netInit() {
    if (initRun) {
	return true;
    }
    try {
	do_net_init(getVM());
    } catch(InternalError(e)) {
	netError(e);
    }
    return true;
}

"<argument name='proto'>The network protocol to use</argument>
<argument name='server'>The hostname or IP address of the server to connect to (e.g. <code>service.example.com</code>, <code>localhost</code> or <code>192.168.20.25</code>)</argument>
<argument name='port'>The network port to connect to</argument>
<argument name='usetls'>If this is true, and the TLS library is available, then the connection will be encrypted. This parameter may be omitted and defaults to false.</argument>
<argument name='certs'>An optional list of certificate files for TLS encrypted connections. Each file should contain one or more PEM-encoded certificates for a trusted certification authority. If this list is empty (the default), no certificate verification will be performed on encrypted connections, which is insecure.</argument>
<summary>Connect to a server</summary>
<prose>Connect to a server (optionally using TLS). Returns a connection handle.</prose>
<related><dataref>NetHandle</dataref></related>
<related><dataref>Protocol</dataref></related>
<related><functionref>closeConnection</functionref></related>
<related><functionref>recv</functionref></related>
<related><functionref>shutdown</functionref></related>
<related><functionref>send</functionref></related>"
public NetHandle connect(Protocol proto, String server, Int port, Bool usetls=false, [String] certfiles=[])
{
    try{
	h = net_connect(getVM(),protocolToInt(proto),server,port);
	if (usetls) {
	    tls = makeTLS(h);
	    if (size(certfiles) > 0) {
	      for cf in certfiles {
		acceptCertificate(tls,cf);
	      }
	      verifyCertificate(tls,server);
	    }
	    return NetH(h,just(tls),server);
	} else {
	    return NetH(h,nothing,server);
	}
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='proto'>The network protocol to use</argument>
<argument name='port'>The port to listen on</argument>
<argument name='backlog'>The maximum number of unaccepted connections.</argument>
<summary>Listen on a port.</summary>
<prose>Listen on a port, returning a socket id.</prose>
<related><dataref>Protocol</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>closeSocket</functionref></related>"
public Int listen(Protocol proto, Int port, Int backlog)
{
    try {
	return net_listen(getVM(),protocolToInt(proto),port,backlog);
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"Return the Int representation of protocol needed by the C glue library."
Int protocolToInt(Protocol proto) = case proto of {
	TCP -> 1;
      | TCP_IPv4 -> 1;
      | TCP_IPv6 -> 2;
};

"<argument name='socket'>A socket number returned from <functionref>listen</functionref>.</argument>
<argument name='timeout'>The number of seconds to wait for a connection attempt before timing out (defaults to zero)</argument>
<summary>Check for connections waiting on a socket.</summary>
<prose>Returns whether any connections are waiting on a socket opened with <functionref>listen</functionref>.</prose>
<related><functionref>listen</functionref></related>
<related><functionref>accept</functionref></related>"
public Bool connWaiting(Int socket, Int timeout = 0) {
    try {
	return net_connwaiting(getVM(),socket,timeout*1000000);
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='socket'>A socket number returned from <functionref>listen</functionref>.</argument>
<summary>Accept a connection on a socket.</summary>
<prose>Accept a connection on a socket. Returns a connection handle. If no connections are currently waiting then an Exception will be thrown - you can call <functionref>connWaiting</functionref> first to determine if connections are available, although this may not be completely reliable in threaded applications.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>closeConnection</functionref></related>
<related><functionref>closeSocket</functionref></related>
<related><functionref>connWaiting</functionref></related>
<related><functionref>listen</functionref></related>
<related><functionref>recv</functionref></related>
<related><functionref>shutdown</functionref></related>
<related><functionref>send</functionref></related>"
public NetHandle accept(Int socket) {
    try {
	h = net_accept(getVM(),socket);
	return NetH(h,nothing,"");
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='h'>The handle to shut down</argument>
<summary>Shutdown a network handle.</summary>
<prose>Shutdown a network handle so that no further data can be sent or received.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>closeConnection</functionref></related>"
public Void shutdown(NetHandle h) {
    try {
	net_shutdown(h.handle);
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='h'>The handle to close</argument>
<summary>Close a connection handle.</summary>
<prose>Close a connection handle. You should always close connection handles once you have finished with them.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>connect</functionref></related>"
public Void closeConnection(NetHandle h) {
    try {
	case h.tls of {
	    nothing -> ;
	    | just(tls) -> closeTLS(tls);
	}
	net_close(getVM,h.handle);
    } catch(InternalError(e)) {
	netError(e);
    }
}

"<argument name='x'>The socket returned by <functionref>listen</functionref>.</argument>
<summary>Close a socket.</summary>
<prose>Stop listening on a socket and accept no further connections.</prose>
<related><functionref>listen</functionref></related>"
public Void closeSocket(Int x) {
    close(x);
}

"<argument name='h'>The connection</argument>
<argument name='d'>The data to send</argument>
<summary>Send data across a connection.</summary>
<prose>Send data across a connection. This function is not suitable for data containing null bytes, for which <functionref>sendByte</functionref> or <functionref>sendBytes</functionref> should be used.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>closeConnection</functionref></related>
<related><functionref>connect</functionref></related>
<related><functionref>sendByte</functionref></related>
<related><functionref>sendBytes</functionref></related>
<related><functionref>recv</functionref></related>"
public Void send(NetHandle h, String d) {
    try {
	case h.tls of {
	    nothing -> net_send(getVM(),h.handle,d);
	    | just(tls) -> putTLS(tls,d);
	}
    } catch(InternalError(e)) {
	netError(e);
    }
}

"<argument name='h'>The connection</argument>
<argument name='d'>The data to send</argument>
<summary>Send binary data across a connection.</summary>
<prose>Send binary data across a connection. This function does not yet support TLS connections.</prose>
<related><dataref>Binary::Binary</dataref></related>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>closeConnection</functionref></related>
<related><functionref>connect</functionref></related>
<related><functionref>send</functionref></related>
<related><functionref>sendByte</functionref></related>
<related><functionref>recv</functionref></related>"
public Void sendBytes(NetHandle h, Binary d) {
    try {
	case h.tls of {
	    nothing -> net_sendchars(getVM(),h.handle,blockData(d),blockSize(d));
	    | just(tls) -> putTLSBytes(tls,d);
	}
    } catch(InternalError(e)) {
	netError(e);
    }
}

"<argument name='h'>The connection</argument>
<argument name='byte'>The byte to send</argument>
<summary>Send a single byte across a connection.</summary>
<prose>Send a single byte across a connection. The Int passed should be between 0 and 255, or the results are unpredictable. This function does not yet support TLS connections.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>closeConnection</functionref></related>
<related><functionref>connect</functionref></related>
<related><functionref>send</functionref></related>
<related><functionref>sendBytes</functionref></related>
<related><functionref>recv</functionref></related>"
public Void sendByte(NetHandle h, Int byte) {
    try {
	case h.tls of {
	    nothing -> net_sendbyte(getVM(),h.handle,byte);
	    | just(tls) -> putTLSByte(tls,byte);
	}
    } catch(InternalError(e)) {
	netError(e);
    }
}

"<argument name='h'>The connection</argument>
<argument name='maxlen'>The maximum number of bytes to read (or unlimited, if negative). For TLS connections, this is always unlimited. Note that if the data to be received contains multi-byte characters, care must be taken to avoid ending receiving part-way through a multi-byte character.</argument>
<argument name='timeout'>The number of seconds to wait without input before timing out. The default is zero. For TLS connections, this parameter is ignored.</argument>
<summary>Recieve data from a connection.</summary>
<prose>Reads up to <variable>maxlen</variable> bytes from the connection, and times out after <variable>timeout</variable> seconds.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>connect</functionref></related>
<related><functionref>microPending</functionref></related>
<related><functionref>microRecv</functionref></related>
<related><functionref>pending</functionref></related>
<related><functionref>recvByte</functionref></related>
<related><functionref>recvBytes</functionref></related>
<related><functionref>send</functionref></related>"
public String recv(NetHandle h, Int maxlen = -1, Int timeout = 0) 
{
    try {
	case h.tls of {
	    nothing -> return dorecv(h, maxlen, timeout*1000000);
	    | just(tls) -> try {
	      return getTLS(tls,true,maxlen,timeout*1000000,microPending@(h)); // for now, set iiscompensation here
	    } catch(TLSTimeout) {
	      throw(NothingToReceive);
	    }
	}
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='h'>The connection</argument>
<argument name='maxlen'>The maximum number of bytes to read. This argument must be a positive integer</argument>
<argument name='timeout'>The number of microseconds to wait without input before timing out. The default is zero. <!-- For TLS connections, this parameter is ignored.--></argument>
<summary>Recieve binary data from a connection.</summary>
<prose>Reads up to <variable>maxlen</variable> bytes from the connection as binary data, and times out after <variable>timeout</variable> microseconds. This function does not yet support TLS connections.</prose>
<related><dataref>Binary::Binary</dataref></related>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>connect</functionref></related>
<related><functionref>microPending</functionref></related>
<related><functionref>microRecv</functionref></related>
<related><functionref>pending</functionref></related>
<related><functionref>recv</functionref></related>
<related><functionref>recvByte</functionref></related>
<related><functionref>send</functionref></related>"
public Binary recvBytes(NetHandle h, Int maxlen = 1024, Int timeout = 0) 
{
    if (maxlen < 1) {
        throw(NegativeLength);
    }
    try {
      if (timeout <= 0 || microPending(h,timeout)) {
	case h.tls of {
	    nothing -> ptr = net_recvchars(getVM,h.handle,maxlen);
	    return createInitialisedBlock(ptr,maxlen);
	    | just(tls) -> return getTLSBytes(tls,true); // for now, set iiscompensation here
	}
      } else {
	throw(NothingToReceive);
      }
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='h'>The connection</argument>
<argument name='timeout'>The number of microseconds to wait without input before timing out. The default is zero. <!--For TLS connections, this parameter is ignored.--></argument>
<summary>Recieve data from a connection.</summary>
<prose>Reads a single byte from the connection, and times out after <variable>timeout</variable> microseconds. This function does not yet support TLS connections.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>connect</functionref></related>
<related><functionref>microPending</functionref></related>
<related><functionref>microRecv</functionref></related>
<related><functionref>pending</functionref></related>
<related><functionref>recv</functionref></related>
<related><functionref>recvBytes</functionref></related>
<related><functionref>send</functionref></related>"
public Int recvByte(NetHandle h, Int timeout = 0) 
{
    try {
      if (timeout <= 0 || microPending(h,timeout)) {
	case h.tls of {
	    nothing -> return net_recvbyte(getVM,h.handle);
	    | just(tls) -> r = getTLSByte(tls,true); // for now, set iiscompensation here
	    if (r == -1) {
	      throw(NothingToReceive);
	    } else {
	      return r;
	    }
	}
      } else {
	throw(NothingToReceive);
      }
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='h'>The connection</argument>
<argument name='maxlen'>The maximum number of bytes to read (or unlimited, if negative). For TLS connections, this is always unlimited.</argument>
<argument name='timeout'>The number of microseconds to wait without input before timing out. The default is zero. For TLS connections, this parameter is ignored.</argument>
<summary>Recieve data from a connection.</summary>
<prose>Reads up to <variable>maxlen</variable> bytes from the connection, and times out after <variable>timeout</variable> microseconds. Apart from the units the timeout is measured in, this is otherwise identical to <functionref>recv</functionref></prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>connect</functionref></related>
<related><functionref>microPending</functionref></related>
<related><functionref>pending</functionref></related>
<related><functionref>recv</functionref></related>
<related><functionref>send</functionref></related>"
public String microRecv(NetHandle h, Int maxlen = -1, Int microtimeout = 0) 
{
    try {
	case h.tls of {
	    nothing -> return dorecv(h, maxlen, microtimeout);
	    | just(tls) -> return getTLS(tls,true,maxlen,microtimeout,microPending@(h));
	}
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

// timeout in microseconds
String dorecv(NetHandle h, Int maxlen = -1, Int timeout = 0) {
    try {
	len = maxlen;
	rpt = false;
	if (maxlen==-1) {
	    len = 65535; // Repeatedly read in 64k chunks.
	    rpt = true;
	}

	str="";
	do {
	    if (timeout<=0) {
		str += net_recv(getVM(),h.handle,len);
//	    putStrLn(str);
	    }
	    else {
		if (microPending(h,timeout)) {
		    str += net_recv(getVM(),h.handle,len);
		}
		else {
		    throw(NothingToReceive());
		}
	    }
	    if (len==0) { rpt=false; }
	}
	while (rpt);
	return str;
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='h'>The connection</argument>
<argument name='timeout'>The number of seconds to wait for data before timing out (defaults to five)</argument>
<summary>Return whether data is waiting at a socket.</summary>
<prose>Return whether data is waiting at a socket, returning true if so, and returning false if there is still no data after the timeout period.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>microPending</functionref></related>
<related><functionref>microRecv</functionref></related>
<related><functionref>recv</functionref></related>"
public Bool pending(NetHandle h, Int timeout = 5) {
    try {
	return net_pending(getVM(),h.handle,timeout*1000000);
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='h'>The connection</argument>
<argument name='timeout'>The number of microseconds to wait for data before timing out (defaults to 100)</argument>
<summary>Return whether data is waiting at a socket.</summary>
<prose>Return whether data is waiting at a socket, returning true if so, and returning false if there is still no data after the timeout period.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>microRecv</functionref></related>
<related><functionref>pending</functionref></related>
<related><functionref>recv</functionref></related>"
public Bool microPending(NetHandle h, Int timeout = 100) {
    try {
	return net_pending(getVM(),h.handle,timeout);
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}


"<argument name='h'>The connection handle</argument>
<summary>Get the host name from a connection handle.</summary>
<prose>Get the host name from a connection handle. This is most useful with connections created with <functionref>accept</functionref>.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>accept</functionref></related>
<related><functionref>connect</functionref></related>"
public String getHost(NetHandle h) {
    try {
	return net_getaddr(h.handle);
    } catch(InternalError(e)) {
	netError(e);
    }
    throw(InternalError(e));
}

"<argument name='h'>The connection handle</argument>
<argument name='certs'>An optional list of certificate files for TLS encrypted connections. Each file should contain one or more PEM-encoded certificates for a trusted certification authority. If this list is empty (the default), no certificate verification will be performed on encrypted connections, which is insecure.</argument>
<summary>Convert an existing connection to use TLS.</summary>
<prose>Begins TLS encryption on an existing connection equivilant to having set
usetls to true when creating it. Useful for secure SMTP connections. This function will fail silently if TLS encryption is already in use on the connection.</prose>
<prose>Note that this function is only designed for client connections started with <functionref>connect</functionref> rather than server connections from <functionref>accept</functionref>.</prose>
<related><dataref>NetHandle</dataref></related>
<related><functionref>connect</functionref></related>"
public Void startTLS(NetHandle h, [String] certfiles=[])
{
  case h.tls of {
    nothing -> try{
      tls = makeTLS(h.handle);
      if (size(certfiles) > 0) {
	for cf in certfiles {
	  acceptCertificate(tls,cf);
	}
	verifyCertificate(tls,h.hostname);
      }
      h.tls = just(tls);
      return;
    } catch(InternalError(e)) {
      netError(e);
    }
    throw(InternalError(e));
    | just(t) -> return; // don't start twice.
  }
}