namestore_api.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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,
  212. uint32_t rid)
  213. {
  214. struct GNUNET_NAMESTORE_QueueEntry *qe;
  215. for (qe = h->op_head; qe != NULL; qe = qe->next)
  216. if (qe->op_id == rid)
  217. return qe;
  218. return NULL;
  219. }
  220. /**
  221. * Find the zone iteration entry that matches the @a rid
  222. *
  223. * @param h namestore handle
  224. * @param rid id to look up
  225. * @return NULL if @a rid was not found
  226. */
  227. static struct GNUNET_NAMESTORE_ZoneIterator *
  228. find_zi (struct GNUNET_NAMESTORE_Handle *h,
  229. uint32_t rid)
  230. {
  231. struct GNUNET_NAMESTORE_ZoneIterator *ze;
  232. for (ze = h->z_head; ze != NULL; ze = ze->next)
  233. if (ze->op_id == rid)
  234. return ze;
  235. return NULL;
  236. }
  237. /**
  238. * Free @a qe.
  239. *
  240. * @param qe entry to free
  241. */
  242. static void
  243. free_qe (struct GNUNET_NAMESTORE_QueueEntry *qe)
  244. {
  245. struct GNUNET_NAMESTORE_Handle *h = qe->h;
  246. GNUNET_CONTAINER_DLL_remove (h->op_head,
  247. h->op_tail,
  248. qe);
  249. if (NULL != qe->env)
  250. GNUNET_MQ_discard (qe->env);
  251. if (NULL != qe->timeout_task)
  252. GNUNET_SCHEDULER_cancel (qe->timeout_task);
  253. GNUNET_free (qe);
  254. }
  255. /**
  256. * Free @a ze.
  257. *
  258. * @param ze entry to free
  259. */
  260. static void
  261. free_ze (struct GNUNET_NAMESTORE_ZoneIterator *ze)
  262. {
  263. struct GNUNET_NAMESTORE_Handle *h = ze->h;
  264. GNUNET_CONTAINER_DLL_remove (h->z_head,
  265. h->z_tail,
  266. ze);
  267. if (NULL != ze->env)
  268. GNUNET_MQ_discard (ze->env);
  269. GNUNET_free (ze);
  270. }
  271. /**
  272. * Check that @a rd_buf of lenght @a rd_len contains
  273. * @a rd_count records.
  274. *
  275. * @param rd_len length of @a rd_buf
  276. * @param rd_buf buffer with serialized records
  277. * @param rd_count number of records expected
  278. * @return #GNUNET_OK if @a rd_buf is well-formed
  279. */
  280. static int
  281. check_rd (size_t rd_len,
  282. const void *rd_buf,
  283. unsigned int rd_count)
  284. {
  285. struct GNUNET_GNSRECORD_Data rd[rd_count];
  286. if (GNUNET_OK !=
  287. GNUNET_GNSRECORD_records_deserialize (rd_len,
  288. rd_buf,
  289. rd_count,
  290. rd))
  291. {
  292. GNUNET_break (0);
  293. return GNUNET_SYSERR;
  294. }
  295. return GNUNET_OK;
  296. }
  297. /**
  298. * Handle an incoming message of type
  299. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
  300. *
  301. * @param cls
  302. * @param msg the message we received
  303. */
  304. static void
  305. handle_record_store_response (void *cls,
  306. const struct RecordStoreResponseMessage *msg)
  307. {
  308. struct GNUNET_NAMESTORE_Handle *h = cls;
  309. struct GNUNET_NAMESTORE_QueueEntry *qe;
  310. int res;
  311. const char *emsg;
  312. qe = find_qe (h,
  313. ntohl (msg->gns_header.r_id));
  314. res = ntohl (msg->op_result);
  315. LOG (GNUNET_ERROR_TYPE_DEBUG,
  316. "Received RECORD_STORE_RESPONSE with result %d\n",
  317. res);
  318. /* TODO: add actual error message from namestore to response... */
  319. if (GNUNET_SYSERR == res)
  320. emsg = _("Namestore failed to store record\n");
  321. else
  322. emsg = NULL;
  323. if (NULL != qe->cont)
  324. qe->cont (qe->cont_cls,
  325. res,
  326. emsg);
  327. free_qe (qe);
  328. }
  329. /**
  330. * Check validity of an incoming message of type
  331. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
  332. *
  333. * @param cls
  334. * @param msg the message we received
  335. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  336. */
  337. static int
  338. check_lookup_result (void *cls,
  339. const struct LabelLookupResponseMessage *msg)
  340. {
  341. const char *name;
  342. size_t exp_msg_len;
  343. size_t msg_len;
  344. size_t name_len;
  345. size_t rd_len;
  346. (void) cls;
  347. rd_len = ntohs (msg->rd_len);
  348. msg_len = ntohs (msg->gns_header.header.size);
  349. name_len = ntohs (msg->name_len);
  350. exp_msg_len = sizeof (*msg) + name_len + rd_len;
  351. if (msg_len != exp_msg_len)
  352. {
  353. GNUNET_break (0);
  354. return GNUNET_SYSERR;
  355. }
  356. name = (const char *) &msg[1];
  357. if ( (name_len > 0) &&
  358. ('\0' != name[name_len -1]) )
  359. {
  360. GNUNET_break (0);
  361. return GNUNET_SYSERR;
  362. }
  363. if (GNUNET_NO == ntohs (msg->found))
  364. {
  365. if (0 != ntohs (msg->rd_count))
  366. {
  367. GNUNET_break (0);
  368. return GNUNET_SYSERR;
  369. }
  370. return GNUNET_OK;
  371. }
  372. return check_rd (rd_len,
  373. &name[name_len],
  374. ntohs (msg->rd_count));
  375. }
  376. /**
  377. * Handle an incoming message of type
  378. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
  379. *
  380. * @param cls
  381. * @param msg the message we received
  382. */
  383. static void
  384. handle_lookup_result (void *cls,
  385. const struct LabelLookupResponseMessage *msg)
  386. {
  387. struct GNUNET_NAMESTORE_Handle *h = cls;
  388. struct GNUNET_NAMESTORE_QueueEntry *qe;
  389. const char *name;
  390. const char *rd_tmp;
  391. size_t name_len;
  392. size_t rd_len;
  393. unsigned int rd_count;
  394. LOG (GNUNET_ERROR_TYPE_DEBUG,
  395. "Received RECORD_LOOKUP_RESULT\n");
  396. qe = find_qe (h,
  397. ntohl (msg->gns_header.r_id));
  398. if (NULL == qe)
  399. return;
  400. rd_len = ntohs (msg->rd_len);
  401. rd_count = ntohs (msg->rd_count);
  402. name_len = ntohs (msg->name_len);
  403. name = (const char *) &msg[1];
  404. if (GNUNET_NO == ntohs (msg->found))
  405. {
  406. /* label was not in namestore */
  407. if (NULL != qe->proc)
  408. qe->proc (qe->proc_cls,
  409. &msg->private_key,
  410. name,
  411. 0,
  412. NULL);
  413. free_qe (qe);
  414. return;
  415. }
  416. rd_tmp = &name[name_len];
  417. {
  418. struct GNUNET_GNSRECORD_Data rd[rd_count];
  419. GNUNET_assert (GNUNET_OK ==
  420. GNUNET_GNSRECORD_records_deserialize (rd_len,
  421. rd_tmp,
  422. rd_count,
  423. rd));
  424. if (0 == name_len)
  425. name = NULL;
  426. if (NULL != qe->proc)
  427. qe->proc (qe->proc_cls,
  428. &msg->private_key,
  429. name,
  430. rd_count,
  431. (rd_count > 0) ? rd : NULL);
  432. }
  433. free_qe (qe);
  434. }
  435. /**
  436. * Handle an incoming message of type
  437. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
  438. *
  439. * @param cls
  440. * @param msg the message we received
  441. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  442. */
  443. static int
  444. check_record_result (void *cls,
  445. const struct RecordResultMessage *msg)
  446. {
  447. static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
  448. const char *name;
  449. size_t msg_len;
  450. size_t name_len;
  451. size_t rd_len;
  452. (void) cls;
  453. rd_len = ntohs (msg->rd_len);
  454. msg_len = ntohs (msg->gns_header.header.size);
  455. name_len = ntohs (msg->name_len);
  456. if (0 != ntohs (msg->reserved))
  457. {
  458. GNUNET_break (0);
  459. return GNUNET_SYSERR;
  460. }
  461. if (msg_len != sizeof (struct RecordResultMessage) + name_len + rd_len)
  462. {
  463. GNUNET_break (0);
  464. return GNUNET_SYSERR;
  465. }
  466. name = (const char *) &msg[1];
  467. if ( (0 == name_len) ||
  468. ('\0' != name[name_len -1]) )
  469. {
  470. GNUNET_break (0);
  471. return GNUNET_SYSERR;
  472. }
  473. if (0 == memcmp (&msg->private_key,
  474. &priv_dummy,
  475. sizeof (priv_dummy)) )
  476. {
  477. GNUNET_break (0);
  478. return GNUNET_SYSERR;
  479. }
  480. return check_rd (rd_len,
  481. &name[name_len],
  482. ntohs (msg->rd_count));
  483. }
  484. /**
  485. * Handle an incoming message of type
  486. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
  487. *
  488. * @param cls
  489. * @param msg the message we received
  490. */
  491. static void
  492. handle_record_result (void *cls,
  493. const struct RecordResultMessage *msg)
  494. {
  495. struct GNUNET_NAMESTORE_Handle *h = cls;
  496. struct GNUNET_NAMESTORE_QueueEntry *qe;
  497. struct GNUNET_NAMESTORE_ZoneIterator *ze;
  498. const char *name;
  499. const char *rd_tmp;
  500. size_t name_len;
  501. size_t rd_len;
  502. unsigned int rd_count;
  503. LOG (GNUNET_ERROR_TYPE_DEBUG,
  504. "Received RECORD_RESULT\n");
  505. rd_len = ntohs (msg->rd_len);
  506. rd_count = ntohs (msg->rd_count);
  507. name_len = ntohs (msg->name_len);
  508. ze = find_zi (h,
  509. ntohl (msg->gns_header.r_id));
  510. qe = find_qe (h,
  511. ntohl (msg->gns_header.r_id));
  512. if ( (NULL == ze) &&
  513. (NULL == qe) )
  514. return; /* rid not found */
  515. if ( (NULL != ze) &&
  516. (NULL != qe) )
  517. {
  518. GNUNET_break (0); /* rid ambigous */
  519. force_reconnect (h);
  520. return;
  521. }
  522. name = (const char *) &msg[1];
  523. rd_tmp = &name[name_len];
  524. {
  525. struct GNUNET_GNSRECORD_Data rd[rd_count];
  526. GNUNET_assert (GNUNET_OK ==
  527. GNUNET_GNSRECORD_records_deserialize(rd_len,
  528. rd_tmp,
  529. rd_count,
  530. rd));
  531. if (0 == name_len)
  532. name = NULL;
  533. if (NULL != qe)
  534. {
  535. if (NULL != qe->proc)
  536. qe->proc (qe->proc_cls,
  537. &msg->private_key,
  538. name,
  539. rd_count,
  540. (rd_count > 0) ? rd : NULL);
  541. free_qe (qe);
  542. return;
  543. }
  544. if (NULL != ze)
  545. {
  546. if (NULL != ze->proc)
  547. ze->proc (ze->proc_cls,
  548. &msg->private_key,
  549. name,
  550. rd_count,
  551. rd);
  552. return;
  553. }
  554. }
  555. GNUNET_assert (0);
  556. }
  557. /**
  558. * Handle an incoming message of type
  559. * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END
  560. *
  561. * @param cls
  562. * @param msg the message we received
  563. */
  564. static void
  565. handle_record_result_end (void *cls,
  566. const struct GNUNET_NAMESTORE_Header *msg)
  567. {
  568. struct GNUNET_NAMESTORE_Handle *h = cls;
  569. struct GNUNET_NAMESTORE_QueueEntry *qe;
  570. struct GNUNET_NAMESTORE_ZoneIterator *ze;
  571. LOG (GNUNET_ERROR_TYPE_DEBUG,
  572. "Received RECORD_RESULT_END\n");
  573. ze = find_zi (h,
  574. ntohl (msg->r_id));
  575. qe = find_qe (h,
  576. ntohl (msg->r_id));
  577. if ( (NULL == ze) &&
  578. (NULL == qe) )
  579. return; /* rid not found */
  580. if ( (NULL != ze) &&
  581. (NULL != qe) )
  582. {
  583. GNUNET_break (0); /* rid ambigous */
  584. force_reconnect (h);
  585. return;
  586. }
  587. LOG (GNUNET_ERROR_TYPE_DEBUG,
  588. "Zone iteration completed!\n");
  589. if (NULL == ze)
  590. {
  591. GNUNET_break (0);
  592. force_reconnect (h);
  593. return;
  594. }
  595. if (NULL != ze->finish_cb)
  596. ze->finish_cb (ze->finish_cb_cls);
  597. free_ze (ze);
  598. }
  599. /**
  600. * Handle an incoming message of type
  601. * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
  602. *
  603. * @param qe the respective entry in the message queue
  604. * @param msg the message we received
  605. * @return #GNUNET_OK on success, #GNUNET_SYSERR if message malformed
  606. */
  607. static int
  608. check_zone_to_name_response (void *cls,
  609. const struct ZoneToNameResponseMessage *msg)
  610. {
  611. size_t name_len;
  612. size_t rd_ser_len;
  613. const char *name_tmp;
  614. (void) cls;
  615. if (GNUNET_OK != ntohs (msg->res))
  616. return GNUNET_OK;
  617. name_len = ntohs (msg->name_len);
  618. rd_ser_len = ntohs (msg->rd_len);
  619. if (ntohs (msg->gns_header.header.size) !=
  620. sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len)
  621. {
  622. GNUNET_break (0);
  623. return GNUNET_SYSERR;
  624. }
  625. name_tmp = (const char *) &msg[1];
  626. if ( (name_len > 0) &&
  627. ('\0' != name_tmp[name_len -1]) )
  628. {
  629. GNUNET_break (0);
  630. return GNUNET_SYSERR;
  631. }
  632. return check_rd (rd_ser_len,
  633. &name_tmp[name_len],
  634. ntohs (msg->rd_count));
  635. }
  636. /**
  637. * Handle an incoming message of type
  638. * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
  639. *
  640. * @param cls
  641. * @param msg the message we received
  642. */
  643. static void
  644. handle_zone_to_name_response (void *cls,
  645. const struct ZoneToNameResponseMessage *msg)
  646. {
  647. struct GNUNET_NAMESTORE_Handle *h = cls;
  648. struct GNUNET_NAMESTORE_QueueEntry *qe;
  649. int res;
  650. size_t name_len;
  651. size_t rd_ser_len;
  652. unsigned int rd_count;
  653. const char *name_tmp;
  654. const char *rd_tmp;
  655. LOG (GNUNET_ERROR_TYPE_DEBUG,
  656. "Received ZONE_TO_NAME_RESPONSE\n");
  657. qe = find_qe (h,
  658. ntohl (msg->gns_header.r_id));
  659. res = ntohs (msg->res);
  660. switch (res)
  661. {
  662. case GNUNET_SYSERR:
  663. LOG (GNUNET_ERROR_TYPE_DEBUG,
  664. "An error occurred during zone to name operation\n");
  665. break;
  666. case GNUNET_NO:
  667. LOG (GNUNET_ERROR_TYPE_DEBUG,
  668. "Namestore has no result for zone to name mapping \n");
  669. if (NULL != qe->proc)
  670. qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
  671. free_qe (qe);
  672. return;
  673. case GNUNET_YES:
  674. LOG (GNUNET_ERROR_TYPE_DEBUG,
  675. "Namestore has result for zone to name mapping \n");
  676. name_len = ntohs (msg->name_len);
  677. rd_count = ntohs (msg->rd_count);
  678. rd_ser_len = ntohs (msg->rd_len);
  679. name_tmp = (const char *) &msg[1];
  680. rd_tmp = &name_tmp[name_len];
  681. {
  682. struct GNUNET_GNSRECORD_Data rd[rd_count];
  683. GNUNET_assert (GNUNET_OK ==
  684. GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
  685. rd_tmp,
  686. rd_count,
  687. rd));
  688. /* normal end, call continuation with result */
  689. if (NULL != qe->proc)
  690. qe->proc (qe->proc_cls,
  691. &msg->zone,
  692. name_tmp,
  693. rd_count,
  694. rd);
  695. /* return is important here: break would call continuation with error! */
  696. free_qe (qe);
  697. return;
  698. }
  699. default:
  700. GNUNET_break (0);
  701. force_reconnect (h);
  702. return;
  703. }
  704. /* error case, call continuation with error */
  705. if (NULL != qe->error_cb)
  706. qe->error_cb (qe->error_cb_cls);
  707. free_qe (qe);
  708. }
  709. /**
  710. * Generic error handler, called with the appropriate error code and
  711. * the same closure specified at the creation of the message queue.
  712. * Not every message queue implementation supports an error handler.
  713. *
  714. * @param cls closure with the `struct GNUNET_NAMESTORE_Handle *`
  715. * @param error error code
  716. */
  717. static void
  718. mq_error_handler (void *cls,
  719. enum GNUNET_MQ_Error error)
  720. {
  721. struct GNUNET_NAMESTORE_Handle *h = cls;
  722. (void) error;
  723. force_reconnect (h);
  724. }
  725. /**
  726. * Reconnect to namestore service.
  727. *
  728. * @param h the handle to the NAMESTORE service
  729. */
  730. static void
  731. reconnect (struct GNUNET_NAMESTORE_Handle *h)
  732. {
  733. struct GNUNET_MQ_MessageHandler handlers[] = {
  734. GNUNET_MQ_hd_fixed_size (record_store_response,
  735. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE,
  736. struct RecordStoreResponseMessage,
  737. h),
  738. GNUNET_MQ_hd_var_size (zone_to_name_response,
  739. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE,
  740. struct ZoneToNameResponseMessage,
  741. h),
  742. GNUNET_MQ_hd_var_size (record_result,
  743. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
  744. struct RecordResultMessage,
  745. h),
  746. GNUNET_MQ_hd_fixed_size (record_result_end,
  747. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END,
  748. struct GNUNET_NAMESTORE_Header,
  749. h),
  750. GNUNET_MQ_hd_var_size (lookup_result,
  751. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE,
  752. struct LabelLookupResponseMessage,
  753. h),
  754. GNUNET_MQ_handler_end ()
  755. };
  756. struct GNUNET_NAMESTORE_ZoneIterator *it;
  757. struct GNUNET_NAMESTORE_QueueEntry *qe;
  758. GNUNET_assert (NULL == h->mq);
  759. h->mq = GNUNET_CLIENT_connect (h->cfg,
  760. "namestore",
  761. handlers,
  762. &mq_error_handler,
  763. h);
  764. if (NULL == h->mq)
  765. return;
  766. /* re-transmit pending requests that waited for a reconnect... */
  767. for (it = h->z_head; NULL != it; it = it->next)
  768. {
  769. GNUNET_MQ_send (h->mq,
  770. it->env);
  771. it->env = NULL;
  772. }
  773. for (qe = h->op_head; NULL != qe; qe = qe->next)
  774. {
  775. GNUNET_MQ_send (h->mq,
  776. qe->env);
  777. qe->env = NULL;
  778. }
  779. }
  780. /**
  781. * Re-establish the connection to the service.
  782. *
  783. * @param cls handle to use to re-connect.
  784. */
  785. static void
  786. reconnect_task (void *cls)
  787. {
  788. struct GNUNET_NAMESTORE_Handle *h = cls;
  789. h->reconnect_task = NULL;
  790. reconnect (h);
  791. }
  792. /**
  793. * Disconnect from service and then reconnect.
  794. *
  795. * @param h our handle
  796. */
  797. static void
  798. force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
  799. {
  800. struct GNUNET_NAMESTORE_ZoneIterator *ze;
  801. struct GNUNET_NAMESTORE_QueueEntry *qe;
  802. GNUNET_MQ_destroy (h->mq);
  803. h->mq = NULL;
  804. while (NULL != (ze = h->z_head))
  805. {
  806. if (NULL != ze->error_cb)
  807. ze->error_cb (ze->error_cb_cls);
  808. free_ze (ze);
  809. }
  810. while (NULL != (qe = h->op_head))
  811. {
  812. if (NULL != qe->error_cb)
  813. qe->error_cb (qe->error_cb_cls);
  814. if (NULL != qe->cont)
  815. qe->cont (qe->cont_cls,
  816. GNUNET_SYSERR,
  817. "failure in communication with namestore service");
  818. free_qe (qe);
  819. }
  820. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  821. "Reconnecting to namestore\n");
  822. h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
  823. h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
  824. &reconnect_task,
  825. h);
  826. }
  827. /**
  828. * Get a fresh operation id to distinguish between namestore requests
  829. *
  830. * @param h the namestore handle
  831. * @return next operation id to use
  832. */
  833. static uint32_t
  834. get_op_id (struct GNUNET_NAMESTORE_Handle *h)
  835. {
  836. return h->last_op_id_used++;
  837. }
  838. /**
  839. * Initialize the connection with the NAMESTORE service.
  840. *
  841. * @param cfg configuration to use
  842. * @return handle to the GNS service, or NULL on error
  843. */
  844. struct GNUNET_NAMESTORE_Handle *
  845. GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  846. {
  847. struct GNUNET_NAMESTORE_Handle *h;
  848. h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
  849. h->cfg = cfg;
  850. reconnect (h);
  851. if (NULL == h->mq)
  852. {
  853. GNUNET_free (h);
  854. return NULL;
  855. }
  856. return h;
  857. }
  858. /**
  859. * Disconnect from the namestore service (and free associated
  860. * resources).
  861. *
  862. * @param h handle to the namestore
  863. */
  864. void
  865. GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
  866. {
  867. struct GNUNET_NAMESTORE_QueueEntry *q;
  868. struct GNUNET_NAMESTORE_ZoneIterator *z;
  869. LOG (GNUNET_ERROR_TYPE_DEBUG,
  870. "Cleaning up\n");
  871. GNUNET_break (NULL == h->op_head);
  872. while (NULL != (q = h->op_head))
  873. {
  874. GNUNET_CONTAINER_DLL_remove (h->op_head,
  875. h->op_tail,
  876. q);
  877. GNUNET_free (q);
  878. }
  879. GNUNET_break (NULL == h->z_head);
  880. while (NULL != (z = h->z_head))
  881. {
  882. GNUNET_CONTAINER_DLL_remove (h->z_head,
  883. h->z_tail,
  884. z);
  885. GNUNET_free (z);
  886. }
  887. if (NULL != h->mq)
  888. {
  889. GNUNET_MQ_destroy (h->mq);
  890. h->mq = NULL;
  891. }
  892. if (NULL != h->reconnect_task)
  893. {
  894. GNUNET_SCHEDULER_cancel (h->reconnect_task);
  895. h->reconnect_task = NULL;
  896. }
  897. GNUNET_free (h);
  898. }
  899. /**
  900. * Task launched to warn the user that the namestore is
  901. * excessively slow and that a query was thus dropped.
  902. *
  903. * @param cls a `struct GNUNET_NAMESTORE_QueueEntry *`
  904. */
  905. static void
  906. warn_delay (void *cls)
  907. {
  908. struct GNUNET_NAMESTORE_QueueEntry *qe = cls;
  909. qe->timeout_task = NULL;
  910. LOG (GNUNET_ERROR_TYPE_WARNING,
  911. "Did not receive response from namestore after %s!\n",
  912. GNUNET_STRINGS_relative_time_to_string (NAMESTORE_DELAY_TOLERANCE,
  913. GNUNET_YES));
  914. if (NULL != qe->cont)
  915. {
  916. qe->cont (qe->cont_cls,
  917. GNUNET_SYSERR,
  918. "timeout");
  919. qe->cont = NULL;
  920. }
  921. GNUNET_NAMESTORE_cancel (qe);
  922. }
  923. /**
  924. * Store an item in the namestore. If the item is already present,
  925. * it is replaced with the new record. Use an empty array to
  926. * remove all records under the given name.
  927. *
  928. * @param h handle to the namestore
  929. * @param pkey private key of the zone
  930. * @param label name that is being mapped (at most 255 characters long)
  931. * @param rd_count number of records in the @a rd array
  932. * @param rd array of records with data to store
  933. * @param cont continuation to call when done
  934. * @param cont_cls closure for @a cont
  935. * @return handle to abort the request
  936. */
  937. struct GNUNET_NAMESTORE_QueueEntry *
  938. GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
  939. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  940. const char *label,
  941. unsigned int rd_count,
  942. const struct GNUNET_GNSRECORD_Data *rd,
  943. GNUNET_NAMESTORE_ContinuationWithStatus cont,
  944. void *cont_cls)
  945. {
  946. struct GNUNET_NAMESTORE_QueueEntry *qe;
  947. struct GNUNET_MQ_Envelope *env;
  948. char *name_tmp;
  949. char *rd_ser;
  950. ssize_t rd_ser_len;
  951. size_t name_len;
  952. uint32_t rid;
  953. struct RecordStoreMessage *msg;
  954. ssize_t sret;
  955. name_len = strlen (label) + 1;
  956. if (name_len > MAX_NAME_LEN)
  957. {
  958. GNUNET_break (0);
  959. return NULL;
  960. }
  961. rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
  962. rd);
  963. if (rd_ser_len < 0)
  964. {
  965. GNUNET_break (0);
  966. return NULL;
  967. }
  968. if (rd_ser_len > UINT16_MAX)
  969. {
  970. GNUNET_break (0);
  971. return NULL;
  972. }
  973. rid = get_op_id (h);
  974. qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
  975. qe->h = h;
  976. qe->cont = cont;
  977. qe->cont_cls = cont_cls;
  978. qe->op_id = rid;
  979. GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
  980. h->op_tail,
  981. qe);
  982. /* setup msg */
  983. env = GNUNET_MQ_msg_extra (msg,
  984. name_len + rd_ser_len,
  985. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
  986. msg->gns_header.r_id = htonl (rid);
  987. msg->name_len = htons (name_len);
  988. msg->rd_count = htons (rd_count);
  989. msg->rd_len = htons (rd_ser_len);
  990. msg->reserved = htons (0);
  991. msg->private_key = *pkey;
  992. name_tmp = (char *) &msg[1];
  993. GNUNET_memcpy (name_tmp,
  994. label,
  995. name_len);
  996. rd_ser = &name_tmp[name_len];
  997. sret = GNUNET_GNSRECORD_records_serialize (rd_count,
  998. rd,
  999. rd_ser_len,
  1000. rd_ser);
  1001. if ( (0 > sret) ||
  1002. (sret != rd_ser_len) )
  1003. {
  1004. GNUNET_break (0);
  1005. GNUNET_free (env);
  1006. return NULL;
  1007. }
  1008. GNUNET_assert (rd_ser_len == (size_t) sret);
  1009. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1010. "Sending NAMESTORE_RECORD_STORE message for name `%s' with %u records\n",
  1011. label,
  1012. rd_count);
  1013. qe->timeout_task = GNUNET_SCHEDULER_add_delayed (NAMESTORE_DELAY_TOLERANCE,
  1014. &warn_delay,
  1015. qe);
  1016. if (NULL == h->mq)
  1017. {
  1018. qe->env = env;
  1019. LOG (GNUNET_ERROR_TYPE_WARNING,
  1020. "Delaying NAMESTORE_RECORD_STORE message as namestore is not ready!\n");
  1021. }
  1022. else
  1023. {
  1024. GNUNET_MQ_send (h->mq,
  1025. env);
  1026. }
  1027. return qe;
  1028. }
  1029. /**
  1030. * Set the desired nick name for a zone
  1031. *
  1032. * @param h handle to the namestore
  1033. * @param pkey private key of the zone
  1034. * @param nick the nick name to set
  1035. * @param cont continuation to call when done
  1036. * @param cont_cls closure for @a cont
  1037. * @return handle to abort the request
  1038. */
  1039. struct GNUNET_NAMESTORE_QueueEntry *
  1040. GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
  1041. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  1042. const char *nick,
  1043. GNUNET_NAMESTORE_ContinuationWithStatus cont,
  1044. void *cont_cls)
  1045. {
  1046. struct GNUNET_GNSRECORD_Data rd;
  1047. if (NULL == h->mq)
  1048. return NULL;
  1049. memset (&rd, 0, sizeof (rd));
  1050. rd.data = nick;
  1051. rd.data_size = strlen (nick) +1;
  1052. rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
  1053. rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
  1054. rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
  1055. return GNUNET_NAMESTORE_records_store (h,
  1056. pkey,
  1057. GNUNET_GNS_EMPTY_LABEL_AT,
  1058. 1,
  1059. &rd,
  1060. cont,
  1061. cont_cls);
  1062. }
  1063. /**
  1064. * Lookup an item in the namestore.
  1065. *
  1066. * @param h handle to the namestore
  1067. * @param pkey private key of the zone
  1068. * @param label name that is being mapped (at most 255 characters long)
  1069. * @param error_cb function to call on error (i.e. disconnect)
  1070. * @param error_cb_cls closure for @a error_cb
  1071. * @param rm function to call with the result (with 0 records if we don't have that label)
  1072. * @param rm_cls closure for @a rm
  1073. * @return handle to abort the request
  1074. */
  1075. struct GNUNET_NAMESTORE_QueueEntry *
  1076. GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
  1077. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  1078. const char *label,
  1079. GNUNET_SCHEDULER_TaskCallback error_cb,
  1080. void *error_cb_cls,
  1081. GNUNET_NAMESTORE_RecordMonitor rm,
  1082. void *rm_cls)
  1083. {
  1084. struct GNUNET_NAMESTORE_QueueEntry *qe;
  1085. struct GNUNET_MQ_Envelope *env;
  1086. struct LabelLookupMessage *msg;
  1087. size_t label_len;
  1088. if (1 == (label_len = strlen (label) + 1))
  1089. {
  1090. GNUNET_break (0);
  1091. return NULL;
  1092. }
  1093. qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
  1094. qe->h = h;
  1095. qe->error_cb = error_cb;
  1096. qe->error_cb_cls = error_cb_cls;
  1097. qe->proc = rm;
  1098. qe->proc_cls = rm_cls;
  1099. qe->op_id = get_op_id(h);
  1100. GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
  1101. h->op_tail,
  1102. qe);
  1103. env = GNUNET_MQ_msg_extra (msg,
  1104. label_len,
  1105. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
  1106. msg->gns_header.r_id = htonl (qe->op_id);
  1107. msg->zone = *pkey;
  1108. msg->label_len = htonl (label_len);
  1109. GNUNET_memcpy (&msg[1],
  1110. label,
  1111. label_len);
  1112. if (NULL == h->mq)
  1113. qe->env = env;
  1114. else
  1115. GNUNET_MQ_send (h->mq,
  1116. env);
  1117. return qe;
  1118. }
  1119. /**
  1120. * Look for an existing PKEY delegation record for a given public key.
  1121. * Returns at most one result to the processor.
  1122. *
  1123. * @param h handle to the namestore
  1124. * @param zone public key of the zone to look up in, never NULL
  1125. * @param value_zone public key of the target zone (value), never NULL
  1126. * @param error_cb function to call on error (i.e. disconnect)
  1127. * @param error_cb_cls closure for @a error_cb
  1128. * @param proc function to call on the matching records, or with
  1129. * NULL (rd_count == 0) if there are no matching records
  1130. * @param proc_cls closure for @a proc
  1131. * @return a handle that can be used to
  1132. * cancel
  1133. */
  1134. struct GNUNET_NAMESTORE_QueueEntry *
  1135. GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
  1136. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  1137. const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
  1138. GNUNET_SCHEDULER_TaskCallback error_cb,
  1139. void *error_cb_cls,
  1140. GNUNET_NAMESTORE_RecordMonitor proc,
  1141. void *proc_cls)
  1142. {
  1143. struct GNUNET_NAMESTORE_QueueEntry *qe;
  1144. struct GNUNET_MQ_Envelope *env;
  1145. struct ZoneToNameMessage *msg;
  1146. uint32_t rid;
  1147. rid = get_op_id(h);
  1148. qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
  1149. qe->h = h;
  1150. qe->error_cb = error_cb;
  1151. qe->error_cb_cls = error_cb_cls;
  1152. qe->proc = proc;
  1153. qe->proc_cls = proc_cls;
  1154. qe->op_id = rid;
  1155. GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
  1156. h->op_tail,
  1157. qe);
  1158. env = GNUNET_MQ_msg (msg,
  1159. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
  1160. msg->gns_header.r_id = htonl (rid);
  1161. msg->zone = *zone;
  1162. msg->value_zone = *value_zone;
  1163. if (NULL == h->mq)
  1164. qe->env = env;
  1165. else
  1166. GNUNET_MQ_send (h->mq,
  1167. env);
  1168. return qe;
  1169. }
  1170. /**
  1171. * Starts a new zone iteration (used to periodically PUT all of our
  1172. * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
  1173. * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
  1174. * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
  1175. * immediately, and then again after
  1176. * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
  1177. *
  1178. * @param h handle to the namestore
  1179. * @param zone zone to access, NULL for all zones
  1180. * @param error_cb function to call on error (i.e. disconnect)
  1181. * @param error_cb_cls closure for @a error_cb
  1182. * @param proc function to call on each name from the zone; it
  1183. * will be called repeatedly with a value (if available)
  1184. * @param proc_cls closure for @a proc
  1185. * @param finish_cb function to call on completion
  1186. * @param finish_cb_cls closure for @a finish_cb
  1187. * @return an iterator handle to use for iteration
  1188. */
  1189. struct GNUNET_NAMESTORE_ZoneIterator *
  1190. GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
  1191. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  1192. GNUNET_SCHEDULER_TaskCallback error_cb,
  1193. void *error_cb_cls,
  1194. GNUNET_NAMESTORE_RecordMonitor proc,
  1195. void *proc_cls,
  1196. GNUNET_SCHEDULER_TaskCallback finish_cb,
  1197. void *finish_cb_cls)
  1198. {
  1199. struct GNUNET_NAMESTORE_ZoneIterator *it;
  1200. struct GNUNET_MQ_Envelope *env;
  1201. struct ZoneIterationStartMessage *msg;
  1202. uint32_t rid;
  1203. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1204. "Sending ZONE_ITERATION_START message\n");
  1205. rid = get_op_id (h);
  1206. it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
  1207. it->h = h;
  1208. it->error_cb = error_cb;
  1209. it->error_cb_cls = error_cb_cls;
  1210. it->finish_cb = finish_cb;
  1211. it->finish_cb_cls = finish_cb_cls;
  1212. it->proc = proc;
  1213. it->proc_cls = proc_cls;
  1214. it->op_id = rid;
  1215. if (NULL != zone)
  1216. it->zone = *zone;
  1217. GNUNET_CONTAINER_DLL_insert_tail (h->z_head,
  1218. h->z_tail,
  1219. it);
  1220. env = GNUNET_MQ_msg (msg,
  1221. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
  1222. msg->gns_header.r_id = htonl (rid);
  1223. if (NULL != zone)
  1224. msg->zone = *zone;
  1225. if (NULL == h->mq)
  1226. it->env = env;
  1227. else
  1228. GNUNET_MQ_send (h->mq,
  1229. env);
  1230. return it;
  1231. }
  1232. /**
  1233. * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
  1234. * for the next record.
  1235. *
  1236. * @param it the iterator
  1237. * @param limit number of records to return to the iterator in one shot
  1238. * (before #GNUNET_NAMESTORE_zone_iterator_next is to be called again)
  1239. */
  1240. void
  1241. GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it,
  1242. uint64_t limit)
  1243. {
  1244. struct GNUNET_NAMESTORE_Handle *h = it->h;
  1245. struct ZoneIterationNextMessage *msg;
  1246. struct GNUNET_MQ_Envelope *env;
  1247. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1248. "Sending ZONE_ITERATION_NEXT message with limit %llu\n",
  1249. (unsigned long long) limit);
  1250. env = GNUNET_MQ_msg (msg,
  1251. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
  1252. msg->gns_header.r_id = htonl (it->op_id);
  1253. msg->limit = GNUNET_htonll (limit);
  1254. GNUNET_MQ_send (h->mq,
  1255. env);
  1256. }
  1257. /**
  1258. * Stops iteration and releases the namestore handle for further calls.
  1259. *
  1260. * @param it the iterator
  1261. */
  1262. void
  1263. GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
  1264. {
  1265. struct GNUNET_NAMESTORE_Handle *h = it->h;
  1266. struct GNUNET_MQ_Envelope *env;
  1267. struct ZoneIterationStopMessage *msg;
  1268. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1269. "Sending ZONE_ITERATION_STOP message\n");
  1270. if (NULL != h->mq)
  1271. {
  1272. env = GNUNET_MQ_msg (msg,
  1273. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
  1274. msg->gns_header.r_id = htonl (it->op_id);
  1275. GNUNET_MQ_send (h->mq,
  1276. env);
  1277. }
  1278. free_ze (it);
  1279. }
  1280. /**
  1281. * Cancel a namestore operation. The final callback from the
  1282. * operation must not have been done yet.
  1283. *
  1284. * @param qe operation to cancel
  1285. */
  1286. void
  1287. GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
  1288. {
  1289. free_qe (qe);
  1290. }
  1291. /* end of namestore_api.c */