File: socket.c

package info (click to toggle)
lilypond 2.19.83-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 76,396 kB
  • sloc: cpp: 79,385; ansic: 75,071; lisp: 59,263; xml: 31,196; python: 22,406; sh: 8,159; yacc: 3,872; perl: 3,507; asm: 1,587; lex: 1,408; makefile: 1,048; tcl: 457; awk: 199; php: 53; csh: 50
file content (1816 lines) | stat: -rw-r--r-- 53,727 bytes parent folder | download | duplicates (5)
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
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 * 
 * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
 */




#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <errno.h>
#include <gmp.h>

#include "libguile/_scm.h"
#include "libguile/unif.h"
#include "libguile/feature.h"
#include "libguile/fports.h"
#include "libguile/strings.h"
#include "libguile/vectors.h"
#include "libguile/dynwind.h"

#include "libguile/validate.h"
#include "libguile/socket.h"

#include "libguile/iselect.h"

#ifdef __MINGW32__
#include "win32-socket.h"
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#else
#include <sys/socket.h>
#ifdef HAVE_UNIX_DOMAIN_SOCKETS
#include <sys/un.h>
#endif
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif

#if defined (HAVE_UNIX_DOMAIN_SOCKETS) && !defined (SUN_LEN)
#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
		      + strlen ((ptr)->sun_path))
#endif

/* The largest possible socket address.  Wrapping it in a union guarantees
   that the compiler will make it suitably aligned.  */
typedef union
{
  struct sockaddr     sockaddr;
  struct sockaddr_in  sockaddr_in;

#ifdef HAVE_UNIX_DOMAIN_SOCKETS
  struct sockaddr_un  sockaddr_un;
#endif
#ifdef HAVE_IPV6
  struct sockaddr_in6 sockaddr_in6;
#endif
} scm_t_max_sockaddr;


/* Maximum size of a socket address.  */
#define MAX_ADDR_SIZE   (sizeof (scm_t_max_sockaddr))




SCM_DEFINE (scm_htons, "htons", 1, 0, 0, 
            (SCM value),
	    "Convert a 16 bit quantity from host to network byte ordering.\n"
	    "@var{value} is packed into 2 bytes, which are then converted\n"
	    "and returned as a new integer.")
#define FUNC_NAME s_scm_htons
{
  return scm_from_ushort (htons (scm_to_ushort (value)));
}
#undef FUNC_NAME

SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0, 
            (SCM value),
	    "Convert a 16 bit quantity from network to host byte ordering.\n"
	    "@var{value} is packed into 2 bytes, which are then converted\n"
	    "and returned as a new integer.")
#define FUNC_NAME s_scm_ntohs
{
  return scm_from_ushort (ntohs (scm_to_ushort (value)));
}
#undef FUNC_NAME

SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0, 
            (SCM value),
	    "Convert a 32 bit quantity from host to network byte ordering.\n"
	    "@var{value} is packed into 4 bytes, which are then converted\n"
	    "and returned as a new integer.")
#define FUNC_NAME s_scm_htonl
{
  return scm_from_ulong (htonl (scm_to_uint32 (value)));
}
#undef FUNC_NAME

SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0, 
            (SCM value),
	    "Convert a 32 bit quantity from network to host byte ordering.\n"
	    "@var{value} is packed into 4 bytes, which are then converted\n"
	    "and returned as a new integer.")
#define FUNC_NAME s_scm_ntohl
{
  return scm_from_ulong (ntohl (scm_to_uint32 (value)));
}
#undef FUNC_NAME

#ifndef HAVE_INET_ATON
/* for our definition in inet_aton.c, not usually needed.  */
extern int inet_aton ();
#endif

SCM_DEFINE (scm_inet_aton, "inet-aton", 1, 0, 0, 
            (SCM address),
	    "Convert an IPv4 Internet address from printable string\n"
	    "(dotted decimal notation) to an integer.  E.g.,\n\n"
	    "@lisp\n"
	    "(inet-aton \"127.0.0.1\") @result{} 2130706433\n"
	    "@end lisp")
#define FUNC_NAME s_scm_inet_aton
{
  struct in_addr soka;
  char *c_address;
  int rv;

  c_address = scm_to_locale_string (address);
  rv = inet_aton (c_address, &soka);
  free (c_address);
  if (rv == 0)
    SCM_MISC_ERROR ("bad address", SCM_EOL);
  return scm_from_ulong (ntohl (soka.s_addr));
}
#undef FUNC_NAME


SCM_DEFINE (scm_inet_ntoa, "inet-ntoa", 1, 0, 0, 
            (SCM inetid),
	    "Convert an IPv4 Internet address to a printable\n"
	    "(dotted decimal notation) string.  E.g.,\n\n"
	    "@lisp\n"
	    "(inet-ntoa 2130706433) @result{} \"127.0.0.1\"\n"
	    "@end lisp")
#define FUNC_NAME s_scm_inet_ntoa
{
  struct in_addr addr;
  char *s;
  SCM answer;
  addr.s_addr = htonl (SCM_NUM2ULONG (1, inetid));
  s = inet_ntoa (addr);
  answer = scm_from_locale_string (s);
  return answer;
}
#undef FUNC_NAME

#ifdef HAVE_INET_NETOF
SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0, 
            (SCM address),
	    "Return the network number part of the given IPv4\n"
	    "Internet address.  E.g.,\n\n"
	    "@lisp\n"
	    "(inet-netof 2130706433) @result{} 127\n"
	    "@end lisp")
#define FUNC_NAME s_scm_inet_netof
{
  struct in_addr addr;
  addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
  return scm_from_ulong (inet_netof (addr));
}
#undef FUNC_NAME
#endif

#ifdef HAVE_INET_LNAOF
SCM_DEFINE (scm_lnaof, "inet-lnaof", 1, 0, 0, 
            (SCM address),
	    "Return the local-address-with-network part of the given\n"
	    "IPv4 Internet address, using the obsolete class A/B/C system.\n"
	    "E.g.,\n\n"
	    "@lisp\n"
	    "(inet-lnaof 2130706433) @result{} 1\n"
	    "@end lisp")
#define FUNC_NAME s_scm_lnaof
{
  struct in_addr addr;
  addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
  return scm_from_ulong (inet_lnaof (addr));
}
#undef FUNC_NAME
#endif

#ifdef HAVE_INET_MAKEADDR
SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
            (SCM net, SCM lna),
	    "Make an IPv4 Internet address by combining the network number\n"
	    "@var{net} with the local-address-within-network number\n"
	    "@var{lna}.  E.g.,\n\n"
	    "@lisp\n"
	    "(inet-makeaddr 127 1) @result{} 2130706433\n"
	    "@end lisp")
#define FUNC_NAME s_scm_inet_makeaddr
{
  struct in_addr addr;
  unsigned long netnum;
  unsigned long lnanum;

  netnum = SCM_NUM2ULONG (1, net);
  lnanum = SCM_NUM2ULONG (2, lna);
  addr = inet_makeaddr (netnum, lnanum);
  return scm_from_ulong (ntohl (addr.s_addr));
}
#undef FUNC_NAME
#endif

#ifdef HAVE_IPV6

/* flip a 128 bit IPv6 address between host and network order.  */
#ifdef WORDS_BIGENDIAN
#define FLIP_NET_HOST_128(addr)
#else
#define FLIP_NET_HOST_128(addr)\
{\
  int i;\
  \
  for (i = 0; i < 8; i++)\
    {\
      scm_t_uint8 c = (addr)[i];\
      \
      (addr)[i] = (addr)[15 - i];\
      (addr)[15 - i] = c;\
    }\
}
#endif

