plugin_namestore_sqlite.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * This file is part of GNUnet
  3. * Copyright (C) 2009-2017 GNUnet e.V.
  4. *
  5. * GNUnet is free software: you can redistribute it and/or modify it
  6. * under the terms of the GNU Affero General Public License as published
  7. * by the Free Software Foundation, either version 3 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * GNUnet is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. SPDX-License-Identifier: AGPL3.0-or-later
  18. */
  19. /**
  20. * @file namestore/plugin_namestore_sqlite.c
  21. * @brief sqlite-based namestore backend
  22. * @author Christian Grothoff
  23. */
  24. #include "platform.h"
  25. #include "gnunet_namestore_plugin.h"
  26. #include "gnunet_namestore_service.h"
  27. #include "gnunet_gnsrecord_lib.h"
  28. #include "gnunet_sq_lib.h"
  29. #include "namestore.h"
  30. #include <sqlite3.h>
  31. /**
  32. * After how many ms "busy" should a DB operation fail for good? A
  33. * low value makes sure that we are more responsive to requests
  34. * (especially PUTs). A high value guarantees a higher success rate
  35. * (SELECTs in iterate can take several seconds despite LIMIT=1).
  36. *
  37. * The default value of 1s should ensure that users do not experience
  38. * huge latencies while at the same time allowing operations to
  39. * succeed with reasonable probability.
  40. */
  41. #define BUSY_TIMEOUT_MS 1000
  42. /**
  43. * Log an error message at log-level 'level' that indicates
  44. * a failure of the command 'cmd' on file 'filename'
  45. * with the message given by strerror(errno).
  46. */
  47. #define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, "namestore-sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
  48. #define LOG(kind,...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__)
  49. /**
  50. * Context for all functions in this plugin.
  51. */
  52. struct Plugin
  53. {
  54. const struct GNUNET_CONFIGURATION_Handle *cfg;
  55. /**
  56. * Database filename.
  57. */
  58. char *fn;
  59. /**
  60. * Native SQLite database handle.
  61. */
  62. sqlite3 *dbh;
  63. /**
  64. * Precompiled SQL to store records.
  65. */
  66. sqlite3_stmt *store_records;
  67. /**
  68. * Precompiled SQL to deltete existing records.
  69. */
  70. sqlite3_stmt *delete_records;
  71. /**
  72. * Precompiled SQL for iterate records within a zone.
  73. */
  74. sqlite3_stmt *iterate_zone;
  75. /**
  76. * Precompiled SQL for iterate all records within all zones.
  77. */
  78. sqlite3_stmt *iterate_all_zones;
  79. /**
  80. * Precompiled SQL to for reverse lookup based on PKEY.
  81. */
  82. sqlite3_stmt *zone_to_name;
  83. /**
  84. * Precompiled SQL to lookup records based on label.
  85. */
  86. sqlite3_stmt *lookup_label;
  87. };
  88. /**
  89. * Initialize the database connections and associated
  90. * data structures (create tables and indices
  91. * as needed as well).
  92. *
  93. * @param plugin the plugin context (state for this module)
  94. * @return #GNUNET_OK on success
  95. */
  96. static int
  97. database_setup (struct Plugin *plugin)
  98. {
  99. char *sqlite_filename;
  100. struct GNUNET_SQ_ExecuteStatement es[] = {
  101. GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
  102. GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"),
  103. GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"),
  104. GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"),
  105. GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""),
  106. GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"),
  107. GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"),
  108. GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
  109. GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records ("
  110. " uid INTEGER PRIMARY KEY,"
  111. " zone_private_key BLOB NOT NULL,"
  112. " pkey BLOB,"
  113. " rvalue INT8 NOT NULL,"
  114. " record_count INT NOT NULL,"
  115. " record_data BLOB NOT NULL,"
  116. " label TEXT NOT NULL"
  117. ")"),
  118. GNUNET_SQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_reverse "
  119. "ON ns098records (zone_private_key,pkey)"),
  120. GNUNET_SQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_iter "
  121. "ON ns098records (zone_private_key,uid)"),
  122. GNUNET_SQ_EXECUTE_STATEMENT_END
  123. };
  124. struct GNUNET_SQ_PrepareStatement ps[] = {
  125. GNUNET_SQ_make_prepare ("INSERT INTO ns098records "
  126. "(zone_private_key,pkey,rvalue,record_count,record_data,label)"
  127. " VALUES (?, ?, ?, ?, ?, ?)",
  128. &plugin->store_records),
  129. GNUNET_SQ_make_prepare ("DELETE FROM ns098records "
  130. "WHERE zone_private_key=? AND label=?",
  131. &plugin->delete_records),
  132. GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
  133. " FROM ns098records"
  134. " WHERE zone_private_key=? AND pkey=?",
  135. &plugin->zone_to_name),
  136. GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
  137. " FROM ns098records"
  138. " WHERE zone_private_key=? AND uid > ?"
  139. " ORDER BY uid ASC"
  140. " LIMIT ?",
  141. &plugin->iterate_zone),
  142. GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label,zone_private_key"
  143. " FROM ns098records"
  144. " WHERE uid > ?"
  145. " ORDER BY uid ASC"
  146. " LIMIT ?",
  147. &plugin->iterate_all_zones),
  148. GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
  149. " FROM ns098records"
  150. " WHERE zone_private_key=? AND label=?",
  151. &plugin->lookup_label),
  152. GNUNET_SQ_PREPARE_END
  153. };
  154. if (GNUNET_OK !=
  155. GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
  156. "namestore-sqlite",
  157. "FILENAME",
  158. &sqlite_filename))
  159. {
  160. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  161. "namestore-sqlite",
  162. "FILENAME");
  163. return GNUNET_SYSERR;
  164. }
  165. if (GNUNET_OK !=
  166. GNUNET_DISK_file_test (sqlite_filename))
  167. {
  168. if (GNUNET_OK !=
  169. GNUNET_DISK_directory_create_for_file (sqlite_filename))
  170. {
  171. GNUNET_break (0);
  172. GNUNET_free (sqlite_filename);
  173. return GNUNET_SYSERR;
  174. }
  175. }
  176. /* sqlite_filename should be UTF-8-encoded. If it isn't, it's a bug */
  177. plugin->fn = sqlite_filename;
  178. /* Open database and precompile statements */
  179. if (SQLITE_OK !=
  180. sqlite3_open (plugin->fn,
  181. &plugin->dbh))
  182. {
  183. LOG (GNUNET_ERROR_TYPE_ERROR,
  184. _("Unable to initialize SQLite: %s.\n"),
  185. sqlite3_errmsg (plugin->dbh));
  186. return GNUNET_SYSERR;
  187. }
  188. GNUNET_break (SQLITE_OK ==
  189. sqlite3_busy_timeout (plugin->dbh,
  190. BUSY_TIMEOUT_MS));
  191. if (GNUNET_OK !=
  192. GNUNET_SQ_exec_statements (plugin->dbh,
  193. es))
  194. {
  195. GNUNET_break (0);
  196. LOG (GNUNET_ERROR_TYPE_ERROR,
  197. _("Failed to setup database at `%s'\n"),
  198. plugin->fn);
  199. return GNUNET_SYSERR;
  200. }
  201. if (GNUNET_OK !=
  202. GNUNET_SQ_prepare (plugin->dbh,
  203. ps))
  204. {
  205. GNUNET_break (0);
  206. LOG (GNUNET_ERROR_TYPE_ERROR,
  207. _("Failed to setup database at `%s'\n"),
  208. plugin->fn);
  209. return GNUNET_SYSERR;
  210. }
  211. return GNUNET_OK;
  212. }
  213. /**
  214. * Shutdown database connection and associate data
  215. * structures.
  216. * @param plugin the plugin context (state for this module)
  217. */
  218. static void
  219. database_shutdown (struct Plugin *plugin)
  220. {
  221. int result;
  222. sqlite3_stmt *stmt;
  223. if (NULL != plugin->store_records)
  224. sqlite3_finalize (plugin->store_records);
  225. if (NULL != plugin->delete_records)
  226. sqlite3_finalize (plugin->delete_records);
  227. if (NULL != plugin->iterate_zone)
  228. sqlite3_finalize (plugin->iterate_zone);
  229. if (NULL != plugin->iterate_all_zones)
  230. sqlite3_finalize (plugin->iterate_all_zones);
  231. if (NULL != plugin->zone_to_name)
  232. sqlite3_finalize (plugin->zone_to_name);
  233. if (NULL != plugin->lookup_label)
  234. sqlite3_finalize (plugin->lookup_label);
  235. result = sqlite3_close (plugin->dbh);
  236. if (result == SQLITE_BUSY)
  237. {
  238. LOG (GNUNET_ERROR_TYPE_WARNING,
  239. _("Tried to close sqlite without finalizing all prepared statements.\n"));
  240. stmt = sqlite3_next_stmt (plugin->dbh,
  241. NULL);
  242. while (NULL != stmt)
  243. {
  244. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  245. "sqlite",
  246. "Closing statement %p\n",
  247. stmt);
  248. result = sqlite3_finalize (stmt);
  249. if (result != SQLITE_OK)
  250. GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
  251. "sqlite",
  252. "Failed to close statement %p: %d\n",
  253. stmt,
  254. result);
  255. stmt = sqlite3_next_stmt (plugin->dbh,
  256. NULL);
  257. }
  258. result = sqlite3_close (plugin->dbh);
  259. }
  260. if (SQLITE_OK != result)
  261. LOG_SQLITE (plugin,
  262. GNUNET_ERROR_TYPE_ERROR,
  263. "sqlite3_close");
  264. GNUNET_free_non_null (plugin->fn);
  265. }
  266. /**
  267. * Store a record in the datastore. Removes any existing record in the
  268. * same zone with the same name.
  269. *
  270. * @param cls closure (internal context for the plugin)
  271. * @param zone_key private key of the zone
  272. * @param label name that is being mapped (at most 255 characters long)
  273. * @param rd_count number of entries in @a rd array
  274. * @param rd array of records with data to store
  275. * @return #GNUNET_OK on success, else #GNUNET_SYSERR
  276. */
  277. static int
  278. namestore_sqlite_store_records (void *cls,
  279. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  280. const char *label,
  281. unsigned int rd_count,
  282. const struct GNUNET_GNSRECORD_Data *rd)
  283. {
  284. struct Plugin *plugin = cls;
  285. int n;
  286. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  287. uint64_t rvalue;
  288. ssize_t data_size;
  289. memset (&pkey,
  290. 0,
  291. sizeof (pkey));
  292. for (unsigned int i=0;i<rd_count;i++)
  293. if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
  294. {
  295. GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) ==
  296. rd[i].data_size);
  297. GNUNET_memcpy (&pkey,
  298. rd[i].data,
  299. rd[i].data_size);
  300. break;
  301. }
  302. rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  303. UINT64_MAX);
  304. data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
  305. rd);
  306. if (data_size < 0)
  307. {
  308. GNUNET_break (0);
  309. return GNUNET_SYSERR;
  310. }
  311. if (data_size > 64 * 65536)
  312. {
  313. GNUNET_break (0);
  314. return GNUNET_SYSERR;
  315. }
  316. {
  317. /* First delete 'old' records */
  318. char data[data_size];
  319. struct GNUNET_SQ_QueryParam dparams[] = {
  320. GNUNET_SQ_query_param_auto_from_type (zone_key),
  321. GNUNET_SQ_query_param_string (label),
  322. GNUNET_SQ_query_param_end
  323. };
  324. ssize_t ret;
  325. ret = GNUNET_GNSRECORD_records_serialize (rd_count,
  326. rd,
  327. data_size,
  328. data);
  329. if ( (ret < 0) ||
  330. (data_size != ret) )
  331. {
  332. GNUNET_break (0);
  333. return GNUNET_SYSERR;
  334. }
  335. if (GNUNET_OK !=
  336. GNUNET_SQ_bind (plugin->delete_records,
  337. dparams))
  338. {
  339. LOG_SQLITE (plugin,
  340. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  341. "sqlite3_bind_XXXX");
  342. GNUNET_SQ_reset (plugin->dbh,
  343. plugin->delete_records);
  344. return GNUNET_SYSERR;
  345. }
  346. n = sqlite3_step (plugin->delete_records);
  347. GNUNET_SQ_reset (plugin->dbh,
  348. plugin->delete_records);
  349. if (0 != rd_count)
  350. {
  351. uint32_t rd_count32 = (uint32_t) rd_count;
  352. struct GNUNET_SQ_QueryParam sparams[] = {
  353. GNUNET_SQ_query_param_auto_from_type (zone_key),
  354. GNUNET_SQ_query_param_auto_from_type (&pkey),
  355. GNUNET_SQ_query_param_uint64 (&rvalue),
  356. GNUNET_SQ_query_param_uint32 (&rd_count32),
  357. GNUNET_SQ_query_param_fixed_size (data, data_size),
  358. GNUNET_SQ_query_param_string (label),
  359. GNUNET_SQ_query_param_end
  360. };
  361. if (GNUNET_OK !=
  362. GNUNET_SQ_bind (plugin->store_records,
  363. sparams))
  364. {
  365. LOG_SQLITE (plugin,
  366. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  367. "sqlite3_bind_XXXX");
  368. GNUNET_SQ_reset (plugin->dbh,
  369. plugin->store_records);
  370. return GNUNET_SYSERR;
  371. }
  372. n = sqlite3_step (plugin->store_records);
  373. GNUNET_SQ_reset (plugin->dbh,
  374. plugin->store_records);
  375. }
  376. }
  377. switch (n)
  378. {
  379. case SQLITE_DONE:
  380. if (0 != rd_count)
  381. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  382. "sqlite",
  383. "Record stored\n");
  384. else
  385. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  386. "sqlite",
  387. "Record deleted\n");
  388. return GNUNET_OK;
  389. case SQLITE_BUSY:
  390. LOG_SQLITE (plugin,
  391. GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
  392. "sqlite3_step");
  393. return GNUNET_NO;
  394. default:
  395. LOG_SQLITE (plugin,
  396. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  397. "sqlite3_step");
  398. return GNUNET_SYSERR;
  399. }
  400. }
  401. /**
  402. * The given 'sqlite' statement has been prepared to be run.
  403. * It will return a record which should be given to the iterator.
  404. * Runs the statement and parses the returned record.
  405. *
  406. * @param plugin plugin context
  407. * @param stmt to run (and then clean up)
  408. * @param zone_key private key of the zone
  409. * @param limit maximum number of results to fetch
  410. * @param iter iterator to call with the result
  411. * @param iter_cls closure for @a iter
  412. * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
  413. */
  414. static int
  415. get_records_and_call_iterator (struct Plugin *plugin,
  416. sqlite3_stmt *stmt,
  417. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  418. uint64_t limit,
  419. GNUNET_NAMESTORE_RecordIterator iter,
  420. void *iter_cls)
  421. {
  422. int ret;
  423. int sret;
  424. ret = GNUNET_OK;
  425. for (uint64_t i = 0;i<limit;i++)
  426. {
  427. sret = sqlite3_step (stmt);
  428. if (SQLITE_DONE == sret)
  429. {
  430. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  431. "Iteration done (no results)\n");
  432. ret = GNUNET_NO;
  433. break;
  434. }
  435. if (SQLITE_ROW != sret)
  436. {
  437. LOG_SQLITE (plugin,
  438. GNUNET_ERROR_TYPE_ERROR,
  439. "sqlite_step");
  440. ret = GNUNET_SYSERR;
  441. break;
  442. }
  443. {
  444. uint64_t seq;
  445. uint32_t record_count;
  446. size_t data_size;
  447. void *data;
  448. char *label;
  449. struct GNUNET_CRYPTO_EcdsaPrivateKey zk;
  450. struct GNUNET_SQ_ResultSpec rs[] = {
  451. GNUNET_SQ_result_spec_uint64 (&seq),
  452. GNUNET_SQ_result_spec_uint32 (&record_count),
  453. GNUNET_SQ_result_spec_variable_size (&data,
  454. &data_size),
  455. GNUNET_SQ_result_spec_string (&label),
  456. GNUNET_SQ_result_spec_end
  457. };
  458. struct GNUNET_SQ_ResultSpec rsx[] = {
  459. GNUNET_SQ_result_spec_uint64 (&seq),
  460. GNUNET_SQ_result_spec_uint32 (&record_count),
  461. GNUNET_SQ_result_spec_variable_size (&data,
  462. &data_size),
  463. GNUNET_SQ_result_spec_string (&label),
  464. GNUNET_SQ_result_spec_auto_from_type (&zk),
  465. GNUNET_SQ_result_spec_end
  466. };
  467. ret = GNUNET_SQ_extract_result (stmt,
  468. (NULL == zone_key)
  469. ? rsx
  470. : rs);
  471. if ( (GNUNET_OK != ret) ||
  472. (record_count > 64 * 1024) )
  473. {
  474. /* sanity check, don't stack allocate far too much just
  475. because database might contain a large value here */
  476. GNUNET_break (0);
  477. ret = GNUNET_SYSERR;
  478. break;
  479. }
  480. else
  481. {
  482. struct GNUNET_GNSRECORD_Data rd[record_count];
  483. GNUNET_assert (0 != seq);
  484. if (GNUNET_OK !=
  485. GNUNET_GNSRECORD_records_deserialize (data_size,
  486. data,
  487. record_count,
  488. rd))
  489. {
  490. GNUNET_break (0);
  491. ret = GNUNET_SYSERR;
  492. break;
  493. }
  494. else
  495. {
  496. if (NULL != zone_key)
  497. zk = *zone_key;
  498. if (NULL != iter)
  499. iter (iter_cls,
  500. seq,
  501. &zk,
  502. label,
  503. record_count,
  504. rd);
  505. }
  506. }
  507. GNUNET_SQ_cleanup_result (rs);
  508. }
  509. }
  510. GNUNET_SQ_reset (plugin->dbh,
  511. stmt);
  512. return ret;
  513. }
  514. /**
  515. * Lookup records in the datastore for which we are the authority.
  516. *
  517. * @param cls closure (internal context for the plugin)
  518. * @param zone private key of the zone
  519. * @param label name of the record in the zone
  520. * @param iter function to call with the result
  521. * @param iter_cls closure for @a iter
  522. * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
  523. */
  524. static int
  525. namestore_sqlite_lookup_records (void *cls,
  526. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  527. const char *label,
  528. GNUNET_NAMESTORE_RecordIterator iter,
  529. void *iter_cls)
  530. {
  531. struct Plugin *plugin = cls;
  532. struct GNUNET_SQ_QueryParam params[] = {
  533. GNUNET_SQ_query_param_auto_from_type (zone),
  534. GNUNET_SQ_query_param_string (label),
  535. GNUNET_SQ_query_param_end
  536. };
  537. if (NULL == zone)
  538. {
  539. GNUNET_break (0);
  540. return GNUNET_SYSERR;
  541. }
  542. if (GNUNET_OK !=
  543. GNUNET_SQ_bind (plugin->lookup_label,
  544. params))
  545. {
  546. LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  547. "sqlite3_bind_XXXX");
  548. GNUNET_SQ_reset (plugin->dbh,
  549. plugin->lookup_label);
  550. return GNUNET_SYSERR;
  551. }
  552. return get_records_and_call_iterator (plugin,
  553. plugin->lookup_label,
  554. zone,
  555. 1,
  556. iter,
  557. iter_cls);
  558. }
  559. /**
  560. * Iterate over the results for a particular key and zone in the
  561. * datastore. Will return at most one result to the iterator.
  562. *
  563. * @param cls closure (internal context for the plugin)
  564. * @param zone hash of public key of the zone, NULL to iterate over all zones
  565. * @param serial serial number to exclude in the list of all matching records
  566. * @param limit maximum number of results to return
  567. * @param iter function to call with the result
  568. * @param iter_cls closure for @a iter
  569. * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
  570. */
  571. static int
  572. namestore_sqlite_iterate_records (void *cls,
  573. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  574. uint64_t serial,
  575. uint64_t limit,
  576. GNUNET_NAMESTORE_RecordIterator iter,
  577. void *iter_cls)
  578. {
  579. struct Plugin *plugin = cls;
  580. sqlite3_stmt *stmt;
  581. int err;
  582. if (NULL == zone)
  583. {
  584. struct GNUNET_SQ_QueryParam params[] = {
  585. GNUNET_SQ_query_param_uint64 (&serial),
  586. GNUNET_SQ_query_param_uint64 (&limit),
  587. GNUNET_SQ_query_param_end
  588. };
  589. stmt = plugin->iterate_all_zones;
  590. err = GNUNET_SQ_bind (stmt,
  591. params);
  592. }
  593. else
  594. {
  595. struct GNUNET_SQ_QueryParam params[] = {
  596. GNUNET_SQ_query_param_auto_from_type (zone),
  597. GNUNET_SQ_query_param_uint64 (&serial),
  598. GNUNET_SQ_query_param_uint64 (&limit),
  599. GNUNET_SQ_query_param_end
  600. };
  601. stmt = plugin->iterate_zone;
  602. err = GNUNET_SQ_bind (stmt,
  603. params);
  604. }
  605. if (GNUNET_OK != err)
  606. {
  607. LOG_SQLITE (plugin,
  608. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  609. "sqlite3_bind_XXXX");
  610. GNUNET_SQ_reset (plugin->dbh,
  611. stmt);
  612. return GNUNET_SYSERR;
  613. }
  614. return get_records_and_call_iterator (plugin,
  615. stmt,
  616. zone,
  617. limit,
  618. iter,
  619. iter_cls);
  620. }
  621. /**
  622. * Look for an existing PKEY delegation record for a given public key.
  623. * Returns at most one result to the iterator.
  624. *
  625. * @param cls closure (internal context for the plugin)
  626. * @param zone private key of the zone to look up in, never NULL
  627. * @param value_zone public key of the target zone (value), never NULL
  628. * @param iter function to call with the result
  629. * @param iter_cls closure for @a iter
  630. * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
  631. */
  632. static int
  633. namestore_sqlite_zone_to_name (void *cls,
  634. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  635. const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
  636. GNUNET_NAMESTORE_RecordIterator iter,
  637. void *iter_cls)
  638. {
  639. struct Plugin *plugin = cls;
  640. struct GNUNET_SQ_QueryParam params[] = {
  641. GNUNET_SQ_query_param_auto_from_type (zone),
  642. GNUNET_SQ_query_param_auto_from_type (value_zone),
  643. GNUNET_SQ_query_param_end
  644. };
  645. if (GNUNET_OK !=
  646. GNUNET_SQ_bind (plugin->zone_to_name,
  647. params))
  648. {
  649. LOG_SQLITE (plugin,
  650. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  651. "sqlite3_bind_XXXX");
  652. GNUNET_SQ_reset (plugin->dbh,
  653. plugin->zone_to_name);
  654. return GNUNET_SYSERR;
  655. }
  656. LOG (GNUNET_ERROR_TYPE_DEBUG,
  657. "Performing reverse lookup for `%s'\n",
  658. GNUNET_GNSRECORD_z2s (value_zone));
  659. return get_records_and_call_iterator (plugin,
  660. plugin->zone_to_name,
  661. zone,
  662. 1,
  663. iter,
  664. iter_cls);
  665. }
  666. /**
  667. * Entry point for the plugin.
  668. *
  669. * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
  670. * @return NULL on error, otherwise the plugin context
  671. */
  672. void *
  673. libgnunet_plugin_namestore_sqlite_init (void *cls)
  674. {
  675. static struct Plugin plugin;
  676. const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  677. struct GNUNET_NAMESTORE_PluginFunctions *api;
  678. if (NULL != plugin.cfg)
  679. return NULL; /* can only initialize once! */
  680. memset (&plugin,
  681. 0,
  682. sizeof (struct Plugin));
  683. plugin.cfg = cfg;
  684. if (GNUNET_OK != database_setup (&plugin))
  685. {
  686. database_shutdown (&plugin);
  687. return NULL;
  688. }
  689. api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
  690. api->cls = &plugin;
  691. api->store_records = &namestore_sqlite_store_records;
  692. api->iterate_records = &namestore_sqlite_iterate_records;
  693. api->zone_to_name = &namestore_sqlite_zone_to_name;
  694. api->lookup_records = &namestore_sqlite_lookup_records;
  695. LOG (GNUNET_ERROR_TYPE_INFO,
  696. _("Sqlite database running\n"));
  697. return api;
  698. }
  699. /**
  700. * Exit point from the plugin.
  701. *
  702. * @param cls the plugin context (as returned by "init")
  703. * @return always NULL
  704. */
  705. void *
  706. libgnunet_plugin_namestore_sqlite_done (void *cls)
  707. {
  708. struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
  709. struct Plugin *plugin = api->cls;
  710. database_shutdown (plugin);
  711. plugin->cfg = NULL;
  712. GNUNET_free (api);
  713. LOG (GNUNET_ERROR_TYPE_DEBUG,
  714. "sqlite plugin is finished\n");
  715. return NULL;
  716. }
  717. /* end of plugin_namestore_sqlite.c */