File: network.cpp

package info (click to toggle)
polyml 5.2.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 19,692 kB
  • ctags: 17,567
  • sloc: cpp: 37,221; sh: 9,591; asm: 4,120; ansic: 428; makefile: 203; ml: 191; awk: 91; sed: 10
file content (1646 lines) | stat: -rw-r--r-- 59,899 bytes parent folder | download | duplicates (2)
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
/*
    Title:      Network functions.

    Copyright (c) 2000-7 David C. J. Matthews

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
#ifdef WIN32
#include "winconfig.h"
#else
#include "config.h"
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

#ifdef HAVE_ASSERT_H
#include <assert.h>
#define ASSERT(x) assert(x)
#else
#define ASSERT(x) 0
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif

#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif

#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#ifndef HAVE_SOCKLEN_T
typedef int socklen_t;
#endif

#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif


#ifdef WINDOWS_PC
#ifdef USEWINSOCK2
#include <winsock2.h>
#else
#include <winsock.h>
#endif
#endif

#include "globals.h"
#include "gc.h"
#include "arb.h"
#include "run_time.h"
#include "mpoly.h"
#include "processes.h"
#include "network.h"
#include "io_internal.h"
#include "sys.h"
#include "polystring.h"
#include "save_vec.h"
#include "rts_module.h"
#include "machine_dep.h"

#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif

#define STREAMID(x) (DEREFSTREAMHANDLE(x)->streamNo)
#define SAVE(x) taskData->saveVec.push(x)
#define ALLOC(n) alloc_and_save(taskData, n)
#define SIZEOF(x) (sizeof(x)/sizeof(PolyWord))

#ifdef WINDOWS_PC
static int winsock_init = 0; /* Check that it has been initialised. */

#else
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR    (-1)
#endif

#ifndef HAVE_SOCKLEN_T
typedef int socklen_t; // This must be int for Windows at least
#endif

#ifndef SHUT_RD
#define SHUT_RD     0
#endif

#ifndef SHUT_WR
#define SHUT_WR     1
#endif

#ifndef SHUT_RDWR
#define SHUT_RDWR   2
#endif

/* Address families.  Although this table is in ascending
   numerical order of address family nothing depends on that.
   The only requirement is that "INET" => AF_INET must always
   be present and "UNIX" => AF_UNIX must be present on Unix.
   Other entries are entirely optional and are for amusement
   only. */
struct af_tab_struct {
    const char *af_name;
    int af_num;
} af_table[] =
{
#ifdef AF_UNIX
    { "UNIX",       AF_UNIX }, /* This is nearly always there. */
#endif
#ifdef AF_LOCAL
    { "LOCAL",      AF_LOCAL },
#endif
    { "INET",       AF_INET }, /* This one should always be there. */
#ifdef AF_IMPLINK
    { "IMPLINK",        AF_IMPLINK },
#endif
#ifdef AF_PUP
    { "PUP",        AF_PUP },
#endif
#ifdef AF_CHAOS
    { "CHAOS",      AF_CHAOS },
#endif
#ifdef AF_IPX
    { "IPX",        AF_IPX },
#endif
#ifdef AF_NS
    { "NS",         AF_NS },
#endif
#ifdef AF_ISO
    { "ISO",        AF_ISO },
#endif
#ifdef AF_OSI
    { "OSI",        AF_OSI },
#endif
#ifdef AF_ECMA
    { "ECMA",       AF_ECMA },
#endif
#ifdef AF_DATAKIT
    { "DATAKIT",        AF_DATAKIT },
#endif
#ifdef AF_CCITT
    { "CCITT",      AF_CCITT },
#endif
#ifdef AF_SNA
    { "SNA",        AF_SNA },
#endif
#ifdef AF_DECnet
    { "DECnet",     AF_DECnet },
#endif
#ifdef AF_DLI
    { "DLI",        AF_DLI },
#endif
#ifdef AF_LAT
    { "LAT",        AF_LAT },
#endif
#ifdef AF_HYLINK
    { "HYLINK",     AF_HYLINK },
#endif
#ifdef AF_APPLETALK
    { "APPLETALK",      AF_APPLETALK },
#endif
#ifdef AF_NETBIOS
    { "NETBIOS",        AF_NETBIOS },
#endif
#ifdef AF_ROUTE
    { "ROUTE",      AF_ROUTE },
#endif
#ifdef AF_VOICEVIEW
    { "VOICEVIEW",      AF_VOICEVIEW },
#endif
#ifdef AF_FIREFOX
    { "FIREFOX",        AF_FIREFOX },
#endif
#ifdef AF_BAN
    { "BAN",        AF_BAN },
#endif
#ifdef AF_LINK
    { "LINK",       AF_LINK },
#endif
#ifdef AF_COIP
    { "COIP",       AF_COIP },
#endif
#ifdef AF_CNT
    { "CNT",        AF_CNT },
#endif
#ifdef AF_SIP
    { "SIP",        AF_SIP },
#endif
#ifdef AF_ISDN
    { "ISDN",       AF_ISDN },
#endif
#ifdef AF_E164
    { "E164",       AF_E164 },
#endif
#ifdef AF_INET6
    { "INET6",      AF_INET6 },
#endif
#ifdef AF_NATM
    { "NATM",       AF_NATM },
#endif
#ifdef AF_ATM
    { "ATM",        AF_ATM },
#endif
#ifdef AF_NETGRAPH
    { "NETGRAPH",       AF_NETGRAPH },
#endif
};

/* Socket types.  Only STREAM and DGRAM are required.  */
struct sk_tab_struct {
    const char *sk_name;
    int sk_num;
} sk_table[] =
{
    { "STREAM",     SOCK_STREAM },
    { "DGRAM",      SOCK_DGRAM },
    { "RAW",        SOCK_RAW },
    { "RDM",        SOCK_RDM },
    { "SEQPACKET",      SOCK_SEQPACKET }
};

static Handle makeHostEntry(TaskData *taskData, struct hostent *host);
static Handle makeProtoEntry(TaskData *taskData, struct protoent *proto);
static Handle makeServEntry(TaskData *taskData, struct servent *proto);
static Handle makeNetEntry(TaskData *taskData, struct netent *net);
static Handle makeList(TaskData *taskData, int count, char *p, int size, void *arg,
                       Handle (mkEntry)(TaskData *, void*, char*));
static Handle mkAftab(TaskData *taskData, void*, char *p);
static Handle mkSktab(TaskData *taskData, void*, char *p);
static Handle setSocketOption(TaskData *taskData, Handle args, int level, int opt);
static Handle getSocketOption(TaskData *taskData, Handle args, int level, int opt);
static Handle getSocketInt(TaskData *taskData, Handle args, int level, int opt);
static Handle selectCall(TaskData *taskData, Handle args, int blockType);

#ifdef WINDOWS_PC
/* To allow for portable code we map Windows socket errors to
   errno-style errors if there is a suitable entry in errno.h.
   If there isn't we simply return the negative value and handle
   it later. N.B. WSAEWOULDBLOCK is one of those which does not
   have an equivalent.  (It does seem to be defined in Windows CE). */
