gnunet_network_lib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Nils Durner
  19. *
  20. * @file
  21. * Basic low-level networking interface
  22. *
  23. * @defgroup network Network library
  24. * Basic low-level networking interface
  25. * @{
  26. */
  27. #ifndef GNUNET_NETWORK_LIB_H
  28. #define GNUNET_NETWORK_LIB_H
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #if 0 /* keep Emacsens' auto-indent happy */
  33. }
  34. #endif
  35. #endif
  36. /**
  37. * @brief handle to a socket
  38. */
  39. struct GNUNET_NETWORK_Handle;
  40. /**
  41. * @brief collection of IO descriptors
  42. */
  43. struct GNUNET_NETWORK_FDSet
  44. {
  45. /**
  46. * Maximum number of any socket socket descriptor in the set (plus one)
  47. */
  48. int nsds;
  49. /**
  50. * Bitset with the descriptors.
  51. */
  52. fd_set sds;
  53. #ifdef WINDOWS
  54. /**
  55. * Array of file handles (from pipes) that are also in
  56. * the FDSet. Needed as those cannot go into @e sds
  57. * on W32.
  58. */
  59. const struct GNUNET_DISK_FileHandle **handles;
  60. /**
  61. * Size of the @e handles array
  62. */
  63. unsigned int handles_size;
  64. /**
  65. * Number of @e handles slots in use. Always
  66. * smaller than @e handles_size.
  67. */
  68. unsigned int handles_pos;
  69. #endif
  70. };
  71. #include "gnunet_disk_lib.h"
  72. #include "gnunet_time_lib.h"
  73. /**
  74. * Test if the given protocol family is supported by this system.
  75. *
  76. * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
  77. * @return #GNUNET_OK if the PF is supported
  78. */
  79. int
  80. GNUNET_NETWORK_test_pf (int pf);
  81. /**
  82. * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
  83. * shorten it to an acceptable length while keeping it unique
  84. * and making sure it remains a valid filename (if possible).
  85. *
  86. * @param unixpath long path, will be freed (or same pointer returned
  87. * with moved 0-termination).
  88. * @return shortened unixpath, NULL on error
  89. */
  90. char *
  91. GNUNET_NETWORK_shorten_unixpath (char *unixpath);
  92. #ifndef WINDOWS
  93. /**
  94. * If services crash, they can leave a unix domain socket file on the
  95. * disk. This needs to be manually removed, because otherwise both
  96. * bind() and connect() for the respective address will fail. In this
  97. * function, we test if such a left-over file exists, and if so,
  98. * remove it (unless there is a listening service at the address).
  99. *
  100. * @param un unix domain socket address to check
  101. */
  102. void
  103. GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un);
  104. #endif
  105. /**
  106. * Accept a new connection on a socket. Configure it for non-blocking
  107. * IO and mark it as non-inheritable to child processes (set the
  108. * close-on-exec flag).
  109. *
  110. * @param desc bound socket
  111. * @param address address of the connecting peer, may be NULL
  112. * @param address_len length of address
  113. * @return client socket
  114. */
  115. struct GNUNET_NETWORK_Handle *
  116. GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
  117. struct sockaddr *address,
  118. socklen_t *address_len);
  119. /**
  120. * Box a native socket (and check that it is a socket).
  121. *
  122. * @param fd socket to box
  123. * @return NULL on error (including not supported on target platform)
  124. */
  125. struct GNUNET_NETWORK_Handle *
  126. GNUNET_NETWORK_socket_box_native (SOCKTYPE fd);
  127. /**
  128. * Set if a socket should use blocking or non-blocking IO.
  129. *
  130. * @param fd socket
  131. * @param doBlock blocking mode
  132. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  133. */
  134. int
  135. GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
  136. int doBlock);
  137. /**
  138. * Bind a socket to a particular address.
  139. *
  140. * @param desc socket to bind
  141. * @param address address to be bound
  142. * @param address_len length of @a address
  143. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  144. */
  145. int
  146. GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
  147. const struct sockaddr *address,
  148. socklen_t address_len);
  149. /**
  150. * Close a socket.
  151. *
  152. * @param desc socket to close
  153. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  154. */
  155. int
  156. GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
  157. /**
  158. * Only free memory of a socket, keep the file descriptor untouched.
  159. *
  160. * @param desc socket
  161. */
  162. void
  163. GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc);
  164. /**
  165. * Connect a socket to some remote address.
  166. *
  167. * @param desc socket to connect
  168. * @param address peer address
  169. * @param address_len of @a address
  170. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  171. */
  172. int
  173. GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
  174. const struct sockaddr *address,
  175. socklen_t address_len);
  176. /**
  177. * Get socket options
  178. *
  179. * @param desc socket to inspect
  180. * @param level protocol level of the option
  181. * @param optname identifier of the option
  182. * @param optval options
  183. * @param optlen length of optval
  184. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  185. */
  186. int
  187. GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
  188. int level,
  189. int optname,
  190. void *optval,
  191. socklen_t *optlen);
  192. /**
  193. * Listen on a socket
  194. *
  195. * @param desc socket to start listening on
  196. * @param backlog length of the listen queue
  197. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  198. */
  199. int
  200. GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
  201. int backlog);
  202. /**
  203. * How much data is available to be read on this descriptor?
  204. *
  205. * @param desc socket
  206. * @returns #GNUNET_NO if no data is available, or on error!
  207. */
  208. ssize_t
  209. GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc);
  210. /**
  211. * Read data from a socket (always non-blocking).
  212. *
  213. * @param desc socket
  214. * @param buffer buffer
  215. * @param length length of buffer
  216. * @param src_addr either the source to recv from, or all zeroes
  217. * to be filled in by recvfrom
  218. * @param addrlen length of the addr
  219. */
  220. ssize_t
  221. GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
  222. void *buffer,
  223. size_t length,
  224. struct sockaddr *src_addr,
  225. socklen_t *addrlen);
  226. /**
  227. * Read data from a connected socket (always non-blocking).
  228. *
  229. * @param desc socket
  230. * @param buffer buffer
  231. * @param length length of buffer
  232. * @return number of bytes read
  233. */
  234. ssize_t
  235. GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
  236. void *buffer,
  237. size_t length);
  238. /**
  239. * Check if sockets meet certain conditions.
  240. *
  241. * @param rfds set of sockets to be checked for readability
  242. * @param wfds set of sockets to be checked for writability
  243. * @param efds set of sockets to be checked for exceptions
  244. * @param timeout relative value when to return
  245. * @return number of selected sockets, #GNUNET_SYSERR on error
  246. */
  247. int
  248. GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
  249. struct GNUNET_NETWORK_FDSet *wfds,
  250. struct GNUNET_NETWORK_FDSet *efds,
  251. struct GNUNET_TIME_Relative timeout);
  252. /**
  253. * Send data (always non-blocking).
  254. *
  255. * @param desc socket
  256. * @param buffer data to send
  257. * @param length size of the buffer
  258. * @return number of bytes sent, #GNUNET_SYSERR on error
  259. */
  260. ssize_t
  261. GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
  262. const void *buffer,
  263. size_t length);
  264. /**
  265. * Send data to a particular destination (always non-blocking).
  266. * This function only works for UDP sockets.
  267. *
  268. * @param desc socket
  269. * @param message data to send
  270. * @param length size of the data in @a message
  271. * @param dest_addr destination address
  272. * @param dest_len length of @a dest_addr
  273. * @return number of bytes sent, #GNUNET_SYSERR on error
  274. */
  275. ssize_t
  276. GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
  277. const void *message,
  278. size_t length,
  279. const struct sockaddr *dest_addr,
  280. socklen_t dest_len);
  281. /**
  282. * Set socket option
  283. *
  284. * @param fd socket
  285. * @param level protocol level of the option
  286. * @param option_name option identifier
  287. * @param option_value value to set
  288. * @param option_len size of @a option_value
  289. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  290. */
  291. int
  292. GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
  293. int level,
  294. int option_name,
  295. const void *option_value,
  296. socklen_t option_len);
  297. /**
  298. * Shut down socket operations
  299. *
  300. * @param desc socket
  301. * @param how type of shutdown
  302. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  303. */
  304. int
  305. GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
  306. int how);
  307. /**
  308. * Disable the "CORK" feature for communication with the given socket,
  309. * forcing the OS to immediately flush the buffer on transmission
  310. * instead of potentially buffering multiple messages. Essentially
  311. * reduces the OS send buffers to zero.
  312. *
  313. * @param desc socket
  314. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  315. */
  316. int
  317. GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
  318. /**
  319. * Create a new socket. Configure it for non-blocking IO and
  320. * mark it as non-inheritable to child processes (set the
  321. * close-on-exec flag).
  322. *
  323. * @param domain domain of the socket
  324. * @param type socket type
  325. * @param protocol network protocol
  326. * @return new socket, NULL on error
  327. */
  328. struct GNUNET_NETWORK_Handle *
  329. GNUNET_NETWORK_socket_create (int domain,
  330. int type,
  331. int protocol);
  332. /**
  333. * Reset FD set (clears all file descriptors).
  334. *
  335. * @param fds fd set to clear
  336. */
  337. void
  338. GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
  339. /**
  340. * Add a socket to the FD set
  341. *
  342. * @param fds fd set
  343. * @param desc socket to add
  344. */
  345. void
  346. GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
  347. const struct GNUNET_NETWORK_Handle *desc);
  348. #if WINDOWS
  349. /**
  350. * Add a W32 file handle to the fd set
  351. *
  352. * @param fds fd set
  353. * @param h the file handle to add
  354. */
  355. void
  356. GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet *fds,
  357. HANDLE h);
  358. #endif
  359. /**
  360. * Check whether a socket is part of the fd set
  361. *
  362. * @param fds fd set
  363. * @param desc socket
  364. * @return #GNUNET_YES if the socket is in the set
  365. */
  366. int
  367. GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
  368. const struct GNUNET_NETWORK_Handle *desc);
  369. /**
  370. * Add one fd set to another (computes the union).
  371. *
  372. * @param dst the fd set to add to
  373. * @param src the fd set to add from
  374. */
  375. void
  376. GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
  377. const struct GNUNET_NETWORK_FDSet *src);
  378. /**
  379. * Copy one fd set to another
  380. *
  381. * @param to destination
  382. * @param from source
  383. */
  384. void
  385. GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
  386. const struct GNUNET_NETWORK_FDSet *from);
  387. /**
  388. * Return file descriptor for this network handle
  389. *
  390. * @param desc wrapper to process
  391. * @return POSIX file descriptor
  392. */
  393. int
  394. GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
  395. /**
  396. * Return the sockaddr for this network handle
  397. *
  398. * @param desc wrapper to process
  399. * @return POSIX file descriptor
  400. */
  401. struct sockaddr*
  402. GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc);
  403. /**
  404. * Return sockaddr length for this network handle
  405. *
  406. * @param desc wrapper to process
  407. * @return socklen_t for sockaddr
  408. */
  409. socklen_t
  410. GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc);
  411. /**
  412. * Copy a native fd set into the GNUnet representation.
  413. *
  414. * @param to destination
  415. * @param from native source set
  416. * @param nfds the biggest socket number in from + 1
  417. */
  418. void
  419. GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
  420. const fd_set *from,
  421. int nfds);
  422. /**
  423. * Set a native fd in a set
  424. *
  425. * @param to destination
  426. * @param nfd native FD to set
  427. */
  428. void
  429. GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
  430. int nfd);
  431. /**
  432. * Test native fd in a set
  433. *
  434. * @param to set to test, NULL for empty set
  435. * @param nfd native FD to test, -1 for none
  436. * @return #GNUNET_YES if to contains nfd
  437. */
  438. int
  439. GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
  440. int nfd);
  441. /**
  442. * Add a file handle to the fd set
  443. *
  444. * @param fds fd set
  445. * @param h the file handle to add
  446. */
  447. void
  448. GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
  449. const struct GNUNET_DISK_FileHandle *h);
  450. /**
  451. * Add a file handle to the fd set
  452. * On W32: ensure that the handle is first in the array.
  453. *
  454. * @param fds fd set
  455. * @param h the file handle to add
  456. */
  457. void
  458. GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
  459. const struct GNUNET_DISK_FileHandle *h);
  460. /**
  461. * Check if a file handle is part of an fd set
  462. *
  463. * @param fds fd set
  464. * @param h file handle
  465. * @return #GNUNET_YES if the file handle is part of the set
  466. */
  467. int
  468. GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
  469. const struct GNUNET_DISK_FileHandle *h);
  470. /**
  471. * Checks if two fd sets overlap
  472. *
  473. * @param fds1 first fd set
  474. * @param fds2 second fd set
  475. * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
  476. */
  477. int
  478. GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
  479. const struct GNUNET_NETWORK_FDSet *fds2);
  480. /**
  481. * Creates an fd set
  482. *
  483. * @return a new fd set
  484. */
  485. struct GNUNET_NETWORK_FDSet *
  486. GNUNET_NETWORK_fdset_create (void);
  487. /**
  488. * Releases the associated memory of an fd set
  489. *
  490. * @param fds fd set
  491. */
  492. void
  493. GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
  494. #if 0 /* keep Emacsens' auto-indent happy */
  495. {
  496. #endif
  497. #ifdef __cplusplus
  498. }
  499. #endif
  500. #endif /* GNUNET_NETWORK_LIB_H */
  501. /** @} */ /* end of group */