gnunet_server_lib.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. This file is part of GNUnet.
  3. (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., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file include/gnunet_server_lib.h
  19. * @brief library for building GNUnet network servers
  20. * @author Christian Grothoff
  21. * @defgroup server functions for a server that communicates with clients
  22. * @{
  23. */
  24. #ifndef GNUNET_SERVER_LIB_H
  25. #define GNUNET_SERVER_LIB_H
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #if 0 /* keep Emacsens' auto-indent happy */
  30. }
  31. #endif
  32. #endif
  33. #include "gnunet_common.h"
  34. #include "gnunet_connection_lib.h"
  35. /**
  36. * Largest supported message (to be precise, one byte more
  37. * than the largest possible message, so tests involving
  38. * this value should check for messages being smaller than
  39. * this value).
  40. */
  41. #define GNUNET_SERVER_MAX_MESSAGE_SIZE 65536
  42. /**
  43. * Smallest supported message.
  44. */
  45. #define GNUNET_SERVER_MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
  46. /**
  47. * @brief handle for a server
  48. */
  49. struct GNUNET_SERVER_Handle;
  50. /**
  51. * @brief opaque handle for a client of the server
  52. */
  53. struct GNUNET_SERVER_Client;
  54. /**
  55. * @brief opaque handle server returns for aborting transmission to a client.
  56. */
  57. struct GNUNET_SERVER_TransmitHandle;
  58. /**
  59. * Functions with this signature are called whenever a message is
  60. * received.
  61. *
  62. * @param cls closure
  63. * @param client identification of the client
  64. * @param message the actual message
  65. */
  66. typedef void (*GNUNET_SERVER_MessageCallback) (void *cls,
  67. struct GNUNET_SERVER_Client *client,
  68. const struct GNUNET_MessageHeader
  69. *message);
  70. /**
  71. * Message handler. Each struct specifies how to handle on particular
  72. * type of message received.
  73. */
  74. struct GNUNET_SERVER_MessageHandler
  75. {
  76. /**
  77. * Function to call for messages of "type".
  78. */
  79. GNUNET_SERVER_MessageCallback callback;
  80. /**
  81. * Closure argument for @e callback.
  82. */
  83. void *callback_cls;
  84. /**
  85. * Type of the message this handler covers.
  86. */
  87. uint16_t type;
  88. /**
  89. * Expected size of messages of this type. Use 0 for
  90. * variable-size. If non-zero, messages of the given
  91. * type will be discarded (and the connection closed)
  92. * if they do not have the right size.
  93. */
  94. uint16_t expected_size;
  95. };
  96. /**
  97. * Create a new server.
  98. *
  99. * @param access function for access control
  100. * @param access_cls closure for @a access
  101. * @param lsocks NULL-terminated array of listen sockets
  102. * @param idle_timeout after how long should we timeout idle connections?
  103. * @param require_found if #GNUNET_YES, connections sending messages of unknown type
  104. * will be closed
  105. * @return handle for the new server, NULL on error
  106. * (typically, "port" already in use)
  107. */
  108. struct GNUNET_SERVER_Handle *
  109. GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
  110. void *access_cls,
  111. struct GNUNET_NETWORK_Handle **lsocks,
  112. struct GNUNET_TIME_Relative idle_timeout,
  113. int require_found);
  114. /**
  115. * Create a new server.
  116. *
  117. * @param access function for access control
  118. * @param access_cls closure for @a access
  119. * @param server_addr address toes listen on (including port), NULL terminated array
  120. * @param socklen lengths of respective @a server_addr
  121. * @param idle_timeout after how long should we timeout idle connections?
  122. * @param require_found if #GNUNET_YES, connections sending messages of unknown type
  123. * will be closed
  124. * @return handle for the new server, NULL on error
  125. * (typically, "port" already in use)
  126. */
  127. struct GNUNET_SERVER_Handle *
  128. GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
  129. struct sockaddr *const *server_addr,
  130. const socklen_t * socklen,
  131. struct GNUNET_TIME_Relative idle_timeout,
  132. int require_found);
  133. /**
  134. * Suspend accepting connections from the listen socket temporarily.
  135. * Resume activity using #GNUNET_SERVER_resume.
  136. *
  137. * @param server server to stop accepting connections.
  138. */
  139. void
  140. GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
  141. /**
  142. * Resume accepting connections from the listen socket.
  143. *
  144. * @param server server to resume accepting connections.
  145. */
  146. void
  147. GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
  148. /**
  149. * Stop the listen socket and get ready to shutdown the server once
  150. * only clients marked using #GNUNET_SERVER_client_mark_monitor are
  151. * left.
  152. *
  153. * @param server server to stop listening on
  154. */
  155. void
  156. GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server);
  157. /**
  158. * Free resources held by this server.
  159. *
  160. * @param server server to destroy
  161. */
  162. void
  163. GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
  164. /**
  165. * Add additional handlers to an existing server.
  166. *
  167. * @param server the server to add handlers to
  168. * @param handlers array of message handlers for
  169. * incoming messages; the last entry must
  170. * have "NULL" for the "callback"; multiple
  171. * entries for the same type are allowed,
  172. * they will be called in order of occurence.
  173. * These handlers can be removed later;
  174. * the handlers array must exist until removed
  175. * (or server is destroyed).
  176. */
  177. void
  178. GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
  179. const struct GNUNET_SERVER_MessageHandler *handlers);
  180. /**
  181. * Notify us when the server has enough space to transmit
  182. * a message of the given size to the given client.
  183. *
  184. * @param client client to transmit message to
  185. * @param size requested amount of buffer space
  186. * @param timeout after how long should we give up (and call
  187. * notify with buf NULL and size 0)?
  188. * @param callback function to call when space is available
  189. * @param callback_cls closure for @a callback
  190. * @return non-NULL if the notify callback was queued; can be used
  191. * to cancel the request using
  192. * #GNUNET_SERVER_notify_transmit_ready_cancel.
  193. * NULL if we are already going to notify someone else (busy)
  194. */
  195. struct GNUNET_SERVER_TransmitHandle *
  196. GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
  197. size_t size,
  198. struct GNUNET_TIME_Relative timeout,
  199. GNUNET_CONNECTION_TransmitReadyNotify callback,
  200. void *callback_cls);
  201. /**
  202. * Abort transmission request.
  203. *
  204. * @param th request to abort
  205. */
  206. void
  207. GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
  208. /**
  209. * Set the 'monitor' flag on this client. Clients which have been
  210. * marked as 'monitors' won't prevent the server from shutting down
  211. * once #GNUNET_SERVER_stop_listening has been invoked. The idea is
  212. * that for "normal" clients we likely want to allow them to process
  213. * their requests; however, monitor-clients are likely to 'never'
  214. * disconnect during shutdown and thus will not be considered when
  215. * determining if the server should continue to exist after
  216. * #GNUNET_SERVER_destroy has been called.
  217. *
  218. * @param client the client to set the 'monitor' flag on
  219. */
  220. void
  221. GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client);
  222. /**
  223. * Set the persistent flag on this client, used to setup client
  224. * connection to only be killed when the process of the service it's
  225. * connected to is actually dead. This API is used during shutdown
  226. * signalling within ARM, and it is not expected that typical users
  227. * of the API would need this function.
  228. *
  229. * @param client the client to set the persistent flag on
  230. */
  231. void
  232. GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client);
  233. /**
  234. * Resume receiving from this client, we are done processing the
  235. * current request. This function must be called from within each
  236. * #GNUNET_SERVER_MessageCallback (or its respective continuations).
  237. *
  238. * @param client client we were processing a message of
  239. * @param success #GNUNET_OK to keep the connection open and
  240. * continue to receive
  241. * #GNUNET_NO to close the connection (normal behavior)
  242. * #GNUNET_SYSERR to close the connection (signal
  243. * serious error)
  244. */
  245. void
  246. GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
  247. int success);
  248. /**
  249. * Change the timeout for a particular client. Decreasing the timeout
  250. * may not go into effect immediately (only after the previous timeout
  251. * times out or activity happens on the socket).
  252. *
  253. * @param client the client to update
  254. * @param timeout new timeout for activities on the socket
  255. */
  256. void
  257. GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
  258. struct GNUNET_TIME_Relative timeout);
  259. /**
  260. * Return user context associated with the given client.
  261. * Note: you should probably use the macro (call without the underscore).
  262. *
  263. * @param client client to query
  264. * @param size number of bytes in user context struct (for verification only)
  265. * @return pointer to user context
  266. */
  267. void *
  268. GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
  269. size_t size);
  270. /**
  271. * Set user context to be associated with the given client.
  272. * Note: you should probably use the macro (call without the underscore).
  273. *
  274. * @param client client to query
  275. * @param ptr pointer to user context
  276. * @param size number of bytes in user context struct (for verification only)
  277. */
  278. void
  279. GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
  280. void *ptr,
  281. size_t size);
  282. /**
  283. * Return user context associated with the given client.
  284. *
  285. * @param client client to query
  286. * @param type expected return type (i.e. 'struct Foo')
  287. * @return pointer to user context of type 'type *'.
  288. */
  289. #define GNUNET_SERVER_client_get_user_context(client,type) \
  290. (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type))
  291. /**
  292. * Set user context to be associated with the given client.
  293. *
  294. * @param client client to query
  295. * @param value pointer to user context
  296. */
  297. #define GNUNET_SERVER_client_set_user_context(client,value) \
  298. GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
  299. /**
  300. * Disable the warning the server issues if a message is not acknowledged
  301. * in a timely fashion. Use this call if a client is intentionally delayed
  302. * for a while. Only applies to the current message.
  303. *
  304. * @param client client for which to disable the warning
  305. */
  306. void
  307. GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client
  308. *client);
  309. /**
  310. * Inject a message into the server, pretend it came
  311. * from the specified client. Delivery of the message
  312. * will happen instantly (if a handler is installed;
  313. * otherwise the call does nothing).
  314. *
  315. * @param server the server receiving the message
  316. * @param sender the "pretended" sender of the message
  317. * can be NULL!
  318. * @param message message to transmit
  319. * @return #GNUNET_OK if the message was OK and the
  320. * connection can stay open
  321. * #GNUNET_SYSERR if the connection to the
  322. * client should be shut down
  323. */
  324. int
  325. GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
  326. struct GNUNET_SERVER_Client *sender,
  327. const struct GNUNET_MessageHeader *message);
  328. /**
  329. * Add a TCP socket-based connection to the set of handles managed by
  330. * this server. Use this function for outgoing (P2P) connections that
  331. * we initiated (and where this server should process incoming
  332. * messages).
  333. *
  334. * @param server the server to use
  335. * @param connection the connection to manage (client must
  336. * stop using this connection from now on)
  337. * @return the client handle (client should call
  338. * #GNUNET_SERVER_client_drop on the return value eventually)
  339. */
  340. struct GNUNET_SERVER_Client *
  341. GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
  342. struct GNUNET_CONNECTION_Handle *connection);
  343. /**
  344. * Notify the server that the given client handle should
  345. * be kept (keeps the connection up if possible, increments
  346. * the internal reference counter).
  347. *
  348. * @param client the client to keep
  349. */
  350. void
  351. GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
  352. /**
  353. * Notify the server that the given client handle is no
  354. * longer required. Decrements the reference counter. If
  355. * that counter reaches zero an inactive connection maybe
  356. * closed.
  357. *
  358. * @param client the client to drop
  359. */
  360. void
  361. GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
  362. /**
  363. * Obtain the network address of the other party.
  364. *
  365. * @param client the client to get the address for
  366. * @param addr where to store the address
  367. * @param addrlen where to store the length of @a addr
  368. * @return #GNUNET_OK on success
  369. */
  370. int
  371. GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
  372. void **addr, size_t *addrlen);
  373. /**
  374. * Functions with this signature are called whenever a client
  375. * is disconnected on the network level.
  376. *
  377. * @param cls closure
  378. * @param client identification of the client; NULL
  379. * for the last call when the server is destroyed
  380. */
  381. typedef void (*GNUNET_SERVER_DisconnectCallback) (void *cls,
  382. struct GNUNET_SERVER_Client *client);
  383. /**
  384. * Functions with this signature are called whenever a client
  385. * is connected on the network level.
  386. *
  387. * @param cls closure
  388. * @param client identification of the client
  389. */
  390. typedef void (*GNUNET_SERVER_ConnectCallback) (void *cls,
  391. struct GNUNET_SERVER_Client *client);
  392. /**
  393. * Ask the server to notify us whenever a client disconnects.
  394. * This function is called whenever the actual network connection
  395. * is closed; the reference count may be zero or larger than zero
  396. * at this point. If the server is destroyed before this
  397. * notification is explicitly cancelled, the 'callback' will
  398. * once be called with a 'client' argument of NULL to indicate
  399. * that the server itself is now gone (and that the callback
  400. * won't be called anymore and also can no longer be cancelled).
  401. *
  402. * @param server the server manageing the clients
  403. * @param callback function to call on disconnect
  404. * @param callback_cls closure for @a callback
  405. */
  406. void
  407. GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
  408. GNUNET_SERVER_DisconnectCallback callback,
  409. void *callback_cls);
  410. /**
  411. * Ask the server to notify us whenever a client connects.
  412. * This function is called whenever the actual network connection
  413. * is opened. If the server is destroyed before this
  414. * notification is explicitly cancelled, the @a callback will
  415. * once be called with a 'client' argument of NULL to indicate
  416. * that the server itself is now gone (and that the callback
  417. * won't be called anymore and also can no longer be cancelled).
  418. *
  419. * @param server the server manageing the clients
  420. * @param callback function to call on sconnect
  421. * @param callback_cls closure for @a callback
  422. */
  423. void
  424. GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
  425. GNUNET_SERVER_ConnectCallback callback,
  426. void *callback_cls);
  427. /**
  428. * Ask the server to stop notifying us whenever a client disconnects.
  429. * Arguments must match exactly those given to
  430. * #GNUNET_SERVER_disconnect_notify. It is not necessary to call this
  431. * function during shutdown of the server; in fact, most applications
  432. * will never use this function.
  433. *
  434. * @param server the server manageing the clients
  435. * @param callback function to call on disconnect
  436. * @param callback_cls closure for @a callback
  437. */
  438. void
  439. GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
  440. GNUNET_SERVER_DisconnectCallback callback,
  441. void *callback_cls);
  442. /**
  443. * Ask the server to stop notifying us whenever a client connects.
  444. * Arguments must match exactly those given to
  445. * #GNUNET_SERVER_connect_notify. It is not necessary to call this
  446. * function during shutdown of the server; in fact, most applications
  447. * will never use this function.
  448. *
  449. * @param server the server manageing the clients
  450. * @param callback function to call on connect
  451. * @param callback_cls closure for @a callback
  452. */
  453. void
  454. GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
  455. GNUNET_SERVER_ConnectCallback callback,
  456. void *callback_cls);
  457. /**
  458. * Ask the server to disconnect from the given client. This is the
  459. * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
  460. * except that it allows dropping of a client even when not handling a
  461. * message from that client.
  462. *
  463. * @param client the client to disconnect from
  464. */
  465. void
  466. GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
  467. /**
  468. * Disable the "CORK" feature for communication with the given client,
  469. * forcing the OS to immediately flush the buffer on transmission
  470. * instead of potentially buffering multiple messages.
  471. *
  472. * @param client handle to the client
  473. * @return #GNUNET_OK on success
  474. */
  475. int
  476. GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client);
  477. /**
  478. * The tansmit context is the key datastructure for a conveniance API
  479. * used for transmission of complex results to the client followed
  480. * ONLY by signaling receive_done with success or error
  481. */
  482. struct GNUNET_SERVER_TransmitContext;
  483. /**
  484. * Create a new transmission context for the given client.
  485. *
  486. * @param client client to create the context for.
  487. * @return NULL on error
  488. */
  489. struct GNUNET_SERVER_TransmitContext *
  490. GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client);
  491. /**
  492. * Append a message to the transmission context.
  493. * All messages in the context will be sent by
  494. * the #GNUNET_SERVER_transmit_context_run method.
  495. *
  496. * @param tc context to use
  497. * @param data what to append to the result message
  498. * @param length length of @a data
  499. * @param type type of the message
  500. */
  501. void
  502. GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext *tc,
  503. const void *data,
  504. size_t length, uint16_t type);
  505. /**
  506. * Append a message to the transmission context.
  507. * All messages in the context will be sent by
  508. * the transmit_context_run method.
  509. *
  510. * @param tc context to use
  511. * @param msg message to append
  512. */
  513. void
  514. GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext *tc,
  515. const struct GNUNET_MessageHeader *msg);
  516. /**
  517. * Execute a transmission context. If there is an error in the
  518. * transmission, the receive_done method will be called with an error
  519. * code (#GNUNET_SYSERR), otherwise with #GNUNET_OK.
  520. *
  521. * @param tc transmission context to use
  522. * @param timeout when to time out and abort the transmission
  523. */
  524. void
  525. GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
  526. struct GNUNET_TIME_Relative timeout);
  527. /**
  528. * Destroy a transmission context. This function must not be called
  529. * after #GNUNET_SERVER_transmit_context_run.
  530. *
  531. * @param tc transmission context to destroy
  532. * @param success code to give to #GNUNET_SERVER_receive_done for
  533. * the client: #GNUNET_OK to keep the connection open and
  534. * continue to receive
  535. * #GNUNET_NO to close the connection (normal behavior)
  536. * #GNUNET_SYSERR to close the connection (signal
  537. * serious error)
  538. */
  539. void
  540. GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext *tc,
  541. int success);
  542. /**
  543. * The notification context is the key datastructure for a conveniance
  544. * API used for transmission of notifications to the client until the
  545. * client disconnects or is disconnected (or the notification context
  546. * is destroyed, in which case we disconnect these clients).
  547. * Essentially, all (notification) messages are queued up until the
  548. * client is able to read them.
  549. */
  550. struct GNUNET_SERVER_NotificationContext;
  551. /**
  552. * Create a new notification context.
  553. *
  554. * @param server server for which this function creates the context
  555. * @param queue_length maximum number of messages to keep in
  556. * the notification queue; optional messages are dropped
  557. * if the queue gets longer than this number of messages
  558. * @return handle to the notification context
  559. */
  560. struct GNUNET_SERVER_NotificationContext *
  561. GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
  562. unsigned int queue_length);
  563. /**
  564. * Destroy the context, force disconnect for all clients.
  565. *
  566. * @param nc context to destroy.
  567. */
  568. void
  569. GNUNET_SERVER_notification_context_destroy (struct GNUNET_SERVER_NotificationContext *nc);
  570. /**
  571. * Add a client to the notification context.
  572. *
  573. * @param nc context to modify
  574. * @param client client to add
  575. */
  576. void
  577. GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext *nc,
  578. struct GNUNET_SERVER_Client *client);
  579. /**
  580. * Send a message to a particular client; must have
  581. * already been added to the notification context.
  582. *
  583. * @param nc context to modify
  584. * @param client client to transmit to
  585. * @param msg message to send
  586. * @param can_drop can this message be dropped due to queue length limitations
  587. */
  588. void
  589. GNUNET_SERVER_notification_context_unicast (struct GNUNET_SERVER_NotificationContext *nc,
  590. struct GNUNET_SERVER_Client *client,
  591. const struct GNUNET_MessageHeader *msg,
  592. int can_drop);
  593. /**
  594. * Send a message to all clients of this context.
  595. *
  596. * @param nc context to modify
  597. * @param msg message to send
  598. * @param can_drop can this message be dropped due to queue length limitations
  599. */
  600. void
  601. GNUNET_SERVER_notification_context_broadcast (struct GNUNET_SERVER_NotificationContext *nc,
  602. const struct GNUNET_MessageHeader *msg,
  603. int can_drop);
  604. /**
  605. * Return active number of subscribers in this context.
  606. *
  607. * @param nc context to query
  608. * @return number of current subscribers
  609. */
  610. unsigned int
  611. GNUNET_SERVER_notification_context_get_size (struct GNUNET_SERVER_NotificationContext *nc);
  612. /**
  613. * Handle to a message stream tokenizer.
  614. */
  615. struct GNUNET_SERVER_MessageStreamTokenizer;
  616. /**
  617. * Functions with this signature are called whenever a
  618. * complete message is received by the tokenizer.
  619. *
  620. * Do not call #GNUNET_SERVER_mst_destroy from within
  621. * the scope of this callback.
  622. *
  623. * @param cls closure
  624. * @param client identification of the client
  625. * @param message the actual message
  626. * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
  627. */
  628. typedef int (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client,
  629. const struct GNUNET_MessageHeader *message);
  630. /**
  631. * Create a message stream tokenizer.
  632. *
  633. * @param cb function to call on completed messages
  634. * @param cb_cls closure for @a cb
  635. * @return handle to tokenizer
  636. */
  637. struct GNUNET_SERVER_MessageStreamTokenizer *
  638. GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
  639. void *cb_cls);
  640. /**
  641. * Add incoming data to the receive buffer and call the
  642. * callback for all complete messages.
  643. *
  644. * @param mst tokenizer to use
  645. * @param client_identity ID of client for which this is a buffer,
  646. * can be NULL (will be passed back to 'cb')
  647. * @param buf input data to add
  648. * @param size number of bytes in @a buf
  649. * @param purge should any excess bytes in the buffer be discarded
  650. * (i.e. for packet-based services like UDP)
  651. * @param one_shot only call callback once, keep rest of message in buffer
  652. * @return #GNUNET_OK if we are done processing (need more data)
  653. * #GNUNET_NO if one_shot was set and we have another message ready
  654. * #GNUNET_SYSERR if the data stream is corrupt
  655. */
  656. int
  657. GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
  658. void *client_identity,
  659. const char *buf, size_t size,
  660. int purge, int one_shot);
  661. /**
  662. * Destroys a tokenizer.
  663. *
  664. * @param mst tokenizer to destroy
  665. */
  666. void
  667. GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
  668. /**
  669. * Signature of a function to create a custom tokenizer.
  670. *
  671. * @param cls closure from #GNUNET_SERVER_set_callbacks
  672. * @param client handle to client the tokenzier will be used for
  673. * @return handle to custom tokenizer ('mst')
  674. */
  675. typedef void* (*GNUNET_SERVER_MstCreateCallback) (void *cls,
  676. struct GNUNET_SERVER_Client *client);
  677. /**
  678. * Signature of a function to destroy a custom tokenizer.
  679. *
  680. * @param cls closure from #GNUNET_SERVER_set_callbacks
  681. * @param mst custom tokenizer handle
  682. */
  683. typedef void (*GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst);
  684. /**
  685. * Signature of a function to receive data for a custom tokenizer.
  686. *
  687. * @param cls closure from #GNUNET_SERVER_set_callbacks
  688. * @param mst custom tokenizer handle
  689. * @param client_identity ID of client for which this is a buffer,
  690. * can be NULL (will be passed back to 'cb')
  691. * @param buf input data to add
  692. * @param size number of bytes in @a buf
  693. * @param purge should any excess bytes in the buffer be discarded
  694. * (i.e. for packet-based services like UDP)
  695. * @param one_shot only call callback once, keep rest of message in buffer
  696. * @return #GNUNET_OK if we are done processing (need more data)
  697. * #GNUNET_NO if one_shot was set and we have another message ready
  698. * #GNUNET_SYSERR if the data stream is corrupt
  699. */
  700. typedef int (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
  701. struct GNUNET_SERVER_Client *client,
  702. const char *buf, size_t size,
  703. int purge, int one_shot);
  704. /**
  705. * Change functions used by the server to tokenize the message stream.
  706. * (very rarely used).
  707. *
  708. * @param server server to modify
  709. * @param create new tokenizer initialization function
  710. * @param destroy new tokenizer destruction function
  711. * @param receive new tokenizer receive function
  712. * @param cls closure for @a create, @a receive and @a destroy
  713. */
  714. void
  715. GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
  716. GNUNET_SERVER_MstCreateCallback create,
  717. GNUNET_SERVER_MstDestroyCallback destroy,
  718. GNUNET_SERVER_MstReceiveCallback receive,
  719. void *cls);
  720. #if 0 /* keep Emacsens' auto-indent happy */
  721. {
  722. #endif
  723. #ifdef __cplusplus
  724. }
  725. #endif
  726. /** @} */ /* end of group server */
  727. /* ifndef GNUNET_SERVER_LIB_H */
  728. #endif
  729. /* end of gnunet_server_lib.h */