#ifdef WORDS_BIGENDIAN
#define FLIPCPY_NET_HOST_128(dest, src) memcpy (dest, src, 16)
#else
#define FLIPCPY_NET_HOST_128(dest, src) \
{ \
  const scm_t_uint8 *tmp_srcp = (src) + 15; \
  scm_t_uint8 *tmp_destp = (dest); \
  \
  do { \
    *tmp_destp++ = *tmp_srcp--; \
  } while (tmp_srcp != (src)); \
}
#endif


#if (SIZEOF_SCM_T_BITS * SCM_CHAR_BIT) > 128
#error "Assumption that scm_t_bits <= 128 bits has been violated."
#endif

#if (SIZEOF_UNSIGNED_LONG * SCM_CHAR_BIT) > 128
#error "Assumption that unsigned long <= 128 bits has been violated."
#endif

#if (SIZEOF_UNSIGNED_LONG_LONG * SCM_CHAR_BIT) > 128
#error "Assumption that unsigned long long <= 128 bits has been violated."
#endif

/* convert a 128 bit IPv6 address in network order to a host ordered
   SCM integer.  */
static SCM
scm_from_ipv6 (const scm_t_uint8 *src)
{
  SCM result = scm_i_mkbig ();
  mpz_import (SCM_I_BIG_MPZ (result),
              1,  /* chunk */
              1,  /* big-endian chunk ordering */
              16, /* chunks are 16 bytes long */
              1,  /* big-endian byte ordering */
              0,  /* "nails" -- leading unused bits per chunk */
              src);
  return scm_i_normbig (result);
}

/* convert a host ordered SCM integer to a 128 bit IPv6 address in
   network order.  */
static void
scm_to_ipv6 (scm_t_uint8 dst[16], SCM src)
{
  if (SCM_I_INUMP (src))
    {
      scm_t_signed_bits n = SCM_I_INUM (src);
      if (n < 0)
	scm_out_of_range (NULL, src);
#ifdef WORDS_BIGENDIAN
      memset (dst, 0, 16 - sizeof (scm_t_signed_bits));
      memcpy (dst + (16 - sizeof (scm_t_signed_bits)),
              &n,
              sizeof (scm_t_signed_bits));
#else
      memset (dst + sizeof (scm_t_signed_bits),
              0,
              16 - sizeof (scm_t_signed_bits));
      /* FIXME: this pair of ops is kinda wasteful -- should rewrite as
         a single loop perhaps, similar to the handling of bignums. */
      memcpy (dst, &n, sizeof (scm_t_signed_bits));
      FLIP_NET_HOST_128 (dst);
#endif
    }
  else if (SCM_BIGP (src))
    {
      size_t count;
      
      if ((mpz_sgn (SCM_I_BIG_MPZ (src)) < 0)
	  || mpz_sizeinbase (SCM_I_BIG_MPZ (src), 2) > 128)
	scm_out_of_range (NULL, src);
      
      memset (dst, 0, 16);
      mpz_export (dst,
                  &count,
                  1, /* big-endian chunk ordering */
                  16, /* chunks are 16 bytes long */
                  1, /* big-endian byte ordering */
                  0, /* "nails" -- leading unused bits per chunk */
                  SCM_I_BIG_MPZ (src));
      scm_remember_upto_here_1 (src);
    }
  else
    scm_wrong_type_arg_msg ("scm_to_ipv6", 0, src, "integer");
}

#ifdef HAVE_INET_PTON
SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
            (SCM family, SCM address),
	    "Convert a string containing a printable network address to\n"
	    "an integer address.  Note that unlike the C version of this\n"
	    "function,\n"
	    "the result is an integer with normal host byte ordering.\n"
	    "@var{family} can be @code{AF_INET} or @code{AF_INET6}.  E.g.,\n\n"
	    "@lisp\n"
	    "(inet-pton AF_INET \"127.0.0.1\") @result{} 2130706433\n"
	    "(inet-pton AF_INET6 \"::1\") @result{} 1\n"
	    "@end lisp")
#define FUNC_NAME s_scm_inet_pton
{
  int af;
  char *src;
  scm_t_uint32 dst[4];
  int rv, eno;

  af = scm_to_int (family);
  SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
  src = scm_to_locale_string (address);
  rv = inet_pton (af, src, dst);
  eno = errno;
  free (src);
  errno = eno;
  if (rv == -1)
    SCM_SYSERROR;
  else if (rv == 0)
    SCM_MISC_ERROR ("Bad address", SCM_EOL);
  if (af == AF_INET)
    return scm_from_ulong (ntohl (*dst));
  else
    return scm_from_ipv6 ((scm_t_uint8 *) dst);
}
#undef FUNC_NAME
#endif

#ifdef HAVE_INET_NTOP
SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
            (SCM family, SCM address),
	    "Convert a network address into a printable string.\n"
	    "Note that unlike the C version of this function,\n"
	    "the input is an integer with normal host byte ordering.\n"
	    "@var{family} can be @code{AF_INET} or @code{AF_INET6}.  E.g.,\n\n"
	    "@lisp\n"
	    "(inet-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\n"
	    "(inet-ntop AF_INET6 (- (expt 2 128) 1))\n"
	    "  @result{} \"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\"\n"
	    "@end lisp")
#define FUNC_NAME s_scm_inet_ntop
{
  int af;
#ifdef INET6_ADDRSTRLEN
  char dst[INET6_ADDRSTRLEN];
#else
  char dst[46];
#endif
  const char *result;

  af = scm_to_int (family);
  SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
  if (af == AF_INET)
    {
      scm_t_uint32 addr4;

      addr4 = htonl (SCM_NUM2ULONG (2, address));
      result = inet_ntop (af, &addr4, dst, sizeof (dst));
    }
  else
    {
      char addr6[16];

      scm_to_ipv6 ((scm_t_uint8 *) addr6, address);
      result = inet_ntop (af, &addr6, dst, sizeof (dst));
    }

  if (result == NULL)
    SCM_SYSERROR;

  return scm_from_locale_string (dst);
}
#undef FUNC_NAME
#endif

#endif  /* HAVE_IPV6 */

SCM_SYMBOL (sym_socket, "socket");

#define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket)

SCM_DEFINE (scm_socket, "socket", 3, 0, 0,
            (SCM family, SCM style, SCM proto),
	    "Return a new socket port of the type specified by @var{family},\n"
	    "@var{style} and @var{proto}.  All three parameters are\n"
	    "integers.  Supported values for @var{family} are\n"
	    "@code{AF_UNIX}, @code{AF_INET} and @code{AF_INET6}.\n"
	    "Typical values for @var{style} are @code{SOCK_STREAM},\n"
	    "@code{SOCK_DGRAM} and @code{SOCK_RAW}.\n\n"
	    "@var{proto} can be obtained from a protocol name using\n"
	    "@code{getprotobyname}.  A value of zero specifies the default\n"
	    "protocol, which is usually right.\n\n"
	    "A single socket port cannot by used for communication until it\n"
	    "has been connected to another socket.")
#define FUNC_NAME s_scm_socket
{
  int fd;

  fd = socket (scm_to_int (family),
	       scm_to_int (style),
	       scm_to_int (proto));
  if (fd == -1)
    SCM_SYSERROR;
  return SCM_SOCK_FD_TO_PORT (fd);
}
#undef FUNC_NAME

#ifdef HAVE_SOCKETPAIR
SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
            (SCM family, SCM style, SCM proto),
	    "Return a pair of connected (but unnamed) socket ports of the\n"
	    "type specified by @var{family}, @var{style} and @var{proto}.\n"
	    "Many systems support only socket pairs of the @code{AF_UNIX}\n"
	    "family.  Zero is likely to be the only meaningful value for\n"
	    "@var{proto}.")
#define FUNC_NAME s_scm_socketpair
{
  int fam;
  int fd[2];

  fam = scm_to_int (family);

  if (socketpair (fam, scm_to_int (style), scm_to_int (proto), fd) == -1)
    SCM_SYSERROR;

  return scm_cons (SCM_SOCK_FD_TO_PORT (fd[0]), SCM_SOCK_FD_TO_PORT (fd[1]));
}
#undef FUNC_NAME
#endif