static int MapError(int err)
{
    switch (err)
    {
#ifdef EINTR
    case WSAEINTR: return EINTR;
#endif
#ifdef EBADF
    case WSAEBADF: return EBADF;
#endif
#ifdef EACCES
    case WSAEACCES: return EACCES;
#endif
#ifdef EFAULT
    case WSAEFAULT: return EFAULT;
#endif
#ifdef EINVAL
    case WSAEINVAL: return EINVAL;
#endif
#ifdef EMFILE
    case WSAEMFILE: return EMFILE;
#endif
#ifdef ENAMETOOLONG
    case WSAENAMETOOLONG: return ENAMETOOLONG;
#endif
#ifdef ENOTEMPTY
    case WSAENOTEMPTY: return ENOTEMPTY;
#endif
#ifdef EWOULDBLOCK
	case WSAEWOULDBLOCK: return EWOULDBLOCK;
#endif
    default: return -err;
    }
}

/* If these are not defined define them as negative because GetError returns
   negative values for socket library errors which do not have
   equivalents as errno-style errors. */
#ifndef EWOULDBLOCK
#define EWOULDBLOCK (-WSAEWOULDBLOCK)
#endif

#ifndef EINPROGRESS
#define EINPROGRESS EWOULDBLOCK
#endif

#ifndef EMFILE 
#define EMFILE      (-WSAEMFILE)
#endif

#ifndef EINTR
#define EINTR       (-WSAEINTR)
#endif

#ifndef EBADF
#define EBADF       (-WSAEBADF)
#endif

static int GetError()
{
    return MapError(WSAGetLastError());
}
#define GETERROR    (GetError())
#define MAPERROR(x) (MapError(x))
#else
#define GETERROR    (errno)
#define MAPERROR(x) (x)
#endif


