gnunet-service-namestore.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012, 2013, 2014, 2018 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/gnunet-service-namestore.c
  18. * @brief namestore for the GNUnet naming system
  19. * @author Matthias Wachs
  20. * @author Christian Grothoff
  21. *
  22. * TODO:
  23. * - "get_nick_record" is a bottleneck, introduce a cache to
  24. * avoid looking it up again and again (for the same few
  25. * zones that the user will typically manage!)
  26. * - run testcases, make sure everything works!
  27. */
  28. #include "platform.h"
  29. #include "gnunet_util_lib.h"
  30. #include "gnunet_dnsparser_lib.h"
  31. #include "gnunet_gns_service.h"
  32. #include "gnunet_namecache_service.h"
  33. #include "gnunet_namestore_service.h"
  34. #include "gnunet_namestore_plugin.h"
  35. #include "gnunet_statistics_service.h"
  36. #include "gnunet_signatures.h"
  37. #include "namestore.h"
  38. #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
  39. /**
  40. * If a monitor takes more than 1 minute to process an event, print a warning.
  41. */
  42. #define MONITOR_STALL_WARN_DELAY GNUNET_TIME_UNIT_MINUTES
  43. /**
  44. * Size of the cache used by #get_nick_record()
  45. */
  46. #define NC_SIZE 16
  47. /**
  48. * A namestore client
  49. */
  50. struct NamestoreClient;
  51. /**
  52. * A namestore iteration operation.
  53. */
  54. struct ZoneIteration
  55. {
  56. /**
  57. * Next element in the DLL
  58. */
  59. struct ZoneIteration *next;
  60. /**
  61. * Previous element in the DLL
  62. */
  63. struct ZoneIteration *prev;
  64. /**
  65. * Namestore client which intiated this zone iteration
  66. */
  67. struct NamestoreClient *nc;
  68. /**
  69. * The nick to add to the records
  70. */
  71. struct GNUNET_GNSRECORD_Data *nick;
  72. /**
  73. * Key of the zone we are iterating over.
  74. */
  75. struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
  76. /**
  77. * Last sequence number in the zone iteration used to address next
  78. * result of the zone iteration in the store
  79. *
  80. * Initialy set to 0.
  81. * Updated in #zone_iterate_proc()
  82. */
  83. uint64_t seq;
  84. /**
  85. * The operation id fot the zone iteration in the response for the client
  86. */
  87. uint32_t request_id;
  88. /**
  89. * Offset of the zone iteration used to address next result of the zone
  90. * iteration in the store
  91. *
  92. * Initialy set to 0 in #handle_iteration_start
  93. * Incremented with by every call to #handle_iteration_next
  94. */
  95. uint32_t offset;
  96. /**
  97. * Number of pending cache operations triggered by this zone iteration which we
  98. * need to wait for before allowing the client to continue.
  99. */
  100. unsigned int cache_ops;
  101. /**
  102. * Set to #GNUNET_YES if the last iteration exhausted the limit set by the
  103. * client and we should send the #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END
  104. * message and free the data structure once @e cache_ops is zero.
  105. */
  106. int send_end;
  107. };
  108. /**
  109. * A namestore client
  110. */
  111. struct NamestoreClient
  112. {
  113. /**
  114. * The client
  115. */
  116. struct GNUNET_SERVICE_Client *client;
  117. /**
  118. * Message queue for transmission to @e client
  119. */
  120. struct GNUNET_MQ_Handle *mq;
  121. /**
  122. * Head of the DLL of
  123. * Zone iteration operations in progress initiated by this client
  124. */
  125. struct ZoneIteration *op_head;
  126. /**
  127. * Tail of the DLL of
  128. * Zone iteration operations in progress initiated by this client
  129. */
  130. struct ZoneIteration *op_tail;
  131. };
  132. /**
  133. * A namestore monitor.
  134. */
  135. struct ZoneMonitor
  136. {
  137. /**
  138. * Next element in the DLL
  139. */
  140. struct ZoneMonitor *next;
  141. /**
  142. * Previous element in the DLL
  143. */
  144. struct ZoneMonitor *prev;
  145. /**
  146. * Namestore client which intiated this zone monitor
  147. */
  148. struct NamestoreClient *nc;
  149. /**
  150. * Private key of the zone.
  151. */
  152. struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
  153. /**
  154. * Task active during initial iteration.
  155. */
  156. struct GNUNET_SCHEDULER_Task *task;
  157. /**
  158. * Task to warn about slow monitors.
  159. */
  160. struct GNUNET_SCHEDULER_Task *sa_wait_warning;
  161. /**
  162. * Since when are we blocked on this monitor?
  163. */
  164. struct GNUNET_TIME_Absolute sa_waiting_start;
  165. /**
  166. * Last sequence number in the zone iteration used to address next
  167. * result of the zone iteration in the store
  168. *
  169. * Initialy set to 0.
  170. * Updated in #monitor_iterate_cb()
  171. */
  172. uint64_t seq;
  173. /**
  174. * Current limit of how many more messages we are allowed
  175. * to queue to this monitor.
  176. */
  177. uint64_t limit;
  178. /**
  179. * How many more requests may we receive from the iterator
  180. * before it is at the limit we gave it? Will be below or
  181. * equal to @e limit. The effective limit for monitor
  182. * events is thus @e iteration_cnt - @e limit!
  183. */
  184. uint64_t iteration_cnt;
  185. /**
  186. * Are we (still) in the initial iteration pass?
  187. */
  188. int in_first_iteration;
  189. /**
  190. * Is there a store activity waiting for this monitor? We only raise the
  191. * flag when it happens and search the DLL for the store activity when we
  192. * had a limit increase. If we cannot find any waiting store activity at
  193. * that time, we clear the flag again.
  194. */
  195. int sa_waiting;
  196. };
  197. /**
  198. * Pending operation on the namecache.
  199. */
  200. struct CacheOperation
  201. {
  202. /**
  203. * Kept in a DLL.
  204. */
  205. struct CacheOperation *prev;
  206. /**
  207. * Kept in a DLL.
  208. */
  209. struct CacheOperation *next;
  210. /**
  211. * Handle to namecache queue.
  212. */
  213. struct GNUNET_NAMECACHE_QueueEntry *qe;
  214. /**
  215. * Client to notify about the result, can be NULL.
  216. */
  217. struct NamestoreClient *nc;
  218. /**
  219. * Zone iteration to call #zone_iteration_done_client_continue()
  220. * for if applicable, can be NULL.
  221. */
  222. struct ZoneIteration *zi;
  223. /**
  224. * Client's request ID.
  225. */
  226. uint32_t rid;
  227. };
  228. /**
  229. * Information for an ongoing #handle_record_store() operation.
  230. * Needed as we may wait for monitors to be ready for the notification.
  231. */
  232. struct StoreActivity
  233. {
  234. /**
  235. * Kept in a DLL.
  236. */
  237. struct StoreActivity *next;
  238. /**
  239. * Kept in a DLL.
  240. */
  241. struct StoreActivity *prev;
  242. /**
  243. * Which client triggered the store activity?
  244. */
  245. struct NamestoreClient *nc;
  246. /**
  247. * Copy of the original store message (as data fields in @e rd will
  248. * point into it!).
  249. */
  250. const struct RecordStoreMessage *rsm;
  251. /**
  252. * Next zone monitor that still needs to be notified about this PUT.
  253. */
  254. struct ZoneMonitor *zm_pos;
  255. /**
  256. * Label nicely canonicalized (lower case).
  257. */
  258. char *conv_name;
  259. };
  260. /**
  261. * Entry in list of cached nick resolutions.
  262. */
  263. struct NickCache
  264. {
  265. /**
  266. * Zone the cache entry is for.
  267. */
  268. struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
  269. /**
  270. * Cached record data.
  271. */
  272. struct GNUNET_GNSRECORD_Data *rd;
  273. /**
  274. * Timestamp when this cache entry was used last.
  275. */
  276. struct GNUNET_TIME_Absolute last_used;
  277. };
  278. /**
  279. * We cache nick records to reduce DB load.
  280. */
  281. static struct NickCache nick_cache[NC_SIZE];
  282. /**
  283. * Public key of all zeros.
  284. */
  285. static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
  286. /**
  287. * Configuration handle.
  288. */
  289. static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
  290. /**
  291. * Handle to the statistics service
  292. */
  293. static struct GNUNET_STATISTICS_Handle *statistics;
  294. /**
  295. * Namecache handle.
  296. */
  297. static struct GNUNET_NAMECACHE_Handle *namecache;
  298. /**
  299. * Database handle
  300. */
  301. static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
  302. /**
  303. * Name of the database plugin
  304. */
  305. static char *db_lib_name;
  306. /**
  307. * Head of cop DLL.
  308. */
  309. static struct CacheOperation *cop_head;
  310. /**
  311. * Tail of cop DLL.
  312. */
  313. static struct CacheOperation *cop_tail;
  314. /**
  315. * First active zone monitor.
  316. */
  317. static struct ZoneMonitor *monitor_head;
  318. /**
  319. * Last active zone monitor.
  320. */
  321. static struct ZoneMonitor *monitor_tail;
  322. /**
  323. * Head of DLL of monitor-blocked store activities.
  324. */
  325. static struct StoreActivity *sa_head;
  326. /**
  327. * Tail of DLL of monitor-blocked store activities.
  328. */
  329. static struct StoreActivity *sa_tail;
  330. /**
  331. * Notification context shared by all monitors.
  332. */
  333. static struct GNUNET_NotificationContext *monitor_nc;
  334. /**
  335. * Optimize block insertion by caching map of private keys to
  336. * public keys in memory?
  337. */
  338. static int cache_keys;
  339. /**
  340. * Use the namecache? Doing so creates additional cryptographic
  341. * operations whenever we touch a record.
  342. */
  343. static int disable_namecache;
  344. /**
  345. * Task run during shutdown.
  346. *
  347. * @param cls unused
  348. */
  349. static void
  350. cleanup_task (void *cls)
  351. {
  352. struct CacheOperation *cop;
  353. (void) cls;
  354. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  355. "Stopping namestore service\n");
  356. while (NULL != (cop = cop_head))
  357. {
  358. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  359. "Aborting incomplete namecache operation\n");
  360. GNUNET_NAMECACHE_cancel (cop->qe);
  361. GNUNET_CONTAINER_DLL_remove (cop_head,
  362. cop_tail,
  363. cop);
  364. GNUNET_free (cop);
  365. }
  366. if (NULL != namecache)
  367. {
  368. GNUNET_NAMECACHE_disconnect (namecache);
  369. namecache = NULL;
  370. }
  371. GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
  372. GSN_database));
  373. GNUNET_free (db_lib_name);
  374. db_lib_name = NULL;
  375. if (NULL != monitor_nc)
  376. {
  377. GNUNET_notification_context_destroy (monitor_nc);
  378. monitor_nc = NULL;
  379. }
  380. if (NULL != statistics)
  381. {
  382. GNUNET_STATISTICS_destroy (statistics,
  383. GNUNET_NO);
  384. statistics = NULL;
  385. }
  386. }
  387. /**
  388. * Release memory used by @a sa.
  389. *
  390. * @param sa activity to free
  391. */
  392. static void
  393. free_store_activity (struct StoreActivity *sa)
  394. {
  395. GNUNET_CONTAINER_DLL_remove (sa_head,
  396. sa_tail,
  397. sa);
  398. GNUNET_free (sa->conv_name);
  399. GNUNET_free (sa);
  400. }
  401. /**
  402. * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
  403. * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
  404. * record, which (if found) is then copied to @a cls for future use.
  405. *
  406. * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
  407. * @param seq sequence number of the record, MUST NOT BE ZERO
  408. * @param private_key the private key of the zone (unused)
  409. * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
  410. * @param rd_count number of records in @a rd
  411. * @param rd records stored under @a label in the zone
  412. */
  413. static void
  414. lookup_nick_it (void *cls,
  415. uint64_t seq,
  416. const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
  417. const char *label,
  418. unsigned int rd_count,
  419. const struct GNUNET_GNSRECORD_Data *rd)
  420. {
  421. struct GNUNET_GNSRECORD_Data **res = cls;
  422. (void) private_key;
  423. GNUNET_assert (0 != seq);
  424. if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
  425. {
  426. GNUNET_break (0);
  427. return;
  428. }
  429. for (unsigned int c = 0; c < rd_count; c++)
  430. {
  431. if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
  432. {
  433. (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
  434. (*res)->data = &(*res)[1];
  435. GNUNET_memcpy ((void *) (*res)->data,
  436. rd[c].data,
  437. rd[c].data_size);
  438. (*res)->data_size = rd[c].data_size;
  439. (*res)->expiration_time = rd[c].expiration_time;
  440. (*res)->flags = rd[c].flags;
  441. (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
  442. return;
  443. }
  444. }
  445. (*res) = NULL;
  446. }
  447. /**
  448. * Add entry to the cache for @a zone and @a nick
  449. *
  450. * @param zone zone key to cache under
  451. * @param nick nick entry to cache
  452. */
  453. static void
  454. cache_nick (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  455. const struct GNUNET_GNSRECORD_Data *nick)
  456. {
  457. struct NickCache *oldest;
  458. oldest = NULL;
  459. for (unsigned int i=0;i<NC_SIZE;i++)
  460. {
  461. struct NickCache *pos = &nick_cache[i];
  462. if ( (NULL == oldest) ||
  463. (oldest->last_used.abs_value_us >
  464. pos->last_used.abs_value_us) )
  465. oldest = pos;
  466. if (0 == memcmp (zone,
  467. &pos->zone,
  468. sizeof (*zone)))
  469. {
  470. oldest = pos;
  471. break;
  472. }
  473. }
  474. GNUNET_free_non_null (oldest->rd);
  475. oldest->zone = *zone;
  476. oldest->rd = GNUNET_malloc (sizeof (*nick) +
  477. nick->data_size);
  478. *oldest->rd = *nick;
  479. oldest->rd->data = &oldest->rd[1];
  480. memcpy (&oldest->rd[1],
  481. nick->data,
  482. nick->data_size);
  483. oldest->last_used = GNUNET_TIME_absolute_get ();
  484. }
  485. /**
  486. * Return the NICK record for the zone (if it exists).
  487. *
  488. * @param zone private key for the zone to look for nick
  489. * @return NULL if no NICK record was found
  490. */
  491. static struct GNUNET_GNSRECORD_Data *
  492. get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
  493. {
  494. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  495. struct GNUNET_GNSRECORD_Data *nick;
  496. int res;
  497. /* check cache first */
  498. for (unsigned int i=0;i<NC_SIZE;i++)
  499. {
  500. struct NickCache *pos = &nick_cache[i];
  501. if ( (NULL != pos->rd) &&
  502. (0 == memcmp (zone,
  503. &pos->zone,
  504. sizeof (*zone))) )
  505. {
  506. nick = GNUNET_malloc (sizeof (*nick) +
  507. pos->rd->data_size);
  508. *nick = *pos->rd;
  509. nick->data = &nick[1];
  510. memcpy (&nick[1],
  511. pos->rd->data,
  512. pos->rd->data_size);
  513. pos->last_used = GNUNET_TIME_absolute_get ();
  514. return nick;
  515. }
  516. }
  517. nick = NULL;
  518. res = GSN_database->lookup_records (GSN_database->cls,
  519. zone,
  520. GNUNET_GNS_EMPTY_LABEL_AT,
  521. &lookup_nick_it,
  522. &nick);
  523. if ( (GNUNET_OK != res) ||
  524. (NULL == nick) )
  525. {
  526. GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
  527. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
  528. "No nick name set for zone `%s'\n",
  529. GNUNET_GNSRECORD_z2s (&pub));
  530. return NULL;
  531. }
  532. /* update cache */
  533. cache_nick (zone,
  534. nick);
  535. return nick;
  536. }
  537. /**
  538. * Merge the nick record @a nick_rd with the rest of the
  539. * record set given in @a rd2. Store the result in @a rdc_res
  540. * and @a rd_res. The @a nick_rd's expiration time is set to
  541. * the maximum expiration time of all of the records in @a rd2.
  542. *
  543. * @param nick_rd the nick record to integrate
  544. * @param rd2_length length of the @a rd2 array
  545. * @param rd2 array of records
  546. * @param rdc_res[out] length of the resulting @a rd_res array
  547. * @param rd_res[out] set to an array of records,
  548. * including @a nick_rd and @a rd2;
  549. * all of the variable-size 'data' fields in @a rd2 are
  550. * allocated in the same chunk of memory!
  551. */
  552. static void
  553. merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
  554. unsigned int rd2_length,
  555. const struct GNUNET_GNSRECORD_Data *rd2,
  556. unsigned int *rdc_res,
  557. struct GNUNET_GNSRECORD_Data **rd_res)
  558. {
  559. uint64_t latest_expiration;
  560. size_t req;
  561. char *data;
  562. size_t data_offset;
  563. struct GNUNET_GNSRECORD_Data *target;
  564. (*rdc_res) = 1 + rd2_length;
  565. if (0 == 1 + rd2_length)
  566. {
  567. GNUNET_break (0);
  568. (*rd_res) = NULL;
  569. return;
  570. }
  571. req = sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd->data_size;
  572. for (unsigned int i=0; i<rd2_length; i++)
  573. {
  574. const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
  575. if (req + sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size < req)
  576. {
  577. GNUNET_break (0);
  578. (*rd_res) = NULL;
  579. return;
  580. }
  581. req += sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size;
  582. }
  583. target = GNUNET_malloc (req);
  584. (*rd_res) = target;
  585. data = (char *) &target[1 + rd2_length];
  586. data_offset = 0;
  587. latest_expiration = 0;
  588. for (unsigned int i=0;i<rd2_length;i++)
  589. {
  590. const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
  591. if (0 != (orig->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
  592. {
  593. if ((GNUNET_TIME_absolute_get().abs_value_us + orig->expiration_time) >
  594. latest_expiration)
  595. latest_expiration = orig->expiration_time;
  596. }
  597. else if (orig->expiration_time > latest_expiration)
  598. latest_expiration = orig->expiration_time;
  599. target[i] = *orig;
  600. target[i].data = (void *) &data[data_offset];
  601. GNUNET_memcpy (&data[data_offset],
  602. orig->data,
  603. orig->data_size);
  604. data_offset += orig->data_size;
  605. }
  606. /* append nick */
  607. target[rd2_length] = *nick_rd;
  608. target[rd2_length].expiration_time = latest_expiration;
  609. target[rd2_length].data = (void *) &data[data_offset];
  610. GNUNET_memcpy (&data[data_offset],
  611. nick_rd->data,
  612. nick_rd->data_size);
  613. data_offset += nick_rd->data_size;
  614. GNUNET_assert (req ==
  615. (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
  616. }
  617. /**
  618. * Generate a `struct LookupNameResponseMessage` and send it to the
  619. * given client using the given notification context.
  620. *
  621. * @param nc client to unicast to
  622. * @param request_id request ID to use
  623. * @param zone_key zone key of the zone
  624. * @param name name
  625. * @param rd_count number of records in @a rd
  626. * @param rd array of records
  627. */
  628. static void
  629. send_lookup_response (struct NamestoreClient *nc,
  630. uint32_t request_id,
  631. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  632. const char *name,
  633. unsigned int rd_count,
  634. const struct GNUNET_GNSRECORD_Data *rd)
  635. {
  636. struct GNUNET_MQ_Envelope *env;
  637. struct RecordResultMessage *zir_msg;
  638. struct GNUNET_GNSRECORD_Data *nick;
  639. struct GNUNET_GNSRECORD_Data *res;
  640. unsigned int res_count;
  641. size_t name_len;
  642. ssize_t rd_ser_len;
  643. char *name_tmp;
  644. char *rd_ser;
  645. nick = get_nick_record (zone_key);
  646. GNUNET_assert (-1 !=
  647. GNUNET_GNSRECORD_records_get_size (rd_count,
  648. rd));
  649. if ( (NULL != nick) &&
  650. (0 != strcmp (name,
  651. GNUNET_GNS_EMPTY_LABEL_AT)))
  652. {
  653. nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
  654. merge_with_nick_records (nick,
  655. rd_count,
  656. rd,
  657. &res_count,
  658. &res);
  659. GNUNET_free (nick);
  660. }
  661. else
  662. {
  663. res_count = rd_count;
  664. res = (struct GNUNET_GNSRECORD_Data *) rd;
  665. }
  666. GNUNET_assert (-1 !=
  667. GNUNET_GNSRECORD_records_get_size (res_count,
  668. res));
  669. name_len = strlen (name) + 1;
  670. rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count,
  671. res);
  672. if (rd_ser_len < 0)
  673. {
  674. GNUNET_break (0);
  675. GNUNET_SERVICE_client_drop (nc->client);
  676. return;
  677. }
  678. if (((size_t) rd_ser_len) >= UINT16_MAX - name_len - sizeof (*zir_msg))
  679. {
  680. GNUNET_break (0);
  681. GNUNET_SERVICE_client_drop (nc->client);
  682. return;
  683. }
  684. env = GNUNET_MQ_msg_extra (zir_msg,
  685. name_len + rd_ser_len,
  686. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
  687. zir_msg->gns_header.r_id = htonl (request_id);
  688. zir_msg->name_len = htons (name_len);
  689. zir_msg->rd_count = htons (res_count);
  690. zir_msg->rd_len = htons ((uint16_t) rd_ser_len);
  691. zir_msg->private_key = *zone_key;
  692. name_tmp = (char *) &zir_msg[1];
  693. GNUNET_memcpy (name_tmp,
  694. name,
  695. name_len);
  696. rd_ser = &name_tmp[name_len];
  697. GNUNET_assert (rd_ser_len ==
  698. GNUNET_GNSRECORD_records_serialize (res_count,
  699. res,
  700. rd_ser_len,
  701. rd_ser));
  702. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  703. "Sending RECORD_RESULT message with %u records\n",
  704. res_count);
  705. GNUNET_STATISTICS_update (statistics,
  706. "Record sets sent to clients",
  707. 1,
  708. GNUNET_NO);
  709. GNUNET_MQ_send (nc->mq,
  710. env);
  711. if (rd != res)
  712. GNUNET_free (res);
  713. }
  714. /**
  715. * Send response to the store request to the client.
  716. *
  717. * @param client client to talk to
  718. * @param res status of the operation
  719. * @param rid client's request ID
  720. */
  721. static void
  722. send_store_response (struct NamestoreClient *nc,
  723. int res,
  724. uint32_t rid)
  725. {
  726. struct GNUNET_MQ_Envelope *env;
  727. struct RecordStoreResponseMessage *rcr_msg;
  728. GNUNET_assert (NULL != nc);
  729. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  730. "Sending RECORD_STORE_RESPONSE message\n");
  731. GNUNET_STATISTICS_update (statistics,
  732. "Store requests completed",
  733. 1,
  734. GNUNET_NO);
  735. env = GNUNET_MQ_msg (rcr_msg,
  736. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
  737. rcr_msg->gns_header.r_id = htonl (rid);
  738. rcr_msg->op_result = htonl (res);
  739. GNUNET_MQ_send (nc->mq,
  740. env);
  741. }
  742. /**
  743. * Function called once we are done with the zone iteration and
  744. * allow the zone iteration client to send us more messages.
  745. *
  746. * @param zi zone iteration we are processing
  747. */
  748. static void
  749. zone_iteration_done_client_continue (struct ZoneIteration *zi)
  750. {
  751. struct GNUNET_MQ_Envelope *env;
  752. struct GNUNET_NAMESTORE_Header *em;
  753. GNUNET_SERVICE_client_continue (zi->nc->client);
  754. if (! zi->send_end)
  755. return;
  756. /* send empty response to indicate end of list */
  757. env = GNUNET_MQ_msg (em,
  758. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END);
  759. em->r_id = htonl (zi->request_id);
  760. GNUNET_MQ_send (zi->nc->mq,
  761. env);
  762. GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
  763. zi->nc->op_tail,
  764. zi);
  765. GNUNET_free (zi);
  766. }
  767. /**
  768. * Cache operation complete, clean up.
  769. *
  770. * @param cls the `struct CacheOperation`
  771. * @param success success
  772. * @param emsg error messages
  773. */
  774. static void
  775. finish_cache_operation (void *cls,
  776. int32_t success,
  777. const char *emsg)
  778. {
  779. struct CacheOperation *cop = cls;
  780. struct ZoneIteration *zi;
  781. if (NULL != emsg)
  782. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  783. _("Failed to replicate block in namecache: %s\n"),
  784. emsg);
  785. else
  786. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  787. "CACHE operation completed\n");
  788. GNUNET_CONTAINER_DLL_remove (cop_head,
  789. cop_tail,
  790. cop);
  791. if (NULL != cop->nc)
  792. send_store_response (cop->nc,
  793. success,
  794. cop->rid);
  795. if (NULL != (zi = cop->zi))
  796. {
  797. zi->cache_ops--;
  798. if (0 == zi->cache_ops)
  799. {
  800. /* unchoke zone iteration, cache has caught up */
  801. zone_iteration_done_client_continue (zi);
  802. }
  803. }
  804. GNUNET_free (cop);
  805. }
  806. /**
  807. * We just touched the plaintext information about a name in our zone;
  808. * refresh the corresponding (encrypted) block in the namecache.
  809. *
  810. * @param nc client responsible for the request, can be NULL
  811. * @param zi zone iteration response for the request, can be NULL
  812. * @param rid request ID of the client
  813. * @param zone_key private key of the zone
  814. * @param name label for the records
  815. * @param rd_count number of records
  816. * @param rd records stored under the given @a name
  817. */
  818. static void
  819. refresh_block (struct NamestoreClient *nc,
  820. struct ZoneIteration *zi,
  821. uint32_t rid,
  822. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  823. const char *name,
  824. unsigned int rd_count,
  825. const struct GNUNET_GNSRECORD_Data *rd)
  826. {
  827. struct GNUNET_GNSRECORD_Block *block;
  828. struct CacheOperation *cop;
  829. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  830. struct GNUNET_GNSRECORD_Data *nick;
  831. struct GNUNET_GNSRECORD_Data *res;
  832. unsigned int res_count;
  833. struct GNUNET_TIME_Absolute exp_time;
  834. nick = get_nick_record (zone_key);
  835. res_count = rd_count;
  836. res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
  837. if (NULL != nick)
  838. {
  839. nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
  840. merge_with_nick_records (nick,
  841. rd_count,rd,
  842. &res_count,
  843. &res);
  844. GNUNET_free (nick);
  845. }
  846. if (0 == res_count)
  847. {
  848. if (NULL != nc)
  849. send_store_response (nc,
  850. GNUNET_OK,
  851. rid);
  852. return; /* no data, no need to update cache */
  853. }
  854. if (GNUNET_YES == disable_namecache)
  855. {
  856. GNUNET_STATISTICS_update (statistics,
  857. "Namecache updates skipped (NC disabled)",
  858. 1,
  859. GNUNET_NO);
  860. if (NULL != nc)
  861. send_store_response (nc,
  862. GNUNET_OK,
  863. rid);
  864. return;
  865. }
  866. exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
  867. res);
  868. if (cache_keys)
  869. block = GNUNET_GNSRECORD_block_create2 (zone_key,
  870. exp_time,
  871. name,
  872. res,
  873. res_count);
  874. else
  875. block = GNUNET_GNSRECORD_block_create (zone_key,
  876. exp_time,
  877. name,
  878. res,
  879. res_count);
  880. GNUNET_assert (NULL != block);
  881. GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
  882. &pkey);
  883. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  884. "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
  885. name,
  886. res_count,
  887. GNUNET_STRINGS_absolute_time_to_string (exp_time),
  888. GNUNET_GNSRECORD_z2s (&pkey));
  889. GNUNET_STATISTICS_update (statistics,
  890. "Namecache updates pushed",
  891. 1,
  892. GNUNET_NO);
  893. cop = GNUNET_new (struct CacheOperation);
  894. cop->nc = nc;
  895. cop->zi = zi;
  896. if (NULL != zi)
  897. zi->cache_ops++;
  898. cop->rid = rid;
  899. GNUNET_CONTAINER_DLL_insert (cop_head,
  900. cop_tail,
  901. cop);
  902. cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
  903. block,
  904. &finish_cache_operation,
  905. cop);
  906. GNUNET_free (block);
  907. }
  908. /**
  909. * Print a warning that one of our monitors is no longer reacting.
  910. *
  911. * @param cls a `struct ZoneMonitor` to warn about
  912. */
  913. static void
  914. warn_monitor_slow (void *cls)
  915. {
  916. struct ZoneMonitor *zm = cls;
  917. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  918. "No response from monitor since %s\n",
  919. GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
  920. zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
  921. &warn_monitor_slow,
  922. zm);
  923. }
  924. /**
  925. * Continue processing the @a sa.
  926. *
  927. * @param sa store activity to process
  928. */
  929. static void
  930. continue_store_activity (struct StoreActivity *sa)
  931. {
  932. const struct RecordStoreMessage *rp_msg = sa->rsm;
  933. unsigned int rd_count;
  934. size_t name_len;
  935. size_t rd_ser_len;
  936. uint32_t rid;
  937. const char *name_tmp;
  938. const char *rd_ser;
  939. rid = ntohl (rp_msg->gns_header.r_id);
  940. name_len = ntohs (rp_msg->name_len);
  941. rd_count = ntohs (rp_msg->rd_count);
  942. rd_ser_len = ntohs (rp_msg->rd_len);
  943. name_tmp = (const char *) &rp_msg[1];
  944. rd_ser = &name_tmp[name_len];
  945. {
  946. struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
  947. /* We did this before, must succeed again */
  948. GNUNET_assert (GNUNET_OK ==
  949. GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
  950. rd_ser,
  951. rd_count,
  952. rd));
  953. for (struct ZoneMonitor *zm = sa->zm_pos;
  954. NULL != zm;
  955. zm = sa->zm_pos)
  956. {
  957. if ( (0 != memcmp (&rp_msg->private_key,
  958. &zm->zone,
  959. sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
  960. (0 != memcmp (&zm->zone,
  961. &zero,
  962. sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
  963. {
  964. sa->zm_pos = zm->next; /* not interesting to this monitor */
  965. continue;
  966. }
  967. if (zm->limit == zm->iteration_cnt)
  968. {
  969. zm->sa_waiting = GNUNET_YES;
  970. zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
  971. if (NULL != zm->sa_wait_warning)
  972. GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
  973. zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
  974. &warn_monitor_slow,
  975. zm);
  976. return; /* blocked on zone monitor */
  977. }
  978. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  979. "Notifying monitor about changes under label `%s'\n",
  980. sa->conv_name);
  981. zm->limit--;
  982. send_lookup_response (zm->nc,
  983. 0,
  984. &rp_msg->private_key,
  985. sa->conv_name,
  986. rd_count,
  987. rd);
  988. sa->zm_pos = zm->next;
  989. }
  990. /* great, done with the monitors, unpack (again) for refresh_block operation */
  991. refresh_block (sa->nc,
  992. NULL,
  993. rid,
  994. &rp_msg->private_key,
  995. sa->conv_name,
  996. rd_count,
  997. rd);
  998. }
  999. GNUNET_SERVICE_client_continue (sa->nc->client);
  1000. free_store_activity (sa);
  1001. }
  1002. /**
  1003. * Called whenever a client is disconnected.
  1004. * Frees our resources associated with that client.
  1005. *
  1006. * @param cls closure
  1007. * @param client identification of the client
  1008. * @param app_ctx the `struct NamestoreClient` of @a client
  1009. */
  1010. static void
  1011. client_disconnect_cb (void *cls,
  1012. struct GNUNET_SERVICE_Client *client,
  1013. void *app_ctx)
  1014. {
  1015. struct NamestoreClient *nc = app_ctx;
  1016. struct ZoneIteration *no;
  1017. struct CacheOperation *cop;
  1018. (void) cls;
  1019. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1020. "Client %p disconnected\n",
  1021. client);
  1022. for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
  1023. {
  1024. struct StoreActivity *san;
  1025. if (nc != zm->nc)
  1026. continue;
  1027. GNUNET_CONTAINER_DLL_remove (monitor_head,
  1028. monitor_tail,
  1029. zm);
  1030. if (NULL != zm->task)
  1031. {
  1032. GNUNET_SCHEDULER_cancel (zm->task);
  1033. zm->task = NULL;
  1034. }
  1035. if (NULL != zm->sa_wait_warning)
  1036. {
  1037. GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
  1038. zm->sa_wait_warning = NULL;
  1039. }
  1040. for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
  1041. {
  1042. san = sa->next;
  1043. if (zm == sa->zm_pos)
  1044. {
  1045. sa->zm_pos = zm->next;
  1046. /* this may free sa */
  1047. continue_store_activity (sa);
  1048. }
  1049. }
  1050. GNUNET_free (zm);
  1051. break;
  1052. }
  1053. for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
  1054. {
  1055. if (sa->nc == nc)
  1056. {
  1057. /* this may free sa */
  1058. free_store_activity (sa);
  1059. break; /* there can only be one per nc */
  1060. }
  1061. }
  1062. while (NULL != (no = nc->op_head))
  1063. {
  1064. GNUNET_CONTAINER_DLL_remove (nc->op_head,
  1065. nc->op_tail,
  1066. no);
  1067. GNUNET_free (no);
  1068. }
  1069. for (cop = cop_head; NULL != cop; cop = cop->next)
  1070. if (nc == cop->nc)
  1071. cop->nc = NULL;
  1072. GNUNET_free (nc);
  1073. }
  1074. /**
  1075. * Add a client to our list of active clients.
  1076. *
  1077. * @param cls NULL
  1078. * @param client client to add
  1079. * @param mq message queue for @a client
  1080. * @return internal namestore client structure for this client
  1081. */
  1082. static void *
  1083. client_connect_cb (void *cls,
  1084. struct GNUNET_SERVICE_Client *client,
  1085. struct GNUNET_MQ_Handle *mq)
  1086. {
  1087. struct NamestoreClient *nc;
  1088. (void) cls;
  1089. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1090. "Client %p connected\n",
  1091. client);
  1092. nc = GNUNET_new (struct NamestoreClient);
  1093. nc->client = client;
  1094. nc->mq = mq;
  1095. return nc;
  1096. }
  1097. /**
  1098. * Closure for #lookup_it().
  1099. */
  1100. struct RecordLookupContext
  1101. {
  1102. /**
  1103. * FIXME.
  1104. */
  1105. const char *label;
  1106. /**
  1107. * FIXME.
  1108. */
  1109. char *res_rd;
  1110. /**
  1111. * FIXME.
  1112. */
  1113. struct GNUNET_GNSRECORD_Data *nick;
  1114. /**
  1115. * FIXME.
  1116. */
  1117. int found;
  1118. /**
  1119. * FIXME.
  1120. */
  1121. unsigned int res_rd_count;
  1122. /**
  1123. * FIXME.
  1124. */
  1125. ssize_t rd_ser_len;
  1126. };
  1127. /**
  1128. * Function called by the namestore plugin when we are trying to lookup
  1129. * a record as part of #handle_record_lookup(). Merges all results into
  1130. * the context.
  1131. *
  1132. * @param cls closure with a `struct RecordLookupContext`
  1133. * @param seq unique serial number of the record, MUST NOT BE ZERO
  1134. * @param zone_key private key of the zone
  1135. * @param label name that is being mapped (at most 255 characters long)
  1136. * @param rd_count number of entries in @a rd array
  1137. * @param rd array of records with data to store
  1138. */
  1139. static void
  1140. lookup_it (void *cls,
  1141. uint64_t seq,
  1142. const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
  1143. const char *label,
  1144. unsigned int rd_count,
  1145. const struct GNUNET_GNSRECORD_Data *rd)
  1146. {
  1147. struct RecordLookupContext *rlc = cls;
  1148. (void) private_key;
  1149. GNUNET_assert (0 != seq);
  1150. if (0 != strcmp (label,
  1151. rlc->label))
  1152. return;
  1153. rlc->found = GNUNET_YES;
  1154. if (0 == rd_count)
  1155. {
  1156. rlc->rd_ser_len = 0;
  1157. rlc->res_rd_count = 0;
  1158. rlc->res_rd = NULL;
  1159. return;
  1160. }
  1161. if ( (NULL != rlc->nick) &&
  1162. (0 != strcmp (label,
  1163. GNUNET_GNS_EMPTY_LABEL_AT)) )
  1164. {
  1165. /* Merge */
  1166. struct GNUNET_GNSRECORD_Data *rd_res;
  1167. unsigned int rdc_res;
  1168. rd_res = NULL;
  1169. rdc_res = 0;
  1170. rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
  1171. merge_with_nick_records (rlc->nick,
  1172. rd_count,
  1173. rd,
  1174. &rdc_res,
  1175. &rd_res);
  1176. rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
  1177. rd_res);
  1178. if (rlc->rd_ser_len < 0)
  1179. {
  1180. GNUNET_break (0);
  1181. GNUNET_free (rd_res);
  1182. rlc->found = GNUNET_NO;
  1183. rlc->rd_ser_len = 0;
  1184. return;
  1185. }
  1186. rlc->res_rd_count = rdc_res;
  1187. rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
  1188. if (rlc->rd_ser_len !=
  1189. GNUNET_GNSRECORD_records_serialize (rdc_res,
  1190. rd_res,
  1191. rlc->rd_ser_len,
  1192. rlc->res_rd))
  1193. {
  1194. GNUNET_break (0);
  1195. GNUNET_free (rlc->res_rd);
  1196. rlc->res_rd = NULL;
  1197. rlc->res_rd_count = 0;
  1198. rlc->rd_ser_len = 0;
  1199. GNUNET_free (rd_res);
  1200. rlc->found = GNUNET_NO;
  1201. return;
  1202. }
  1203. GNUNET_free (rd_res);
  1204. GNUNET_free (rlc->nick);
  1205. rlc->nick = NULL;
  1206. }
  1207. else
  1208. {
  1209. rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
  1210. rd);
  1211. if (rlc->rd_ser_len < 0)
  1212. {
  1213. GNUNET_break (0);
  1214. rlc->found = GNUNET_NO;
  1215. rlc->rd_ser_len = 0;
  1216. return;
  1217. }
  1218. rlc->res_rd_count = rd_count;
  1219. rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
  1220. if (rlc->rd_ser_len !=
  1221. GNUNET_GNSRECORD_records_serialize (rd_count,
  1222. rd,
  1223. rlc->rd_ser_len,
  1224. rlc->res_rd))
  1225. {
  1226. GNUNET_break (0);
  1227. GNUNET_free (rlc->res_rd);
  1228. rlc->res_rd = NULL;
  1229. rlc->res_rd_count = 0;
  1230. rlc->rd_ser_len = 0;
  1231. rlc->found = GNUNET_NO;
  1232. return;
  1233. }
  1234. }
  1235. }
  1236. /**
  1237. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
  1238. *
  1239. * @param cls client sending the message
  1240. * @param ll_msg message of type `struct LabelLookupMessage`
  1241. * @return #GNUNET_OK if @a ll_msg is well-formed
  1242. */
  1243. static int
  1244. check_record_lookup (void *cls,
  1245. const struct LabelLookupMessage *ll_msg)
  1246. {
  1247. uint32_t name_len;
  1248. size_t src_size;
  1249. (void) cls;
  1250. name_len = ntohl (ll_msg->label_len);
  1251. src_size = ntohs (ll_msg->gns_header.header.size);
  1252. if (name_len != src_size - sizeof (struct LabelLookupMessage))
  1253. {
  1254. GNUNET_break (0);
  1255. return GNUNET_SYSERR;
  1256. }
  1257. GNUNET_MQ_check_zero_termination (ll_msg);
  1258. return GNUNET_OK;
  1259. }
  1260. /**
  1261. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
  1262. *
  1263. * @param cls client sending the message
  1264. * @param ll_msg message of type `struct LabelLookupMessage`
  1265. */
  1266. static void
  1267. handle_record_lookup (void *cls,
  1268. const struct LabelLookupMessage *ll_msg)
  1269. {
  1270. struct NamestoreClient *nc = cls;
  1271. struct GNUNET_MQ_Envelope *env;
  1272. struct LabelLookupResponseMessage *llr_msg;
  1273. struct RecordLookupContext rlc;
  1274. const char *name_tmp;
  1275. char *res_name;
  1276. char *conv_name;
  1277. uint32_t name_len;
  1278. int res;
  1279. name_len = ntohl (ll_msg->label_len);
  1280. name_tmp = (const char *) &ll_msg[1];
  1281. GNUNET_SERVICE_client_continue (nc->client);
  1282. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1283. "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
  1284. name_tmp);
  1285. conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
  1286. if (NULL == conv_name)
  1287. {
  1288. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1289. "Error converting name `%s'\n",
  1290. name_tmp);
  1291. GNUNET_SERVICE_client_drop (nc->client);
  1292. return;
  1293. }
  1294. rlc.label = conv_name;
  1295. rlc.found = GNUNET_NO;
  1296. rlc.res_rd_count = 0;
  1297. rlc.res_rd = NULL;
  1298. rlc.rd_ser_len = 0;
  1299. rlc.nick = get_nick_record (&ll_msg->zone);
  1300. res = GSN_database->lookup_records (GSN_database->cls,
  1301. &ll_msg->zone,
  1302. conv_name,
  1303. &lookup_it,
  1304. &rlc);
  1305. GNUNET_free (conv_name);
  1306. env = GNUNET_MQ_msg_extra (llr_msg,
  1307. name_len + rlc.rd_ser_len,
  1308. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
  1309. llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
  1310. llr_msg->private_key = ll_msg->zone;
  1311. llr_msg->name_len = htons (name_len);
  1312. llr_msg->rd_count = htons (rlc.res_rd_count);
  1313. llr_msg->rd_len = htons (rlc.rd_ser_len);
  1314. res_name = (char *) &llr_msg[1];
  1315. if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
  1316. llr_msg->found = ntohs (GNUNET_YES);
  1317. else
  1318. llr_msg->found = ntohs (GNUNET_NO);
  1319. GNUNET_memcpy (&llr_msg[1],
  1320. name_tmp,
  1321. name_len);
  1322. GNUNET_memcpy (&res_name[name_len],
  1323. rlc.res_rd,
  1324. rlc.rd_ser_len);
  1325. GNUNET_MQ_send (nc->mq,
  1326. env);
  1327. GNUNET_free_non_null (rlc.res_rd);
  1328. }
  1329. /**
  1330. * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
  1331. *
  1332. * @param cls client sending the message
  1333. * @param rp_msg message of type `struct RecordStoreMessage`
  1334. * @return #GNUNET_OK if @a rp_msg is well-formed
  1335. */
  1336. static int
  1337. check_record_store (void *cls,
  1338. const struct RecordStoreMessage *rp_msg)
  1339. {
  1340. size_t name_len;
  1341. size_t msg_size;
  1342. size_t msg_size_exp;
  1343. size_t rd_ser_len;
  1344. const char *name_tmp;
  1345. (void) cls;
  1346. name_len = ntohs (rp_msg->name_len);
  1347. msg_size = ntohs (rp_msg->gns_header.header.size);
  1348. rd_ser_len = ntohs (rp_msg->rd_len);
  1349. msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
  1350. if (msg_size != msg_size_exp)
  1351. {
  1352. GNUNET_break (0);
  1353. return GNUNET_SYSERR;
  1354. }
  1355. if ( (0 == name_len) ||
  1356. (name_len > MAX_NAME_LEN) )
  1357. {
  1358. GNUNET_break (0);
  1359. return GNUNET_SYSERR;
  1360. }
  1361. name_tmp = (const char *) &rp_msg[1];
  1362. if ('\0' != name_tmp[name_len -1])
  1363. {
  1364. GNUNET_break (0);
  1365. return GNUNET_SYSERR;
  1366. }
  1367. return GNUNET_OK;
  1368. }
  1369. /**
  1370. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
  1371. *
  1372. * @param cls client sending the message
  1373. * @param rp_msg message of type `struct RecordStoreMessage`
  1374. */
  1375. static void
  1376. handle_record_store (void *cls,
  1377. const struct RecordStoreMessage *rp_msg)
  1378. {
  1379. struct NamestoreClient *nc = cls;
  1380. size_t name_len;
  1381. size_t rd_ser_len;
  1382. uint32_t rid;
  1383. const char *name_tmp;
  1384. char *conv_name;
  1385. const char *rd_ser;
  1386. unsigned int rd_count;
  1387. int res;
  1388. struct StoreActivity *sa;
  1389. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1390. "Received NAMESTORE_RECORD_STORE message\n");
  1391. rid = ntohl (rp_msg->gns_header.r_id);
  1392. name_len = ntohs (rp_msg->name_len);
  1393. rd_count = ntohs (rp_msg->rd_count);
  1394. rd_ser_len = ntohs (rp_msg->rd_len);
  1395. GNUNET_break (0 == ntohs (rp_msg->reserved));
  1396. name_tmp = (const char *) &rp_msg[1];
  1397. rd_ser = &name_tmp[name_len];
  1398. {
  1399. struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
  1400. if (GNUNET_OK !=
  1401. GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
  1402. rd_ser,
  1403. rd_count,
  1404. rd))
  1405. {
  1406. GNUNET_break (0);
  1407. GNUNET_SERVICE_client_drop (nc->client);
  1408. return;
  1409. }
  1410. /* Extracting and converting private key */
  1411. conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
  1412. if (NULL == conv_name)
  1413. {
  1414. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1415. "Error converting name `%s'\n",
  1416. name_tmp);
  1417. GNUNET_SERVICE_client_drop (nc->client);
  1418. return;
  1419. }
  1420. GNUNET_STATISTICS_update (statistics,
  1421. "Well-formed store requests received",
  1422. 1,
  1423. GNUNET_NO);
  1424. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1425. "Creating %u records for name `%s'\n",
  1426. (unsigned int) rd_count,
  1427. conv_name);
  1428. if ( (0 == rd_count) &&
  1429. (GNUNET_NO ==
  1430. GSN_database->lookup_records (GSN_database->cls,
  1431. &rp_msg->private_key,
  1432. conv_name,
  1433. NULL,
  1434. 0)) )
  1435. {
  1436. /* This name does not exist, so cannot be removed */
  1437. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1438. "Name `%s' does not exist, no deletion required\n",
  1439. conv_name);
  1440. res = GNUNET_NO;
  1441. }
  1442. else
  1443. {
  1444. /* remove "NICK" records, unless this is for the
  1445. #GNUNET_GNS_EMPTY_LABEL_AT label */
  1446. struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
  1447. unsigned int rd_clean_off;
  1448. rd_clean_off = 0;
  1449. for (unsigned int i=0;i<rd_count;i++)
  1450. {
  1451. rd_clean[rd_clean_off] = rd[i];
  1452. if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
  1453. conv_name)) ||
  1454. (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
  1455. rd_clean_off++;
  1456. if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
  1457. conv_name)) &&
  1458. (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) )
  1459. cache_nick (&rp_msg->private_key,
  1460. &rd[i]);
  1461. }
  1462. res = GSN_database->store_records (GSN_database->cls,
  1463. &rp_msg->private_key,
  1464. conv_name,
  1465. rd_clean_off,
  1466. rd_clean);
  1467. }
  1468. if (GNUNET_OK != res)
  1469. {
  1470. /* store not successful, not need to tell monitors */
  1471. send_store_response (nc,
  1472. res,
  1473. rid);
  1474. GNUNET_SERVICE_client_continue (nc->client);
  1475. GNUNET_free (conv_name);
  1476. return;
  1477. }
  1478. sa = GNUNET_malloc (sizeof (struct StoreActivity) +
  1479. ntohs (rp_msg->gns_header.header.size));
  1480. GNUNET_CONTAINER_DLL_insert (sa_head,
  1481. sa_tail,
  1482. sa);
  1483. sa->nc = nc;
  1484. sa->rsm = (const struct RecordStoreMessage *) &sa[1];
  1485. GNUNET_memcpy (&sa[1],
  1486. rp_msg,
  1487. ntohs (rp_msg->gns_header.header.size));
  1488. sa->zm_pos = monitor_head;
  1489. sa->conv_name = conv_name;
  1490. continue_store_activity (sa);
  1491. }
  1492. }
  1493. /**
  1494. * Context for record remove operations passed from #handle_zone_to_name to
  1495. * #handle_zone_to_name_it as closure
  1496. */
  1497. struct ZoneToNameCtx
  1498. {
  1499. /**
  1500. * Namestore client
  1501. */
  1502. struct NamestoreClient *nc;
  1503. /**
  1504. * Request id (to be used in the response to the client).
  1505. */
  1506. uint32_t rid;
  1507. /**
  1508. * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
  1509. * not finding a name for the zone still counts as a 'success' here,
  1510. * as this field is about the success of executing the IPC protocol.
  1511. */
  1512. int success;
  1513. };
  1514. /**
  1515. * Zone to name iterator
  1516. *
  1517. * @param cls struct ZoneToNameCtx *
  1518. * @param seq sequence number of the record, MUST NOT BE ZERO
  1519. * @param zone_key the zone key
  1520. * @param name name
  1521. * @param rd_count number of records in @a rd
  1522. * @param rd record data
  1523. */
  1524. static void
  1525. handle_zone_to_name_it (void *cls,
  1526. uint64_t seq,
  1527. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  1528. const char *name,
  1529. unsigned int rd_count,
  1530. const struct GNUNET_GNSRECORD_Data *rd)
  1531. {
  1532. struct ZoneToNameCtx *ztn_ctx = cls;
  1533. struct GNUNET_MQ_Envelope *env;
  1534. struct ZoneToNameResponseMessage *ztnr_msg;
  1535. int16_t res;
  1536. size_t name_len;
  1537. ssize_t rd_ser_len;
  1538. size_t msg_size;
  1539. char *name_tmp;
  1540. char *rd_tmp;
  1541. GNUNET_assert (0 != seq);
  1542. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1543. "Found result for zone-to-name lookup: `%s'\n",
  1544. name);
  1545. res = GNUNET_YES;
  1546. name_len = (NULL == name) ? 0 : strlen (name) + 1;
  1547. rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
  1548. rd);
  1549. if (rd_ser_len < 0)
  1550. {
  1551. GNUNET_break (0);
  1552. ztn_ctx->success = GNUNET_SYSERR;
  1553. return;
  1554. }
  1555. msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
  1556. if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
  1557. {
  1558. GNUNET_break (0);
  1559. ztn_ctx->success = GNUNET_SYSERR;
  1560. return;
  1561. }
  1562. env = GNUNET_MQ_msg_extra (ztnr_msg,
  1563. name_len + rd_ser_len,
  1564. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
  1565. ztnr_msg->gns_header.header.size = htons (msg_size);
  1566. ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
  1567. ztnr_msg->res = htons (res);
  1568. ztnr_msg->rd_len = htons (rd_ser_len);
  1569. ztnr_msg->rd_count = htons (rd_count);
  1570. ztnr_msg->name_len = htons (name_len);
  1571. ztnr_msg->zone = *zone_key;
  1572. name_tmp = (char *) &ztnr_msg[1];
  1573. GNUNET_memcpy (name_tmp,
  1574. name,
  1575. name_len);
  1576. rd_tmp = &name_tmp[name_len];
  1577. GNUNET_assert (rd_ser_len ==
  1578. GNUNET_GNSRECORD_records_serialize (rd_count,
  1579. rd,
  1580. rd_ser_len,
  1581. rd_tmp));
  1582. ztn_ctx->success = GNUNET_OK;
  1583. GNUNET_MQ_send (ztn_ctx->nc->mq,
  1584. env);
  1585. }
  1586. /**
  1587. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
  1588. *
  1589. * @param cls client client sending the message
  1590. * @param ztn_msg message of type 'struct ZoneToNameMessage'
  1591. */
  1592. static void
  1593. handle_zone_to_name (void *cls,
  1594. const struct ZoneToNameMessage *ztn_msg)
  1595. {
  1596. struct NamestoreClient *nc = cls;
  1597. struct ZoneToNameCtx ztn_ctx;
  1598. struct GNUNET_MQ_Envelope *env;
  1599. struct ZoneToNameResponseMessage *ztnr_msg;
  1600. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1601. "Received ZONE_TO_NAME message\n");
  1602. ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
  1603. ztn_ctx.nc = nc;
  1604. ztn_ctx.success = GNUNET_NO;
  1605. if (GNUNET_SYSERR ==
  1606. GSN_database->zone_to_name (GSN_database->cls,
  1607. &ztn_msg->zone,
  1608. &ztn_msg->value_zone,
  1609. &handle_zone_to_name_it, &ztn_ctx))
  1610. {
  1611. /* internal error, hang up instead of signalling something
  1612. that might be wrong */
  1613. GNUNET_break (0);
  1614. GNUNET_SERVICE_client_drop (nc->client);
  1615. return;
  1616. }
  1617. if (GNUNET_NO == ztn_ctx.success)
  1618. {
  1619. /* no result found, send empty response */
  1620. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1621. "Found no result for zone-to-name lookup.\n");
  1622. env = GNUNET_MQ_msg (ztnr_msg,
  1623. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
  1624. ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
  1625. ztnr_msg->res = htons (GNUNET_NO);
  1626. GNUNET_MQ_send (nc->mq,
  1627. env);
  1628. }
  1629. GNUNET_SERVICE_client_continue (nc->client);
  1630. }
  1631. /**
  1632. * Context for record remove operations passed from
  1633. * #run_zone_iteration_round to #zone_iterate_proc as closure
  1634. */
  1635. struct ZoneIterationProcResult
  1636. {
  1637. /**
  1638. * The zone iteration handle
  1639. */
  1640. struct ZoneIteration *zi;
  1641. /**
  1642. * Number of results left to be returned in this iteration.
  1643. */
  1644. uint64_t limit;
  1645. };
  1646. /**
  1647. * Process results for zone iteration from database
  1648. *
  1649. * @param cls struct ZoneIterationProcResult
  1650. * @param seq sequence number of the record, MUST NOT BE ZERO
  1651. * @param zone_key the zone key
  1652. * @param name name
  1653. * @param rd_count number of records for this name
  1654. * @param rd record data
  1655. */
  1656. static void
  1657. zone_iterate_proc (void *cls,
  1658. uint64_t seq,
  1659. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  1660. const char *name,
  1661. unsigned int rd_count,
  1662. const struct GNUNET_GNSRECORD_Data *rd)
  1663. {
  1664. struct ZoneIterationProcResult *proc = cls;
  1665. int do_refresh_block;
  1666. GNUNET_assert (0 != seq);
  1667. if ( (NULL == zone_key) &&
  1668. (NULL == name) )
  1669. {
  1670. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1671. "Iteration done\n");
  1672. return;
  1673. }
  1674. if ( (NULL == zone_key) ||
  1675. (NULL == name) )
  1676. {
  1677. /* what is this!? should never happen */
  1678. GNUNET_break (0);
  1679. return;
  1680. }
  1681. if (0 == proc->limit)
  1682. {
  1683. /* what is this!? should never happen */
  1684. GNUNET_break (0);
  1685. return;
  1686. }
  1687. proc->limit--;
  1688. proc->zi->seq = seq;
  1689. send_lookup_response (proc->zi->nc,
  1690. proc->zi->request_id,
  1691. zone_key,
  1692. name,
  1693. rd_count,
  1694. rd);
  1695. do_refresh_block = GNUNET_NO;
  1696. for (unsigned int i=0;i<rd_count;i++)
  1697. if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
  1698. {
  1699. do_refresh_block = GNUNET_YES;
  1700. break;
  1701. }
  1702. if (GNUNET_YES == do_refresh_block)
  1703. refresh_block (NULL,
  1704. proc->zi,
  1705. 0,
  1706. zone_key,
  1707. name,
  1708. rd_count,
  1709. rd);
  1710. }
  1711. /**
  1712. * Perform the next round of the zone iteration.
  1713. *
  1714. * @param zi zone iterator to process
  1715. * @param limit number of results to return in one pass
  1716. */
  1717. static void
  1718. run_zone_iteration_round (struct ZoneIteration *zi,
  1719. uint64_t limit)
  1720. {
  1721. struct ZoneIterationProcResult proc;
  1722. struct GNUNET_TIME_Absolute start;
  1723. struct GNUNET_TIME_Relative duration;
  1724. memset (&proc,
  1725. 0,
  1726. sizeof (proc));
  1727. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1728. "Asked to return up to %llu records at position %llu\n",
  1729. (unsigned long long) limit,
  1730. (unsigned long long) zi->seq);
  1731. proc.zi = zi;
  1732. proc.limit = limit;
  1733. start = GNUNET_TIME_absolute_get ();
  1734. GNUNET_break (GNUNET_SYSERR !=
  1735. GSN_database->iterate_records (GSN_database->cls,
  1736. (0 == memcmp (&zi->zone,
  1737. &zero,
  1738. sizeof (zero)))
  1739. ? NULL
  1740. : &zi->zone,
  1741. zi->seq,
  1742. limit,
  1743. &zone_iterate_proc,
  1744. &proc));
  1745. duration = GNUNET_TIME_absolute_get_duration (start);
  1746. duration = GNUNET_TIME_relative_divide (duration,
  1747. limit - proc.limit);
  1748. GNUNET_STATISTICS_set (statistics,
  1749. "NAMESTORE iteration delay (μs/record)",
  1750. duration.rel_value_us,
  1751. GNUNET_NO);
  1752. if (0 == proc.limit)
  1753. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1754. "Returned %llu results, more results available\n",
  1755. (unsigned long long) limit);
  1756. zi->send_end = (0 != proc.limit);
  1757. if (0 == zi->cache_ops)
  1758. zone_iteration_done_client_continue (zi);
  1759. }
  1760. /**
  1761. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
  1762. *
  1763. * @param cls the client sending the message
  1764. * @param zis_msg message from the client
  1765. */
  1766. static void
  1767. handle_iteration_start (void *cls,
  1768. const struct ZoneIterationStartMessage *zis_msg)
  1769. {
  1770. struct NamestoreClient *nc = cls;
  1771. struct ZoneIteration *zi;
  1772. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1773. "Received ZONE_ITERATION_START message\n");
  1774. zi = GNUNET_new (struct ZoneIteration);
  1775. zi->request_id = ntohl (zis_msg->gns_header.r_id);
  1776. zi->offset = 0;
  1777. zi->nc = nc;
  1778. zi->zone = zis_msg->zone;
  1779. GNUNET_CONTAINER_DLL_insert (nc->op_head,
  1780. nc->op_tail,
  1781. zi);
  1782. run_zone_iteration_round (zi,
  1783. 1);
  1784. }
  1785. /**
  1786. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
  1787. *
  1788. * @param cls the client sending the message
  1789. * @param zis_msg message from the client
  1790. */
  1791. static void
  1792. handle_iteration_stop (void *cls,
  1793. const struct ZoneIterationStopMessage *zis_msg)
  1794. {
  1795. struct NamestoreClient *nc = cls;
  1796. struct ZoneIteration *zi;
  1797. uint32_t rid;
  1798. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1799. "Received ZONE_ITERATION_STOP message\n");
  1800. rid = ntohl (zis_msg->gns_header.r_id);
  1801. for (zi = nc->op_head; NULL != zi; zi = zi->next)
  1802. if (zi->request_id == rid)
  1803. break;
  1804. if (NULL == zi)
  1805. {
  1806. GNUNET_break (0);
  1807. GNUNET_SERVICE_client_drop (nc->client);
  1808. return;
  1809. }
  1810. GNUNET_CONTAINER_DLL_remove (nc->op_head,
  1811. nc->op_tail,
  1812. zi);
  1813. GNUNET_free (zi);
  1814. GNUNET_SERVICE_client_continue (nc->client);
  1815. }
  1816. /**
  1817. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
  1818. *
  1819. * @param cls the client sending the message
  1820. * @param message message from the client
  1821. */
  1822. static void
  1823. handle_iteration_next (void *cls,
  1824. const struct ZoneIterationNextMessage *zis_msg)
  1825. {
  1826. struct NamestoreClient *nc = cls;
  1827. struct ZoneIteration *zi;
  1828. uint32_t rid;
  1829. uint64_t limit;
  1830. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1831. "Received ZONE_ITERATION_NEXT message\n");
  1832. GNUNET_STATISTICS_update (statistics,
  1833. "Iteration NEXT messages received",
  1834. 1,
  1835. GNUNET_NO);
  1836. rid = ntohl (zis_msg->gns_header.r_id);
  1837. limit = GNUNET_ntohll (zis_msg->limit);
  1838. for (zi = nc->op_head; NULL != zi; zi = zi->next)
  1839. if (zi->request_id == rid)
  1840. break;
  1841. if (NULL == zi)
  1842. {
  1843. GNUNET_break (0);
  1844. GNUNET_SERVICE_client_drop (nc->client);
  1845. return;
  1846. }
  1847. run_zone_iteration_round (zi,
  1848. limit);
  1849. }
  1850. /**
  1851. * Function called when the monitor is ready for more data, and we
  1852. * should thus unblock PUT operations that were blocked on the
  1853. * monitor not being ready.
  1854. */
  1855. static void
  1856. monitor_unblock (struct ZoneMonitor *zm)
  1857. {
  1858. struct StoreActivity *sa = sa_head;
  1859. while ( (NULL != sa) &&
  1860. (zm->limit > zm->iteration_cnt) )
  1861. {
  1862. struct StoreActivity *sn = sa->next;
  1863. if (sa->zm_pos == zm)
  1864. continue_store_activity (sa);
  1865. sa = sn;
  1866. }
  1867. if (zm->limit > zm->iteration_cnt)
  1868. {
  1869. zm->sa_waiting = GNUNET_NO;
  1870. if (NULL != zm->sa_wait_warning)
  1871. {
  1872. GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
  1873. zm->sa_wait_warning = NULL;
  1874. }
  1875. }
  1876. else if (GNUNET_YES == zm->sa_waiting)
  1877. {
  1878. zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
  1879. if (NULL != zm->sa_wait_warning)
  1880. GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
  1881. zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
  1882. &warn_monitor_slow,
  1883. zm);
  1884. }
  1885. }
  1886. /**
  1887. * Send 'sync' message to zone monitor, we're now in sync.
  1888. *
  1889. * @param zm monitor that is now in sync
  1890. */
  1891. static void
  1892. monitor_sync (struct ZoneMonitor *zm)
  1893. {
  1894. struct GNUNET_MQ_Envelope *env;
  1895. struct GNUNET_MessageHeader *sync;
  1896. env = GNUNET_MQ_msg (sync,
  1897. GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
  1898. GNUNET_MQ_send (zm->nc->mq,
  1899. env);
  1900. /* mark iteration done */
  1901. zm->in_first_iteration = GNUNET_NO;
  1902. zm->iteration_cnt = 0;
  1903. if ( (zm->limit > 0) &&
  1904. (zm->sa_waiting) )
  1905. monitor_unblock (zm);
  1906. }
  1907. /**
  1908. * Obtain the next datum during the zone monitor's zone initial iteration.
  1909. *
  1910. * @param cls zone monitor that does its initial iteration
  1911. */
  1912. static void
  1913. monitor_iteration_next (void *cls);
  1914. /**
  1915. * A #GNUNET_NAMESTORE_RecordIterator for monitors.
  1916. *
  1917. * @param cls a 'struct ZoneMonitor *' with information about the monitor
  1918. * @param seq sequence number of the record, MUST NOT BE ZERO
  1919. * @param zone_key zone key of the zone
  1920. * @param name name
  1921. * @param rd_count number of records in @a rd
  1922. * @param rd array of records
  1923. */
  1924. static void
  1925. monitor_iterate_cb (void *cls,
  1926. uint64_t seq,
  1927. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  1928. const char *name,
  1929. unsigned int rd_count,
  1930. const struct GNUNET_GNSRECORD_Data *rd)
  1931. {
  1932. struct ZoneMonitor *zm = cls;
  1933. GNUNET_assert (0 != seq);
  1934. zm->seq = seq;
  1935. GNUNET_assert (NULL != name);
  1936. GNUNET_STATISTICS_update (statistics,
  1937. "Monitor notifications sent",
  1938. 1,
  1939. GNUNET_NO);
  1940. zm->limit--;
  1941. zm->iteration_cnt--;
  1942. send_lookup_response (zm->nc,
  1943. 0,
  1944. zone_key,
  1945. name,
  1946. rd_count,
  1947. rd);
  1948. if ( (0 == zm->iteration_cnt) &&
  1949. (0 != zm->limit) )
  1950. {
  1951. /* We are done with the current iteration batch, AND the
  1952. client would right now accept more, so go again! */
  1953. GNUNET_assert (NULL == zm->task);
  1954. zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
  1955. zm);
  1956. }
  1957. }
  1958. /**
  1959. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
  1960. *
  1961. * @param cls the client sending the message
  1962. * @param zis_msg message from the client
  1963. */
  1964. static void
  1965. handle_monitor_start (void *cls,
  1966. const struct ZoneMonitorStartMessage *zis_msg)
  1967. {
  1968. struct NamestoreClient *nc = cls;
  1969. struct ZoneMonitor *zm;
  1970. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1971. "Received ZONE_MONITOR_START message\n");
  1972. zm = GNUNET_new (struct ZoneMonitor);
  1973. zm->nc = nc;
  1974. zm->zone = zis_msg->zone;
  1975. zm->limit = 1;
  1976. zm->in_first_iteration = (GNUNET_YES == ntohl (zis_msg->iterate_first));
  1977. GNUNET_CONTAINER_DLL_insert (monitor_head,
  1978. monitor_tail,
  1979. zm);
  1980. GNUNET_SERVICE_client_mark_monitor (nc->client);
  1981. GNUNET_SERVICE_client_continue (nc->client);
  1982. GNUNET_notification_context_add (monitor_nc,
  1983. nc->mq);
  1984. if (zm->in_first_iteration)
  1985. zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
  1986. zm);
  1987. else
  1988. monitor_sync (zm);
  1989. }
  1990. /**
  1991. * Obtain the next datum during the zone monitor's zone initial iteration.
  1992. *
  1993. * @param cls zone monitor that does its initial iteration
  1994. */
  1995. static void
  1996. monitor_iteration_next (void *cls)
  1997. {
  1998. struct ZoneMonitor *zm = cls;
  1999. int ret;
  2000. zm->task = NULL;
  2001. GNUNET_assert (0 == zm->iteration_cnt);
  2002. if (zm->limit > 16)
  2003. zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */
  2004. else
  2005. zm->iteration_cnt = zm->limit; /* use it all */
  2006. ret = GSN_database->iterate_records (GSN_database->cls,
  2007. (0 == memcmp (&zm->zone,
  2008. &zero,
  2009. sizeof (zero)))
  2010. ? NULL
  2011. : &zm->zone,
  2012. zm->seq,
  2013. zm->iteration_cnt,
  2014. &monitor_iterate_cb,
  2015. zm);
  2016. if (GNUNET_SYSERR == ret)
  2017. {
  2018. GNUNET_SERVICE_client_drop (zm->nc->client);
  2019. return;
  2020. }
  2021. if (GNUNET_NO == ret)
  2022. {
  2023. /* empty zone */
  2024. monitor_sync (zm);
  2025. return;
  2026. }
  2027. }
  2028. /**
  2029. * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT message
  2030. *
  2031. * @param cls the client sending the message
  2032. * @param nm message from the client
  2033. */
  2034. static void
  2035. handle_monitor_next (void *cls,
  2036. const struct ZoneMonitorNextMessage *nm)
  2037. {
  2038. struct NamestoreClient *nc = cls;
  2039. struct ZoneMonitor *zm;
  2040. uint64_t inc;
  2041. inc = GNUNET_ntohll (nm->limit);
  2042. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2043. "Received ZONE_MONITOR_NEXT message with limit %llu\n",
  2044. (unsigned long long) inc);
  2045. for (zm = monitor_head; NULL != zm; zm = zm->next)
  2046. if (zm->nc == nc)
  2047. break;
  2048. if (NULL == zm)
  2049. {
  2050. GNUNET_break (0);
  2051. GNUNET_SERVICE_client_drop (nc->client);
  2052. return;
  2053. }
  2054. GNUNET_SERVICE_client_continue (nc->client);
  2055. if (zm->limit + inc < zm->limit)
  2056. {
  2057. GNUNET_break (0);
  2058. GNUNET_SERVICE_client_drop (nc->client);
  2059. return;
  2060. }
  2061. zm->limit += inc;
  2062. if ( (zm->in_first_iteration) &&
  2063. (zm->limit == inc) )
  2064. {
  2065. /* We are still iterating, and the previous iteration must
  2066. have stopped due to the client's limit, so continue it! */
  2067. GNUNET_assert (NULL == zm->task);
  2068. zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
  2069. zm);
  2070. }
  2071. GNUNET_assert (zm->iteration_cnt <= zm->limit);
  2072. if ( (zm->limit > zm->iteration_cnt) &&
  2073. (zm->sa_waiting) )
  2074. {
  2075. monitor_unblock (zm);
  2076. }
  2077. else if (GNUNET_YES == zm->sa_waiting)
  2078. {
  2079. if (NULL != zm->sa_wait_warning)
  2080. GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
  2081. zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
  2082. zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
  2083. &warn_monitor_slow,
  2084. zm);
  2085. }
  2086. }
  2087. /**
  2088. * Process namestore requests.
  2089. *
  2090. * @param cls closure
  2091. * @param cfg configuration to use
  2092. * @param service the initialized service
  2093. */
  2094. static void
  2095. run (void *cls,
  2096. const struct GNUNET_CONFIGURATION_Handle *cfg,
  2097. struct GNUNET_SERVICE_Handle *service)
  2098. {
  2099. char *database;
  2100. (void) cls;
  2101. (void) service;
  2102. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2103. "Starting namestore service\n");
  2104. cache_keys = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  2105. "namestore",
  2106. "CACHE_KEYS");
  2107. disable_namecache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  2108. "namecache",
  2109. "DISABLE");
  2110. GSN_cfg = cfg;
  2111. monitor_nc = GNUNET_notification_context_create (1);
  2112. if (GNUNET_YES != disable_namecache)
  2113. {
  2114. namecache = GNUNET_NAMECACHE_connect (cfg);
  2115. GNUNET_assert (NULL != namecache);
  2116. }
  2117. /* Loading database plugin */
  2118. if (GNUNET_OK !=
  2119. GNUNET_CONFIGURATION_get_value_string (cfg,
  2120. "namestore",
  2121. "database",
  2122. &database))
  2123. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2124. "No database backend configured\n");
  2125. GNUNET_asprintf (&db_lib_name,
  2126. "libgnunet_plugin_namestore_%s",
  2127. database);
  2128. GSN_database = GNUNET_PLUGIN_load (db_lib_name,
  2129. (void *) GSN_cfg);
  2130. GNUNET_free (database);
  2131. statistics = GNUNET_STATISTICS_create ("namestore",
  2132. cfg);
  2133. GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
  2134. NULL);
  2135. if (NULL == GSN_database)
  2136. {
  2137. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2138. "Could not load database backend `%s'\n",
  2139. db_lib_name);
  2140. GNUNET_SCHEDULER_shutdown ();
  2141. return;
  2142. }
  2143. }
  2144. /**
  2145. * Define "main" method using service macro.
  2146. */
  2147. GNUNET_SERVICE_MAIN
  2148. ("namestore",
  2149. GNUNET_SERVICE_OPTION_NONE,
  2150. &run,
  2151. &client_connect_cb,
  2152. &client_disconnect_cb,
  2153. NULL,
  2154. GNUNET_MQ_hd_var_size (record_store,
  2155. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
  2156. struct RecordStoreMessage,
  2157. NULL),
  2158. GNUNET_MQ_hd_var_size (record_lookup,
  2159. GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
  2160. struct LabelLookupMessage,
  2161. NULL),
  2162. GNUNET_MQ_hd_fixed_size (zone_to_name,
  2163. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
  2164. struct ZoneToNameMessage,
  2165. NULL),
  2166. GNUNET_MQ_hd_fixed_size (iteration_start,
  2167. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
  2168. struct ZoneIterationStartMessage,
  2169. NULL),
  2170. GNUNET_MQ_hd_fixed_size (iteration_next,
  2171. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
  2172. struct ZoneIterationNextMessage,
  2173. NULL),
  2174. GNUNET_MQ_hd_fixed_size (iteration_stop,
  2175. GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
  2176. struct ZoneIterationStopMessage,
  2177. NULL),
  2178. GNUNET_MQ_hd_fixed_size (monitor_start,
  2179. GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
  2180. struct ZoneMonitorStartMessage,
  2181. NULL),
  2182. GNUNET_MQ_hd_fixed_size (monitor_next,
  2183. GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT,
  2184. struct ZoneMonitorNextMessage,
  2185. NULL),
  2186. GNUNET_MQ_handler_end ());
  2187. /* end of gnunet-service-namestore.c */