/* Possible results for `getsockopt ()'.  Wrapping it into a union guarantees
   suitable alignment.  */
typedef union
{
#ifdef HAVE_STRUCT_LINGER
  struct linger linger;
#endif
  size_t size;
  int    integer;
} scm_t_getsockopt_result;

SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
            (SCM sock, SCM level, SCM optname),
	    "Return an option value from socket port @var{sock}.\n"
	    "\n"
	    "@var{level} is an integer specifying a protocol layer, either\n"
	    "@code{SOL_SOCKET} for socket level options, or a protocol\n"
	    "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
	    "(@pxref{Network Databases}).\n"
	    "\n"
	    "@defvar SOL_SOCKET\n"
	    "@defvarx IPPROTO_IP\n"
	    "@defvarx IPPROTO_TCP\n"
	    "@defvarx IPPROTO_UDP\n"
	    "@end defvar\n"
	    "\n"
	    "@var{optname} is an integer specifying an option within the\n"
	    "protocol layer.\n"
	    "\n"
	    "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
	    "defined (when provided by the system).  For their meaning see\n"
	    "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
	    "Manual}, or @command{man 7 socket}.\n"
	    "\n"
	    "@defvar SO_DEBUG\n"
	    "@defvarx SO_REUSEADDR\n"
	    "@defvarx SO_STYLE\n"
	    "@defvarx SO_TYPE\n"
	    "@defvarx SO_ERROR\n"
	    "@defvarx SO_DONTROUTE\n"
	    "@defvarx SO_BROADCAST\n"
	    "@defvarx SO_SNDBUF\n"
	    "@defvarx SO_RCVBUF\n"
	    "@defvarx SO_KEEPALIVE\n"
	    "@defvarx SO_OOBINLINE\n"
	    "@defvarx SO_NO_CHECK\n"
	    "@defvarx SO_PRIORITY\n"
	    "The value returned is an integer.\n"
	    "@end defvar\n"
	    "\n"
	    "@defvar SO_LINGER\n"
	    "The @var{value} returned is a pair of integers\n"
	    "@code{(@var{ENABLE} . @var{TIMEOUT})}.  On old systems without\n"
	    "timeout support (ie.@: without @code{struct linger}), only\n"
	    "@var{ENABLE} has an effect but the value in Guile is always a\n"
	    "pair.\n"
	    "@end defvar")
#define FUNC_NAME s_scm_getsockopt
{
  int fd;
  /* size of optval is the largest supported option.  */
  scm_t_getsockopt_result optval;
  socklen_t optlen = sizeof (optval);
  int ilevel;
  int ioptname;

  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  ilevel = scm_to_int (level);
  ioptname = scm_to_int (optname);

  fd = SCM_FPORT_FDES (sock);
  if (getsockopt (fd, ilevel, ioptname, (void *) &optval, &optlen) == -1)
    SCM_SYSERROR;

  if (ilevel == SOL_SOCKET)
    {
#ifdef SO_LINGER
      if (ioptname == SO_LINGER)
	{
#ifdef HAVE_STRUCT_LINGER
	  struct linger *ling = (struct linger *) &optval;

	  return scm_cons (scm_from_long (ling->l_onoff),
			   scm_from_long (ling->l_linger));
#else
	  return scm_cons (scm_from_long (*(int *) &optval),
			   scm_from_int (0));
#endif
	}
      else
#endif
	if (0
#ifdef SO_SNDBUF
	    || ioptname == SO_SNDBUF
#endif
#ifdef SO_RCVBUF
	    || ioptname == SO_RCVBUF
#endif
	    )
	  {
	    return scm_from_size_t (*(size_t *) &optval);
	  }
    }
  return scm_from_int (*(int *) &optval);
}
#undef FUNC_NAME

SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
            (SCM sock, SCM level, SCM optname, SCM value),
	    "Set an option on socket port @var{sock}.  The return value is\n"
	    "unspecified.\n"
	    "\n"
	    "@var{level} is an integer specifying a protocol layer, either\n"
	    "@code{SOL_SOCKET} for socket level options, or a protocol\n"
	    "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
	    "(@pxref{Network Databases}).\n"
	    "\n"
	    "@defvar SOL_SOCKET\n"
	    "@defvarx IPPROTO_IP\n"
	    "@defvarx IPPROTO_TCP\n"
	    "@defvarx IPPROTO_UDP\n"
	    "@end defvar\n"
	    "\n"
	    "@var{optname} is an integer specifying an option within the\n"
	    "protocol layer.\n"
	    "\n"
	    "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
	    "defined (when provided by the system).  For their meaning see\n"
	    "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
	    "Manual}, or @command{man 7 socket}.\n"
	    "\n"
	    "@defvar SO_DEBUG\n"
	    "@defvarx SO_REUSEADDR\n"
	    "@defvarx SO_STYLE\n"
	    "@defvarx SO_TYPE\n"
	    "@defvarx SO_ERROR\n"
	    "@defvarx SO_DONTROUTE\n"
	    "@defvarx SO_BROADCAST\n"
	    "@defvarx SO_SNDBUF\n"
	    "@defvarx SO_RCVBUF\n"
	    "@defvarx SO_KEEPALIVE\n"
	    "@defvarx SO_OOBINLINE\n"
	    "@defvarx SO_NO_CHECK\n"
	    "@defvarx SO_PRIORITY\n"
	    "@var{value} is an integer.\n"
	    "@end defvar\n"
	    "\n"
	    "@defvar SO_LINGER\n"
	    "@var{value} is a pair of integers @code{(@var{ENABLE}\n"
	    ". @var{TIMEOUT})}.  On old systems without timeout support\n"
	    "(ie.@: without @code{struct linger}), only @var{ENABLE} has an\n"
	    "effect but the value in Guile is always a pair.\n"
	    "@end defvar\n"
	    "\n"
	    "@c  Note that we refer only to ``man ip'' here.  On GNU/Linux it's\n"
	    "@c  ``man 7 ip'' but on NetBSD it's ``man 4 ip''.\n"
	    "@c \n"
	    "For IP level (@code{IPPROTO_IP}) the following @var{optname}s\n"
	    "are defined (when provided by the system).  See @command{man\n"
	    "ip} for what they mean.\n"
	    "\n"
	    "@defvar IP_ADD_MEMBERSHIP\n"
	    "@defvarx IP_DROP_MEMBERSHIP\n"
	    "These can be used only with @code{setsockopt}, not\n"
	    "@code{getsockopt}.  @var{value} is a pair\n"
	    "@code{(@var{MULTIADDR} . @var{INTERFACEADDR})} of IPv4\n"
	    "addresses (@pxref{Network Address Conversion}).\n"
	    "@var{MULTIADDR} is a multicast address to be added to or\n"
	    "dropped from the interface @var{INTERFACEADDR}.\n"
	    "@var{INTERFACEADDR} can be @code{INADDR_ANY} to have the system\n"
	    "select the interface.  @var{INTERFACEADDR} can also be an\n"
	    "interface index number, on systems supporting that.\n"
	    "@end defvar")