Handle Net_dispatch_c(TaskData *taskData, Handle args, Handle code)
{
    int c = get_C_long(taskData, DEREFWORDHANDLE(code));
TryAgain:
    switch (c)
    {
    case 0:
        { /* Get the current host name. */
            char hostName[MAXHOSTNAMELEN];
            if (gethostname(hostName, MAXHOSTNAMELEN) != 0)
                raise_syscall(taskData, "gethostname failed", GETERROR);
            return (SAVE(C_string_to_Poly(taskData, hostName)));
        }

    case 1:
        {
            /* Look up a host name. */
            char hostName[MAXHOSTNAMELEN];
            int length;
            struct hostent *host;
            length = Poly_string_to_C(DEREFWORD(args),
                        hostName, MAXHOSTNAMELEN);
            if (length > MAXHOSTNAMELEN)
                raise_syscall(taskData, "Host name too long", ENAMETOOLONG);
            host = gethostbyname(hostName);
            if (host == NULL)
                raise_syscall(taskData, "gethostbyname failed", GETERROR);
            return makeHostEntry(taskData, host);
        }

    case 2:
        {
            /* Look up entry by address. */
            unsigned long addr =
                htonl(get_C_ulong(taskData, DEREFWORDHANDLE(args)));
            struct hostent *host;
            /* Look up a host name given an address. */
            host = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);
            if (host == NULL)
                raise_syscall(taskData, "gethostbyaddr failed", GETERROR);
            return makeHostEntry(taskData, host);
        }

    case 3:
        {
            /* Look up protocol entry. */
            char protoName[MAXHOSTNAMELEN];
            int length;
            struct protoent *proto;
            length = Poly_string_to_C(DEREFWORD(args),
                        protoName, MAXHOSTNAMELEN);
            if (length > MAXHOSTNAMELEN)
                raise_syscall(taskData, "Protocol name too long", ENAMETOOLONG);
            proto = getprotobyname(protoName);
            if (proto == NULL)
                raise_syscall(taskData, "getprotobyname failed", GETERROR);
            return makeProtoEntry(taskData, proto);
        }

    case 4:
        {
            /* Look up protocol entry. */
            struct protoent *proto;
            int pNum = get_C_ulong(taskData, DEREFWORDHANDLE(args));
            proto = getprotobynumber(pNum);
            if (proto == NULL)
                raise_syscall(taskData, "getprotobynumber failed", GETERROR);
            return makeProtoEntry(taskData, proto);
        }

    case 5:
        {
            /* Get service given service name only. */
            char servName[MAXHOSTNAMELEN];
            int length;
            struct servent *serv;
            length = Poly_string_to_C(DEREFWORD(args),
                        servName, MAXHOSTNAMELEN);
            if (length > MAXHOSTNAMELEN)
                raise_syscall(taskData, "Service name too long", ENAMETOOLONG);
            serv = getservbyname (servName, NULL);
            if (serv == NULL)
                raise_syscall(taskData, "getservbyname failed", GETERROR);
            return makeServEntry(taskData, serv);
        }

    case 6:
        {
            /* Get service given service name and protocol name. */
            char servName[MAXHOSTNAMELEN], protoName[MAXHOSTNAMELEN];
            int length;
            struct servent *serv; 
            length = Poly_string_to_C(args->WordP()->Get(0), servName, MAXHOSTNAMELEN);
            if (length > MAXHOSTNAMELEN)
                raise_syscall(taskData, "Service name too long", ENAMETOOLONG);
            length = Poly_string_to_C(args->WordP()->Get(1), protoName, MAXHOSTNAMELEN);
            if (length > MAXHOSTNAMELEN)
                raise_syscall(taskData, "Protocol name too long", ENAMETOOLONG);
            serv = getservbyname (servName, protoName);
            if (serv == NULL)
                raise_syscall(taskData, "getservbyname failed", GETERROR);
            return makeServEntry(taskData, serv);
        }

    case 7:
        {
            /* Get service given port number only. */
            struct servent *serv;
            long port = htons(get_C_ushort(taskData, DEREFWORDHANDLE(args)));
            serv = getservbyport(port, NULL);
            if (serv == NULL)
                raise_syscall(taskData, "getservbyport failed", GETERROR);
            return makeServEntry(taskData, serv);
        }

    case 8:
        {
            /* Get service given port number and protocol name. */
            char protoName[MAXHOSTNAMELEN];
            int length;
            struct servent *serv;
            long port = htons(get_C_ushort(taskData, DEREFHANDLE(args)->Get(0)));
            length = Poly_string_to_C(args->WordP()->Get(1), protoName, MAXHOSTNAMELEN);
            if (length > MAXHOSTNAMELEN)
                raise_syscall(taskData, "Protocol name too long", ENAMETOOLONG);
            serv = getservbyport (port, protoName);
            if (serv == NULL)
                raise_syscall(taskData, "getservbyport failed", GETERROR);
            return makeServEntry(taskData, serv);
        }

    case 9:
        {
            /* Get a network entry. */
            struct netent *net = NULL;
#ifdef HAVE_GETNETBYNAME
            char netName[MAXHOSTNAMELEN];
            POLYUNSIGNED length = Poly_string_to_C(DEREFWORD(args),
                        netName, MAXHOSTNAMELEN);
            if (length > MAXHOSTNAMELEN)
                raise_syscall(taskData, "Network name too long", ENAMETOOLONG);
            /* Winsock2 does not have getnetbyname.
               Just return failure. */
            net = getnetbyname(netName);
#endif
            if (net == NULL)
                raise_syscall(taskData, "getnetbyname failed", GETERROR);
            return makeNetEntry(taskData, net);
        }

    case 10:
        {
            /* Look up network entry from a number. */
            struct netent *net = NULL;
#ifdef HAVE_GETNETBYADDR
            unsigned long netNum = htonl(get_C_ulong(taskData, DEREFHANDLE(args)->Get(0)));
            long af = get_C_ulong(taskData, DEREFHANDLE(args)->Get(1));
            /* Winsock2 does not have getnetbyaddr.
               Just return failure. */
            net = getnetbyaddr(netNum, af);
#endif
            if (net == NULL)
                raise_syscall(taskData, "getnetbyaddr failed", GETERROR);
            return makeNetEntry(taskData, net);
        }

    case 11:
        {
            /* Return a list of known address families. */
            return makeList(taskData, sizeof(af_table)/sizeof(af_table[0]),
                            (char*)af_table, sizeof(af_table[0]),
                            0, mkAftab);
        }

    case 12:
        {
            /* Return a list of known socket types. */
            return makeList(taskData, sizeof(sk_table)/sizeof(sk_table[0]),
                            (char*)sk_table, sizeof(sk_table[0]),
                            0, mkSktab);
        }

    case 13: /* Return the "any" internet address. */
        return Make_unsigned(taskData, INADDR_ANY);

    case 14: /* Create a socket */
        {
            Handle str_token = make_stream_entry(taskData);
            PIOSTRUCT strm;
            int stream_no = STREAMID(str_token);
            int af = get_C_long(taskData, DEREFHANDLE(args)->Get(0));
            int type = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            int proto = get_C_long(taskData, DEREFHANDLE(args)->Get(2));
            unsigned long onOff = 1;
            SOCKET skt = socket(af, type, proto);
            if (skt == INVALID_SOCKET)
            {
                free_stream_entry(stream_no);
                switch (GETERROR)
                {
                case EMFILE: /* too many open files */
                    {
                        if (emfileFlag) /* Previously had an EMFILE error. */
                            raise_syscall(taskData, "socket failed", EMFILE);
                        emfileFlag = true;
                        FullGC(taskData); /* May clear emfileFlag if we close a file. */
                        goto TryAgain;
                    }
                case EINTR: goto TryAgain;
                default: raise_syscall(taskData, "socket failed", GETERROR);
                }
            }
            /* Set the socket to non-blocking mode. */
#ifdef WINDOWS_PC
            if (ioctlsocket(skt, FIONBIO, &onOff) != 0)
#else
            if (ioctl(skt, FIONBIO, &onOff) < 0)
#endif
            {
                free_stream_entry(stream_no);
#ifdef WINDOWS_PC
                closesocket(skt);
#else
                close(skt);
#endif
                raise_syscall(taskData, "ioctl failed", GETERROR);
            }
            strm = &basic_io_vector[stream_no];
            strm->device.sock = skt;
            strm->ioBits =
                IO_BIT_OPEN | IO_BIT_READ | IO_BIT_WRITE | IO_BIT_SOCKET ;
            return(str_token);
        }

    case 15: /* Set TCP No-delay option. */
        return setSocketOption(taskData, args, IPPROTO_TCP, TCP_NODELAY);

    case 16: /* Get TCP No-delay option. */
        return getSocketOption(taskData, args, IPPROTO_TCP, TCP_NODELAY);

    case 17: /* Set Debug option. */
        return setSocketOption(taskData, args, SOL_SOCKET, SO_DEBUG);

    case 18: /* Get Debug option. */
        return getSocketOption(taskData, args, SOL_SOCKET, SO_DEBUG);

    case 19: /* Set REUSEADDR option. */
        return setSocketOption(taskData, args, SOL_SOCKET, SO_REUSEADDR);

    case 20: /* Get REUSEADDR option. */
        return getSocketOption(taskData, args, SOL_SOCKET, SO_REUSEADDR);

    case 21: /* Set KEEPALIVE option. */
        return setSocketOption(taskData, args, SOL_SOCKET, SO_KEEPALIVE);

    case 22: /* Get KEEPALIVE option. */
        return getSocketOption(taskData, args, SOL_SOCKET, SO_KEEPALIVE);

    case 23: /* Set DONTROUTE option. */
        return setSocketOption(taskData, args, SOL_SOCKET, SO_DONTROUTE);

    case 24: /* Get DONTROUTE option. */
        return getSocketOption(taskData, args, SOL_SOCKET, SO_DONTROUTE);

    case 25: /* Set BROADCAST option. */
        return setSocketOption(taskData, args, SOL_SOCKET, SO_BROADCAST);

    case 26: /* Get BROADCAST option. */
        return getSocketOption(taskData, args, SOL_SOCKET, SO_BROADCAST);

    case 27: /* Set OOBINLINE option. */
        return setSocketOption(taskData, args, SOL_SOCKET, SO_OOBINLINE);

    case 28: /* Get OOBINLINE option. */
        return getSocketOption(taskData, args, SOL_SOCKET, SO_OOBINLINE);

    case 29: /* Set SNDBUF size. */
        return setSocketOption(taskData, args, SOL_SOCKET, SO_SNDBUF);

    case 30: /* Get SNDBUF size. */
        return getSocketInt(taskData, args, SOL_SOCKET, SO_SNDBUF);

    case 31: /* Set RCVBUF size. */
        return setSocketOption(taskData, args, SOL_SOCKET, SO_RCVBUF);

    case 32: /* Get RCVBUF size. */
        return getSocketInt(taskData, args, SOL_SOCKET, SO_RCVBUF);

    case 33: /* Get socket type e.g. SOCK_STREAM. */
        return getSocketInt(taskData, args, SOL_SOCKET, SO_TYPE);

    case 34: /* Get error status and clear it. */
        return getSocketOption(taskData, args, SOL_SOCKET, SO_ERROR);

    case 35: /* Set Linger time. */
        {
            struct linger linger;
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int lTime = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            /* We pass in a negative value to turn the option off,
               zero or positive to turn it on. */
            if (lTime < 0)
            {
                linger.l_onoff = 0;
                linger.l_linger = 0;
            }
            else
            {
                linger.l_onoff = 1;
                linger.l_linger = lTime;
            }
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (setsockopt(strm->device.sock, SOL_SOCKET, SO_LINGER,
                (char*)&linger, sizeof(linger)) != 0)
                raise_syscall(taskData, "setsockopt failed", GETERROR);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 36: /* Get Linger time. */
        {
            struct linger linger;
            PIOSTRUCT strm = get_stream(args->WordP());
            socklen_t size = sizeof(linger);
            int lTime = 0;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (getsockopt(strm->device.sock, SOL_SOCKET, SO_LINGER,
                (char*)&linger, &size) != 0)
                raise_syscall(taskData, "getsockopt failed", GETERROR);
            /* If the option is off return a negative. */
            if (linger.l_onoff == 0) lTime = -1;
            else lTime = linger.l_linger;
            return Make_arbitrary_precision(taskData, lTime);
        }

    case 37: /* Get peer name. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            struct sockaddr sockA;
            socklen_t   size = sizeof(sockA);
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (getpeername(strm->device.sock, &sockA, &size) != 0)
                raise_syscall(taskData, "getpeername failed", GETERROR);
            /* Addresses are treated as strings. */
            return(SAVE(Buffer_to_Poly(taskData, (char*)&sockA, size)));
        }

    case 38: /* Get socket name. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            struct sockaddr sockA;
            socklen_t   size = sizeof(sockA);
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (getsockname(strm->device.sock, &sockA, &size) != 0)
                raise_syscall(taskData, "getsockname failed", GETERROR);
            return(SAVE(Buffer_to_Poly(taskData, (char*)&sockA, size)));
        }

    case 39: /* Return the address family from an address. */
        {
            PolyStringObject *psAddr = (PolyStringObject *)args->WordP();
            struct sockaddr *psock = (struct sockaddr *)&psAddr->chars;
            return Make_unsigned(taskData, psock->sa_family);
        }

    case 40: /* Create a socket address from a port number and
                internet address. */
        {
            struct sockaddr_in sockaddr;
            memset(&sockaddr, 0, sizeof(sockaddr));
            sockaddr.sin_family = AF_INET;
            sockaddr.sin_port = htons(get_C_ushort(taskData, DEREFHANDLE(args)->Get(0)));
            sockaddr.sin_addr.s_addr =
                htonl(get_C_ulong(taskData, DEREFHANDLE(args)->Get(1)));
            return(SAVE(Buffer_to_Poly(taskData, (char*)&sockaddr, sizeof(sockaddr))));
        }

    case 41: /* Return port number from an internet socket address.
                Assumes that we've already checked the address family. */
        {
            PolyStringObject *psAddr = (PolyStringObject *)args->WordP();
            struct sockaddr_in *psock =
                (struct sockaddr_in *)&psAddr->chars;
            return Make_unsigned(taskData, ntohs(psock->sin_port));
        }

    case 42: /* Return internet address from an internet socket address.
                Assumes that we've already checked the address family. */
        {
            PolyStringObject * psAddr = (PolyStringObject *)args->WordP();
            struct sockaddr_in *psock =
                (struct sockaddr_in *)&psAddr->chars;
            return Make_unsigned(taskData, ntohl(psock->sin_addr.s_addr));
        }

        /* 43 - Set non-blocking mode.  Now removed. */

    case 44: /* Find number of bytes available. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            unsigned long readable;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
#ifdef WINDOWS_PC
            if (ioctlsocket(strm->device.sock, FIONREAD, &readable) != 0)
                raise_syscall(taskData, "ioctlsocket failed", GETERROR);
#else
            if (ioctl(strm->device.sock, FIONREAD, &readable) < 0)
                raise_syscall(taskData, "ioctl failed", GETERROR);
#endif
            return Make_arbitrary_precision(taskData, readable);
        }

    case 45: /* Find out if we are at the mark. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            unsigned long atMark;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
#ifdef WINDOWS_PC
            if (ioctlsocket(strm->device.sock, SIOCATMARK, &atMark) != 0)
                raise_syscall(taskData, "ioctlsocket failed", GETERROR);
#else
            if (ioctl(strm->device.sock, SIOCATMARK, &atMark) < 0)
                raise_syscall(taskData, "ioctl failed", GETERROR);
#endif
            return Make_arbitrary_precision(taskData, atMark == 0 ? 0 : 1);
        }

    case 46: /* Accept a connection. */
    case 58: /* Non-blocking accept. */
        {
            PIOSTRUCT strm = get_stream(args->WordP());
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            else {
                SOCKET sock = strm->device.sock, result;
                struct sockaddr resultAddr;
                int stream_no;
                socklen_t addrLen;
                Handle addrHandle, pair;
                Handle str_token;
                PIOSTRUCT newStrm;
                /* Get a token for the new socket - may raise an
                   exception if it fails. */
                str_token = make_stream_entry(taskData);
                stream_no = STREAMID(str_token);
                addrLen = sizeof(resultAddr);
                result = accept(sock, &resultAddr, &addrLen);

                if (result == INVALID_SOCKET)
                {
                    /* Free the stream entry */
                    free_stream_entry(stream_no);
                    switch (GETERROR)
                    {
                    case EINTR:
                        goto TryAgain; /* Have to retry if we got EINTR. */
                    case EMFILE: /* Too many files. */
                        {
                            if (emfileFlag) /* Previously had an EMFILE error. */
                                raise_syscall(taskData, "accept failed", EMFILE);
                            emfileFlag = true;
                            FullGC(taskData); /* May clear emfileFlag if we close a file. */
                            goto TryAgain;
                        }
                    case EWOULDBLOCK:
                        /* If the socket is in non-blocking mode we pass
                           this back to the caller.  If it is blocking we
                           suspend this process and try again later. */
                        if (c == 46 /* blocking version. */)
                            processes->BlockAndRestart(taskData, strm->device.sock, false, POLY_SYS_network);
                        /* else drop through. */
                    default:
                        raise_syscall(taskData, "accept failed", GETERROR);
                    }
                }

                addrHandle = SAVE(Buffer_to_Poly(taskData, (char*)&resultAddr, addrLen));
                newStrm = &basic_io_vector[stream_no];
                newStrm->device.sock = result;
                newStrm->ioBits =
                    IO_BIT_OPEN | IO_BIT_READ | IO_BIT_WRITE | IO_BIT_SOCKET;
                /* Return a pair of the new socket and the address. */
                pair = ALLOC(2);
                DEREFHANDLE(pair)->Set(0, DEREFWORDHANDLE(str_token));
                DEREFHANDLE(pair)->Set(1, DEREFWORDHANDLE(addrHandle));
                return pair;
            }
        }

    case 47: /* Bind an address to a socket. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            PolyStringObject * psAddr = (PolyStringObject *)args->WordP()->Get(1).AsObjPtr();
            struct sockaddr *psock = (struct sockaddr *)&psAddr->chars;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (bind(strm->device.sock, psock, psAddr->length) != 0)
                raise_syscall(taskData, "bind failed", GETERROR);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 48: /* Connect to an address. */
    case 59: /* Non-blocking connect. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            PolyStringObject * psAddr = (PolyStringObject *)args->WordP()->Get(1).AsObjPtr();
            struct sockaddr *psock = (struct sockaddr *)&psAddr->chars;
            int res;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            /* In Windows, and possibly also in Unix, if we have
               received a previous EWOULDBLOCK we have to use "select"
               to tell us whether the connection actually succeeded. */
            while (1)
            {
                if (strm->ioBits & IO_BIT_INPROGRESS)
                {
                    fd_set read_fds, write_fds, except_fds;
                    struct timeval delay;
                    int sel;
                    SOCKET sock = strm->device.sock;
                    FD_ZERO(&read_fds);
                    FD_ZERO(&write_fds);
                    FD_ZERO(&except_fds);
                    FD_SET(sock, &write_fds);
                    FD_SET(sock, &except_fds);
                    delay.tv_sec  = 0; /* Poll. */
                    delay.tv_usec = 0;
                    /* In Windows failure is indicated by the bit being set in
                       the exception set rather than the write set. */
                    sel = select(FD_SETSIZE,&read_fds,&write_fds,&except_fds,&delay);
                    if (sel < 0)
                    {
                        int err = GETERROR;
                        if (err != EINTR)
                            raise_syscall(taskData, "select failed", err);
                        /* else continue */
                    }
                    else if (sel == 0)
                    {
                        /* Nothing yet */
                        processes->BlockAndRestart(taskData, -1, false, POLY_SYS_network);
                            /* -1 => not for reading. */
                    }
                    else /* Definite result. */
                    {
                        int result = 0;
                        socklen_t len = sizeof(result);
                        strm->ioBits &= ~IO_BIT_INPROGRESS; /* No longer in progress. */
                        if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)&result, &len) != 0
                            || result != 0)
                            raise_syscall(taskData, "connect failed", MAPERROR(result));
                        return Make_arbitrary_precision(taskData, 0); /* Success. */
                    }
                }
                else
                {
                    int err;
                    res = connect(strm->device.sock, psock, psAddr->length);
                    if (res == 0) return Make_arbitrary_precision(taskData, 0); /* OK */
                    /* It isn't clear that EINTR can ever occur with
                       connect, but just to be safe, we retry. */
                    err = GETERROR;
                    if ((err == EWOULDBLOCK || err == EINPROGRESS) && c == 48 /*blocking version*/)
                    {
                        strm->ioBits |= IO_BIT_INPROGRESS;
                        processes->BlockAndRestart(taskData, -1, false, POLY_SYS_network);
                            /* -1 => not for reading. */
                    }
                    else if (err != EINTR)
                        raise_syscall(taskData, "connect failed", err);
                    /* else try again. */
                }
            }
        }

    case 49: /* Put socket into listening mode. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int backlog = get_C_ulong(taskData, DEREFHANDLE(args)->Get(1));
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (listen(strm->device.sock, backlog) != 0)
                raise_syscall(taskData, "listen failed", GETERROR);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 50: /* Shutdown the socket. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            int mode = 0;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            switch (get_C_ulong(taskData, DEREFHANDLE(args)->Get(1)))
            {
            case 1: mode = SHUT_RD; break;
            case 2: mode = SHUT_WR; break;
            case 3: mode = SHUT_RDWR;
            }
            if (shutdown(strm->device.sock, mode) != 0)
                raise_syscall(taskData, "shutdown failed", GETERROR);
            return Make_arbitrary_precision(taskData, 0);
        }

    case 51: /* Send data on a socket. */
    case 60: /* Non-blocking send. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            PolyWord pBase = DEREFHANDLE(args)->Get(1);
            char    ch, *base;
            unsigned int offset = get_C_ulong(taskData, DEREFHANDLE(args)->Get(2));
            unsigned int length = get_C_ulong(taskData, DEREFHANDLE(args)->Get(3));
            unsigned int dontRoute = get_C_ulong(taskData, DEREFHANDLE(args)->Get(4));
            unsigned int outOfBand = get_C_ulong(taskData, DEREFHANDLE(args)->Get(5));
            int flags = 0, sent;
            if (dontRoute != 0) flags |= MSG_DONTROUTE;
            if (outOfBand != 0) flags |= MSG_OOB;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (IS_INT(pBase)) {
                /* Handle the special case where we are sending a single
                   byte vector and the "address" is the tagged byte itself. */
                ch = (char)UNTAGGED(pBase);
                base = &ch;
                offset = 0;
                length = 1;
            }
            else base = (char*)pBase.AsObjPtr()->AsBytePtr();

            while (1)
            {
                int err;
                sent = send(strm->device.sock, base+offset, length, flags);
                /* It isn't clear that EINTR can ever occur with
                   send but just to be safe we deal with that case and
                   retry the send. */
                if (sent != SOCKET_ERROR) /* OK. */
                    return Make_arbitrary_precision(taskData, sent);
                err = GETERROR;
                if (err == EWOULDBLOCK && c == 51 /* blocking */)
                {
                    processes->BlockAndRestart(taskData, -1, false, POLY_SYS_network);
                        /* -1 => not for reading */
                    ASSERT(0); /* Must not have returned. */
                }
                else if (err != EINTR)
                    raise_syscall(taskData, "send failed", err);
                /* else try again */
            }
        }

    case 52: /* Send data on a socket to a given address. */
    case 61: /* Non-blocking send. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            PolyStringObject * psAddr = (PolyStringObject *)args->WordP()->Get(1).AsObjPtr();
            PolyWord pBase = DEREFHANDLE(args)->Get(2);
            char    ch, *base;
            unsigned int offset = get_C_ulong(taskData, DEREFHANDLE(args)->Get(3));
            unsigned int length = get_C_ulong(taskData, DEREFHANDLE(args)->Get(4));
            unsigned int dontRoute = get_C_ulong(taskData, DEREFHANDLE(args)->Get(5));
            unsigned int outOfBand = get_C_ulong(taskData, DEREFHANDLE(args)->Get(6));
            int flags = 0, sent;
            if (dontRoute != 0) flags |= MSG_DONTROUTE;
            if (outOfBand != 0) flags |= MSG_OOB;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
            if (IS_INT(pBase)) {
                /* Handle the special case where we are sending a single
                   byte vector and the "address" is the tagged byte itself. */
                ch = (char)UNTAGGED(pBase);
                base = &ch;
                offset = 0;
                length = 1;
            }
            else base = (char*)pBase.AsObjPtr()->AsBytePtr();

            while (1)
            {
                int err;
                sent = sendto(strm->device.sock, base+offset, length, flags,
                            (struct sockaddr *)psAddr->chars, psAddr->length);
                /* It isn't clear that EINTR can ever occur with
                   send but just to be safe we deal with that case and
                   retry the send. */
                if (sent != SOCKET_ERROR) /* OK. */
                    return Make_arbitrary_precision(taskData, sent);
                err = GETERROR;
                if (err == EWOULDBLOCK && c == 52 /* blocking */)
                {
                    processes->BlockAndRestart(taskData, -1, false, POLY_SYS_network);
                    ASSERT(0); /* Must not have returned. */
                }
                else if (err != EINTR)
                    raise_syscall(taskData, "sendto failed", err);
                /* else try again */
            }
        }

    case 53: /* Receive data into an array. */
    case 62: /* Non-blocking receive. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            char *base = (char*)DEREFHANDLE(args)->Get(1).AsObjPtr()->AsBytePtr();
            unsigned int offset = get_C_ulong(taskData, DEREFHANDLE(args)->Get(2));
            unsigned int length = get_C_ulong(taskData, DEREFHANDLE(args)->Get(3));
            unsigned int peek = get_C_ulong(taskData, DEREFHANDLE(args)->Get(4));
            unsigned int outOfBand = get_C_ulong(taskData, DEREFHANDLE(args)->Get(5));
            int flags = 0, recvd;
            if (peek != 0) flags |= MSG_PEEK;
            if (outOfBand != 0) flags |= MSG_OOB;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);

            while (1) {
                int err;
                recvd = recv(strm->device.sock, base+offset, length, flags);
                err = GETERROR;
                if (recvd != SOCKET_ERROR) { /* OK. */
                    /* It appears that recv may return the length of the
                       message if that is longer than the buffer. */
                    if (recvd > (int)length) recvd = length;
                    return Make_arbitrary_precision(taskData, recvd);
                }
                if (err == EWOULDBLOCK && c == 53 /* blocking */)
                {
                    /* Block until something arrives. */
                    processes->BlockAndRestart(taskData, outOfBand ? -1 : strm->device.sock,
                        false, POLY_SYS_network);
                    ASSERT(0); /* Must not have returned. */
                }
                else if (err != EINTR)
                    raise_syscall(taskData, "recv failed", err);
                /* else try again */
            }
        }

    case 54: /* Receive data into an array and return the sender's
                address along with the length.  In Windows this can
                only be used with datagrams. */
    case 63: /* Non-blocking receive. */
        {
            PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
            char *base = (char*)DEREFHANDLE(args)->Get(1).AsObjPtr()->AsBytePtr();
            unsigned int offset = get_C_ulong(taskData, DEREFHANDLE(args)->Get(2));
            unsigned int length = get_C_ulong(taskData, DEREFHANDLE(args)->Get(3));
            unsigned int peek = get_C_ulong(taskData, DEREFHANDLE(args)->Get(4));
            unsigned int outOfBand = get_C_ulong(taskData, DEREFHANDLE(args)->Get(5));
            int flags = 0, recvd;
            socklen_t addrLen;
            struct sockaddr resultAddr;

            if (peek != 0) flags |= MSG_PEEK;
            if (outOfBand != 0) flags |= MSG_OOB;
            if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);

            while (1) {
                int err;
                recvd = recvfrom(strm->device.sock, base+offset,
                                 length, flags, &resultAddr, &addrLen);
                err = GETERROR;

                if (recvd != SOCKET_ERROR) { /* OK. */
                    Handle addrHandle, lengthHandle, pair;
                    if (recvd > (int)length) recvd = length;
                    lengthHandle = Make_arbitrary_precision(taskData, recvd);
                    addrHandle = SAVE(Buffer_to_Poly(taskData, (char*)&resultAddr, addrLen));
                    pair = ALLOC(2);
                    DEREFHANDLE(pair)->Set(0, DEREFWORDHANDLE(lengthHandle));
                    DEREFHANDLE(pair)->Set(1, DEREFWORDHANDLE(addrHandle));
                    return pair;
                }
                if (err == EWOULDBLOCK && c == 54 /* blocking */)
                {
                    processes->BlockAndRestart(taskData, outOfBand ? -1 : strm->device.sock,
                        false, POLY_SYS_network);
                    ASSERT(0); /* Must not have returned. */
                }
                else if (err != EINTR)
                    raise_syscall(taskData, "recvfrom failed", err);
                /* else try again */
            }
        }

    case 55: /* Create a socket pair. */
