fs_search.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009 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/fs_search.c
  19. * @brief Helper functions for searching.
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_constants.h"
  24. #include "gnunet_fs_service.h"
  25. #include "gnunet_protocols.h"
  26. #include "fs.h"
  27. #define DEBUG_SEARCH GNUNET_NO
  28. /**
  29. * Fill in all of the generic fields for a search event and
  30. * call the callback.
  31. *
  32. * @param pi structure to fill in
  33. * @param sc overall search context
  34. * @return value returned by the callback
  35. */
  36. void *
  37. GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
  38. struct GNUNET_FS_SearchContext *sc)
  39. {
  40. void *ret;
  41. pi->value.search.sc = sc;
  42. pi->value.search.cctx = sc->client_info;
  43. pi->value.search.pctx =
  44. (sc->psearch_result == NULL) ? NULL : sc->psearch_result->client_info;
  45. pi->value.search.query = sc->uri;
  46. pi->value.search.duration =
  47. GNUNET_TIME_absolute_get_duration (sc->start_time);
  48. pi->value.search.anonymity = sc->anonymity;
  49. ret = sc->h->upcb (sc->h->upcb_cls, pi);
  50. return ret;
  51. }
  52. /**
  53. * Check if the given result is identical
  54. * to the given URI.
  55. *
  56. * @param cls points to the URI we check against
  57. * @param key not used
  58. * @param value a "struct GNUNET_FS_SearchResult" who's URI we
  59. * should compare with
  60. * @return GNUNET_SYSERR if the result is present,
  61. * GNUNET_OK otherwise
  62. */
  63. static int
  64. test_result_present (void *cls, const GNUNET_HashCode * key, void *value)
  65. {
  66. const struct GNUNET_FS_Uri *uri = cls;
  67. struct GNUNET_FS_SearchResult *sr = value;
  68. if (GNUNET_FS_uri_test_equal (uri, sr->uri))
  69. return GNUNET_SYSERR;
  70. return GNUNET_OK;
  71. }
  72. /**
  73. * We've found a new CHK result. Let the client
  74. * know about it.
  75. *
  76. * @param sc the search context
  77. * @param sr the specific result
  78. */
  79. static void
  80. notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
  81. struct GNUNET_FS_SearchResult *sr)
  82. {
  83. struct GNUNET_FS_ProgressInfo pi;
  84. pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
  85. pi.value.search.specifics.result.meta = sr->meta;
  86. pi.value.search.specifics.result.uri = sr->uri;
  87. pi.value.search.specifics.result.result = sr;
  88. pi.value.search.specifics.result.applicability_rank = sr->optional_support;
  89. sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  90. }
  91. /**
  92. * We've found new information about an existing CHK result. Let the
  93. * client know about it.
  94. *
  95. * @param sc the search context
  96. * @param sr the specific result
  97. */
  98. static void
  99. notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
  100. struct GNUNET_FS_SearchResult *sr)
  101. {
  102. struct GNUNET_FS_ProgressInfo pi;
  103. pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
  104. pi.value.search.specifics.update.cctx = sr->client_info;
  105. pi.value.search.specifics.update.meta = sr->meta;
  106. pi.value.search.specifics.update.uri = sr->uri;
  107. pi.value.search.specifics.update.availability_rank =
  108. 2 * sr->availability_success - sr->availability_trials;
  109. pi.value.search.specifics.update.availability_certainty =
  110. sr->availability_trials;
  111. pi.value.search.specifics.update.applicability_rank = sr->optional_support;
  112. sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  113. }
  114. /**
  115. * Context for "get_result_present".
  116. */
  117. struct GetResultContext
  118. {
  119. /**
  120. * The URI we're looking for.
  121. */
  122. const struct GNUNET_FS_Uri *uri;
  123. /**
  124. * Where to store a pointer to the search
  125. * result struct if we found a match.
  126. */
  127. struct GNUNET_FS_SearchResult *sr;
  128. };
  129. /**
  130. * Check if the given result is identical to the given URI and if so
  131. * return it.
  132. *
  133. * @param cls a "struct GetResultContext"
  134. * @param key not used
  135. * @param value a "struct GNUNET_FS_SearchResult" who's URI we
  136. * should compare with
  137. * @return GNUNET_OK
  138. */
  139. static int
  140. get_result_present (void *cls, const GNUNET_HashCode * key, void *value)
  141. {
  142. struct GetResultContext *grc = cls;
  143. struct GNUNET_FS_SearchResult *sr = value;
  144. if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
  145. grc->sr = sr;
  146. return GNUNET_OK;
  147. }
  148. /**
  149. * Signal result of last probe to client and then schedule next
  150. * probe.
  151. */
  152. static void
  153. signal_probe_result (struct GNUNET_FS_SearchResult *sr)
  154. {
  155. struct GNUNET_FS_ProgressInfo pi;
  156. pi.status = GNUNET_FS_STATUS_SEARCH_START;
  157. pi.value.search.specifics.update.cctx = sr->client_info;
  158. pi.value.search.specifics.update.meta = sr->meta;
  159. pi.value.search.specifics.update.uri = sr->uri;
  160. pi.value.search.specifics.update.availability_rank = sr->availability_success;
  161. pi.value.search.specifics.update.availability_certainty =
  162. sr->availability_trials;
  163. pi.value.search.specifics.update.applicability_rank = sr->optional_support;
  164. sr->sc->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
  165. GNUNET_FS_search_start_probe_ (sr);
  166. }
  167. /**
  168. * Handle the case where we have failed to receive a response for our probe.
  169. *
  170. * @param cls our 'struct GNUNET_FS_SearchResult*'
  171. * @param tc scheduler context
  172. */
  173. static void
  174. probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  175. {
  176. struct GNUNET_FS_SearchResult *sr = cls;
  177. sr->availability_trials++;
  178. GNUNET_FS_search_result_sync_ (sr);
  179. signal_probe_result (sr);
  180. }
  181. /**
  182. * Handle the case where we have gotten a response for our probe.
  183. *
  184. * @param cls our 'struct GNUNET_FS_SearchResult*'
  185. * @param tc scheduler context
  186. */
  187. static void
  188. probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  189. {
  190. struct GNUNET_FS_SearchResult *sr = cls;
  191. sr->availability_trials++;
  192. sr->availability_success++;
  193. GNUNET_FS_search_result_sync_ (sr);
  194. signal_probe_result (sr);
  195. }
  196. /**
  197. * Notification of FS that a search probe has made progress.
  198. * This function is used INSTEAD of the client's event handler
  199. * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
  200. *
  201. * @param cls closure, always NULL (!), actual closure
  202. * is in the client-context of the info struct
  203. * @param info details about the event, specifying the event type
  204. * and various bits about the event
  205. * @return client-context (for the next progress call
  206. * for this operation; should be set to NULL for
  207. * SUSPEND and STOPPED events). The value returned
  208. * will be passed to future callbacks in the respective
  209. * field in the GNUNET_FS_ProgressInfo struct.
  210. */
  211. void *
  212. GNUNET_FS_search_probe_progress_ (void *cls,
  213. const struct GNUNET_FS_ProgressInfo *info)
  214. {
  215. struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
  216. struct GNUNET_TIME_Relative dur;
  217. switch (info->status)
  218. {
  219. case GNUNET_FS_STATUS_DOWNLOAD_START:
  220. /* ignore */
  221. break;
  222. case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
  223. /* probes should never be resumed */
  224. GNUNET_assert (0);
  225. break;
  226. case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
  227. /* probes should never be suspended */
  228. GNUNET_break (0);
  229. break;
  230. case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
  231. /* ignore */
  232. break;
  233. case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
  234. if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
  235. {
  236. GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
  237. sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
  238. }
  239. sr->probe_cancel_task =
  240. GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
  241. &probe_failure_handler, sr);
  242. break;
  243. case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
  244. if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
  245. {
  246. GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
  247. sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
  248. }
  249. sr->probe_cancel_task =
  250. GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
  251. &probe_success_handler, sr);
  252. break;
  253. case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
  254. if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
  255. {
  256. GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
  257. sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
  258. }
  259. sr = NULL;
  260. break;
  261. case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
  262. GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
  263. sr->probe_active_time = GNUNET_TIME_absolute_get ();
  264. sr->probe_cancel_task =
  265. GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
  266. &probe_failure_handler, sr);
  267. break;
  268. case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
  269. if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
  270. {
  271. GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
  272. sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
  273. }
  274. dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
  275. sr->remaining_probe_time =
  276. GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
  277. GNUNET_FS_search_result_sync_ (sr);
  278. break;
  279. default:
  280. GNUNET_break (0);
  281. return NULL;
  282. }
  283. return sr;
  284. }
  285. /**
  286. * Start download probes for the given search result.
  287. *
  288. * @param sr the search result
  289. */
  290. void
  291. GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
  292. {
  293. uint64_t off;
  294. uint64_t len;
  295. if (sr->probe_ctx != NULL)
  296. return;
  297. if (sr->download != NULL)
  298. return;
  299. if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
  300. return;
  301. if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
  302. return;
  303. len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
  304. if (len == 0)
  305. return;
  306. if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
  307. return;
  308. off = len / DBLOCK_SIZE;
  309. if (off > 0)
  310. off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
  311. off *= DBLOCK_SIZE;
  312. if (len - off < DBLOCK_SIZE)
  313. len = len - off;
  314. else
  315. len = DBLOCK_SIZE;
  316. sr->remaining_probe_time =
  317. GNUNET_TIME_relative_multiply (sr->sc->h->avg_block_latency,
  318. 2 * (1 + sr->availability_trials));
  319. sr->probe_ctx =
  320. GNUNET_FS_download_start (sr->sc->h, sr->uri, sr->meta, NULL, NULL, off,
  321. len, sr->sc->anonymity,
  322. GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
  323. GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
  324. }
  325. /**
  326. * We have received a KSK result. Check how it fits in with the
  327. * overall query and notify the client accordingly.
  328. *
  329. * @param sc context for the overall query
  330. * @param ent entry for the specific keyword
  331. * @param uri the URI that was found
  332. * @param meta metadata associated with the URI
  333. * under the "ent" keyword
  334. */
  335. static void
  336. process_ksk_result (struct GNUNET_FS_SearchContext *sc,
  337. struct SearchRequestEntry *ent,
  338. const struct GNUNET_FS_Uri *uri,
  339. const struct GNUNET_CONTAINER_MetaData *meta)
  340. {
  341. GNUNET_HashCode key;
  342. struct GNUNET_FS_SearchResult *sr;
  343. struct GetResultContext grc;
  344. int is_new;
  345. /* check if new */
  346. GNUNET_FS_uri_to_key (uri, &key);
  347. if (GNUNET_SYSERR ==
  348. GNUNET_CONTAINER_multihashmap_get_multiple (ent->results, &key,
  349. &test_result_present,
  350. (void *) uri))
  351. return; /* duplicate result */
  352. /* try to find search result in master map */
  353. grc.sr = NULL;
  354. grc.uri = uri;
  355. GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
  356. &get_result_present, &grc);
  357. sr = grc.sr;
  358. is_new = (NULL == sr) || (sr->mandatory_missing > 0);
  359. if (NULL == sr)
  360. {
  361. sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
  362. sr->sc = sc;
  363. sr->uri = GNUNET_FS_uri_dup (uri);
  364. sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
  365. sr->mandatory_missing = sc->mandatory_count;
  366. sr->key = key;
  367. GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
  368. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  369. }
  370. else
  371. {
  372. GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
  373. }
  374. /* check if mandatory satisfied */
  375. if (ent->mandatory)
  376. sr->mandatory_missing--;
  377. else
  378. sr->optional_support++;
  379. if (0 != sr->mandatory_missing)
  380. return;
  381. if (is_new)
  382. notify_client_chk_result (sc, sr);
  383. else
  384. notify_client_chk_update (sc, sr);
  385. GNUNET_FS_search_result_sync_ (sr);
  386. GNUNET_FS_search_start_probe_ (sr);
  387. }
  388. /**
  389. * Start search for content, internal API.
  390. *
  391. * @param h handle to the file sharing subsystem
  392. * @param uri specifies the search parameters; can be
  393. * a KSK URI or an SKS URI.
  394. * @param anonymity desired level of anonymity
  395. * @param options options for the search
  396. * @param cctx client context
  397. * @param psearch parent search result (for namespace update searches)
  398. * @return context that can be used to control the search
  399. */
  400. static struct GNUNET_FS_SearchContext *
  401. search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
  402. uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
  403. void *cctx, struct GNUNET_FS_SearchResult *psearch);
  404. /**
  405. * We have received an SKS result. Start searching for updates and
  406. * notify the client if it is a new result.
  407. *
  408. * @param sc context for the overall query
  409. * @param id_update identifier for updates, NULL for none
  410. * @param uri the URI that was found
  411. * @param meta metadata associated with the URI
  412. */
  413. static void
  414. process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
  415. const struct GNUNET_FS_Uri *uri,
  416. const struct GNUNET_CONTAINER_MetaData *meta)
  417. {
  418. struct GNUNET_FS_Uri uu;
  419. GNUNET_HashCode key;
  420. struct GNUNET_FS_SearchResult *sr;
  421. /* check if new */
  422. GNUNET_FS_uri_to_key (uri, &key);
  423. GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
  424. &key);
  425. if (GNUNET_SYSERR ==
  426. GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
  427. &test_result_present,
  428. (void *) uri))
  429. return; /* duplicate result */
  430. sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
  431. sr->sc = sc;
  432. sr->uri = GNUNET_FS_uri_dup (uri);
  433. sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
  434. sr->key = key;
  435. GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
  436. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  437. GNUNET_FS_search_result_sync_ (sr);
  438. GNUNET_FS_search_start_probe_ (sr);
  439. /* notify client */
  440. notify_client_chk_result (sc, sr);
  441. /* search for updates */
  442. if (strlen (id_update) == 0)
  443. return; /* no updates */
  444. uu.type = sks;
  445. uu.data.sks.namespace = sc->uri->data.sks.namespace;
  446. uu.data.sks.identifier = GNUNET_strdup (id_update);
  447. (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
  448. GNUNET_free (uu.data.sks.identifier);
  449. }
  450. /**
  451. * Process a keyword-search result.
  452. *
  453. * @param sc our search context
  454. * @param kb the kblock
  455. * @param size size of kb
  456. */
  457. static void
  458. process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb,
  459. size_t size)
  460. {
  461. unsigned int i;
  462. size_t j;
  463. GNUNET_HashCode q;
  464. char pt[size - sizeof (struct KBlock)];
  465. struct GNUNET_CRYPTO_AesSessionKey skey;
  466. struct GNUNET_CRYPTO_AesInitializationVector iv;
  467. const char *eos;
  468. struct GNUNET_CONTAINER_MetaData *meta;
  469. struct GNUNET_FS_Uri *uri;
  470. char *emsg;
  471. GNUNET_CRYPTO_hash (&kb->keyspace,
  472. sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
  473. &q);
  474. /* find key */
  475. for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
  476. if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
  477. break;
  478. if (i == sc->uri->data.ksk.keywordCount)
  479. {
  480. /* oops, does not match any of our keywords!? */
  481. GNUNET_break (0);
  482. return;
  483. }
  484. /* decrypt */
  485. GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
  486. if (-1 ==
  487. GNUNET_CRYPTO_aes_decrypt (&kb[1], size - sizeof (struct KBlock), &skey,
  488. &iv, pt))
  489. {
  490. GNUNET_break (0);
  491. return;
  492. }
  493. /* parse */
  494. eos = memchr (pt, 0, sizeof (pt));
  495. if (NULL == eos)
  496. {
  497. GNUNET_break_op (0);
  498. return;
  499. }
  500. j = eos - pt + 1;
  501. if (sizeof (pt) == j)
  502. meta = GNUNET_CONTAINER_meta_data_create ();
  503. else
  504. meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
  505. if (meta == NULL)
  506. {
  507. GNUNET_break_op (0); /* kblock malformed */
  508. return;
  509. }
  510. uri = GNUNET_FS_uri_parse (pt, &emsg);
  511. if (uri == NULL)
  512. {
  513. GNUNET_break_op (0); /* kblock malformed */
  514. GNUNET_free_non_null (emsg);
  515. GNUNET_CONTAINER_meta_data_destroy (meta);
  516. return;
  517. }
  518. /* process */
  519. process_ksk_result (sc, &sc->requests[i], uri, meta);
  520. /* clean up */
  521. GNUNET_CONTAINER_meta_data_destroy (meta);
  522. GNUNET_FS_uri_destroy (uri);
  523. }
  524. /**
  525. * Process a keyword-search result with a namespace advertisment.
  526. *
  527. * @param sc our search context
  528. * @param nb the nblock
  529. * @param size size of nb
  530. */
  531. static void
  532. process_nblock (struct GNUNET_FS_SearchContext *sc, const struct NBlock *nb,
  533. size_t size)
  534. {
  535. unsigned int i;
  536. size_t j;
  537. GNUNET_HashCode q;
  538. char pt[size - sizeof (struct NBlock)];
  539. struct GNUNET_CRYPTO_AesSessionKey skey;
  540. struct GNUNET_CRYPTO_AesInitializationVector iv;
  541. const char *eos;
  542. struct GNUNET_CONTAINER_MetaData *meta;
  543. struct GNUNET_FS_Uri *uri;
  544. char *uris;
  545. GNUNET_CRYPTO_hash (&nb->keyspace,
  546. sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
  547. &q);
  548. /* find key */
  549. for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
  550. if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
  551. break;
  552. if (i == sc->uri->data.ksk.keywordCount)
  553. {
  554. /* oops, does not match any of our keywords!? */
  555. GNUNET_break (0);
  556. return;
  557. }
  558. /* decrypt */
  559. GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
  560. if (-1 ==
  561. GNUNET_CRYPTO_aes_decrypt (&nb[1], size - sizeof (struct NBlock), &skey,
  562. &iv, pt))
  563. {
  564. GNUNET_break (0);
  565. return;
  566. }
  567. /* parse */
  568. eos = memchr (pt, 0, sizeof (pt));
  569. if (NULL == eos)
  570. {
  571. GNUNET_break_op (0);
  572. return;
  573. }
  574. j = eos - pt + 1;
  575. if (sizeof (pt) == j)
  576. meta = GNUNET_CONTAINER_meta_data_create ();
  577. else
  578. meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
  579. if (meta == NULL)
  580. {
  581. GNUNET_break_op (0); /* nblock malformed */
  582. return;
  583. }
  584. uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
  585. uri->type = sks;
  586. uri->data.sks.identifier = GNUNET_strdup (pt);
  587. GNUNET_CRYPTO_hash (&nb->subspace,
  588. sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
  589. &uri->data.sks.namespace);
  590. uris = GNUNET_FS_uri_to_string (uri);
  591. GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_URI,
  592. EXTRACTOR_METAFORMAT_UTF8, "text/plain",
  593. uris, strlen (uris) + 1);
  594. GNUNET_free (uris);
  595. GNUNET_PSEUDONYM_add (sc->h->cfg, &uri->data.sks.namespace, meta);
  596. /* process */
  597. process_ksk_result (sc, &sc->requests[i], uri, meta);
  598. /* clean up */
  599. GNUNET_CONTAINER_meta_data_destroy (meta);
  600. GNUNET_FS_uri_destroy (uri);
  601. }
  602. /**
  603. * Process a namespace-search result.
  604. *
  605. * @param sc our search context
  606. * @param sb the sblock
  607. * @param size size of sb
  608. */
  609. static void
  610. process_sblock (struct GNUNET_FS_SearchContext *sc, const struct SBlock *sb,
  611. size_t size)
  612. {
  613. size_t len = size - sizeof (struct SBlock);
  614. char pt[len];
  615. struct GNUNET_CRYPTO_AesSessionKey skey;
  616. struct GNUNET_CRYPTO_AesInitializationVector iv;
  617. struct GNUNET_FS_Uri *uri;
  618. struct GNUNET_CONTAINER_MetaData *meta;
  619. const char *id;
  620. const char *uris;
  621. size_t off;
  622. char *emsg;
  623. GNUNET_HashCode key;
  624. char *identifier;
  625. /* decrypt */
  626. identifier = sc->uri->data.sks.identifier;
  627. GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
  628. GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
  629. if (-1 == GNUNET_CRYPTO_aes_decrypt (&sb[1], len, &skey, &iv, pt))
  630. {
  631. GNUNET_break (0);
  632. return;
  633. }
  634. /* parse */
  635. off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris);
  636. if (off == 0)
  637. {
  638. GNUNET_break_op (0); /* sblock malformed */
  639. return;
  640. }
  641. meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off);
  642. if (meta == NULL)
  643. {
  644. GNUNET_break_op (0); /* sblock malformed */
  645. return;
  646. }
  647. uri = GNUNET_FS_uri_parse (uris, &emsg);
  648. if (uri == NULL)
  649. {
  650. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to parse URI `%s': %s\n", uris,
  651. emsg);
  652. GNUNET_break_op (0); /* sblock malformed */
  653. GNUNET_free_non_null (emsg);
  654. GNUNET_CONTAINER_meta_data_destroy (meta);
  655. return;
  656. }
  657. /* process */
  658. process_sks_result (sc, id, uri, meta);
  659. /* clean up */
  660. GNUNET_FS_uri_destroy (uri);
  661. GNUNET_CONTAINER_meta_data_destroy (meta);
  662. }
  663. /**
  664. * Process a search result.
  665. *
  666. * @param sc our search context
  667. * @param type type of the result
  668. * @param expiration when it will expire
  669. * @param data the (encrypted) response
  670. * @param size size of data
  671. */
  672. static void
  673. process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
  674. struct GNUNET_TIME_Absolute expiration, const void *data,
  675. size_t size)
  676. {
  677. if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
  678. {
  679. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  680. "Result received has already expired.\n");
  681. return; /* result expired */
  682. }
  683. switch (type)
  684. {
  685. case GNUNET_BLOCK_TYPE_FS_KBLOCK:
  686. if (!GNUNET_FS_uri_test_ksk (sc->uri))
  687. {
  688. GNUNET_break (0);
  689. return;
  690. }
  691. if (sizeof (struct KBlock) > size)
  692. {
  693. GNUNET_break_op (0);
  694. return;
  695. }
  696. process_kblock (sc, data, size);
  697. break;
  698. case GNUNET_BLOCK_TYPE_FS_SBLOCK:
  699. if (!GNUNET_FS_uri_test_sks (sc->uri))
  700. {
  701. GNUNET_break (0);
  702. return;
  703. }
  704. if (sizeof (struct SBlock) > size)
  705. {
  706. GNUNET_break_op (0);
  707. return;
  708. }
  709. process_sblock (sc, data, size);
  710. break;
  711. case GNUNET_BLOCK_TYPE_FS_NBLOCK:
  712. if (!GNUNET_FS_uri_test_ksk (sc->uri))
  713. {
  714. GNUNET_break (0);
  715. return;
  716. }
  717. if (sizeof (struct NBlock) > size)
  718. {
  719. GNUNET_break_op (0);
  720. return;
  721. }
  722. process_nblock (sc, data, size);
  723. break;
  724. case GNUNET_BLOCK_TYPE_ANY:
  725. GNUNET_break (0);
  726. break;
  727. case GNUNET_BLOCK_TYPE_FS_DBLOCK:
  728. GNUNET_break (0);
  729. break;
  730. case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
  731. GNUNET_break (0);
  732. break;
  733. case GNUNET_BLOCK_TYPE_FS_IBLOCK:
  734. GNUNET_break (0);
  735. break;
  736. default:
  737. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  738. _("Got result with unknown block type `%d', ignoring"), type);
  739. break;
  740. }
  741. }
  742. /**
  743. * Shutdown any existing connection to the FS
  744. * service and try to establish a fresh one
  745. * (and then re-transmit our search request).
  746. *
  747. * @param sc the search to reconnec
  748. */
  749. static void
  750. try_reconnect (struct GNUNET_FS_SearchContext *sc);
  751. /**
  752. * Type of a function to call when we receive a message
  753. * from the service.
  754. *
  755. * @param cls closure
  756. * @param msg message received, NULL on timeout or fatal error
  757. */
  758. static void
  759. receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
  760. {
  761. struct GNUNET_FS_SearchContext *sc = cls;
  762. const struct PutMessage *cm;
  763. uint16_t msize;
  764. if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
  765. (ntohs (msg->size) <= sizeof (struct PutMessage)))
  766. {
  767. try_reconnect (sc);
  768. return;
  769. }
  770. msize = ntohs (msg->size);
  771. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  772. "Receiving %u bytes of result from fs service\n", msize);
  773. cm = (const struct PutMessage *) msg;
  774. process_result (sc, ntohl (cm->type),
  775. GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
  776. msize - sizeof (struct PutMessage));
  777. /* continue receiving */
  778. GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
  779. GNUNET_TIME_UNIT_FOREVER_REL);
  780. }
  781. /**
  782. * Schedule the transmission of the (next) search request
  783. * to the service.
  784. *
  785. * @param sc context for the search
  786. */
  787. static void
  788. schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
  789. /**
  790. * Closure for 'build_result_set'.
  791. */
  792. struct MessageBuilderContext
  793. {
  794. /**
  795. * How many entries can we store to xoff.
  796. */
  797. unsigned int put_cnt;
  798. /**
  799. * How many entries should we skip.
  800. */
  801. unsigned int skip_cnt;
  802. /**
  803. * Where to store the keys.
  804. */
  805. GNUNET_HashCode *xoff;
  806. /**
  807. * Search context we are iterating for.
  808. */
  809. struct GNUNET_FS_SearchContext *sc;
  810. /**
  811. * URI the search result must match, NULL for any
  812. */
  813. struct GNUNET_FS_Uri *uri;
  814. };
  815. /**
  816. * Iterating over the known results, pick those
  817. * matching the given result range and store
  818. * their keys at 'xoff'.
  819. *
  820. * @param cls the 'struct MessageBuilderContext'
  821. * @param key key for a result
  822. * @param value the search result
  823. * @return GNUNET_OK to continue iterating
  824. */
  825. static int
  826. build_result_set (void *cls, const GNUNET_HashCode * key, void *value)
  827. {
  828. struct MessageBuilderContext *mbc = cls;
  829. struct GNUNET_FS_SearchResult *sr = value;
  830. if ((mbc->uri != NULL) &&
  831. (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri, sr->uri)))
  832. return GNUNET_OK;
  833. if (mbc->skip_cnt > 0)
  834. {
  835. mbc->skip_cnt--;
  836. return GNUNET_OK;
  837. }
  838. if (mbc->put_cnt == 0)
  839. return GNUNET_SYSERR;
  840. mbc->sc->search_request_map_offset++;
  841. mbc->xoff[--mbc->put_cnt] = *key;
  842. return GNUNET_OK;
  843. }
  844. /**
  845. * Iterating over the known results, count those
  846. * matching the given result range and increment
  847. * put count for each.
  848. *
  849. * @param cls the 'struct MessageBuilderContext'
  850. * @param key key for a result
  851. * @param value the search result
  852. * @return GNUNET_OK to continue iterating
  853. */
  854. static int
  855. find_result_set (void *cls, const GNUNET_HashCode * key, void *value)
  856. {
  857. struct MessageBuilderContext *mbc = cls;
  858. struct GNUNET_FS_SearchResult *sr = value;
  859. if ((mbc->uri != NULL) &&
  860. (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri, sr->uri)))
  861. return GNUNET_OK;
  862. mbc->put_cnt++;
  863. return GNUNET_OK;
  864. }
  865. /**
  866. * We're ready to transmit the search request to the
  867. * file-sharing service. Do it.
  868. *
  869. * @param cls closure
  870. * @param size number of bytes available in buf
  871. * @param buf where the callee should write the message
  872. * @return number of bytes written to buf
  873. */
  874. static size_t
  875. transmit_search_request (void *cls, size_t size, void *buf)
  876. {
  877. struct GNUNET_FS_SearchContext *sc = cls;
  878. struct MessageBuilderContext mbc;
  879. size_t msize;
  880. struct SearchMessage *sm;
  881. const char *identifier;
  882. GNUNET_HashCode key;
  883. GNUNET_HashCode idh;
  884. unsigned int sqms;
  885. if (NULL == buf)
  886. {
  887. try_reconnect (sc);
  888. return 0;
  889. }
  890. mbc.sc = sc;
  891. mbc.skip_cnt = sc->search_request_map_offset;
  892. sm = buf;
  893. sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
  894. mbc.xoff = (GNUNET_HashCode *) & sm[1];
  895. if (GNUNET_FS_uri_test_ksk (sc->uri))
  896. {
  897. msize = sizeof (struct SearchMessage);
  898. GNUNET_assert (size >= msize);
  899. mbc.uri = NULL;
  900. mbc.put_cnt = 0;
  901. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  902. &find_result_set, &mbc);
  903. sqms = mbc.put_cnt;
  904. mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
  905. mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
  906. if (sc->search_request_map_offset < sqms)
  907. GNUNET_assert (mbc.put_cnt > 0);
  908. sm->header.size = htons (msize);
  909. if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
  910. sm->options = htonl (1);
  911. else
  912. sm->options = htonl (0);
  913. sm->type = htonl (GNUNET_BLOCK_TYPE_ANY);
  914. sm->anonymity_level = htonl (sc->anonymity);
  915. memset (&sm->target, 0, sizeof (GNUNET_HashCode));
  916. sm->query = sc->requests[sc->keyword_offset].query;
  917. msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
  918. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  919. &build_result_set, &mbc);
  920. sm->header.size = htons (msize);
  921. if (sqms != sc->search_request_map_offset)
  922. {
  923. /* more requesting to be done... */
  924. schedule_transmit_search_request (sc);
  925. return msize;
  926. }
  927. sc->keyword_offset++;
  928. if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
  929. {
  930. /* more requesting to be done... */
  931. schedule_transmit_search_request (sc);
  932. return msize;
  933. }
  934. }
  935. else
  936. {
  937. GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
  938. msize = sizeof (struct SearchMessage);
  939. GNUNET_assert (size >= msize);
  940. if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
  941. sm->options = htonl (1);
  942. else
  943. sm->options = htonl (0);
  944. sm->type = htonl (GNUNET_BLOCK_TYPE_FS_SBLOCK);
  945. sm->anonymity_level = htonl (sc->anonymity);
  946. sm->target = sc->uri->data.sks.namespace;
  947. identifier = sc->uri->data.sks.identifier;
  948. GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
  949. GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &idh);
  950. GNUNET_CRYPTO_hash_xor (&idh, &sm->target, &sm->query);
  951. mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
  952. sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
  953. mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
  954. mbc.uri = NULL;
  955. if (sc->search_request_map_offset < sqms)
  956. GNUNET_assert (mbc.put_cnt > 0);
  957. msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
  958. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  959. &build_result_set, &mbc);
  960. sm->header.size = htons (msize);
  961. if (sqms != sc->search_request_map_offset)
  962. {
  963. /* more requesting to be done... */
  964. schedule_transmit_search_request (sc);
  965. return msize;
  966. }
  967. }
  968. GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
  969. GNUNET_TIME_UNIT_FOREVER_REL);
  970. return msize;
  971. }
  972. /**
  973. * Schedule the transmission of the (next) search request
  974. * to the service.
  975. *
  976. * @param sc context for the search
  977. */
  978. static void
  979. schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
  980. {
  981. size_t size;
  982. unsigned int sqms;
  983. unsigned int fit;
  984. size = sizeof (struct SearchMessage);
  985. sqms =
  986. GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
  987. sc->search_request_map_offset;
  988. fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (GNUNET_HashCode);
  989. fit = GNUNET_MIN (fit, sqms);
  990. size += sizeof (GNUNET_HashCode) * fit;
  991. GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
  992. GNUNET_CONSTANTS_SERVICE_TIMEOUT,
  993. GNUNET_NO, &transmit_search_request, sc);
  994. }
  995. /**
  996. * Reconnect to the FS service and transmit
  997. * our queries NOW.
  998. *
  999. * @param cls our search context
  1000. * @param tc unused
  1001. */
  1002. static void
  1003. do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  1004. {
  1005. struct GNUNET_FS_SearchContext *sc = cls;
  1006. struct GNUNET_CLIENT_Connection *client;
  1007. sc->task = GNUNET_SCHEDULER_NO_TASK;
  1008. client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
  1009. if (NULL == client)
  1010. {
  1011. try_reconnect (sc);
  1012. return;
  1013. }
  1014. sc->client = client;
  1015. sc->search_request_map_offset = 0;
  1016. sc->keyword_offset = 0;
  1017. schedule_transmit_search_request (sc);
  1018. }
  1019. /**
  1020. * Shutdown any existing connection to the FS
  1021. * service and try to establish a fresh one
  1022. * (and then re-transmit our search request).
  1023. *
  1024. * @param sc the search to reconnec
  1025. */
  1026. static void
  1027. try_reconnect (struct GNUNET_FS_SearchContext *sc)
  1028. {
  1029. if (NULL != sc->client)
  1030. {
  1031. GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
  1032. sc->client = NULL;
  1033. }
  1034. sc->task =
  1035. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
  1036. sc);
  1037. }
  1038. /**
  1039. * Start search for content, internal API.
  1040. *
  1041. * @param h handle to the file sharing subsystem
  1042. * @param uri specifies the search parameters; can be
  1043. * a KSK URI or an SKS URI.
  1044. * @param anonymity desired level of anonymity
  1045. * @param options options for the search
  1046. * @param cctx initial value for the client context
  1047. * @param psearch parent search result (for namespace update searches)
  1048. * @return context that can be used to control the search
  1049. */
  1050. static struct GNUNET_FS_SearchContext *
  1051. search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
  1052. uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
  1053. void *cctx, struct GNUNET_FS_SearchResult *psearch)
  1054. {
  1055. struct GNUNET_FS_SearchContext *sc;
  1056. struct GNUNET_FS_ProgressInfo pi;
  1057. sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
  1058. sc->h = h;
  1059. sc->options = options;
  1060. sc->uri = GNUNET_FS_uri_dup (uri);
  1061. sc->anonymity = anonymity;
  1062. sc->start_time = GNUNET_TIME_absolute_get ();
  1063. if (psearch != NULL)
  1064. {
  1065. sc->psearch_result = psearch;
  1066. psearch->update_search = sc;
  1067. }
  1068. sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
  1069. sc->client_info = cctx;
  1070. if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
  1071. {
  1072. GNUNET_FS_uri_destroy (sc->uri);
  1073. GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
  1074. GNUNET_free (sc);
  1075. return NULL;
  1076. }
  1077. GNUNET_FS_search_sync_ (sc);
  1078. pi.status = GNUNET_FS_STATUS_SEARCH_START;
  1079. sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  1080. return sc;
  1081. }
  1082. /**
  1083. * Build the request and actually initiate the search using the
  1084. * GNUnet FS service.
  1085. *
  1086. * @param sc search context
  1087. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  1088. */
  1089. int
  1090. GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
  1091. {
  1092. unsigned int i;
  1093. const char *keyword;
  1094. GNUNET_HashCode hc;
  1095. struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
  1096. struct GNUNET_CRYPTO_RsaPrivateKey *pk;
  1097. GNUNET_assert (NULL == sc->client);
  1098. if (GNUNET_FS_uri_test_ksk (sc->uri))
  1099. {
  1100. GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
  1101. sc->requests =
  1102. GNUNET_malloc (sizeof (struct SearchRequestEntry) *
  1103. sc->uri->data.ksk.keywordCount);
  1104. for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
  1105. {
  1106. keyword = &sc->uri->data.ksk.keywords[i][1];
  1107. GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
  1108. pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
  1109. GNUNET_assert (pk != NULL);
  1110. GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
  1111. GNUNET_CRYPTO_rsa_key_free (pk);
  1112. GNUNET_CRYPTO_hash (&pub,
  1113. sizeof (struct
  1114. GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
  1115. &sc->requests[i].query);
  1116. sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
  1117. if (sc->requests[i].mandatory)
  1118. sc->mandatory_count++;
  1119. sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
  1120. GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].key);
  1121. }
  1122. }
  1123. sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
  1124. if (NULL == sc->client)
  1125. return GNUNET_SYSERR;
  1126. schedule_transmit_search_request (sc);
  1127. return GNUNET_OK;
  1128. }
  1129. /**
  1130. * Freeze probes for the given search result.
  1131. *
  1132. * @param cls the global FS handle
  1133. * @param key the key for the search result (unused)
  1134. * @param value the search result to free
  1135. * @return GNUNET_OK
  1136. */
  1137. static int
  1138. search_result_freeze_probes (void *cls, const GNUNET_HashCode * key,
  1139. void *value)
  1140. {
  1141. struct GNUNET_FS_SearchResult *sr = value;
  1142. if (sr->probe_ctx != NULL)
  1143. {
  1144. GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
  1145. sr->probe_ctx = NULL;
  1146. }
  1147. if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
  1148. {
  1149. GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
  1150. sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
  1151. }
  1152. if (sr->update_search != NULL)
  1153. GNUNET_FS_search_pause (sr->update_search);
  1154. return GNUNET_OK;
  1155. }
  1156. /**
  1157. * Resume probes for the given search result.
  1158. *
  1159. * @param cls the global FS handle
  1160. * @param key the key for the search result (unused)
  1161. * @param value the search result to free
  1162. * @return GNUNET_OK
  1163. */
  1164. static int
  1165. search_result_resume_probes (void *cls, const GNUNET_HashCode * key,
  1166. void *value)
  1167. {
  1168. struct GNUNET_FS_SearchResult *sr = value;
  1169. GNUNET_FS_search_start_probe_ (sr);
  1170. if (sr->update_search != NULL)
  1171. GNUNET_FS_search_continue (sr->update_search);
  1172. return GNUNET_OK;
  1173. }
  1174. /**
  1175. * Signal suspend and free the given search result.
  1176. *
  1177. * @param cls the global FS handle
  1178. * @param key the key for the search result (unused)
  1179. * @param value the search result to free
  1180. * @return GNUNET_OK
  1181. */
  1182. static int
  1183. search_result_suspend (void *cls, const GNUNET_HashCode * key, void *value)
  1184. {
  1185. struct GNUNET_FS_SearchContext *sc = cls;
  1186. struct GNUNET_FS_SearchResult *sr = value;
  1187. struct GNUNET_FS_ProgressInfo pi;
  1188. if (sr->download != NULL)
  1189. GNUNET_FS_download_signal_suspend_ (sr->download);
  1190. if (sr->update_search != NULL)
  1191. GNUNET_FS_search_signal_suspend_ (sr->update_search);
  1192. pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
  1193. pi.value.search.specifics.result_suspend.cctx = sr->client_info;
  1194. pi.value.search.specifics.result_suspend.meta = sr->meta;
  1195. pi.value.search.specifics.result_suspend.uri = sr->uri;
  1196. sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  1197. GNUNET_break (NULL == sr->client_info);
  1198. GNUNET_free_non_null (sr->serialization);
  1199. GNUNET_FS_uri_destroy (sr->uri);
  1200. GNUNET_CONTAINER_meta_data_destroy (sr->meta);
  1201. if (sr->probe_ctx != NULL)
  1202. GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
  1203. if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
  1204. GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
  1205. GNUNET_free (sr);
  1206. return GNUNET_OK;
  1207. }
  1208. /**
  1209. * Create SUSPEND event for the given search operation
  1210. * and then clean up our state (without stop signal).
  1211. *
  1212. * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
  1213. */
  1214. void
  1215. GNUNET_FS_search_signal_suspend_ (void *cls)
  1216. {
  1217. struct GNUNET_FS_SearchContext *sc = cls;
  1218. struct GNUNET_FS_ProgressInfo pi;
  1219. unsigned int i;
  1220. GNUNET_FS_end_top (sc->h, sc->top);
  1221. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  1222. &search_result_suspend, sc);
  1223. pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
  1224. sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  1225. GNUNET_break (NULL == sc->client_info);
  1226. if (sc->task != GNUNET_SCHEDULER_NO_TASK)
  1227. GNUNET_SCHEDULER_cancel (sc->task);
  1228. if (NULL != sc->client)
  1229. GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
  1230. GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
  1231. if (sc->requests != NULL)
  1232. {
  1233. GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
  1234. for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
  1235. GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
  1236. }
  1237. GNUNET_free_non_null (sc->requests);
  1238. GNUNET_free_non_null (sc->emsg);
  1239. GNUNET_FS_uri_destroy (sc->uri);
  1240. GNUNET_free_non_null (sc->serialization);
  1241. GNUNET_free (sc);
  1242. }
  1243. /**
  1244. * Start search for content.
  1245. *
  1246. * @param h handle to the file sharing subsystem
  1247. * @param uri specifies the search parameters; can be
  1248. * a KSK URI or an SKS URI.
  1249. * @param anonymity desired level of anonymity
  1250. * @param options options for the search
  1251. * @param cctx initial value for the client context
  1252. * @return context that can be used to control the search
  1253. */
  1254. struct GNUNET_FS_SearchContext *
  1255. GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
  1256. const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
  1257. enum GNUNET_FS_SearchOptions options, void *cctx)
  1258. {
  1259. struct GNUNET_FS_SearchContext *ret;
  1260. ret = search_start (h, uri, anonymity, options, cctx, NULL);
  1261. if (ret == NULL)
  1262. return NULL;
  1263. ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
  1264. return ret;
  1265. }
  1266. /**
  1267. * Pause search.
  1268. *
  1269. * @param sc context for the search that should be paused
  1270. */
  1271. void
  1272. GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
  1273. {
  1274. struct GNUNET_FS_ProgressInfo pi;
  1275. if (sc->task != GNUNET_SCHEDULER_NO_TASK)
  1276. GNUNET_SCHEDULER_cancel (sc->task);
  1277. sc->task = GNUNET_SCHEDULER_NO_TASK;
  1278. if (NULL != sc->client)
  1279. GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
  1280. sc->client = NULL;
  1281. GNUNET_FS_search_sync_ (sc);
  1282. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  1283. &search_result_freeze_probes, sc);
  1284. pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
  1285. sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  1286. }
  1287. /**
  1288. * Continue paused search.
  1289. *
  1290. * @param sc context for the search that should be resumed
  1291. */
  1292. void
  1293. GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
  1294. {
  1295. struct GNUNET_FS_ProgressInfo pi;
  1296. GNUNET_assert (sc->client == NULL);
  1297. GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
  1298. do_reconnect (sc, NULL);
  1299. GNUNET_FS_search_sync_ (sc);
  1300. pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
  1301. sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  1302. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  1303. &search_result_resume_probes, sc);
  1304. }
  1305. /**
  1306. * Free the given search result.
  1307. *
  1308. * @param cls the global FS handle
  1309. * @param key the key for the search result (unused)
  1310. * @param value the search result to free
  1311. * @return GNUNET_OK
  1312. */
  1313. static int
  1314. search_result_free (void *cls, const GNUNET_HashCode * key, void *value)
  1315. {
  1316. struct GNUNET_FS_SearchContext *sc = cls;
  1317. struct GNUNET_FS_SearchResult *sr = value;
  1318. struct GNUNET_FS_ProgressInfo pi;
  1319. if (NULL != sr->download)
  1320. {
  1321. sr->download->search = NULL;
  1322. sr->download->top =
  1323. GNUNET_FS_make_top (sr->download->h,
  1324. &GNUNET_FS_download_signal_suspend_, sr->download);
  1325. if (NULL != sr->download->serialization)
  1326. {
  1327. GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
  1328. sr->download->serialization);
  1329. GNUNET_free (sr->download->serialization);
  1330. sr->download->serialization = NULL;
  1331. }
  1332. pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
  1333. GNUNET_FS_download_make_status_ (&pi, sr->download);
  1334. GNUNET_FS_download_sync_ (sr->download);
  1335. sr->download = NULL;
  1336. }
  1337. if (NULL != sr->update_search)
  1338. {
  1339. GNUNET_FS_search_stop (sr->update_search);
  1340. GNUNET_assert (sr->update_search == NULL);
  1341. }
  1342. pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
  1343. pi.value.search.specifics.result_stopped.cctx = sr->client_info;
  1344. pi.value.search.specifics.result_stopped.meta = sr->meta;
  1345. pi.value.search.specifics.result_stopped.uri = sr->uri;
  1346. sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  1347. GNUNET_break (NULL == sr->client_info);
  1348. GNUNET_free_non_null (sr->serialization);
  1349. GNUNET_FS_uri_destroy (sr->uri);
  1350. GNUNET_CONTAINER_meta_data_destroy (sr->meta);
  1351. if (sr->probe_ctx != NULL)
  1352. GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
  1353. if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
  1354. GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
  1355. GNUNET_free (sr);
  1356. return GNUNET_OK;
  1357. }
  1358. /**
  1359. * Stop search for content.
  1360. *
  1361. * @param sc context for the search that should be stopped
  1362. */
  1363. void
  1364. GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
  1365. {
  1366. struct GNUNET_FS_ProgressInfo pi;
  1367. unsigned int i;
  1368. if (sc->top != NULL)
  1369. GNUNET_FS_end_top (sc->h, sc->top);
  1370. if (sc->psearch_result != NULL)
  1371. sc->psearch_result->update_search = NULL;
  1372. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  1373. &search_result_free, sc);
  1374. if (sc->serialization != NULL)
  1375. {
  1376. GNUNET_FS_remove_sync_file_ (sc->h,
  1377. (sc->psearch_result !=
  1378. NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
  1379. GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
  1380. sc->serialization);
  1381. GNUNET_FS_remove_sync_dir_ (sc->h,
  1382. (sc->psearch_result !=
  1383. NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
  1384. GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
  1385. sc->serialization);
  1386. GNUNET_free (sc->serialization);
  1387. }
  1388. pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
  1389. sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
  1390. GNUNET_break (NULL == sc->client_info);
  1391. if (sc->task != GNUNET_SCHEDULER_NO_TASK)
  1392. GNUNET_SCHEDULER_cancel (sc->task);
  1393. if (NULL != sc->client)
  1394. GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
  1395. GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
  1396. if (sc->requests != NULL)
  1397. {
  1398. GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
  1399. for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
  1400. GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
  1401. }
  1402. GNUNET_free_non_null (sc->requests);
  1403. GNUNET_free_non_null (sc->emsg);
  1404. GNUNET_FS_uri_destroy (sc->uri);
  1405. GNUNET_free (sc);
  1406. }
  1407. /* end of fs_search.c */