#define FUNC_NAME s_scm_setsockopt
{
  int fd;

  int opt_int;
#ifdef HAVE_STRUCT_LINGER
  struct linger opt_linger;
#endif

#if HAVE_STRUCT_IP_MREQ
  struct ip_mreq opt_mreq;
#endif

  const void *optval = NULL;
  socklen_t optlen = 0;

  int ilevel, ioptname;

  sock = SCM_COERCE_OUTPORT (sock);

  SCM_VALIDATE_OPFPORT (1, sock);
  ilevel = scm_to_int (level);
  ioptname = scm_to_int (optname);

  fd = SCM_FPORT_FDES (sock);
  
  if (ilevel == SOL_SOCKET)
    {
#ifdef SO_LINGER
      if (ioptname == SO_LINGER)
	{
#ifdef HAVE_STRUCT_LINGER
	  SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
	  opt_linger.l_onoff = scm_to_int (SCM_CAR (value));
	  opt_linger.l_linger = scm_to_int (SCM_CDR (value));
	  optlen = sizeof (struct linger);
	  optval = &opt_linger;
#else
	  SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
	  opt_int = scm_to_int (SCM_CAR (value));
	  /* timeout is ignored, but may as well validate it.  */
	  scm_to_int (SCM_CDR (value));
	  optlen = sizeof (int);
	  optval = &opt_int;
#endif
	}
      else
#endif
	if (0
#ifdef SO_SNDBUF
	    || ioptname == SO_SNDBUF
#endif
#ifdef SO_RCVBUF
	    || ioptname == SO_RCVBUF
#endif
	    )
	  {
	    opt_int = scm_to_int (value);
	    optlen = sizeof (size_t);
	    optval = &opt_int;
	  }
    }

#if HAVE_STRUCT_IP_MREQ
  if (ilevel == IPPROTO_IP &&
      (ioptname == IP_ADD_MEMBERSHIP || ioptname == IP_DROP_MEMBERSHIP))
    {
      /* Fourth argument must be a pair of addresses. */
      SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
      opt_mreq.imr_multiaddr.s_addr = htonl (scm_to_ulong (SCM_CAR (value)));
      opt_mreq.imr_interface.s_addr = htonl (scm_to_ulong (SCM_CDR (value)));
      optlen = sizeof (opt_mreq);
      optval = &opt_mreq;
    }
#endif

  if (optval == NULL)
    {
      /* Most options take an int.  */
      opt_int = scm_to_int (value);
      optlen = sizeof (int);
      optval = &opt_int;
    }

  if (setsockopt (fd, ilevel, ioptname, optval, optlen) == -1)
    SCM_SYSERROR;
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
          (SCM sock, SCM how),
	    "Sockets can be closed simply by using @code{close-port}. The\n"
	    "@code{shutdown} procedure allows reception or transmission on a\n"
	    "connection to be shut down individually, according to the parameter\n"
	    "@var{how}:\n\n"
	    "@table @asis\n"
	    "@item 0\n"
	    "Stop receiving data for this socket.  If further data arrives,  reject it.\n"
	    "@item 1\n"
	    "Stop trying to transmit data from this socket.  Discard any\n"
	    "data waiting to be sent.  Stop looking for acknowledgement of\n"
	    "data already sent; don't retransmit it if it is lost.\n"
	    "@item 2\n"
	    "Stop both reception and transmission.\n"
	    "@end table\n\n"
	    "The return value is unspecified.")
#define FUNC_NAME s_scm_shutdown
{
  int fd;
  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  fd = SCM_FPORT_FDES (sock);
  if (shutdown (fd, scm_to_signed_integer (how, 0, 2)) == -1)
    SCM_SYSERROR;
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

/* convert fam/address/args into a sockaddr of the appropriate type.
   args is modified by removing the arguments actually used.
   which_arg and proc are used when reporting errors:
   which_arg is the position of address in the original argument list.
   proc is the name of the original procedure.
   size returns the size of the structure allocated.  */

static struct sockaddr *
scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
		   const char *proc, size_t *size)
#define FUNC_NAME proc
{
  switch (fam)
    {
    case AF_INET:
      {
	struct sockaddr_in *soka;
	unsigned long addr;
	int port;

	SCM_VALIDATE_ULONG_COPY (which_arg, address, addr);
	SCM_VALIDATE_CONS (which_arg + 1, *args);
	port = scm_to_int (SCM_CAR (*args));
	*args = SCM_CDR (*args);
	soka = (struct sockaddr_in *) scm_malloc (sizeof (struct sockaddr_in));

#if HAVE_STRUCT_SOCKADDR_SIN_LEN
	soka->sin_len = sizeof (struct sockaddr_in);
#endif
	soka->sin_family = AF_INET;
	soka->sin_addr.s_addr = htonl (addr);
	soka->sin_port = htons (port);
	*size = sizeof (struct sockaddr_in);
	return (struct sockaddr *) soka;
      }
#ifdef HAVE_IPV6
    case AF_INET6:
      {
	/* see RFC2553.  */
	int port;
	struct sockaddr_in6 *soka;
	unsigned long flowinfo = 0;
	unsigned long scope_id = 0;

	SCM_VALIDATE_CONS (which_arg + 1, *args);
	port = scm_to_int (SCM_CAR (*args));
	*args = SCM_CDR (*args);
	if (scm_is_pair (*args))
	  {
	    SCM_VALIDATE_ULONG_COPY (which_arg + 2, SCM_CAR (*args), flowinfo);
	    *args = SCM_CDR (*args);
	    if (scm_is_pair (*args))
	      {
		SCM_VALIDATE_ULONG_COPY (which_arg + 3, SCM_CAR (*args),
					 scope_id);
		*args = SCM_CDR (*args);
	      }
	  }
	soka = (struct sockaddr_in6 *) scm_malloc (sizeof (struct sockaddr_in6));

#if HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
	soka->sin6_len = sizeof (struct sockaddr_in6);
#endif
	soka->sin6_family = AF_INET6;
	scm_to_ipv6 (soka->sin6_addr.s6_addr, address);
	soka->sin6_port = htons (port);
	soka->sin6_flowinfo = flowinfo;
#ifdef HAVE_SIN6_SCOPE_ID
	soka->sin6_scope_id = scope_id;
#endif
	*size = sizeof (struct sockaddr_in6);
	return (struct sockaddr *) soka;
      }
#endif
#ifdef HAVE_UNIX_DOMAIN_SOCKETS
    case AF_UNIX:
      {
	struct sockaddr_un *soka;
	int addr_size;
	char *c_address;

	scm_dynwind_begin (0);

	c_address = scm_to_locale_string (address);
	scm_dynwind_free (c_address);

	/* the static buffer size in sockaddr_un seems to be arbitrary
	   and not necessarily a hard limit.  e.g., the glibc manual
	   suggests it may be possible to declare it size 0.  let's
	   ignore it.  if the O/S doesn't like the size it will cause
	   connect/bind etc., to fail.  sun_path is always the last
	   member of the structure.  */
	addr_size = sizeof (struct sockaddr_un)
	  + max (0, strlen (c_address) + 1 - (sizeof soka->sun_path));
	soka = (struct sockaddr_un *) scm_malloc (addr_size);
	memset (soka, 0, addr_size);  /* for sun_len: see sin_len above. */
	soka->sun_family = AF_UNIX;
	strcpy (soka->sun_path, c_address);
	*size = SUN_LEN (soka);

	scm_dynwind_end ();
	return (struct sockaddr *) soka;
      }
#endif
    default:
      scm_out_of_range (proc, scm_from_int (fam));
    }
}
#undef FUNC_NAME