#ifdef WINDOWS_PC
        /* Not implemented. */
        raise_syscall(taskData, "socketpair not implemented", -WSAEAFNOSUPPORT);
#else
        {
            Handle str_token1 = make_stream_entry(taskData);
            Handle str_token2 = make_stream_entry(taskData);
            Handle pair;
            PIOSTRUCT strm1, strm2;
            int stream_no1 = STREAMID(str_token1);
            int stream_no2 = STREAMID(str_token2);
            int af = get_C_long(taskData, DEREFHANDLE(args)->Get(0));
            int type = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
            int proto = get_C_long(taskData, DEREFHANDLE(args)->Get(2));
            int onOff = 1;
            SOCKET skt[2];
            if (socketpair(af, type, proto, skt) != 0)
            {
                free_stream_entry(stream_no1);
                free_stream_entry(stream_no2);
                switch (GETERROR)
                {
                case EMFILE: /* too many open files */
                    {
                        if (emfileFlag) /* Previously had an EMFILE error. */
                            raise_syscall(taskData, "socket failed", EMFILE);
                        emfileFlag = true;
                        FullGC(taskData); /* May clear emfileFlag if we close a file. */
                        goto TryAgain;
                    }
                case EINTR: goto TryAgain;
                default: raise_syscall(taskData, "socketpair failed", GETERROR);
                }
            }
            /* Set the sockets to non-blocking mode. */
            if (ioctl(skt[0], FIONBIO, &onOff) < 0 ||
                ioctl(skt[1], FIONBIO, &onOff) < 0)
            {
                free_stream_entry(stream_no1);
                free_stream_entry(stream_no2);
                close(skt[0]);
                close(skt[1]);
                raise_syscall(taskData, "ioctl failed", GETERROR);
            }
            strm1 = &basic_io_vector[stream_no1];
            strm1->device.sock = skt[0];
            strm1->ioBits =
                IO_BIT_OPEN | IO_BIT_READ | IO_BIT_WRITE | IO_BIT_SOCKET ;
            strm2 = &basic_io_vector[stream_no2];
            strm2->device.sock = skt[1];
            strm2->ioBits =
                IO_BIT_OPEN | IO_BIT_READ | IO_BIT_WRITE | IO_BIT_SOCKET ;
            /* Return the two streams as a pair. */
            pair = ALLOC(2);
            DEREFHANDLE(pair)->Set(0, DEREFWORDHANDLE(str_token1));
            DEREFHANDLE(pair)->Set(1, DEREFWORDHANDLE(str_token2));
            return pair;
        }
