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