SCM_DEFINE (scm_connect, "connect", 2, 1, 1,
            (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
	    "Initiate a connection from a socket using a specified address\n"
	    "family to the address\n"
	    "specified by @var{address} and possibly @var{args}.\n"
	    "The format required for @var{address}\n"
	    "and @var{args} depends on the family of the socket.\n\n"
	    "For a socket of family @code{AF_UNIX},\n"
	    "only @var{address} is specified and must be a string with the\n"
	    "filename where the socket is to be created.\n\n"
	    "For a socket of family @code{AF_INET},\n"
	    "@var{address} must be an integer IPv4 host address and\n"
	    "@var{args} must be a single integer port number.\n\n"
	    "For a socket of family @code{AF_INET6},\n"
	    "@var{address} must be an integer IPv6 host address and\n"
	    "@var{args} may be up to three integers:\n"
	    "port [flowinfo] [scope_id],\n"
	    "where flowinfo and scope_id default to zero.\n\n"
	    "Alternatively, the second argument can be a socket address object "
	    "as returned by @code{make-socket-address}, in which case the "
	    "no additional arguments should be passed.\n\n"
	    "The return value is unspecified.")
#define FUNC_NAME s_scm_connect
{
  int fd;
  struct sockaddr *soka;
  size_t size;

  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  fd = SCM_FPORT_FDES (sock);

  if (address == SCM_UNDEFINED)
    /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
       `socket address' object.  */
    soka = scm_to_sockaddr (fam_or_sockaddr, &size);
  else
    soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
			      &args, 3, FUNC_NAME, &size);

  if (connect (fd, soka, size) == -1)
    {
      int save_errno = errno;

      free (soka);
      errno = save_errno;
      SCM_SYSERROR;
    }
  free (soka);
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE (scm_bind, "bind", 2, 1, 1,
            (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
	    "Assign an address to the socket port @var{sock}.\n"
	    "Generally this only needs to be done for server sockets,\n"
	    "so they know where to look for incoming connections.  A socket\n"
	    "without an address will be assigned one automatically when it\n"
	    "starts communicating.\n\n"
	    "The format of @var{address} and @var{args} depends\n"
	    "on the family of the socket.\n\n"
	    "For a socket of family @code{AF_UNIX}, only @var{address}\n"
	    "is specified and must be a string with the filename where\n"
	    "the socket is to be created.\n\n"
	    "For a socket of family @code{AF_INET}, @var{address}\n"
	    "must be an integer IPv4 address and @var{args}\n"
	    "must be a single integer port number.\n\n"
	    "The values of the following variables can also be used for\n"
	    "@var{address}:\n\n"
	    "@defvar INADDR_ANY\n"
	    "Allow connections from any address.\n"
	    "@end defvar\n\n"
	    "@defvar INADDR_LOOPBACK\n"
	    "The address of the local host using the loopback device.\n"
	    "@end defvar\n\n"
	    "@defvar INADDR_BROADCAST\n"
	    "The broadcast address on the local network.\n"
	    "@end defvar\n\n"
	    "@defvar INADDR_NONE\n"
	    "No address.\n"
	    "@end defvar\n\n"
	    "For a socket of family @code{AF_INET6}, @var{address}\n"
	    "must be an integer IPv6 address and @var{args}\n"
	    "may be up to three integers:\n"
	    "port [flowinfo] [scope_id],\n"
	    "where flowinfo and scope_id default to zero.\n\n"
	    "Alternatively, the second argument can be a socket address object "
	    "as returned by @code{make-socket-address}, in which case the "
	    "no additional arguments should be passed.\n\n"
	    "The return value is unspecified.")
#define FUNC_NAME s_scm_bind
{
  struct sockaddr *soka;
  size_t size;
  int fd;

  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  fd = SCM_FPORT_FDES (sock);

  if (address == SCM_UNDEFINED)
    /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
       `socket address' object.  */
    soka = scm_to_sockaddr (fam_or_sockaddr, &size);
  else
    soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
			      &args, 3, FUNC_NAME, &size);


  if (bind (fd, soka, size) == -1)
  {
    int save_errno = errno;

    free (soka);
    errno = save_errno;
    SCM_SYSERROR;
  }
  free (soka);
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
            (SCM sock, SCM backlog),
	    "Enable @var{sock} to accept connection\n"
	    "requests.  @var{backlog} is an integer specifying\n"
	    "the maximum length of the queue for pending connections.\n"
	    "If the queue fills, new clients will fail to connect until\n"
	    "the server calls @code{accept} to accept a connection from\n"
	    "the queue.\n\n"
	    "The return value is unspecified.")
#define FUNC_NAME s_scm_listen
{
  int fd;
  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  fd = SCM_FPORT_FDES (sock);
  if (listen (fd, scm_to_int (backlog)) == -1)
    SCM_SYSERROR;
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

/* Put the components of a sockaddr into a new SCM vector.  */
static SCM_C_INLINE_KEYWORD SCM
_scm_from_sockaddr (const scm_t_max_sockaddr *address, unsigned addr_size,
		    const char *proc)
{
  SCM result = SCM_EOL;
  short int fam = ((struct sockaddr *) address)->sa_family;

  switch (fam)
    {
    case AF_INET:
      {
	const struct sockaddr_in *nad = (struct sockaddr_in *) address;

	result = scm_c_make_vector (3, SCM_UNSPECIFIED);

	SCM_SIMPLE_VECTOR_SET(result, 0,
			      scm_from_short (fam));
	SCM_SIMPLE_VECTOR_SET(result, 1,
			      scm_from_ulong (ntohl (nad->sin_addr.s_addr)));
	SCM_SIMPLE_VECTOR_SET(result, 2,
			      scm_from_ushort (ntohs (nad->sin_port)));
      }
      break;
#ifdef HAVE_IPV6
    case AF_INET6:
      {
	const struct sockaddr_in6 *nad = (struct sockaddr_in6 *) address;

	result = scm_c_make_vector (5, SCM_UNSPECIFIED);
	SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam));
	SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_ipv6 (nad->sin6_addr.s6_addr));
	SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ushort (ntohs (nad->sin6_port)));
	SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_uint32 (nad->sin6_flowinfo));
#ifdef HAVE_SIN6_SCOPE_ID
	SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_ulong (nad->sin6_scope_id));
#else
	SCM_SIMPLE_VECTOR_SET(result, 4, SCM_INUM0);
#endif
      }
      break;
#endif
#ifdef HAVE_UNIX_DOMAIN_SOCKETS
    case AF_UNIX:
      {
	const struct sockaddr_un *nad = (struct sockaddr_un *) address;

	result = scm_c_make_vector (2, SCM_UNSPECIFIED);

	SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam));
	/* When addr_size is not enough to cover sun_path, do not try
	   to access it. */
	if (addr_size <= offsetof (struct sockaddr_un, sun_path))
	  SCM_SIMPLE_VECTOR_SET(result, 1, SCM_BOOL_F);
	else
	  SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (nad->sun_path));
      }
      break;
#endif
    default:
      result = SCM_UNSPECIFIED;
      scm_misc_error (proc, "unrecognised address family: ~A",
		      scm_list_1 (scm_from_int (fam)));

    }
  return result;
}

/* The publicly-visible function.  Return a Scheme object representing
   ADDRESS, an address of ADDR_SIZE bytes.  */
SCM
scm_from_sockaddr (const struct sockaddr *address, unsigned addr_size)
{
  return (_scm_from_sockaddr ((scm_t_max_sockaddr *) address,
			      addr_size, "scm_from_sockaddr"));
}

/* Convert ADDRESS, an address object returned by either
   `scm_from_sockaddr ()' or `scm_make_socket_address ()', into its C
   representation.  On success, a non-NULL pointer is returned and
   ADDRESS_SIZE is updated to the actual size (in bytes) of the returned
   address.  The result must eventually be freed using `free ()'.  */
struct sockaddr *
scm_to_sockaddr (SCM address, size_t *address_size)
#define FUNC_NAME "scm_to_sockaddr"
{
  short int family;
  struct sockaddr *c_address = NULL;

  SCM_VALIDATE_VECTOR (1, address);

  *address_size = 0;
  family = scm_to_short (SCM_SIMPLE_VECTOR_REF (address, 0));

  switch (family)
    {
    case AF_INET:
      {
	if (SCM_SIMPLE_VECTOR_LENGTH (address) != 3)
	  scm_misc_error (FUNC_NAME,
			  "invalid inet address representation: ~A",
			  scm_list_1 (address));
	else
	  {
	    struct sockaddr_in c_inet;

	    /*
	     * initialise the entire struct, including all
	     * possible padding, to NUL to avoid copying
	     * uninitialised memory in the memcpy below
	     */
	    memset (&c_inet, 0, sizeof (c_inet));

	    c_inet.sin_addr.s_addr =
	      htonl (scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 1)));
	    c_inet.sin_port =
	      htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
	    c_inet.sin_family = AF_INET;

	    *address_size = sizeof (c_inet);
	    c_address = scm_malloc (sizeof (c_inet));
	    memcpy (c_address, &c_inet, sizeof (c_inet));
	  }

	break;
      }

