gnunet_server_lib.h 28 KB

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