PageRenderTime 356ms CodeModel.GetById 27ms RepoModel.GetById 7ms app.codeStats 0ms

/libphobos/libdruntime/core/sys/posix/sys/socket.d

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