#endif

    case 56: /* Create a Unix socket address from a string. */
#ifdef WINDOWS_PC
        /* Not implemented. */
        raise_syscall(taskData, "Unix addresses not implemented", -WSAEAFNOSUPPORT);
#else
        {
            struct sockaddr_un addr;
            int length;
            memset(&addr, 0, sizeof(addr));
            addr.sun_family = AF_UNIX;
#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
            addr.sun_len = sizeof(addr); // Used in FreeBSD only.
#endif
            length = Poly_string_to_C(DEREFWORD(args),
                        addr.sun_path, sizeof(addr.sun_path));
            if (length > (int)sizeof(addr.sun_path))
                raise_syscall(taskData, "Address too long", ENAMETOOLONG);
            return SAVE(Buffer_to_Poly(taskData, (char*)&addr, sizeof(addr)));
        }
#endif

    case 57: /* Get the file name from a Unix socket address. */
#ifdef WINDOWS_PC
        /* Not implemented. */
        raise_syscall(taskData, "Unix addresses not implemented", -WSAEAFNOSUPPORT);
#else
        {
            PolyStringObject * psAddr = (PolyStringObject *)args->WordP();
            struct sockaddr_un *psock = (struct sockaddr_un *)&psAddr->chars;
            return SAVE(C_string_to_Poly(taskData, psock->sun_path));
        }
