namestore_api.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2013, 2016 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 namestore/namestore_api.c
  18. * @brief API to access the NAMESTORE service
  19. * @author Martin Schanzenbach
  20. * @author Matthias Wachs
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_crypto_lib.h"
  26. #include "gnunet_constants.h"
  27. #include "gnunet_dnsparser_lib.h"
  28. #include "gnunet_arm_service.h"
  29. #include "gnunet_signatures.h"
  30. #include "gnunet_gns_service.h"
  31. #include "gnunet_namestore_service.h"
  32. #include "namestore.h"
  33. #define LOG(kind, ...) GNUNET_log_from (kind, "namestore-api", __VA_ARGS__)
  34. /**
  35. * We grant the namestore up to 1 minute of latency, if it is slower than
  36. * that, store queries will fail.
  37. */
  38. #define NAMESTORE_DELAY_TOLERANCE GNUNET_TIME_UNIT_MINUTES
  39. /**
  40. * An QueueEntry used to store information for a pending
  41. * NAMESTORE record operation
  42. */
  43. struct GNUNET_NAMESTORE_QueueEntry
  44. {
  45. /**
  46. * Kept in a DLL.
  47. */
  48. struct GNUNET_NAMESTORE_QueueEntry *next;
  49. /**
  50. * Kept in a DLL.
  51. */
  52. struct GNUNET_NAMESTORE_QueueEntry *prev;
  53. /**
  54. * Main handle to access the namestore.
  55. */
  56. struct GNUNET_NAMESTORE_Handle *h;
  57. /**
  58. * Continuation to call
  59. */
  60. GNUNET_NAMESTORE_ContinuationWithStatus cont;
  61. /**
  62. * Closure for @e cont.
  63. */
  64. void *cont_cls;
  65. /**
  66. * Function to call with the records we get back; or NULL.
  67. */
  68. GNUNET_NAMESTORE_RecordMonitor proc;
  69. /**
  70. * Closure for @e proc.
  71. */
  72. void *proc_cls;
  73. /**
  74. * Function to call on errors.
  75. */
  76. GNUNET_SCHEDULER_TaskCallback error_cb;
  77. /**
  78. * Closure for @e error_cb.
  79. */
  80. void *error_cb_cls;
  81. /**
  82. * Envelope of the message to send to the service, if not yet
  83. * sent.
  84. */
  85. struct GNUNET_MQ_Envelope *env;
  86. /**
  87. * Task scheduled to warn us if the namestore is way too slow.
  88. */
  89. struct GNUNET_SCHEDULER_Task *timeout_task;
  90. /**
  91. * The operation id this zone iteration operation has
  92. */
  93. uint32_t op_id;
  94. };
  95. /**
  96. * Handle for a zone iterator operation
  97. */
  98. struct GNUNET_NAMESTORE_ZoneIterator
  99. {
  100. /**
  101. * Kept in a DLL.
  102. */
  103. struct GNUNET_NAMESTORE_ZoneIterator *next;
  104. /**
  105. * Kept in a DLL.
  106. */
  107. struct GNUNET_NAMESTORE_ZoneIterator *prev;
  108. /**
  109. * Main handle to access the namestore.
  110. */
  111. struct GNUNET_NAMESTORE_Handle *h;
  112. /**
  113. * Function to call on completion.
  114. */
  115. GNUNET_SCHEDULER_TaskCallback finish_cb;
  116. /**
  117. * Closure for @e error_cb.
  118. */
  119. void *finish_cb_cls;
  120. /**
  121. * The continuation to call with the results
  122. */
  123. GNUNET_NAMESTORE_RecordMonitor proc;
  124. /**
  125. * Closure for @e proc.
  126. */
  127. void *proc_cls;
  128. /**
  129. * Function to call on errors.
  130. */
  131. GNUNET_SCHEDULER_TaskCallback error_cb;
  132. /**
  133. * Closure for @e error_cb.
  134. */
  135. void *error_cb_cls;
  136. /**
  137. * Envelope of the message to send to the service, if not yet
  138. * sent.
  139. */
  140. struct GNUNET_MQ_Envelope *env;
  141. /**
  142. * Private key of the zone.
  143. */
  144. struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
  145. /**
  146. * The operation id this zone iteration operation has
  147. */
  148. uint32_t op_id;
  149. };
  150. /**
  151. * Connection to the NAMESTORE service.
  152. */
  153. struct GNUNET_NAMESTORE_Handle
  154. {
  155. /**
  156. * Configuration to use.
  157. */
  158. const struct GNUNET_CONFIGURATION_Handle *cfg;
  159. /**
  160. * Connection to the service (if available).
  161. */
  162. struct GNUNET_MQ_Handle *mq;
  163. /**
  164. * Head of pending namestore queue entries
  165. */
  166. struct GNUNET_NAMESTORE_QueueEntry *op_head;
  167. /**
  168. * Tail of pending namestore queue entries
  169. */
  170. struct GNUNET_NAMESTORE_QueueEntry *op_tail;
  171. /**
  172. * Head of pending namestore zone iterator entries
  173. */
  174. struct GNUNET_NAMESTORE_ZoneIterator *z_head;
  175. /**
  176. * Tail of pending namestore zone iterator entries
  177. */
  178. struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
  179. /**
  180. * Reconnect task
  181. */
  182. struct GNUNET_SCHEDULER_Task *reconnect_task;
  183. /**
  184. * Delay introduced before we reconnect.
  185. */
  186. struct GNUNET_TIME_Relative reconnect_delay;
  187. /**
  188. * Should we reconnect to service due to some serious error?
  189. */
  190. int reconnect;
  191. /**
  192. * The last operation id used for a NAMESTORE operation
  193. */
  194. uint32_t last_op_id_used;
  195. };
  196. /**
  197. * Disconnect from service and then reconnect.
  198. *
  199. * @param h our handle
  200. */
  201. static void
  202. force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
  203. /**
  204. * Find the queue entry that matches the @a rid
  205. *
  206. * @param h namestore handle
  207. * @param rid id to look up
  208. * @return NULL if @a rid was not found
  209. */
  210. static struct GNUNET_NAMESTORE_QueueEntry *
  211. find_qe (struct GNUNET_NAMESTORE_Handle *h, uint32_t rid)
  212. {
  213. struct GNUNET_NAMESTORE_QueueEntry *qe;
  214. for (qe = h->op_head; qe != NULL; qe = qe->next)
  215. if (qe->op_id == rid)
  216. return qe;
  217. return NULL;
  218. }
  219. /**
  220. * Find the zone iteration entry that matches the @a rid
  221. *
  222. * @param h namestore handle
  223. * @param rid id to look up
  224. * @return NULL if @a rid was not found
  225. */
  226. static struct GNUNET_NAMESTORE_ZoneIterator *
  227. find_zi (struct GNUNET_NAMESTORE_Handle *h, uint32_t rid)
  228. {
  229. struct GNUNET_NAMESTORE_ZoneIterator *ze;
  230. for (ze = h->z_head; ze != NULL; ze = ze->next)
  231. if (ze->op_id == rid)
  232. return ze;
  233. return NULL;
  234. }
  235. /**
  236. * Free @a qe.
  237. *
  238. * @param qe entry to free
  239. */
  240. static void
  241. free_qe (struct GNUNET_NAMESTORE_QueueEntry *qe)
  242. {
  243. struct GNUNET_NAMESTORE_Handle *h = qe->h;
  244. GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, qe);
  245. if (NULL != qe->env)
  246. GNUNET_MQ_discard (qe->env);
  247. if (NULL != qe->timeout_task)
  248. GNUNET_SCHEDULER_cancel (qe->timeout_task);
  249. GNUNET_free (qe);
  250. }
  251. /**
  252. * Free @a ze.
  253. *
  254. * @param ze entry to free
  255. */
  256. static void
  257. free_ze (struct GNUNET_NAMESTORE_ZoneIterator *ze)
  258. {
  259. struct GNUNET_NAMESTORE_Handle *h = ze->h;
  260. GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, ze);
  261. if (NULL != ze->env)
  262. GNUNET_MQ_discard (ze->env);
  263. GNUNET_free (ze);
  264. }
  265. /**
  266. * Check that @a rd_buf of lenght @a rd_len contains
  267. * @a rd_count records.
  268. *
  269. * @param rd_len length of @a rd_buf
  270. * @param rd_buf buffer with serialized records
  271. * @param rd_count number of records expected
  272. * @return #GNUNET_OK if @a rd_buf is well-formed
  273. */
  274. static int
  275. check_rd (size_t rd_len, const void *rd_buf, unsigned int rd_count)
  276. {
  277. struct GNUNET_GNSRECORD_Data rd[rd_count];
  278. if (GNUNET_OK !=
  279. GNUNET_GNSRECORD_records_deserialize (rd_len, rd_buf, rd_count, rd))
  280. {
  281. GNUNET_break (0);
  282. return GNUNET_SYSERR;
  283. }
  284. return GNUNET_OK;
  285. }
  286. /**
  287. * Handle an incoming message of type
  288. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
  289. *
  290. * @param cls
  291. * @param msg the message we received
  292. */
  293. static void
  294. handle_record_store_response (void *cls,
  295. const struct RecordStoreResponseMessage *msg)
  296. {
  297. struct GNUNET_NAMESTORE_Handle *h = cls;
  298. struct GNUNET_NAMESTORE_QueueEntry *qe;
  299. int res;
  300. const char *emsg;
  301. qe = find_qe (h, ntohl (msg->gns_header.r_id));
  302. res = ntohl (msg->op_result);
  303. LOG (GNUNET_ERROR_TYPE_DEBUG,
  304. "Received RECORD_STORE_RESPONSE with result %d\n",
  305. res);
  306. /* TODO: add actual error message from namestore to response... */
  307. if (GNUNET_SYSERR == res)
  308. emsg = _ ("Namestore failed to store record\n");
  309. else
  310. emsg = NULL;
  311. if (NULL != qe->cont)
  312. qe->cont (qe->cont_cls, res, emsg);
  313. free_qe (qe);
  314. }
  315. /**
  316. * Check validity of an incoming message of type
  317. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
  318. *
  319. * @param cls
  320. * @param msg the message we received
  321. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  322. */
  323. static int
  324. check_lookup_result (void *cls, const struct LabelLookupResponseMessage *msg)
  325. {
  326. const char *name;
  327. size_t exp_msg_len;
  328. size_t msg_len;
  329. size_t name_len;
  330. size_t rd_len;
  331. (void) cls;
  332. rd_len = ntohs (msg->rd_len);
  333. msg_len = ntohs (msg->gns_header.header.size);
  334. name_len = ntohs (msg->name_len);
  335. exp_msg_len = sizeof (*msg) + name_len + rd_len;
  336. if (msg_len != exp_msg_len)
  337. {
  338. GNUNET_break (0);
  339. return GNUNET_SYSERR;
  340. }
  341. name = (const char *) &msg[1];
  342. if ((name_len > 0) && ('\0' != name[name_len - 1]))
  343. {
  344. GNUNET_break (0);
  345. return GNUNET_SYSERR;
  346. }
  347. if (GNUNET_NO == ntohs (msg->found))
  348. {
  349. if (0 != ntohs (msg->rd_count))
  350. {
  351. GNUNET_break (0);
  352. return GNUNET_SYSERR;
  353. }
  354. return GNUNET_OK;
  355. }
  356. return check_rd (rd_len, &name[name_len], ntohs (msg->rd_count));
  357. }
  358. /**
  359. * Handle an incoming message of type
  360. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
  361. *
  362. * @param cls
  363. * @param msg the message we received
  364. */
  365. static void
  366. handle_lookup_result (void *cls, const struct LabelLookupResponseMessage *msg)
  367. {
  368. struct GNUNET_NAMESTORE_Handle *h = cls;
  369. struct GNUNET_NAMESTORE_QueueEntry *qe;
  370. const char *name;
  371. const char *rd_tmp;
  372. size_t name_len;
  373. size_t rd_len;
  374. unsigned int rd_count;
  375. LOG (GNUNET_ERROR_TYPE_DEBUG, "Received RECORD_LOOKUP_RESULT\n");
  376. qe = find_qe (h, ntohl (msg->gns_header.r_id));
  377. if (NULL == qe)
  378. return;
  379. rd_len = ntohs (msg->rd_len);
  380. rd_count = ntohs (msg->rd_count);
  381. name_len = ntohs (msg->name_len);
  382. name = (const char *) &msg[1];
  383. if (GNUNET_NO == ntohs (msg->found))
  384. {
  385. /* label was not in namestore */
  386. if (NULL != qe->proc)
  387. qe->proc (qe->proc_cls, &msg->private_key, name, 0, NULL);
  388. free_qe (qe);
  389. return;
  390. }
  391. rd_tmp = &name[name_len];
  392. {
  393. struct GNUNET_GNSRECORD_Data rd[rd_count];
  394. GNUNET_assert (
  395. GNUNET_OK ==
  396. GNUNET_GNSRECORD_records_deserialize (rd_len, rd_tmp, rd_count, rd));
  397. if (0 == name_len)
  398. name = NULL;
  399. if (NULL != qe->proc)
  400. qe->proc (qe->proc_cls,
  401. &msg->private_key,
  402. name,
  403. rd_count,
  404. (rd_count > 0) ? rd : NULL);
  405. }
  406. free_qe (qe);
  407. }
  408. /**
  409. * Handle an incoming message of type
  410. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
  411. *
  412. * @param cls
  413. * @param msg the message we received
  414. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  415. */
  416. static int
  417. check_record_result (void *cls, const struct RecordResultMessage *msg)
  418. {
  419. static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
  420. const char *name;
  421. size_t msg_len;
  422. size_t name_len;
  423. size_t rd_len;
  424. (void) cls;
  425. rd_len = ntohs (msg->rd_len);
  426. msg_len = ntohs (msg->gns_header.header.size);
  427. name_len = ntohs (msg->name_len);
  428. if (0 != ntohs (msg->reserved))
  429. {
  430. GNUNET_break (0);
  431. return GNUNET_SYSERR;
  432. }
  433. if (msg_len != sizeof (struct RecordResultMessage) + name_len + rd_len)
  434. {
  435. GNUNET_break (0);
  436. return GNUNET_SYSERR;
  437. }
  438. name = (const char *) &msg[1];
  439. if ((0 == name_len) || ('\0' != name[name_len - 1]))
  440. {
  441. GNUNET_break (0);
  442. return GNUNET_SYSERR;
  443. }
  444. if (0 == GNUNET_memcmp (&msg->private_key, &priv_dummy))
  445. {
  446. GNUNET_break (0);
  447. return GNUNET_SYSERR;
  448. }
  449. return check_rd (rd_len, &name[name_len], ntohs (msg->rd_count));
  450. }
  451. /**
  452. * Handle an incoming message of type
  453. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
  454. *
  455. * @param cls
  456. * @param msg the message we received
  457. */
  458. static void
  459. handle_record_result (void *cls, const struct RecordResultMessage *msg)
  460. {
  461. struct GNUNET_NAMESTORE_Handle *h = cls;
  462. struct GNUNET_NAMESTORE_QueueEntry *qe;
  463. struct GNUNET_NAMESTORE_ZoneIterator *ze;
  464. const char *name;
  465. const char *rd_tmp;
  466. size_t name_len;
  467. size_t rd_len;
  468. unsigned int rd_count;
  469. LOG (GNUNET_ERROR_TYPE_DEBUG, "Received RECORD_RESULT\n");
  470. rd_len = ntohs (msg->rd_len);
  471. rd_count = ntohs (msg->rd_count);
  472. name_len = ntohs (msg->name_len);
  473. ze = find_zi (h, ntohl (msg->gns_header.r_id));
  474. qe = find_qe (h, ntohl (msg->gns_header.r_id));
  475. if ((NULL == ze) && (NULL == qe))
  476. return; /* rid not found */
  477. if ((NULL != ze) && (NULL != qe))
  478. {
  479. GNUNET_break (0); /* rid ambigous */
  480. force_reconnect (h);
  481. return;
  482. }
  483. name = (const char *) &msg[1];
  484. rd_tmp = &name[name_len];
  485. {
  486. struct GNUNET_GNSRECORD_Data rd[rd_count];
  487. GNUNET_assert (
  488. GNUNET_OK ==
  489. GNUNET_GNSRECORD_records_deserialize (rd_len, rd_tmp, rd_count, rd));
  490. if (0 == name_len)
  491. name = NULL;
  492. if (NULL != qe)
  493. {
  494. if (NULL != qe->proc)
  495. qe->proc (qe->proc_cls,
  496. &msg->private_key,
  497. name,
  498. rd_count,
  499. (rd_count > 0) ? rd : NULL);
  500. free_qe (qe);
  501. return;
  502. }
  503. if (NULL != ze)
  504. {
  505. if (NULL != ze->proc)
  506. ze->proc (ze->proc_cls, &msg->private_key, name, rd_count, rd);
  507. return;
  508. }
  509. }
  510. GNUNET_assert (0);
  511. }
  512. /**
  513. * Handle an incoming message of type
  514. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END
  515. *
  516. * @param cls
  517. * @param msg the message we received
  518. */
  519. static void
  520. handle_record_result_end (void *cls, const struct GNUNET_NAMESTORE_Header *msg)
  521. {
  522. struct GNUNET_NAMESTORE_Handle *h = cls;
  523. struct GNUNET_NAMESTORE_QueueEntry *qe;
  524. struct GNUNET_NAMESTORE_ZoneIterator *ze;
  525. LOG (GNUNET_ERROR_TYPE_DEBUG, "Received RECORD_RESULT_END\n");
  526. ze = find_zi (h, ntohl (msg->r_id));
  527. qe = find_qe (h, ntohl (msg->r_id));
  528. if ((NULL == ze) && (NULL == qe))
  529. return; /* rid not found */
  530. if ((NULL != ze) && (NULL != qe))
  531. {
  532. GNUNET_break (0); /* rid ambigous */
  533. force_reconnect (h);
  534. return;
  535. }
  536. LOG (GNUNET_ERROR_TYPE_DEBUG, "Zone iteration completed!\n");
  537. if (NULL == ze)
  538. {
  539. GNUNET_break (0);
  540. force_reconnect (h);
  541. return;
  542. }
  543. if (NULL != ze->finish_cb)
  544. ze->finish_cb (ze->finish_cb_cls);
  545. free_ze (ze);
  546. }
  547. /**
  548. * Handle an incoming message of type
  549. * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
  550. *
  551. * @param qe the respective entry in the message queue
  552. * @param msg the message we received
  553. * @return #GNUNET_OK on success, #GNUNET_SYSERR if message malformed
  554. */
  555. static int
  556. check_zone_to_name_response (void *cls,
  557. const struct ZoneToNameResponseMessage *msg)
  558. {
  559. size_t name_len;
  560. size_t rd_ser_len;
  561. const char *name_tmp;
  562. (void) cls;
  563. if (GNUNET_OK != ntohs (msg->res))
  564. return GNUNET_OK;
  565. name_len = ntohs (msg->name_len);
  566. rd_ser_len = ntohs (msg->rd_len);
  567. if (ntohs (msg->gns_header.header.size) !=
  568. sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len)
  569. {
  570. GNUNET_break (0);
  571. return GNUNET_SYSERR;
  572. }
  573. name_tmp = (const char *) &msg[1];
  574. if ((name_len > 0) && ('\0' != name_tmp[name_len - 1]))
  575. {
  576. GNUNET_break (0);
  577. return GNUNET_SYSERR;
  578. }
  579. return check_rd (rd_ser_len, &name_tmp[name_len], ntohs (msg->rd_count));
  580. }
  581. /**
  582. * Handle an incoming message of type
  583. * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
  584. *
  585. * @param cls
  586. * @param msg the message we received
  587. */
  588. static void
  589. handle_zone_to_name_response (void *cls,
  590. const struct ZoneToNameResponseMessage *msg)
  591. {
  592. struct GNUNET_NAMESTORE_Handle *h = cls;
  593. struct GNUNET_NAMESTORE_QueueEntry *qe;
  594. int res;
  595. size_t name_len;
  596. size_t rd_ser_len;
  597. unsigned int rd_count;
  598. const char *name_tmp;
  599. const char *rd_tmp;
  600. LOG (GNUNET_ERROR_TYPE_DEBUG, "Received ZONE_TO_NAME_RESPONSE\n");
  601. qe = find_qe (h, ntohl (msg->gns_header.r_id));
  602. res = ntohs (msg->res);
  603. switch (res)
  604. {
  605. case GNUNET_SYSERR:
  606. LOG (GNUNET_ERROR_TYPE_DEBUG,
  607. "An error occurred during zone to name operation\n");
  608. break;
  609. case GNUNET_NO:
  610. LOG (GNUNET_ERROR_TYPE_DEBUG,
  611. "Namestore has no result for zone to name mapping \n");
  612. if (NULL != qe->proc)
  613. qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
  614. free_qe (qe);
  615. return;
  616. case GNUNET_YES:
  617. LOG (GNUNET_ERROR_TYPE_DEBUG,
  618. "Namestore has result for zone to name mapping \n");
  619. name_len = ntohs (msg->name_len);
  620. rd_count = ntohs (msg->rd_count);
  621. rd_ser_len = ntohs (msg->rd_len);
  622. name_tmp = (const char *) &msg[1];
  623. rd_tmp = &name_tmp[name_len];
  624. {
  625. struct GNUNET_GNSRECORD_Data rd[rd_count];
  626. GNUNET_assert (GNUNET_OK ==
  627. GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
  628. rd_tmp,
  629. rd_count,
  630. rd));
  631. /* normal end, call continuation with result */
  632. if (NULL != qe->proc)
  633. qe->proc (qe->proc_cls, &msg->zone, name_tmp, rd_count, rd);
  634. /* return is important here: break would call continuation with error! */
  635. free_qe (qe);
  636. return;
  637. }
  638. default:
  639. GNUNET_break (0);
  640. force_reconnect (h);
  641. return;
  642. }
  643. /* error case, call continuation with error */
  644. if (NULL != qe->error_cb)
  645. qe->error_cb (qe->error_cb_cls);
  646. free_qe (qe);
  647. }
  648. /**
  649. * Generic error handler, called with the appropriate error code and
  650. * the same closure specified at the creation of the message queue.
  651. * Not every message queue implementation supports an error handler.
  652. *
  653. * @param cls closure with the `struct GNUNET_NAMESTORE_Handle *`
  654. * @param error error code
  655. */
  656. static void
  657. mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  658. {
  659. struct GNUNET_NAMESTORE_Handle *h = cls;
  660. (void) error;
  661. force_reconnect (h);
  662. }
  663. /**
  664. * Reconnect to namestore service.
  665. *
  666. * @param h the handle to the NAMESTORE service
  667. */
  668. static void
  669. reconnect (struct GNUNET_NAMESTORE_Handle *h)
  670. {
  671. struct GNUNET_MQ_MessageHandler handlers[] =
  672. {GNUNET_MQ_hd_fixed_size (record_store_response,
  673. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE,
  674. struct RecordStoreResponseMessage,
  675. h),
  676. GNUNET_MQ_hd_var_size (zone_to_name_response,
  677. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE,
  678. struct ZoneToNameResponseMessage,
  679. h),
  680. GNUNET_MQ_hd_var_size (record_result,
  681. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
  682. struct RecordResultMessage,
  683. h),
  684. GNUNET_MQ_hd_fixed_size (record_result_end,
  685. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END,
  686. struct GNUNET_NAMESTORE_Header,
  687. h),
  688. GNUNET_MQ_hd_var_size (lookup_result,
  689. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE,
  690. struct LabelLookupResponseMessage,
  691. h),
  692. GNUNET_MQ_handler_end ()};
  693. struct GNUNET_NAMESTORE_ZoneIterator *it;
  694. struct GNUNET_NAMESTORE_QueueEntry *qe;
  695. GNUNET_assert (NULL == h->mq);
  696. h->mq =
  697. GNUNET_CLIENT_connect (h->cfg, "namestore", handlers, &mq_error_handler, h);
  698. if (NULL == h->mq)
  699. return;
  700. /* re-transmit pending requests that waited for a reconnect... */
  701. for (it = h->z_head; NULL != it; it = it->next)
  702. {
  703. GNUNET_MQ_send (h->mq, it->env);
  704. it->env = NULL;
  705. }
  706. for (qe = h->op_head; NULL != qe; qe = qe->next)
  707. {
  708. GNUNET_MQ_send (h->mq, qe->env);
  709. qe->env = NULL;
  710. }
  711. }
  712. /**
  713. * Re-establish the connection to the service.
  714. *
  715. * @param cls handle to use to re-connect.
  716. */
  717. static void
  718. reconnect_task (void *cls)
  719. {
  720. struct GNUNET_NAMESTORE_Handle *h = cls;
  721. h->reconnect_task = NULL;
  722. reconnect (h);
  723. }
  724. /**
  725. * Disconnect from service and then reconnect.
  726. *
  727. * @param h our handle
  728. */
  729. static void
  730. force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
  731. {
  732. struct GNUNET_NAMESTORE_ZoneIterator *ze;
  733. struct GNUNET_NAMESTORE_QueueEntry *qe;
  734. GNUNET_MQ_destroy (h->mq);
  735. h->mq = NULL;
  736. while (NULL != (ze = h->z_head))
  737. {
  738. if (NULL != ze->error_cb)
  739. ze->error_cb (ze->error_cb_cls);
  740. free_ze (ze);
  741. }
  742. while (NULL != (qe = h->op_head))
  743. {
  744. if (NULL != qe->error_cb)
  745. qe->error_cb (qe->error_cb_cls);
  746. if (NULL != qe->cont)
  747. qe->cont (qe->cont_cls,
  748. GNUNET_SYSERR,
  749. "failure in communication with namestore service");
  750. free_qe (qe);
  751. }
  752. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting to namestore\n");
  753. h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
  754. h->reconnect_task =
  755. GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect_task, h);
  756. }
  757. /**
  758. * Get a fresh operation id to distinguish between namestore requests
  759. *
  760. * @param h the namestore handle
  761. * @return next operation id to use
  762. */
  763. static uint32_t
  764. get_op_id (struct GNUNET_NAMESTORE_Handle *h)
  765. {
  766. return h->last_op_id_used++;
  767. }
  768. /**
  769. * Initialize the connection with the NAMESTORE service.
  770. *
  771. * @param cfg configuration to use
  772. * @return handle to the GNS service, or NULL on error
  773. */
  774. struct GNUNET_NAMESTORE_Handle *
  775. GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  776. {
  777. struct GNUNET_NAMESTORE_Handle *h;
  778. h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
  779. h->cfg = cfg;
  780. reconnect (h);
  781. if (NULL == h->mq)
  782. {
  783. GNUNET_free (h);
  784. return NULL;
  785. }
  786. return h;
  787. }
  788. /**
  789. * Disconnect from the namestore service (and free associated
  790. * resources).
  791. *
  792. * @param h handle to the namestore
  793. */
  794. void
  795. GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
  796. {
  797. struct GNUNET_NAMESTORE_QueueEntry *q;
  798. struct GNUNET_NAMESTORE_ZoneIterator *z;
  799. LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
  800. GNUNET_break (NULL == h->op_head);
  801. while (NULL != (q = h->op_head))
  802. {
  803. GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
  804. GNUNET_free (q);
  805. }
  806. GNUNET_break (NULL == h->z_head);
  807. while (NULL != (z = h->z_head))
  808. {
  809. GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
  810. GNUNET_free (z);
  811. }
  812. if (NULL != h->mq)
  813. {
  814. GNUNET_MQ_destroy (h->mq);
  815. h->mq = NULL;
  816. }
  817. if (NULL != h->reconnect_task)
  818. {
  819. GNUNET_SCHEDULER_cancel (h->reconnect_task);
  820. h->reconnect_task = NULL;
  821. }
  822. GNUNET_free (h);
  823. }
  824. /**
  825. * Task launched to warn the user that the namestore is
  826. * excessively slow and that a query was thus dropped.
  827. *
  828. * @param cls a `struct GNUNET_NAMESTORE_QueueEntry *`
  829. */
  830. static void
  831. warn_delay (void *cls)
  832. {
  833. struct GNUNET_NAMESTORE_QueueEntry *qe = cls;
  834. qe->timeout_task = NULL;
  835. LOG (GNUNET_ERROR_TYPE_WARNING,
  836. "Did not receive response from namestore after %s!\n",
  837. GNUNET_STRINGS_relative_time_to_string (NAMESTORE_DELAY_TOLERANCE,
  838. GNUNET_YES));
  839. if (NULL != qe->cont)
  840. {
  841. qe->cont (qe->cont_cls, GNUNET_SYSERR, "timeout");
  842. qe->cont = NULL;
  843. }
  844. GNUNET_NAMESTORE_cancel (qe);
  845. }
  846. /**
  847. * Store an item in the namestore. If the item is already present,
  848. * it is replaced with the new record. Use an empty array to
  849. * remove all records under the given name.
  850. *
  851. * @param h handle to the namestore
  852. * @param pkey private key of the zone
  853. * @param label name that is being mapped (at most 255 characters long)
  854. * @param rd_count number of records in the @a rd array
  855. * @param rd array of records with data to store
  856. * @param cont continuation to call when done
  857. * @param cont_cls closure for @a cont
  858. * @return handle to abort the request
  859. */
  860. struct GNUNET_NAMESTORE_QueueEntry *
  861. GNUNET_NAMESTORE_records_store (
  862. struct GNUNET_NAMESTORE_Handle *h,
  863. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  864. const char *label,
  865. unsigned int rd_count,
  866. const struct GNUNET_GNSRECORD_Data *rd,
  867. GNUNET_NAMESTORE_ContinuationWithStatus cont,
  868. void *cont_cls)
  869. {
  870. struct GNUNET_NAMESTORE_QueueEntry *qe;
  871. struct GNUNET_MQ_Envelope *env;
  872. char *name_tmp;
  873. char *rd_ser;
  874. ssize_t rd_ser_len;
  875. size_t name_len;
  876. uint32_t rid;
  877. struct RecordStoreMessage *msg;
  878. ssize_t sret;
  879. name_len = strlen (label) + 1;
  880. if (name_len > MAX_NAME_LEN)
  881. {
  882. GNUNET_break (0);
  883. return NULL;
  884. }
  885. rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
  886. if (rd_ser_len < 0)
  887. {
  888. GNUNET_break (0);
  889. return NULL;
  890. }
  891. if (rd_ser_len > UINT16_MAX)
  892. {
  893. GNUNET_break (0);
  894. return NULL;
  895. }
  896. rid = get_op_id (h);
  897. qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
  898. qe->h = h;
  899. qe->cont = cont;
  900. qe->cont_cls = cont_cls;
  901. qe->op_id = rid;
  902. GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
  903. /* setup msg */
  904. env = GNUNET_MQ_msg_extra (msg,
  905. name_len + rd_ser_len,
  906. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
  907. msg->gns_header.r_id = htonl (rid);
  908. msg->name_len = htons (name_len);
  909. msg->rd_count = htons (rd_count);
  910. msg->rd_len = htons (rd_ser_len);
  911. msg->reserved = htons (0);
  912. msg->private_key = *pkey;
  913. name_tmp = (char *) &msg[1];
  914. GNUNET_memcpy (name_tmp, label, name_len);
  915. rd_ser = &name_tmp[name_len];
  916. sret = GNUNET_GNSRECORD_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
  917. if ((0 > sret) || (sret != rd_ser_len))
  918. {
  919. GNUNET_break (0);
  920. GNUNET_free (env);
  921. return NULL;
  922. }
  923. GNUNET_assert (rd_ser_len == sret);
  924. LOG (GNUNET_ERROR_TYPE_DEBUG,
  925. "Sending NAMESTORE_RECORD_STORE message for name `%s' with %u records\n",
  926. label,
  927. rd_count);
  928. qe->timeout_task =
  929. GNUNET_SCHEDULER_add_delayed (NAMESTORE_DELAY_TOLERANCE, &warn_delay, qe);
  930. if (NULL == h->mq)
  931. {
  932. qe->env = env;
  933. LOG (GNUNET_ERROR_TYPE_WARNING,
  934. "Delaying NAMESTORE_RECORD_STORE message as namestore is not ready!\n");
  935. }
  936. else
  937. {
  938. GNUNET_MQ_send (h->mq, env);
  939. }
  940. return qe;
  941. }
  942. /**
  943. * Set the desired nick name for a zone
  944. *
  945. * @param h handle to the namestore
  946. * @param pkey private key of the zone
  947. * @param nick the nick name to set
  948. * @param cont continuation to call when done
  949. * @param cont_cls closure for @a cont
  950. * @return handle to abort the request
  951. */
  952. struct GNUNET_NAMESTORE_QueueEntry *
  953. GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
  954. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  955. const char *nick,
  956. GNUNET_NAMESTORE_ContinuationWithStatus cont,
  957. void *cont_cls)
  958. {
  959. struct GNUNET_GNSRECORD_Data rd;
  960. if (NULL == h->mq)
  961. return NULL;
  962. memset (&rd, 0, sizeof (rd));
  963. rd.data = nick;
  964. rd.data_size = strlen (nick) + 1;
  965. rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
  966. rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
  967. rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
  968. return GNUNET_NAMESTORE_records_store (h,
  969. pkey,
  970. GNUNET_GNS_EMPTY_LABEL_AT,
  971. 1,
  972. &rd,
  973. cont,
  974. cont_cls);
  975. }
  976. /**
  977. * Lookup an item in the namestore.
  978. *
  979. * @param h handle to the namestore
  980. * @param pkey private key of the zone
  981. * @param label name that is being mapped (at most 255 characters long)
  982. * @param error_cb function to call on error (i.e. disconnect)
  983. * @param error_cb_cls closure for @a error_cb
  984. * @param rm function to call with the result (with 0 records if we don't have that label)
  985. * @param rm_cls closure for @a rm
  986. * @return handle to abort the request
  987. */
  988. struct GNUNET_NAMESTORE_QueueEntry *
  989. GNUNET_NAMESTORE_records_lookup (
  990. struct GNUNET_NAMESTORE_Handle *h,
  991. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  992. const char *label,
  993. GNUNET_SCHEDULER_TaskCallback error_cb,
  994. void *error_cb_cls,
  995. GNUNET_NAMESTORE_RecordMonitor rm,
  996. void *rm_cls)
  997. {
  998. struct GNUNET_NAMESTORE_QueueEntry *qe;
  999. struct GNUNET_MQ_Envelope *env;
  1000. struct LabelLookupMessage *msg;
  1001. size_t label_len;
  1002. if (1 == (label_len = strlen (label) + 1))
  1003. {
  1004. GNUNET_break (0);
  1005. return NULL;
  1006. }
  1007. qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
  1008. qe->h = h;
  1009. qe->error_cb = error_cb;
  1010. qe->error_cb_cls = error_cb_cls;
  1011. qe->proc = rm;
  1012. qe->proc_cls = rm_cls;
  1013. qe->op_id = get_op_id (h);
  1014. GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
  1015. env = GNUNET_MQ_msg_extra (msg,
  1016. label_len,
  1017. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
  1018. msg->gns_header.r_id = htonl (qe->op_id);
  1019. msg->zone = *pkey;
  1020. msg->label_len = htonl (label_len);
  1021. GNUNET_memcpy (&msg[1], label, label_len);
  1022. if (NULL == h->mq)
  1023. qe->env = env;
  1024. else
  1025. GNUNET_MQ_send (h->mq, env);
  1026. return qe;
  1027. }
  1028. /**
  1029. * Look for an existing PKEY delegation record for a given public key.
  1030. * Returns at most one result to the processor.
  1031. *
  1032. * @param h handle to the namestore
  1033. * @param zone public key of the zone to look up in, never NULL
  1034. * @param value_zone public key of the target zone (value), never NULL
  1035. * @param error_cb function to call on error (i.e. disconnect)
  1036. * @param error_cb_cls closure for @a error_cb
  1037. * @param proc function to call on the matching records, or with
  1038. * NULL (rd_count == 0) if there are no matching records
  1039. * @param proc_cls closure for @a proc
  1040. * @return a handle that can be used to
  1041. * cancel
  1042. */
  1043. struct GNUNET_NAMESTORE_QueueEntry *
  1044. GNUNET_NAMESTORE_zone_to_name (
  1045. struct GNUNET_NAMESTORE_Handle *h,
  1046. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  1047. const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
  1048. GNUNET_SCHEDULER_TaskCallback error_cb,
  1049. void *error_cb_cls,
  1050. GNUNET_NAMESTORE_RecordMonitor proc,
  1051. void *proc_cls)
  1052. {
  1053. struct GNUNET_NAMESTORE_QueueEntry *qe;
  1054. struct GNUNET_MQ_Envelope *env;
  1055. struct ZoneToNameMessage *msg;
  1056. uint32_t rid;
  1057. rid = get_op_id (h);
  1058. qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
  1059. qe->h = h;
  1060. qe->error_cb = error_cb;
  1061. qe->error_cb_cls = error_cb_cls;
  1062. qe->proc = proc;
  1063. qe->proc_cls = proc_cls;
  1064. qe->op_id = rid;
  1065. GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
  1066. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
  1067. msg->gns_header.r_id = htonl (rid);
  1068. msg->zone = *zone;
  1069. msg->value_zone = *value_zone;
  1070. if (NULL == h->mq)
  1071. qe->env = env;
  1072. else
  1073. GNUNET_MQ_send (h->mq, env);
  1074. return qe;
  1075. }
  1076. /**
  1077. * Starts a new zone iteration (used to periodically PUT all of our
  1078. * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
  1079. * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
  1080. * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
  1081. * immediately, and then again after
  1082. * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
  1083. *
  1084. * @param h handle to the namestore
  1085. * @param zone zone to access, NULL for all zones
  1086. * @param error_cb function to call on error (i.e. disconnect)
  1087. * @param error_cb_cls closure for @a error_cb
  1088. * @param proc function to call on each name from the zone; it
  1089. * will be called repeatedly with a value (if available)
  1090. * @param proc_cls closure for @a proc
  1091. * @param finish_cb function to call on completion
  1092. * @param finish_cb_cls closure for @a finish_cb
  1093. * @return an iterator handle to use for iteration
  1094. */
  1095. struct GNUNET_NAMESTORE_ZoneIterator *
  1096. GNUNET_NAMESTORE_zone_iteration_start (
  1097. struct GNUNET_NAMESTORE_Handle *h,
  1098. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  1099. GNUNET_SCHEDULER_TaskCallback error_cb,
  1100. void *error_cb_cls,
  1101. GNUNET_NAMESTORE_RecordMonitor proc,
  1102. void *proc_cls,
  1103. GNUNET_SCHEDULER_TaskCallback finish_cb,
  1104. void *finish_cb_cls)
  1105. {
  1106. struct GNUNET_NAMESTORE_ZoneIterator *it;
  1107. struct GNUNET_MQ_Envelope *env;
  1108. struct ZoneIterationStartMessage *msg;
  1109. uint32_t rid;
  1110. LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ZONE_ITERATION_START message\n");
  1111. rid = get_op_id (h);
  1112. it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
  1113. it->h = h;
  1114. it->error_cb = error_cb;
  1115. it->error_cb_cls = error_cb_cls;
  1116. it->finish_cb = finish_cb;
  1117. it->finish_cb_cls = finish_cb_cls;
  1118. it->proc = proc;
  1119. it->proc_cls = proc_cls;
  1120. it->op_id = rid;
  1121. if (NULL != zone)
  1122. it->zone = *zone;
  1123. GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
  1124. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
  1125. msg->gns_header.r_id = htonl (rid);
  1126. if (NULL != zone)
  1127. msg->zone = *zone;
  1128. if (NULL == h->mq)
  1129. it->env = env;
  1130. else
  1131. GNUNET_MQ_send (h->mq, env);
  1132. return it;
  1133. }
  1134. /**
  1135. * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
  1136. * for the next record.
  1137. *
  1138. * @param it the iterator
  1139. * @param limit number of records to return to the iterator in one shot
  1140. * (before #GNUNET_NAMESTORE_zone_iterator_next is to be called again)
  1141. */
  1142. void
  1143. GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it,
  1144. uint64_t limit)
  1145. {
  1146. struct GNUNET_NAMESTORE_Handle *h = it->h;
  1147. struct ZoneIterationNextMessage *msg;
  1148. struct GNUNET_MQ_Envelope *env;
  1149. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1150. "Sending ZONE_ITERATION_NEXT message with limit %llu\n",
  1151. (unsigned long long) limit);
  1152. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
  1153. msg->gns_header.r_id = htonl (it->op_id);
  1154. msg->limit = GNUNET_htonll (limit);
  1155. GNUNET_MQ_send (h->mq, env);
  1156. }
  1157. /**
  1158. * Stops iteration and releases the namestore handle for further calls.
  1159. *
  1160. * @param it the iterator
  1161. */
  1162. void
  1163. GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
  1164. {
  1165. struct GNUNET_NAMESTORE_Handle *h = it->h;
  1166. struct GNUNET_MQ_Envelope *env;
  1167. struct ZoneIterationStopMessage *msg;
  1168. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending ZONE_ITERATION_STOP message\n");
  1169. if (NULL != h->mq)
  1170. {
  1171. env =
  1172. GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
  1173. msg->gns_header.r_id = htonl (it->op_id);
  1174. GNUNET_MQ_send (h->mq, env);
  1175. }
  1176. free_ze (it);
  1177. }
  1178. /**
  1179. * Cancel a namestore operation. The final callback from the
  1180. * operation must not have been done yet.
  1181. *
  1182. * @param qe operation to cancel
  1183. */
  1184. void
  1185. GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
  1186. {
  1187. free_qe (qe);
  1188. }
  1189. /* end of namestore_api.c */