gns_api.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2020 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 gns/gns_api.c
  18. * @brief library to access the GNS service
  19. * @author Martin Schanzenbach
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_constants.h"
  25. #include "gnunet_arm_service.h"
  26. #include "gnunet_hello_lib.h"
  27. #include "gnunet_protocols.h"
  28. #include "gnunet_dht_service.h"
  29. #include "gns.h"
  30. #include "gns_api.h"
  31. #define LOG(kind, ...) GNUNET_log_from (kind, "gns-api", __VA_ARGS__)
  32. /**
  33. * Default recursion depth limit to apply if
  34. * the application does not specify any.
  35. */
  36. #define DEFAULT_LIMIT 128
  37. /**
  38. * Handle to a lookup request
  39. */
  40. struct GNUNET_GNS_LookupRequest
  41. {
  42. /**
  43. * DLL
  44. */
  45. struct GNUNET_GNS_LookupRequest *next;
  46. /**
  47. * DLL
  48. */
  49. struct GNUNET_GNS_LookupRequest *prev;
  50. /**
  51. * handle to gns
  52. */
  53. struct GNUNET_GNS_Handle *gns_handle;
  54. /**
  55. * processor to call on lookup result
  56. */
  57. GNUNET_GNS_LookupResultProcessor lookup_proc;
  58. /**
  59. * @e lookup_proc closure
  60. */
  61. void *proc_cls;
  62. /**
  63. * Envelope with the message for this queue entry.
  64. */
  65. struct GNUNET_MQ_Envelope *env;
  66. /**
  67. * request id
  68. */
  69. uint32_t r_id;
  70. };
  71. /**
  72. * Reconnect to GNS service.
  73. *
  74. * @param handle the handle to the GNS service
  75. */
  76. static void
  77. reconnect (struct GNUNET_GNS_Handle *handle);
  78. /**
  79. * Reconnect to GNS
  80. *
  81. * @param cls the handle
  82. */
  83. static void
  84. reconnect_task (void *cls)
  85. {
  86. struct GNUNET_GNS_Handle *handle = cls;
  87. handle->reconnect_task = NULL;
  88. reconnect (handle);
  89. }
  90. /**
  91. * Disconnect from service and then reconnect.
  92. *
  93. * @param handle our handle
  94. */
  95. static void
  96. force_reconnect (struct GNUNET_GNS_Handle *handle)
  97. {
  98. GNUNET_MQ_destroy (handle->mq);
  99. handle->mq = NULL;
  100. handle->reconnect_backoff
  101. = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
  102. handle->reconnect_task
  103. = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
  104. &reconnect_task,
  105. handle);
  106. }
  107. /**
  108. * Generic error handler, called with the appropriate error code and
  109. * the same closure specified at the creation of the message queue.
  110. * Not every message queue implementation supports an error handler.
  111. *
  112. * @param cls closure with the `struct GNUNET_GNS_Handle *`
  113. * @param error error code
  114. */
  115. static void
  116. mq_error_handler (void *cls,
  117. enum GNUNET_MQ_Error error)
  118. {
  119. struct GNUNET_GNS_Handle *handle = cls;
  120. LOG (GNUNET_ERROR_TYPE_WARNING,
  121. "Problem with message queue. error: %i\n",
  122. error);
  123. force_reconnect (handle);
  124. }
  125. /**
  126. * Check validity of message received from the GNS service
  127. *
  128. * @param cls the `struct GNUNET_GNS_Handle *`
  129. * @param loookup_msg the incoming message
  130. */
  131. static int
  132. check_result (void *cls,
  133. const struct LookupResultMessage *lookup_msg)
  134. {
  135. size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg);
  136. uint32_t rd_count = ntohl (lookup_msg->rd_count);
  137. struct GNUNET_GNSRECORD_Data rd[rd_count];
  138. (void) cls;
  139. if (GNUNET_SYSERR ==
  140. GNUNET_GNSRECORD_records_deserialize (mlen,
  141. (const char *) &lookup_msg[1],
  142. rd_count,
  143. rd))
  144. {
  145. GNUNET_break (0);
  146. return GNUNET_SYSERR;
  147. }
  148. return GNUNET_OK;
  149. }
  150. /**
  151. * Handler for messages received from the GNS service
  152. *
  153. * @param cls the `struct GNUNET_GNS_Handle *`
  154. * @param loookup_msg the incoming message
  155. */
  156. static void
  157. handle_result (void *cls,
  158. const struct LookupResultMessage *lookup_msg)
  159. {
  160. struct GNUNET_GNS_Handle *handle = cls;
  161. size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg);
  162. uint32_t rd_count = ntohl (lookup_msg->rd_count);
  163. struct GNUNET_GNSRECORD_Data rd[rd_count];
  164. uint32_t r_id = ntohl (lookup_msg->id);
  165. struct GNUNET_GNS_LookupRequest *lr;
  166. GNUNET_GNS_LookupResultProcessor proc;
  167. void *proc_cls;
  168. LOG (GNUNET_ERROR_TYPE_DEBUG,
  169. "Received lookup reply from GNS service (%u records)\n",
  170. (unsigned int) rd_count);
  171. for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
  172. if (lr->r_id == r_id)
  173. break;
  174. if (NULL == lr)
  175. return;
  176. proc = lr->lookup_proc;
  177. proc_cls = lr->proc_cls;
  178. GNUNET_assert (GNUNET_OK ==
  179. GNUNET_GNSRECORD_records_deserialize (mlen,
  180. (const
  181. char *) &lookup_msg[1],
  182. rd_count,
  183. rd));
  184. proc (proc_cls,
  185. rd_count,
  186. rd);
  187. GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
  188. handle->lookup_tail,
  189. lr);
  190. if (NULL != lr->env)
  191. GNUNET_MQ_discard (lr->env);
  192. GNUNET_free (lr);
  193. }
  194. /**
  195. * Reconnect to GNS service.
  196. *
  197. * @param handle the handle to the GNS service
  198. */
  199. static void
  200. reconnect (struct GNUNET_GNS_Handle *handle)
  201. {
  202. struct GNUNET_MQ_MessageHandler handlers[] = {
  203. GNUNET_MQ_hd_var_size (result,
  204. GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
  205. struct LookupResultMessage,
  206. handle),
  207. GNUNET_MQ_handler_end ()
  208. };
  209. GNUNET_assert (NULL == handle->mq);
  210. LOG (GNUNET_ERROR_TYPE_DEBUG,
  211. "Trying to connect to GNS\n");
  212. handle->mq = GNUNET_CLIENT_connect (handle->cfg,
  213. "gns",
  214. handlers,
  215. &mq_error_handler,
  216. handle);
  217. if (NULL == handle->mq)
  218. return;
  219. for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head;
  220. NULL != lh;
  221. lh = lh->next)
  222. GNUNET_MQ_send_copy (handle->mq,
  223. lh->env);
  224. }
  225. /**
  226. * Initialize the connection with the GNS service.
  227. *
  228. * @param cfg configuration to use
  229. * @return handle to the GNS service, or NULL on error
  230. */
  231. struct GNUNET_GNS_Handle *
  232. GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  233. {
  234. struct GNUNET_GNS_Handle *handle;
  235. handle = GNUNET_new (struct GNUNET_GNS_Handle);
  236. handle->cfg = cfg;
  237. reconnect (handle);
  238. if (NULL == handle->mq)
  239. {
  240. GNUNET_free (handle);
  241. return NULL;
  242. }
  243. return handle;
  244. }
  245. /**
  246. * Shutdown connection with the GNS service.
  247. *
  248. * @param handle handle of the GNS connection to stop
  249. */
  250. void
  251. GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
  252. {
  253. if (NULL != handle->mq)
  254. {
  255. GNUNET_MQ_destroy (handle->mq);
  256. handle->mq = NULL;
  257. }
  258. if (NULL != handle->reconnect_task)
  259. {
  260. GNUNET_SCHEDULER_cancel (handle->reconnect_task);
  261. handle->reconnect_task = NULL;
  262. }
  263. GNUNET_assert (NULL == handle->lookup_head);
  264. GNUNET_free (handle);
  265. }
  266. /**
  267. * Cancel pending lookup request
  268. *
  269. * @param lr the lookup request to cancel
  270. * @return closure from the lookup result processor
  271. */
  272. void *
  273. GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
  274. {
  275. struct GNUNET_GNS_Handle *handle = lr->gns_handle;
  276. void *ret;
  277. GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
  278. handle->lookup_tail,
  279. lr);
  280. GNUNET_MQ_discard (lr->env);
  281. ret = lr->proc_cls;
  282. GNUNET_free (lr);
  283. return ret;
  284. }
  285. /**
  286. * Perform an asynchronous lookup operation on the GNS.
  287. *
  288. * @param handle handle to the GNS service
  289. * @param name the name to look up (in UTF-8 encoding)
  290. * @param zone zone to look in
  291. * @param type the GNS record type to look for
  292. * @param options local options for the lookup
  293. * @param recursion_depth_limit maximum number of zones
  294. * that the lookup may (still) traverse
  295. * @param proc function to call on result
  296. * @param proc_cls closure for @a proc
  297. * @return handle to the queued request
  298. */
  299. struct GNUNET_GNS_LookupRequest *
  300. GNUNET_GNS_lookup_limited (struct GNUNET_GNS_Handle *handle,
  301. const char *name,
  302. const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
  303. uint32_t type,
  304. enum GNUNET_GNS_LocalOptions options,
  305. uint16_t recursion_depth_limit,
  306. GNUNET_GNS_LookupResultProcessor proc,
  307. void *proc_cls)
  308. {
  309. /* IPC to shorten gns names, return shorten_handle */
  310. struct LookupMessage *lookup_msg;
  311. struct GNUNET_GNS_LookupRequest *lr;
  312. size_t nlen;
  313. if (NULL == name)
  314. {
  315. GNUNET_break (0);
  316. return NULL;
  317. }
  318. LOG (GNUNET_ERROR_TYPE_DEBUG,
  319. "Trying to lookup `%s' in GNS\n",
  320. name);
  321. nlen = strlen (name) + 1;
  322. if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*lr))
  323. {
  324. GNUNET_break (0);
  325. return NULL;
  326. }
  327. lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
  328. lr->gns_handle = handle;
  329. lr->lookup_proc = proc;
  330. lr->proc_cls = proc_cls;
  331. lr->r_id = handle->r_id_gen++;
  332. lr->env = GNUNET_MQ_msg_extra (lookup_msg,
  333. nlen,
  334. GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
  335. lookup_msg->id = htonl (lr->r_id);
  336. lookup_msg->options = htons ((uint16_t) options);
  337. lookup_msg->recursion_depth_limit
  338. = htons (recursion_depth_limit);
  339. lookup_msg->zone = *zone;
  340. lookup_msg->type = htonl (type);
  341. GNUNET_memcpy (&lookup_msg[1],
  342. name,
  343. nlen);
  344. GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
  345. handle->lookup_tail,
  346. lr);
  347. if (NULL != handle->mq)
  348. GNUNET_MQ_send_copy (handle->mq,
  349. lr->env);
  350. return lr;
  351. }
  352. /**
  353. * Perform an asynchronous lookup operation on the GNS.
  354. *
  355. * @param handle handle to the GNS service
  356. * @param name the name to look up (in UTF-8 encoding)
  357. * @param zone the zone to start the resolution in
  358. * @param type the record type to look up
  359. * @param options local options for the lookup
  360. * @param proc processor to call on result
  361. * @param proc_cls closure for @a proc
  362. * @return handle to the get request
  363. */
  364. struct GNUNET_GNS_LookupRequest*
  365. GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
  366. const char *name,
  367. const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
  368. uint32_t type,
  369. enum GNUNET_GNS_LocalOptions options,
  370. GNUNET_GNS_LookupResultProcessor proc,
  371. void *proc_cls)
  372. {
  373. return GNUNET_GNS_lookup_limited (handle,
  374. name,
  375. zone,
  376. type,
  377. options,
  378. DEFAULT_LIMIT,
  379. proc,
  380. proc_cls);
  381. }
  382. /* end of gns_api.c */