#endif

    case 64: /* Blocking select call. Infinite timeout. */
        return selectCall(taskData, args, 1);

    case 65: /* Polling select call. Zero timeout. */
        return selectCall(taskData, args, 2);

    case 66: /* Select call with non-zero timeout. */
        return selectCall(taskData, args, 0);


    default:
        {
            char msg[100];
            sprintf(msg, "Unknown net function: %d", c);
            raise_exception_string(taskData, EXC_Fail, msg);
			return 0;
        }
    }
}

/* "Polymorphic" function to generate a list. */
static Handle makeList(TaskData *taskData, int count, char *p, int size, void *arg,
                       Handle (mkEntry)(TaskData *, void*, char*))
{
    Handle saved = taskData->saveVec.mark();
    Handle list = SAVE(ListNull);
    /* Start from the end of the list. */
    p += count*size;
    while (count > 0)
    {
        Handle value, next;
        p -= size; /* Back up to the last entry. */
        value = mkEntry(taskData, arg, p);
        next  = ALLOC(SIZEOF(ML_Cons_Cell));

        DEREFLISTHANDLE(next)->h = DEREFWORDHANDLE(value); 
        DEREFLISTHANDLE(next)->t = DEREFLISTHANDLE(list);

        taskData->saveVec.reset(saved);
        list = SAVE(DEREFHANDLE(next));
        count--;
    }
    return list;
}

