The OpenD Programming Language

1 /**
2  * D header file for POSIX.
3  *
4  * Copyright: Copyright Sean Kelly 2005 - 2009.
5  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6  * Authors:   Sean Kelly, Alex Rønne Petersen
7  * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
8  */
9 
10 /*          Copyright Sean Kelly 2005 - 2009.
11  * Distributed under the Boost Software License, Version 1.0.
12  *    (See accompanying file LICENSE or copy at
13  *          http://www.boost.org/LICENSE_1_0.txt)
14  */
15 module core.sys.posix.sys.socket;
16 
17 import core.sys.posix.config;
18 public import core.sys.posix.sys.types; // for ssize_t
19 public import core.sys.posix.sys.uio;   // for iovec
20 
21 version (OSX)
22     version = Darwin;
23 else version (iOS)
24     version = Darwin;
25 else version (TVOS)
26     version = Darwin;
27 else version (WatchOS)
28     version = Darwin;
29 
30 version (ARM)     version = ARM_Any;
31 version (AArch64) version = ARM_Any;
32 version (HPPA)    version = HPPA_Any;
33 version (MIPS32)  version = MIPS_Any;
34 version (MIPS64)  version = MIPS_Any;
35 version (PPC)     version = PPC_Any;
36 version (PPC64)   version = PPC_Any;
37 version (RISCV32) version = RISCV_Any;
38 version (RISCV64) version = RISCV_Any;
39 version (S390)    version = IBMZ_Any;
40 version (SPARC)   version = SPARC_Any;
41 version (SPARC64) version = SPARC_Any;
42 version (SystemZ) version = IBMZ_Any;
43 version (X86)     version = X86_Any;
44 version (X86_64)  version = X86_Any;
45 
46 version (Posix):
47 extern (C) nothrow @nogc:
48 
49 //
50 // Required
51 //
52 /*
53 socklen_t
54 sa_family_t
55 
56 struct sockaddr
57 {
58     sa_family_t sa_family;
59     char        sa_data[];
60 }
61 
62 struct sockaddr_storage
63 {
64     sa_family_t ss_family;
65 }
66 
67 struct msghdr
68 {
69     void*         msg_name;
70     socklen_t     msg_namelen;
71     struct iovec* msg_iov;
72     int           msg_iovlen;
73     void*         msg_control;
74     socklen_t     msg_controllen;
75     int           msg_flags;
76 }
77 
78 struct iovec {} // from core.sys.posix.sys.uio
79 
80 struct cmsghdr
81 {
82     socklen_t cmsg_len;
83     int       cmsg_level;
84     int       cmsg_type;
85 }
86 
87 SCM_RIGHTS
88 
89 CMSG_DATA(cmsg)
90 CMSG_NXTHDR(mhdr,cmsg)
91 CMSG_FIRSTHDR(mhdr)
92 
93 struct linger
94 {
95     int l_onoff;
96     int l_linger;
97 }
98 
99 SOCK_DGRAM
100 SOCK_SEQPACKET
101 SOCK_STREAM
102 
103 SOL_SOCKET
104 
105 SO_ACCEPTCONN
106 SO_BROADCAST
107 SO_DEBUG
108 SO_DONTROUTE
109 SO_ERROR
110 SO_KEEPALIVE
111 SO_LINGER
112 SO_OOBINLINE
113 SO_RCVBUF
114 SO_RCVLOWAT
115 SO_RCVTIMEO
116 SO_REUSEADDR
117 SO_SNDBUF
118 SO_SNDLOWAT
119 SO_SNDTIMEO
120 SO_TYPE
121 
122 SOMAXCONN
123 
124 MSG_CTRUNC
125 MSG_DONTROUTE
126 MSG_EOR
127 MSG_OOB
128 MSG_PEEK
129 MSG_TRUNC
130 MSG_WAITALL
131 
132 AF_INET
133 AF_UNIX
134 AF_UNSPEC
135 
136 SHUT_RD
137 SHUT_RDWR
138 SHUT_WR
139 */
140 
141 version (linux)
142 {
143     alias uint   socklen_t;
144     alias ushort sa_family_t;
145 
146     struct sockaddr
147     {
148         sa_family_t sa_family;
149         byte[14]    sa_data;
150     }
151 
152     private enum : size_t
153     {
154         _SS_SIZE    = 128,
155         _SS_PADSIZE = _SS_SIZE - c_ulong.sizeof - sa_family_t.sizeof
156     }
157 
158     struct sockaddr_storage
159     {
160         sa_family_t ss_family;
161         byte[_SS_PADSIZE] __ss_padding;
162         c_ulong     __ss_align;
163     }
164 
165     struct msghdr
166     {
167         void*     msg_name;
168         socklen_t msg_namelen;
169         iovec*    msg_iov;
170         size_t    msg_iovlen;
171         void*     msg_control;
172         size_t    msg_controllen;
173         int       msg_flags;
174     }
175 
176     struct cmsghdr
177     {
178         size_t cmsg_len;
179         int    cmsg_level;
180         int    cmsg_type;
181     }
182 
183     enum : uint
184     {
185         SCM_RIGHTS = 0x01
186     }
187 
188     extern (D) inout(ubyte)*   CMSG_DATA( return scope inout(cmsghdr)* cmsg ) pure nothrow @nogc @system { return cast(ubyte*)( cmsg + 1 ); }
189 
190     version (CRuntime_Musl)
191     {
192         extern (D)
193         {
194             private size_t __CMSG_LEN(inout(cmsghdr)* cmsg) pure nothrow @nogc
195             {
196                 return (cmsg.cmsg_len + size_t.sizeof -1) & cast(size_t)(~(size_t.sizeof - 1));
197             }
198 
199             private inout(cmsghdr)* __CMSG_NEXT(inout(cmsghdr)* cmsg) pure nothrow @system @nogc
200             {
201                 return cmsg + __CMSG_LEN(cmsg);
202             }
203 
204             private inout(msghdr)* __MHDR_END(inout(msghdr)* mhdr) pure nothrow @system @nogc
205             {
206                 return cast(inout(msghdr)*)(mhdr.msg_control + mhdr.msg_controllen);
207             }
208 
209             inout(cmsghdr)* CMSG_NXTHDR(inout(msghdr)* msg, inout(cmsghdr)* cmsg) pure nothrow @nogc
210             {
211                 return cmsg.cmsg_len < cmsghdr.sizeof ||
212                     __CMSG_LEN(cmsg) + cmsghdr.sizeof >= __MHDR_END(msg) - cast(inout(msghdr)*)(cmsg)
213                         ? cast(inout(cmsghdr)*) null : cast(inout(cmsghdr)*) __CMSG_NEXT(cmsg);
214             }
215         }
216     }
217     else
218     {
219         private inout(cmsghdr)* __cmsg_nxthdr(inout(msghdr)*, inout(cmsghdr)*) pure nothrow @nogc;
220         extern (D)  inout(cmsghdr)* CMSG_NXTHDR(inout(msghdr)* msg, inout(cmsghdr)* cmsg) pure nothrow @nogc
221         {
222             return __cmsg_nxthdr(msg, cmsg);
223         }
224     }
225 
226     extern (D) inout(cmsghdr)* CMSG_FIRSTHDR( inout(msghdr)* mhdr ) pure nothrow @nogc
227     {
228         return ( cast(size_t)mhdr.msg_controllen >= cmsghdr.sizeof
229                              ? cast(inout(cmsghdr)*) mhdr.msg_control
230                              : cast(inout(cmsghdr)*) null );
231     }
232 
233     extern (D)
234     {
235         size_t CMSG_ALIGN( size_t len ) pure nothrow @nogc
236         {
237             return (len + size_t.sizeof - 1) & cast(size_t) (~(size_t.sizeof - 1));
238         }
239 
240         size_t CMSG_LEN( size_t len ) pure nothrow @nogc
241         {
242             return CMSG_ALIGN(cmsghdr.sizeof) + len;
243         }
244     }
245 
246     extern (D) size_t CMSG_SPACE(size_t len) pure nothrow @nogc
247     {
248         return CMSG_ALIGN(len) + CMSG_ALIGN(cmsghdr.sizeof);
249     }
250 
251     struct linger
252     {
253         int l_onoff;
254         int l_linger;
255     }
256 
257     version (X86_Any)
258     {
259         enum
260         {
261             SOCK_DGRAM      = 2,
262             SOCK_SEQPACKET  = 5,
263             SOCK_STREAM     = 1
264         }
265 
266         enum
267         {
268             SOL_SOCKET      = 1
269         }
270 
271         enum
272         {
273             SO_ACCEPTCONN   = 30,
274             SO_BROADCAST    = 6,
275             SO_DEBUG        = 1,
276             SO_DONTROUTE    = 5,
277             SO_ERROR        = 4,
278             SO_KEEPALIVE    = 9,
279             SO_LINGER       = 13,
280             SO_OOBINLINE    = 10,
281             SO_RCVBUF       = 8,
282             SO_RCVLOWAT     = 18,
283             SO_RCVTIMEO     = 20,
284             SO_REUSEADDR    = 2,
285             SO_REUSEPORT    = 15,
286             SO_SNDBUF       = 7,
287             SO_SNDLOWAT     = 19,
288             SO_SNDTIMEO     = 21,
289             SO_TYPE         = 3
290         }
291     }
292     else version (HPPA_Any)
293     {
294         enum
295         {
296             SOCK_DGRAM      = 2,
297             SOCK_SEQPACKET  = 5,
298             SOCK_STREAM     = 1,
299         }
300 
301         enum
302         {
303             SOL_SOCKET      = 0xffff
304         }
305 
306         enum
307         {
308             SO_ACCEPTCONN   = 0x401c,
309             SO_BROADCAST    = 0x0020,
310             SO_DEBUG        = 0x0001,
311             SO_DONTROUTE    = 0x0010,
312             SO_ERROR        = 0x1007,
313             SO_KEEPALIVE    = 0x0008,
314             SO_LINGER       = 0x0080,
315             SO_OOBINLINE    = 0x0100,
316             SO_RCVBUF       = 0x1002,
317             SO_RCVLOWAT     = 0x1004,
318             SO_RCVTIMEO     = 0x1006,
319             SO_REUSEADDR    = 0x0004,
320             SO_SNDBUF       = 0x1001,
321             SO_SNDLOWAT     = 0x1003,
322             SO_SNDTIMEO     = 0x1005,
323             SO_TYPE         = 0x1008,
324         }
325     }
326     else version (MIPS_Any)
327     {
328         enum
329         {
330             SOCK_DGRAM      = 1,
331             SOCK_SEQPACKET  = 5,
332             SOCK_STREAM     = 2,
333         }
334 
335         enum
336         {
337             SOL_SOCKET      = 0xffff
338         }
339 
340         enum
341         {
342             SO_ACCEPTCONN   = 0x1009,
343             SO_BROADCAST    = 0x0020,
344             SO_DEBUG        = 0x0001,
345             SO_DONTROUTE    = 0x0010,
346             SO_ERROR        = 0x1007,
347             SO_KEEPALIVE    = 0x0008,
348             SO_LINGER       = 0x0080,
349             SO_OOBINLINE    = 0x0100,
350             SO_RCVBUF       = 0x1002,
351             SO_RCVLOWAT     = 0x1004,
352             SO_RCVTIMEO     = 0x1006,
353             SO_REUSEADDR    = 0x0004,
354             SO_SNDBUF       = 0x1001,
355             SO_SNDLOWAT     = 0x1003,
356             SO_SNDTIMEO     = 0x1005,
357             SO_TYPE         = 0x1008,
358         }
359     }
360     else version (PPC_Any)
361     {
362         enum
363         {
364             SOCK_DGRAM      = 2,
365             SOCK_SEQPACKET  = 5,
366             SOCK_STREAM     = 1
367         }
368 
369         enum
370         {
371             SOL_SOCKET      = 1
372         }
373 
374         enum
375         {
376             SO_ACCEPTCONN   = 30,
377             SO_BROADCAST    = 6,
378             SO_DEBUG        = 1,
379             SO_DONTROUTE    = 5,
380             SO_ERROR        = 4,
381             SO_KEEPALIVE    = 9,
382             SO_LINGER       = 13,
383             SO_OOBINLINE    = 10,
384             SO_RCVBUF       = 8,
385             SO_RCVLOWAT     = 16,
386             SO_RCVTIMEO     = 18,
387             SO_REUSEADDR    = 2,
388             SO_SNDBUF       = 7,
389             SO_SNDLOWAT     = 17,
390             SO_SNDTIMEO     = 19,
391             SO_TYPE         = 3
392         }
393     }
394     else version (ARM_Any)
395     {
396         enum
397         {
398             SOCK_DGRAM      = 2,
399             SOCK_SEQPACKET  = 5,
400             SOCK_STREAM     = 1
401         }
402 
403         enum
404         {
405             SOL_SOCKET      = 1
406         }
407 
408         enum
409         {
410             SO_ACCEPTCONN   = 30,
411             SO_BROADCAST    = 6,
412             SO_DEBUG        = 1,
413             SO_DONTROUTE    = 5,
414             SO_ERROR        = 4,
415             SO_KEEPALIVE    = 9,
416             SO_LINGER       = 13,
417             SO_OOBINLINE    = 10,
418             SO_RCVBUF       = 8,
419             SO_RCVLOWAT     = 18,
420             SO_RCVTIMEO     = 20,
421             SO_REUSEADDR    = 2,
422             SO_REUSEPORT    = 15,
423             SO_SNDBUF       = 7,
424             SO_SNDLOWAT     = 19,
425             SO_SNDTIMEO     = 21,
426             SO_TYPE         = 3
427         }
428     }
429     else version (RISCV_Any)
430     {
431         enum
432         {
433             SOCK_DGRAM      = 2,
434             SOCK_SEQPACKET  = 5,
435             SOCK_STREAM     = 1
436         }
437 
438         enum
439         {
440             SOL_SOCKET      = 1
441         }
442 
443         enum
444         {
445             SO_ACCEPTCONN   = 30,
446             SO_BROADCAST    = 6,
447             SO_DEBUG        = 1,
448             SO_DONTROUTE    = 5,
449             SO_ERROR        = 4,
450             SO_KEEPALIVE    = 9,
451             SO_LINGER       = 13,
452             SO_OOBINLINE    = 10,
453             SO_RCVBUF       = 8,
454             SO_RCVLOWAT     = 18,
455             SO_RCVTIMEO     = 20,
456             SO_REUSEADDR    = 2,
457             SO_SNDBUF       = 7,
458             SO_SNDLOWAT     = 19,
459             SO_SNDTIMEO     = 21,
460             SO_TYPE         = 3
461         }
462     }
463     else version (SPARC_Any)
464     {
465         enum
466         {
467             SOCK_DGRAM      = 2,
468             SOCK_SEQPACKET  = 5,
469             SOCK_STREAM     = 1
470         }
471 
472         enum
473         {
474             SOL_SOCKET      = 1
475         }
476 
477         enum
478         {
479             SO_ACCEPTCONN   = 30,
480             SO_BROADCAST    = 6,
481             SO_DEBUG        = 1,
482             SO_DONTROUTE    = 5,
483             SO_ERROR        = 4,
484             SO_KEEPALIVE    = 9,
485             SO_LINGER       = 13,
486             SO_OOBINLINE    = 10,
487             SO_RCVBUF       = 8,
488             SO_RCVLOWAT     = 18,
489             SO_RCVTIMEO     = 20,
490             SO_REUSEADDR    = 2,
491             SO_SNDBUF       = 7,
492             SO_SNDLOWAT     = 19,
493             SO_SNDTIMEO     = 21,
494             SO_TYPE         = 3
495         }
496     }
497     else version (IBMZ_Any)
498     {
499         enum
500         {
501             SOCK_DGRAM      = 2,
502             SOCK_SEQPACKET  = 5,
503             SOCK_STREAM     = 1
504         }
505 
506         enum
507         {
508             SOL_SOCKET      = 1
509         }
510 
511         enum
512         {
513             SO_ACCEPTCONN   = 30,
514             SO_BROADCAST    = 6,
515             SO_DEBUG        = 1,
516             SO_DONTROUTE    = 5,
517             SO_ERROR        = 4,
518             SO_KEEPALIVE    = 9,
519             SO_LINGER       = 13,
520             SO_OOBINLINE    = 10,
521             SO_RCVBUF       = 8,
522             SO_RCVLOWAT     = 18,
523             SO_RCVTIMEO     = 20,
524             SO_REUSEADDR    = 2,
525             SO_SNDBUF       = 7,
526             SO_SNDLOWAT     = 19,
527             SO_SNDTIMEO     = 21,
528             SO_TYPE         = 3
529         }
530     }
531     else version (LoongArch64)
532     {
533         enum
534         {
535             SOCK_DGRAM      = 2,
536             SOCK_SEQPACKET  = 5,
537             SOCK_STREAM     = 1
538         }
539 
540         enum
541         {
542             SOL_SOCKET      = 1
543         }
544 
545         enum
546         {
547             SO_ACCEPTCONN   = 30,
548             SO_BROADCAST    = 6,
549             SO_DEBUG        = 1,
550             SO_DONTROUTE    = 5,
551             SO_ERROR        = 4,
552             SO_KEEPALIVE    = 9,
553             SO_LINGER       = 13,
554             SO_OOBINLINE    = 10,
555             SO_RCVBUF       = 8,
556             SO_RCVLOWAT     = 18,
557             SO_RCVTIMEO     = 20,
558             SO_REUSEADDR    = 2,
559             SO_SNDBUF       = 7,
560             SO_SNDLOWAT     = 19,
561             SO_SNDTIMEO     = 21,
562             SO_TYPE         = 3
563         }
564     }
565     else
566         static assert(0, "unimplemented");
567 
568     version (CRuntime_Glibc)
569     {
570         enum
571         {
572             SOMAXCONN   = 4096
573         }
574     }
575     else
576     {
577         enum
578         {
579             SOMAXCONN   = 128
580         }
581     }
582 
583     enum : uint
584     {
585         MSG_CTRUNC      = 0x08,
586         MSG_DONTROUTE   = 0x04,
587         MSG_EOR         = 0x80,
588         MSG_OOB         = 0x01,
589         MSG_PEEK        = 0x02,
590         MSG_TRUNC       = 0x20,
591         MSG_WAITALL     = 0x100,
592         MSG_NOSIGNAL    = 0x4000
593     }
594 
595     enum
596     {
597         AF_APPLETALK    = 5,
598         AF_INET         = 2,
599         AF_IPX          = 4,
600         AF_UNIX         = 1,
601         AF_UNSPEC       = 0,
602         PF_APPLETALK    = AF_APPLETALK,
603         PF_IPX          = AF_IPX
604     }
605 
606     enum int SOCK_RDM   = 4;
607 
608     enum
609     {
610         SHUT_RD,
611         SHUT_WR,
612         SHUT_RDWR
613     }
614 }
615 else version (Darwin)
616 {
617     alias uint   socklen_t;
618     alias ubyte  sa_family_t;
619 
620     struct sockaddr
621     {
622         ubyte       sa_len;
623         sa_family_t sa_family;
624         byte[14]    sa_data;
625     }
626 
627     private enum : size_t
628     {
629         _SS_PAD1    = long.sizeof - ubyte.sizeof - sa_family_t.sizeof,
630         _SS_PAD2    = 128 - ubyte.sizeof - sa_family_t.sizeof - _SS_PAD1 - long.sizeof
631     }
632 
633     struct sockaddr_storage
634     {
635          ubyte          ss_len;
636          sa_family_t    ss_family;
637          byte[_SS_PAD1] __ss_pad1;
638          long           __ss_align;
639          byte[_SS_PAD2] __ss_pad2;
640     }
641 
642     struct msghdr
643     {
644         void*     msg_name;
645         socklen_t msg_namelen;
646         iovec*    msg_iov;
647         int       msg_iovlen;
648         void*     msg_control;
649         socklen_t msg_controllen;
650         int       msg_flags;
651     }
652 
653     struct cmsghdr
654     {
655         socklen_t  cmsg_len;
656         int        cmsg_level;
657         int        cmsg_type;
658     }
659 
660 
661     extern (D)
662     {
663         socklen_t CMSG_ALIGN(socklen_t len) pure nothrow @nogc { return (len + socklen_t.sizeof - 1) & cast(socklen_t) (~(socklen_t.sizeof - 1)); }
664         socklen_t CMSG_SPACE(socklen_t len) pure nothrow @nogc { return CMSG_ALIGN(len) + CMSG_ALIGN(cmsghdr.sizeof); }
665         socklen_t CMSG_LEN(socklen_t len) pure nothrow @nogc { return CMSG_ALIGN(cmsghdr.sizeof) + len; }
666 
667         inout(ubyte)*   CMSG_DATA( return scope inout(cmsghdr)* cmsg ) pure nothrow @nogc @system { return cast(ubyte*)( cmsg + 1 ); }
668 
669         inout(cmsghdr)* CMSG_FIRSTHDR( inout(msghdr)* mhdr ) pure nothrow @nogc @system
670         {
671             return ( cast(socklen_t)mhdr.msg_controllen >= cmsghdr.sizeof ? cast(inout(cmsghdr)*) mhdr.msg_control : cast(inout(cmsghdr)*) null );
672         }
673     }
674 
675     enum : uint
676     {
677         SCM_RIGHTS = 0x01
678     }
679 
680     struct linger
681     {
682         int l_onoff;
683         int l_linger;
684     }
685 
686     enum
687     {
688         SOCK_DGRAM      = 2,
689         SOCK_RDM        = 4,
690         SOCK_SEQPACKET  = 5,
691         SOCK_STREAM     = 1
692     }
693 
694     enum : uint
695     {
696         SOL_SOCKET      = 0xffff
697     }
698 
699     enum : uint
700     {
701         SO_ACCEPTCONN   = 0x0002,
702         SO_BROADCAST    = 0x0020,
703         SO_DEBUG        = 0x0001,
704         SO_DONTROUTE    = 0x0010,
705         SO_ERROR        = 0x1007,
706         SO_KEEPALIVE    = 0x0008,
707         SO_LINGER       = 0x1080,
708         SO_NOSIGPIPE    = 0x1022, // non-standard
709         SO_OOBINLINE    = 0x0100,
710         SO_RCVBUF       = 0x1002,
711         SO_RCVLOWAT     = 0x1004,
712         SO_RCVTIMEO     = 0x1006,
713         SO_REUSEADDR    = 0x0004,
714         SO_REUSEPORT    = 0x0200,
715         SO_SNDBUF       = 0x1001,
716         SO_SNDLOWAT     = 0x1003,
717         SO_SNDTIMEO     = 0x1005,
718         SO_TYPE         = 0x1008
719     }
720 
721     enum
722     {
723         SOMAXCONN       = 128
724     }
725 
726     enum : uint
727     {
728         MSG_CTRUNC      = 0x20,
729         MSG_DONTROUTE   = 0x4,
730         MSG_EOR         = 0x8,
731         MSG_OOB         = 0x1,
732         MSG_PEEK        = 0x2,
733         MSG_TRUNC       = 0x10,
734         MSG_WAITALL     = 0x40
735     }
736 
737     enum
738     {
739         AF_APPLETALK    = 16,
740         AF_INET         = 2,
741         AF_IPX          = 23,
742         AF_UNIX         = 1,
743         AF_UNSPEC       = 0,
744         PF_APPLETALK    = AF_APPLETALK,
745         PF_IPX          = AF_IPX
746     }
747 
748     enum
749     {
750         SHUT_RD,
751         SHUT_WR,
752         SHUT_RDWR
753     }
754 }
755 else version (FreeBSD)
756 {
757     alias uint   socklen_t;
758     alias ubyte  sa_family_t;
759 
760     struct sockaddr
761     {
762         ubyte       sa_len;
763         sa_family_t sa_family;
764         byte[14]    sa_data;
765     }
766 
767     private
768     {
769         enum _SS_ALIGNSIZE  = long.sizeof;
770         enum _SS_MAXSIZE    = 128;
771         enum _SS_PAD1SIZE   = _SS_ALIGNSIZE - ubyte.sizeof - sa_family_t.sizeof;
772         enum _SS_PAD2SIZE   = _SS_MAXSIZE - ubyte.sizeof - sa_family_t.sizeof - _SS_PAD1SIZE - _SS_ALIGNSIZE;
773     }
774 
775     struct sockaddr_storage
776     {
777          ubyte              ss_len;
778          sa_family_t        ss_family;
779          byte[_SS_PAD1SIZE] __ss_pad1;
780          long               __ss_align;
781          byte[_SS_PAD2SIZE] __ss_pad2;
782     }
783 
784     struct msghdr
785     {
786         void*     msg_name;
787         socklen_t msg_namelen;
788         iovec*    msg_iov;
789         int       msg_iovlen;
790         void*     msg_control;
791         socklen_t msg_controllen;
792         int       msg_flags;
793     }
794 
795     struct cmsghdr
796     {
797          socklen_t cmsg_len;
798          int       cmsg_level;
799          int       cmsg_type;
800     }
801 
802     enum : uint
803     {
804         SCM_RIGHTS = 0x01
805     }
806 
807     private // <machine/param.h>
808     {
809         enum _ALIGNBYTES = /+c_int+/ int.sizeof - 1;
810         extern (D) size_t _ALIGN( size_t p ) { return (p + _ALIGNBYTES) & ~_ALIGNBYTES; }
811     }
812 
813     extern (D) ubyte* CMSG_DATA( cmsghdr* cmsg ) @system
814     {
815         return cast(ubyte*) cmsg + _ALIGN( cmsghdr.sizeof );
816     }
817 
818     extern (D) cmsghdr* CMSG_NXTHDR( msghdr* mhdr, cmsghdr* cmsg ) @system
819     {
820         if ( cmsg == null )
821         {
822            return CMSG_FIRSTHDR( mhdr );
823         }
824         else
825         {
826             if ( cast(ubyte*) cmsg + _ALIGN( cmsg.cmsg_len ) + _ALIGN( cmsghdr.sizeof ) >
827                     cast(ubyte*) mhdr.msg_control + mhdr.msg_controllen )
828                 return null;
829             else
830                 return cast(cmsghdr*) (cast(ubyte*) cmsg + _ALIGN( cmsg.cmsg_len ));
831         }
832     }
833 
834     extern (D) cmsghdr* CMSG_FIRSTHDR( msghdr* mhdr ) @system
835     {
836         return mhdr.msg_controllen >= cmsghdr.sizeof ? cast(cmsghdr*) mhdr.msg_control : null;
837     }
838 
839     struct linger
840     {
841         int l_onoff;
842         int l_linger;
843     }
844 
845     enum
846     {
847         SOCK_DGRAM      = 2,
848         SOCK_RDM        = 4,
849         SOCK_SEQPACKET  = 5,
850         SOCK_STREAM     = 1
851     }
852 
853     enum : uint
854     {
855         SOL_SOCKET      = 0xffff
856     }
857 
858     enum : uint
859     {
860         SO_ACCEPTCONN   = 0x0002,
861         SO_BROADCAST    = 0x0020,
862         SO_DEBUG        = 0x0001,
863         SO_DONTROUTE    = 0x0010,
864         SO_ERROR        = 0x1007,
865         SO_KEEPALIVE    = 0x0008,
866         SO_LINGER       = 0x0080,
867         SO_NOSIGPIPE    = 0x0800, // non-standard
868         SO_OOBINLINE    = 0x0100,
869         SO_RCVBUF       = 0x1002,
870         SO_RCVLOWAT     = 0x1004,
871         SO_RCVTIMEO     = 0x1006,
872         SO_REUSEADDR    = 0x0004,
873         SO_REUSEPORT    = 0x0200,
874         SO_SNDBUF       = 0x1001,
875         SO_SNDLOWAT     = 0x1003,
876         SO_SNDTIMEO     = 0x1005,
877         SO_TYPE         = 0x1008
878     }
879 
880     enum
881     {
882         SOMAXCONN       = 128
883     }
884 
885     enum : uint
886     {
887         MSG_CTRUNC      = 0x20,
888         MSG_DONTROUTE   = 0x4,
889         MSG_EOR         = 0x8,
890         MSG_OOB         = 0x1,
891         MSG_PEEK        = 0x2,
892         MSG_TRUNC       = 0x10,
893         MSG_WAITALL     = 0x40,
894         MSG_NOSIGNAL    = 0x20000
895     }
896 
897     enum
898     {
899         AF_APPLETALK    = 16,
900         AF_INET         = 2,
901         AF_IPX          = 23,
902         AF_UNIX         = 1,
903         AF_UNSPEC       = 0
904     }
905 
906     enum
907     {
908         SHUT_RD = 0,
909         SHUT_WR = 1,
910         SHUT_RDWR = 2
911     }
912 }
913 else version (NetBSD)
914 {
915     alias uint   socklen_t;
916     alias ubyte  sa_family_t;
917 
918     struct sockaddr
919     {
920         ubyte       sa_len;
921         sa_family_t sa_family;
922         byte[14]    sa_data;
923     }
924 
925     private
926     {
927         enum _SS_ALIGNSIZE  = long.sizeof;
928         enum _SS_MAXSIZE    = 128;
929         enum _SS_PAD1SIZE   = _SS_ALIGNSIZE - ubyte.sizeof - sa_family_t.sizeof;
930         enum _SS_PAD2SIZE   = _SS_MAXSIZE - ubyte.sizeof - sa_family_t.sizeof - _SS_PAD1SIZE - _SS_ALIGNSIZE;
931     }
932 
933     struct sockaddr_storage
934     {
935          ubyte              ss_len;
936          sa_family_t        ss_family;
937          byte[_SS_PAD1SIZE] __ss_pad1;
938          long               __ss_align;
939          byte[_SS_PAD2SIZE] __ss_pad2;
940     }
941 
942     struct msghdr
943     {
944         void*     msg_name;
945         socklen_t msg_namelen;
946         iovec*    msg_iov;
947         int       msg_iovlen;
948         void*     msg_control;
949         socklen_t msg_controllen;
950         int       msg_flags;
951     }
952 
953     struct cmsghdr
954     {
955          socklen_t cmsg_len;
956          int       cmsg_level;
957          int       cmsg_type;
958     }
959 
960     enum : uint
961     {
962         SCM_RIGHTS = 0x01
963     }
964 
965     private // <machine/param.h>
966     {
967         enum _ALIGNBYTES = /+c_int+/ int.sizeof - 1;
968         extern (D) size_t _ALIGN( size_t p ) { return (p + _ALIGNBYTES) & ~_ALIGNBYTES; }
969     }
970 
971     extern (D) ubyte* CMSG_DATA( cmsghdr* cmsg )
972     {
973         return cast(ubyte*) cmsg + _ALIGN( cmsghdr.sizeof );
974     }
975 
976     extern (D) cmsghdr* CMSG_NXTHDR( msghdr* mhdr, cmsghdr* cmsg )
977     {
978         if ( cmsg == null )
979         {
980            return CMSG_FIRSTHDR( mhdr );
981         }
982         else
983         {
984             if ( cast(ubyte*) cmsg + _ALIGN( cmsg.cmsg_len ) + _ALIGN( cmsghdr.sizeof ) >
985                     cast(ubyte*) mhdr.msg_control + mhdr.msg_controllen )
986                 return null;
987             else
988                 return cast(cmsghdr*) (cast(ubyte*) cmsg + _ALIGN( cmsg.cmsg_len ));
989         }
990     }
991 
992     extern (D) cmsghdr* CMSG_FIRSTHDR( msghdr* mhdr )
993     {
994         return mhdr.msg_controllen >= cmsghdr.sizeof ? cast(cmsghdr*) mhdr.msg_control : null;
995     }
996 
997     struct linger
998     {
999         int l_onoff;
1000         int l_linger;
1001     }
1002 
1003     enum
1004     {
1005         SOCK_DGRAM      = 2,
1006         SOCK_RDM        = 4,
1007         SOCK_SEQPACKET  = 5,
1008         SOCK_STREAM     = 1
1009     }
1010 
1011     enum : uint
1012     {
1013         SOL_SOCKET      = 0xffff
1014     }
1015 
1016     enum : uint
1017     {
1018          SO_DEBUG        = 0x0001,          /* turn on debugging info recording */
1019          SO_ACCEPTCONN   = 0x0002,          /* socket has had listen() */
1020          SO_REUSEADDR    = 0x0004,          /* allow local address reuse */
1021          SO_KEEPALIVE    = 0x0008,          /* keep connections alive */
1022          SO_DONTROUTE    = 0x0010,          /* just use interface addresses */
1023          SO_BROADCAST    = 0x0020,          /* permit sending of broadcast msgs */
1024          SO_USELOOPBACK  = 0x0040,          /* bypass hardware when possible */
1025          SO_LINGER       = 0x0080,          /* linger on close if data present */
1026          SO_OOBINLINE    = 0x0100,          /* leave received OOB data in line */
1027          SO_REUSEPORT    = 0x0200,          /* allow local address & port reuse */
1028         /*      SO_OTIMESTAMP   0x0400          */
1029          SO_NOSIGPIPE    = 0x0800,          /* no SIGPIPE from EPIPE */
1030          SO_ACCEPTFILTER = 0x1000,          /* there is an accept filter */
1031          SO_TIMESTAMP    = 0x2000,          /* timestamp received dgram traffic */
1032 
1033         /*
1034          * Additional options, not kept in so_options.
1035          */
1036          SO_SNDBUF       = 0x1001,          /* send buffer size */
1037          SO_RCVBUF       = 0x1002,          /* receive buffer size */
1038          SO_SNDLOWAT     = 0x1003,          /* send low-water mark */
1039          SO_RCVLOWAT     = 0x1004,          /* receive low-water mark */
1040         /* SO_OSNDTIMEO         0x1005 */
1041         /* SO_ORCVTIMEO         0x1006 */
1042          SO_ERROR        = 0x1007,          /* get error status and clear */
1043          SO_TYPE         = 0x1008,          /* get socket type */
1044          SO_OVERFLOWED   = 0x1009,          /* datagrams: return packets dropped */
1045 
1046          SO_NOHEADER     = 0x100a,          /* user supplies no header to kernel;
1047                                                  * kernel removes header and supplies
1048                                                  * payload
1049                                                  */
1050          SO_SNDTIMEO     = 0x100b,          /* send timeout */
1051          SO_RCVTIMEO     = 0x100c          /* receive timeout */
1052 
1053     }
1054 
1055     enum
1056     {
1057         SOMAXCONN       = 128
1058     }
1059 
1060     enum : uint
1061     {
1062          MSG_OOB         = 0x0001,          /* process out-of-band data */
1063          MSG_PEEK        = 0x0002,          /* peek at incoming message */
1064          MSG_DONTROUTE   = 0x0004,          /* send without using routing tables */
1065          MSG_EOR         = 0x0008,          /* data completes record */
1066          MSG_TRUNC       = 0x0010,          /* data discarded before delivery */
1067          MSG_CTRUNC      = 0x0020,          /* control data lost before delivery */
1068          MSG_WAITALL     = 0x0040,          /* wait for full request or error */
1069          MSG_DONTWAIT    = 0x0080,          /* this message should be nonblocking */
1070          MSG_BCAST       = 0x0100,          /* this message was rcvd using link-level brdcst */
1071          MSG_MCAST       = 0x0200,          /* this message was rcvd using link-level mcast */
1072          MSG_NOSIGNAL    = 0x0400          /* do not generate SIGPIPE on EOF */
1073     }
1074 
1075     enum
1076     {
1077         AF_APPLETALK    = 16,
1078         AF_INET         = 2,
1079         AF_IPX          = 23,
1080         AF_UNIX         = 1,
1081         AF_UNSPEC       = 0
1082     }
1083 
1084     enum
1085     {
1086         SHUT_RD = 0,
1087         SHUT_WR = 1,
1088         SHUT_RDWR = 2
1089     }
1090 }
1091 else version (OpenBSD)
1092 {
1093     alias uint   socklen_t;
1094     alias ubyte  sa_family_t;
1095 
1096     struct sockaddr
1097     {
1098         ubyte       sa_len;
1099         sa_family_t sa_family;
1100         byte[14]    sa_data;
1101     }
1102 
1103     struct sockaddr_storage
1104     {
1105          ubyte       ss_len;
1106          sa_family_t ss_family;
1107          ubyte[6]    __ss_pad1;
1108          long        __ss_align;
1109          ubyte[240]  __ss_pad2;
1110     }
1111 
1112     struct msghdr
1113     {
1114         void*     msg_name;
1115         socklen_t msg_namelen;
1116         iovec*    msg_iov;
1117         uint      msg_iovlen;
1118         void*     msg_control;
1119         socklen_t msg_controllen;
1120         int       msg_flags;
1121     }
1122 
1123     struct cmsghdr
1124     {
1125          socklen_t cmsg_len;
1126          int       cmsg_level;
1127          int       cmsg_type;
1128     }
1129 
1130     enum : uint
1131     {
1132         SCM_RIGHTS    = 0x01,
1133         SCM_TIMESTAMP = 0x04
1134     }
1135 
1136     private // <sys/_types.h>
1137     {
1138         enum _ALIGNBYTES = c_long.sizeof - 1;
1139         extern (D) size_t _ALIGN(size_t p) pure nothrow @nogc
1140         {
1141             return (p + _ALIGNBYTES) & ~_ALIGNBYTES;
1142         }
1143     }
1144 
1145     extern (D) ubyte* CMSG_DATA(cmsghdr* cmsg) pure nothrow @nogc
1146     {
1147         return cast(ubyte*) cmsg + _ALIGN(cmsghdr.sizeof);
1148     }
1149 
1150     extern (D) cmsghdr* CMSG_NXTHDR(msghdr* mhdr, cmsghdr* cmsg) pure nothrow @nogc
1151     {
1152         if (cast(ubyte*) cmsg + _ALIGN(cmsg.cmsg_len) + _ALIGN(cmsghdr.sizeof) >
1153                 cast(ubyte*) mhdr.msg_control + mhdr.msg_controllen)
1154             return null;
1155         else
1156             return cast(cmsghdr*) (cast(ubyte*) cmsg + _ALIGN(cmsg.cmsg_len));
1157     }
1158 
1159     extern (D) cmsghdr* CMSG_FIRSTHDR(msghdr* mhdr) pure nothrow @nogc
1160     {
1161         return mhdr.msg_controllen >= cmsghdr.sizeof ? cast(cmsghdr*) mhdr.msg_control : null;
1162     }
1163 
1164     extern (D)
1165     {
1166         size_t CMSG_LEN(size_t len) pure nothrow @nogc
1167         {
1168             return _ALIGN(cmsghdr.sizeof) + len;
1169         }
1170     }
1171 
1172     extern (D) size_t CMSG_SPACE(size_t len) pure nothrow @nogc
1173     {
1174         return _ALIGN(cmsghdr.sizeof) + _ALIGN(len);
1175     }
1176 
1177     struct linger
1178     {
1179         int l_onoff;
1180         int l_linger;
1181     }
1182 
1183     enum
1184     {
1185         SOCK_DGRAM      = 2,
1186         SOCK_RDM        = 4,
1187         SOCK_SEQPACKET  = 5,
1188         SOCK_STREAM     = 1
1189     }
1190 
1191     enum : uint
1192     {
1193         SOL_SOCKET      = 0xffff
1194     }
1195 
1196     enum : uint
1197     {
1198          SO_DEBUG        = 0x0001,
1199          SO_ACCEPTCONN   = 0x0002,
1200          SO_REUSEADDR    = 0x0004,
1201          SO_KEEPALIVE    = 0x0008,
1202          SO_DONTROUTE    = 0x0010,
1203          SO_BROADCAST    = 0x0020,
1204          SO_USELOOPBACK  = 0x0040,
1205          SO_LINGER       = 0x0080,
1206          SO_OOBINLINE    = 0x0100,
1207          SO_REUSEPORT    = 0x0200,
1208          SO_TIMESTAMP    = 0x0800,
1209          SO_BINDANY      = 0x1000,
1210          SO_ZEROSIZE     = 0x2000,
1211 
1212          SO_SNDBUF       = 0x1001,
1213          SO_RCVBUF       = 0x1002,
1214          SO_SNDLOWAT     = 0x1003,
1215          SO_RCVLOWAT     = 0x1004,
1216          SO_SNDTIMEO     = 0x1005,
1217          SO_RCVTIMEO     = 0x1006,
1218          SO_ERROR        = 0x1007,
1219          SO_TYPE         = 0x1008,
1220          SO_NETPROC      = 0x1020,
1221          SO_RTABLE       = 0x1021,
1222          SO_PEERCRED     = 0x1022,
1223          SO_SPLICE       = 0x1023,
1224     }
1225 
1226     enum
1227     {
1228         SOMAXCONN       = 128
1229     }
1230 
1231     enum : uint
1232     {
1233          MSG_OOB          = 0x001,
1234          MSG_PEEK         = 0x002,
1235          MSG_DONTROUTE    = 0x004,
1236          MSG_EOR          = 0x008,
1237          MSG_TRUNC        = 0x010,
1238          MSG_CTRUNC       = 0x020,
1239          MSG_WAITALL      = 0x040,
1240          MSG_DONTWAIT     = 0x080,
1241          MSG_BCAST        = 0x100,
1242          MSG_MCAST        = 0x200,
1243          MSG_NOSIGNAL     = 0x400,
1244          MSG_CMSG_CLOEXEC = 0x800,
1245     }
1246 
1247     enum
1248     {
1249         AF_APPLETALK    = 16,
1250         AF_INET         = 2,
1251         AF_IPX          = 23,
1252         AF_UNIX         = 1,
1253         AF_UNSPEC       = 0
1254     }
1255 
1256     enum
1257     {
1258         SHUT_RD = 0,
1259         SHUT_WR = 1,
1260         SHUT_RDWR = 2
1261     }
1262 }
1263 else version (DragonFlyBSD)
1264 {
1265     alias uint   socklen_t;
1266     alias ubyte  sa_family_t;
1267 
1268     enum
1269     {
1270         SOCK_STREAM         = 1,
1271         SOCK_DGRAM          = 2,
1272         //SOCK_RAW          = 3,      // defined below
1273         SOCK_RDM            = 4,
1274         SOCK_SEQPACKET      = 5,
1275     }
1276 
1277     enum SOCK_CLOEXEC       = 0x10000000;
1278     enum SOCK_NONBLOCK      = 0x20000000;
1279 
1280     enum : uint
1281     {
1282         SO_DEBUG            = 0x0001,
1283         SO_ACCEPTCONN       = 0x0002,
1284         SO_REUSEADDR        = 0x0004,
1285         SO_KEEPALIVE        = 0x0008,
1286         SO_DONTROUTE        = 0x0010,
1287         SO_BROADCAST        = 0x0020,
1288         SO_USELOOPBACK      = 0x0040,
1289         SO_LINGER           = 0x0080,
1290         SO_OOBINLINE        = 0x0100,
1291         SO_REUSEPORT        = 0x0200,
1292         SO_TIMESTAMP        = 0x0400,
1293         SO_NOSIGPIPE        = 0x0800, // non-standard
1294         SO_ACCEPTFILTER     = 0x1000,
1295 
1296         SO_SNDBUF           = 0x1001,
1297         SO_RCVBUF           = 0x1002,
1298         SO_SNDLOWAT         = 0x1003,
1299         SO_RCVLOWAT         = 0x1004,
1300         SO_SNDTIMEO         = 0x1005,
1301         SO_RCVTIMEO         = 0x1006,
1302         SO_ERROR            = 0x1007,
1303         SO_TYPE             = 0x1008,
1304         SO_SNDSPACE         = 0x100a, // get appr. send buffer free space
1305         SO_CPUHINT          = 0x1030, // get socket's owner cpuid hint
1306     }
1307 
1308     struct linger
1309     {
1310         int l_onoff;
1311         int l_linger;
1312     }
1313 
1314     struct  accept_filter_arg {
1315         byte[16] af_name;
1316         byte[256-16] af_arg;
1317     }
1318 
1319     enum : uint
1320     {
1321         SOL_SOCKET          = 0xffff
1322     }
1323 
1324     enum
1325     {
1326         AF_UNSPEC           = 0,
1327         AF_LOCAL            = 1,
1328         AF_UNIX             = AF_LOCAL,
1329         AF_INET             = 2,
1330         AF_APPLETALK        = 16,
1331         AF_IPX              = 23,
1332     }
1333 
1334     struct sockaddr
1335     {
1336         ubyte               sa_len;
1337         sa_family_t         sa_family;
1338         byte[14]            sa_data;
1339     }
1340 
1341     enum SOCK_MAXADDRLEN = 255;
1342 
1343     struct sockproto {
1344         ushort              sp_family;
1345         ushort              sp_protocol;
1346     }
1347 
1348     private
1349     {
1350         enum _SS_ALIGNSIZE  = long.sizeof;
1351         enum _SS_MAXSIZE    = 128;
1352         enum _SS_PAD1SIZE   = _SS_ALIGNSIZE - ubyte.sizeof - sa_family_t.sizeof;
1353         enum _SS_PAD2SIZE   = _SS_MAXSIZE - ubyte.sizeof - sa_family_t.sizeof - _SS_PAD1SIZE - _SS_ALIGNSIZE;
1354     }
1355 
1356     struct sockaddr_storage
1357     {
1358         ubyte              ss_len;
1359         sa_family_t        ss_family;
1360         byte[_SS_PAD1SIZE] __ss_pad1;
1361         long               __ss_align;
1362         byte[_SS_PAD2SIZE] __ss_pad2;
1363     }
1364 
1365     struct msghdr
1366     {
1367         void*               msg_name;
1368         socklen_t           msg_namelen;
1369         iovec*              msg_iov;
1370         int                 msg_iovlen;
1371         void*               msg_control;
1372         socklen_t           msg_controllen;
1373         int                 msg_flags;
1374     }
1375 
1376     enum SOMAXCONN          = 128;
1377     enum SOMAXOPT_SIZE      = 65536;
1378     enum SOMAXOPT_SIZE0     = (32 * 1024 * 1024);
1379 
1380     enum : uint
1381     {
1382         MSG_OOB             = 0x00000001,
1383         MSG_PEEK            = 0x00000002,
1384         MSG_DONTROUTE       = 0x00000004,
1385         MSG_EOR             = 0x00000008,
1386         MSG_TRUNC           = 0x00000010,
1387         MSG_CTRUNC          = 0x00000020,
1388         MSG_WAITALL         = 0x00000040,
1389         MSG_DONTWAIT        = 0x00000080,
1390         MSG_EOF             = 0x00000100,
1391         MSG_UNUSED09        = 0x00000200,
1392         MSG_NOSIGNAL        = 0x00000400,
1393         MSG_SYNC            = 0x00000800,
1394         MSG_CMSG_CLOEXEC    = 0x00001000,
1395         /* These override FIONBIO.  MSG_FNONBLOCKING is functionally equivalent to MSG_DONTWAIT.*/
1396         MSG_FBLOCKING       = 0x00010000,
1397         MSG_FNONBLOCKING    = 0x00020000,
1398         MSG_FMASK           = 0xFFFF0000,
1399     }
1400 
1401     struct cmsghdr
1402     {
1403          socklen_t          cmsg_len;
1404          int                cmsg_level;
1405          int                cmsg_type;
1406     }
1407 
1408     enum CMGROUP_MAX        = 16;
1409 
1410     struct cmsgcred {
1411             pid_t           cmcred_pid;
1412             uid_t           cmcred_uid;
1413             uid_t           cmcred_euid;
1414             gid_t           cmcred_gid;
1415             short           cmcred_ngroups;
1416             gid_t[CMGROUP_MAX] cmcred_groups;
1417     }
1418 
1419     enum : uint
1420     {
1421         SCM_RIGHTS = 0x01
1422     }
1423 
1424     private // <machine/param.h>
1425     {
1426         enum _ALIGNBYTES = /+c_int+/ int.sizeof - 1;
1427         extern (D) size_t _ALIGN( size_t p ) { return (p + _ALIGNBYTES) & ~_ALIGNBYTES; }
1428     }
1429 
1430     extern (D) ubyte* CMSG_DATA( cmsghdr* cmsg )
1431     {
1432         return cast(ubyte*) cmsg + _ALIGN( cmsghdr.sizeof );
1433     }
1434 
1435     extern (D) cmsghdr* CMSG_NXTHDR( msghdr* mhdr, cmsghdr* cmsg )
1436     {
1437         if ( cmsg == null )
1438         {
1439            return CMSG_FIRSTHDR( mhdr );
1440         }
1441         else
1442         {
1443             if ( cast(ubyte*) cmsg + _ALIGN( cmsg.cmsg_len ) + _ALIGN( cmsghdr.sizeof ) >
1444                     cast(ubyte*) mhdr.msg_control + mhdr.msg_controllen )
1445                 return null;
1446             else
1447                 return cast(cmsghdr*) (cast(ubyte*) cmsg + _ALIGN( cmsg.cmsg_len ));
1448         }
1449     }
1450 
1451     extern (D) cmsghdr* CMSG_FIRSTHDR( msghdr* mhdr )
1452     {
1453         return mhdr.msg_controllen >= cmsghdr.sizeof ? cast(cmsghdr*) mhdr.msg_control : null;
1454     }
1455 
1456     enum
1457     {
1458         SHUT_RD             = 0,
1459         SHUT_WR             = 1,
1460         SHUT_RDWR           = 2
1461     }
1462 }
1463 else version (Solaris)
1464 {
1465     alias uint socklen_t;
1466     alias ushort sa_family_t;
1467 
1468     struct sockaddr
1469     {
1470         sa_family_t sa_family;
1471         char[14] sa_data = 0;
1472     }
1473 
1474     alias double sockaddr_maxalign_t;
1475 
1476     private
1477     {
1478         enum _SS_ALIGNSIZE  = sockaddr_maxalign_t.sizeof;
1479         enum _SS_MAXSIZE    = 256;
1480         enum _SS_PAD1SIZE   = _SS_ALIGNSIZE - sa_family_t.sizeof;
1481         enum _SS_PAD2SIZE   = _SS_MAXSIZE - sa_family_t.sizeof + _SS_PAD1SIZE + _SS_ALIGNSIZE;
1482     }
1483 
1484     struct sockaddr_storage
1485     {
1486          sa_family_t ss_family;
1487          char[_SS_PAD1SIZE] _ss_pad1 = void;
1488          sockaddr_maxalign_t _ss_align;
1489          char[_SS_PAD2SIZE] _ss_pad2 = void;
1490     }
1491 
1492     struct msghdr
1493     {
1494         void* msg_name;
1495         socklen_t msg_namelen;
1496         iovec* msg_iov;
1497         int msg_iovlen;
1498         void* msg_control;
1499         socklen_t msg_controllen;
1500         int msg_flags;
1501     }
1502 
1503     struct cmsghdr
1504     {
1505          socklen_t cmsg_len;
1506          int cmsg_level;
1507          int cmsg_type;
1508     }
1509 
1510     enum : uint
1511     {
1512         SCM_RIGHTS = 0x1010
1513     }
1514 
1515     // FIXME: CMSG_DATA, CMSG_NXTHDR, CMSG_FIRSTHDR missing
1516 
1517     struct linger
1518     {
1519         int l_onoff;
1520         int l_linger;
1521     }
1522 
1523     enum
1524     {
1525         SOCK_STREAM = 2,
1526         SOCK_DGRAM = 1,
1527         SOCK_RDM = 5,
1528         SOCK_SEQPACKET = 6,
1529     }
1530 
1531     enum : uint
1532     {
1533         SOL_SOCKET      = 0xffff
1534     }
1535 
1536     enum : uint
1537     {
1538         SO_ACCEPTCONN   = 0x0002,
1539         SO_BROADCAST    = 0x0020,
1540         SO_DEBUG        = 0x0001,
1541         SO_DONTROUTE    = 0x0010,
1542         SO_ERROR        = 0x1007,
1543         SO_KEEPALIVE    = 0x0008,
1544         SO_LINGER       = 0x0080,
1545         SO_OOBINLINE    = 0x0100,
1546         SO_RCVBUF       = 0x1002,
1547         SO_RCVLOWAT     = 0x1004,
1548         SO_RCVTIMEO     = 0x1006,
1549         SO_REUSEADDR    = 0x0004,
1550         SO_SNDBUF       = 0x1001,
1551         SO_SNDLOWAT     = 0x1003,
1552         SO_SNDTIMEO     = 0x1005,
1553         SO_TYPE         = 0x1008,
1554 
1555         SO_USELOOPBACK  = 0x0040, // non-standard
1556         SO_DGRAM_ERRIND = 0x0200, // non-standard
1557         SO_RECVUCRED    = 0x0400, // non-standard
1558     }
1559 
1560     enum
1561     {
1562         SOMAXCONN = 128
1563     }
1564 
1565     enum : uint
1566     {
1567         MSG_CTRUNC      = 0x10,
1568         MSG_DONTROUTE   = 0x4,
1569         MSG_EOR         = 0x8,
1570         MSG_OOB         = 0x1,
1571         MSG_PEEK        = 0x2,
1572         MSG_TRUNC       = 0x20,
1573         MSG_WAITALL     = 0x40
1574     }
1575 
1576     enum
1577     {
1578         AF_IPX          = 23,
1579         AF_APPLETALK    = 16,
1580         AF_INET         = 2,
1581         AF_UNIX         = 1,
1582         AF_UNSPEC       = 0
1583     }
1584 
1585     enum
1586     {
1587         SHUT_RD,
1588         SHUT_WR,
1589         SHUT_RDWR
1590     }
1591 }
1592 else
1593 {
1594     static assert(false, "Unsupported platform");
1595 }
1596 
1597 /*
1598 int     accept(int, sockaddr*, socklen_t*);
1599 int     bind(int, const scope sockaddr*, socklen_t);
1600 int     connect(int, const scope sockaddr*, socklen_t);
1601 int     getpeername(int, sockaddr*, socklen_t*);
1602 int     getsockname(int, sockaddr*, socklen_t*);
1603 int     getsockopt(int, int, int, void*, socklen_t*);
1604 int     listen(int, int);
1605 ssize_t recv(int, void*, size_t, int);
1606 ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
1607 ssize_t recvmsg(int, msghdr*, int);
1608 ssize_t send(int, const scope void*, size_t, int);
1609 ssize_t sendmsg(int, const scope msghdr*, int);
1610 ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1611 int     setsockopt(int, int, int, const scope void*, socklen_t);
1612 int     shutdown(int, int);
1613 int     socket(int, int, int);
1614 int     sockatmark(int);
1615 int     socketpair(int, int, int, ref int[2]);
1616 */
1617 
1618 version (CRuntime_Glibc)
1619 {
1620     int     accept(int, scope sockaddr*, scope socklen_t*);
1621     int     bind(int, const scope sockaddr*, socklen_t);
1622     int     connect(int, const scope sockaddr*, socklen_t);
1623     int     getpeername(int, scope sockaddr*, scope socklen_t*);
1624     int     getsockname(int, scope sockaddr*, scope socklen_t*);
1625     int     getsockopt(int, int, int, scope void*, scope socklen_t*);
1626     int     listen(int, int) @safe;
1627     ssize_t recv(int, scope void*, size_t, int);
1628     ssize_t recvfrom(int, scope void*, size_t, int, scope sockaddr*, scope socklen_t*);
1629     ssize_t recvmsg(int, scope msghdr*, int);
1630     ssize_t send(int, const scope void*, size_t, int);
1631     ssize_t sendmsg(int, const scope msghdr*, int);
1632     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1633     int     setsockopt(int, int, int, const scope void*, socklen_t);
1634     int     shutdown(int, int) @safe;
1635     int     socket(int, int, int) @safe;
1636     int     sockatmark(int) @safe;
1637     int     socketpair(int, int, int, ref int[2]) @safe;
1638 }
1639 else version (Darwin)
1640 {
1641     int     accept(int, scope sockaddr*, scope socklen_t*);
1642     int     bind(int, const scope sockaddr*, socklen_t);
1643     int     connect(int, const scope sockaddr*, socklen_t);
1644     int     getpeername(int, scope sockaddr*, scope socklen_t*);
1645     int     getsockname(int, scope sockaddr*, scope socklen_t*);
1646     int     getsockopt(int, int, int, scope void*, scope socklen_t*);
1647     int     listen(int, int) @safe;
1648     ssize_t recv(int, scope void*, size_t, int);
1649     ssize_t recvfrom(int, scope void*, size_t, int, scope sockaddr*, scope socklen_t*);
1650     ssize_t recvmsg(int, scope msghdr*, int);
1651     ssize_t send(int, const scope void*, size_t, int);
1652     ssize_t sendmsg(int, const scope msghdr*, int);
1653     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1654     int     setsockopt(int, int, int, const scope void*, socklen_t);
1655     int     shutdown(int, int) @safe;
1656     int     socket(int, int, int) @safe;
1657     int     sockatmark(int) @safe;
1658     int     socketpair(int, int, int, ref int[2]) @safe;
1659 }
1660 else version (FreeBSD)
1661 {
1662     int     accept(int, scope sockaddr*, scope socklen_t*);
1663     int     bind(int, const scope sockaddr*, socklen_t);
1664     int     connect(int, const scope sockaddr*, socklen_t);
1665     int     getpeername(int, scope sockaddr*, scope socklen_t*);
1666     int     getsockname(int, scope sockaddr*, scope socklen_t*);
1667     int     getsockopt(int, int, int, scope void*, scope socklen_t*);
1668     int     listen(int, int) @safe;
1669     ssize_t recv(int, scope void*, size_t, int);
1670     ssize_t recvfrom(int, scope void*, size_t, int, scope sockaddr*, scope socklen_t*);
1671     ssize_t recvmsg(int, scope msghdr*, int);
1672     ssize_t send(int, const scope void*, size_t, int);
1673     ssize_t sendmsg(int, const scope msghdr*, int);
1674     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1675     int     setsockopt(int, int, int, const scope void*, socklen_t);
1676     int     shutdown(int, int) @safe;
1677     int     socket(int, int, int) @safe;
1678     int     sockatmark(int) @safe;
1679     int     socketpair(int, int, int, ref int[2]) @safe;
1680 }
1681 else version (NetBSD)
1682 {
1683     int     accept(int, scope sockaddr*, scope socklen_t*);
1684     int     bind(int, const scope sockaddr*, socklen_t);
1685     int     connect(int, const scope sockaddr*, socklen_t);
1686     int     getpeername(int, scope sockaddr*, scope socklen_t*);
1687     int     getsockname(int, scope sockaddr*, scope socklen_t*);
1688     int     getsockopt(int, int, int, scope void*, scope socklen_t*);
1689     int     listen(int, int) @safe;
1690     ssize_t recv(int, scope void*, size_t, int);
1691     ssize_t recvfrom(int, scope void*, size_t, int, scope sockaddr*, scope socklen_t*);
1692     ssize_t recvmsg(int, scope msghdr*, int);
1693     ssize_t send(int, const scope void*, size_t, int);
1694     ssize_t sendmsg(int, const scope msghdr*, int);
1695     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1696     int     setsockopt(int, int, int, const scope void*, socklen_t);
1697     int     shutdown(int, int) @safe;
1698     int     socket(int, int, int) @safe;
1699     int     sockatmark(int) @safe;
1700     int     socketpair(int, int, int, ref int[2]) @safe;
1701 }
1702 else version (OpenBSD)
1703 {
1704     int     accept(int, scope sockaddr*, scope socklen_t*);
1705     int     bind(int, const scope sockaddr*, socklen_t);
1706     int     connect(int, const scope sockaddr*, socklen_t);
1707     int     getpeername(int, scope sockaddr*, scope socklen_t*);
1708     int     getsockname(int, scope sockaddr*, scope socklen_t*);
1709     int     getsockopt(int, int, int, scope void*, scope socklen_t*);
1710     int     listen(int, int) @safe;
1711     ssize_t recv(int, scope void*, size_t, int);
1712     ssize_t recvfrom(int, scope void*, size_t, int, scope sockaddr*, scope socklen_t*);
1713     ssize_t recvmsg(int, scope msghdr*, int);
1714     ssize_t send(int, const scope void*, size_t, int);
1715     ssize_t sendmsg(int, const scope msghdr*, int);
1716     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1717     int     setsockopt(int, int, int, const scope void*, socklen_t);
1718     int     shutdown(int, int) @safe;
1719     int     socket(int, int, int) @safe;
1720     int     sockatmark(int) @safe;
1721     int     socketpair(int, int, int, ref int[2]) @safe;
1722 }
1723 else version (DragonFlyBSD)
1724 {
1725     int     accept(int, sockaddr*, socklen_t*);
1726 //    int     accept4(int, sockaddr*, socklen_t*, int);
1727     int     bind(int, const scope sockaddr*, socklen_t);
1728     int     connect(int, const scope sockaddr*, socklen_t);
1729 //    int     extconnect(int, int, sockaddr*, socklen_t);
1730     int     getpeername(int, sockaddr*, socklen_t*);
1731     int     getsockname(int, sockaddr*, socklen_t*);
1732     int     getsockopt(int, int, int, void*, socklen_t*);
1733     int     listen(int, int);
1734     ssize_t recv(int, void*, size_t, int);
1735     ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
1736     ssize_t recvmsg(int, msghdr*, int);
1737     ssize_t send(int, const scope void*, size_t, int);
1738     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1739     ssize_t sendmsg(int, const scope msghdr*, int);
1740 //    int     sendfile(int, int, off_t, size_t, sf_hdtr *, off_t *, int);
1741     int     setsockopt(int, int, int, const scope void*, socklen_t);
1742     int     shutdown(int, int);
1743     int     sockatmark(int);
1744     int     socket(int, int, int);
1745     int     socketpair(int, int, int, ref int[2]);
1746 //  void    pfctlinput(int, struct sockaddr *);
1747 }
1748 else version (Solaris)
1749 {
1750     int     accept(int, scope sockaddr*, scope socklen_t*);
1751     int     bind(int, const scope sockaddr*, socklen_t);
1752     int     connect(int, const scope sockaddr*, socklen_t);
1753     int     getpeername(int, scope sockaddr*, scope socklen_t*);
1754     int     getsockname(int, scope sockaddr*, scope socklen_t*);
1755     int     getsockopt(int, int, int, scope void*, scope socklen_t*);
1756     int     listen(int, int) @safe;
1757     ssize_t recv(int, scope void*, size_t, int);
1758     ssize_t recvfrom(int, scope void*, size_t, int, scope sockaddr*, scope socklen_t*);
1759     ssize_t recvmsg(int, scope msghdr*, int);
1760     ssize_t send(int, const scope void*, size_t, int);
1761     ssize_t sendmsg(int, const scope msghdr*, int);
1762     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1763     int     setsockopt(int, int, int, const scope void*, socklen_t);
1764     int     shutdown(int, int) @safe;
1765     int     socket(int, int, int) @safe;
1766     int     sockatmark(int) @safe;
1767     int     socketpair(int, int, int, ref int[2]) @safe;
1768 }
1769 else version (CRuntime_Bionic)
1770 {
1771     int     accept(int, scope sockaddr*, scope socklen_t*);
1772     int     bind(int, const scope sockaddr*, socklen_t);
1773     int     connect(int, const scope sockaddr*, socklen_t);
1774     int     getpeername(int, scope sockaddr*, scope socklen_t*);
1775     int     getsockname(int, scope sockaddr*, scope socklen_t*);
1776     int     getsockopt(int, int, int, scope void*, scope socklen_t*);
1777     int     listen(int, int) @safe;
1778     ssize_t recv(int, scope void*, size_t, int);
1779     ssize_t recvfrom(int, scope void*, size_t, int, scope sockaddr*, scope socklen_t*);
1780     int     recvmsg(int, scope msghdr*, int);
1781     ssize_t send(int, const scope void*, size_t, int);
1782     int     sendmsg(int, const scope msghdr*, int);
1783     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1784     int     setsockopt(int, int, int, const scope void*, socklen_t);
1785     int     shutdown(int, int) @safe;
1786     int     socket(int, int, int) @safe;
1787     int     sockatmark(int) @safe;
1788     int     socketpair(int, int, int, ref int[2]) @safe;
1789 }
1790 else version (CRuntime_Musl)
1791 {
1792     int     accept(int, sockaddr*, socklen_t*);
1793     int     bind(int, const scope sockaddr*, socklen_t);
1794     int     connect(int, const scope sockaddr*, socklen_t);
1795     int     getpeername(int, sockaddr*, socklen_t*);
1796     int     getsockname(int, sockaddr*, socklen_t*);
1797     int     getsockopt(int, int, int, void*, socklen_t*);
1798     int     listen(int, int);
1799     ssize_t recv(int, void*, size_t, int);
1800     ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
1801     ssize_t recvmsg(int, msghdr*, int);
1802     ssize_t send(int, const scope void*, size_t, int);
1803     ssize_t sendmsg(int, const scope msghdr*, int);
1804     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1805     int     setsockopt(int, int, int, const scope void*, socklen_t);
1806     int     shutdown(int, int);
1807     int     socket(int, int, int);
1808     int     sockatmark(int);
1809     int     socketpair(int, int, int, ref int[2]);
1810 }
1811 else version (CRuntime_UClibc)
1812 {
1813     int     accept(int, sockaddr*, socklen_t*);
1814     int     bind(int, const scope sockaddr*, socklen_t);
1815     int     connect(int, const scope sockaddr*, socklen_t);
1816     int     getpeername(int, sockaddr*, socklen_t*);
1817     int     getsockname(int, sockaddr*, socklen_t*);
1818     int     getsockopt(int, int, int, void*, socklen_t*);
1819     int     listen(int, int);
1820     ssize_t recv(int, void*, size_t, int);
1821     ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
1822     ssize_t recvmsg(int, msghdr*, int);
1823     ssize_t send(int, const scope void*, size_t, int);
1824     ssize_t sendmsg(int, const scope msghdr*, int);
1825     ssize_t sendto(int, const scope void*, size_t, int, const scope sockaddr*, socklen_t);
1826     int     setsockopt(int, int, int, const scope void*, socklen_t);
1827     int     shutdown(int, int);
1828     int     socket(int, int, int);
1829     int     sockatmark(int);
1830     int     socketpair(int, int, int, ref int[2]);
1831 }
1832 else
1833 {
1834     static assert(false, "Unsupported platform");
1835 }
1836 
1837 //
1838 // IPV6 (IP6)
1839 //
1840 /*
1841 AF_INET6
1842 */
1843 
1844 version (linux)
1845 {
1846     enum
1847     {
1848         AF_INET6    = 10
1849     }
1850 }
1851 else version (Darwin)
1852 {
1853     enum
1854     {
1855         AF_INET6    = 30
1856     }
1857 }
1858 else version (FreeBSD)
1859 {
1860     enum
1861     {
1862         AF_INET6    = 28
1863     }
1864 }
1865 else version (NetBSD)
1866 {
1867     enum
1868     {
1869         AF_INET6    = 24
1870     }
1871 }
1872 else version (OpenBSD)
1873 {
1874     enum
1875     {
1876         AF_INET6    = 24
1877     }
1878 }
1879 else version (DragonFlyBSD)
1880 {
1881     enum
1882     {
1883         AF_INET6    = 28
1884     }
1885 }
1886 else version (Solaris)
1887 {
1888     enum
1889     {
1890         AF_INET6 = 26,
1891     }
1892 }
1893 else
1894 {
1895     static assert(false, "Unsupported platform");
1896 }
1897 
1898 //
1899 // Raw Sockets (RS)
1900 //
1901 /*
1902 SOCK_RAW
1903 */
1904 
1905 version (linux)
1906 {
1907     enum
1908     {
1909         SOCK_RAW    = 3
1910     }
1911 }
1912 else version (Darwin)
1913 {
1914     enum
1915     {
1916         SOCK_RAW    = 3
1917     }
1918 }
1919 else version (FreeBSD)
1920 {
1921     enum
1922     {
1923         SOCK_RAW    = 3
1924     }
1925 }
1926 else version (NetBSD)
1927 {
1928     enum
1929     {
1930         SOCK_RAW    = 3
1931     }
1932 }
1933 else version (OpenBSD)
1934 {
1935     enum
1936     {
1937         SOCK_RAW    = 3
1938     }
1939 }
1940 else version (DragonFlyBSD)
1941 {
1942     enum
1943     {
1944         SOCK_RAW    = 3
1945     }
1946 }
1947 else version (Solaris)
1948 {
1949     enum
1950     {
1951         SOCK_RAW = 4,
1952     }
1953 }
1954 else
1955 {
1956     static assert(false, "Unsupported platform");
1957 }