socket.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. socket.h
  8. Abstract:
  9. This header contains definitions for socket-based communication endpoints.
  10. Author:
  11. Evan Green 3-May-2013
  12. --*/
  13. #ifndef _SOCKET_H
  14. #define _SOCKET_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <sys/uio.h>
  19. #include <sys/ioctl.h>
  20. //
  21. // --------------------------------------------------------------------- Macros
  22. //
  23. //
  24. // This macro evaluates to a pointer to the ancillary data following a cmsghdr
  25. // structure.
  26. //
  27. #define CMSG_DATA(_Control) \
  28. ((unsigned char *)((struct cmsghdr *)(_Control) + 1))
  29. //
  30. // This macro advances a cmsghdr pointer to the next cmsghdr, or assigns it to
  31. // NULL if it is the last one. The first parameter is a pointer to the original
  32. // msghdr.
  33. //
  34. #define CMSG_NXTHDR(_Message, _Control) __cmsg_nxthdr((_Message), (_Control))
  35. //
  36. // This macro evaluates to the first cmsghdr given a msghdr structure, or
  37. // NULL if there is no data.
  38. //
  39. #define CMSG_FIRSTHDR(_Message) \
  40. (((_Message)->msg_controllen >= sizeof(struct cmsghdr)) ? \
  41. (struct cmsghdr *)(_Message)->msg_control : \
  42. NULL)
  43. //
  44. // This macro returns the required alignment for a given length. This is a
  45. // constant expression.
  46. //
  47. #define CMSG_ALIGN(_Length) \
  48. (((_Length) + sizeof(size_t) - 1) & (size_t)~(sizeof(size_t) - 1))
  49. //
  50. // This macro returns the number of bytes an ancillary element with the given
  51. // payload size takes up. This is a constant expression.
  52. //
  53. #define CMSG_SPACE(_Length) \
  54. (CMSG_ALIGN(_Length) + CMSG_ALIGN(sizeof(struct cmsghdr)))
  55. //
  56. // This macro returns the value to store in the cmsghdr length member, taking
  57. // into account any necessary alignment. It takes the data length as an
  58. // argument. This is a constant expression.
  59. //
  60. #define CMSG_LEN(_Length) \
  61. (CMSG_ALIGN(sizeof(struct cmsghdr)) + (_Length))
  62. //
  63. // ---------------------------------------------------------------- Definitions
  64. //
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. //
  69. // Define the valid address families.
  70. //
  71. #define AF_UNSPEC 0
  72. #define AF_UNIX 1
  73. #define AF_LOCAL AF_UNIX
  74. #define AF_INET 2
  75. #define AF_INET6 3
  76. #define AF_LINK 5
  77. //
  78. // Define valid protocol families as the same as the address families.
  79. //
  80. #define PF_UNSPEC AF_UNSPEC
  81. #define PF_UNIX AF_UNIX
  82. #define PF_LOCAL AF_LOCAL
  83. #define PF_INET AF_INET
  84. #define PF_INET6 AF_INET6
  85. #define PF_LINK AF_LINK
  86. //
  87. // Define the socket types.
  88. //
  89. //
  90. // Datagram sockets provide connectionless unreliable packets of a fixed
  91. // maximum length.
  92. //
  93. #define SOCK_DGRAM 1
  94. //
  95. // Raw sockets snoop all traffic.
  96. //
  97. #define SOCK_RAW 2
  98. //
  99. // Sequenced packet sockets provide reliable bidirectional connection-based
  100. // tranmission paths for records.
  101. //
  102. #define SOCK_SEQPACKET 3
  103. //
  104. // Streams provide reliable bidirectional connection-mode byte streams, and may
  105. // provide a transmission mechanism for out of band data.
  106. //
  107. #define SOCK_STREAM 4
  108. //
  109. // Define flags to the accept4 function.
  110. //
  111. //
  112. // Set this flag to request that the new descriptor be created with the
  113. // non-blocking flag set.
  114. //
  115. #define SOCK_NONBLOCK 0x00001000
  116. //
  117. // Set this flag to request that the new descriptor be created with the
  118. // close-on-execute flag set.
  119. //
  120. #define SOCK_CLOEXEC 0x00004000
  121. //
  122. // Define flags that can be passed to the send and recv functions.
  123. //
  124. //
  125. // Peeks at an incoming message without officially receiving it. The data is
  126. // treated as unread and the next recv or similar function call still returns
  127. // the same data.
  128. //
  129. #define MSG_PEEK 0x00000001
  130. //
  131. // Requests out-of-band data. The significance and semantics of out-of-band
  132. // data are protocol-specific. This flag is also returned by the kernel when
  133. // out-of-band data is received.
  134. //
  135. #define MSG_OOB 0x00000002
  136. //
  137. // On SOCK_STREAM sockets this requests that the function block until the full
  138. // amount of data can be returned. The function may return the smaller amount
  139. // of data if the socket is a message-based socket, if a signal is caught, if
  140. // the connection is terminated, is MSG_PEEK was specified, or if an error is
  141. // pending for the socket.
  142. //
  143. #define MSG_WAITALL 0x00000004
  144. //
  145. // This flag indicates a complete message, used by sequential packet sockets.
  146. // This flag can be set by user-mode on transmit and kernel-mode on receive.
  147. //
  148. #define MSG_EOR 0x00000008
  149. //
  150. // This flag is returned by the kernel when the trailing portion of the
  151. // datagram was discarded because the datagram was larger than the buffer
  152. // supplied.
  153. //
  154. #define MSG_TRUNC 0x00000010
  155. //
  156. // This flag is returned by the kernel when some control/ancillary data is
  157. // discarded due to lack of space in the provided ancillary buffer.
  158. //
  159. #define MSG_CTRUNC 0x00000020
  160. //
  161. // This flag requests not to send a broken pipe signal on stream oriented
  162. // sockets when the other end breaks the connection. The broken pipe status
  163. // is still returned.
  164. //
  165. #define MSG_NOSIGNAL 0x00000040
  166. //
  167. // This flag requests that the operation not block.
  168. //
  169. #define MSG_DONTWAIT 0x00000080
  170. //
  171. // This flag requests that routing tables not be used when sending a packet.
  172. // This limits the system to sending the packet across networks that are
  173. // directly connected.
  174. //
  175. #define MSG_DONTROUTE 0x00000100
  176. //
  177. // Define the shutdown types. Read closes the socket for further reading, write
  178. // closes the socket for further writing, and rdwr closes the socket for both
  179. // reading and writing.
  180. //
  181. #define SHUT_RD 0
  182. #define SHUT_WR 1
  183. #define SHUT_RDWR 2
  184. //
  185. // Define socket level control message types, currently only used by local
  186. // sockets.
  187. //
  188. //
  189. // This control message type allows the passing of file descriptors.
  190. //
  191. #define SCM_RIGHTS 1
  192. //
  193. // This control message type allows the passing of credentials.
  194. //
  195. #define SCM_CREDENTIALS 2
  196. //
  197. // Define socket options.
  198. //
  199. //
  200. // This options reports whether or not socket listening is enabled. The option
  201. // value is an int boolean and is read only.
  202. //
  203. #define SO_ACCEPTCONN 1
  204. //
  205. // This option permits the sending of broadcast messages, if supported by the
  206. // protocol. The option value is an int boolean.
  207. //
  208. #define SO_BROADCAST 2
  209. //
  210. // This option turns on recording of debugging information in the protocol. The
  211. // option value is an int boolean.
  212. //
  213. #define SO_DEBUG 3
  214. //
  215. // This option requests that outgoing messages bypass the standard routing
  216. // facilities. The destination shall assumed to be directly connected, and
  217. // messages are directed to the appropriate network interface based on the
  218. // destination address. The affect, if any, depends on what protocl is in use.
  219. // This option takes an int boolean value.
  220. //
  221. #define SO_DONTROUTE 4
  222. //
  223. // This option reports information about the error status and clears it. The
  224. // option value is an int.
  225. //
  226. #define SO_ERROR 5
  227. //
  228. // This option keeps connections active by enabling the periodic transmission
  229. // of messages, if supported by the protocol. This option takes an int boolean.
  230. // If the connected socket fails to respond to these messages, the connection
  231. // is broken and threads writing to the socket are notified with a SIGPIPE
  232. // signal.
  233. //
  234. #define SO_KEEPALIVE 6
  235. //
  236. // This option lingers on a close function if data is present. This option
  237. // controls the action taken when unsent messages queue on a socket and close
  238. // is performed. If this option is set, the system blocks the calling thread
  239. // during close until it can transmit the data or until the time expires. If
  240. // this option is not set and close is called, the system handles the call in
  241. // a way that allows the calling thread to continue as quickly as possible. The
  242. // option takes a linger structure to specify the state of the option and
  243. // linger interval.
  244. //
  245. #define SO_LINGER 7
  246. //
  247. // This option leaves out-of-band data (data marked urgent) inline. The option
  248. // value is an integer boolean.
  249. //
  250. #define SO_OOBINLINE 8
  251. //
  252. // This option sets the receive buffer size. It taks an int value.
  253. //
  254. #define SO_RCVBUF 9
  255. //
  256. // This option sets the minimum number of bytes to process for socket input
  257. // operations. The default value is 1 byte. If this is set to a larger value,
  258. // blocking receive calls normally wait until they have received the smaller of
  259. // the low water mark or the requested amount. They may return less than the
  260. // low water mark if an error or signal occurs. This option takes an int value.
  261. //
  262. #define SO_RCVLOWAT 10
  263. //
  264. // This option sets the maximum amount of time an input function waits until it
  265. // completes. The value is a timeval structure specifying how long to wait
  266. // before returning with whatever data was collected, if any. The default value
  267. // is zero, meaning the receive operation does not time out.
  268. //
  269. #define SO_RCVTIMEO 11
  270. //
  271. // This option sets the send buffer size. It takes an int value.
  272. //
  273. #define SO_SNDBUF 12
  274. //
  275. // This option sets the minimum number of bytes to process for socket output
  276. // operations. Non-blocking output operations shall process no data if flow
  277. // control does not allow the smaller of the send low water mark value or the
  278. // entire request to be processed. This option takes an int value.
  279. //
  280. #define SO_SNDLOWAT 13
  281. //
  282. // This option sets maximum amount of time an output function would block
  283. // because flow control is preventing data from being sent. If a send operation
  284. // has blocked for this time, it shall return with a partial count or 0 if no
  285. // data was sent. The default value is 0, indicating that send operations do
  286. // not time out. This option takes a timeval structure.
  287. //
  288. #define SO_SNDTIMEO 14
  289. //
  290. // This option reports the socket type. The value is an int.
  291. //
  292. #define SO_TYPE 15
  293. //
  294. // Despite its name, when enabled, this option allows the socket to bind to
  295. // the same local port as an existing socket as long as one of them is bound to
  296. // the any address and the other is bound to a different local address (i.e.
  297. // they cannot both be bound to the any address). Additionally, this option
  298. // allows the socket to bind to the exact same local address and port as an
  299. // existing socket if the existing socket is in the time-wait state. Both
  300. // sockets must have this option set for it to take effect. This option takes
  301. // an int boolean.
  302. //
  303. #define SO_REUSEADDR 16
  304. //
  305. // This option allows a socket to bind to the exact same local address and
  306. // port as an existing socket. Both sockets must have the option set for it to
  307. // take effect. This option takes an int boolean.
  308. //
  309. #define SO_REUSEPORT 17
  310. //
  311. // This option determines whether or not to send and receive credentials
  312. // automatically in the control data. This only applies to local sockets.
  313. //
  314. #define SO_PASSCRED 18
  315. //
  316. // This option returns the credentials of the foreign socket at the time of
  317. // connect. This only applies to local sockets. The argument is a pointer to
  318. // a ucred structure.
  319. //
  320. #define SO_PEERCRED 19
  321. //
  322. // Define the level number for the get/setsockopts function to apply to the
  323. // socket itself.
  324. //
  325. #define SOL_SOCKET 0xFFFF
  326. //
  327. // Define socket ioctl numbers.
  328. //
  329. //
  330. // This ioctl returns a non-zero integer if the inbound data stream is at the
  331. // urgent mark. If the SO_OOBINLINE option is not and SIOCATMARK returns true,
  332. // then the next read from the socket will return the bytes following the
  333. // urgent data. Note that a read never reads across the urgent mark.
  334. //
  335. #define SIOCATMARK 0x7300
  336. //
  337. // This ioctl returns the amount of unread data in the receive buffer for
  338. // stream sockets.
  339. //
  340. #define SIOCINQ FIONREAD
  341. //
  342. // Define the maximum length of the connection backlog queue for listen calls
  343. // before the system stats refusing connection requests.
  344. //
  345. #define SOMAXCONN 512
  346. //
  347. // ------------------------------------------------------ Data Type Definitions
  348. //
  349. //
  350. // Define the unsigned integer type used for the sockaddr family type.
  351. //
  352. typedef unsigned int sa_family_t;
  353. //
  354. // Define the type used for passing the length of a socket.
  355. //
  356. typedef unsigned long socklen_t;
  357. /*++
  358. Structure Description:
  359. This structure defines a socket address.
  360. Members:
  361. sa_family - Stores the socket address family. See AF_* definitions.
  362. sa_data - Stores the address data information, which may or may not use
  363. all of the bytes available in this member.
  364. --*/
  365. struct sockaddr {
  366. sa_family_t sa_family;
  367. char sa_data[28];
  368. };
  369. /*++
  370. Structure Description:
  371. This structure defines a socket address storage structure, which is
  372. guaranteed to be as big as the biggest type of sockaddr.
  373. Members:
  374. sa_family - Stores the socket address family. See AF_* definitions.
  375. sa_data - Stores the address data information, which may or may not use
  376. all of the bytes available in this member.
  377. --*/
  378. struct sockaddr_storage {
  379. sa_family_t ss_family;
  380. char ss_data[28];
  381. };
  382. /*++
  383. Structure Description:
  384. This structure defines the linger state for a socket.
  385. Members:
  386. l_onoff - Stores a boolean value indicating whether or not lingering is
  387. enabled on socket close.
  388. l_linger - Stores the time, in seconds, that the socket is set to linger
  389. on close.
  390. --*/
  391. struct linger {
  392. int l_onoff;
  393. int l_linger;
  394. };
  395. /*++
  396. Structure Description:
  397. This structure defines a socket message.
  398. Members:
  399. msg_name - Stores a pointer to the socket address to send to or receive
  400. from.
  401. msg_namelen - Stores the size of the name buffer in bytes.
  402. msg_iov - Stores a pointer to an array of I/O vectors to do the I/O to or
  403. from.
  404. msg_iovlen - Stores the number of elements in the I/O vector.
  405. msg_control - Stores an optional pointer to the ancillary data.
  406. msg_controllen - Stores the length of the ancillary data in bytes on input.
  407. On output, this value is adjusted to indicate the actual amount of
  408. data.
  409. msg_flags - Stores a bitmask of message flags. See MSG_* for definitions.
  410. --*/
  411. struct msghdr {
  412. void *msg_name;
  413. socklen_t msg_namelen;
  414. struct iovec *msg_iov;
  415. size_t msg_iovlen;
  416. void *msg_control;
  417. socklen_t msg_controllen;
  418. int msg_flags;
  419. };
  420. /*++
  421. Structure Description:
  422. This structure defines a socket control message, the header for the socket
  423. ancillary data.
  424. Members:
  425. cmsg_len - Stores the length of the data for this message, including this
  426. structure.
  427. cmsg_level - Stores the originating protocol of the control message.
  428. cmsg_type - Stores the control message type.
  429. --*/
  430. struct cmsghdr {
  431. socklen_t cmsg_len;
  432. int cmsg_level;
  433. int cmsg_type;
  434. };
  435. /*++
  436. Structure Description:
  437. This structure defines the user credential structure used when passing a
  438. SCM_CREDENTIALS ancillary message. These credentials are checked and
  439. validated by the kernel on the sending side unless the sender has the
  440. system administrator permission.
  441. Members:
  442. pid - Stores the ID of the process that sent the message.
  443. uid - Stores the user ID of the process that sent the message.
  444. gid - Stores the group ID of the process that sent the message.
  445. --*/
  446. struct ucred {
  447. pid_t pid;
  448. uid_t uid;
  449. gid_t gid;
  450. };
  451. //
  452. // -------------------------------------------------------------------- Globals
  453. //
  454. //
  455. // -------------------------------------------------------- Function Prototypes
  456. //
  457. LIBC_API
  458. int
  459. socketpair (
  460. int Domain,
  461. int Type,
  462. int Protocol,
  463. int Sockets[2]
  464. );
  465. /*++
  466. Routine Description:
  467. This routine creates an unbound pair of connected sockets. The two sockets
  468. are identical.
  469. Arguments:
  470. Domain - Supplies the communicaion domain in which a sockets are to be
  471. created. Currently only AF_UNIX is supported for socket pairs.
  472. Type - Supplies the type of socket to be created. See the SOCK_*
  473. definitions. Common values include SOCK_STREAM and SOCK_DGRAM.
  474. Protocol - Supplies the particular protocol to use for the given domain
  475. and type. Supply 0 to use a default protocol appropriate for the
  476. specified type.
  477. Sockets - Supplies an array where the two connected sockets will be
  478. returned on success.
  479. Return Value:
  480. 0 on success.
  481. -1 on error, and the errno variable will be set to contain more information.
  482. --*/
  483. LIBC_API
  484. int
  485. socket (
  486. int Domain,
  487. int Type,
  488. int Protocol
  489. );
  490. /*++
  491. Routine Description:
  492. This routine creates a new socket for communication.
  493. Arguments:
  494. Domain - Supplies the communicaion domain in which a socket is to be
  495. created. See the AF_* or PF_* definitions. The most common values are
  496. AF_INET, AF_INET6, and AF_UNIX.
  497. Type - Supplies the type of socket to be created. See the SOCK_*
  498. definitions. Common values include SOCK_STREAM and SOCK_DGRAM.
  499. Protocol - Supplies the particular protocol to use for the given domain
  500. and type. Supply 0 to use a default protocol appropriate for the
  501. specified type.
  502. Return Value:
  503. Returns a non-negative integer representing the descriptor for the new
  504. socket.
  505. -1 on error, and the errno variable will be set to contain more information.
  506. --*/
  507. LIBC_API
  508. int
  509. bind (
  510. int Socket,
  511. const struct sockaddr *Address,
  512. socklen_t AddressLength
  513. );
  514. /*++
  515. Routine Description:
  516. This routine assigns a local socket address to a socket that currently has
  517. no local address assigned.
  518. Arguments:
  519. Socket - Supplies the file descriptor of the socket to be bound.
  520. Address - Supplies a pointer to the address to bind the socket to. The
  521. length and format depend on the address family of the socket.
  522. AddressLength - Supplies the length of the address structure in bytes.
  523. Return Value:
  524. 0 on success.
  525. -1 on error, and the errno variable will be set to contain more information.
  526. --*/
  527. LIBC_API
  528. int
  529. listen (
  530. int Socket,
  531. int Backlog
  532. );
  533. /*++
  534. Routine Description:
  535. This routine marks a connection-mode socket as ready to accept new
  536. incoming connections.
  537. Arguments:
  538. Socket - Supplies the file descriptor of the socket to be marked as
  539. listening.
  540. Backlog - Supplies a suggestion to the system as to the number of
  541. un-accepted connections to queue up before refusing additional
  542. incoming connection requests.
  543. Return Value:
  544. 0 on success.
  545. -1 on error, and the errno variable will be set to contain more information.
  546. --*/
  547. LIBC_API
  548. int
  549. accept (
  550. int Socket,
  551. struct sockaddr *Address,
  552. socklen_t *AddressLength
  553. );
  554. /*++
  555. Routine Description:
  556. This routine extracts the first pending incoming connection from the
  557. given listening socket, creates a new socket representing that connection,
  558. and allocates a file descriptor for that new socket. These newly created
  559. file descriptors are then ready for reading and writing.
  560. Arguments:
  561. Socket - Supplies the file descriptor of the listening socket to accept
  562. a connection on.
  563. Address - Supplies an optional pointer where the address of the connecting
  564. socket will be returned.
  565. AddressLength - Supplies a pointer that on input contains the length of the
  566. specified Address structure, and on output returns the length of the
  567. returned address.
  568. Return Value:
  569. Returns a non-negative file descriptor representing the new connection on
  570. success.
  571. -1 on error, and the errno variable will be set to contain more information.
  572. --*/
  573. LIBC_API
  574. int
  575. accept4 (
  576. int Socket,
  577. struct sockaddr *Address,
  578. socklen_t *AddressLength,
  579. int Flags
  580. );
  581. /*++
  582. Routine Description:
  583. This routine extracts the first pending incoming connection from the
  584. given listening socket, creates a new socket representing that connection,
  585. and allocates a file descriptor for that new socket. These newly created
  586. file descriptors are then ready for reading and writing.
  587. Arguments:
  588. Socket - Supplies the file descriptor of the listening socket to accept
  589. a connection on.
  590. Address - Supplies an optional pointer where the address of the connecting
  591. socket will be returned.
  592. AddressLength - Supplies a pointer that on input contains the length of the
  593. specified Address structure, and on output returns the length of the
  594. returned address.
  595. Flags - Supplies an optional bitfield of flags governing the newly created
  596. file descriptor. Set SOCK_CLOEXEC to set the O_CLOEXEC flag on the new
  597. descriptor, and SOCK_NONBLOCK to set the O_NONBLOCK flag on the new
  598. descriptor.
  599. Return Value:
  600. Returns a non-negative file descriptor representing the new connection on
  601. success.
  602. -1 on error, and the errno variable will be set to contain more information.
  603. --*/
  604. LIBC_API
  605. int
  606. connect (
  607. int Socket,
  608. const struct sockaddr *Address,
  609. socklen_t AddressLength
  610. );
  611. /*++
  612. Routine Description:
  613. This routine attempts to reach out and establish a connection with another
  614. socket.
  615. Arguments:
  616. Socket - Supplies the file descriptor of the socket to use for the
  617. connection.
  618. Address - Supplies a pointer to the address to connect to. The length and
  619. format depend on the address family of the socket.
  620. AddressLength - Supplies the length of the address structure in bytes.
  621. Return Value:
  622. 0 on success.
  623. -1 on error, and the errno variable will be set to contain more information.
  624. --*/
  625. LIBC_API
  626. ssize_t
  627. send (
  628. int Socket,
  629. const void *Data,
  630. size_t Length,
  631. int Flags
  632. );
  633. /*++
  634. Routine Description:
  635. This routine sends data out of a connected socket.
  636. Arguments:
  637. Socket - Supplies the file descriptor of the socket to send data out of.
  638. Data - Supplies the buffer of data to send.
  639. Length - Supplies the length of the data buffer, in bytes.
  640. Flags - Supplies a bitfield of flags governing the transmission of the data.
  641. See MSG_* definitions.
  642. Return Value:
  643. Returns the number of bytes sent on success.
  644. -1 on error, and the errno variable will be set to contain more information.
  645. --*/
  646. LIBC_API
  647. ssize_t
  648. sendto (
  649. int Socket,
  650. const void *Data,
  651. size_t Length,
  652. int Flags,
  653. const struct sockaddr *DestinationAddress,
  654. socklen_t DestinationAddressLength
  655. );
  656. /*++
  657. Routine Description:
  658. This routine sends data out of a socket, potentially to a specific
  659. destination address for connection-less sockets.
  660. Arguments:
  661. Socket - Supplies the file descriptor of the socket to send data out of.
  662. Data - Supplies the buffer of data to send.
  663. Length - Supplies the length of the data buffer, in bytes.
  664. Flags - Supplies a bitfield of flags governing the transmission of the data.
  665. See MSG_* definitions.
  666. DestinationAddress - Supplies an optional pointer to the destination
  667. address to send the data to.
  668. DestinationAddressLength - Supplies the length of the destination address
  669. structure.
  670. Return Value:
  671. Returns the number of bytes sent on success.
  672. -1 on error, and the errno variable will be set to contain more information.
  673. --*/
  674. LIBC_API
  675. ssize_t
  676. sendmsg (
  677. int Socket,
  678. const struct msghdr *Message,
  679. int Flags
  680. );
  681. /*++
  682. Routine Description:
  683. This routine sends a message out of a socket, potentially to a specific
  684. destination address for connection-less sockets. This version of the send
  685. function allows for vectored I/O and sending of ancillary data.
  686. Arguments:
  687. Socket - Supplies the file descriptor of the socket to send data out of.
  688. Message - Supplies a pointer to the message details to send.
  689. Flags - Supplies a bitfield of flags governing the transmission of the data.
  690. See MSG_* definitions.
  691. Return Value:
  692. Returns the number of bytes sent on success.
  693. -1 on error, and the errno variable will be set to contain more information.
  694. --*/
  695. LIBC_API
  696. ssize_t
  697. recv (
  698. int Socket,
  699. void *Buffer,
  700. size_t Length,
  701. int Flags
  702. );
  703. /*++
  704. Routine Description:
  705. This routine receives data from a connected socket.
  706. Arguments:
  707. Socket - Supplies the file descriptor of the socket to receive data from.
  708. Buffer - Supplies a pointer to a buffer where the received data will be
  709. returned.
  710. Length - Supplies the length of the data buffer, in bytes.
  711. Flags - Supplies a bitfield of flags governing the reception of the data.
  712. See MSG_* definitions.
  713. Return Value:
  714. Returns the number of bytes received on success.
  715. -1 on error, and the errno variable will be set to contain more information.
  716. --*/
  717. LIBC_API
  718. ssize_t
  719. recvfrom (
  720. int Socket,
  721. void *Buffer,
  722. size_t Length,
  723. int Flags,
  724. struct sockaddr *SourceAddress,
  725. socklen_t *SourceAddressLength
  726. );
  727. /*++
  728. Routine Description:
  729. This routine receives data from a socket, potentially receiving the
  730. source address for connection-less sockets.
  731. Arguments:
  732. Socket - Supplies the file descriptor of the socket to receive data from.
  733. Buffer - Supplies a pointer to a buffer where the received data will be
  734. returned.
  735. Length - Supplies the length of the data buffer, in bytes.
  736. Flags - Supplies a bitfield of flags governing the reception of the data.
  737. See MSG_* definitions.
  738. SourceAddress - Supplies an optional pointer where the source of the packet
  739. will be returned for connection-less sockets.
  740. SourceAddressLength - Supplies the length of the source address structure.
  741. Return Value:
  742. Returns the number of bytes received on success.
  743. -1 on error, and the errno variable will be set to contain more information.
  744. --*/
  745. LIBC_API
  746. ssize_t
  747. recvmsg (
  748. int Socket,
  749. struct msghdr *Message,
  750. int Flags
  751. );
  752. /*++
  753. Routine Description:
  754. This routine receives data from a socket, potentially receiving the
  755. source address for connection-less sockets. This variation of the recv
  756. function has the ability to receive vectored I/O, as well as ancillary
  757. data.
  758. Arguments:
  759. Socket - Supplies the file descriptor of the socket to receive data from.
  760. Message - Supplies a pointer to an initialized structure where the message
  761. information will be returned. The caller must initialize the
  762. appropriate members to valid buffers if the remote network address or
  763. ancillary data is desired.
  764. Flags - Supplies a bitfield of flags governing the reception of the data.
  765. See MSG_* definitions.
  766. Return Value:
  767. Returns the number of bytes received on success.
  768. -1 on error, and the errno variable will be set to contain more information.
  769. --*/
  770. LIBC_API
  771. int
  772. shutdown (
  773. int Socket,
  774. int How
  775. );
  776. /*++
  777. Routine Description:
  778. This routine shuts down all or part of a full-duplex socket connection.
  779. Arguments:
  780. Socket - Supplies the socket to shut down.
  781. How - Supplies the type of shutdown. Valid values are SHUT_RD to disable
  782. further receive operations, SHUT_WR to disable further send operations,
  783. or SHUT_RDWR to disable further send and receive operations.
  784. Return Value:
  785. 0 on success.
  786. -1 on failure, and errno will be set to contain more information.
  787. --*/
  788. LIBC_API
  789. int
  790. setsockopt (
  791. int Socket,
  792. int Level,
  793. int OptionName,
  794. const void *OptionValue,
  795. socklen_t OptionLength
  796. );
  797. /*++
  798. Routine Description:
  799. This routine sets a socket option for the given socket.
  800. Arguments:
  801. Socket - Supplies the file descriptor of the socket to set options for.
  802. Level - Supplies the protocol level at which the option resides. To set
  803. options at the socket level, supply SOL_SOCKET. To set options at other
  804. levels, specify the identifier for the protocol controlling the option.
  805. For example, to indicate that an option is interpreted by the TCP
  806. protocol, set this parameter to IPPROTO_TCP.
  807. OptionName - Supplies the option name that is passed to the protocol module
  808. for interpretation. See SO_* definitions.
  809. OptionValue - Supplies a pointer to a buffer that is passed uninterpreted
  810. to the protocol module. The contents of the buffer are option-specific.
  811. OptionLength - Supplies the length of the option buffer in bytes.
  812. Return Value:
  813. 0 on success.
  814. -1 on failure, and errno will be set to contain more information.
  815. --*/
  816. LIBC_API
  817. int
  818. getsockopt (
  819. int Socket,
  820. int Level,
  821. int OptionName,
  822. void *OptionValue,
  823. socklen_t *OptionLength
  824. );
  825. /*++
  826. Routine Description:
  827. This routine retrieves the current value of a given socket option.
  828. Arguments:
  829. Socket - Supplies the file descriptor of the socket.
  830. Level - Supplies the protocol level at which the option resides. To get
  831. options at the socket level, supply SOL_SOCKET. To get options at other
  832. levels, specify the identifier for the protocol controlling the option.
  833. For example, to indicate that an option is interpreted by the TCP
  834. protocol, set this parameter to IPPROTO_TCP.
  835. OptionName - Supplies the option name that is passed to the protocol module
  836. for interpretation. See SO_* definitions.
  837. OptionValue - Supplies a pointer to a buffer where the option value is
  838. returned on success.
  839. OptionLength - Supplies a pointer that on input contains the size of the
  840. option value buffer in bytes. If the supplied length is less than the
  841. actual size of the option value, then the option value will be silently
  842. truncated. On output, if the supplied length is greater than the actual
  843. size of the value, this will contain the actual size of the value.
  844. Return Value:
  845. 0 on success.
  846. -1 on failure, and errno will be set to contain more information.
  847. --*/
  848. LIBC_API
  849. int
  850. getsockname (
  851. int Socket,
  852. struct sockaddr *SocketAddress,
  853. socklen_t *AddressLength
  854. );
  855. /*++
  856. Routine Description:
  857. This routine returns the current address to which the given socket is bound.
  858. Arguments:
  859. Socket - Supplies the file descriptor of the socket.
  860. SocketAddress - Supplies a pointer where the socket address will be
  861. returned.
  862. AddressLength - Supplies a pointer that on input supplies the size of the
  863. socket address buffer. On output, this will contain the actual size of
  864. the buffer. The buffer will have been truncated if the number returned
  865. here is greater than the number supplied.
  866. Return Value:
  867. 0 on success.
  868. -1 on failure, and errno will be set to contain more information.
  869. --*/
  870. LIBC_API
  871. int
  872. getpeername (
  873. int Socket,
  874. struct sockaddr *SocketAddress,
  875. socklen_t *AddressLength
  876. );
  877. /*++
  878. Routine Description:
  879. This routine returns the peer address of the specified socket.
  880. Arguments:
  881. Socket - Supplies the file descriptor of the socket.
  882. SocketAddress - Supplies a pointer where the socket's peer address will be
  883. returned.
  884. AddressLength - Supplies a pointer that on input supplies the size of the
  885. socket address buffer. On output, this will contain the actual size of
  886. the buffer. The buffer will have been truncated if the number returned
  887. here is greater than the number supplied.
  888. Return Value:
  889. 0 on success.
  890. -1 on failure, and errno will be set to contain more information.
  891. --*/
  892. LIBC_API
  893. struct cmsghdr *
  894. __cmsg_nxthdr (
  895. struct msghdr *Message,
  896. struct cmsghdr *ControlMessage
  897. );
  898. /*++
  899. Routine Description:
  900. This routine gets the next control message in the buffer of ancillary data.
  901. Arguments:
  902. Message - Supplies a pointer to the beginning of the ancillary data.
  903. ControlMessage - Supplies the previous control message. This routine
  904. returns the next control message after this one.
  905. Return Value:
  906. Returns a pointer to the control message after the given control message.
  907. NULL if there are no more messages or the buffer does not contain enough
  908. space.
  909. --*/
  910. #ifdef __cplusplus
  911. }
  912. #endif
  913. #endif