2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file identity/identity_api_lookup.c
23 * @brief api to lookup an ego
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_identity_service.h"
31 #define LOG(kind, ...) GNUNET_log_from (kind, "identity-api", __VA_ARGS__)
35 * Handle for ego lookup.
37 struct GNUNET_IDENTITY_EgoLookup
40 * Connection to service.
42 struct GNUNET_MQ_Handle *mq;
45 * Name of the ego we are looking up.
50 * Function to call with the result.
52 GNUNET_IDENTITY_EgoCallback cb;
62 * We received a result code from the service. Check the message
66 * @param rcm result message received
67 * @return #GNUNET_OK if the message is well-formed
70 check_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
72 if (sizeof(*rcm) != htons (rcm->header.size))
73 GNUNET_MQ_check_zero_termination (rcm);
79 * We received a result code from the service.
82 * @param rcm result message received
85 handle_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
87 struct GNUNET_IDENTITY_EgoLookup *el = cls;
89 el->cb (el->cb_cls, NULL);
90 GNUNET_IDENTITY_ego_lookup_cancel (el);
95 * Check validity of identity update message.
98 * @param um message received
99 * @return #GNUNET_OK if the message is well-formed
102 check_identity_update (void *cls, const struct UpdateMessage *um)
104 uint16_t size = ntohs (um->header.size);
105 uint16_t name_len = ntohs (um->name_len);
106 const char *str = (const char *) &um[1];
108 if ((size != name_len + sizeof(struct UpdateMessage)) ||
109 ((0 != name_len) && ('\0' != str[name_len - 1])))
112 return GNUNET_SYSERR;
119 * Handle identity update message.
122 * @param um message received
125 handle_identity_update (void *cls, const struct UpdateMessage *um)
127 struct GNUNET_IDENTITY_EgoLookup *el = cls;
128 uint16_t name_len = ntohs (um->name_len);
129 const char *str = (0 == name_len) ? NULL : (const char *) &um[1];
130 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
131 struct GNUNET_HashCode id;
132 struct GNUNET_IDENTITY_Ego ego;
134 GNUNET_break (GNUNET_YES != ntohs (um->end_of_list));
135 GNUNET_CRYPTO_ecdsa_key_get_public (&um->private_key, &pub);
136 GNUNET_CRYPTO_hash (&pub, sizeof(pub), &id);
137 ego.pk = um->private_key;
138 ego.name = (char *) str;
140 el->cb (el->cb_cls, &ego);
141 GNUNET_IDENTITY_ego_lookup_cancel (el);
146 * Generic error handler, called with the appropriate error code and
147 * the same closure specified at the creation of the message queue.
148 * Not every message queue implementation supports an error handler.
150 * @param cls closure with the `struct GNUNET_IDENTITY_EgoLookup *`
151 * @param error error code
154 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
156 struct GNUNET_IDENTITY_EgoLookup *el = cls;
158 el->cb (el->cb_cls, NULL);
163 * Lookup an ego by name.
165 * @param cfg configuration to use
166 * @param name name to look up
167 * @param cb callback to invoke with the result
168 * @param cb_cls closure for @a cb
169 * @return NULL on error
171 struct GNUNET_IDENTITY_EgoLookup *
172 GNUNET_IDENTITY_ego_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg,
174 GNUNET_IDENTITY_EgoCallback cb,
177 struct GNUNET_IDENTITY_EgoLookup *el;
178 struct GNUNET_MQ_Envelope *env;
179 struct GNUNET_MessageHeader *req;
182 GNUNET_assert (NULL != cb);
183 el = GNUNET_new (struct GNUNET_IDENTITY_EgoLookup);
187 struct GNUNET_MQ_MessageHandler handlers[] =
188 { GNUNET_MQ_hd_var_size (identity_result_code,
189 GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE,
190 struct ResultCodeMessage,
192 GNUNET_MQ_hd_var_size (identity_update,
193 GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE,
194 struct UpdateMessage,
196 GNUNET_MQ_handler_end () };
199 GNUNET_CLIENT_connect (cfg, "identity", handlers, &mq_error_handler, el);
207 el->name = GNUNET_strdup (name);
208 nlen = strlen (name) + 1;
209 env = GNUNET_MQ_msg_extra (req, nlen, GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP);
210 memcpy (&req[1], name, nlen);
211 GNUNET_MQ_send (el->mq, env);
217 * Abort ego lookup attempt.
219 * @param el handle for lookup to abort
222 GNUNET_IDENTITY_ego_lookup_cancel (struct GNUNET_IDENTITY_EgoLookup *el)
224 GNUNET_MQ_destroy (el->mq);
225 GNUNET_free (el->name);
230 /* end of identity_api_lookup.c */