gnunet-service-fs_lc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/gnunet-service-fs_lc.c
  19. * @brief API to handle 'local clients'
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet-service-fs.h"
  24. #include "gnunet-service-fs_lc.h"
  25. #include "gnunet-service-fs_cp.h"
  26. #include "gnunet-service-fs_pr.h"
  27. /**
  28. * Doubly-linked list of requests we are performing
  29. * on behalf of the same client.
  30. */
  31. struct ClientRequest
  32. {
  33. /**
  34. * This is a doubly-linked list.
  35. */
  36. struct ClientRequest *next;
  37. /**
  38. * This is a doubly-linked list.
  39. */
  40. struct ClientRequest *prev;
  41. /**
  42. * Request this entry represents.
  43. */
  44. struct GSF_PendingRequest *pr;
  45. /**
  46. * Client list this request belongs to.
  47. */
  48. struct GSF_LocalClient *lc;
  49. /**
  50. * Task scheduled to destroy the request.
  51. */
  52. GNUNET_SCHEDULER_TaskIdentifier kill_task;
  53. };
  54. /**
  55. * Replies to be transmitted to the client. The actual
  56. * response message is allocated after this struct.
  57. */
  58. struct ClientResponse
  59. {
  60. /**
  61. * This is a doubly-linked list.
  62. */
  63. struct ClientResponse *next;
  64. /**
  65. * This is a doubly-linked list.
  66. */
  67. struct ClientResponse *prev;
  68. /**
  69. * Client list entry this response belongs to.
  70. */
  71. struct GSF_LocalClient *lc;
  72. /**
  73. * Number of bytes in the response.
  74. */
  75. size_t msize;
  76. };
  77. /**
  78. * A local client.
  79. */
  80. struct GSF_LocalClient
  81. {
  82. /**
  83. * We keep clients in a DLL.
  84. */
  85. struct GSF_LocalClient *next;
  86. /**
  87. * We keep clients in a DLL.
  88. */
  89. struct GSF_LocalClient *prev;
  90. /**
  91. * ID of the client.
  92. */
  93. struct GNUNET_SERVER_Client *client;
  94. /**
  95. * Head of list of requests performed on behalf
  96. * of this client right now.
  97. */
  98. struct ClientRequest *cr_head;
  99. /**
  100. * Tail of list of requests performed on behalf
  101. * of this client right now.
  102. */
  103. struct ClientRequest *cr_tail;
  104. /**
  105. * Head of linked list of responses.
  106. */
  107. struct ClientResponse *res_head;
  108. /**
  109. * Tail of linked list of responses.
  110. */
  111. struct ClientResponse *res_tail;
  112. /**
  113. * Context for sending replies.
  114. */
  115. struct GNUNET_SERVER_TransmitHandle *th;
  116. };
  117. /**
  118. * Head of linked list of our local clients.
  119. */
  120. static struct GSF_LocalClient *client_head;
  121. /**
  122. * Head of linked list of our local clients.
  123. */
  124. static struct GSF_LocalClient *client_tail;
  125. /**
  126. * Look up a local client record or create one if it
  127. * doesn't exist yet.
  128. *
  129. * @param client handle of the client
  130. * @return handle to local client entry
  131. */
  132. struct GSF_LocalClient *
  133. GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
  134. {
  135. struct GSF_LocalClient *pos;
  136. pos = client_head;
  137. while ((NULL != pos) && (pos->client != client))
  138. pos = pos->next;
  139. if (NULL != pos)
  140. return pos;
  141. pos = GNUNET_new (struct GSF_LocalClient);
  142. pos->client = client;
  143. GNUNET_CONTAINER_DLL_insert (client_head, client_tail, pos);
  144. return pos;
  145. }
  146. /**
  147. * Free the given client request.
  148. *
  149. * @param cls the client request to free
  150. * @param tc task context
  151. */
  152. static void
  153. client_request_destroy (void *cls,
  154. const struct GNUNET_SCHEDULER_TaskContext *tc)
  155. {
  156. struct ClientRequest *cr = cls;
  157. struct GSF_LocalClient *lc;
  158. cr->kill_task = GNUNET_SCHEDULER_NO_TASK;
  159. lc = cr->lc;
  160. GNUNET_CONTAINER_DLL_remove (lc->cr_head, lc->cr_tail, cr);
  161. GSF_pending_request_cancel_ (cr->pr, GNUNET_YES);
  162. GNUNET_STATISTICS_update (GSF_stats,
  163. gettext_noop ("# client searches active"), -1,
  164. GNUNET_NO);
  165. GNUNET_free (cr);
  166. }
  167. /**
  168. * Handle a reply to a pending request. Also called if a request
  169. * expires (then with data == NULL). The handler may be called
  170. * many times (depending on the request type), but will not be
  171. * called during or after a call to GSF_pending_request_cancel
  172. * and will also not be called anymore after a call signalling
  173. * expiration.
  174. *
  175. * @param cls user-specified closure
  176. * @param eval evaluation of the result
  177. * @param pr handle to the original pending request
  178. * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
  179. * @param expiration when does 'data' expire?
  180. * @param last_transmission when was the last time we've tried to download this block? (FOREVER if unknown)
  181. * @param type type of the block
  182. * @param data response data, NULL on request expiration
  183. * @param data_len number of bytes in data
  184. */
  185. static void
  186. client_response_handler (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
  187. struct GSF_PendingRequest *pr,
  188. uint32_t reply_anonymity_level,
  189. struct GNUNET_TIME_Absolute expiration,
  190. struct GNUNET_TIME_Absolute last_transmission,
  191. enum GNUNET_BLOCK_Type type, const void *data,
  192. size_t data_len)
  193. {
  194. struct ClientRequest *cr = cls;
  195. struct GSF_LocalClient *lc;
  196. struct ClientPutMessage *pm;
  197. const struct GSF_PendingRequestData *prd;
  198. size_t msize;
  199. if (NULL == data)
  200. {
  201. /* ugh, request 'timed out' -- how can this be? */
  202. GNUNET_break (0);
  203. return;
  204. }
  205. prd = GSF_pending_request_get_data_ (pr);
  206. GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
  207. if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
  208. {
  209. GNUNET_break (0);
  210. return;
  211. }
  212. GNUNET_STATISTICS_update (GSF_stats,
  213. gettext_noop
  214. ("# replies received for local clients"), 1,
  215. GNUNET_NO);
  216. GNUNET_assert (pr == cr->pr);
  217. lc = cr->lc;
  218. msize = sizeof (struct ClientPutMessage) + data_len;
  219. {
  220. char buf[msize] GNUNET_ALIGN;
  221. pm = (struct ClientPutMessage *) buf;
  222. pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
  223. pm->header.size = htons (msize);
  224. pm->type = htonl (type);
  225. pm->expiration = GNUNET_TIME_absolute_hton (expiration);
  226. pm->last_transmission = GNUNET_TIME_absolute_hton (last_transmission);
  227. pm->num_transmissions = htonl (prd->num_transmissions);
  228. pm->respect_offered = htonl (prd->respect_offered);
  229. memcpy (&pm[1], data, data_len);
  230. GSF_local_client_transmit_ (lc, &pm->header);
  231. }
  232. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  233. "Queued reply to query `%s' for local client\n",
  234. GNUNET_h2s (&prd->query), (unsigned int) prd->type);
  235. if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
  236. {
  237. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  238. "Evaluation %d - keeping query alive\n",
  239. (int) eval);
  240. return;
  241. }
  242. if (GNUNET_SCHEDULER_NO_TASK == cr->kill_task)
  243. cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy, cr);
  244. }
  245. /**
  246. * Handle START_SEARCH-message (search request from local client).
  247. * Only responsible for creating the request entry itself and setting
  248. * up reply callback and cancellation on client disconnect. Does NOT
  249. * execute the actual request strategy (planning).
  250. *
  251. * @param client identification of the client
  252. * @param message the actual message
  253. * @param prptr where to store the pending request handle for the request
  254. * @return #GNUNET_YES to start local processing,
  255. * #GNUNET_NO to not (yet) start local processing,
  256. * #GNUNET_SYSERR on error
  257. */
  258. int
  259. GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
  260. const struct GNUNET_MessageHeader *message,
  261. struct GSF_PendingRequest **prptr)
  262. {
  263. static struct GNUNET_PeerIdentity all_zeros;
  264. const struct SearchMessage *sm;
  265. struct GSF_LocalClient *lc;
  266. struct ClientRequest *cr;
  267. struct GSF_PendingRequestData *prd;
  268. uint16_t msize;
  269. unsigned int sc;
  270. enum GNUNET_BLOCK_Type type;
  271. enum GSF_PendingRequestOptions options;
  272. msize = ntohs (message->size);
  273. if ((msize < sizeof (struct SearchMessage)) ||
  274. (0 != (msize - sizeof (struct SearchMessage)) % sizeof (struct GNUNET_HashCode)))
  275. {
  276. GNUNET_break (0);
  277. *prptr = NULL;
  278. return GNUNET_SYSERR;
  279. }
  280. GNUNET_STATISTICS_update (GSF_stats,
  281. gettext_noop ("# client searches received"), 1,
  282. GNUNET_NO);
  283. sc = (msize - sizeof (struct SearchMessage)) / sizeof (struct GNUNET_HashCode);
  284. sm = (const struct SearchMessage *) message;
  285. type = ntohl (sm->type);
  286. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  287. "Received request for `%s' of type %u from local client\n",
  288. GNUNET_h2s (&sm->query), (unsigned int) type);
  289. lc = GSF_local_client_lookup_ (client);
  290. cr = NULL;
  291. /* detect duplicate UBLOCK requests */
  292. if ((type == GNUNET_BLOCK_TYPE_FS_UBLOCK) ||
  293. (type == GNUNET_BLOCK_TYPE_ANY))
  294. {
  295. cr = lc->cr_head;
  296. while (NULL != cr)
  297. {
  298. prd = GSF_pending_request_get_data_ (cr->pr);
  299. /* only unify with queries that hae not yet started local processing
  300. (SEARCH_MESSAGE_OPTION_CONTINUED was always set) and that have a
  301. matching query and type */
  302. if ((GNUNET_YES != prd->has_started) &&
  303. (0 != memcmp (&prd->query, &sm->query, sizeof (struct GNUNET_HashCode))) &&
  304. (prd->type == type))
  305. break;
  306. cr = cr->next;
  307. }
  308. }
  309. if (NULL != cr)
  310. {
  311. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  312. "Have existing request, merging content-seen lists.\n");
  313. GSF_pending_request_update_ (cr->pr, (const struct GNUNET_HashCode *) &sm[1], sc);
  314. GNUNET_STATISTICS_update (GSF_stats,
  315. gettext_noop
  316. ("# client searches updated (merged content seen list)"),
  317. 1, GNUNET_NO);
  318. }
  319. else
  320. {
  321. GNUNET_STATISTICS_update (GSF_stats,
  322. gettext_noop ("# client searches active"), 1,
  323. GNUNET_NO);
  324. cr = GNUNET_new (struct ClientRequest);
  325. cr->lc = lc;
  326. GNUNET_CONTAINER_DLL_insert (lc->cr_head, lc->cr_tail, cr);
  327. options = GSF_PRO_LOCAL_REQUEST;
  328. if (0 != (SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY & ntohl (sm->options)))
  329. options |= GSF_PRO_LOCAL_ONLY;
  330. cr->pr = GSF_pending_request_create_ (options, type,
  331. &sm->query,
  332. (0 !=
  333. memcmp (&sm->target, &all_zeros,
  334. sizeof (struct GNUNET_PeerIdentity)))
  335. ? &sm->target : NULL, NULL, 0,
  336. 0 /* bf */ ,
  337. ntohl (sm->anonymity_level),
  338. 0 /* priority */ ,
  339. 0 /* ttl */ ,
  340. 0 /* sender PID */ ,
  341. 0 /* origin PID */ ,
  342. (const struct GNUNET_HashCode *) &sm[1], sc,
  343. &client_response_handler, cr);
  344. }
  345. *prptr = cr->pr;
  346. return (0 !=
  347. (SEARCH_MESSAGE_OPTION_CONTINUED & ntohl (sm->options))) ? GNUNET_NO :
  348. GNUNET_YES;
  349. }
  350. /**
  351. * Transmit the given message by copying it to the target buffer
  352. * "buf". "buf" will be NULL and "size" zero if the socket was closed
  353. * for writing in the meantime. In that case, do nothing
  354. * (the disconnect or shutdown handler will take care of the rest).
  355. * If we were able to transmit messages and there are still more
  356. * pending, ask core again for further calls to this function.
  357. *
  358. * @param cls closure, pointer to the 'struct GSF_LocalClient'
  359. * @param size number of bytes available in buf
  360. * @param buf where the callee should write the message
  361. * @return number of bytes written to buf
  362. */
  363. static size_t
  364. transmit_to_client (void *cls, size_t size, void *buf)
  365. {
  366. struct GSF_LocalClient *lc = cls;
  367. char *cbuf = buf;
  368. struct ClientResponse *res;
  369. size_t msize;
  370. lc->th = NULL;
  371. if (NULL == buf)
  372. return 0;
  373. msize = 0;
  374. while ((NULL != (res = lc->res_head)) && (res->msize <= size))
  375. {
  376. memcpy (&cbuf[msize], &res[1], res->msize);
  377. msize += res->msize;
  378. size -= res->msize;
  379. GNUNET_CONTAINER_DLL_remove (lc->res_head, lc->res_tail, res);
  380. GNUNET_free (res);
  381. }
  382. if (NULL != res)
  383. lc->th =
  384. GNUNET_SERVER_notify_transmit_ready (lc->client, res->msize,
  385. GNUNET_TIME_UNIT_FOREVER_REL,
  386. &transmit_to_client, lc);
  387. return msize;
  388. }
  389. /**
  390. * Transmit a message to the given local client as soon as possible.
  391. * If the client disconnects before transmission, the message is
  392. * simply discarded.
  393. *
  394. * @param lc recipient
  395. * @param msg message to transmit to client
  396. */
  397. void
  398. GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
  399. const struct GNUNET_MessageHeader *msg)
  400. {
  401. struct ClientResponse *res;
  402. size_t msize;
  403. msize = ntohs (msg->size);
  404. res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
  405. res->lc = lc;
  406. res->msize = msize;
  407. memcpy (&res[1], msg, msize);
  408. GNUNET_CONTAINER_DLL_insert_tail (lc->res_head, lc->res_tail, res);
  409. if (NULL == lc->th)
  410. lc->th =
  411. GNUNET_SERVER_notify_transmit_ready (lc->client, msize,
  412. GNUNET_TIME_UNIT_FOREVER_REL,
  413. &transmit_to_client, lc);
  414. }
  415. /**
  416. * A client disconnected from us. Tear down the local client
  417. * record.
  418. *
  419. * @param cls unused
  420. * @param client handle of the client
  421. */
  422. void
  423. GSF_client_disconnect_handler_ (void *cls, struct GNUNET_SERVER_Client *client)
  424. {
  425. struct GSF_LocalClient *pos;
  426. struct ClientRequest *cr;
  427. struct ClientResponse *res;
  428. pos = client_head;
  429. while ((pos != NULL) && (pos->client != client))
  430. pos = pos->next;
  431. if (NULL == pos)
  432. return;
  433. while (NULL != (cr = pos->cr_head))
  434. {
  435. if (GNUNET_SCHEDULER_NO_TASK != cr->kill_task)
  436. GNUNET_SCHEDULER_cancel (cr->kill_task);
  437. client_request_destroy (cr, NULL);
  438. }
  439. while (NULL != (res = pos->res_head))
  440. {
  441. GNUNET_CONTAINER_DLL_remove (pos->res_head, pos->res_tail, res);
  442. GNUNET_free (res);
  443. }
  444. if (NULL != pos->th)
  445. {
  446. GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
  447. pos->th = NULL;
  448. }
  449. GSF_handle_local_client_disconnect_ (pos);
  450. GNUNET_CONTAINER_DLL_remove (client_head, client_tail, pos);
  451. GNUNET_free (pos);
  452. }
  453. /* end of gnunet-service-fs_lc.c */