identity_api_suffix_lookup.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 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 identity/identity_api_suffix_lookup.c
  18. * @brief api to lookup an ego
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_identity_service.h"
  24. #include "identity.h"
  25. #define LOG(kind, ...) GNUNET_log_from (kind, "identity-api", __VA_ARGS__)
  26. /**
  27. * Handle for ego lookup.
  28. */
  29. struct GNUNET_IDENTITY_EgoSuffixLookup
  30. {
  31. /**
  32. * Connection to service.
  33. */
  34. struct GNUNET_MQ_Handle *mq;
  35. /**
  36. * Suffix we are looking up.
  37. */
  38. char *suffix;
  39. /**
  40. * Function to call with the result.
  41. */
  42. GNUNET_IDENTITY_EgoSuffixCallback cb;
  43. /**
  44. * Closure for @e cb
  45. */
  46. void *cb_cls;
  47. };
  48. /**
  49. * We received a result code from the service. Check the message
  50. * is well-formed.
  51. *
  52. * @param cls closure
  53. * @param rcm result message received
  54. * @return #GNUNET_OK if the message is well-formed
  55. */
  56. static int
  57. check_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
  58. {
  59. (void) cls;
  60. if (sizeof(*rcm) != htons (rcm->header.size))
  61. GNUNET_MQ_check_zero_termination (rcm);
  62. return GNUNET_OK;
  63. }
  64. /**
  65. * We received a result code from the service.
  66. *
  67. * @param cls closure
  68. * @param rcm result message received
  69. */
  70. static void
  71. handle_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
  72. {
  73. struct GNUNET_IDENTITY_EgoSuffixLookup *el = cls;
  74. (void) rcm;
  75. el->cb (el->cb_cls, NULL, NULL);
  76. GNUNET_IDENTITY_ego_lookup_by_suffix_cancel (el);
  77. }
  78. /**
  79. * Check validity of identity update message.
  80. *
  81. * @param cls closure
  82. * @param um message received
  83. * @return #GNUNET_OK if the message is well-formed
  84. */
  85. static int
  86. check_identity_update (void *cls, const struct UpdateMessage *um)
  87. {
  88. uint16_t size = ntohs (um->header.size);
  89. uint16_t name_len = ntohs (um->name_len);
  90. const char *str = (const char *) &um[1];
  91. (void) cls;
  92. if ((size != name_len + sizeof(struct UpdateMessage)) ||
  93. ((0 != name_len) && ('\0' != str[name_len - 1])))
  94. {
  95. GNUNET_break (0);
  96. return GNUNET_SYSERR;
  97. }
  98. return GNUNET_OK;
  99. }
  100. /**
  101. * Handle identity update message.
  102. *
  103. * @param cls closure
  104. * @param um message received
  105. */
  106. static void
  107. handle_identity_update (void *cls, const struct UpdateMessage *um)
  108. {
  109. struct GNUNET_IDENTITY_EgoSuffixLookup *el = cls;
  110. uint16_t name_len = ntohs (um->name_len);
  111. const char *str = (0 == name_len) ? NULL : (const char *) &um[1];
  112. el->cb (el->cb_cls, &um->private_key, str);
  113. GNUNET_IDENTITY_ego_lookup_by_suffix_cancel (el);
  114. }
  115. /**
  116. * Generic error handler, called with the appropriate error code and
  117. * the same closure specified at the creation of the message queue.
  118. * Not every message queue implementation supports an error handler.
  119. *
  120. * @param cls closure with the `struct GNUNET_IDENTITY_EgoSuffixLookup *`
  121. * @param error error code
  122. */
  123. static void
  124. mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  125. {
  126. struct GNUNET_IDENTITY_EgoSuffixLookup *el = cls;
  127. (void) error;
  128. el->cb (el->cb_cls, NULL, NULL);
  129. GNUNET_IDENTITY_ego_lookup_by_suffix_cancel (el);
  130. }
  131. /**
  132. * Lookup an ego by name.
  133. *
  134. * @param cfg configuration to use
  135. * @param name name to look up
  136. * @param cb callback to invoke with the result
  137. * @param cb_cls closure for @a cb
  138. * @return NULL on error
  139. */
  140. struct GNUNET_IDENTITY_EgoSuffixLookup *
  141. GNUNET_IDENTITY_ego_lookup_by_suffix (const struct
  142. GNUNET_CONFIGURATION_Handle *cfg,
  143. const char *suffix,
  144. GNUNET_IDENTITY_EgoSuffixCallback cb,
  145. void *cb_cls)
  146. {
  147. struct GNUNET_IDENTITY_EgoSuffixLookup *el;
  148. struct GNUNET_MQ_Envelope *env;
  149. struct GNUNET_MessageHeader *req;
  150. size_t nlen;
  151. GNUNET_assert (NULL != cb);
  152. el = GNUNET_new (struct GNUNET_IDENTITY_EgoSuffixLookup);
  153. el->cb = cb;
  154. el->cb_cls = cb_cls;
  155. {
  156. struct GNUNET_MQ_MessageHandler handlers[] =
  157. { GNUNET_MQ_hd_var_size (identity_result_code,
  158. GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE,
  159. struct ResultCodeMessage,
  160. el),
  161. GNUNET_MQ_hd_var_size (identity_update,
  162. GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE,
  163. struct UpdateMessage,
  164. el),
  165. GNUNET_MQ_handler_end () };
  166. el->mq =
  167. GNUNET_CLIENT_connect (cfg, "identity", handlers, &mq_error_handler, el);
  168. }
  169. if (NULL == el->mq)
  170. {
  171. GNUNET_break (0);
  172. GNUNET_free (el);
  173. return NULL;
  174. }
  175. el->suffix = GNUNET_strdup (suffix);
  176. nlen = strlen (suffix) + 1;
  177. env = GNUNET_MQ_msg_extra (req, nlen,
  178. GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP_BY_SUFFIX);
  179. memcpy (&req[1], suffix, nlen);
  180. GNUNET_MQ_send (el->mq, env);
  181. return el;
  182. }
  183. /**
  184. * Abort ego lookup attempt.
  185. *
  186. * @param el handle for lookup to abort
  187. */
  188. void
  189. GNUNET_IDENTITY_ego_lookup_by_suffix_cancel (struct
  190. GNUNET_IDENTITY_EgoSuffixLookup *el)
  191. {
  192. GNUNET_MQ_destroy (el->mq);
  193. GNUNET_free (el->suffix);
  194. GNUNET_free (el);
  195. }
  196. /* end of identity_api_suffix_lookup.c */