PageRenderTime 189ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tango/stdc/posix/sys/socket.d

https://github.com/SiegeLord/Tango-D2
D | 498 lines | 311 code | 45 blank | 142 comment | 8 complexity | 569302454e75d20ee6490c9a125d726d MD5 | raw file
  1. /**
  2. * D header file for POSIX.
  3. *
  4. * Copyright: Public Domain
  5. * License: Public Domain
  6. * Authors: Sean Kelly
  7. * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
  8. */
  9. module tango.stdc.posix.sys.socket;
  10. private import tango.stdc.posix.config;
  11. public import tango.stdc.posix.sys.types; // for ssize_t, size_t
  12. public import tango.stdc.posix.sys.uio; // for iovec
  13. public import tango.sys.consts.socket;
  14. extern (C):
  15. //
  16. // Required
  17. //
  18. /*
  19. socklen_t
  20. sa_family_t
  21. struct sockaddr
  22. {
  23. sa_family_t sa_family;
  24. char sa_data[];
  25. }
  26. struct sockaddr_storage
  27. {
  28. sa_family_t ss_family;
  29. }
  30. struct msghdr
  31. {
  32. void* msg_name;
  33. socklen_t msg_namelen;
  34. struct iovec* msg_iov;
  35. int msg_iovlen;
  36. void* msg_control;
  37. socklen_t msg_controllen;
  38. int msg_flags;
  39. }
  40. struct iovec {} // from tango.stdc.posix.sys.uio
  41. struct cmsghdr
  42. {
  43. socklen_t cmsg_len;
  44. int cmsg_level;
  45. int cmsg_type;
  46. }
  47. SCM_RIGHTS
  48. CMSG_DATA(cmsg)
  49. CMSG_NXTHDR(mhdr,cmsg)
  50. CMSG_FIRSTHDR(mhdr)
  51. struct linger
  52. {
  53. int l_onoff;
  54. int l_linger;
  55. }
  56. SOCK_DGRAM
  57. SOCK_SEQPACKET
  58. SOCK_STREAM
  59. SOL_SOCKET
  60. SO_ACCEPTCONN
  61. SO_BROADCAST
  62. SO_DEBUG
  63. SO_DONTROUTE
  64. SO_ERROR
  65. SO_KEEPALIVE
  66. SO_LINGER
  67. SO_OOBINLINE
  68. SO_RCVBUF
  69. SO_RCVLOWAT
  70. SO_RCVTIMEO
  71. SO_REUSEADDR
  72. SO_SNDBUF
  73. SO_SNDLOWAT
  74. SO_SNDTIMEO
  75. SO_TYPE
  76. SOMAXCONN
  77. MSG_CTRUNC
  78. MSG_DONTROUTE
  79. MSG_EOR
  80. MSG_OOB
  81. MSG_PEEK
  82. MSG_TRUNC
  83. MSG_WAITALL
  84. AF_INET
  85. AF_UNIX
  86. AF_UNSPEC
  87. SHUT_RD
  88. SHUT_RDWR
  89. SHUT_WR
  90. int accept(int, sockaddr*, socklen_t*);
  91. int bind(int, in sockaddr*, socklen_t);
  92. int connect(int, in sockaddr*, socklen_t);
  93. int getpeername(int, sockaddr*, socklen_t*);
  94. int getsockname(int, sockaddr*, socklen_t*);
  95. int getsockopt(int, int, int, void*, socklen_t*);
  96. int listen(int, int);
  97. ssize_t recv(int, void*, size_t, int);
  98. ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
  99. ssize_t recvmsg(int, msghdr*, int);
  100. ssize_t send(int, in void*, size_t, int);
  101. ssize_t sendmsg(int, in msghdr*, int);
  102. ssize_t sendto(int, in void*, size_t, int, in sockaddr*, socklen_t);
  103. int setsockopt(int, int, int, in void*, socklen_t);
  104. int shutdown(int, int);
  105. int socket(int, int, int);
  106. int sockatmark(int);
  107. int socketpair(int, int, int, int[2]);
  108. */
  109. version( linux )
  110. {
  111. alias uint socklen_t;
  112. alias ushort sa_family_t;
  113. struct sockaddr
  114. {
  115. sa_family_t sa_family;
  116. byte[14] sa_data;
  117. }
  118. private enum : size_t
  119. {
  120. _SS_SIZE = 128,
  121. _SS_PADSIZE = _SS_SIZE - (c_ulong.sizeof * 2)
  122. }
  123. struct sockaddr_storage
  124. {
  125. sa_family_t ss_family;
  126. c_ulong __ss_align;
  127. byte[_SS_PADSIZE] __ss_padding;
  128. }
  129. struct msghdr
  130. {
  131. void* msg_name;
  132. socklen_t msg_namelen;
  133. iovec* msg_iov;
  134. size_t msg_iovlen;
  135. void* msg_control;
  136. size_t msg_controllen;
  137. int msg_flags;
  138. }
  139. struct cmsghdr
  140. {
  141. size_t cmsg_len;
  142. int cmsg_level;
  143. int cmsg_type;
  144. static if( false /* (!is( __STRICT_ANSI__ ) && __GNUC__ >= 2) || __STDC_VERSION__ >= 199901L */ )
  145. {
  146. ubyte[1] __cmsg_data;
  147. }
  148. }
  149. static if( false /* (!is( __STRICT_ANSI__ ) && __GNUC__ >= 2) || __STDC_VERSION__ >= 199901L */ )
  150. {
  151. extern (D) ubyte[1] CMSG_DATA( cmsghdr* cmsg ) { return cmsg.__cmsg_data; }
  152. }
  153. else
  154. {
  155. extern (D) ubyte* CMSG_DATA( cmsghdr* cmsg ) { return cast(ubyte*)( cmsg + 1 ); }
  156. }
  157. private cmsghdr* __cmsg_nxthdr(msghdr*, cmsghdr*);
  158. alias __cmsg_nxthdr CMSG_NXTHDR;
  159. extern (D) size_t CMSG_FIRSTHDR( msghdr* mhdr )
  160. {
  161. return ( mhdr.msg_controllen >= cmsghdr.sizeof
  162. ? cast(size_t) mhdr.msg_control
  163. : cast(size_t) 0 );
  164. }
  165. struct linger
  166. {
  167. int l_onoff;
  168. int l_linger;
  169. }
  170. int accept(int, sockaddr*, socklen_t*);
  171. int bind(int, in sockaddr*, socklen_t);
  172. int connect(int, in sockaddr*, socklen_t);
  173. int getpeername(int, sockaddr*, socklen_t*);
  174. int getsockname(int, sockaddr*, socklen_t*);
  175. int getsockopt(int, int, int, void*, socklen_t*);
  176. int listen(int, int);
  177. ssize_t recv(int, void*, size_t, int);
  178. ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
  179. ssize_t recvmsg(int, msghdr*, int);
  180. ssize_t send(int, in void*, size_t, int);
  181. ssize_t sendmsg(int, in msghdr*, int);
  182. ssize_t sendto(int, in void*, size_t, int, in sockaddr*, socklen_t);
  183. int setsockopt(int, int, int, in void*, socklen_t);
  184. int shutdown(int, int);
  185. int socket(int, int, int);
  186. int sockatmark(int);
  187. int socketpair(int, int, int, int[2]);
  188. }
  189. else version(OSX)
  190. {
  191. alias uint socklen_t;
  192. alias ubyte sa_family_t;
  193. struct sockaddr
  194. {
  195. ubyte sa_len;
  196. sa_family_t sa_family;
  197. byte[14] sa_data;
  198. }
  199. private enum : size_t
  200. {
  201. _SS_PAD1 = long.sizeof - ubyte.sizeof - sa_family_t.sizeof,
  202. _SS_PAD2 = 128 - ubyte.sizeof - sa_family_t.sizeof - _SS_PAD1 - long.sizeof
  203. }
  204. struct sockaddr_storage
  205. {
  206. ubyte ss_len;
  207. sa_family_t ss_family;
  208. byte[_SS_PAD1] __ss_pad1;
  209. long __ss_align;
  210. byte[_SS_PAD2] __ss_pad2;
  211. }
  212. struct msghdr
  213. {
  214. void* msg_name;
  215. socklen_t msg_namelen;
  216. iovec* msg_iov;
  217. int msg_iovlen;
  218. void* msg_control;
  219. socklen_t msg_controllen;
  220. int msg_flags;
  221. }
  222. struct cmsghdr
  223. {
  224. socklen_t cmsg_len;
  225. int cmsg_level;
  226. int cmsg_type;
  227. }
  228. /+
  229. CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
  230. ALIGN(sizeof(struct cmsghdr)))
  231. CMSG_NXTHDR(mhdr, cmsg) \
  232. (((unsigned char *)(cmsg) + ALIGN((cmsg)->cmsg_len) + \
  233. ALIGN(sizeof(struct cmsghdr)) > \
  234. (unsigned char *)(mhdr)->msg_control +(mhdr)->msg_controllen) ? \
  235. (struct cmsghdr *)0 /* NULL */ : \
  236. (struct cmsghdr *)((unsigned char *)(cmsg) + ALIGN((cmsg)->cmsg_len)))
  237. CMSG_FIRSTHDR(mhdr) ((struct cmsghdr *)(mhdr)->msg_control)
  238. +/
  239. struct linger
  240. {
  241. int l_onoff;
  242. int l_linger;
  243. }
  244. int accept(int, sockaddr*, socklen_t*);
  245. int bind(int, in sockaddr*, socklen_t);
  246. int connect(int, in sockaddr*, socklen_t);
  247. int getpeername(int, sockaddr*, socklen_t*);
  248. int getsockname(int, sockaddr*, socklen_t*);
  249. int getsockopt(int, int, int, void*, socklen_t*);
  250. int listen(int, int);
  251. ssize_t recv(int, void*, size_t, int);
  252. ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
  253. ssize_t recvmsg(int, msghdr*, int);
  254. ssize_t send(int, in void*, size_t, int);
  255. ssize_t sendmsg(int, in msghdr*, int);
  256. ssize_t sendto(int, in void*, size_t, int, in sockaddr*, socklen_t);
  257. int setsockopt(int, int, int, in void*, socklen_t);
  258. int shutdown(int, int);
  259. int socket(int, int, int);
  260. int sockatmark(int);
  261. int socketpair(int, int, int, int[2]);
  262. }
  263. else version( FreeBSD )
  264. {
  265. alias uint socklen_t;
  266. alias ubyte sa_family_t;
  267. struct sockaddr
  268. {
  269. ubyte sa_len;
  270. sa_family_t sa_family;
  271. byte[14] sa_data;
  272. }
  273. private
  274. {
  275. const _SS_ALIGNSIZE = long.sizeof;
  276. const uint _SS_MAXSIZE = 128;
  277. const _SS_PAD1SIZE = _SS_ALIGNSIZE - ubyte.sizeof - sa_family_t.sizeof;
  278. const _SS_PAD2SIZE = _SS_MAXSIZE - ubyte.sizeof - sa_family_t.sizeof - _SS_PAD1SIZE - _SS_ALIGNSIZE;
  279. }
  280. struct sockaddr_storage
  281. {
  282. ubyte ss_len;
  283. sa_family_t ss_family;
  284. byte[_SS_PAD1SIZE] __ss_pad1;
  285. long __ss_align;
  286. byte[_SS_PAD2SIZE] __ss_pad2;
  287. }
  288. struct msghdr
  289. {
  290. void* msg_name;
  291. socklen_t msg_namelen;
  292. iovec* msg_iov;
  293. int msg_iovlen;
  294. void* msg_control;
  295. socklen_t msg_controllen;
  296. int msg_flags;
  297. }
  298. struct cmsghdr
  299. {
  300. socklen_t cmsg_len;
  301. int cmsg_level;
  302. int cmsg_type;
  303. }
  304. /+
  305. CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
  306. ALIGN(sizeof(struct cmsghdr)))
  307. CMSG_NXTHDR(mhdr, cmsg) \
  308. (((unsigned char *)(cmsg) + ALIGN((cmsg)->cmsg_len) + \
  309. ALIGN(sizeof(struct cmsghdr)) > \
  310. (unsigned char *)(mhdr)->msg_control +(mhdr)->msg_controllen) ? \
  311. (struct cmsghdr *)0 /* NULL */ : \
  312. (struct cmsghdr *)((unsigned char *)(cmsg) + ALIGN((cmsg)->cmsg_len)))
  313. CMSG_FIRSTHDR(mhdr) ((struct cmsghdr *)(mhdr)->msg_control)
  314. +/
  315. struct linger
  316. {
  317. int l_onoff;
  318. int l_linger;
  319. }
  320. int accept(int, sockaddr*, socklen_t*);
  321. int bind(int, in sockaddr*, socklen_t);
  322. int connect(int, in sockaddr*, socklen_t);
  323. int getpeername(int, sockaddr*, socklen_t*);
  324. int getsockname(int, sockaddr*, socklen_t*);
  325. int getsockopt(int, int, int, void*, socklen_t*);
  326. int listen(int, int);
  327. ssize_t recv(int, void*, size_t, int);
  328. ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
  329. ssize_t recvmsg(int, msghdr*, int);
  330. ssize_t send(int, in void*, size_t, int);
  331. ssize_t sendmsg(int, in msghdr*, int);
  332. ssize_t sendto(int, in void*, size_t, int, in sockaddr*, socklen_t);
  333. int setsockopt(int, int, int, in void*, socklen_t);
  334. int shutdown(int, int);
  335. int socket(int, int, int);
  336. int sockatmark(int);
  337. int socketpair(int, int, int, int[2]);
  338. }
  339. else version( solaris )
  340. {
  341. alias uint socklen_t;
  342. alias ushort sa_family_t;
  343. struct sockaddr
  344. {
  345. sa_family_t sa_family;
  346. char[14] sa_data;
  347. }
  348. private
  349. {
  350. alias double sockaddr_maxalign_t;
  351. const _SS_ALIGNSIZE = sockaddr_maxalign_t.sizeof;
  352. const _SS_MAXSIZE = 256;
  353. const _SS_PAD1SIZE = _SS_ALIGNSIZE - sa_family_t.sizeof;
  354. const _SS_PAD2SIZE = _SS_MAXSIZE - (sa_family_t.sizeof + _SS_PAD1SIZE + _SS_ALIGNSIZE);
  355. }
  356. struct sockaddr_storage
  357. {
  358. sa_family_t ss_family; /* Address family */
  359. /* Following fields are implementation specific */
  360. char[_SS_PAD1SIZE] _ss_pad1;
  361. sockaddr_maxalign_t _ss_align;
  362. char[_SS_PAD2SIZE] _ss_pad2;
  363. }
  364. struct msghdr
  365. {
  366. void* msg_name;
  367. socklen_t msg_namelen;
  368. iovec* msg_iov;
  369. int msg_iovlen;
  370. void* msg_control;
  371. socklen_t msg_controllen;
  372. int msg_flags;
  373. }
  374. struct iovec {} // from tango.stdc.posix.sys.uio
  375. struct cmsghdr
  376. {
  377. socklen_t cmsg_len;
  378. int cmsg_level;
  379. int cmsg_type;
  380. }
  381. private
  382. {
  383. const _CMSG_DATA_ALIGNMENT = int.sizeof;
  384. version (X86) const _CMSG_HDR_ALIGNMENT = 4;
  385. else version(X86_64) const _CMSG_HDR_ALIGNMENT = 4;
  386. else /* SPARC */ const _CMSG_HDR_ALIGNMENT = 8;
  387. extern (D)
  388. {
  389. private ubyte* _CMSG_DATA_ALIGN(cmsghdr* x) {
  390. return cast(ubyte*)((cast(size_t)x + _CMSG_DATA_ALIGNMENT - 1) & ~(_CMSG_DATA_ALIGNMENT - 1));
  391. }
  392. private size_t _CMSG_HDR_ALIGN(cmsghdr* x) {
  393. return (cast(size_t)x + _CMSG_HDR_ALIGNMENT - 1) & ~(_CMSG_HDR_ALIGNMENT - 1);
  394. }
  395. }
  396. }
  397. extern (D) ubyte* CMSG_DATA( cmsghdr* cmsg ) { return cast(ubyte*)_CMSG_DATA_ALIGN( cmsg + 1 ); }
  398. extern (D) cmsghdr* CMSG_FIRSTHDR( msghdr* mhdr )
  399. {
  400. return mhdr.msg_controllen >= cmsghdr.sizeof
  401. ? cast(cmsghdr*) mhdr.msg_control
  402. : null;
  403. }
  404. extern (D) cmsghdr* CMSG_NXTHDR( msghdr* m, cmsghdr* c )
  405. {
  406. /* Hurrah for unreadable C macros! */
  407. if(c is null) return CMSG_FIRSTHDR(m);
  408. size_t aligned_cmsg = _CMSG_HDR_ALIGN(c);
  409. return
  410. (aligned_cmsg + c.cmsg_len + cmsghdr.sizeof) > (cast(size_t)m.msg_control + m.msg_controllen)
  411. ? null
  412. : cast(cmsghdr*)(aligned_cmsg + c.cmsg_len);
  413. }
  414. struct linger
  415. {
  416. int l_onoff;
  417. int l_linger;
  418. }
  419. int accept(int, sockaddr*, socklen_t*);
  420. int bind(int, in sockaddr*, socklen_t);
  421. int connect(int, in sockaddr*, socklen_t);
  422. int getpeername(int, sockaddr*, socklen_t*);
  423. int getsockname(int, sockaddr*, socklen_t*);
  424. int getsockopt(int, int, int, void*, socklen_t*);
  425. int listen(int, int);
  426. ssize_t recv(int, void*, size_t, int);
  427. ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*);
  428. ssize_t recvmsg(int, msghdr*, int);
  429. ssize_t send(int, in void*, size_t, int);
  430. ssize_t sendmsg(int, in msghdr*, int);
  431. ssize_t sendto(int, in void*, size_t, int, in sockaddr*, socklen_t);
  432. int setsockopt(int, int, int, in void*, socklen_t);
  433. int shutdown(int, int);
  434. int socket(int, int, int);
  435. int sockatmark(int);
  436. int socketpair(int, int, int, int[2]);
  437. }