static Handle mkAddr(TaskData *taskData, void *arg, char *p)
{
    int j;
    struct hostent *host = (struct hostent *)arg;
    unsigned long addr = 0;
    /* Addresses are in network order so this is fairly easy.
       In practice they will be 4 byte entries so we could
       just use ntohl. */
    for (j = 0; j < host->h_length; j++)
        addr = (addr << 8) | ((*(char**)p)[j] & 255);
    return Make_unsigned(taskData, addr);
}

/* Convert a host entry into a tuple for ML. */
static Handle makeHostEntry(TaskData *taskData, struct hostent *host)
{
    /* We need to do all this in the right order.  We cannot
       construct the result tuple until all the values are
       ready.  We have to save each entry on the save stack
       just in case of a garbage collection. */
    int i;
    char **p;
    Handle aliases, name, addrType, result;
    Handle addrList = SAVE(ListNull);

    /* Canonical name. */
    name = SAVE(C_string_to_Poly(taskData, host->h_name));

    /* Aliases. */
    for (i=0, p = host->h_aliases; *p != NULL; p++, i++);
    aliases = convert_string_list(taskData, i, host->h_aliases);

    /* Address type. */
    addrType = Make_unsigned(taskData, host->h_addrtype);

    /* Addresses. */
    /* Count them first and then work from the end back. */
    for (i=0, p = host->h_addr_list; *p != NULL; p++, i++);
    addrList = makeList(taskData, i, (char*)host->h_addr_list, sizeof(char*), host, mkAddr);

    /* Make the result structure. */
    result = ALLOC(4);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(name));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(aliases));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(addrType));
    DEREFHANDLE(result)->Set(3, DEREFWORDHANDLE(addrList));
    return result;
}

static Handle makeProtoEntry(TaskData *taskData, struct protoent *proto)
{
    int i;
    char **p;
    Handle aliases, name, protocol, result;

    /* Canonical name. */
    name = SAVE(C_string_to_Poly(taskData, proto->p_name));

    /* Aliases. */
    for (i=0, p = proto->p_aliases; *p != NULL; p++, i++);
    aliases = convert_string_list(taskData, i, proto->p_aliases);

    /* Protocol number. */
    protocol = Make_unsigned(taskData, proto->p_proto);

    /* Make the result structure. */
    result = ALLOC(3);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(name));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(aliases));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(protocol));
    return result;
}

static Handle makeServEntry(TaskData *taskData, struct servent *serv)
{
    int i;
    char **p;
    Handle aliases, name, protocol, result, port;

    /* Canonical name. */
    name = SAVE(C_string_to_Poly(taskData, serv->s_name));

    /* Aliases. */
    for (i=0, p = serv->s_aliases; *p != NULL; p++, i++);
    aliases = convert_string_list(taskData, i, serv->s_aliases);

    /* Port number. */
    port = Make_unsigned(taskData, ntohs(serv->s_port));

    /* Protocol name. */
    protocol = SAVE(C_string_to_Poly(taskData, serv->s_proto));

    /* Make the result structure. */
    result = ALLOC(4);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(name));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(aliases));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(port));
    DEREFHANDLE(result)->Set(3, DEREFWORDHANDLE(protocol));
    return result;
}

static Handle makeNetEntry(TaskData *taskData, struct netent *net)
{
    int i;
    char **p;
    Handle aliases, name, addrType, result, network;

    /* Canonical name. */
    name = SAVE(C_string_to_Poly(taskData, net->n_name));

    /* Aliases. */
    for (i=0, p = net->n_aliases; *p != NULL; p++, i++);
    aliases = convert_string_list(taskData, i, net->n_aliases);

    /* Address type. */
    addrType = Make_unsigned(taskData, net->n_addrtype);

    /* Protocol name. */
    network = Make_unsigned(taskData, ntohl(net->n_net));

    /* Make the result structure. */
    result = ALLOC(4);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(name));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(aliases));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(addrType));
    DEREFHANDLE(result)->Set(3, DEREFWORDHANDLE(network));
    return result;
}

static Handle mkAftab(TaskData *taskData, void *arg, char *p)
{
    struct af_tab_struct *af = (struct af_tab_struct *)p;
    Handle result, name, num;
    /* Construct a pair of the string and the number. */
    name = SAVE(C_string_to_Poly(taskData, af->af_name));
    num = Make_unsigned(taskData, af->af_num);
    result = ALLOC(2);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(name));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(num));
    return result;
}

static Handle mkSktab(TaskData *taskData, void *arg, char *p)
{
    struct sk_tab_struct *sk = (struct sk_tab_struct *)p;
    Handle result, name, num;
    /* Construct a pair of the string and the number. */
    name = SAVE(C_string_to_Poly(taskData, sk->sk_name));
    num = Make_unsigned(taskData, sk->sk_num);
    result = ALLOC(2);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(name));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(num));
    return result;
}

/* This sets an option and can also be used to set an integer. */
static Handle setSocketOption(TaskData *taskData, Handle args, int level, int opt)
{
    PIOSTRUCT strm = get_stream(DEREFHANDLE(args)->Get(0).AsObjPtr());
    int onOff = get_C_long(taskData, DEREFHANDLE(args)->Get(1));
    if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
    if (setsockopt(strm->device.sock, level, opt,
        (char*)&onOff, sizeof(int)) != 0)
        raise_syscall(taskData, "setsockopt failed", GETERROR);
    return Make_arbitrary_precision(taskData, 0);
}

/* Get a socket option as a boolean */
static Handle getSocketOption(TaskData *taskData, Handle args, int level, int opt)
{
    PIOSTRUCT strm = get_stream(args->WordP());
    int onOff = 0;
    socklen_t size = sizeof(int);
    if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
    if (getsockopt(strm->device.sock, level, opt,
        (char*)&onOff, &size) != 0)
        raise_syscall(taskData, "getsockopt failed", GETERROR);
    return Make_arbitrary_precision(taskData, onOff == 0 ? 0 : 1);
}

/* Get a socket option as an integer */
static Handle getSocketInt(TaskData *taskData, Handle args, int level, int opt)
{
    PIOSTRUCT strm = get_stream(args->WordP());
    int optVal = 0;
    socklen_t size = sizeof(int);
    if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF);
    if (getsockopt(strm->device.sock, level, opt,
        (char*)&optVal, &size) != 0)
        raise_syscall(taskData, "getsockopt failed", GETERROR);
    return Make_arbitrary_precision(taskData, optVal);
}

