summaryrefslogtreecommitdiff
path: root/support/dktools/dk4sock.h
blob: 91ba45fe17605a260aaa6f9db88df096976effe0 (plain)
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
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
#ifndef DK4SOCK_H_INCLUDED
/**	Protection against multiple inclusions. */
#define	DK4SOCK_H_INCLUDED	1

/**	@file	dk4sock.h	Socket functions.

The functions in this module are for both networking and local UNIX
domain sockets.

Use dk4socket_up() before using any other function shown here, use
dk4socket_down() to release the socket subsystem when you are done.
These functions simply indicate success on POSIX systems but
are required for subsystem initialization and release on Windows.

Exception: dk4socket_htons(), dk4socket_ntohs(), dk4socket_htonl()
and dk4socket_ntohl() and some other bit/byte handling functions
can be used without calling dk4socket_up() before.

Errors from the systems native socket functions are stored in errno
on POSIX systems. On Windows you can use WSAGetLastError() to find
an error code.
The dk4socket_error_reset(), dk4socket_error_get() and dk4socket_error_report()
functions are used to hide this difference.


*/
#include "dk4conf.h"



#if DK4_ON_WINDOWS

#if DK4_HAVE_WINSOCK2_H
#ifndef WINSOCK2_H_INCLUDED
#include <winsock2.h>
#define	WINSOCK2_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_WS2TCPIP_H
#ifndef WS2TCPIP_H_INCLUDED
#include <ws2tcpip.h>
#define	WS2TCPIP_H_INCLUDED
#endif
#endif

#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define	WINDOWS_H_INCLUDED 1
#endif

#endif
/* if DK4_ON_WINDOWS*/

#if DK4_HAVE_SYS_TYPES_H
#ifndef SYS_TYPES_H_INCLUDED
#include <sys/types.h>
#define	SYS_TYPES_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_SYS_SOCKET_H
#ifndef	SYS_SOCKET_H_INCLUDED
#include <sys/socket.h>
#define	SYS_SOCKET_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_SYS_UN_H
#ifndef SYS_UN_H_INCLUDED
#include <sys/un.h>
#define	SYS_UN_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_ARPA_INET_H
#ifndef ARPA_INET_H_INCLUDED
#include <arpa/inet.h>
#define	ARPA_INET_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_NETINET_IN_H
#ifndef	NETINET_IN_H_INCLUDED
#include <netinet/in.h>
#define	NETINET_IN_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_NETINET_IN6_H
#ifndef	NETINET_IN6_H_INCLUDED
#include <netinet/in6.h>
#define	NETINET_IN6_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_NETDB_H
#ifndef NETDB_H_INCLUDED
#include <netdb.h>
#define	NETDB_H_INCLUDED 1
#endif
#endif

#if DK4_ON_WINDOWS
/**	Data type for sockets. */
typedef	SOCKET	dk4_socket_t;
#else
/**	Data type for sockets. */
typedef	int	dk4_socket_t;
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif

#ifndef DK4ERROR_H_INCLUDED
#include "dk4error.h"
#endif



/**	Set of sockets, for listening or for receiving datagrams.
*/
typedef struct {
  dk4_socket_t	*pSockets;	/**< Array of sockets. */
  size_t	 szMax;		/**< Array size allocated, no of sockets. */
  size_t	 szUsed;	/**< Number of used sockets in array. */
} dk4_socket_set_t;


#if !DK4_HAVE_IN_ADDR
/**	IPv4 internet address.
*/
typedef	struct in_addr	IN_ADDR;
#endif


#if DK4_HAVE_STRUCT_SOCKADDR_IN6
#if !DK4_HAVE_IN6_ADDR
/**	IPv6 internet address.
*/
typedef	struct in6_addr	IN6_ADDR;
#endif
#endif


/**	Allowed IPv4 peer address or address range.
*/
typedef struct {
  IN_ADDR	ad;	/**< Address or address base. */
  IN_ADDR	ma;	/**< Net mask. */
} dk4_allowed_peer_ipv4_t;



#if DK4_HAVE_STRUCT_SOCKADDR_IN6
/**	Allowed IPv6 peer address or address range.
*/
typedef struct {
  IN6_ADDR	ad;	/**< Address or address base. */
  IN6_ADDR	ma;	/**< Net mask. */
} dk4_allowed_peer_ipv6_t;
#endif



/**	Allowed IPv6 or IPv4 peer address or range.
*/
typedef struct {
  int				af;	/**< Addr family AF_INET or AF_INET6. */
  union {
    dk4_allowed_peer_ipv4_t	ipv4;	/**< IPv4 peer data. */
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
    dk4_allowed_peer_ipv6_t	ipv6;	/**< IPv6 peer data. */
#endif
  }				data;	/**< Peer data. */
} dk4_allowed_peer_t;



#if DK4_HAVE_SOCKADDR_STORAGE
/**	Buffer large enough to hold all sockaddr_xxx structures.
*/
typedef struct sockaddr_storage	dk4_sockaddr_storage_t;
#else
/**	Buffer large enough to hold all sockaddr_xxx structures.
*/
typedef union {
  struct sockaddr		sa;	/**< Default sockaddr. */
  struct sockaddr_in		i4;	/**< IPv4 address. */
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
  struct sockaddr_in6		i6;	/**< IPv6 address. */
#endif
#if DK4_HAVE_STRUCT_SOCKADDR_UN
  struct sockaddr_un		su;	/**< UNIX domain socket address. */
#endif
} dk4_sockaddr_storage_t;
#endif

/**	Operation result values.
*/
enum {
				/**	The operation failed.
				*/
  DK4_SOCKET_RESULT_FAILED	= -1,

				/**	The operation is still in progress,
					not yet finished.
				*/
  DK4_SOCKET_RESULT_IN_PROGRESS	=  0,

				/**	The operation succeeded.
				*/
  DK4_SOCKET_RESULT_SUCCESS	=  1
};


#if DK4_ON_WINDOWS
/**	How to shut down a socket.
*/
enum {
  				/**	Shut down read (receive) operations.
				*/
  DK4_SOCKET_SHUT_READ		= SD_RECEIVE ,

  				/**	Shut down write (send) operations.
				*/
  DK4_SOCKET_SHUT_WRITE		= SD_SEND ,

  				/**	Shut down both.
				*/
  DK4_SOCKET_SHUT_RDWR		= SD_BOTH
};
#else
/**	How to shut down a socket.
*/
enum {
  				/**	Shut down read (receive) operations.
				*/
  DK4_SOCKET_SHUT_READ		= SHUT_RD ,

  				/**	Shut down write (send) operations.
				*/
  DK4_SOCKET_SHUT_WRITE		= SHUT_WR ,

  				/**	Shut down both.
				*/
  DK4_SOCKET_SHUT_RDWR		= SHUT_RDWR
};
#endif


#ifndef INVALID_SOCKET
/**	Special socket values.
*/
enum {
			/**	Invalid socket.
			*/
  INVALID_SOCKET	= -1
};
#endif

