gnunet_network_lib.h 15 KB

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