/* Helper function for selectCall.  Creates the result vector of active sockets. */
static Handle getSelectResult(TaskData *taskData, Handle args, int offset, fd_set *pFds)
{
    /* Construct the result vectors. */
    PolyObject *inVec = DEREFHANDLE(args)->Get(offset).AsObjPtr();
    int nVec = OBJECT_LENGTH(inVec);
    int nRes = 0;
    int i;
    for (i = 0; i < nVec; i++) {
        PIOSTRUCT strm = get_stream(inVec->Get(i).AsObjPtr());
        if (FD_ISSET(strm->device.sock, pFds)) nRes++;
    }
    if (nRes == 0)
        return SAVE(EmptyString()); /* None - return empty vector. */
    else {
        Handle result = ALLOC(nRes);
        inVec = DEREFHANDLE(args)->Get(offset).AsObjPtr(); /* It could have moved as a result of a gc. */
        nRes = 0;
        for (i = 0; i < nVec; i++) {
            PIOSTRUCT strm = get_stream(inVec->Get(i).AsObjPtr());
            if (FD_ISSET(strm->device.sock, pFds))
                DEREFWORDHANDLE(result)->Set(nRes++, inVec->Get(i));
        }
        return result;
    }
}

/* Wrapper for "select" call.  The arguments are arrays of socket ids.  These arrays are
   updated so that "active" sockets are left unchanged and inactive sockets are set to
   minus one.  */
static Handle selectCall(TaskData *taskData, Handle args, int blockType)
{
    fd_set readers, writers, excepts;
    struct timeval timeout;
    int i, nVec, selectRes;
    Handle rdResult, wrResult, exResult, result;
    /* Set up the bitmaps for the select call from the arrays. */
    PolyObject *readVec = DEREFHANDLE(args)->Get(0).AsObjPtr();
    PolyObject *writeVec = DEREFHANDLE(args)->Get(1).AsObjPtr();
    PolyObject *excVec = DEREFHANDLE(args)->Get(2).AsObjPtr();
    FD_ZERO(&readers);
    FD_ZERO(&writers);
    FD_ZERO(&excepts);
    nVec = OBJECT_LENGTH(readVec);
    for (i = 0; i < nVec; i++) {
        PIOSTRUCT strm = get_stream(readVec->Get(i).AsObjPtr());
        if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF); 
        FD_SET(strm->device.sock, &readers);
    }
    nVec = OBJECT_LENGTH(writeVec);
    for (i = 0; i < nVec; i++) {
        PIOSTRUCT strm = get_stream(writeVec->Get(i).AsObjPtr());
        if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF); 
        FD_SET(strm->device.sock, &writers);
    }
    nVec = OBJECT_LENGTH(excVec);
    for (i = 0; i < nVec; i++) {
        PIOSTRUCT strm = get_stream(excVec->Get(i).AsObjPtr());
        if (strm == NULL) raise_syscall(taskData, "Stream is closed", EBADF); 
        FD_SET(strm->device.sock, &excepts);
    }
    /* Whatever the timeout specified we simply poll here. */
    memset(&timeout, 0, sizeof(timeout));
    selectRes = select(FD_SETSIZE, &readers, &writers, &excepts, &timeout);
    if (selectRes < 0) raise_syscall(taskData, "select failed", GETERROR);

    if (selectRes == 0) { /* Timed out.  Have to look at the timeout value. */
        switch (blockType)
        {
        case 0: /* Check the timeout. */
            {
            /* The time argument is an absolute time. */
#ifdef WINDOWS_PC
            FILETIME ftTime, ftNow;
            /* Get the file time. */
            get_C_pair(taskData, DEREFHANDLE(args)->Get(3),
                &ftTime.dwHighDateTime, &ftTime.dwLowDateTime);
			GetSystemTimeAsFileTime(&ftNow);
            /* If the timeout time is earlier than the current time
               we must return, otherwise we block. */
            if (CompareFileTime(&ftTime, &ftNow) <= 0)
                break; /* Return the empty set. */
            /* else drop through and block. */
#else /* Unix */
            struct timeval tv;
            /* We have a value in microseconds.  We need to split
               it into seconds and microseconds. */
            Handle hTime = SAVE(DEREFWORDHANDLE(args)->Get(3));
            Handle hMillion = Make_arbitrary_precision(taskData, 1000000);
            unsigned long secs =
                get_C_ulong(taskData, DEREFWORDHANDLE(div_longc(taskData, hMillion, hTime)));
            unsigned long usecs =
                get_C_ulong(taskData, DEREFWORDHANDLE(rem_longc(taskData, hMillion, hTime)));
            /* If the timeout time is earlier than the current time
               we must return, otherwise we block. */
            if (gettimeofday(&tv, NULL) != 0)
                raise_syscall(taskData, "gettimeofday failed", errno);
            if ((unsigned long)tv.tv_sec > secs ||
                ((unsigned long)tv.tv_sec == secs && (unsigned long)tv.tv_usec >= usecs))
                break;
            /* else block. */
#endif
        }
        case 1: /* Block until one of the descriptors is ready. */
            processes->BlockAndRestart(taskData, -1, false, POLY_SYS_network);
            /*NOTREACHED*/
        case 2: /* Just a simple poll - drop through. */
            break;
        }
    }

    /* Construct the result vectors. */
    rdResult = getSelectResult(taskData, args, 0, &readers);
    wrResult = getSelectResult(taskData, args, 1, &writers);
    exResult = getSelectResult(taskData, args, 2, &excepts);

    result = ALLOC(3);
    DEREFHANDLE(result)->Set(0, DEREFWORDHANDLE(rdResult));
    DEREFHANDLE(result)->Set(1, DEREFWORDHANDLE(wrResult));
    DEREFHANDLE(result)->Set(2, DEREFWORDHANDLE(exResult));
    return result;
}

class Networking: public RtsModule
{
public:
    virtual void Init(void);
    virtual void Uninit(void);
};

// Declare this.  It will be automatically added to the table.
static Networking networkingModule;

void Networking::Init(void)
{
#ifdef WINDOWS_PC
#ifdef USEWINSOCK2
#define WINSOCK_MAJOR_VERSION   2
#define WINSOCK_MINOR_VERSION   2
#else
#define WINSOCK_MAJOR_VERSION   1
#define WINSOCK_MINOR_VERSION   1
#endif
    WSADATA wsaData;
    WORD wVersion = MAKEWORD(WINSOCK_MINOR_VERSION, WINSOCK_MAJOR_VERSION);
    /* Initialise the system and check that the version it supplied
       is the one we requested. */
    if(WSAStartup(wVersion, &wsaData) == 0)
    {
        if (wsaData.wVersion == wVersion)
            winsock_init = 1;
        else WSACleanup();
    }
#endif
}

void Networking::Uninit(void)
{
#ifdef WINDOWS_PC
    if (winsock_init) WSACleanup();
    winsock_init = 0;
#endif
}