#ifdef HAVE_IPV6
    case AF_INET6:
      {
	if (SCM_SIMPLE_VECTOR_LENGTH (address) != 5)
	  scm_misc_error (FUNC_NAME, "invalid inet6 address representation: ~A",
			  scm_list_1 (address));
	else
	  {
	    struct sockaddr_in6 c_inet6;

	    scm_to_ipv6 (c_inet6.sin6_addr.s6_addr,
			 SCM_SIMPLE_VECTOR_REF (address, 1));
	    c_inet6.sin6_port =
	      htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
	    c_inet6.sin6_flowinfo =
	      scm_to_uint32 (SCM_SIMPLE_VECTOR_REF (address, 3));
#ifdef HAVE_SIN6_SCOPE_ID
	    c_inet6.sin6_scope_id =
	      scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 4));
#endif

	    c_inet6.sin6_family = AF_INET6;

	    *address_size = sizeof (c_inet6);
	    c_address = scm_malloc (sizeof (c_inet6));
	    memcpy (c_address, &c_inet6, sizeof (c_inet6));
	  }

	break;
      }
#endif

#ifdef HAVE_UNIX_DOMAIN_SOCKETS
    case AF_UNIX:
      {
	if (SCM_SIMPLE_VECTOR_LENGTH (address) != 2)
	  scm_misc_error (FUNC_NAME, "invalid unix address representation: ~A",
			  scm_list_1 (address));
	else
	  {
	    SCM path;
	    size_t path_len = 0;

	    path = SCM_SIMPLE_VECTOR_REF (address, 1);
	    if ((!scm_is_string (path)) && (path != SCM_BOOL_F))
	      scm_misc_error (FUNC_NAME, "invalid unix address "
			      "path: ~A", scm_list_1 (path));
	    else
	      {
		struct sockaddr_un c_unix;

		if (path == SCM_BOOL_F)
		  path_len = 0;
		else
		  path_len = scm_c_string_length (path);

#ifdef UNIX_PATH_MAX
		if (path_len >= UNIX_PATH_MAX)
#else
/* We can hope that this limit will eventually vanish, at least on GNU.
   However, currently, while glibc doesn't define `UNIX_PATH_MAX', it
   documents it has being limited to 108 bytes.  */
		if (path_len >= sizeof (c_unix.sun_path))
#endif
		  scm_misc_error (FUNC_NAME, "unix address path "
				  "too long: ~A", scm_list_1 (path));
		else
		  {
		    if (path_len)
		      {
			scm_to_locale_stringbuf (path, c_unix.sun_path,
#ifdef UNIX_PATH_MAX
						 UNIX_PATH_MAX);
#else
			                         sizeof (c_unix.sun_path));
#endif
			c_unix.sun_path[path_len] = '\0';

			/* Sanity check.  */
			if (strlen (c_unix.sun_path) != path_len)
			  scm_misc_error (FUNC_NAME, "unix address path "
					  "contains nul characters: ~A",
					  scm_list_1 (path));
		      }
		    else
		      c_unix.sun_path[0] = '\0';

		    c_unix.sun_family = AF_UNIX;

		    *address_size = SUN_LEN (&c_unix);
		    c_address = scm_malloc (sizeof (c_unix));
		    memcpy (c_address, &c_unix, sizeof (c_unix));
		  }
	      }
	  }

	break;
      }
#endif

    default:
      scm_misc_error (FUNC_NAME, "unrecognised address family: ~A",
		      scm_list_1 (scm_from_ushort (family)));
    }

  return c_address;
}
#undef FUNC_NAME


/* Return a newly-allocated `sockaddr' structure that reflects ADDRESS, being
   an address of family FAMILY, with the family-specific parameters ARGS (see
   the description of `connect' for details).  The returned structure may be
   freed using `free ()'.  */
struct sockaddr *
scm_c_make_socket_address (SCM family, SCM address, SCM args,
			   size_t *address_size)
{
  struct sockaddr *soka;

  soka = scm_fill_sockaddr (scm_to_ushort (family), address, &args, 1,
			    "scm_c_make_socket_address", address_size);

  return soka;
}

SCM_DEFINE (scm_make_socket_address, "make-socket-address", 2, 0, 1,
	    (SCM family, SCM address, SCM args),
	    "Return a Scheme address object that reflects @var{address}, "
	    "being an address of family @var{family}, with the "
	    "family-specific parameters @var{args} (see the description of "
	    "@code{connect} for details).")
#define FUNC_NAME s_scm_make_socket_address
{
  SCM result = SCM_BOOL_F;
  struct sockaddr *c_address;
  size_t c_address_size;

  c_address = scm_c_make_socket_address (family, address, args,
					 &c_address_size);
  if (c_address != NULL)
    {
      result = scm_from_sockaddr (c_address, c_address_size);
      free (c_address);
    }

  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_accept, "accept", 1, 0, 0, 
            (SCM sock),
	    "Accept a connection on a bound, listening socket.\n"
	    "If there\n"
	    "are no pending connections in the queue, wait until\n"
	    "one is available unless the non-blocking option has been\n"
	    "set on the socket.\n\n"
	    "The return value is a\n"
	    "pair in which the @emph{car} is a new socket port for the\n"
	    "connection and\n"
	    "the @emph{cdr} is an object with address information about the\n"
	    "client which initiated the connection.\n\n"
	    "@var{sock} does not become part of the\n"
	    "connection and will continue to accept new requests.")
#define FUNC_NAME s_scm_accept
{
  int fd, selected;
  int newfd;
  SCM address;
  SCM newsock;
  SELECT_TYPE readfds, exceptfds;
  socklen_t addr_size = MAX_ADDR_SIZE;
  scm_t_max_sockaddr addr;

  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  fd = SCM_FPORT_FDES (sock);

  FD_ZERO (&readfds);
  FD_ZERO (&exceptfds);
  FD_SET (fd, &readfds);
  FD_SET (fd, &exceptfds);

  /* Block until something happens on FD, leaving guile mode while
     waiting.  */
  selected = scm_std_select (fd + 1, &readfds, NULL, &exceptfds,
			     NULL);
  if (selected < 0)
    SCM_SYSERROR;

  newfd = accept (fd, (struct sockaddr *) &addr, &addr_size);
  if (newfd == -1)
    SCM_SYSERROR;
  newsock = SCM_SOCK_FD_TO_PORT (newfd);
  address = _scm_from_sockaddr (&addr, addr_size,
				FUNC_NAME);

  return scm_cons (newsock, address);
}
#undef FUNC_NAME

SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0, 
            (SCM sock),
	    "Return the address of @var{sock}, in the same form as the\n"
	    "object returned by @code{accept}.  On many systems the address\n"
	    "of a socket in the @code{AF_FILE} namespace cannot be read.")
#define FUNC_NAME s_scm_getsockname
{
  int fd;
  socklen_t addr_size = MAX_ADDR_SIZE;
  scm_t_max_sockaddr addr;

  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  fd = SCM_FPORT_FDES (sock);
  if (getsockname (fd, (struct sockaddr *) &addr, &addr_size) == -1)
    SCM_SYSERROR;

  return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
}
#undef FUNC_NAME

SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0, 
            (SCM sock),
	    "Return the address that @var{sock}\n"
	    "is connected to, in the same form as the object returned by\n"
	    "@code{accept}.  On many systems the address of a socket in the\n"
	    "@code{AF_FILE} namespace cannot be read.")
#define FUNC_NAME s_scm_getpeername
{
  int fd;
  socklen_t addr_size = MAX_ADDR_SIZE;
  scm_t_max_sockaddr addr;

  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  fd = SCM_FPORT_FDES (sock);
  if (getpeername (fd, (struct sockaddr *) &addr, &addr_size) == -1)
    SCM_SYSERROR;

  return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
}
#undef FUNC_NAME

SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
            (SCM sock, SCM buf, SCM flags),
	    "Receive data from a socket port.\n"
	    "@var{sock} must already\n"
	    "be bound to the address from which data is to be received.\n"
	    "@var{buf} is a string into which\n"
	    "the data will be written.  The size of @var{buf} limits\n"
	    "the amount of\n"
	    "data which can be received: in the case of packet\n"
	    "protocols, if a packet larger than this limit is encountered\n"
	    "then some data\n"
	    "will be irrevocably lost.\n\n"
	    "The optional @var{flags} argument is a value or\n"
	    "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
	    "The value returned is the number of bytes read from the\n"
	    "socket.\n\n"
	    "Note that the data is read directly from the socket file\n"
	    "descriptor:\n"
	    "any unread buffered port data is ignored.")
#define FUNC_NAME s_scm_recv
{
  int rv;
  int fd;
  int flg;
  char *dest;
  size_t len;

  SCM_VALIDATE_OPFPORT (1, sock);
  SCM_VALIDATE_STRING (2, buf);
  if (SCM_UNBNDP (flags))
    flg = 0;
  else
    flg = scm_to_int (flags);
  fd = SCM_FPORT_FDES (sock);

  len =  scm_i_string_length (buf);
  dest = scm_i_string_writable_chars (buf);
  SCM_SYSCALL (rv = recv (fd, dest, len, flg));
  scm_i_string_stop_writing ();

  if (rv == -1)
    SCM_SYSERROR;

  scm_remember_upto_here_1 (buf);
  return scm_from_int (rv);
}
#undef FUNC_NAME

SCM_DEFINE (scm_send, "send", 2, 1, 0,
            (SCM sock, SCM message, SCM flags),
	    "Transmit the string @var{message} on a socket port @var{sock}.\n"
	    "@var{sock} must already be bound to a destination address.  The\n"
	    "value returned is the number of bytes transmitted --\n"
	    "it's possible for\n"
	    "this to be less than the length of @var{message}\n"
	    "if the socket is\n"
	    "set to be non-blocking.  The optional @var{flags} argument\n"
	    "is a value or\n"
	    "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
	    "Note that the data is written directly to the socket\n"
	    "file descriptor:\n"
	    "any unflushed buffered port data is ignored.")
#define FUNC_NAME s_scm_send
{
  int rv;
  int fd;
  int flg;
  const char *src;
  size_t len;

  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_OPFPORT (1, sock);
  SCM_VALIDATE_STRING (2, message);
  if (SCM_UNBNDP (flags))
    flg = 0;
  else
    flg = scm_to_int (flags);
  fd = SCM_FPORT_FDES (sock);

  len = scm_i_string_length (message);
  src = scm_i_string_writable_chars (message);
  SCM_SYSCALL (rv = send (fd, src, len, flg));
  scm_i_string_stop_writing ();

  if (rv == -1)
    SCM_SYSERROR;

  scm_remember_upto_here_1 (message);
  return scm_from_int (rv);
}
#undef FUNC_NAME

SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
            (SCM sock, SCM str, SCM flags, SCM start, SCM end),
	    "Receive data from socket port @var{sock} (which must be already\n"
	    "bound), returning the originating address as well as the data.\n"
	    "This is usually for use on datagram sockets, but can be used on\n"
	    "stream-oriented sockets too.\n"
	    "\n"
	    "The data received is stored in the given @var{str}, using\n"
	    "either the whole string or just the region between the optional\n"
	    "@var{start} and @var{end} positions.  The size of @var{str}\n"
	    "limits the amount of data which can be received.  For datagram\n"
	    "protocols, if a packet larger than this is received then excess\n"
	    "bytes are irrevocably lost.\n"
	    "\n"
	    "The return value is a pair.  The @code{car} is the number of\n"
	    "bytes read.  The @code{cdr} is a socket address object which is\n"
	    "where the data come from, or @code{#f} if the origin is\n"
	    "unknown.\n"
	    "\n"
	    "The optional @var{flags} argument is a or bitwise OR\n"
	    "(@code{logior}) of @code{MSG_OOB}, @code{MSG_PEEK},\n"
	    "@code{MSG_DONTROUTE} etc.\n"
	    "\n"
	    "Data is read directly from the socket file descriptor, any\n"
	    "buffered port data is ignored.\n"
	    "\n"
	    "On a GNU/Linux system @code{recvfrom!} is not multi-threading,\n"
	    "all threads stop while a @code{recvfrom!} call is in progress.\n"
	    "An application may need to use @code{select}, @code{O_NONBLOCK}\n"
	    "or @code{MSG_DONTWAIT} to avoid this.")
#define FUNC_NAME s_scm_recvfrom
{
  int rv;
  int fd;
  int flg;
  char *buf;
  size_t offset;
  size_t cend;
  SCM address;
  socklen_t addr_size = MAX_ADDR_SIZE;
  scm_t_max_sockaddr addr;

  SCM_VALIDATE_OPFPORT (1, sock);
  fd = SCM_FPORT_FDES (sock);
  
  SCM_VALIDATE_STRING (2, str);
  scm_i_get_substring_spec (scm_i_string_length (str),
			    start, &offset, end, &cend);

  if (SCM_UNBNDP (flags))
    flg = 0;
  else
    SCM_VALIDATE_ULONG_COPY (3, flags, flg);

  /* recvfrom will not necessarily return an address.  usually nothing
     is returned for stream sockets.  */
  buf = scm_i_string_writable_chars (str);
  ((struct sockaddr *) &addr)->sa_family = AF_UNSPEC;
  SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
			      cend - offset, flg,
			      (struct sockaddr *) &addr, &addr_size));
  scm_i_string_stop_writing ();

  if (rv == -1)
    SCM_SYSERROR;
  if (((struct sockaddr *) &addr)->sa_family != AF_UNSPEC)
    address = _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
  else
    address = SCM_BOOL_F;

  scm_remember_upto_here_1 (str);

  return scm_cons (scm_from_int (rv), address);
}
#undef FUNC_NAME

SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
            (SCM sock, SCM message, SCM fam_or_sockaddr, SCM address, SCM args_and_flags),
	    "Transmit the string @var{message} on the socket port\n"
	    "@var{sock}.  The\n"
	    "destination address is specified using the @var{fam},\n"
	    "@var{address} and\n"
	    "@var{args_and_flags} arguments, or just a socket address object "
	    "returned by @code{make-socket-address}, in a similar way to the\n"
	    "@code{connect} procedure.  @var{args_and_flags} contains\n"
	    "the usual connection arguments optionally followed by\n"
	    "a flags argument, which is a value or\n"
	    "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
	    "The value returned is the number of bytes transmitted --\n"
	    "it's possible for\n"
	    "this to be less than the length of @var{message} if the\n"
	    "socket is\n"
	    "set to be non-blocking.\n"
	    "Note that the data is written directly to the socket\n"
	    "file descriptor:\n"
	    "any unflushed buffered port data is ignored.")
