identity_api_lookup.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_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_EgoLookup
  30. {
  31. /**
  32. * Connection to service.
  33. */
  34. struct GNUNET_MQ_Handle *mq;
  35. /**
  36. * Name of the ego we are looking up.
  37. */
  38. char *name;
  39. /**
  40. * Function to call with the result.
  41. */
  42. GNUNET_IDENTITY_EgoCallback 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. if (sizeof (*rcm) != htons (rcm->header.size))
  60. GNUNET_MQ_check_zero_termination (rcm);
  61. return GNUNET_OK;
  62. }
  63. /**
  64. * We received a result code from the service.
  65. *
  66. * @param cls closure
  67. * @param rcm result message received
  68. */
  69. static void
  70. handle_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
  71. {
  72. struct GNUNET_IDENTITY_EgoLookup *el = cls;
  73. el->cb (el->cb_cls, NULL);
  74. GNUNET_IDENTITY_ego_lookup_cancel (el);
  75. }
  76. /**
  77. * Check validity of identity update message.
  78. *
  79. * @param cls closure
  80. * @param um message received
  81. * @return #GNUNET_OK if the message is well-formed
  82. */
  83. static int
  84. check_identity_update (void *cls, const struct UpdateMessage *um)
  85. {
  86. uint16_t size = ntohs (um->header.size);
  87. uint16_t name_len = ntohs (um->name_len);
  88. const char *str = (const char *) &um[1];
  89. if ((size != name_len + sizeof (struct UpdateMessage)) ||
  90. ((0 != name_len) && ('\0' != str[name_len - 1])))
  91. {
  92. GNUNET_break (0);
  93. return GNUNET_SYSERR;
  94. }
  95. return GNUNET_OK;
  96. }
  97. /**
  98. * Handle identity update message.
  99. *
  100. * @param cls closure
  101. * @param um message received
  102. */
  103. static void
  104. handle_identity_update (void *cls, const struct UpdateMessage *um)
  105. {
  106. struct GNUNET_IDENTITY_EgoLookup *el = cls;
  107. uint16_t name_len = ntohs (um->name_len);
  108. const char *str = (0 == name_len) ? NULL : (const char *) &um[1];
  109. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  110. struct GNUNET_HashCode id;
  111. struct GNUNET_IDENTITY_Ego ego;
  112. GNUNET_break (GNUNET_YES != ntohs (um->end_of_list));
  113. GNUNET_CRYPTO_ecdsa_key_get_public (&um->private_key, &pub);
  114. GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
  115. ego.pk = (struct GNUNET_CRYPTO_EcdsaPrivateKey *) &um->private_key;
  116. ego.name = (char *) str;
  117. ego.id = id;
  118. el->cb (el->cb_cls, &ego);
  119. GNUNET_IDENTITY_ego_lookup_cancel (el);
  120. }
  121. /**
  122. * Generic error handler, called with the appropriate error code and
  123. * the same closure specified at the creation of the message queue.
  124. * Not every message queue implementation supports an error handler.
  125. *
  126. * @param cls closure with the `struct GNUNET_IDENTITY_EgoLookup *`
  127. * @param error error code
  128. */
  129. static void
  130. mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  131. {
  132. struct GNUNET_IDENTITY_EgoLookup *el = cls;
  133. el->cb (el->cb_cls, NULL);
  134. }
  135. /**
  136. * Lookup an ego by name.
  137. *
  138. * @param cfg configuration to use
  139. * @param name name to look up
  140. * @param cb callback to invoke with the result
  141. * @param cb_cls closure for @a cb
  142. * @return NULL on error
  143. */
  144. struct GNUNET_IDENTITY_EgoLookup *
  145. GNUNET_IDENTITY_ego_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg,
  146. const char *name,
  147. GNUNET_IDENTITY_EgoCallback cb,
  148. void *cb_cls)
  149. {
  150. struct GNUNET_IDENTITY_EgoLookup *el;
  151. struct GNUNET_MQ_Envelope *env;
  152. struct GNUNET_MessageHeader *req;
  153. size_t nlen;
  154. GNUNET_assert (NULL != cb);
  155. el = GNUNET_new (struct GNUNET_IDENTITY_EgoLookup);
  156. el->cb = cb;
  157. el->cb_cls = cb_cls;
  158. {
  159. struct GNUNET_MQ_MessageHandler handlers[] =
  160. {GNUNET_MQ_hd_var_size (identity_result_code,
  161. GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE,
  162. struct ResultCodeMessage,
  163. el),
  164. GNUNET_MQ_hd_var_size (identity_update,
  165. GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE,
  166. struct UpdateMessage,
  167. el),
  168. GNUNET_MQ_handler_end ()};
  169. el->mq =
  170. GNUNET_CLIENT_connect (cfg, "identity", handlers, &mq_error_handler, el);
  171. }
  172. if (NULL == el->mq)
  173. {
  174. GNUNET_break (0);
  175. GNUNET_free (el);
  176. return NULL;
  177. }
  178. el->name = GNUNET_strdup (name);
  179. nlen = strlen (name) + 1;
  180. env = GNUNET_MQ_msg_extra (req, nlen, GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP);
  181. memcpy (&req[1], name, nlen);
  182. GNUNET_MQ_send (el->mq, env);
  183. return el;
  184. }
  185. /**
  186. * Abort ego lookup attempt.
  187. *
  188. * @param el handle for lookup to abort
  189. */
  190. void
  191. GNUNET_IDENTITY_ego_lookup_cancel (struct GNUNET_IDENTITY_EgoLookup *el)
  192. {
  193. GNUNET_MQ_destroy (el->mq);
  194. GNUNET_free (el->name);
  195. GNUNET_free (el);
  196. }
  197. /* end of identity_api_lookup.c */