tcp_server_legacy.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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. * @file util/server.c
  18. * @brief library for building GNUnet network servers
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_protocols.h"
  24. #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-server", syscall, filename)
  25. /**
  26. * List of arrays of message handlers.
  27. */
  28. struct HandlerList
  29. {
  30. /**
  31. * This is a linked list.
  32. */
  33. struct HandlerList *next;
  34. /**
  35. * NULL-terminated array of handlers.
  36. */
  37. const struct GNUNET_SERVER_MessageHandler *handlers;
  38. };
  39. /**
  40. * List of arrays of message handlers.
  41. */
  42. struct NotifyList
  43. {
  44. /**
  45. * This is a doubly linked list.
  46. */
  47. struct NotifyList *next;
  48. /**
  49. * This is a doubly linked list.
  50. */
  51. struct NotifyList *prev;
  52. /**
  53. * Function to call.
  54. */
  55. GNUNET_SERVER_DisconnectCallback callback;
  56. /**
  57. * Closure for callback.
  58. */
  59. void *callback_cls;
  60. };
  61. /**
  62. * @brief handle for a server
  63. */
  64. struct GNUNET_SERVER_Handle
  65. {
  66. /**
  67. * List of handlers for incoming messages.
  68. */
  69. struct HandlerList *handlers;
  70. /**
  71. * Head of list of our current clients.
  72. */
  73. struct GNUNET_SERVER_Client *clients_head;
  74. /**
  75. * Head of list of our current clients.
  76. */
  77. struct GNUNET_SERVER_Client *clients_tail;
  78. /**
  79. * Head of linked list of functions to call on disconnects by clients.
  80. */
  81. struct NotifyList *disconnect_notify_list_head;
  82. /**
  83. * Tail of linked list of functions to call on disconnects by clients.
  84. */
  85. struct NotifyList *disconnect_notify_list_tail;
  86. /**
  87. * Head of linked list of functions to call on connects by clients.
  88. */
  89. struct NotifyList *connect_notify_list_head;
  90. /**
  91. * Tail of linked list of functions to call on connects by clients.
  92. */
  93. struct NotifyList *connect_notify_list_tail;
  94. /**
  95. * Function to call for access control.
  96. */
  97. GNUNET_CONNECTION_AccessCheck access_cb;
  98. /**
  99. * Closure for @e access_cb.
  100. */
  101. void *access_cb_cls;
  102. /**
  103. * NULL-terminated array of sockets used to listen for new
  104. * connections.
  105. */
  106. struct GNUNET_NETWORK_Handle **listen_sockets;
  107. /**
  108. * After how long should an idle connection time
  109. * out (on write).
  110. */
  111. struct GNUNET_TIME_Relative idle_timeout;
  112. /**
  113. * Task scheduled to do the listening.
  114. */
  115. struct GNUNET_SCHEDULER_Task * listen_task;
  116. /**
  117. * Alternative function to create a MST instance.
  118. */
  119. GNUNET_SERVER_MstCreateCallback mst_create;
  120. /**
  121. * Alternative function to destroy a MST instance.
  122. */
  123. GNUNET_SERVER_MstDestroyCallback mst_destroy;
  124. /**
  125. * Alternative function to give data to a MST instance.
  126. */
  127. GNUNET_SERVER_MstReceiveCallback mst_receive;
  128. /**
  129. * Closure for 'mst_'-callbacks.
  130. */
  131. void *mst_cls;
  132. /**
  133. * Do we ignore messages of types that we do not understand or do we
  134. * require that a handler is found (and if not kill the connection)?
  135. */
  136. int require_found;
  137. /**
  138. * Set to #GNUNET_YES once we are in 'soft' shutdown where we wait for
  139. * all non-monitor clients to disconnect before we call
  140. * #GNUNET_SERVER_destroy. See test_monitor_clients(). Set to
  141. * #GNUNET_SYSERR once the final destroy task has been scheduled
  142. * (we cannot run it in the same task).
  143. */
  144. int in_soft_shutdown;
  145. };
  146. /**
  147. * Handle server returns for aborting transmission to a client.
  148. */
  149. struct GNUNET_SERVER_TransmitHandle
  150. {
  151. /**
  152. * Function to call to get the message.
  153. */
  154. GNUNET_CONNECTION_TransmitReadyNotify callback;
  155. /**
  156. * Closure for @e callback
  157. */
  158. void *callback_cls;
  159. /**
  160. * Active connection transmission handle.
  161. */
  162. struct GNUNET_CONNECTION_TransmitHandle *cth;
  163. };
  164. /**
  165. * @brief handle for a client of the server
  166. */
  167. struct GNUNET_SERVER_Client
  168. {
  169. /**
  170. * This is a doubly linked list.
  171. */
  172. struct GNUNET_SERVER_Client *next;
  173. /**
  174. * This is a doubly linked list.
  175. */
  176. struct GNUNET_SERVER_Client *prev;
  177. /**
  178. * Processing of incoming data.
  179. */
  180. void *mst;
  181. /**
  182. * Server that this client belongs to.
  183. */
  184. struct GNUNET_SERVER_Handle *server;
  185. /**
  186. * Client closure for callbacks.
  187. */
  188. struct GNUNET_CONNECTION_Handle *connection;
  189. /**
  190. * User context value, manipulated using
  191. * 'GNUNET_SERVER_client_{get/set}_user_context' functions.
  192. */
  193. void *user_context;
  194. /**
  195. * ID of task used to restart processing.
  196. */
  197. struct GNUNET_SCHEDULER_Task * restart_task;
  198. /**
  199. * Task that warns about missing calls to #GNUNET_SERVER_receive_done.
  200. */
  201. struct GNUNET_SCHEDULER_Task * warn_task;
  202. /**
  203. * Time when the warn task was started.
  204. */
  205. struct GNUNET_TIME_Absolute warn_start;
  206. /**
  207. * Last activity on this socket (used to time it out
  208. * if reference_count == 0).
  209. */
  210. struct GNUNET_TIME_Absolute last_activity;
  211. /**
  212. * Transmission handle we return for this client from
  213. * #GNUNET_SERVER_notify_transmit_ready.
  214. */
  215. struct GNUNET_SERVER_TransmitHandle th;
  216. /**
  217. * After how long should an idle connection time
  218. * out (on write).
  219. */
  220. struct GNUNET_TIME_Relative idle_timeout;
  221. /**
  222. * Number of external entities with a reference to
  223. * this client object.
  224. */
  225. unsigned int reference_count;
  226. /**
  227. * Was processing if incoming messages suspended while
  228. * we were still processing data already received?
  229. * This is a counter saying how often processing was
  230. * suspended (once per handler invoked).
  231. */
  232. unsigned int suspended;
  233. /**
  234. * Last size given when user context was initialized; used for
  235. * sanity check.
  236. */
  237. size_t user_context_size;
  238. /**
  239. * Are we currently in the "process_client_buffer" function (and
  240. * will hence restart the receive job on exit if suspended == 0 once
  241. * we are done?). If this is set, then "receive_done" will
  242. * essentially only decrement suspended; if this is not set, then
  243. * "receive_done" may need to restart the receive process (either
  244. * from the side-buffer or via select/recv).
  245. */
  246. int in_process_client_buffer;
  247. /**
  248. * We're about to close down this client.
  249. */
  250. int shutdown_now;
  251. /**
  252. * Are we currently trying to receive? (#GNUNET_YES if we are,
  253. * #GNUNET_NO if we are not, #GNUNET_SYSERR if data is already
  254. * available in MST).
  255. */
  256. int receive_pending;
  257. /**
  258. * Persist the file handle for this client no matter what happens,
  259. * force the OS to close once the process actually dies. Should only
  260. * be used in special cases!
  261. */
  262. int persist;
  263. /**
  264. * Is this client a 'monitor' client that should not be counted
  265. * when deciding on destroying the server during soft shutdown?
  266. * (see also #GNUNET_SERVICE_start)
  267. */
  268. int is_monitor;
  269. /**
  270. * Type of last message processed (for warn_no_receive_done).
  271. */
  272. uint16_t warn_type;
  273. };
  274. /**
  275. * Return user context associated with the given client.
  276. * Note: you should probably use the macro (call without the underscore).
  277. *
  278. * @param client client to query
  279. * @param size number of bytes in user context struct (for verification only)
  280. * @return pointer to user context
  281. */
  282. void *
  283. GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
  284. size_t size)
  285. {
  286. if ((0 == client->user_context_size) &&
  287. (NULL == client->user_context))
  288. return NULL; /* never set */
  289. GNUNET_assert (size == client->user_context_size);
  290. return client->user_context;
  291. }
  292. /**
  293. * Set user context to be associated with the given client.
  294. * Note: you should probably use the macro (call without the underscore).
  295. *
  296. * @param client client to query
  297. * @param ptr pointer to user context
  298. * @param size number of bytes in user context struct (for verification only)
  299. */
  300. void
  301. GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
  302. void *ptr,
  303. size_t size)
  304. {
  305. if (NULL == ptr)
  306. {
  307. client->user_context_size = 0;
  308. client->user_context = ptr;
  309. return;
  310. }
  311. client->user_context_size = size;
  312. client->user_context = ptr;
  313. }
  314. /**
  315. * Scheduler says our listen socket is ready. Process it!
  316. *
  317. * @param cls handle to our server for which we are processing the listen
  318. * socket
  319. */
  320. static void
  321. process_listen_socket (void *cls)
  322. {
  323. struct GNUNET_SERVER_Handle *server = cls;
  324. const struct GNUNET_SCHEDULER_TaskContext *tc;
  325. struct GNUNET_CONNECTION_Handle *sock;
  326. unsigned int i;
  327. server->listen_task = NULL;
  328. tc = GNUNET_SCHEDULER_get_task_context ();
  329. for (i = 0; NULL != server->listen_sockets[i]; i++)
  330. {
  331. if (GNUNET_NETWORK_fdset_isset (tc->read_ready,
  332. server->listen_sockets[i]))
  333. {
  334. sock =
  335. GNUNET_CONNECTION_create_from_accept (server->access_cb,
  336. server->access_cb_cls,
  337. server->listen_sockets[i]);
  338. if (NULL != sock)
  339. {
  340. LOG (GNUNET_ERROR_TYPE_DEBUG,
  341. "Server accepted incoming connection.\n");
  342. (void) GNUNET_SERVER_connect_socket (server,
  343. sock);
  344. }
  345. }
  346. }
  347. /* listen for more! */
  348. GNUNET_SERVER_resume (server);
  349. }
  350. /**
  351. * Create and initialize a listen socket for the server.
  352. *
  353. * @param server_addr address to listen on
  354. * @param socklen length of @a server_addr
  355. * @return NULL on error, otherwise the listen socket
  356. */
  357. static struct GNUNET_NETWORK_Handle *
  358. open_listen_socket (const struct sockaddr *server_addr,
  359. socklen_t socklen)
  360. {
  361. struct GNUNET_NETWORK_Handle *sock;
  362. uint16_t port;
  363. int eno;
  364. switch (server_addr->sa_family)
  365. {
  366. case AF_INET:
  367. port = ntohs (((const struct sockaddr_in *) server_addr)->sin_port);
  368. break;
  369. case AF_INET6:
  370. port = ntohs (((const struct sockaddr_in6 *) server_addr)->sin6_port);
  371. break;
  372. case AF_UNIX:
  373. port = 0;
  374. break;
  375. default:
  376. GNUNET_break (0);
  377. port = 0;
  378. break;
  379. }
  380. sock = GNUNET_NETWORK_socket_create (server_addr->sa_family, SOCK_STREAM, 0);
  381. if (NULL == sock)
  382. {
  383. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
  384. errno = 0;
  385. return NULL;
  386. }
  387. /* bind the socket */
  388. if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, server_addr, socklen))
  389. {
  390. eno = errno;
  391. if (EADDRINUSE != errno)
  392. {
  393. /* we don't log 'EADDRINUSE' here since an IPv4 bind may
  394. * fail if we already took the port on IPv6; if both IPv4 and
  395. * IPv6 binds fail, then our caller will log using the
  396. * errno preserved in 'eno' */
  397. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
  398. "bind");
  399. if (0 != port)
  400. LOG (GNUNET_ERROR_TYPE_ERROR,
  401. _("`%s' failed for port %d (%s).\n"),
  402. "bind",
  403. port,
  404. (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
  405. eno = 0;
  406. }
  407. else
  408. {
  409. if (0 != port)
  410. LOG (GNUNET_ERROR_TYPE_WARNING,
  411. _("`%s' failed for port %d (%s): address already in use\n"),
  412. "bind", port,
  413. (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
  414. else if (AF_UNIX == server_addr->sa_family)
  415. {
  416. LOG (GNUNET_ERROR_TYPE_WARNING,
  417. _("`%s' failed for `%s': address already in use\n"),
  418. "bind",
  419. GNUNET_a2s (server_addr, socklen));
  420. }
  421. }
  422. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
  423. errno = eno;
  424. return NULL;
  425. }
  426. if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
  427. {
  428. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
  429. "listen");
  430. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
  431. errno = 0;
  432. return NULL;
  433. }
  434. if (0 != port)
  435. LOG (GNUNET_ERROR_TYPE_DEBUG,
  436. "Server starts to listen on port %u.\n",
  437. port);
  438. return sock;
  439. }
  440. /**
  441. * Create a new server.
  442. *
  443. * @param access_cb function for access control
  444. * @param access_cb_cls closure for @a access_cb
  445. * @param lsocks NULL-terminated array of listen sockets
  446. * @param idle_timeout after how long should we timeout idle connections?
  447. * @param require_found if #GNUNET_YES, connections sending messages of unknown type
  448. * will be closed
  449. * @return handle for the new server, NULL on error
  450. * (typically, "port" already in use)
  451. */
  452. struct GNUNET_SERVER_Handle *
  453. GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access_cb,
  454. void *access_cb_cls,
  455. struct GNUNET_NETWORK_Handle **lsocks,
  456. struct GNUNET_TIME_Relative idle_timeout,
  457. int require_found)
  458. {
  459. struct GNUNET_SERVER_Handle *server;
  460. server = GNUNET_new (struct GNUNET_SERVER_Handle);
  461. server->idle_timeout = idle_timeout;
  462. server->listen_sockets = lsocks;
  463. server->access_cb = access_cb;
  464. server->access_cb_cls = access_cb_cls;
  465. server->require_found = require_found;
  466. if (NULL != lsocks)
  467. GNUNET_SERVER_resume (server);
  468. return server;
  469. }
  470. /**
  471. * Create a new server.
  472. *
  473. * @param access_cb function for access control
  474. * @param access_cb_cls closure for @a access_cb
  475. * @param server_addr address to listen on (including port), NULL terminated array
  476. * @param socklen length of server_addr
  477. * @param idle_timeout after how long should we timeout idle connections?
  478. * @param require_found if YES, connections sending messages of unknown type
  479. * will be closed
  480. * @return handle for the new server, NULL on error
  481. * (typically, "port" already in use)
  482. */
  483. struct GNUNET_SERVER_Handle *
  484. GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access_cb,
  485. void *access_cb_cls,
  486. struct sockaddr *const *server_addr,
  487. const socklen_t * socklen,
  488. struct GNUNET_TIME_Relative idle_timeout,
  489. int require_found)
  490. {
  491. struct GNUNET_NETWORK_Handle **lsocks;
  492. unsigned int i;
  493. unsigned int j;
  494. unsigned int k;
  495. int seen;
  496. i = 0;
  497. while (NULL != server_addr[i])
  498. i++;
  499. if (i > 0)
  500. {
  501. lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
  502. i = 0;
  503. j = 0;
  504. while (NULL != server_addr[i])
  505. {
  506. seen = 0;
  507. for (k=0;k<i;k++)
  508. if ( (socklen[k] == socklen[i]) &&
  509. (0 == memcmp (server_addr[k], server_addr[i], socklen[i])) )
  510. {
  511. seen = 1;
  512. break;
  513. }
  514. if (0 != seen)
  515. {
  516. /* duplicate address, skip */
  517. i++;
  518. continue;
  519. }
  520. lsocks[j] = open_listen_socket (server_addr[i], socklen[i]);
  521. if (NULL != lsocks[j])
  522. j++;
  523. i++;
  524. }
  525. if (0 == j)
  526. {
  527. if (0 != errno)
  528. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
  529. GNUNET_free (lsocks);
  530. lsocks = NULL;
  531. }
  532. }
  533. else
  534. {
  535. lsocks = NULL;
  536. }
  537. return GNUNET_SERVER_create_with_sockets (access_cb,
  538. access_cb_cls,
  539. lsocks,
  540. idle_timeout,
  541. require_found);
  542. }
  543. /**
  544. * Set the 'monitor' flag on this client. Clients which have been
  545. * marked as 'monitors' won't prevent the server from shutting down
  546. * once '#GNUNET_SERVER_stop_listening()' has been invoked. The idea is
  547. * that for "normal" clients we likely want to allow them to process
  548. * their requests; however, monitor-clients are likely to 'never'
  549. * disconnect during shutdown and thus will not be considered when
  550. * determining if the server should continue to exist after
  551. * #GNUNET_SERVER_destroy() has been called.
  552. *
  553. * @param client the client to set the 'monitor' flag on
  554. */
  555. void
  556. GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client)
  557. {
  558. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  559. "Marking client as monitor!\n");
  560. client->is_monitor = GNUNET_YES;
  561. }
  562. /**
  563. * Helper function for #test_monitor_clients() to trigger
  564. * #GNUNET_SERVER_destroy() after the stack has unwound.
  565. *
  566. * @param cls the `struct GNUNET_SERVER_Handle *` to destroy
  567. */
  568. static void
  569. do_destroy (void *cls)
  570. {
  571. struct GNUNET_SERVER_Handle *server = cls;
  572. GNUNET_SERVER_destroy (server);
  573. }
  574. /**
  575. * Check if only 'monitor' clients are left. If so, destroy the
  576. * server completely.
  577. *
  578. * @param server server to test for full shutdown
  579. */
  580. static void
  581. test_monitor_clients (struct GNUNET_SERVER_Handle *server)
  582. {
  583. struct GNUNET_SERVER_Client *client;
  584. if (GNUNET_YES != server->in_soft_shutdown)
  585. return;
  586. for (client = server->clients_head; NULL != client; client = client->next)
  587. if (GNUNET_NO == client->is_monitor)
  588. return; /* not done yet */
  589. server->in_soft_shutdown = GNUNET_SYSERR;
  590. (void) GNUNET_SCHEDULER_add_now (&do_destroy, server);
  591. }
  592. /**
  593. * Suspend accepting connections from the listen socket temporarily.
  594. *
  595. * @param server server to stop accepting connections.
  596. */
  597. void
  598. GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server)
  599. {
  600. if (NULL != server->listen_task)
  601. {
  602. GNUNET_SCHEDULER_cancel (server->listen_task);
  603. server->listen_task = NULL;
  604. }
  605. }
  606. /**
  607. * Resume accepting connections from the listen socket.
  608. *
  609. * @param server server to stop accepting connections.
  610. */
  611. void
  612. GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server)
  613. {
  614. struct GNUNET_NETWORK_FDSet *r;
  615. unsigned int i;
  616. if (NULL == server->listen_sockets)
  617. return;
  618. if (NULL == server->listen_sockets[0])
  619. return; /* nothing to do, no listen sockets! */
  620. if (NULL == server->listen_sockets[1])
  621. {
  622. /* simplified method: no fd set needed; this is then much simpler
  623. and much more efficient */
  624. server->listen_task =
  625. GNUNET_SCHEDULER_add_read_net_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
  626. GNUNET_SCHEDULER_PRIORITY_HIGH,
  627. server->listen_sockets[0],
  628. &process_listen_socket, server);
  629. return;
  630. }
  631. r = GNUNET_NETWORK_fdset_create ();
  632. i = 0;
  633. while (NULL != server->listen_sockets[i])
  634. GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
  635. server->listen_task =
  636. GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
  637. GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
  638. &process_listen_socket, server);
  639. GNUNET_NETWORK_fdset_destroy (r);
  640. }
  641. /**
  642. * Stop the listen socket and get ready to shutdown the server
  643. * once only 'monitor' clients are left.
  644. *
  645. * @param server server to stop listening on
  646. */
  647. void
  648. GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server)
  649. {
  650. unsigned int i;
  651. LOG (GNUNET_ERROR_TYPE_DEBUG,
  652. "Server in soft shutdown\n");
  653. if (NULL != server->listen_task)
  654. {
  655. GNUNET_SCHEDULER_cancel (server->listen_task);
  656. server->listen_task = NULL;
  657. }
  658. if (NULL != server->listen_sockets)
  659. {
  660. i = 0;
  661. while (NULL != server->listen_sockets[i])
  662. GNUNET_break (GNUNET_OK ==
  663. GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
  664. GNUNET_free (server->listen_sockets);
  665. server->listen_sockets = NULL;
  666. }
  667. if (GNUNET_NO == server->in_soft_shutdown)
  668. server->in_soft_shutdown = GNUNET_YES;
  669. test_monitor_clients (server);
  670. }
  671. /**
  672. * Free resources held by this server.
  673. *
  674. * @param server server to destroy
  675. */
  676. void
  677. GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server)
  678. {
  679. struct HandlerList *hpos;
  680. struct NotifyList *npos;
  681. unsigned int i;
  682. LOG (GNUNET_ERROR_TYPE_DEBUG,
  683. "Server shutting down.\n");
  684. if (NULL != server->listen_task)
  685. {
  686. GNUNET_SCHEDULER_cancel (server->listen_task);
  687. server->listen_task = NULL;
  688. }
  689. if (NULL != server->listen_sockets)
  690. {
  691. i = 0;
  692. while (NULL != server->listen_sockets[i])
  693. GNUNET_break (GNUNET_OK ==
  694. GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
  695. GNUNET_free (server->listen_sockets);
  696. server->listen_sockets = NULL;
  697. }
  698. while (NULL != server->clients_head)
  699. GNUNET_SERVER_client_disconnect (server->clients_head);
  700. while (NULL != (hpos = server->handlers))
  701. {
  702. server->handlers = hpos->next;
  703. GNUNET_free (hpos);
  704. }
  705. while (NULL != (npos = server->disconnect_notify_list_head))
  706. {
  707. npos->callback (npos->callback_cls,
  708. NULL);
  709. GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
  710. server->disconnect_notify_list_tail,
  711. npos);
  712. GNUNET_free (npos);
  713. }
  714. while (NULL != (npos = server->connect_notify_list_head))
  715. {
  716. npos->callback (npos->callback_cls,
  717. NULL);
  718. GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
  719. server->connect_notify_list_tail,
  720. npos);
  721. GNUNET_free (npos);
  722. }
  723. GNUNET_free (server);
  724. }
  725. /**
  726. * Add additional handlers to an existing server.
  727. *
  728. * @param server the server to add handlers to
  729. * @param handlers array of message handlers for
  730. * incoming messages; the last entry must
  731. * have "NULL" for the "callback"; multiple
  732. * entries for the same type are allowed,
  733. * they will be called in order of occurence.
  734. * These handlers can be removed later;
  735. * the handlers array must exist until removed
  736. * (or server is destroyed).
  737. */
  738. void
  739. GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
  740. const struct GNUNET_SERVER_MessageHandler *handlers)
  741. {
  742. struct HandlerList *p;
  743. p = GNUNET_new (struct HandlerList);
  744. p->handlers = handlers;
  745. p->next = server->handlers;
  746. server->handlers = p;
  747. }
  748. /**
  749. * Change functions used by the server to tokenize the message stream.
  750. * (very rarely used).
  751. *
  752. * @param server server to modify
  753. * @param create new tokenizer initialization function
  754. * @param destroy new tokenizer destruction function
  755. * @param receive new tokenizer receive function
  756. * @param cls closure for @a create, @a receive, @a destroy
  757. */
  758. void
  759. GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
  760. GNUNET_SERVER_MstCreateCallback create,
  761. GNUNET_SERVER_MstDestroyCallback destroy,
  762. GNUNET_SERVER_MstReceiveCallback receive,
  763. void *cls)
  764. {
  765. server->mst_create = create;
  766. server->mst_destroy = destroy;
  767. server->mst_receive = receive;
  768. server->mst_cls = cls;
  769. }
  770. /**
  771. * Task run to warn about missing calls to #GNUNET_SERVER_receive_done.
  772. *
  773. * @param cls our `struct GNUNET_SERVER_Client *` to process more requests from
  774. */
  775. static void
  776. warn_no_receive_done (void *cls)
  777. {
  778. struct GNUNET_SERVER_Client *client = cls;
  779. GNUNET_break (0 != client->warn_type); /* type should never be 0 here, as we don't use 0 */
  780. client->warn_task =
  781. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
  782. &warn_no_receive_done, client);
  783. LOG (GNUNET_ERROR_TYPE_WARNING,
  784. _("Processing code for message of type %u did not call `GNUNET_SERVER_receive_done' after %s\n"),
  785. (unsigned int) client->warn_type,
  786. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (client->warn_start),
  787. GNUNET_YES));
  788. }
  789. /**
  790. * Disable the warning the server issues if a message is not acknowledged
  791. * in a timely fashion. Use this call if a client is intentionally delayed
  792. * for a while. Only applies to the current message.
  793. *
  794. * @param client client for which to disable the warning
  795. */
  796. void
  797. GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
  798. {
  799. if (NULL != client->warn_task)
  800. {
  801. GNUNET_SCHEDULER_cancel (client->warn_task);
  802. client->warn_task = NULL;
  803. }
  804. }
  805. /**
  806. * Inject a message into the server, pretend it came
  807. * from the specified client. Delivery of the message
  808. * will happen instantly (if a handler is installed;
  809. * otherwise the call does nothing).
  810. *
  811. * @param server the server receiving the message
  812. * @param sender the "pretended" sender of the message
  813. * can be NULL!
  814. * @param message message to transmit
  815. * @return #GNUNET_OK if the message was OK and the
  816. * connection can stay open
  817. * #GNUNET_SYSERR if the connection to the
  818. * client should be shut down
  819. */
  820. int
  821. GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
  822. struct GNUNET_SERVER_Client *sender,
  823. const struct GNUNET_MessageHeader *message)
  824. {
  825. struct HandlerList *pos;
  826. const struct GNUNET_SERVER_MessageHandler *mh;
  827. unsigned int i;
  828. uint16_t type;
  829. uint16_t size;
  830. int found;
  831. type = ntohs (message->type);
  832. size = ntohs (message->size);
  833. LOG (GNUNET_ERROR_TYPE_INFO,
  834. "Received message of type %u and size %u from client\n",
  835. type, size);
  836. found = GNUNET_NO;
  837. for (pos = server->handlers; NULL != pos; pos = pos->next)
  838. {
  839. i = 0;
  840. while (pos->handlers[i].callback != NULL)
  841. {
  842. mh = &pos->handlers[i];
  843. if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
  844. {
  845. if ((0 != mh->expected_size) && (mh->expected_size != size))
  846. {
  847. #if GNUNET8_NETWORK_IS_DEAD
  848. LOG (GNUNET_ERROR_TYPE_WARNING,
  849. "Expected %u bytes for message of type %u, got %u\n",
  850. mh->expected_size, mh->type, size);
  851. GNUNET_break_op (0);
  852. #else
  853. LOG (GNUNET_ERROR_TYPE_DEBUG,
  854. "Expected %u bytes for message of type %u, got %u\n",
  855. mh->expected_size, mh->type, size);
  856. #endif
  857. return GNUNET_SYSERR;
  858. }
  859. if (NULL != sender)
  860. {
  861. if ( (0 == sender->suspended) &&
  862. (NULL == sender->warn_task) )
  863. {
  864. GNUNET_break (0 != type); /* type should never be 0 here, as we don't use 0 */
  865. sender->warn_start = GNUNET_TIME_absolute_get ();
  866. sender->warn_task =
  867. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
  868. &warn_no_receive_done,
  869. sender);
  870. sender->warn_type = type;
  871. }
  872. sender->suspended++;
  873. }
  874. mh->callback (mh->callback_cls, sender, message);
  875. found = GNUNET_YES;
  876. }
  877. i++;
  878. }
  879. }
  880. if (GNUNET_NO == found)
  881. {
  882. LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
  883. "Received message of unknown type %d\n", type);
  884. if (GNUNET_YES == server->require_found)
  885. return GNUNET_SYSERR;
  886. }
  887. return GNUNET_OK;
  888. }
  889. /**
  890. * We are receiving an incoming message. Process it.
  891. *
  892. * @param cls our closure (handle for the client)
  893. * @param buf buffer with data received from network
  894. * @param available number of bytes available in buf
  895. * @param addr address of the sender
  896. * @param addrlen length of @a addr
  897. * @param errCode code indicating errors receiving, 0 for success
  898. */
  899. static void
  900. process_incoming (void *cls,
  901. const void *buf,
  902. size_t available,
  903. const struct sockaddr *addr,
  904. socklen_t addrlen,
  905. int errCode);
  906. /**
  907. * Process messages from the client's message tokenizer until either
  908. * the tokenizer is empty (and then schedule receiving more), or
  909. * until some handler is not immediately done (then wait for restart_processing)
  910. * or shutdown.
  911. *
  912. * @param client the client to process, RC must have already been increased
  913. * using #GNUNET_SERVER_client_keep and will be decreased by one in this
  914. * function
  915. * @param ret #GNUNET_NO to start processing from the buffer,
  916. * #GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
  917. * #GNUNET_SYSERR if we should instantly abort due to error in a previous step
  918. */
  919. static void
  920. process_mst (struct GNUNET_SERVER_Client *client,
  921. int ret)
  922. {
  923. while ((GNUNET_SYSERR != ret) && (NULL != client->server) &&
  924. (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
  925. {
  926. if (GNUNET_OK == ret)
  927. {
  928. LOG (GNUNET_ERROR_TYPE_DEBUG,
  929. "Server re-enters receive loop, timeout: %s.\n",
  930. GNUNET_STRINGS_relative_time_to_string (client->idle_timeout, GNUNET_YES));
  931. client->receive_pending = GNUNET_YES;
  932. if (GNUNET_OK !=
  933. GNUNET_CONNECTION_receive (client->connection,
  934. GNUNET_MAX_MESSAGE_SIZE - 1,
  935. client->idle_timeout,
  936. &process_incoming,
  937. client))
  938. return;
  939. break;
  940. }
  941. LOG (GNUNET_ERROR_TYPE_DEBUG,
  942. "Server processes additional messages instantly.\n");
  943. if (NULL != client->server->mst_receive)
  944. ret =
  945. client->server->mst_receive (client->server->mst_cls, client->mst,
  946. client, NULL, 0, GNUNET_NO, GNUNET_YES);
  947. else
  948. ret =
  949. GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
  950. GNUNET_YES);
  951. }
  952. LOG (GNUNET_ERROR_TYPE_DEBUG,
  953. "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
  954. ret, client->server,
  955. client->shutdown_now,
  956. client->suspended);
  957. if (GNUNET_NO == ret)
  958. {
  959. LOG (GNUNET_ERROR_TYPE_DEBUG,
  960. "Server has more data pending but is suspended.\n");
  961. client->receive_pending = GNUNET_SYSERR; /* data pending */
  962. }
  963. if ( (GNUNET_SYSERR == ret) ||
  964. (GNUNET_YES == client->shutdown_now) )
  965. GNUNET_SERVER_client_disconnect (client);
  966. }
  967. /**
  968. * We are receiving an incoming message. Process it.
  969. *
  970. * @param cls our closure (handle for the client)
  971. * @param buf buffer with data received from network
  972. * @param available number of bytes available in buf
  973. * @param addr address of the sender
  974. * @param addrlen length of @a addr
  975. * @param errCode code indicating errors receiving, 0 for success
  976. */
  977. static void
  978. process_incoming (void *cls,
  979. const void *buf,
  980. size_t available,
  981. const struct sockaddr *addr,
  982. socklen_t addrlen,
  983. int errCode)
  984. {
  985. struct GNUNET_SERVER_Client *client = cls;
  986. struct GNUNET_SERVER_Handle *server = client->server;
  987. struct GNUNET_TIME_Absolute end;
  988. struct GNUNET_TIME_Absolute now;
  989. int ret;
  990. GNUNET_assert (GNUNET_YES == client->receive_pending);
  991. client->receive_pending = GNUNET_NO;
  992. now = GNUNET_TIME_absolute_get ();
  993. end = GNUNET_TIME_absolute_add (client->last_activity,
  994. client->idle_timeout);
  995. if ( (NULL == buf) &&
  996. (0 == available) &&
  997. (NULL == addr) &&
  998. (0 == errCode) &&
  999. (GNUNET_YES != client->shutdown_now) &&
  1000. (NULL != server) &&
  1001. (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
  1002. (end.abs_value_us > now.abs_value_us) )
  1003. {
  1004. /* wait longer, timeout changed (i.e. due to us sending) */
  1005. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1006. "Receive time out, but no disconnect due to sending (%p)\n",
  1007. client);
  1008. client->receive_pending = GNUNET_YES;
  1009. GNUNET_CONNECTION_receive (client->connection,
  1010. GNUNET_MAX_MESSAGE_SIZE - 1,
  1011. GNUNET_TIME_absolute_get_remaining (end),
  1012. &process_incoming,
  1013. client);
  1014. return;
  1015. }
  1016. if ( (NULL == buf) ||
  1017. (0 == available) ||
  1018. (0 != errCode) ||
  1019. (NULL == server) ||
  1020. (GNUNET_YES == client->shutdown_now) ||
  1021. (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)) )
  1022. {
  1023. /* other side closed connection, error connecting, etc. */
  1024. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1025. "Failed to connect or other side closed connection (%p)\n",
  1026. client);
  1027. GNUNET_SERVER_client_disconnect (client);
  1028. return;
  1029. }
  1030. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1031. "Server receives %u bytes from `%s'.\n",
  1032. (unsigned int) available,
  1033. GNUNET_a2s (addr, addrlen));
  1034. GNUNET_SERVER_client_keep (client);
  1035. client->last_activity = now;
  1036. if (NULL != server->mst_receive)
  1037. {
  1038. ret = client->server->mst_receive (client->server->mst_cls,
  1039. client->mst,
  1040. client,
  1041. buf,
  1042. available,
  1043. GNUNET_NO,
  1044. GNUNET_YES);
  1045. }
  1046. else if (NULL != client->mst)
  1047. {
  1048. ret =
  1049. GNUNET_SERVER_mst_receive (client->mst,
  1050. client,
  1051. buf,
  1052. available,
  1053. GNUNET_NO,
  1054. GNUNET_YES);
  1055. }
  1056. else
  1057. {
  1058. GNUNET_break (0);
  1059. return;
  1060. }
  1061. process_mst (client,
  1062. ret);
  1063. GNUNET_SERVER_client_drop (client);
  1064. }
  1065. /**
  1066. * Task run to start again receiving from the network
  1067. * and process requests.
  1068. *
  1069. * @param cls our `struct GNUNET_SERVER_Client *` to process more requests from
  1070. */
  1071. static void
  1072. restart_processing (void *cls)
  1073. {
  1074. struct GNUNET_SERVER_Client *client = cls;
  1075. GNUNET_assert (GNUNET_YES != client->shutdown_now);
  1076. client->restart_task = NULL;
  1077. if (GNUNET_NO == client->receive_pending)
  1078. {
  1079. LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
  1080. client->receive_pending = GNUNET_YES;
  1081. GNUNET_CONNECTION_receive (client->connection,
  1082. GNUNET_MAX_MESSAGE_SIZE - 1,
  1083. client->idle_timeout,
  1084. &process_incoming,
  1085. client);
  1086. return;
  1087. }
  1088. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1089. "Server continues processing messages still in the buffer.\n");
  1090. GNUNET_SERVER_client_keep (client);
  1091. client->receive_pending = GNUNET_NO;
  1092. process_mst (client,
  1093. GNUNET_NO);
  1094. GNUNET_SERVER_client_drop (client);
  1095. }
  1096. /**
  1097. * This function is called whenever our inbound message tokenizer has
  1098. * received a complete message.
  1099. *
  1100. * @param cls closure (struct GNUNET_SERVER_Handle)
  1101. * @param client identification of the client (`struct GNUNET_SERVER_Client *`)
  1102. * @param message the actual message
  1103. *
  1104. * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
  1105. */
  1106. static int
  1107. client_message_tokenizer_callback (void *cls,
  1108. void *client,
  1109. const struct GNUNET_MessageHeader *message)
  1110. {
  1111. struct GNUNET_SERVER_Handle *server = cls;
  1112. struct GNUNET_SERVER_Client *sender = client;
  1113. int ret;
  1114. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1115. "Tokenizer gives server message of type %u and size %u from client\n",
  1116. ntohs (message->type), ntohs (message->size));
  1117. sender->in_process_client_buffer = GNUNET_YES;
  1118. ret = GNUNET_SERVER_inject (server, sender, message);
  1119. sender->in_process_client_buffer = GNUNET_NO;
  1120. if ( (GNUNET_OK != ret) || (GNUNET_YES == sender->shutdown_now) )
  1121. {
  1122. GNUNET_SERVER_client_disconnect (sender);
  1123. return GNUNET_SYSERR;
  1124. }
  1125. return GNUNET_OK;
  1126. }
  1127. /**
  1128. * Add a TCP socket-based connection to the set of handles managed by
  1129. * this server. Use this function for outgoing (P2P) connections that
  1130. * we initiated (and where this server should process incoming
  1131. * messages).
  1132. *
  1133. * @param server the server to use
  1134. * @param connection the connection to manage (client must
  1135. * stop using this connection from now on)
  1136. * @return the client handle
  1137. */
  1138. struct GNUNET_SERVER_Client *
  1139. GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
  1140. struct GNUNET_CONNECTION_Handle *connection)
  1141. {
  1142. struct GNUNET_SERVER_Client *client;
  1143. struct NotifyList *n;
  1144. client = GNUNET_new (struct GNUNET_SERVER_Client);
  1145. client->connection = connection;
  1146. client->server = server;
  1147. client->last_activity = GNUNET_TIME_absolute_get ();
  1148. client->idle_timeout = server->idle_timeout;
  1149. GNUNET_CONTAINER_DLL_insert (server->clients_head,
  1150. server->clients_tail,
  1151. client);
  1152. if (NULL != server->mst_create)
  1153. client->mst =
  1154. server->mst_create (server->mst_cls, client);
  1155. else
  1156. client->mst =
  1157. GNUNET_SERVER_mst_create (&client_message_tokenizer_callback,
  1158. server);
  1159. GNUNET_assert (NULL != client->mst);
  1160. for (n = server->connect_notify_list_head; NULL != n; n = n->next)
  1161. n->callback (n->callback_cls, client);
  1162. client->receive_pending = GNUNET_YES;
  1163. if (GNUNET_SYSERR ==
  1164. GNUNET_CONNECTION_receive (client->connection,
  1165. GNUNET_MAX_MESSAGE_SIZE - 1,
  1166. client->idle_timeout,
  1167. &process_incoming,
  1168. client))
  1169. return NULL;
  1170. return client;
  1171. }
  1172. /**
  1173. * Change the timeout for a particular client. Decreasing the timeout
  1174. * may not go into effect immediately (only after the previous timeout
  1175. * times out or activity happens on the socket).
  1176. *
  1177. * @param client the client to update
  1178. * @param timeout new timeout for activities on the socket
  1179. */
  1180. void
  1181. GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
  1182. struct GNUNET_TIME_Relative timeout)
  1183. {
  1184. client->idle_timeout = timeout;
  1185. }
  1186. /**
  1187. * Notify the server that the given client handle should
  1188. * be kept (keeps the connection up if possible, increments
  1189. * the internal reference counter).
  1190. *
  1191. * @param client the client to keep
  1192. */
  1193. void
  1194. GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
  1195. {
  1196. client->reference_count++;
  1197. }
  1198. /**
  1199. * Notify the server that the given client handle is no
  1200. * longer required. Decrements the reference counter. If
  1201. * that counter reaches zero an inactive connection maybe
  1202. * closed.
  1203. *
  1204. * @param client the client to drop
  1205. */
  1206. void
  1207. GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
  1208. {
  1209. GNUNET_assert (client->reference_count > 0);
  1210. client->reference_count--;
  1211. if ((GNUNET_YES == client->shutdown_now) && (0 == client->reference_count))
  1212. GNUNET_SERVER_client_disconnect (client);
  1213. }
  1214. /**
  1215. * Obtain the network address of the other party.
  1216. *
  1217. * @param client the client to get the address for
  1218. * @param addr where to store the address
  1219. * @param addrlen where to store the length of the @a addr
  1220. * @return #GNUNET_OK on success
  1221. */
  1222. int
  1223. GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
  1224. void **addr, size_t * addrlen)
  1225. {
  1226. return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
  1227. }
  1228. /**
  1229. * Ask the server to notify us whenever a client disconnects.
  1230. * This function is called whenever the actual network connection
  1231. * is closed; the reference count may be zero or larger than zero
  1232. * at this point.
  1233. *
  1234. * @param server the server manageing the clients
  1235. * @param callback function to call on disconnect
  1236. * @param callback_cls closure for @a callback
  1237. */
  1238. void
  1239. GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
  1240. GNUNET_SERVER_DisconnectCallback callback,
  1241. void *callback_cls)
  1242. {
  1243. struct NotifyList *n;
  1244. n = GNUNET_new (struct NotifyList);
  1245. n->callback = callback;
  1246. n->callback_cls = callback_cls;
  1247. GNUNET_CONTAINER_DLL_insert (server->disconnect_notify_list_head,
  1248. server->disconnect_notify_list_tail,
  1249. n);
  1250. }
  1251. /**
  1252. * Ask the server to notify us whenever a client connects.
  1253. * This function is called whenever the actual network connection
  1254. * is opened. If the server is destroyed before this
  1255. * notification is explicitly cancelled, the 'callback' will
  1256. * once be called with a 'client' argument of NULL to indicate
  1257. * that the server itself is now gone (and that the callback
  1258. * won't be called anymore and also can no longer be cancelled).
  1259. *
  1260. * @param server the server manageing the clients
  1261. * @param callback function to call on sconnect
  1262. * @param callback_cls closure for @a callback
  1263. */
  1264. void
  1265. GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
  1266. GNUNET_SERVER_ConnectCallback callback,
  1267. void *callback_cls)
  1268. {
  1269. struct NotifyList *n;
  1270. struct GNUNET_SERVER_Client *client;
  1271. n = GNUNET_new (struct NotifyList);
  1272. n->callback = callback;
  1273. n->callback_cls = callback_cls;
  1274. GNUNET_CONTAINER_DLL_insert (server->connect_notify_list_head,
  1275. server->connect_notify_list_tail,
  1276. n);
  1277. for (client = server->clients_head; NULL != client; client = client->next)
  1278. callback (callback_cls, client);
  1279. }
  1280. /**
  1281. * Ask the server to stop notifying us whenever a client connects.
  1282. *
  1283. * @param server the server manageing the clients
  1284. * @param callback function to call on connect
  1285. * @param callback_cls closure for @a callback
  1286. */
  1287. void
  1288. GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
  1289. GNUNET_SERVER_DisconnectCallback callback,
  1290. void *callback_cls)
  1291. {
  1292. struct NotifyList *pos;
  1293. for (pos = server->disconnect_notify_list_head; NULL != pos; pos = pos->next)
  1294. if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
  1295. break;
  1296. if (NULL == pos)
  1297. {
  1298. GNUNET_break (0);
  1299. return;
  1300. }
  1301. GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
  1302. server->disconnect_notify_list_tail,
  1303. pos);
  1304. GNUNET_free (pos);
  1305. }
  1306. /**
  1307. * Ask the server to stop notifying us whenever a client disconnects.
  1308. *
  1309. * @param server the server manageing the clients
  1310. * @param callback function to call on disconnect
  1311. * @param callback_cls closure for @a callback
  1312. */
  1313. void
  1314. GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
  1315. GNUNET_SERVER_ConnectCallback callback,
  1316. void *callback_cls)
  1317. {
  1318. struct NotifyList *pos;
  1319. for (pos = server->connect_notify_list_head; NULL != pos; pos = pos->next)
  1320. if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
  1321. break;
  1322. if (NULL == pos)
  1323. {
  1324. GNUNET_break (0);
  1325. return;
  1326. }
  1327. GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
  1328. server->connect_notify_list_tail,
  1329. pos);
  1330. GNUNET_free (pos);
  1331. }
  1332. /**
  1333. * Ask the server to disconnect from the given client.
  1334. * This is the same as returning #GNUNET_SYSERR from a message
  1335. * handler, except that it allows dropping of a client even
  1336. * when not handling a message from that client.
  1337. *
  1338. * @param client the client to disconnect from
  1339. */
  1340. void
  1341. GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
  1342. {
  1343. struct GNUNET_SERVER_Handle *server = client->server;
  1344. struct NotifyList *n;
  1345. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1346. "Client is being disconnected from the server.\n");
  1347. if (NULL != client->restart_task)
  1348. {
  1349. GNUNET_SCHEDULER_cancel (client->restart_task);
  1350. client->restart_task = NULL;
  1351. }
  1352. if (NULL != client->warn_task)
  1353. {
  1354. GNUNET_SCHEDULER_cancel (client->warn_task);
  1355. client->warn_task = NULL;
  1356. }
  1357. if (GNUNET_YES == client->receive_pending)
  1358. {
  1359. GNUNET_CONNECTION_receive_cancel (client->connection);
  1360. client->receive_pending = GNUNET_NO;
  1361. }
  1362. client->shutdown_now = GNUNET_YES;
  1363. client->reference_count++; /* make sure nobody else clean up client... */
  1364. if ( (NULL != client->mst) &&
  1365. (NULL != server) )
  1366. {
  1367. GNUNET_CONTAINER_DLL_remove (server->clients_head,
  1368. server->clients_tail,
  1369. client);
  1370. if (NULL != server->mst_destroy)
  1371. server->mst_destroy (server->mst_cls,
  1372. client->mst);
  1373. else
  1374. GNUNET_SERVER_mst_destroy (client->mst);
  1375. client->mst = NULL;
  1376. for (n = server->disconnect_notify_list_head; NULL != n; n = n->next)
  1377. n->callback (n->callback_cls,
  1378. client);
  1379. }
  1380. client->reference_count--;
  1381. if (client->reference_count > 0)
  1382. {
  1383. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1384. "RC of %p still positive, not destroying everything.\n",
  1385. client);
  1386. client->server = NULL;
  1387. return;
  1388. }
  1389. if (GNUNET_YES == client->in_process_client_buffer)
  1390. {
  1391. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1392. "Still processing inputs of %p, not destroying everything.\n",
  1393. client);
  1394. return;
  1395. }
  1396. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1397. "RC of %p now zero, destroying everything.\n",
  1398. client);
  1399. if (GNUNET_YES == client->persist)
  1400. GNUNET_CONNECTION_persist_ (client->connection);
  1401. if (NULL != client->th.cth)
  1402. GNUNET_SERVER_notify_transmit_ready_cancel (&client->th);
  1403. GNUNET_CONNECTION_destroy (client->connection);
  1404. /* need to cancel again, as it might have been re-added
  1405. in the meantime (i.e. during callbacks) */
  1406. if (NULL != client->warn_task)
  1407. {
  1408. GNUNET_SCHEDULER_cancel (client->warn_task);
  1409. client->warn_task = NULL;
  1410. }
  1411. if (GNUNET_YES == client->receive_pending)
  1412. {
  1413. GNUNET_CONNECTION_receive_cancel (client->connection);
  1414. client->receive_pending = GNUNET_NO;
  1415. }
  1416. GNUNET_free (client);
  1417. /* we might be in soft-shutdown, test if we're done */
  1418. if (NULL != server)
  1419. test_monitor_clients (server);
  1420. }
  1421. /**
  1422. * Disable the "CORK" feature for communication with the given client,
  1423. * forcing the OS to immediately flush the buffer on transmission
  1424. * instead of potentially buffering multiple messages.
  1425. *
  1426. * @param client handle to the client
  1427. * @return #GNUNET_OK on success
  1428. */
  1429. int
  1430. GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
  1431. {
  1432. return GNUNET_CONNECTION_disable_corking (client->connection);
  1433. }
  1434. /**
  1435. * Wrapper for transmission notification that calls the original
  1436. * callback and update the last activity time for our connection.
  1437. *
  1438. * @param cls the `struct GNUNET_SERVER_Client *`
  1439. * @param size number of bytes we can transmit
  1440. * @param buf where to copy the message
  1441. * @return number of bytes actually transmitted
  1442. */
  1443. static size_t
  1444. transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
  1445. {
  1446. struct GNUNET_SERVER_Client *client = cls;
  1447. GNUNET_CONNECTION_TransmitReadyNotify callback;
  1448. client->th.cth = NULL;
  1449. callback = client->th.callback;
  1450. client->th.callback = NULL;
  1451. client->last_activity = GNUNET_TIME_absolute_get ();
  1452. return callback (client->th.callback_cls, size, buf);
  1453. }
  1454. /**
  1455. * Notify us when the server has enough space to transmit
  1456. * a message of the given size to the given client.
  1457. *
  1458. * @param client client to transmit message to
  1459. * @param size requested amount of buffer space
  1460. * @param timeout after how long should we give up (and call
  1461. * notify with buf NULL and size 0)?
  1462. * @param callback function to call when space is available
  1463. * @param callback_cls closure for @a callback
  1464. * @return non-NULL if the notify callback was queued; can be used
  1465. * to cancel the request using
  1466. * #GNUNET_SERVER_notify_transmit_ready_cancel().
  1467. * NULL if we are already going to notify someone else (busy)
  1468. */
  1469. struct GNUNET_SERVER_TransmitHandle *
  1470. GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
  1471. size_t size,
  1472. struct GNUNET_TIME_Relative timeout,
  1473. GNUNET_CONNECTION_TransmitReadyNotify callback,
  1474. void *callback_cls)
  1475. {
  1476. if (NULL != client->th.callback)
  1477. return NULL;
  1478. client->th.callback_cls = callback_cls;
  1479. client->th.callback = callback;
  1480. client->th.cth = GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
  1481. timeout,
  1482. &transmit_ready_callback_wrapper,
  1483. client);
  1484. return &client->th;
  1485. }
  1486. /**
  1487. * Abort transmission request.
  1488. *
  1489. * @param th request to abort
  1490. */
  1491. void
  1492. GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th)
  1493. {
  1494. GNUNET_CONNECTION_notify_transmit_ready_cancel (th->cth);
  1495. th->cth = NULL;
  1496. th->callback = NULL;
  1497. }
  1498. /**
  1499. * Set the persistent flag on this client, used to setup client connection
  1500. * to only be killed when the service it's connected to is actually dead.
  1501. *
  1502. * @param client the client to set the persistent flag on
  1503. */
  1504. void
  1505. GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
  1506. {
  1507. client->persist = GNUNET_YES;
  1508. }
  1509. /**
  1510. * Resume receiving from this client, we are done processing the
  1511. * current request. This function must be called from within each
  1512. * GNUNET_SERVER_MessageCallback (or its respective continuations).
  1513. *
  1514. * @param client client we were processing a message of
  1515. * @param success #GNUNET_OK to keep the connection open and
  1516. * continue to receive
  1517. * #GNUNET_NO to close the connection (normal behavior)
  1518. * #GNUNET_SYSERR to close the connection (signal
  1519. * serious error)
  1520. */
  1521. void
  1522. GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
  1523. int success)
  1524. {
  1525. if (NULL == client)
  1526. return;
  1527. GNUNET_assert (client->suspended > 0);
  1528. client->suspended--;
  1529. if (GNUNET_OK != success)
  1530. {
  1531. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1532. "GNUNET_SERVER_receive_done called with failure indication\n");
  1533. if ( (client->reference_count > 0) || (client->suspended > 0) )
  1534. client->shutdown_now = GNUNET_YES;
  1535. else
  1536. GNUNET_SERVER_client_disconnect (client);
  1537. return;
  1538. }
  1539. if (client->suspended > 0)
  1540. {
  1541. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1542. "GNUNET_SERVER_receive_done called, but more clients pending\n");
  1543. return;
  1544. }
  1545. if (NULL != client->warn_task)
  1546. {
  1547. GNUNET_SCHEDULER_cancel (client->warn_task);
  1548. client->warn_task = NULL;
  1549. }
  1550. if (GNUNET_YES == client->in_process_client_buffer)
  1551. {
  1552. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1553. "GNUNET_SERVER_receive_done called while still in processing loop\n");
  1554. return;
  1555. }
  1556. if ((NULL == client->server) || (GNUNET_YES == client->shutdown_now))
  1557. {
  1558. GNUNET_SERVER_client_disconnect (client);
  1559. return;
  1560. }
  1561. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1562. "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
  1563. GNUNET_assert (NULL == client->restart_task);
  1564. client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing,
  1565. client);
  1566. }
  1567. /* end of server.c */