#define FUNC_NAME s_scm_sendto
{
  int rv;
  int fd;
  int flg;
  struct sockaddr *soka;
  size_t size;

  sock = SCM_COERCE_OUTPORT (sock);
  SCM_VALIDATE_FPORT (1, sock);
  SCM_VALIDATE_STRING (2, message);
  fd = SCM_FPORT_FDES (sock);

  if (!scm_is_number (fam_or_sockaddr))
    {
      /* FAM_OR_SOCKADDR must actually be a `socket address' object.  This
	 means that the following arguments, i.e. ADDRESS and those listed in
	 ARGS_AND_FLAGS, are the `MSG_' flags.  */
      soka = scm_to_sockaddr (fam_or_sockaddr, &size);
      if (address != SCM_UNDEFINED)
	args_and_flags = scm_cons (address, args_and_flags);
    }
  else
    soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
			      &args_and_flags, 3, FUNC_NAME, &size);

  if (scm_is_null (args_and_flags))
    flg = 0;
  else
    {
      SCM_VALIDATE_CONS (5, args_and_flags);
      flg = SCM_NUM2ULONG (5, SCM_CAR (args_and_flags));
    }
  SCM_SYSCALL (rv = sendto (fd,
			    scm_i_string_chars (message),
			    scm_i_string_length (message),
			    flg, soka, size));
  if (rv == -1)
    {
      int save_errno = errno;
      free (soka);
      errno = save_errno;
      SCM_SYSERROR;
    }
  free (soka);

  scm_remember_upto_here_1 (message);
  return scm_from_int (rv);
}
#undef FUNC_NAME



void
scm_init_socket ()
{
  /* protocol families.  */
#ifdef AF_UNSPEC
  scm_c_define ("AF_UNSPEC", scm_from_int (AF_UNSPEC));
#endif
#ifdef AF_UNIX
  scm_c_define ("AF_UNIX", scm_from_int (AF_UNIX));
#endif
#ifdef AF_INET
  scm_c_define ("AF_INET", scm_from_int (AF_INET));
#endif
#ifdef AF_INET6
  scm_c_define ("AF_INET6", scm_from_int (AF_INET6));
#endif

#ifdef PF_UNSPEC
  scm_c_define ("PF_UNSPEC", scm_from_int (PF_UNSPEC));
#endif
#ifdef PF_UNIX
  scm_c_define ("PF_UNIX", scm_from_int (PF_UNIX));
#endif
#ifdef PF_INET
  scm_c_define ("PF_INET", scm_from_int (PF_INET));
#endif
#ifdef PF_INET6
  scm_c_define ("PF_INET6", scm_from_int (PF_INET6));
#endif

  /* standard addresses.  */
#ifdef INADDR_ANY
  scm_c_define ("INADDR_ANY", scm_from_ulong (INADDR_ANY));
#endif
#ifdef INADDR_BROADCAST
  scm_c_define ("INADDR_BROADCAST", scm_from_ulong (INADDR_BROADCAST));
#endif
#ifdef INADDR_NONE
  scm_c_define ("INADDR_NONE", scm_from_ulong (INADDR_NONE));
#endif
#ifdef INADDR_LOOPBACK
  scm_c_define ("INADDR_LOOPBACK", scm_from_ulong (INADDR_LOOPBACK));
#endif

  /* socket types.

     SOCK_PACKET is deliberately omitted, the GNU/Linux socket(2) and
     packet(7) advise that it's obsolete and strongly deprecated.  */

#ifdef SOCK_STREAM
  scm_c_define ("SOCK_STREAM", scm_from_int (SOCK_STREAM));
#endif
#ifdef SOCK_DGRAM
  scm_c_define ("SOCK_DGRAM", scm_from_int (SOCK_DGRAM));
#endif
#ifdef SOCK_SEQPACKET
  scm_c_define ("SOCK_SEQPACKET", scm_from_int (SOCK_SEQPACKET));
#endif
#ifdef SOCK_RAW
  scm_c_define ("SOCK_RAW", scm_from_int (SOCK_RAW));
#endif
#ifdef SOCK_RDM
  scm_c_define ("SOCK_RDM", scm_from_int (SOCK_RDM));
#endif

  /* setsockopt level.

     SOL_IP, SOL_TCP and SOL_UDP are defined on gnu/linux, but not on for
     instance NetBSD.  We define IPPROTOs because that's what the posix spec
     shows in its example at

     http://www.opengroup.org/onlinepubs/007904975/functions/getsockopt.html
  */
#ifdef SOL_SOCKET
  scm_c_define ("SOL_SOCKET", scm_from_int (SOL_SOCKET));
#endif
#ifdef IPPROTO_IP
  scm_c_define ("IPPROTO_IP", scm_from_int (IPPROTO_IP));
#endif
#ifdef IPPROTO_TCP
  scm_c_define ("IPPROTO_TCP", scm_from_int (IPPROTO_TCP));
#endif
#ifdef IPPROTO_UDP
  scm_c_define ("IPPROTO_UDP", scm_from_int (IPPROTO_UDP));
#endif

  /* setsockopt names.  */
#ifdef SO_DEBUG
  scm_c_define ("SO_DEBUG", scm_from_int (SO_DEBUG));
#endif
#ifdef SO_REUSEADDR
  scm_c_define ("SO_REUSEADDR", scm_from_int (SO_REUSEADDR));
#endif
#ifdef SO_STYLE
  scm_c_define ("SO_STYLE", scm_from_int (SO_STYLE));
#endif
#ifdef SO_TYPE
  scm_c_define ("SO_TYPE", scm_from_int (SO_TYPE));
#endif
#ifdef SO_ERROR
  scm_c_define ("SO_ERROR", scm_from_int (SO_ERROR));
#endif
#ifdef SO_DONTROUTE
  scm_c_define ("SO_DONTROUTE", scm_from_int (SO_DONTROUTE));
#endif
#ifdef SO_BROADCAST
  scm_c_define ("SO_BROADCAST", scm_from_int (SO_BROADCAST));
#endif
#ifdef SO_SNDBUF
  scm_c_define ("SO_SNDBUF", scm_from_int (SO_SNDBUF));
#endif
#ifdef SO_RCVBUF
  scm_c_define ("SO_RCVBUF", scm_from_int (SO_RCVBUF));
#endif
#ifdef SO_KEEPALIVE
  scm_c_define ("SO_KEEPALIVE", scm_from_int (SO_KEEPALIVE));
#endif
#ifdef SO_OOBINLINE
  scm_c_define ("SO_OOBINLINE", scm_from_int (SO_OOBINLINE));
#endif
#ifdef SO_NO_CHECK
  scm_c_define ("SO_NO_CHECK", scm_from_int (SO_NO_CHECK));
#endif
#ifdef SO_PRIORITY
  scm_c_define ("SO_PRIORITY", scm_from_int (SO_PRIORITY));
#endif
#ifdef SO_LINGER
  scm_c_define ("SO_LINGER", scm_from_int (SO_LINGER));
#endif

  /* recv/send options.  */
#ifdef MSG_DONTWAIT
  scm_c_define ("MSG_DONTWAIT", scm_from_int (MSG_DONTWAIT));
#endif
#ifdef MSG_OOB
  scm_c_define ("MSG_OOB", scm_from_int (MSG_OOB));
#endif
#ifdef MSG_PEEK
  scm_c_define ("MSG_PEEK", scm_from_int (MSG_PEEK));
#endif
#ifdef MSG_DONTROUTE
  scm_c_define ("MSG_DONTROUTE", scm_from_int (MSG_DONTROUTE));
#endif

#ifdef __MINGW32__
  scm_i_init_socket_Win32 ();
#endif

#ifdef IP_ADD_MEMBERSHIP
  scm_c_define ("IP_ADD_MEMBERSHIP", scm_from_int (IP_ADD_MEMBERSHIP));
  scm_c_define ("IP_DROP_MEMBERSHIP", scm_from_int (IP_DROP_MEMBERSHIP));
#endif

  scm_add_feature ("socket");

#include "libguile/socket.x"
}


/*
  Local Variables:
  c-file-style: "gnu"
  End:
*/