gnunet-service-fs_lc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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_CONNECTION_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 ( (pos != NULL) &&
  138. (pos->client != client) )
  139. pos = pos->next;
  140. if (pos != NULL)
  141. return pos;
  142. pos = GNUNET_malloc (sizeof (struct GSF_LocalClient));
  143. pos->client = client;
  144. GNUNET_CONTAINER_DLL_insert (client_head,
  145. client_tail,
  146. pos);
  147. return pos;
  148. }
  149. /**
  150. * Free the given client request.
  151. *
  152. * @param cls the client request to free
  153. * @param tc task context
  154. */
  155. static void
  156. client_request_destroy (void *cls,
  157. const struct GNUNET_SCHEDULER_TaskContext *tc)
  158. {
  159. struct ClientRequest *cr = cls;
  160. struct GSF_LocalClient *lc;
  161. cr->kill_task = GNUNET_SCHEDULER_NO_TASK;
  162. lc = cr->lc;
  163. GNUNET_CONTAINER_DLL_remove (lc->cr_head,
  164. lc->cr_tail,
  165. cr);
  166. GSF_pending_request_cancel_ (cr->pr, GNUNET_NO);
  167. GNUNET_STATISTICS_update (GSF_stats,
  168. gettext_noop ("# client searches active"),
  169. - 1,
  170. GNUNET_NO);
  171. GNUNET_free (cr);
  172. }
  173. /**
  174. * Handle a reply to a pending request. Also called if a request
  175. * expires (then with data == NULL). The handler may be called
  176. * many times (depending on the request type), but will not be
  177. * called during or after a call to GSF_pending_request_cancel
  178. * and will also not be called anymore after a call signalling
  179. * expiration.
  180. *
  181. * @param cls user-specified closure
  182. * @param eval evaluation of the result
  183. * @param pr handle to the original pending request
  184. * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
  185. * @param expiration when does 'data' expire?
  186. * @param type type of the block
  187. * @param data response data, NULL on request expiration
  188. * @param data_len number of bytes in data
  189. */
  190. static void
  191. client_response_handler (void *cls,
  192. enum GNUNET_BLOCK_EvaluationResult eval,
  193. struct GSF_PendingRequest *pr,
  194. uint32_t reply_anonymity_level,
  195. struct GNUNET_TIME_Absolute expiration,
  196. enum GNUNET_BLOCK_Type type,
  197. const void *data,
  198. size_t data_len)
  199. {
  200. struct ClientRequest *cr = cls;
  201. struct GSF_LocalClient *lc;
  202. struct PutMessage *pm;
  203. const struct GSF_PendingRequestData *prd;
  204. size_t msize;
  205. if (NULL == data)
  206. {
  207. /* ugh, request 'timed out' -- how can this be? */
  208. GNUNET_break (0);
  209. return;
  210. }
  211. prd = GSF_pending_request_get_data_ (pr);
  212. GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
  213. if ( (prd->type != type) &&
  214. (prd->type != GNUNET_BLOCK_TYPE_ANY) )
  215. {
  216. GNUNET_break (0);
  217. return;
  218. }
  219. GNUNET_STATISTICS_update (GSF_stats,
  220. gettext_noop ("# replies received for local clients"),
  221. 1,
  222. GNUNET_NO);
  223. GNUNET_assert (pr == cr->pr);
  224. lc = cr->lc;
  225. msize = sizeof (struct PutMessage) + data_len;
  226. {
  227. char buf[msize];
  228. pm = (struct PutMessage*) buf;
  229. pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
  230. pm->header.size = htons (msize);
  231. pm->type = htonl (type);
  232. pm->expiration = GNUNET_TIME_absolute_hton (expiration);
  233. memcpy (&pm[1], data, data_len);
  234. GSF_local_client_transmit_ (lc, &pm->header);
  235. }
  236. #if DEBUG_FS_CLIENT
  237. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  238. "Queued reply to query `%s' for local client\n",
  239. GNUNET_h2s (&prd->query),
  240. (unsigned int) prd->type);
  241. #endif
  242. if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
  243. return;
  244. cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
  245. cr);
  246. }
  247. /**
  248. * Handle START_SEARCH-message (search request from local client).
  249. *
  250. * @param client identification of the client
  251. * @param message the actual message
  252. * @return pending request handle for the request, NULL on error
  253. */
  254. struct GSF_PendingRequest *
  255. GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
  256. const struct GNUNET_MessageHeader *message)
  257. {
  258. static GNUNET_HashCode all_zeros;
  259. const struct SearchMessage *sm;
  260. struct GSF_LocalClient *lc;
  261. struct ClientRequest *cr;
  262. struct GSF_PendingRequestData *prd;
  263. uint16_t msize;
  264. unsigned int sc;
  265. enum GNUNET_BLOCK_Type type;
  266. enum GSF_PendingRequestOptions options;
  267. msize = ntohs (message->size);
  268. if ( (msize < sizeof (struct SearchMessage)) ||
  269. (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
  270. {
  271. GNUNET_break (0);
  272. GNUNET_SERVER_receive_done (client,
  273. GNUNET_SYSERR);
  274. return NULL;
  275. }
  276. GNUNET_STATISTICS_update (GSF_stats,
  277. gettext_noop ("# client searches received"),
  278. 1,
  279. GNUNET_NO);
  280. sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
  281. sm = (const struct SearchMessage*) message;
  282. type = ntohl (sm->type);
  283. #if DEBUG_FS_CLIENT
  284. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  285. "Received request for `%s' of type %u from local client\n",
  286. GNUNET_h2s (&sm->query),
  287. (unsigned int) type);
  288. #endif
  289. lc = GSF_local_client_lookup_ (client);
  290. /* detect duplicate KBLOCK requests */
  291. if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
  292. (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
  293. (type == GNUNET_BLOCK_TYPE_ANY) )
  294. {
  295. /* FIXME: this does currently not work to filter duplicate
  296. results from *local* datastore since the local store is
  297. queried before we continue to process additional
  298. messages from the client! -- fix protocol? */
  299. cr = lc->cr_head;
  300. while (cr != NULL)
  301. {
  302. prd = GSF_pending_request_get_data_ (cr->pr);
  303. if ( (0 != memcmp (&prd->query,
  304. &sm->query,
  305. sizeof (GNUNET_HashCode))) &&
  306. (prd->type == type) )
  307. break;
  308. cr = cr->next;
  309. }
  310. if (cr != NULL)
  311. {
  312. #if DEBUG_FS_CLIENT
  313. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  314. "Have existing request, merging content-seen lists.\n");
  315. #endif
  316. GSF_pending_request_update_ (cr->pr,
  317. (const GNUNET_HashCode*) &sm[1],
  318. sc);
  319. GNUNET_STATISTICS_update (GSF_stats,
  320. gettext_noop ("# client searches updated (merged content seen list)"),
  321. 1,
  322. GNUNET_NO);
  323. GNUNET_SERVER_receive_done (client,
  324. GNUNET_OK);
  325. return NULL;
  326. }
  327. }
  328. GNUNET_STATISTICS_update (GSF_stats,
  329. gettext_noop ("# client searches active"),
  330. 1,
  331. GNUNET_NO);
  332. cr = GNUNET_malloc (sizeof (struct ClientRequest));
  333. cr->lc = lc;
  334. GNUNET_CONTAINER_DLL_insert (lc->cr_head,
  335. lc->cr_tail,
  336. cr);
  337. options = GSF_PRO_LOCAL_REQUEST;
  338. if (0 != (1 & ntohl (sm->options)))
  339. options |= GSF_PRO_LOCAL_ONLY;
  340. cr->pr = GSF_pending_request_create_ (options,
  341. type,
  342. &sm->query,
  343. (type == GNUNET_BLOCK_TYPE_FS_SBLOCK)
  344. ? &sm->target /* namespace */
  345. : NULL,
  346. (0 != memcmp (&sm->target,
  347. &all_zeros,
  348. sizeof (GNUNET_HashCode)))
  349. ? (const struct GNUNET_PeerIdentity*) &sm->target
  350. : NULL,
  351. NULL, 0, 0 /* bf */,
  352. ntohl (sm->anonymity_level),
  353. 0 /* priority */,
  354. 0 /* ttl */,
  355. 0 /* sender PID */,
  356. (const GNUNET_HashCode*) &sm[1], sc,
  357. &client_response_handler,
  358. cr);
  359. return cr->pr;
  360. }
  361. /**
  362. * Transmit the given message by copying it to the target buffer
  363. * "buf". "buf" will be NULL and "size" zero if the socket was closed
  364. * for writing in the meantime. In that case, do nothing
  365. * (the disconnect or shutdown handler will take care of the rest).
  366. * If we were able to transmit messages and there are still more
  367. * pending, ask core again for further calls to this function.
  368. *
  369. * @param cls closure, pointer to the 'struct GSF_LocalClient'
  370. * @param size number of bytes available in buf
  371. * @param buf where the callee should write the message
  372. * @return number of bytes written to buf
  373. */
  374. static size_t
  375. transmit_to_client (void *cls,
  376. size_t size,
  377. void *buf)
  378. {
  379. struct GSF_LocalClient *lc = cls;
  380. char *cbuf = buf;
  381. struct ClientResponse *res;
  382. size_t msize;
  383. lc->th = NULL;
  384. if (NULL == buf)
  385. return 0;
  386. msize = 0;
  387. while ( (NULL != (res = lc->res_head) ) &&
  388. (res->msize <= size) )
  389. {
  390. memcpy (&cbuf[msize], &res[1], res->msize);
  391. msize += res->msize;
  392. size -= res->msize;
  393. GNUNET_CONTAINER_DLL_remove (lc->res_head,
  394. lc->res_tail,
  395. res);
  396. GNUNET_free (res);
  397. }
  398. if (NULL != res)
  399. lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
  400. res->msize,
  401. GNUNET_TIME_UNIT_FOREVER_REL,
  402. &transmit_to_client,
  403. lc);
  404. return msize;
  405. }
  406. /**
  407. * Transmit a message to the given local client as soon as possible.
  408. * If the client disconnects before transmission, the message is
  409. * simply discarded.
  410. *
  411. * @param lc recipient
  412. * @param msg message to transmit to client
  413. */
  414. void
  415. GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
  416. const struct GNUNET_MessageHeader *msg)
  417. {
  418. struct ClientResponse *res;
  419. size_t msize;
  420. msize = ntohs (msg->size);
  421. res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
  422. res->lc = lc;
  423. res->msize = msize;
  424. memcpy (&res[1], msg, msize);
  425. GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
  426. lc->res_tail,
  427. res);
  428. if (NULL == lc->th)
  429. lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
  430. msize,
  431. GNUNET_TIME_UNIT_FOREVER_REL,
  432. &transmit_to_client,
  433. lc);
  434. }
  435. /**
  436. * A client disconnected from us. Tear down the local client
  437. * record.
  438. *
  439. * @param cls unused
  440. * @param client handle of the client
  441. */
  442. void
  443. GSF_client_disconnect_handler_ (void *cls,
  444. struct GNUNET_SERVER_Client *client)
  445. {
  446. struct GSF_LocalClient *pos;
  447. struct ClientRequest *cr;
  448. struct ClientResponse *res;
  449. pos = client_head;
  450. while ( (pos != NULL) &&
  451. (pos->client != client) )
  452. pos = pos->next;
  453. if (pos == NULL)
  454. return;
  455. while (NULL != (cr = pos->cr_head))
  456. {
  457. GNUNET_CONTAINER_DLL_remove (pos->cr_head,
  458. pos->cr_tail,
  459. cr);
  460. GSF_pending_request_cancel_ (cr->pr, GNUNET_NO);
  461. GNUNET_STATISTICS_update (GSF_stats,
  462. gettext_noop ("# client searches active"),
  463. - 1,
  464. GNUNET_NO);
  465. if (GNUNET_SCHEDULER_NO_TASK != cr->kill_task)
  466. GNUNET_SCHEDULER_cancel (cr->kill_task);
  467. GNUNET_free (cr);
  468. }
  469. while (NULL != (res = pos->res_head))
  470. {
  471. GNUNET_CONTAINER_DLL_remove (pos->res_head,
  472. pos->res_tail,
  473. res);
  474. GNUNET_free (res);
  475. }
  476. if (pos->th != NULL)
  477. {
  478. GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
  479. pos->th = NULL;
  480. }
  481. GSF_handle_local_client_disconnect_ (pos);
  482. GNUNET_CONTAINER_DLL_remove (client_head,
  483. client_tail,
  484. pos);
  485. GNUNET_free (pos);
  486. }
  487. /* end of gnunet-service-fs_lc.c */