gnunet_connection_lib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009 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., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file include/gnunet_connection_lib.h
  19. * @brief basic, low-level TCP networking interface
  20. * @author Christian Grothoff
  21. */
  22. #ifndef GNUNET_CONNECTION_LIB_H
  23. #define GNUNET_CONNECTION_LIB_H
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #if 0 /* keep Emacsens' auto-indent happy */
  28. }
  29. #endif
  30. #endif
  31. #include "gnunet_network_lib.h"
  32. #include "gnunet_scheduler_lib.h"
  33. #include "gnunet_time_lib.h"
  34. /**
  35. * Timeout we use on TCP connect before trying another
  36. * result from the DNS resolver. Actual value used
  37. * is this value divided by the number of address families.
  38. * Default is 5s.
  39. */
  40. #define GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  41. /**
  42. * @brief handle for a network connection
  43. */
  44. struct GNUNET_CONNECTION_Handle;
  45. /**
  46. * Credentials for UNIX domain sockets.
  47. */
  48. struct GNUNET_CONNECTION_Credentials
  49. {
  50. /**
  51. * UID of the other end of the connection.
  52. */
  53. uid_t uid;
  54. /**
  55. * GID of the other end of the connection.
  56. */
  57. gid_t gid;
  58. };
  59. /**
  60. * Function to call for access control checks.
  61. *
  62. * @param cls closure
  63. * @param ucred credentials, if available, otherwise NULL
  64. * @param addr address
  65. * @param addrlen length of address
  66. * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
  67. * for unknown address family (will be denied).
  68. */
  69. typedef int (*GNUNET_CONNECTION_AccessCheck) (void *cls,
  70. const struct
  71. GNUNET_CONNECTION_Credentials *
  72. ucred,
  73. const struct sockaddr * addr,
  74. socklen_t addrlen);
  75. /**
  76. * Callback function for data received from the network. Note that
  77. * both "available" and "err" would be 0 if the read simply timed out.
  78. *
  79. * @param cls closure
  80. * @param buf pointer to received data
  81. * @param available number of bytes availabe in "buf",
  82. * possibly 0 (on errors)
  83. * @param addr address of the sender
  84. * @param addrlen size of addr
  85. * @param errCode value of errno (on errors receiving)
  86. */
  87. typedef void (*GNUNET_CONNECTION_Receiver) (void *cls, const void *buf,
  88. size_t available,
  89. const struct sockaddr * addr,
  90. socklen_t addrlen, int errCode);
  91. /**
  92. * Set the persist option on this connection handle. Indicates
  93. * that the underlying socket or fd should never really be closed.
  94. * Used for indicating process death.
  95. *
  96. * @param connection the connection to set persistent
  97. */
  98. void
  99. GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *connection);
  100. /**
  101. * Disable the "CORK" feature for communication with the given socket,
  102. * forcing the OS to immediately flush the buffer on transmission
  103. * instead of potentially buffering multiple messages. Essentially
  104. * reduces the OS send buffers to zero.
  105. * Used to make sure that the last messages sent through the connection
  106. * reach the other side before the process is terminated.
  107. *
  108. * @param connection the connection to make flushing and blocking
  109. * @return #GNUNET_OK on success
  110. */
  111. int
  112. GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *connection);
  113. /**
  114. * Create a connection handle by (asynchronously) connecting to a host.
  115. * This function returns immediately, even if the connection has not
  116. * yet been established. This function only creates TCP connections.
  117. *
  118. * @param s socket to connect
  119. * @param serv_addr server address
  120. * @param addrlen length of server address
  121. * @return the connection handle
  122. */
  123. struct GNUNET_CONNECTION_Handle *
  124. GNUNET_CONNECTION_connect_socket (struct GNUNET_NETWORK_Handle *s,
  125. const struct sockaddr *serv_addr,
  126. socklen_t addrlen);
  127. /**
  128. * Create a connection handle by boxing an existing OS socket. The OS
  129. * socket should henceforth be no longer used directly.
  130. * #GNUNET_CONNECTION_destroy() will close it.
  131. *
  132. * @param osSocket existing socket to box
  133. * @return the boxed socket handle
  134. */
  135. struct GNUNET_CONNECTION_Handle *
  136. GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket);
  137. /**
  138. * Create a connection handle by accepting on a listen socket. This
  139. * function may block if the listen socket has no connection ready.
  140. *
  141. * @param access function to use to check if access is allowed
  142. * @param access_cls closure for access
  143. * @param lsock listen socket
  144. * @return the connection handle, NULL on error (for example, access refused)
  145. */
  146. struct GNUNET_CONNECTION_Handle *
  147. GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
  148. void *access_cls,
  149. struct GNUNET_NETWORK_Handle *lsock);
  150. /**
  151. * Create a connection handle by (asynchronously) connecting to a host.
  152. * This function returns immediately, even if the connection has not
  153. * yet been established. This function only creates TCP connections.
  154. *
  155. * @param cfg configuration to use
  156. * @param hostname name of the host to connect to
  157. * @param port port to connect to
  158. * @return the connection handle
  159. */
  160. struct GNUNET_CONNECTION_Handle *
  161. GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
  162. *cfg, const char *hostname,
  163. uint16_t port);
  164. /**
  165. * Create a connection handle by connecting to a UNIX domain service.
  166. * This function returns immediately, even if the connection has not
  167. * yet been established. This function only creates UNIX connections.
  168. *
  169. * @param cfg configuration to use
  170. * @param unixpath path to connect to)
  171. * @return the connection handle, NULL on systems without UNIX support
  172. */
  173. struct GNUNET_CONNECTION_Handle *
  174. GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
  175. GNUNET_CONFIGURATION_Handle
  176. *cfg, const char *unixpath);
  177. /**
  178. * Create a connection handle by (asynchronously) connecting to a host.
  179. * This function returns immediately, even if the connection has not
  180. * yet been established. This function only creates TCP connections.
  181. *
  182. * @param af_family address family to use
  183. * @param serv_addr server address
  184. * @param addrlen length of server address
  185. * @return the connection handle
  186. */
  187. struct GNUNET_CONNECTION_Handle *
  188. GNUNET_CONNECTION_create_from_sockaddr (int af_family,
  189. const struct sockaddr *serv_addr,
  190. socklen_t addrlen);
  191. /**
  192. * Check if connection is valid (no fatal errors have happened so far).
  193. * Note that a connection that is still trying to connect is considered
  194. * valid.
  195. *
  196. * @param connection handle to check
  197. * @return GNUNET_YES if valid, GNUNET_NO otherwise
  198. */
  199. int
  200. GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *connection);
  201. /**
  202. * Obtain the network address of the other party.
  203. *
  204. * @param connection the client to get the address for
  205. * @param addr where to store the address
  206. * @param addrlen where to store the length of the address
  207. * @return GNUNET_OK on success
  208. */
  209. int
  210. GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
  211. void **addr, size_t * addrlen);
  212. /**
  213. * Close the connection and free associated resources. There must
  214. * not be any pending requests for reading or writing to the
  215. * connection at this time.
  216. *
  217. * @param connection connection to destroy
  218. */
  219. void
  220. GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection);
  221. /**
  222. * Receive data from the given connection. Note that this function will
  223. * call "receiver" asynchronously using the scheduler. It will
  224. * "immediately" return. Note that there MUST only be one active
  225. * receive call per connection at any given point in time (so do not
  226. * call receive again until the receiver callback has been invoked).
  227. *
  228. * @param connection connection handle
  229. * @param max maximum number of bytes to read
  230. * @param timeout maximum amount of time to wait
  231. * @param receiver function to call with received data
  232. * @param receiver_cls closure for receiver
  233. */
  234. void
  235. GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *connection, size_t max,
  236. struct GNUNET_TIME_Relative timeout,
  237. GNUNET_CONNECTION_Receiver receiver,
  238. void *receiver_cls);
  239. /**
  240. * Cancel receive job on the given connection. Note that the
  241. * receiver callback must not have been called yet in order
  242. * for the cancellation to be valid.
  243. *
  244. * @param connection connection handle
  245. * @return closure of the original receiver callback closure
  246. */
  247. void *
  248. GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection);
  249. /**
  250. * Function called to notify a client about the connection begin ready
  251. * to queue more data. @a buf will be NULL and @a size zero if the
  252. * connection was closed for writing in the meantime.
  253. *
  254. * @param cls closure
  255. * @param size number of bytes available in @a buf
  256. * @param buf where the callee should write the message
  257. * @return number of bytes written to @a buf
  258. */
  259. typedef size_t
  260. (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
  261. size_t size,
  262. void *buf);
  263. /**
  264. * Opaque handle that can be used to cancel
  265. * a transmit-ready notification.
  266. */
  267. struct GNUNET_CONNECTION_TransmitHandle;
  268. /**
  269. * Ask the connection to call us once the specified number of bytes
  270. * are free in the transmission buffer. Will never call the @a notify
  271. * callback in this task, but always first go into the scheduler. Note that
  272. * this function will abort if "size" is greater than
  273. * #GNUNET_SERVER_MAX_MESSAGE_SIZE.
  274. *
  275. * Note that "notify" will be called either when enough
  276. * buffer space is available OR when the connection is destroyed.
  277. * The size parameter given to notify is guaranteed to be
  278. * larger or equal to size if the buffer is ready, or zero
  279. * if the connection was destroyed (or at least closed for
  280. * writing). Finally, any time before 'notify' is called, a
  281. * client may call "notify_transmit_ready_cancel" to cancel
  282. * the transmission request.
  283. *
  284. * Only one transmission request can be scheduled at the same
  285. * time. Notify will be run with the same scheduler priority
  286. * as that of the caller.
  287. *
  288. * @param connection connection
  289. * @param size number of bytes to send
  290. * @param timeout after how long should we give up (and call
  291. * notify with buf NULL and size 0)?
  292. * @param notify function to call when buffer space is available
  293. * @param notify_cls closure for notify
  294. * @return non-NULL if the notify callback was queued,
  295. * NULL if we are already going to notify someone else (busy)
  296. */
  297. struct GNUNET_CONNECTION_TransmitHandle *
  298. GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connection,
  299. size_t size,
  300. struct GNUNET_TIME_Relative timeout,
  301. GNUNET_CONNECTION_TransmitReadyNotify
  302. notify, void *notify_cls);
  303. /**
  304. * Cancel the specified transmission-ready
  305. * notification.
  306. *
  307. * @param th handle for notification to cancel
  308. */
  309. void
  310. GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
  311. GNUNET_CONNECTION_TransmitHandle
  312. *th);
  313. #if 0 /* keep Emacsens' auto-indent happy */
  314. {
  315. #endif
  316. #ifdef __cplusplus
  317. }
  318. #endif
  319. /* ifndef GNUNET_CONNECTION_LIB_H */
  320. #endif
  321. /* end of gnunet_connection_lib.h */