#ifdef __cplusplus
extern "C" {
#endif

/*
	Error reporting.
	----------------
	Module: dk4sock01
*/

/**	Reset error code variable (either errno or WSAGetLastError()).
*/
void
dk4socket_error_reset(void);

/**	Get error code (either from errno or via WSAGetLastError()).
	@return	Error code.
*/
int
dk4socket_error_get(void);

/**	Note error in error report, use error details from either
	errno or WSAGetLastError().
	@param	erp	Error report to fill.
	@param	ec	Error code DK4_E_xxx.
*/
void
dk4socket_error_report(dk4_er_t *erp, int ec);

/**	Write IP address to debug output file.
	For internal use only.
	@param	text	Text to show before address.
	@param	addr	Start of address buffer.
	@param	sz	Size of address.
*/
void
dk4socket_debug_address(const char *text, struct sockaddr *addr, size_t sz);


#if (DK4_HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_ERROR)

/**	Check integer result from getsockopt() for non-null value.
	On some systems the getsockopt() function writes an int value for
	boolean flags, a simple byte is used on other systems.
	After calling getsockopt() you can use this function to check
	boolean values for true or false.
	@param	optval	The value stored in an int variable by getsockopt().
	@param	slo	The length stored in the size variable.
	@return	1 for non-null value, 0 for 0 value.
*/
int
dk4socket_non_null_getsockopt(int optval, socklen_t slo);

#endif

/*
	Long and short conversion between network and host representation.
	------------------------------------------------------------------
	Module: dk4sock02
*/

/**	Convert short from host to network representation.
	Does not require dk4socket_up() to be called before.
	@param	u	Value to convert.
	@return	Conversion result.
*/
uint16_t
dk4socket_htons(uint16_t u);

/**	Convert short from network to host representation.
	Does not require dk4socket_up() to be called before.
	@param	u	Value to convert.
	@return	Conversion result.
*/
uint16_t
dk4socket_ntohs(uint16_t u);

/**	Convert long from host to network representation.
	Does not require dk4socket_up() to be called before.
	@param	u	Value to convert.
	@return	Conversion result.
*/
uint32_t
dk4socket_htonl(uint32_t u);

/**	Convert long from network to host representation.
	Does not require dk4socket_up() to be called before.
	@param	u	Value to convert.
	@return	Conversion result.
*/
uint32_t
dk4socket_ntohl(uint32_t u);

/**	Change byte order for arbitrary data types, if the
	Does not require dk4socket_up() to be called before.
	systems byte order is not big-endian.
	Does not require dk4socket_up() to be called before.
	@param	buf	Address of variable.
	@param	sz	Size of variable.
*/
void
dk4socket_swap_bytes_if_not_bigendian(void *buf, size_t sz);

/**	Create a netmask in host representation for a given bit number.
	Does not require dk4socket_up() to be called before.
	@param	dptr	Address of destination variable.
	@param	sz	Number of bits to set.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if inp is NULL,
	- DK4_E_SYNTAX<br>
	  if the number of bits is out of range.
*/
int
dk4socket_mask_bits(unsigned long *dptr, size_t sz, dk4_er_t *erp);

/**	Set all bytes to 0x01.
	Does not require dk4socket_up() to be called before.
	@param	ptr	Pointer to variable.
	@param	nbytes	Number of bytes in variable.
*/
void
dk4socket_set_one_bytes(void *ptr, size_t nbytes);



#if DK4_HAVE_STRUCT_SOCKADDR_IN6

/**	Create IPv6 netmask in network representation.
	@param	inp	Address of destination variable.
	@param	sz	Number of bits to set (1 to 128).
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if inp is NULL,
	- DK4_E_SYNTAX<br>
	  if the number of bits is out of range.
*/
int
dk4socket_mask6_bits(struct in6_addr *inp, size_t sz, dk4_er_t *erp);

#endif



#if DK4_HAVE_STRUCT_SOCKADDR_UN

/**	Find socket address length for local UNIX domain socket.
	For UNIX sockets the address size is not the size of
	struct sockaddr_un, only the used part of the file name
	buffer is counted.
	@param	sock	Pointer to socket address structure.
	@return	Number of bytes used by address.
*/
size_t
dk4socket_unix_length(const struct sockaddr_un *sock);

#endif

/**	Get protocol family for address family.
	@param	af	Address family.
	@return	Protocol family on success, PF_UNSPEC on error.
*/
int
dk4socket_pf_for_af(int af);

/**	Check address length, correct if necessary.
	This function allows to pass a dk4_sockaddr_storage_t
	address and sizeof(dk4_sockaddr_storage_t), the
	dk4socket_bind(), dk4socket_connect() and dk4socket_sendto()
	use the correct size for the address family stored.
	@param	soa	Socket address.
	@param	plgt	Address of length variable.
	@return	1 on success, 0 on error.
*/
int
dk4socket_correct_addr_lgt(const struct sockaddr *soa, size_t *plgt);

/*
	Convert addresses to text and vice versa.
	-----------------------------------------
	Module: dk4sock03
*/

/**	Convert address to text representation (char).
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size.
	@param	afam	Address family (AF_INET or AF_INET6).
	@param	src	Pointer to struct in_addr or struct in6_addr.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If the inet_ntop() function is not available
	(i.e. on Windows with DK4_WINDOWS_LEGACY enabled) IPv6 adddresses
	containing zero sequences are not abbreviated using ``::''.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or src is NULL, dsz is 0 or afam is an invalid family,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the output buffer is too small,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size calculations,
	- DK4_E_SOCKET_INET_NTOP<br>
	  if the inet_ntop() function failed (details in iDetails1).
*/
int
dk4socket_c8_inet_ntop(
  char			*dptr,
  size_t		 dsz,
  int			 afam,
  const void		*src,
  dk4_er_t		*erp
);

/**	Convert IP address including port number to char text.
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size.
	@param	afam	Address family.
	@param	saptr	Address buffer.
	@param	sasz	Address size.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If neither the getnameinfo() nor the inet_ntop()
	function is available (i.e. on Windows with DK4_WINDOWS_LEGACY
	enabled) IPv6 adddresses containing zero sequences are
	not abbreviated using ``::''.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS
	- DK4_SOCKET_GETADDRINFO
	- DK4_E_BUFFER_TOO_SMALL
*/
int
dk4socket_c8_addr_port_to_text(
  char			*dptr,
  size_t		 dsz,
  int			 afam,
  const struct sockaddr	*saptr,
  size_t		 sasz,
  dk4_er_t		*erp
);

/**	Convert generic socket address structure to char text.
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size.
	@param	saptr	Pointer to socket address structure.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If neither the getnameinfo() nor the inet_ntop()
	function is available (i.e. on Windows with DK4_WINDOWS_LEGACY
	enabled) IPv6 adddresses containing zero sequences are
	not abbreviated using ``::''.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS
	- DK4_E_SOCKET_GETADDRINFO
	- DK4_E_SYNTAX
	- DK4_E_BUFFER_TOO_SMALL
*/
int
dk4socket_c8_sockaddr_to_text(
  char				*dptr,
  size_t			 dsz,
  const dk4_sockaddr_storage_t	*saptr,
  dk4_er_t			*erp
);

/*
	Convert addresses to text and vice versa.
	-----------------------------------------
	Module: dk4sock04
*/

/**	Convert address to text representation (wchar_t).
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size (number of wchar_t).
	@param	afam	Address family (AF_INET or AF_INET6).
	@param	src	Pointer to struct in_addr or struct in6_addr.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If the inet_ntop() function is not available
	(i.e. on Windows with DK4_WINDOWS_LEGACY enabled) IPv6 adddresses
	containing zero sequences are not abbreviated using ``::''.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or src is NULL, dsz is 0 or afam is an invalid family,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the output buffer is too small,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size calculations,
	- DK4_E_SOCKET_INET_NTOP<br>
	  if the inet_ntop() function failed (details in iDetails1),
	- DK4_E_NOT_SUPPORTED<br>
	  if there is not wchar_t support.
*/
int
dk4socket_wc_inet_ntop(
  wchar_t		*dptr,
  size_t		 dsz,
  int			 afam,
  const void		*src,
  dk4_er_t		*erp
);

/**	Convert IP address including port number to wchar_t text.
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size (number of wchar_t).
	@param	afam	Address family.
	@param	saptr	Address buffer.
	@param	sasz	Address size.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If neither the getnameinfo() nor the inet_ntop()
	function is available (i.e. on Windows with DK4_WINDOWS_LEGACY
	enabled) IPv6 adddresses containing zero sequences are
	not abbreviated using ``::''.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS
	- DK4_SOCKET_GETADDRINFO
	- DK4_E_BUFFER_TOO_SMALL
	- DK4_E_NOT_SUPPORTED
*/
int
dk4socket_wc_addr_port_to_text(
  wchar_t		*dptr,
  size_t		 dsz,
  int			 afam,
  const struct sockaddr	*saptr,
  size_t		 sasz,
  dk4_er_t		*erp
);

/**	Convert generic socket address structure to wchar_t text.
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size (Number of wchar_t).
	@param	saptr	Pointer to socket address structure.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If neither the getnameinfo() nor the inet_ntop()
	function is available (i.e. on Windows with DK4_WINDOWS_LEGACY
	enabled) IPv6 adddresses containing zero sequences are
	not abbreviated using ``::''.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS
	- DK4_E_SOCKET_GETADDRINFO
	- DK4_E_SYNTAX
	- DK4_E_BUFFER_TOO_SMALL
*/
int
dk4socket_wc_sockaddr_to_text(
  wchar_t			*dptr,
  size_t			 dsz,
  const dk4_sockaddr_storage_t	*saptr,
  dk4_er_t			*erp
);

/*
	Convert addresses to text and vice versa.
	-----------------------------------------
	Module: dk4sock05
*/

/**	Convert address to text representation (dkChar).
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size (number of dkChar).
	@param	afam	Address family (AF_INET or AF_INET6).
	@param	src	Pointer to struct in_addr or struct in6_addr.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If the inet_ntop() function is not available
	(i.e. on Windows with DK4_WINDOWS_LEGACY enabled) IPv6 adddresses
	containing zero sequences are not abbreviated using ``::''.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or src is NULL, dsz is 0 or afam is an invalid family,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the output buffer is too small,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size calculations,
	- DK4_E_SOCKET_INET_NTOP<br>
	  if the inet_ntop() function failed (details in iDetails1).
*/
int
dk4socket_inet_ntop(
  dkChar		*dptr,
  size_t		 dsz,
  int			 afam,
  const void		*src,
  dk4_er_t		*erp
);

/**	Convert IP address including port number to dkChar text.
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size (number of dkChar).
	@param	afam	Address family.
	@param	saptr	Address buffer.
	@param	sasz	Address size.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If neither the getnameinfo() nor the inet_ntop()
	function is available (i.e. on Windows with DK4_WINDOWS_LEGACY
	enabled) IPv6 adddresses containing zero sequences are
	not abbreviated using ``::''.
*/
int
dk4socket_addr_port_to_text(
  dkChar		*dptr,
  size_t		 dsz,
  int			 afam,
  const struct sockaddr	*saptr,
  size_t		 sasz,
  dk4_er_t		*erp
);

/**	Convert generic socket address structure to dkChar text.
	@param	dptr	Destination buffer address.
	@param	dsz	Destination buffer size (Number of dkChar).
	@param	saptr	Pointer to socket address structure.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Restriction: If neither the getnameinfo() nor the inet_ntop()
	function is available (i.e. on Windows with DK4_WINDOWS_LEGACY
	enabled) IPv6 adddresses containing zero sequences are
	not abbreviated using ``::''.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS
	- DK4_E_SOCKET_GETADDRINFO
	- DK4_E_SYNTAX
	- DK4_E_BUFFER_TOO_SMALL
*/
int
dk4socket_sockaddr_to_text(
  dkChar			*dptr,
  size_t			 dsz,
  const dk4_sockaddr_storage_t	*saptr,
  dk4_er_t			*erp
);

/*
	Convert text to IP address (char).
	----------------------------------
	Module: dk4sock06
*/

/**	Convert text IP address to struct in_addr or struct in6_addr.
	@param	pAddr	Pointer to destination buffer.
	@param	szAddr	Destination buffer size.
	@param	afam	Address family (AF_INET or AF_INET6).
	@param	src	Source text.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if pAddr or src is NULL or szAddr is 0, or szAddr does not have
	  the correct size for the specified family,
	- DK4_E_SYNTAX<br>
	  if src does not contain a valid IPv4 or IPv6 address,
	- DK4_E_SOCKET_INET_PTON<br>
	  if the inet_pton() function failed for some reason.
*/
int
dk4socket_c8_inet_pton(
  void			*pAddr,
  size_t		 szAddr,
  int			 afam,
  const char		*src,
  dk4_er_t		*erp
);

/*
	Convert text to IP address (wchar_t).
	-------------------------------------
	Module: dk4sock07
*/

/**	Convert text IP address to struct in_addr or struct in6_addr.
	@param	pAddr	Pointer to destination buffer.
	@param	szAddr	Destination buffer size.
	@param	afam	Address family (AF_INET or AF_INET6).
	@param	src	Source text.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if pAddr or src is NULL or szAddr is 0,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if src is too long for conversion into an internal buffer,
	- DK4_E_SYNTAX<br>
	  if src contains non-ASCII characters or is not a valid
	  IPv6 or IPv6 address,
	- DK4_E_SOCKET_INET_PTON<br>
	  if InetPtonW() fails for some reason.
*/
int
dk4socket_wc_inet_pton(
  void			*pAddr,
  size_t		 szAddr,
  int			 afam,
  const wchar_t		*src,
  dk4_er_t		*erp
);

/*
	Convert text to IP address (dkChar).
	------------------------------------
	Module: dk4sock08
*/

/**	Convert text IP address to struct in_addr or struct in6_addr.
	@param	pAddr	Pointer to destination buffer.
	@param	szAddr	Destination buffer size.
	@param	afam	Address family (AF_INET or AF_INET6).
	@param	src	Source text.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if pAddr or src is NULL or szAddr is 0,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if src is too long for conversion into an internal buffer,
	- DK4_E_SYNTAX<br>
	  if src contains non-ASCII characters or is not a valid
	  IPv6 or IPv6 address,
	- DK4_E_SOCKET_INET_PTON<br>
	  if inet_pton() or InetPtonW() fails for some reason.
*/
int
dk4socket_inet_pton(
  void			*pAddr,
  size_t		 szAddr,
  int			 afam,
  const dkChar		*src,
  dk4_er_t		*erp
);

/*
	Bring socket subsystem up and down.
	-----------------------------------
	Module: dk4sock09
*/
/**	Bring socket subsystem up.
	This function calls WSAStartup() on Windows, simply indicates
	success on all other systems.
	@param	er	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	* DK4_E_SOCKET_UP<br>
	  with WSAGetLastError() result in iDetails1 if a WinSock
	  error occured.
*/
int
dk4socket_up(dk4_er_t *er);

/**	Bring socket subsystem down.
	This function calls WSACleanup() on Windows, simply indicates
	success on all other systems.
	@param	er	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	* DK4_E_SOCKET_DOWN<br>
	  with WSAGetLastError() result in iDetails1 if a WinSock
	  error occured.
*/
int
dk4socket_down(dk4_er_t *er);

/*
	Basic socket operations (open and close).
	-----------------------------------------
	Module: dk4sock10
*/

/**	Open a socket.
	@param	af	Address family (AF_INET or AF_INET6).
	@param	tp	Socket type (SOCK_STREAM or SOCK_DGRAM).
	@param	pr	Protocol (0).
	@param	erp	Error report, may be NULL.
	@return	New socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if af is not one from AF_INET, AF_INET6, AF_UNIX, or AF_LOCAL
	  or tp is not one from SOCK_STREAM or SOCK_DGRAM or pr is
	  not 0,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if 
	  the socket() function failed.
*/
dk4_socket_t
dk4socket_open(int af, int tp, int pr, dk4_er_t *erp);

/**	Shut down socket.
	@param	so	Socket to shut down.
	@param	how	Choose tasks to end, one from:
	DK4_SOCKET_SHUT_READ to stop reading (receiving),
	DK4_SOCKET_SHUT_WRITE to stop writing (sending), or
	DK4_SOCKET_SHUT_RDWR to stop both.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.
*/
int
dk4socket_shutdown(dk4_socket_t so, int how, dk4_er_t *erp);

/**	Close a socket.
	@param	so	Socket to close.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if af is not one from AF_INET, AF_INET6, AF_UNIX, or AF_LOCAL
	  or tp is not one from SOCK_STREAM or SOCK_DGRAM or pr is
	  not 0,
	- DK4_E_SOCKET_CLOSE<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the close()/closesocket() function failed.
*/
int
dk4socket_close(dk4_socket_t so, dk4_er_t *erp);

/*
	Address binding.
	----------------
	Module: dk4sock11
*/

/**	Bind a local address.
	@param	sock	Socket to configure.
	@param	pAddr	Pointer to socket address buffer.
	@param	szAddr	Size of socket address buffer.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if the address pointer is NULL or the address size is 0,
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
*/
int
dk4socket_bind(
  dk4_socket_t		 sock,
  const struct sockaddr	*pAddr,
  size_t		 szAddr,
  dk4_er_t		*erp
);

/*
	Client side: connecting to remote server.
	-----------------------------------------
	Module: dk4sock12
*/

/**	Connect to remote server.
	@param	sock	Socket to configure.
	@param	pAddr	Pointer to remote socket address buffer.
	@param	szAddr	Size of remote socket address buffer.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_IN_PROGRESS if the operation is still in progress,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if remote address pointer is NULL or remote address size is 0,
	- DK4_E_SOCKET_CONNECT<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  connect() function failed.
*/
int
dk4socket_connect(
  dk4_socket_t		 sock,
  const struct sockaddr	*pAddr,
  size_t		 szAddr,
  dk4_er_t		*erp
);

#if DK4_HAVE_STRUCT_SOCKADDR_UN

/**	Create a UNIX domain socket in client mode.
	@param	filename	File name, max 107 characters long.
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.
*/
dk4_socket_t
dk4socket_c8_unix_client(
  const char		*filename,
  dk4_er_t		*erp
);

#endif

/*
	Server side: listen and accept.
	-------------------------------
	Module: dk4sock13
*/

/**	Listen on a socket.
	@param	sock	Socket to configure for listening.
	@param	bl	Backlog (Number of connections requests to queue).
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
*/
int
dk4socket_listen(
  dk4_socket_t		 sock,
  int			 bl,
  dk4_er_t		*erp
);

/**	Accept incoming connection requests on listener socket.
	@param	sock	Listener socket.
	@param	pAddr	Pointer to address buffer, address of a
			dk4_sockaddr_storage_t recommended, NULL allowed.
	@param	pSzAddr	Pointer to address buffer size variable,
			in: buffer size,
			out: used size, should be NULL if pAddr is NULL.
	@param	erp	Error report, may be NULL.
	@return	Socket for new connection on success, INVALID_SOCKET
	on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_ACCEPT<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  accept() function failed.
*/
dk4_socket_t
dk4socket_accept(
  dk4_socket_t		 sock,
  struct sockaddr	*pAddr,
  size_t		*pSzAddr,
  dk4_er_t		*erp
);

#if DK4_HAVE_STRUCT_SOCKADDR_UN

/**	Create a UNIX domain socket in listening state.
	@param	filename	File name, max 107 characters long.
	@param	backlog		Number of connection requests to queue.
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if filename is NULL or backlog is 0,
	- DK4_E_SYNTAX<br>
	  if the file name is too long,
	- DK4_E_NON_SOCKET<br>
	  if the file exists but is not a socket,
	- DK4_E_UNLINK_FAILED<br>
	  if deletion of an already existing socket failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno value in iDetails1 if 
	  the socket() function failed,
	- DK4_E_SOCKET_BIND<br>
	  with errno value in iDetails1 if the
	  bind() function failed,
	- DK4_E_SOCKET_LISTEN<br>
	  with errno value in iDetails1 if the
	  listen() function failed,
	- DK4_E_CHMOD_FAILED<br>
	  with errno value in iDetails1 if the chmod() function failed.
*/
dk4_socket_t
dk4socket_c8_unix_listener(
  const char		*filename,
  int			 backlog,
  dk4_er_t		*erp
);

/**	Create a UNIX domain socket in listening state,
	change ownership and permission.
	This function is intended for use in daemons which will give
	up root privileges before doing any service.
	@param	filename	File name, max 107 characters long.
	@param	backlog		Number of connection requests to queue.
	@param	owner		Owner UID.
	@param	group		Owner GID.
	@param	mode		File permissions in octal notation.
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if filename is NULL or backlog is 0,
	- DK4_E_SYNTAX<br>
	  if the file name is too long,
	- DK4_E_NON_SOCKET<br>
	  if the file exists but is not a socket,
	- DK4_E_UNLINK_FAILED<br>
	  if deletion of an already existing socket failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno value in iDetails1 if 
	  the socket() function failed,
	- DK4_E_SOCKET_BIND<br>
	  with errno value in iDetails1 if the
	  bind() function failed,
	- DK4_E_SOCKET_LISTEN<br>
	  with errno value in iDetails1 if the
	  listen() function failed,
	- DK4_E_CHOWN_FAILED<br>
	  with errno value in iDetails1 if the chown() function failed,
	- DK4_E_CHMOD_FAILED<br>
	  with errno value in iDetails1 if the chmod() function failed.
*/
dk4_socket_t
dk4socket_c8_unix_listener_owner_group_mode(
  const char		*filename,
  int			 backlog,
  uid_t			 owner,
  gid_t			 group,
  mode_t		 mode,
  dk4_er_t		*erp
);

#endif

/*
	Set socket options.
	-------------------
	Module: dk4sock14
*/

/**	Set reuse flag for socket.
	Typically you should call this function immediately after
	creating the socket before binding any local address.
	@param	sock
	@param	val
	@param	erp
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_NOT_SUPPORTED<br>
	  if the setsockopt() function is not available,
	- DK4_E_INVALID_ARGUMENTS<br>
	  if the socket is invalid,
	- DK4_E_SOCKET_SETSOCKOPT<br>
	  if setsockopt() indicated an error.
*/
int
dk4socket_option_set_reuse(
  dk4_socket_t	 sock,
  int		 val,
  dk4_er_t	*erp
);

/**	Set broadcast flag for socket.
	Typically you should call this function immediately after
	creating the socket before binding any local address.
	@param	sock
	@param	val
	@param	erp
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_NOT_SUPPORTED<br>
	  if the setsockopt() function is not available,
	- DK4_E_INVALID_ARGUMENTS<br>
	  if the socket is invalid,
	- DK4_E_SOCKET_SETSOCKOPT<br>
	  if setsockopt() indicated an error.
*/
int
dk4socket_option_set_broadcast(
  dk4_socket_t	 sock,
  int		 val,
  dk4_er_t	*erp
);

/*
	Set non-blocking mode.
	----------------------
	Module: dk4sock15
*/

/**	Set non-blocking mode for socket.
	@param	sock
	@param	val
	@param	erp
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_FCNTL<br>
	  with errno value in iDetails1 if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  with WSAGetLastError() value in iDetails1 if the
	  ioctlsocket() function failed.
*/
int
dk4socket_option_set_nonblock(
  dk4_socket_t	 sock,
  int		 val,
  dk4_er_t	*erp
);

/*
	Create client side connection.
	------------------------------
	Module: dk4sock16
*/

/**	Create client side socket for a given address pair (remote
	and local address).
	@param	protfam		Protocol family (PF_INET or PF_INET6).
	@param	pRemoteAddr	Pointer to address structure containing
				remote address.
	@param	szRemoteAddr	Size of remote address.
	@param	pLocalAddr	Pointer to address structure for local
				address to bind, may be NULL to chose
				any address.
	@param	szLocalAddr	Size of remote address, must be 0 if
				pLocalAddr is NULL.
	@param	seconds		Timeout in seconds.
	@param	microSeconds	Timeout in microseconds.
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	For all error codes - except DK4_E_SOCKET_TIMEOUT - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_tcp_client_for_addr(
  int			 protfam,
  struct sockaddr	*pRemoteAddr,
  size_t		 szRemoteAddr,
  struct sockaddr	*pLocalAddr,
  size_t		 szLocalAddr,
  long			 seconds,
  long			 microSeconds,
  dk4_er_t		*erp
);

/*
	Choose remote and optionaly local address and connect.
	------------------------------------------------------
	Module: dk4sock17
*/

/**	Connect to a specified host name and service name.
	@param	hostname	Host name to connect to.
	@param	svcname		Service name or port number as string.
	@param	lsvcname	Local service name or port number as string.
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	nsrv		Flag: svname and lsvcname are numeric strings.
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths.
	For all error codes - except DK4_E_SOCKET_TIMEOUT and
	DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_c8_tcp_client_host_num_srv(
  const char		*hostname,
  const char		*svcname,
  const char		*lsvcname,
  long			 secs,
  long			 usecs,
  int			 nsrv,
  dk4_er_t		*erp
);

/**	Connect to a specified host name and service name.
	@param	hostname	Host name to connect to.
	@param	svcname		Service name or port number as string.
	@param	lsvcname	Local service name or port number as string.
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths.
	For all error codes - except DK4_E_SOCKET_TIMEOUT and
	DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_c8_tcp_client_host_srv(
  const char		*hostname,
  const char		*svcname,
  const char		*lsvcname,
  long			 secs,
  long			 usecs,
  dk4_er_t		*erp
);

/**	Connect to a specified host name and port number.
	@param	hostname	Host name to connect to.
	@param	portno		Port number in host byte order to connect to.
	@param	lportno		Local port number in host byte order (0 = any).
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths.
	For all error codes - except DK4_E_SOCKET_TIMEOUT and
	DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_c8_tcp_client_host_port(
  const	char		*hostname,
  unsigned short	 portno,
  unsigned short	 lportno,
  long			 secs,
  long			 usecs,
  dk4_er_t		*erp
);

/*
	Choose remote and optionally local address and connect (wchar_t).
	-----------------------------------------------------------------
	Module: dk4sock18
*/

/**	Connect to a specified host name and service name.
	@param	hostname	Host name to connect to.
	@param	svcname		Service name or port number as string.
	@param	lsvcname	Local service name or port number as string.
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	nsrv		Flag: svname and lsvcname are numeric strings.
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths,
	- DK4_E_NOT_SUPPORTED<br>
	  if wide character functions for networking are not available.
	For all error codes - except DK4_E_SOCKET_TIMEOUT and
	DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_wc_tcp_client_host_num_srv(
  const wchar_t		*hostname,
  const wchar_t		*svcname,
  const wchar_t		*lsvcname,
  long			 secs,
  long			 usecs,
  int			 nsrv,
  dk4_er_t		*erp
);

/**	Connect to a specified host name and service name.
	@param	hostname	Host name to connect to.
	@param	svcname		Service name or port number as string.
	@param	lsvcname	Local service name or port number as string.
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths,
	- DK4_E_NOT_SUPPORTED<br>
	  if no wide character functions for networking are avaialble.
	For all error codes - except DK4_E_SOCKET_TIMEOUT, DK4_E_NOT_SUPPORTED
	and DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_wc_tcp_client_host_srv(
  const wchar_t		*hostname,
  const wchar_t		*svcname,
  const wchar_t		*lsvcname,
  long			 secs,
  long			 usecs,
  dk4_er_t		*erp
);

/**	Connect to a specified host name and port number.
	@param	hostname	Host name to connect to.
	@param	portno		Port number in host byte order to connect to.
	@param	lportno		Local port number in host byte order (0 = any).
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths,
	- DK4_E_NOT_SUPPORTED<br>
	  if no wide character functions for networking are available.
	For all error codes - except DK4_E_SOCKET_TIMEOUT and
	DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_wc_tcp_client_host_port(
  const	wchar_t		*hostname,
  unsigned short	 portno,
  unsigned short	 lportno,
  long			 secs,
  long			 usecs,
  dk4_er_t		*erp
);

/*
	Choose remote and optionaly local address and connect (dkChar).
	---------------------------------------------------------------
	Module: dk4sock19
*/

/**	Connect to a specified host name and service name.
	@param	hostname	Host name to connect to.
	@param	svcname		Service name or port number as string.
	@param	lsvcname	Local service name or port number as string.
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	nsrv		Flag: svname and lsvcname are numeric strings.
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths,
	- DK4_E_NOT_SUPPORTED<br>
	  if wide character functions for networking are not available.
	For all error codes - except DK4_E_SOCKET_TIMEOUT and
	DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_tcp_client_host_num_srv(
  const dkChar		*hostname,
  const dkChar		*svcname,
  const dkChar		*lsvcname,
  long			 secs,
  long			 usecs,
  int			 nsrv,
  dk4_er_t		*erp
);

/**	Connect to a specified host name and service name.
	@param	hostname	Host name to connect to.
	@param	svcname		Service name or port number as string.
	@param	lsvcname	Local service name or port number as string.
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths,
	- DK4_E_NOT_SUPPORTED<br>
	  if no wide character functions for networking are avaialble.
	For all error codes - except DK4_E_SOCKET_TIMEOUT, DK4_E_NOT_SUPPORTED
	and DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_tcp_client_host_srv(
  const dkChar		*hostname,
  const dkChar		*svcname,
  const dkChar		*lsvcname,
  long			 secs,
  long			 usecs,
  dk4_er_t		*erp
);

/**	Connect to a specified host name and port number.
	@param	hostname	Host name to connect to.
	@param	portno		Port number in host byte order to connect to.
	@param	lportno		Local port number in host byte order (0 = any).
	@param	secs		Timeout seconds (may be 0).
	@param	usecs		Timeout microseconds (may be 0).
	@param	erp		Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if any argument is bad,
	- DK4_E_SOCKET_SOCKET<br>
	  if the socket() function failed to create a new socket,
	- DK4_E_SOCKET_BIND<br>
	  if the bind() function failed to bind the requested local address,
	- DK4_E_SOCKET_FCNTL<br>
	  if the fcntl() function failed,
	- DK4_E_SOCKET_IOCTLSOCKET
	  if the ioctlsocket() function failed.
	- DK4_E_SOCKET_GETHOSTBYNAME<br>
	  if the gethostbyname() function failed to find the remote host.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  if the getservbyname() function failed to find the port
	  number for the remote or local service.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  if the getaddrinfo() function failed to find local addresses.
	- DK4_E_SOCKET_GETADDRINFO<br>
	  if the getaddrinfo() function failed to find the remote host,
	- DK4_E_SOCKET_CONNECT<br>
	  if the connect() function failed,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if a timeout occured in the connect attempt,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed,
	- DK4_E_SOCKET_GETSOCKOPT<br>
	  if the getsockopt() function failed.
	- DK4_E_BUG<br>
	  if the gethostbyname() function returned non-matching or
	  otherwise illegal address lengths,
	- DK4_E_NOT_SUPPORTED<br>
	  if no wide character functions for networking are available.
	For all error codes - except DK4_E_SOCKET_TIMEOUT and
	DK4_E_BUG - the iDetails1
	field contains the errno or WSAGetLastError() value.
*/
dk4_socket_t
dk4socket_tcp_client_host_port(
  const	dkChar		*hostname,
  unsigned short	 portno,
  unsigned short	 lportno,
  long			 secs,
  long			 usecs,
  dk4_er_t		*erp
);

/*
	Send and receive data over an established connection.
	-----------------------------------------------------
	Module: dk4sock20
*/

/**	Send data.
	@param	sock	Socket to use.
	@param	buf	Start address of buffer.
	@param	pszbuf	Pointer to size variable
			(in: buffer size, out: bytes send).
	@param	flags	Flags for send() function.
	@param	secs	Seconds for timeout.
	@param	usecs	Microseconds for timeout.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error (0 bytes send),
	DK4_SOCKET_RESULT_IN_PROGRESS on partial success.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if sock is an invalid socket, buf is NULL, pszbuf is NULL,
	  *pszbuf is 0 or *pszbuf is too large to fit into an ssize_t
	  or int,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if the timeout was reached without getting the socket writable,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed (details in iDetails1),
	- DK4_E_SOCKET_SEND<br>
	  if the send() function failed (details in iDetails1).
*/
int
dk4socket_send(
  dk4_socket_t	 sock,
  const void	*buf,
  size_t	*pszbuf,
  int		 flags,
  long		 secs,
  long		 usecs,
  dk4_er_t	*erp
);

/**	Receive data.
	@param	sock	Socket to use.
	@param	buf	Start address of buffer.
	@param	pszbuf	Pointer to size variable
			(in: buffer size, out: bytes received).
	@param	flags	Flags for send() function.
	@param	secs	Seconds for timeout.
	@param	usecs	Microseconds for timeout.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if sock is an invalid socket, buf is NULL, pszbuf is NULL,
	  *pszbuf is 0 or *pszbuf is too large to fit into an ssize_t
	  or int,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if the timeout was reached without getting data on the socket,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed (details in iDetails1),
	- DK4_E_SOCKET_RECV<br>
	  if the send() function failed (details in iDetails1).
*/
int
dk4socket_recv(
  dk4_socket_t	 sock,
  void		*buf,
  size_t	*pszbuf,
  int		 flags,
  long		 secs,
  long		 usecs,
  dk4_er_t	*erp
);

/*
	Connectionless send and receive operations.
	-------------------------------------------
	Module: dk4sock21
*/

/**	Send UDP datagram.
	@param	sock		Socket to use.
	@param	buf		Start address of buffer.
	@param	pszbuf		Pointer to size variable
				(in: buffer size, out: bytes send).
	@param	flags		Flags for sendto() function.
	@param	saddr		Pointer to destination address.
	@param	addrlen		Length of destination address.
	@param	secs		Seconds for timeout.
	@param	usecs		Microseconds for timeout.
	@param	erp		Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_IN_PROGRESS on partial success (not all bytes send),
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if sock is an invalid socket, buf is NULL, pszbuf is NULL,
	  *pszbuf is 0 or *pszbuf is too large to fit into an ssize_t
	  or int,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if the timeout was reached without getting data on the socket,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed (details in iDetails1),
	- DK4_E_SOCKET_RECV<br>
	  if the send() function failed (details in iDetails1).
*/
int
dk4socket_sendto(
  dk4_socket_t		 sock,
  const void		*buf,
  size_t		*pszbuf,
  int			 flags,
  const struct sockaddr	*saddr,
  size_t		 addrlen,
  long			 secs,
  long			 usecs,
  dk4_er_t		*erp
);

/**	Receive UDP datagram.
	@param	sock		Socket to use.
	@param	buf		Start address of buffer.
	@param	pszbuf		Pointer to size variable
				(in: buffer size, out: bytes used).
	@param	flags		Flags for recvfrom() function.
	@param	saddr		Pointer to buffer for source address.
	@param	paddrlen	Source address buffer size.
	@param	secs		Seconds for timeout.
	@param	usecs		Microseconds for timeout.
	@param	erp		Error report, may be NULL.

	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if sock is an invalid socket, buf is NULL, pszbuf is NULL,
	  *pszbuf is 0 or *pszbuf is too large to fit into an ssize_t
	  or int,
	- DK4_E_SOCKET_TIMEOUT<br>
	  if the timeout was reached without getting data on the socket,
	- DK4_E_SOCKET_SELECT<br>
	  if the select() function failed (details in iDetails1),
	- DK4_E_SOCKET_RECV<br>
	  if the send() function failed (details in iDetails1).
*/
int
dk4socket_recvfrom(
  dk4_socket_t		 sock,
  void			*buf,
  size_t		*pszbuf,
  int			 flags,
  struct sockaddr	*saddr,
  size_t		*paddrlen,
  long			 secs,
  long			 usecs,
  dk4_er_t		*erp
);

/*
	Listener socket set (create and delete).
	----------------------------------------
	Module: dk4sock22
*/

/**	Allocate memory for a socket set but do not open any
	socket (set all to INVALID_SOCKET).
	@param	max	Number of sockets to plan for.
	@param	erp	Error report, may be NULL.
	@return	Pointer to new socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if max is 0,
	- DK4_E_MATH_OVERFLOW<br>
	  on numeric overflow when calculating the number of bytes to
	  allocate,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem set if there is not enough memory
	  available.
*/
dk4_socket_set_t *
dk4socket_set_new(
  size_t		 max,
  dk4_er_t		*erp
);

/**	Delete socket set (do not close anything, just release the memory).
	@param	set	Socket set to destroy.
	@param	erp	Error report, may be NULL.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if set is NULL.
*/
void
dk4socket_set_delete(
  dk4_socket_set_t	*set,
  dk4_er_t		*erp
);

/**	Close all sockets and delete the socket set (release memory).
	@param	set	Socket set to close.
	@param	erp	Error report, may be NULL.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if set is NULL.
	- DK4_E_SOCKET_CLOSE<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the close()/closesocket() function failed.
*/
void
dk4socket_set_close(
  dk4_socket_set_t	*set,
  dk4_er_t		*erp
);

/*
	Create listener socket set for local/remote connections.
	--------------------------------------------------------
	Module: dk4sock23
*/

/**	Create listener socket set for a local port number, do
	not use getaddrinfo() to obtain local addresses, construct
	addresses.
	@param	portno	Local port number to listen on.
	@param	local	Flag: Allow local connections only.
	@param	backlog	Number of connection requests to queue.
	@param	erp	Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
*/
dk4_socket_set_t *
dk4socket_set_server_directly(
  unsigned short	 portno,
  int			 backlog,
  int			 local,
  dk4_er_t		*erp
);

/*
	Create listener socket set for local/remote connections.
	--------------------------------------------------------
	Module: dk4sock24
*/

/**	Create listener socket set for a local port number, use
	getaddrinfo() if available to obtain a collection of local
	addresses.
	@param	portno	Local port number to listen on.
	@param	local	Flag: Allow local connections only.
	@param	backlog	Number of connection requests to queue.
	@param	erp	Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getaddrinfo() function failed.
*/
dk4_socket_set_t *
dk4socket_c8_set_server(
  unsigned short	 portno,
  int			 backlog,
  int			 local,
  dk4_er_t		*erp
);

/**	Create listener socket set for a local port number.
	@param	servicename	Local service name.
	@param	backlog	Number of connection requests to queue.
	@param	local		Flag: Allow local connections only.
	@param	erp		Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getaddrinfo() function failed.
	- DK4_E_SYNTAX<br>
	  if a non-digit was found in a port number,
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getservbyname() function failed to find a port number of a service,
*/
dk4_socket_set_t *
dk4socket_c8_set_service(
  const char		*servicename,
  int			 backlog,
  int			 local,
  dk4_er_t		*erp
);

/*
	dkChar variants of the functions above.
	---------------------------------------
	Module: dk4sock25
*/

/**	Create listener socket set for a local port number.
	@param	portno	Local port number to listen on.
	@param	local	Flag: Allow local connections only.
	@param	backlog	Number of connection requests to queue.
	@param	erp	Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getaddrinfo() function failed.
*/
dk4_socket_set_t *
dk4socket_set_server(
  unsigned short	 portno,
  int			 backlog,
  int			 local,
  dk4_er_t		*erp
);

/**	Create listener socket set for a local port number.
	@param	servicename	Local service name.
	@param	backlog	Number of connection requests to queue.
	@param	local		Flag: Allow local connections only.
	@param	erp		Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getaddrinfo() function failed.
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getservbyname() function failed to find a port number of a service,
*/
dk4_socket_set_t *
dk4socket_set_service(
  const dkChar		*servicename,
  int			 backlog,
  int			 local,
  dk4_er_t		*erp
);

/*
	Get allowed peer from char string.
	----------------------------------
	Module: dk4sock26
*/

/**	Get allowed peer from string.
	@param	peerptr	Data structure to save peer information.
	@param	str	Text containing the information.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS
	- DK4_E_BUFFER_TOO_SMALL
	- DK4_E_SYNTAX<br>
	  if the IP address component in str is not a valid IPv4 or IPv6
	  address, the mask component is not a valid IPv4 or IPv6 address
	  or an unsigned bit number or the bit number is too large,
	- DK4_E_SOCKET_INET_PTON<br>
	  if the inet_pton() function failed for some reason.
*/
int
dk4socket_c8_get_allowed_peer(
  dk4_allowed_peer_t	*peerptr,
  const char		*str,
  dk4_er_t		*erp
);

/*
	Get allowed peer from dkChar string.
	------------------------------------
	Module: dk4sock27
*/

/**	Get allowed peer from string.
	@param	peerptr	Data structure to save peer information.
	@param	str	Text containing the information.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS
	- DK4_E_BUFFER_TOO_SMALL
	- DK4_E_SYNTAX<br>
	  if the IP address component in str is not a valid IPv4 or IPv6
	  address, the mask component is not a valid IPv4 or IPv6 address
	  or an unsigned bit number or the bit number is too large,
	- DK4_E_SOCKET_INET_PTON<br>
	  if the inet_pton() function failed for some reason.
*/
int
dk4socket_get_allowed_peer(
  dk4_allowed_peer_t	*peerptr,
  const dkChar		*str,
  dk4_er_t		*erp
);

/*
	Comparison of allowed peers.
	----------------------------
	Module: dk4sock28
*/

/**	Compare allowed peers.
	@param	pl	Left pointer, always dk4_allowed_peer_t.
	@param	pr	Right pointer, dk4_allowed_peer_t or struct sockaddr.
	@param	cr	Comparison criteria:
	1: pr is struct sockaddr, else pr is dk4_allowed_peer_t.
	@return	-1, 0 or 1 for pl<pr, pl==pr, pl>pr.
*/
int
dk4socket_allowed_peer_compare(
  const void	*pl,
  const void	*pr,
  int		 cr
);

/*
	Create UDP socket set for local/remote connections.
	---------------------------------------------------
	Module: dk4sock29
*/

/**	Create server UDP socket set for a local port number, do
	not use getaddrinfo() to obtain local addresses, construct
	addresses.
	@param	portno		Local port number to listen on.
	@param	local		Flag: Allow local connections only.
	@param	broadcast	Flag: Prepare for broadcast sending.
	@param	erp		Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
*/
dk4_socket_set_t *
dk4socket_set_udp_directly(
  unsigned short	 portno,
  int			 local,
  int			 broadcast,
  dk4_er_t		*erp
);

/*
	Create UDP socket set for local/remote connections.
	---------------------------------------------------
	Module: dk4sock30
*/

/**	Create UDP socket set for a local port number, use
	getaddrinfo() if available to obtain a collection of local
	addresses.
	@param	portno		Local port number to listen on.
	@param	local		Flag: Allow local connections only.
	@param	broadcast	Flag: Prepare for broadcast send.
	@param	erp		Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getaddrinfo() function failed.
*/
dk4_socket_set_t *
dk4socket_c8_set_udp(
  unsigned short	 portno,
  int			 local,
  int			 broadcast,
  dk4_er_t		*erp
);

/**	Create UDP socket set for a local port number.
	@param	servicename	Local service name.
	@param	local		Flag: Allow local connections only.
	@param	broadcast	Flag: Prepare for broadcast transmission.
	@param	erp		Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getaddrinfo() function failed.
	- DK4_E_SYNTAX<br>
	  if a non-digit was found in a port number,
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getservbyname() function failed to find a port number of a service,
*/
dk4_socket_set_t *
dk4socket_c8_set_udp_service(
  const char		*servicename,
  int			 local,
  int			 broadcast,
  dk4_er_t		*erp
);

/*
	Create UDP socket set for local/remote connections.
	---------------------------------------------------
	Module: dk4sock31
*/

/**	Create UDP socket set for a local port number, use
	getaddrinfo() if available to obtain a collection of local
	addresses.
	@param	portno		Local port number to listen on.
	@param	local		Flag: Allow local connections only.
	@param	broadcast	Flag: Prepare for broadcast send.
	@param	erp		Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getaddrinfo() function failed.
*/
dk4_socket_set_t *
dk4socket_set_udp(
  unsigned short	 portno,
  int			 local,
  int			 broadcast,
  dk4_er_t		*erp
);

/**	Create UDP socket set for a local port number.
	@param	servicename	Local service name.
	@param	local		Flag: Allow local connections only.
	@param	broadcast	Flag: Prepare for broadcast transmission.
	@param	erp		Error report, may be NULL.
	@return	Socket set on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if an invalid argument was specified,
	- DK4_E_MATH_OVERFLOW<br>
	  if there was a numeric overflow in memory size calculation
	  (should not happen),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if
	  the socket() function failed.
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
	- DK4_E_SOCKET_LISTEN<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  listen() function failed.
	- DK4_E_SOCKET_GETADDRLOC<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getaddrinfo() function failed.
	- DK4_E_SYNTAX<br>
	  if a non-digit was found in a port number,
	- DK4_E_SOCKET_GETSERVBYNAME<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  getservbyname() function failed to find a port number of a service,
*/
dk4_socket_set_t *
dk4socket_set_udp_service(
  const dkChar		*servicename,
  int			 local,
  int			 broadcast,
  dk4_er_t		*erp
);

/*
	Prepare addresses for UDP client socket.
	----------------------------------------
	Module: dk4sock32
*/

/**	Get address family for generic socket address structure.
	@param	aptr	Address of generic socket address structure.
	@return	AF_INET or AF_INET6 on success, AF_UNSPEC on error.
*/
int
dk4socket_address_family(dk4_sockaddr_storage_t *aptr);

/**	Get used structure size for generic socket address structure.
	@param	aptr	Address of generic socket address structure.
	@return	Size of sockaddr_in or sockaddr_in6 on success, 0 on error.
*/
size_t
dk4socket_address_size(dk4_sockaddr_storage_t *aptr);

/**	Complete address structures if IP address was found
	(add port numbers).
	@param	array	Address structures array (2 elements:
			0=remote, 1=local).
	@param	remport	Remote port number.
	@param	locport	Local port number.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.
*/
int
dk4socket_complete_addresses(
  dk4_sockaddr_storage_t	*array,
  unsigned short		 remport,
  unsigned short		 locport
);

/*
	Prepare addresses for UDP client socket.
	----------------------------------------
	Module: dk4sock33
*/

/**	Find addresses to send UDP packets to a remote host.
	@param	array	Destination array (2 elements),
	element 0 is the remote address to send to,
	element 1 is the local address to bind.
	@param	remhost	Remote host name or IP address.
	@param	remport	Remote port number in host representation.
	@param	locport	Local port number in host representation.
	@param	ip4	Flag: Prefer IPv4 addresses.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  for invalid arguments,
	- DK4_E_NOT_FOUND<br>
	  if the requested host was not found.
*/
int
dk4socket_c8_udp_addresses_numeric(
  dk4_sockaddr_storage_t	*array,
  const	char			*remhost,
  unsigned short		 remport,
  unsigned short		 locport,
  int				 ip4,
  dk4_er_t			*erp
);

/*
	Prepare addresses for UDP client socket.
	----------------------------------------
	Module: dk4sock34
*/

/**	Find addresses to send UDP packets to a remote host (wchar_t).
	@param	array	Destination array (2 elements),
	element 0 is the remote address to send to,
	element 1 is the local address to bind.
	@param	remhost	Remote host name or IP address.
	@param	remport	Remote port number in host representation.
	@param	locport	Local port number in host representation.
	@param	ip4	Flag: Prefer IPv4 addresses.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  for invalid arguments,
	- DK4_E_NOT_FOUND<br>
	  if the requested host was not found.
*/
int
dk4socket_wc_udp_addresses_numeric(
  dk4_sockaddr_storage_t	*array,
  const	wchar_t			*remhost,
  unsigned short		 remport,
  unsigned short		 locport,
  int				 ip4,
  dk4_er_t			*erp
);


/**	Find addresses to send UDP packets to a remote host (dkChar).
	@param	array	Destination array (2 elements),
	element 0 is the remote address to send to,
	element 1 is the local address to bind.
	@param	remhost	Remote host name or IP address.
	@param	remport	Remote port number in host representation.
	@param	locport	Local port number in host representation.
	@param	ip4	Flag: Prefer IPv4 addresses.
	@param	erp	Error report, may be NULL.
	@return	DK4_SOCKET_RESULT_SUCCESS on success,
	DK4_SOCKET_RESULT_FAILED on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  for invalid arguments,
	- DK4_E_NOT_FOUND<br>
	  if the requested host was not found.
	- DK4_E_NOT_SUPPORTED<br>
	  if networking with wide characters is not supported.
*/
int
dk4socket_udp_addresses_numeric(
  dk4_sockaddr_storage_t	*array,
  const	dkChar			*remhost,
  unsigned short		 remport,
  unsigned short		 locport,
  int				 ip4,
  dk4_er_t			*erp
);


/*
	Create UDP socket and bind to local port.
	-----------------------------------------
	Module: dk4sock35
*/

/**	Create UDP socket bound to local port.
	@param	protfam		Protocol family, PF_INET or PF_INET6.
	@param	pLocalAddr	Local address to bind.
	@param	szLocalAddr	Size of local address.
	@param	erp		Error report. may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if pLocalAddr is NULL or szLocalAddr is 0 or protfam is not
	  a valid protocol family,
	- DK4_E_SOCKET_SOCKET<br>
	  with errno/WSAGetLastError() value in iDetails1 if 
	  the socket() function failed,
	- DK4_E_SOCKET_BIND<br>
	  with errno/WSAGetLastError() value in iDetails1 if the
	  bind() function failed.
*/
dk4_socket_t
dk4socket_udp_client_for_addr(
  int			 protfam,
  struct sockaddr	*pLocalAddr,
  size_t		 szLocalAddr,
  dk4_er_t		*erp
);

/*
	Create UDP socket and bind to local port.
	-----------------------------------------
	Module: dk4sock36
*/

/**	Create UDP socket for communication to remote host (char).
	@param	array	Destination array (2 elements),
	element 0 is the remote address we want to send data to,
	element 1 is the local address to use.
	@param	remhost	Remote host name or address.
	@param	remport	Remote port number in host representation.
	@param	locport	Local port number in host representation.
	@param	ip4	Flag: Prefer IPv4 over IPv6.
	@param	erp	Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.
*/
dk4_socket_t
dk4socket_c8_udp_client_for_host(
  dk4_sockaddr_storage_t	*array,
  const char			*remhost,
  unsigned short		 remport,
  unsigned short		 locport,
  int				 ip4,
  dk4_er_t			*erp
);

/*
	Create UDP socket and bind to local port.
	-----------------------------------------
	Module: dk4sock37
*/

/**	Create UDP socket for communication to remote host (wchar_t).
	@param	array	Destination array (2 elements),
	element 0 is the remote address we want to send data to,
	element 1 is the local address to use.
	@param	remhost	Remote host name or address.
	@param	remport	Remote port number in host representation.
	@param	locport	Local port number in host representation.
	@param	ip4	Flag: Prefer IPv4 over IPv6.
	@param	erp	Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.
*/
dk4_socket_t
dk4socket_wc_udp_client_for_host(
  dk4_sockaddr_storage_t	*array,
  const wchar_t			*remhost,
  unsigned short		 remport,
  unsigned short		 locport,
  int				 ip4,
  dk4_er_t			*erp
);

/**	Create UDP socket for communication to remote host (dkChar).
	@param	array	Destination array (2 elements),
	element 0 is the remote address we want to send data to,
	element 1 is the local address to use.
	@param	remhost	Remote host name or address.
	@param	remport	Remote port number in host representation.
	@param	locport	Local port number in host representation.
	@param	ip4	Flag: Prefer IPv4 over IPv6.
	@param	erp	Error report, may be NULL.
	@return	Valid socket on success, INVALID_SOCKET on error.
*/
dk4_socket_t
dk4socket_udp_client_for_host(
  dk4_sockaddr_storage_t	*array,
  const dkChar			*remhost,
  unsigned short		 remport,
  unsigned short		 locport,
  int				 ip4,
  dk4_er_t			*erp
);

#ifdef __cplusplus
}
#endif

#endif
/* ifndef DK4SOCK_H_INCLUDED */