gnunet-service-dht_clients.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2011, 2016, 2017 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 dht/gnunet-service-dht_clients.c
  18. * @brief GNUnet DHT service's client management code
  19. * @author Christian Grothoff
  20. * @author Nathan Evans
  21. */
  22. #include "platform.h"
  23. #include "gnunet_constants.h"
  24. #include "gnunet_protocols.h"
  25. #include "gnunet_statistics_service.h"
  26. #include "gnunet-service-dht.h"
  27. #include "gnunet-service-dht_datacache.h"
  28. #include "gnunet-service-dht_neighbours.h"
  29. #include "dht.h"
  30. /**
  31. * Should routing details be logged to stderr (for debugging)?
  32. */
  33. #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
  34. #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
  35. /**
  36. * Struct containing information about a client,
  37. * handle to connect to it, and any pending messages
  38. * that need to be sent to it.
  39. */
  40. struct ClientHandle;
  41. /**
  42. * Entry in the local forwarding map for a client's GET request.
  43. */
  44. struct ClientQueryRecord
  45. {
  46. /**
  47. * The key this request was about
  48. */
  49. struct GNUNET_HashCode key;
  50. /**
  51. * Kept in a DLL with @e client.
  52. */
  53. struct ClientQueryRecord *next;
  54. /**
  55. * Kept in a DLL with @e client.
  56. */
  57. struct ClientQueryRecord *prev;
  58. /**
  59. * Client responsible for the request.
  60. */
  61. struct ClientHandle *ch;
  62. /**
  63. * Extended query (see gnunet_block_lib.h), allocated at the end of this struct.
  64. */
  65. const void *xquery;
  66. /**
  67. * Replies we have already seen for this request.
  68. */
  69. struct GNUNET_HashCode *seen_replies;
  70. /**
  71. * Pointer to this nodes heap location in the retry-heap (for fast removal)
  72. */
  73. struct GNUNET_CONTAINER_HeapNode *hnode;
  74. /**
  75. * What's the delay between re-try operations that we currently use for this
  76. * request?
  77. */
  78. struct GNUNET_TIME_Relative retry_frequency;
  79. /**
  80. * What's the next time we should re-try this request?
  81. */
  82. struct GNUNET_TIME_Absolute retry_time;
  83. /**
  84. * The unique identifier of this request
  85. */
  86. uint64_t unique_id;
  87. /**
  88. * Number of bytes in xquery.
  89. */
  90. size_t xquery_size;
  91. /**
  92. * Number of entries in 'seen_replies'.
  93. */
  94. unsigned int seen_replies_count;
  95. /**
  96. * Desired replication level
  97. */
  98. uint32_t replication;
  99. /**
  100. * Any message options for this request
  101. */
  102. uint32_t msg_options;
  103. /**
  104. * The type for the data for the GET request.
  105. */
  106. enum GNUNET_BLOCK_Type type;
  107. };
  108. /**
  109. * Struct containing paremeters of monitoring requests.
  110. */
  111. struct ClientMonitorRecord
  112. {
  113. /**
  114. * Next element in DLL.
  115. */
  116. struct ClientMonitorRecord *next;
  117. /**
  118. * Previous element in DLL.
  119. */
  120. struct ClientMonitorRecord *prev;
  121. /**
  122. * Type of blocks that are of interest
  123. */
  124. enum GNUNET_BLOCK_Type type;
  125. /**
  126. * Key of data of interest, NULL for all.
  127. */
  128. struct GNUNET_HashCode *key;
  129. /**
  130. * Flag whether to notify about GET messages.
  131. */
  132. int16_t get;
  133. /**
  134. * Flag whether to notify about GET_REPONSE messages.
  135. */
  136. int16_t get_resp;
  137. /**
  138. * Flag whether to notify about PUT messages.
  139. */
  140. uint16_t put;
  141. /**
  142. * Client to notify of these requests.
  143. */
  144. struct ClientHandle *ch;
  145. };
  146. /**
  147. * Struct containing information about a client,
  148. * handle to connect to it, and any pending messages
  149. * that need to be sent to it.
  150. */
  151. struct ClientHandle
  152. {
  153. /**
  154. * Linked list of active queries of this client.
  155. */
  156. struct ClientQueryRecord *cqr_head;
  157. /**
  158. * Linked list of active queries of this client.
  159. */
  160. struct ClientQueryRecord *cqr_tail;
  161. /**
  162. * The handle to this client
  163. */
  164. struct GNUNET_SERVICE_Client *client;
  165. /**
  166. * The message queue to this client
  167. */
  168. struct GNUNET_MQ_Handle *mq;
  169. };
  170. /**
  171. * Our handle to the BLOCK library.
  172. */
  173. struct GNUNET_BLOCK_Context *GDS_block_context;
  174. /**
  175. * Handle for the statistics service.
  176. */
  177. struct GNUNET_STATISTICS_Handle *GDS_stats;
  178. /**
  179. * Handle for the service.
  180. */
  181. struct GNUNET_SERVICE_Handle *GDS_service;
  182. /**
  183. * The configuration the DHT service is running with
  184. */
  185. const struct GNUNET_CONFIGURATION_Handle *GDS_cfg;
  186. /**
  187. * List of active monitoring requests.
  188. */
  189. static struct ClientMonitorRecord *monitor_head;
  190. /**
  191. * List of active monitoring requests.
  192. */
  193. static struct ClientMonitorRecord *monitor_tail;
  194. /**
  195. * Hashmap for fast key based lookup, maps keys to `struct ClientQueryRecord` entries.
  196. */
  197. static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
  198. /**
  199. * Heap with all of our client's request, sorted by retry time (earliest on top).
  200. */
  201. static struct GNUNET_CONTAINER_Heap *retry_heap;
  202. /**
  203. * Task that re-transmits requests (using retry_heap).
  204. */
  205. static struct GNUNET_SCHEDULER_Task *retry_task;
  206. /**
  207. * Free data structures associated with the given query.
  208. *
  209. * @param record record to remove
  210. */
  211. static void
  212. remove_client_record (struct ClientQueryRecord *record)
  213. {
  214. struct ClientHandle *ch = record->ch;
  215. GNUNET_CONTAINER_DLL_remove (ch->cqr_head,
  216. ch->cqr_tail,
  217. record);
  218. GNUNET_assert (GNUNET_YES ==
  219. GNUNET_CONTAINER_multihashmap_remove (forward_map,
  220. &record->key,
  221. record));
  222. if (NULL != record->hnode)
  223. GNUNET_CONTAINER_heap_remove_node (record->hnode);
  224. GNUNET_array_grow (record->seen_replies,
  225. record->seen_replies_count,
  226. 0);
  227. GNUNET_free (record);
  228. }
  229. /**
  230. * Functions with this signature are called whenever a local client is
  231. * connects to us.
  232. *
  233. * @param cls closure (NULL for dht)
  234. * @param client identification of the client
  235. * @param mq message queue for talking to @a client
  236. * @return our `struct ClientHandle` for @a client
  237. */
  238. static void *
  239. client_connect_cb (void *cls,
  240. struct GNUNET_SERVICE_Client *client,
  241. struct GNUNET_MQ_Handle *mq)
  242. {
  243. struct ClientHandle *ch;
  244. ch = GNUNET_new (struct ClientHandle);
  245. ch->client = client;
  246. ch->mq = mq;
  247. return ch;
  248. }
  249. /**
  250. * Functions with this signature are called whenever a client
  251. * is disconnected on the network level.
  252. *
  253. * @param cls closure (NULL for dht)
  254. * @param client identification of the client
  255. * @param app_ctx our `struct ClientHandle` for @a client
  256. */
  257. static void
  258. client_disconnect_cb (void *cls,
  259. struct GNUNET_SERVICE_Client *client,
  260. void *app_ctx)
  261. {
  262. struct ClientHandle *ch = app_ctx;
  263. struct ClientQueryRecord *cqr;
  264. struct ClientMonitorRecord *monitor;
  265. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  266. "Local client %p disconnects\n",
  267. ch);
  268. monitor = monitor_head;
  269. while (NULL != monitor)
  270. {
  271. if (monitor->ch == ch)
  272. {
  273. struct ClientMonitorRecord *next;
  274. next = monitor->next;
  275. GNUNET_free_non_null (monitor->key);
  276. GNUNET_CONTAINER_DLL_remove (monitor_head,
  277. monitor_tail,
  278. monitor);
  279. GNUNET_free (monitor);
  280. monitor = next;
  281. }
  282. else
  283. {
  284. monitor = monitor->next;
  285. }
  286. }
  287. while (NULL != (cqr = ch->cqr_head))
  288. remove_client_record (cqr);
  289. GNUNET_free (ch);
  290. }
  291. /**
  292. * Route the given request via the DHT. This includes updating
  293. * the bloom filter and retransmission times, building the P2P
  294. * message and initiating the routing operation.
  295. */
  296. static void
  297. transmit_request (struct ClientQueryRecord *cqr)
  298. {
  299. struct GNUNET_BLOCK_Group *bg;
  300. struct GNUNET_CONTAINER_BloomFilter *peer_bf;
  301. GNUNET_STATISTICS_update (GDS_stats,
  302. gettext_noop ("# GET requests from clients injected"),
  303. 1,
  304. GNUNET_NO);
  305. bg = GNUNET_BLOCK_group_create (GDS_block_context,
  306. cqr->type,
  307. GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  308. UINT32_MAX),
  309. NULL,
  310. 0,
  311. "seen-set-size",
  312. cqr->seen_replies_count,
  313. NULL);
  314. GNUNET_BLOCK_group_set_seen (bg,
  315. cqr->seen_replies,
  316. cqr->seen_replies_count);
  317. peer_bf
  318. = GNUNET_CONTAINER_bloomfilter_init (NULL,
  319. DHT_BLOOM_SIZE,
  320. GNUNET_CONSTANTS_BLOOMFILTER_K);
  321. LOG (GNUNET_ERROR_TYPE_DEBUG,
  322. "Initiating GET for %s, replication %u, already have %u replies\n",
  323. GNUNET_h2s (&cqr->key),
  324. cqr->replication,
  325. cqr->seen_replies_count);
  326. GDS_NEIGHBOURS_handle_get (cqr->type,
  327. cqr->msg_options,
  328. cqr->replication,
  329. 0 /* hop count */ ,
  330. &cqr->key,
  331. cqr->xquery,
  332. cqr->xquery_size,
  333. bg,
  334. peer_bf);
  335. GNUNET_BLOCK_group_destroy (bg);
  336. GNUNET_CONTAINER_bloomfilter_free (peer_bf);
  337. /* exponential back-off for retries.
  338. * max GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD (15 min) */
  339. cqr->retry_frequency = GNUNET_TIME_STD_BACKOFF (cqr->retry_frequency);
  340. cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
  341. }
  342. /**
  343. * Task that looks at the #retry_heap and transmits all of the requests
  344. * on the heap that are ready for transmission. Then re-schedules
  345. * itself (unless the heap is empty).
  346. *
  347. * @param cls unused
  348. */
  349. static void
  350. transmit_next_request_task (void *cls)
  351. {
  352. struct ClientQueryRecord *cqr;
  353. struct GNUNET_TIME_Relative delay;
  354. retry_task = NULL;
  355. while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
  356. {
  357. cqr->hnode = NULL;
  358. delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
  359. if (delay.rel_value_us > 0)
  360. {
  361. cqr->hnode
  362. = GNUNET_CONTAINER_heap_insert (retry_heap,
  363. cqr,
  364. cqr->retry_time.abs_value_us);
  365. retry_task
  366. = GNUNET_SCHEDULER_add_at (cqr->retry_time,
  367. &transmit_next_request_task,
  368. NULL);
  369. return;
  370. }
  371. transmit_request (cqr);
  372. cqr->hnode
  373. = GNUNET_CONTAINER_heap_insert (retry_heap,
  374. cqr,
  375. cqr->retry_time.abs_value_us);
  376. }
  377. }
  378. /**
  379. * Check DHT PUT messages from the client.
  380. *
  381. * @param cls the client we received this message from
  382. * @param dht_msg the actual message received
  383. * @return #GNUNET_OK (always)
  384. */
  385. static int
  386. check_dht_local_put (void *cls,
  387. const struct GNUNET_DHT_ClientPutMessage *dht_msg)
  388. {
  389. /* always well-formed */
  390. return GNUNET_OK;
  391. }
  392. /**
  393. * Handler for PUT messages.
  394. *
  395. * @param cls the client we received this message from
  396. * @param dht_msg the actual message received
  397. */
  398. static void
  399. handle_dht_local_put (void *cls,
  400. const struct GNUNET_DHT_ClientPutMessage *dht_msg)
  401. {
  402. struct ClientHandle *ch = cls;
  403. struct GNUNET_CONTAINER_BloomFilter *peer_bf;
  404. uint16_t size;
  405. size = ntohs (dht_msg->header.size);
  406. GNUNET_STATISTICS_update (GDS_stats,
  407. gettext_noop ("# PUT requests received from clients"),
  408. 1,
  409. GNUNET_NO);
  410. LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
  411. "CLIENT-PUT %s\n",
  412. GNUNET_h2s_full (&dht_msg->key));
  413. /* give to local clients */
  414. LOG (GNUNET_ERROR_TYPE_DEBUG,
  415. "Handling local PUT of %u-bytes for query %s\n",
  416. size - sizeof (struct GNUNET_DHT_ClientPutMessage),
  417. GNUNET_h2s (&dht_msg->key));
  418. GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
  419. &dht_msg->key,
  420. 0,
  421. NULL,
  422. 0,
  423. NULL,
  424. ntohl (dht_msg->type),
  425. size - sizeof (struct GNUNET_DHT_ClientPutMessage),
  426. &dht_msg[1]);
  427. /* store locally */
  428. GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
  429. &dht_msg->key,
  430. 0,
  431. NULL,
  432. ntohl (dht_msg->type),
  433. size - sizeof (struct GNUNET_DHT_ClientPutMessage),
  434. &dht_msg[1]);
  435. /* route to other peers */
  436. peer_bf
  437. = GNUNET_CONTAINER_bloomfilter_init (NULL,
  438. DHT_BLOOM_SIZE,
  439. GNUNET_CONSTANTS_BLOOMFILTER_K);
  440. GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type),
  441. ntohl (dht_msg->options),
  442. ntohl (dht_msg->desired_replication_level),
  443. GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
  444. 0 /* hop count */,
  445. peer_bf,
  446. &dht_msg->key,
  447. 0,
  448. NULL,
  449. &dht_msg[1],
  450. size - sizeof (struct GNUNET_DHT_ClientPutMessage));
  451. GDS_CLIENTS_process_put (ntohl (dht_msg->options),
  452. ntohl (dht_msg->type),
  453. 0,
  454. ntohl (dht_msg->desired_replication_level),
  455. 1,
  456. GDS_NEIGHBOURS_get_id(),
  457. GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
  458. &dht_msg->key,
  459. &dht_msg[1],
  460. size - sizeof (struct GNUNET_DHT_ClientPutMessage));
  461. GNUNET_CONTAINER_bloomfilter_free (peer_bf);
  462. GNUNET_SERVICE_client_continue (ch->client);
  463. }
  464. /**
  465. * Check DHT GET messages from the client.
  466. *
  467. * @param cls the client we received this message from
  468. * @param message the actual message received
  469. * @return #GNUNET_OK (always)
  470. */
  471. static int
  472. check_dht_local_get (void *cls,
  473. const struct GNUNET_DHT_ClientGetMessage *get)
  474. {
  475. /* always well-formed */
  476. return GNUNET_OK;
  477. }
  478. /**
  479. * Handle a result from local datacache for a GET operation.
  480. *
  481. * @param cls the `struct ClientHandle` of the client doing the query
  482. * @param type type of the block
  483. * @param expiration_time when does the content expire
  484. * @param key key for the content
  485. * @param put_path_length number of entries in @a put_path
  486. * @param put_path peers the original PUT traversed (if tracked)
  487. * @param get_path_length number of entries in @a get_path
  488. * @param get_path peers this reply has traversed so far (if tracked)
  489. * @param data payload of the reply
  490. * @param data_size number of bytes in @a data
  491. */
  492. static void
  493. handle_local_result (void *cls,
  494. enum GNUNET_BLOCK_Type type,
  495. struct GNUNET_TIME_Absolute expiration_time,
  496. const struct GNUNET_HashCode *key,
  497. unsigned int put_path_length,
  498. const struct GNUNET_PeerIdentity *put_path,
  499. unsigned int get_path_length,
  500. const struct GNUNET_PeerIdentity *get_path,
  501. const void *data,
  502. size_t data_size)
  503. {
  504. // FIXME: this needs some clean up: inline the function,
  505. // possibly avoid even looking up the client!
  506. GDS_CLIENTS_handle_reply (expiration_time,
  507. key,
  508. 0, NULL,
  509. put_path_length, put_path,
  510. type,
  511. data_size, data);
  512. }
  513. /**
  514. * Handler for DHT GET messages from the client.
  515. *
  516. * @param cls the client we received this message from
  517. * @param message the actual message received
  518. */
  519. static void
  520. handle_dht_local_get (void *cls,
  521. const struct GNUNET_DHT_ClientGetMessage *get)
  522. {
  523. struct ClientHandle *ch = cls;
  524. struct ClientQueryRecord *cqr;
  525. size_t xquery_size;
  526. const char *xquery;
  527. uint16_t size;
  528. size = ntohs (get->header.size);
  529. xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
  530. xquery = (const char *) &get[1];
  531. GNUNET_STATISTICS_update (GDS_stats,
  532. gettext_noop
  533. ("# GET requests received from clients"), 1,
  534. GNUNET_NO);
  535. LOG (GNUNET_ERROR_TYPE_DEBUG,
  536. "Received GET request for %s from local client %p, xq: %.*s\n",
  537. GNUNET_h2s (&get->key),
  538. ch->client,
  539. xquery_size,
  540. xquery);
  541. LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
  542. "CLIENT-GET %s\n",
  543. GNUNET_h2s_full (&get->key));
  544. cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
  545. cqr->key = get->key;
  546. cqr->ch = ch;
  547. cqr->xquery = (void *) &cqr[1];
  548. GNUNET_memcpy (&cqr[1], xquery, xquery_size);
  549. cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
  550. cqr->retry_frequency = GNUNET_TIME_UNIT_SECONDS;
  551. cqr->retry_time = GNUNET_TIME_absolute_get ();
  552. cqr->unique_id = get->unique_id;
  553. cqr->xquery_size = xquery_size;
  554. cqr->replication = ntohl (get->desired_replication_level);
  555. cqr->msg_options = ntohl (get->options);
  556. cqr->type = ntohl (get->type);
  557. GNUNET_CONTAINER_DLL_insert (ch->cqr_head,
  558. ch->cqr_tail,
  559. cqr);
  560. GNUNET_CONTAINER_multihashmap_put (forward_map,
  561. &cqr->key,
  562. cqr,
  563. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  564. GDS_CLIENTS_process_get (ntohl (get->options),
  565. ntohl (get->type),
  566. 0,
  567. ntohl (get->desired_replication_level),
  568. 1,
  569. GDS_NEIGHBOURS_get_id(),
  570. &get->key);
  571. /* start remote requests */
  572. if (NULL != retry_task)
  573. GNUNET_SCHEDULER_cancel (retry_task);
  574. retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task,
  575. NULL);
  576. /* perform local lookup */
  577. GDS_DATACACHE_handle_get (&get->key,
  578. cqr->type,
  579. cqr->xquery,
  580. xquery_size,
  581. NULL,
  582. &handle_local_result,
  583. ch);
  584. GNUNET_SERVICE_client_continue (ch->client);
  585. }
  586. /**
  587. * Closure for #find_by_unique_id().
  588. */
  589. struct FindByUniqueIdContext
  590. {
  591. /**
  592. * Where to store the result, if found.
  593. */
  594. struct ClientQueryRecord *cqr;
  595. uint64_t unique_id;
  596. };
  597. /**
  598. * Function called for each existing DHT record for the given
  599. * query. Checks if it matches the UID given in the closure
  600. * and if so returns the entry as a result.
  601. *
  602. * @param cls the search context
  603. * @param key query for the lookup (not used)
  604. * @param value the `struct ClientQueryRecord`
  605. * @return #GNUNET_YES to continue iteration (result not yet found)
  606. */
  607. static int
  608. find_by_unique_id (void *cls,
  609. const struct GNUNET_HashCode *key,
  610. void *value)
  611. {
  612. struct FindByUniqueIdContext *fui_ctx = cls;
  613. struct ClientQueryRecord *cqr = value;
  614. if (cqr->unique_id != fui_ctx->unique_id)
  615. return GNUNET_YES;
  616. fui_ctx->cqr = cqr;
  617. return GNUNET_NO;
  618. }
  619. /**
  620. * Check "GET result seen" messages from the client.
  621. *
  622. * @param cls the client we received this message from
  623. * @param message the actual message received
  624. * @return #GNUNET_OK if @a seen is well-formed
  625. */
  626. static int
  627. check_dht_local_get_result_seen (void *cls,
  628. const struct GNUNET_DHT_ClientGetResultSeenMessage *seen)
  629. {
  630. uint16_t size;
  631. unsigned int hash_count;
  632. size = ntohs (seen->header.size);
  633. hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
  634. if (size != sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage) + hash_count * sizeof (struct GNUNET_HashCode))
  635. {
  636. GNUNET_break (0);
  637. return GNUNET_SYSERR;
  638. }
  639. return GNUNET_OK;
  640. }
  641. /**
  642. * Handler for "GET result seen" messages from the client.
  643. *
  644. * @param cls the client we received this message from
  645. * @param message the actual message received
  646. */
  647. static void
  648. handle_dht_local_get_result_seen (void *cls,
  649. const struct GNUNET_DHT_ClientGetResultSeenMessage *seen)
  650. {
  651. struct ClientHandle *ch = cls;
  652. uint16_t size;
  653. unsigned int hash_count;
  654. unsigned int old_count;
  655. const struct GNUNET_HashCode *hc;
  656. struct FindByUniqueIdContext fui_ctx;
  657. struct ClientQueryRecord *cqr;
  658. size = ntohs (seen->header.size);
  659. hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
  660. hc = (const struct GNUNET_HashCode*) &seen[1];
  661. fui_ctx.unique_id = seen->unique_id;
  662. fui_ctx.cqr = NULL;
  663. GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
  664. &seen->key,
  665. &find_by_unique_id,
  666. &fui_ctx);
  667. if (NULL == (cqr = fui_ctx.cqr))
  668. {
  669. GNUNET_break (0);
  670. GNUNET_SERVICE_client_drop (ch->client);
  671. return;
  672. }
  673. /* finally, update 'seen' list */
  674. old_count = cqr->seen_replies_count;
  675. GNUNET_array_grow (cqr->seen_replies,
  676. cqr->seen_replies_count,
  677. cqr->seen_replies_count + hash_count);
  678. GNUNET_memcpy (&cqr->seen_replies[old_count],
  679. hc,
  680. sizeof (struct GNUNET_HashCode) * hash_count);
  681. }
  682. /**
  683. * Closure for #remove_by_unique_id().
  684. */
  685. struct RemoveByUniqueIdContext
  686. {
  687. /**
  688. * Client that issued the removal request.
  689. */
  690. struct ClientHandle *ch;
  691. /**
  692. * Unique ID of the request.
  693. */
  694. uint64_t unique_id;
  695. };
  696. /**
  697. * Iterator over hash map entries that frees all entries
  698. * that match the given client and unique ID.
  699. *
  700. * @param cls unique ID and client to search for in source routes
  701. * @param key current key code
  702. * @param value value in the hash map, a ClientQueryRecord
  703. * @return #GNUNET_YES (we should continue to iterate)
  704. */
  705. static int
  706. remove_by_unique_id (void *cls,
  707. const struct GNUNET_HashCode *key,
  708. void *value)
  709. {
  710. const struct RemoveByUniqueIdContext *ctx = cls;
  711. struct ClientQueryRecord *cqr = value;
  712. if (cqr->unique_id != ctx->unique_id)
  713. return GNUNET_YES;
  714. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  715. "Removing client %p's record for key %s (by unique id)\n",
  716. ctx->ch->client,
  717. GNUNET_h2s (key));
  718. remove_client_record (cqr);
  719. return GNUNET_YES;
  720. }
  721. /**
  722. * Handler for any generic DHT stop messages, calls the appropriate handler
  723. * depending on message type (if processed locally)
  724. *
  725. * @param cls client we received this message from
  726. * @param message the actual message received
  727. *
  728. */
  729. static void
  730. handle_dht_local_get_stop (void *cls,
  731. const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg)
  732. {
  733. struct ClientHandle *ch = cls;
  734. struct RemoveByUniqueIdContext ctx;
  735. GNUNET_STATISTICS_update (GDS_stats,
  736. gettext_noop
  737. ("# GET STOP requests received from clients"), 1,
  738. GNUNET_NO);
  739. LOG (GNUNET_ERROR_TYPE_DEBUG,
  740. "Received GET STOP request for %s from local client %p\n",
  741. GNUNET_h2s (&dht_stop_msg->key),
  742. ch->client);
  743. ctx.ch = ch;
  744. ctx.unique_id = dht_stop_msg->unique_id;
  745. GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
  746. &dht_stop_msg->key,
  747. &remove_by_unique_id,
  748. &ctx);
  749. GNUNET_SERVICE_client_continue (ch->client);
  750. }
  751. /**
  752. * Handler for monitor start messages
  753. *
  754. * @param cls the client we received this message from
  755. * @param msg the actual message received
  756. *
  757. */
  758. static void
  759. handle_dht_local_monitor (void *cls,
  760. const struct GNUNET_DHT_MonitorStartStopMessage *msg)
  761. {
  762. struct ClientHandle *ch = cls;
  763. struct ClientMonitorRecord *r;
  764. r = GNUNET_new (struct ClientMonitorRecord);
  765. r->ch = ch;
  766. r->type = ntohl (msg->type);
  767. r->get = ntohs (msg->get);
  768. r->get_resp = ntohs (msg->get_resp);
  769. r->put = ntohs (msg->put);
  770. if (0 == ntohs (msg->filter_key))
  771. {
  772. r->key = NULL;
  773. }
  774. else
  775. {
  776. r->key = GNUNET_new (struct GNUNET_HashCode);
  777. GNUNET_memcpy (r->key,
  778. &msg->key,
  779. sizeof (struct GNUNET_HashCode));
  780. }
  781. GNUNET_CONTAINER_DLL_insert (monitor_head,
  782. monitor_tail,
  783. r);
  784. GNUNET_SERVICE_client_continue (ch->client);
  785. }
  786. /**
  787. * Handler for monitor stop messages
  788. *
  789. * @param cls the client we received this message from
  790. * @param msg the actual message received
  791. */
  792. static void
  793. handle_dht_local_monitor_stop (void *cls,
  794. const struct GNUNET_DHT_MonitorStartStopMessage *msg)
  795. {
  796. struct ClientHandle *ch = cls;
  797. struct ClientMonitorRecord *r;
  798. int keys_match;
  799. GNUNET_SERVICE_client_continue (ch->client);
  800. for (r = monitor_head; NULL != r; r = r->next)
  801. {
  802. if (NULL == r->key)
  803. {
  804. keys_match = (0 == ntohs(msg->filter_key));
  805. }
  806. else
  807. {
  808. keys_match = ( (0 != ntohs(msg->filter_key)) &&
  809. (! memcmp (r->key,
  810. &msg->key,
  811. sizeof(struct GNUNET_HashCode))) );
  812. }
  813. if ( (ch == r->ch) &&
  814. (ntohl(msg->type) == r->type) &&
  815. (r->get == msg->get) &&
  816. (r->get_resp == msg->get_resp) &&
  817. (r->put == msg->put) &&
  818. keys_match )
  819. {
  820. GNUNET_CONTAINER_DLL_remove (monitor_head,
  821. monitor_tail,
  822. r);
  823. GNUNET_free_non_null (r->key);
  824. GNUNET_free (r);
  825. return; /* Delete only ONE entry */
  826. }
  827. }
  828. }
  829. /**
  830. * Closure for #forward_reply()
  831. */
  832. struct ForwardReplyContext
  833. {
  834. /**
  835. * Expiration time of the reply.
  836. */
  837. struct GNUNET_TIME_Absolute expiration;
  838. /**
  839. * GET path taken.
  840. */
  841. const struct GNUNET_PeerIdentity *get_path;
  842. /**
  843. * PUT path taken.
  844. */
  845. const struct GNUNET_PeerIdentity *put_path;
  846. /**
  847. * Embedded payload.
  848. */
  849. const void *data;
  850. /**
  851. * Number of bytes in data.
  852. */
  853. size_t data_size;
  854. /**
  855. * Number of entries in @e get_path.
  856. */
  857. unsigned int get_path_length;
  858. /**
  859. * Number of entries in @e put_path.
  860. */
  861. unsigned int put_path_length;
  862. /**
  863. * Type of the data.
  864. */
  865. enum GNUNET_BLOCK_Type type;
  866. };
  867. /**
  868. * Iterator over hash map entries that send a given reply to
  869. * each of the matching clients. With some tricky recycling
  870. * of the buffer.
  871. *
  872. * @param cls the 'struct ForwardReplyContext'
  873. * @param key current key
  874. * @param value value in the hash map, a ClientQueryRecord
  875. * @return #GNUNET_YES (we should continue to iterate),
  876. * if the result is mal-formed, #GNUNET_NO
  877. */
  878. static int
  879. forward_reply (void *cls,
  880. const struct GNUNET_HashCode *key,
  881. void *value)
  882. {
  883. struct ForwardReplyContext *frc = cls;
  884. struct ClientQueryRecord *record = value;
  885. struct GNUNET_MQ_Envelope *env;
  886. struct GNUNET_DHT_ClientResultMessage *reply;
  887. enum GNUNET_BLOCK_EvaluationResult eval;
  888. int do_free;
  889. struct GNUNET_HashCode ch;
  890. struct GNUNET_PeerIdentity *paths;
  891. LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
  892. "CLIENT-RESULT %s\n",
  893. GNUNET_h2s_full (key));
  894. if ( (record->type != GNUNET_BLOCK_TYPE_ANY) &&
  895. (record->type != frc->type))
  896. {
  897. LOG (GNUNET_ERROR_TYPE_DEBUG,
  898. "Record type mismatch, not passing request for key %s to local client\n",
  899. GNUNET_h2s (key));
  900. GNUNET_STATISTICS_update (GDS_stats,
  901. gettext_noop
  902. ("# Key match, type mismatches in REPLY to CLIENT"),
  903. 1, GNUNET_NO);
  904. return GNUNET_YES; /* type mismatch */
  905. }
  906. GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch);
  907. for (unsigned int i = 0; i < record->seen_replies_count; i++)
  908. if (0 == memcmp (&record->seen_replies[i],
  909. &ch,
  910. sizeof (struct GNUNET_HashCode)))
  911. {
  912. LOG (GNUNET_ERROR_TYPE_DEBUG,
  913. "Duplicate reply, not passing request for key %s to local client\n",
  914. GNUNET_h2s (key));
  915. GNUNET_STATISTICS_update (GDS_stats,
  916. gettext_noop
  917. ("# Duplicate REPLIES to CLIENT request dropped"),
  918. 1, GNUNET_NO);
  919. return GNUNET_YES; /* duplicate */
  920. }
  921. eval
  922. = GNUNET_BLOCK_evaluate (GDS_block_context,
  923. record->type,
  924. NULL,
  925. GNUNET_BLOCK_EO_NONE,
  926. key,
  927. record->xquery,
  928. record->xquery_size,
  929. frc->data,
  930. frc->data_size);
  931. LOG (GNUNET_ERROR_TYPE_DEBUG,
  932. "Evaluation result is %d for key %s for local client's query\n",
  933. (int) eval,
  934. GNUNET_h2s (key));
  935. switch (eval)
  936. {
  937. case GNUNET_BLOCK_EVALUATION_OK_LAST:
  938. do_free = GNUNET_YES;
  939. break;
  940. case GNUNET_BLOCK_EVALUATION_OK_MORE:
  941. GNUNET_array_append (record->seen_replies,
  942. record->seen_replies_count,
  943. ch);
  944. do_free = GNUNET_NO;
  945. break;
  946. case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
  947. /* should be impossible to encounter here */
  948. GNUNET_break (0);
  949. return GNUNET_YES;
  950. case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
  951. GNUNET_break_op (0);
  952. return GNUNET_NO;
  953. case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
  954. GNUNET_break (0);
  955. return GNUNET_NO;
  956. case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
  957. GNUNET_break (0);
  958. return GNUNET_NO;
  959. case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
  960. return GNUNET_YES;
  961. case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
  962. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  963. _("Unsupported block type (%u) in request!\n"), record->type);
  964. return GNUNET_NO;
  965. default:
  966. GNUNET_break (0);
  967. return GNUNET_NO;
  968. }
  969. GNUNET_STATISTICS_update (GDS_stats,
  970. gettext_noop ("# RESULTS queued for clients"),
  971. 1,
  972. GNUNET_NO);
  973. env = GNUNET_MQ_msg_extra (reply,
  974. frc->data_size +
  975. (frc->get_path_length + frc->put_path_length) * sizeof (struct GNUNET_PeerIdentity),
  976. GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
  977. reply->type = htonl (frc->type);
  978. reply->get_path_length = htonl (frc->get_path_length);
  979. reply->put_path_length = htonl (frc->put_path_length);
  980. reply->unique_id = record->unique_id;
  981. reply->expiration = GNUNET_TIME_absolute_hton (frc->expiration);
  982. reply->key = *key;
  983. paths = (struct GNUNET_PeerIdentity *) &reply[1];
  984. GNUNET_memcpy (paths,
  985. frc->put_path,
  986. sizeof (struct GNUNET_PeerIdentity) * frc->put_path_length);
  987. GNUNET_memcpy (&paths[frc->put_path_length],
  988. frc->get_path,
  989. sizeof (struct GNUNET_PeerIdentity) * frc->get_path_length);
  990. GNUNET_memcpy (&paths[frc->get_path_length + frc->put_path_length],
  991. frc->data,
  992. frc->data_size);
  993. LOG (GNUNET_ERROR_TYPE_DEBUG,
  994. "Sending reply to query %s for client %p\n",
  995. GNUNET_h2s (key),
  996. record->ch->client);
  997. GNUNET_MQ_send (record->ch->mq,
  998. env);
  999. if (GNUNET_YES == do_free)
  1000. remove_client_record (record);
  1001. return GNUNET_YES;
  1002. }
  1003. /**
  1004. * Handle a reply we've received from another peer. If the reply
  1005. * matches any of our pending queries, forward it to the respective
  1006. * client(s).
  1007. *
  1008. * @param expiration when will the reply expire
  1009. * @param key the query this reply is for
  1010. * @param get_path_length number of peers in @a get_path
  1011. * @param get_path path the reply took on get
  1012. * @param put_path_length number of peers in @a put_path
  1013. * @param put_path path the reply took on put
  1014. * @param type type of the reply
  1015. * @param data_size number of bytes in @a data
  1016. * @param data application payload data
  1017. */
  1018. void
  1019. GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
  1020. const struct GNUNET_HashCode *key,
  1021. unsigned int get_path_length,
  1022. const struct GNUNET_PeerIdentity *get_path,
  1023. unsigned int put_path_length,
  1024. const struct GNUNET_PeerIdentity *put_path,
  1025. enum GNUNET_BLOCK_Type type,
  1026. size_t data_size,
  1027. const void *data)
  1028. {
  1029. struct ForwardReplyContext frc;
  1030. size_t msize;
  1031. msize = sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size +
  1032. (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
  1033. if (msize >= GNUNET_MAX_MESSAGE_SIZE)
  1034. {
  1035. GNUNET_break (0);
  1036. return;
  1037. }
  1038. if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map,
  1039. key))
  1040. {
  1041. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1042. "No matching client for reply for key %s\n",
  1043. GNUNET_h2s (key));
  1044. GNUNET_STATISTICS_update (GDS_stats,
  1045. gettext_noop ("# REPLIES ignored for CLIENTS (no match)"),
  1046. 1,
  1047. GNUNET_NO);
  1048. return; /* no matching request, fast exit! */
  1049. }
  1050. frc.expiration = expiration;
  1051. frc.get_path = get_path;
  1052. frc.put_path = put_path;
  1053. frc.data = data;
  1054. frc.data_size = data_size;
  1055. frc.get_path_length = get_path_length;
  1056. frc.put_path_length = put_path_length;
  1057. frc.type = type;
  1058. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1059. "Forwarding reply for key %s to client\n",
  1060. GNUNET_h2s (key));
  1061. GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
  1062. key,
  1063. &forward_reply,
  1064. &frc);
  1065. }
  1066. /**
  1067. * Check if some client is monitoring GET messages and notify
  1068. * them in that case.
  1069. *
  1070. * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
  1071. * @param type The type of data in the request.
  1072. * @param hop_count Hop count so far.
  1073. * @param path_length number of entries in path (or 0 if not recorded).
  1074. * @param path peers on the GET path (or NULL if not recorded).
  1075. * @param desired_replication_level Desired replication level.
  1076. * @param key Key of the requested data.
  1077. */
  1078. void
  1079. GDS_CLIENTS_process_get (uint32_t options,
  1080. enum GNUNET_BLOCK_Type type,
  1081. uint32_t hop_count,
  1082. uint32_t desired_replication_level,
  1083. unsigned int path_length,
  1084. const struct GNUNET_PeerIdentity *path,
  1085. const struct GNUNET_HashCode * key)
  1086. {
  1087. struct ClientMonitorRecord *m;
  1088. struct ClientHandle **cl;
  1089. unsigned int cl_size;
  1090. cl = NULL;
  1091. cl_size = 0;
  1092. for (m = monitor_head; NULL != m; m = m->next)
  1093. {
  1094. if ( ( (GNUNET_BLOCK_TYPE_ANY == m->type) ||
  1095. (m->type == type) ) &&
  1096. ( (NULL == m->key) ||
  1097. (0 == memcmp (key,
  1098. m->key,
  1099. sizeof(struct GNUNET_HashCode))) ) )
  1100. {
  1101. struct GNUNET_MQ_Envelope *env;
  1102. struct GNUNET_DHT_MonitorGetMessage *mmsg;
  1103. struct GNUNET_PeerIdentity *msg_path;
  1104. size_t msize;
  1105. unsigned int i;
  1106. /* Don't send duplicates */
  1107. for (i = 0; i < cl_size; i++)
  1108. if (cl[i] == m->ch)
  1109. break;
  1110. if (i < cl_size)
  1111. continue;
  1112. GNUNET_array_append (cl,
  1113. cl_size,
  1114. m->ch);
  1115. msize = path_length * sizeof (struct GNUNET_PeerIdentity);
  1116. env = GNUNET_MQ_msg_extra (mmsg,
  1117. msize,
  1118. GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET);
  1119. mmsg->options = htonl(options);
  1120. mmsg->type = htonl(type);
  1121. mmsg->hop_count = htonl(hop_count);
  1122. mmsg->desired_replication_level = htonl(desired_replication_level);
  1123. mmsg->get_path_length = htonl(path_length);
  1124. mmsg->key = *key;
  1125. msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
  1126. GNUNET_memcpy (msg_path,
  1127. path,
  1128. path_length * sizeof (struct GNUNET_PeerIdentity));
  1129. GNUNET_MQ_send (m->ch->mq,
  1130. env);
  1131. }
  1132. }
  1133. GNUNET_free_non_null (cl);
  1134. }
  1135. /**
  1136. * Check if some client is monitoring GET RESP messages and notify
  1137. * them in that case.
  1138. *
  1139. * @param type The type of data in the result.
  1140. * @param get_path Peers on GET path (or NULL if not recorded).
  1141. * @param get_path_length number of entries in get_path.
  1142. * @param put_path peers on the PUT path (or NULL if not recorded).
  1143. * @param put_path_length number of entries in get_path.
  1144. * @param exp Expiration time of the data.
  1145. * @param key Key of the data.
  1146. * @param data Pointer to the result data.
  1147. * @param size Number of bytes in @a data.
  1148. */
  1149. void
  1150. GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type,
  1151. const struct GNUNET_PeerIdentity *get_path,
  1152. unsigned int get_path_length,
  1153. const struct GNUNET_PeerIdentity *put_path,
  1154. unsigned int put_path_length,
  1155. struct GNUNET_TIME_Absolute exp,
  1156. const struct GNUNET_HashCode * key,
  1157. const void *data,
  1158. size_t size)
  1159. {
  1160. struct ClientMonitorRecord *m;
  1161. struct ClientHandle **cl;
  1162. unsigned int cl_size;
  1163. cl = NULL;
  1164. cl_size = 0;
  1165. for (m = monitor_head; NULL != m; m = m->next)
  1166. {
  1167. if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
  1168. (NULL == m->key ||
  1169. memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
  1170. {
  1171. struct GNUNET_MQ_Envelope *env;
  1172. struct GNUNET_DHT_MonitorGetRespMessage *mmsg;
  1173. struct GNUNET_PeerIdentity *path;
  1174. size_t msize;
  1175. unsigned int i;
  1176. /* Don't send duplicates */
  1177. for (i = 0; i < cl_size; i++)
  1178. if (cl[i] == m->ch)
  1179. break;
  1180. if (i < cl_size)
  1181. continue;
  1182. GNUNET_array_append (cl,
  1183. cl_size,
  1184. m->ch);
  1185. msize = size;
  1186. msize += (get_path_length + put_path_length)
  1187. * sizeof (struct GNUNET_PeerIdentity);
  1188. env = GNUNET_MQ_msg_extra (mmsg,
  1189. msize,
  1190. GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP);
  1191. mmsg->type = htonl(type);
  1192. mmsg->put_path_length = htonl(put_path_length);
  1193. mmsg->get_path_length = htonl(get_path_length);
  1194. mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
  1195. mmsg->key = *key;
  1196. path = (struct GNUNET_PeerIdentity *) &mmsg[1];
  1197. GNUNET_memcpy (path,
  1198. put_path,
  1199. put_path_length * sizeof (struct GNUNET_PeerIdentity));
  1200. GNUNET_memcpy (path,
  1201. get_path,
  1202. get_path_length * sizeof (struct GNUNET_PeerIdentity));
  1203. GNUNET_memcpy (&path[get_path_length],
  1204. data,
  1205. size);
  1206. GNUNET_MQ_send (m->ch->mq,
  1207. env);
  1208. }
  1209. }
  1210. GNUNET_free_non_null (cl);
  1211. }
  1212. /**
  1213. * Check if some client is monitoring PUT messages and notify
  1214. * them in that case.
  1215. *
  1216. * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
  1217. * @param type The type of data in the request.
  1218. * @param hop_count Hop count so far.
  1219. * @param path_length number of entries in path (or 0 if not recorded).
  1220. * @param path peers on the PUT path (or NULL if not recorded).
  1221. * @param desired_replication_level Desired replication level.
  1222. * @param exp Expiration time of the data.
  1223. * @param key Key under which data is to be stored.
  1224. * @param data Pointer to the data carried.
  1225. * @param size Number of bytes in data.
  1226. */
  1227. void
  1228. GDS_CLIENTS_process_put (uint32_t options,
  1229. enum GNUNET_BLOCK_Type type,
  1230. uint32_t hop_count,
  1231. uint32_t desired_replication_level,
  1232. unsigned int path_length,
  1233. const struct GNUNET_PeerIdentity *path,
  1234. struct GNUNET_TIME_Absolute exp,
  1235. const struct GNUNET_HashCode *key,
  1236. const void *data,
  1237. size_t size)
  1238. {
  1239. struct ClientMonitorRecord *m;
  1240. struct ClientHandle **cl;
  1241. unsigned int cl_size;
  1242. cl = NULL;
  1243. cl_size = 0;
  1244. for (m = monitor_head; NULL != m; m = m->next)
  1245. {
  1246. if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
  1247. (NULL == m->key ||
  1248. memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
  1249. {
  1250. struct GNUNET_MQ_Envelope *env;
  1251. struct GNUNET_DHT_MonitorPutMessage *mmsg;
  1252. struct GNUNET_PeerIdentity *msg_path;
  1253. size_t msize;
  1254. unsigned int i;
  1255. /* Don't send duplicates */
  1256. for (i = 0; i < cl_size; i++)
  1257. if (cl[i] == m->ch)
  1258. break;
  1259. if (i < cl_size)
  1260. continue;
  1261. GNUNET_array_append (cl,
  1262. cl_size,
  1263. m->ch);
  1264. msize = size;
  1265. msize += path_length * sizeof (struct GNUNET_PeerIdentity);
  1266. env = GNUNET_MQ_msg_extra (mmsg,
  1267. msize,
  1268. GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT);
  1269. mmsg->options = htonl(options);
  1270. mmsg->type = htonl(type);
  1271. mmsg->hop_count = htonl(hop_count);
  1272. mmsg->desired_replication_level = htonl (desired_replication_level);
  1273. mmsg->put_path_length = htonl (path_length);
  1274. mmsg->key = *key;
  1275. mmsg->expiration_time = GNUNET_TIME_absolute_hton (exp);
  1276. msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
  1277. GNUNET_memcpy (msg_path,
  1278. path,
  1279. path_length * sizeof (struct GNUNET_PeerIdentity));
  1280. GNUNET_memcpy (&msg_path[path_length],
  1281. data,
  1282. size);
  1283. GNUNET_MQ_send (m->ch->mq,
  1284. env);
  1285. }
  1286. }
  1287. GNUNET_free_non_null (cl);
  1288. }
  1289. /**
  1290. * Initialize client subsystem.
  1291. *
  1292. * @param server the initialized server
  1293. */
  1294. static void
  1295. GDS_CLIENTS_init ()
  1296. {
  1297. forward_map
  1298. = GNUNET_CONTAINER_multihashmap_create (1024,
  1299. GNUNET_YES);
  1300. retry_heap
  1301. = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  1302. }
  1303. /**
  1304. * Shutdown client subsystem.
  1305. */
  1306. static void
  1307. GDS_CLIENTS_stop ()
  1308. {
  1309. if (NULL != retry_task)
  1310. {
  1311. GNUNET_SCHEDULER_cancel (retry_task);
  1312. retry_task = NULL;
  1313. }
  1314. }
  1315. /**
  1316. * Define "main" method using service macro.
  1317. *
  1318. * @param name name of the service, i.e. "dht" or "xdht"
  1319. * @param run name of the initializaton method for the service
  1320. */
  1321. #define GDS_DHT_SERVICE_INIT(name,run) \
  1322. GNUNET_SERVICE_MAIN \
  1323. (name, \
  1324. GNUNET_SERVICE_OPTION_NONE, \
  1325. run, \
  1326. &client_connect_cb, \
  1327. &client_disconnect_cb, \
  1328. NULL, \
  1329. GNUNET_MQ_hd_var_size (dht_local_put, \
  1330. GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, \
  1331. struct GNUNET_DHT_ClientPutMessage, \
  1332. NULL), \
  1333. GNUNET_MQ_hd_var_size (dht_local_get, \
  1334. GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, \
  1335. struct GNUNET_DHT_ClientGetMessage, \
  1336. NULL), \
  1337. GNUNET_MQ_hd_fixed_size (dht_local_get_stop, \
  1338. GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP, \
  1339. struct GNUNET_DHT_ClientGetStopMessage, \
  1340. NULL), \
  1341. GNUNET_MQ_hd_fixed_size (dht_local_monitor, \
  1342. GNUNET_MESSAGE_TYPE_DHT_MONITOR_START, \
  1343. struct GNUNET_DHT_MonitorStartStopMessage, \
  1344. NULL), \
  1345. GNUNET_MQ_hd_fixed_size (dht_local_monitor_stop, \
  1346. GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP, \
  1347. struct GNUNET_DHT_MonitorStartStopMessage, \
  1348. NULL), \
  1349. GNUNET_MQ_hd_var_size (dht_local_get_result_seen, \
  1350. GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN, \
  1351. struct GNUNET_DHT_ClientGetResultSeenMessage , \
  1352. NULL), \
  1353. GNUNET_MQ_handler_end ())
  1354. /**
  1355. * MINIMIZE heap size (way below 128k) since this process doesn't need much.
  1356. */
  1357. void __attribute__ ((destructor))
  1358. GDS_CLIENTS_done ()
  1359. {
  1360. if (NULL != retry_heap)
  1361. {
  1362. GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
  1363. GNUNET_CONTAINER_heap_destroy (retry_heap);
  1364. retry_heap = NULL;
  1365. }
  1366. if (NULL != forward_map)
  1367. {
  1368. GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
  1369. GNUNET_CONTAINER_multihashmap_destroy (forward_map);
  1370. forward_map = NULL;
  1371. }
  1372. }
  1373. /* end of gnunet-service-dht_clients.c */