aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gns_tld_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gns/gns_tld_api.c')
-rw-r--r--src/gns/gns_tld_api.c236
1 files changed, 117 insertions, 119 deletions
diff --git a/src/gns/gns_tld_api.c b/src/gns/gns_tld_api.c
index f36b31acf..0692199e3 100644
--- a/src/gns/gns_tld_api.c
+++ b/src/gns/gns_tld_api.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file gns/gns_tld_api.c 21 * @file gns/gns_tld_api.c
22 * @brief library to access the GNS service, including TLD lookup 22 * @brief library to access the GNS service, including TLD lookup
@@ -35,15 +35,13 @@
35#include "gns_api.h" 35#include "gns_api.h"
36 36
37 37
38#define LOG(kind, ...) GNUNET_log_from (kind, "gns-tld-api", __VA_ARGS__) 38#define LOG(kind, ...) GNUNET_log_from(kind, "gns-tld-api", __VA_ARGS__)
39 39
40 40
41/** 41/**
42 * Handle to a lookup request 42 * Handle to a lookup request
43 */ 43 */
44struct GNUNET_GNS_LookupWithTldRequest 44struct GNUNET_GNS_LookupWithTldRequest {
45{
46
47 /** 45 /**
48 * handle to gns 46 * handle to gns
49 */ 47 */
@@ -105,11 +103,11 @@ struct GNUNET_GNS_LookupWithTldRequest
105 * or @a name if @a name does not contain a "." 103 * or @a name if @a name does not contain a "."
106 */ 104 */
107static const char * 105static const char *
108get_tld (const char *name) 106get_tld(const char *name)
109{ 107{
110 const char *tld; 108 const char *tld;
111 109
112 tld = strrchr (name, (unsigned char) '.'); 110 tld = strrchr(name, (unsigned char)'.');
113 if (NULL == tld) 111 if (NULL == tld)
114 tld = name; 112 tld = name;
115 else 113 else
@@ -125,18 +123,18 @@ get_tld (const char *name)
125 * @param tld what to eat (can be more than just the tld) 123 * @param tld what to eat (can be more than just the tld)
126 */ 124 */
127static void 125static void
128eat_tld (char *name, const char *tld) 126eat_tld(char *name, const char *tld)
129{ 127{
130 GNUNET_assert (0 < strlen (name)); 128 GNUNET_assert(0 < strlen(name));
131 if ((NULL == tld) || (strlen (name) == strlen (tld))) 129 if ((NULL == tld) || (strlen(name) == strlen(tld)))
132 { 130 {
133 strcpy (name, GNUNET_GNS_EMPTY_LABEL_AT); 131 strcpy(name, GNUNET_GNS_EMPTY_LABEL_AT);
134 } 132 }
135 else 133 else
136 { 134 {
137 GNUNET_assert (strlen (tld) < strlen (name)); 135 GNUNET_assert(strlen(tld) < strlen(name));
138 name[strlen (name) - strlen (tld) - 1] = '\0'; 136 name[strlen(name) - strlen(tld) - 1] = '\0';
139 } 137 }
140} 138}
141 139
142 140
@@ -148,15 +146,15 @@ eat_tld (char *name, const char *tld)
148 * @param rd array of @a rd_count records with the results 146 * @param rd array of @a rd_count records with the results
149 */ 147 */
150static void 148static void
151process_lookup_result (void *cls, 149process_lookup_result(void *cls,
152 uint32_t rd_count, 150 uint32_t rd_count,
153 const struct GNUNET_GNSRECORD_Data *rd) 151 const struct GNUNET_GNSRECORD_Data *rd)
154{ 152{
155 struct GNUNET_GNS_LookupWithTldRequest *ltr = cls; 153 struct GNUNET_GNS_LookupWithTldRequest *ltr = cls;
156 154
157 ltr->lr = NULL; 155 ltr->lr = NULL;
158 ltr->lookup_proc (ltr->lookup_proc_cls, GNUNET_YES, rd_count, rd); 156 ltr->lookup_proc(ltr->lookup_proc_cls, GNUNET_YES, rd_count, rd);
159 GNUNET_GNS_lookup_with_tld_cancel (ltr); 157 GNUNET_GNS_lookup_with_tld_cancel(ltr);
160} 158}
161 159
162 160
@@ -167,16 +165,16 @@ process_lookup_result (void *cls,
167 * @param pkey public key to use for the zone, can be NULL 165 * @param pkey public key to use for the zone, can be NULL
168 */ 166 */
169static void 167static void
170lookup_with_public_key (struct GNUNET_GNS_LookupWithTldRequest *ltr, 168lookup_with_public_key(struct GNUNET_GNS_LookupWithTldRequest *ltr,
171 const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey) 169 const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
172{ 170{
173 ltr->lr = GNUNET_GNS_lookup (ltr->gns_handle, 171 ltr->lr = GNUNET_GNS_lookup(ltr->gns_handle,
174 ltr->name, 172 ltr->name,
175 pkey, 173 pkey,
176 ltr->type, 174 ltr->type,
177 ltr->options, 175 ltr->options,
178 &process_lookup_result, 176 &process_lookup_result,
179 ltr); 177 ltr);
180} 178}
181 179
182 180
@@ -190,38 +188,38 @@ lookup_with_public_key (struct GNUNET_GNS_LookupWithTldRequest *ltr,
190 * @param name name of the ego 188 * @param name name of the ego
191 */ 189 */
192static void 190static void
193identity_zone_cb (void *cls, 191identity_zone_cb(void *cls,
194 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, 192 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
195 const char *ego_name) 193 const char *ego_name)
196{ 194{
197 struct GNUNET_GNS_LookupWithTldRequest *ltr = cls; 195 struct GNUNET_GNS_LookupWithTldRequest *ltr = cls;
198 struct GNUNET_CRYPTO_EcdsaPublicKey pkey; 196 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
199 197
200 ltr->id_co = NULL; 198 ltr->id_co = NULL;
201 if (NULL == priv) 199 if (NULL == priv)
202 { 200 {
203 /* no matching ego found */ 201 /* no matching ego found */
204 ltr->lookup_proc (ltr->lookup_proc_cls, GNUNET_NO, 0, NULL); 202 ltr->lookup_proc(ltr->lookup_proc_cls, GNUNET_NO, 0, NULL);
205 return; 203 return;
206 } 204 }
207 /* Final case: TLD matches one of our egos */ 205 /* Final case: TLD matches one of our egos */
208 if (0 == strcmp (ltr->name, ego_name)) 206 if (0 == strcmp(ltr->name, ego_name))
209 { 207 {
210 /* name matches ego name perfectly, only "@" remains */ 208 /* name matches ego name perfectly, only "@" remains */
211 strcpy (ltr->name, GNUNET_GNS_EMPTY_LABEL_AT); 209 strcpy(ltr->name, GNUNET_GNS_EMPTY_LABEL_AT);
212 } 210 }
213 else 211 else
214 { 212 {
215 GNUNET_assert (strlen (ego_name) < strlen (ltr->name)); 213 GNUNET_assert(strlen(ego_name) < strlen(ltr->name));
216 ltr->name[strlen (ltr->name) - strlen (ego_name) - 1] = '\0'; 214 ltr->name[strlen(ltr->name) - strlen(ego_name) - 1] = '\0';
217 } 215 }
218 /* if the name is of the form 'label' (and not 'label.SUBDOMAIN'), never go to the DHT */ 216 /* if the name is of the form 'label' (and not 'label.SUBDOMAIN'), never go to the DHT */
219 if (NULL == strchr (ltr->name, (unsigned char) '.')) 217 if (NULL == strchr(ltr->name, (unsigned char)'.'))
220 ltr->options = GNUNET_GNS_LO_NO_DHT; 218 ltr->options = GNUNET_GNS_LO_NO_DHT;
221 else 219 else
222 ltr->options = GNUNET_GNS_LO_LOCAL_MASTER; 220 ltr->options = GNUNET_GNS_LO_LOCAL_MASTER;
223 GNUNET_CRYPTO_ecdsa_key_get_public (priv, &pkey); 221 GNUNET_CRYPTO_ecdsa_key_get_public(priv, &pkey);
224 lookup_with_public_key (ltr, &pkey); 222 lookup_with_public_key(ltr, &pkey);
225} 223}
226 224
227 225
@@ -239,12 +237,12 @@ identity_zone_cb (void *cls,
239 * @return handle to the get request, NULL on error (i.e. bad configuration) 237 * @return handle to the get request, NULL on error (i.e. bad configuration)
240 */ 238 */
241struct GNUNET_GNS_LookupWithTldRequest * 239struct GNUNET_GNS_LookupWithTldRequest *
242GNUNET_GNS_lookup_with_tld (struct GNUNET_GNS_Handle *handle, 240GNUNET_GNS_lookup_with_tld(struct GNUNET_GNS_Handle *handle,
243 const char *name, 241 const char *name,
244 uint32_t type, 242 uint32_t type,
245 enum GNUNET_GNS_LocalOptions options, 243 enum GNUNET_GNS_LocalOptions options,
246 GNUNET_GNS_LookupResultProcessor2 proc, 244 GNUNET_GNS_LookupResultProcessor2 proc,
247 void *proc_cls) 245 void *proc_cls)
248{ 246{
249 struct GNUNET_GNS_LookupWithTldRequest *ltr; 247 struct GNUNET_GNS_LookupWithTldRequest *ltr;
250 const char *tld; 248 const char *tld;
@@ -252,70 +250,70 @@ GNUNET_GNS_lookup_with_tld (struct GNUNET_GNS_Handle *handle,
252 char *zonestr; 250 char *zonestr;
253 struct GNUNET_CRYPTO_EcdsaPublicKey pkey; 251 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
254 252
255 ltr = GNUNET_new (struct GNUNET_GNS_LookupWithTldRequest); 253 ltr = GNUNET_new(struct GNUNET_GNS_LookupWithTldRequest);
256 ltr->gns_handle = handle; 254 ltr->gns_handle = handle;
257 ltr->name = GNUNET_strdup (name); 255 ltr->name = GNUNET_strdup(name);
258 ltr->type = type; 256 ltr->type = type;
259 ltr->options = options; 257 ltr->options = options;
260 ltr->lookup_proc = proc; 258 ltr->lookup_proc = proc;
261 ltr->lookup_proc_cls = proc_cls; 259 ltr->lookup_proc_cls = proc_cls;
262 /* start with trivial case: TLD is zkey */ 260 /* start with trivial case: TLD is zkey */
263 tld = get_tld (ltr->name); 261 tld = get_tld(ltr->name);
264 if (GNUNET_OK == 262 if (GNUNET_OK ==
265 GNUNET_CRYPTO_ecdsa_public_key_from_string (tld, strlen (tld), &pkey)) 263 GNUNET_CRYPTO_ecdsa_public_key_from_string(tld, strlen(tld), &pkey))
266 { 264 {
267 eat_tld (ltr->name, tld); 265 eat_tld(ltr->name, tld);
268 lookup_with_public_key (ltr, &pkey); 266 lookup_with_public_key(ltr, &pkey);
269 return ltr; 267 return ltr;
270 } 268 }
271 269
272 /* second case: domain is mapped in our configuration file */ 270 /* second case: domain is mapped in our configuration file */
273 for (const char *domain = name; NULL != domain; 271 for (const char *domain = name; NULL != domain;
274 domain = strchr (domain, (unsigned char) '.')) 272 domain = strchr(domain, (unsigned char)'.'))
275 {
276 if ('.' == domain[0])
277 domain++;
278 GNUNET_asprintf (&dot_tld, ".%s", domain);
279 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (handle->cfg,
280 "gns",
281 dot_tld,
282 &zonestr))
283 { 273 {
284 if (GNUNET_OK != 274 if ('.' == domain[0])
285 GNUNET_CRYPTO_ecdsa_public_key_from_string (zonestr, 275 domain++;
286 strlen (zonestr), 276 GNUNET_asprintf(&dot_tld, ".%s", domain);
287 &pkey)) 277 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(handle->cfg,
288 { 278 "gns",
289 GNUNET_log_config_invalid ( 279 dot_tld,
290 GNUNET_ERROR_TYPE_ERROR, 280 &zonestr))
291 "gns", 281 {
292 dot_tld, 282 if (GNUNET_OK !=
293 _ ("Expected a base32-encoded public zone key\n")); 283 GNUNET_CRYPTO_ecdsa_public_key_from_string(zonestr,
294 GNUNET_free (zonestr); 284 strlen(zonestr),
295 GNUNET_free (dot_tld); 285 &pkey))
296 GNUNET_free (ltr->name); 286 {
297 GNUNET_free (ltr); 287 GNUNET_log_config_invalid(
298 return NULL; 288 GNUNET_ERROR_TYPE_ERROR,
299 } 289 "gns",
300 eat_tld (ltr->name, &dot_tld[1]); 290 dot_tld,
301 GNUNET_free (zonestr); 291 _("Expected a base32-encoded public zone key\n"));
302 GNUNET_free (dot_tld); 292 GNUNET_free(zonestr);
303 lookup_with_public_key (ltr, &pkey); 293 GNUNET_free(dot_tld);
304 return ltr; 294 GNUNET_free(ltr->name);
295 GNUNET_free(ltr);
296 return NULL;
297 }
298 eat_tld(ltr->name, &dot_tld[1]);
299 GNUNET_free(zonestr);
300 GNUNET_free(dot_tld);
301 lookup_with_public_key(ltr, &pkey);
302 return ltr;
303 }
304 GNUNET_free(dot_tld);
305 } 305 }
306 GNUNET_free (dot_tld);
307 }
308 ltr->id_co = 306 ltr->id_co =
309 GNUNET_IDENTITY_ego_lookup_by_suffix (ltr->gns_handle->cfg, 307 GNUNET_IDENTITY_ego_lookup_by_suffix(ltr->gns_handle->cfg,
310 ltr->name, 308 ltr->name,
311 &identity_zone_cb, 309 &identity_zone_cb,
312 ltr); 310 ltr);
313 if (NULL == ltr->id_co) 311 if (NULL == ltr->id_co)
314 { 312 {
315 GNUNET_free (ltr->name); 313 GNUNET_free(ltr->name);
316 GNUNET_free (ltr); 314 GNUNET_free(ltr);
317 return NULL; 315 return NULL;
318 } 316 }
319 return ltr; 317 return ltr;
320} 318}
321 319
@@ -327,23 +325,23 @@ GNUNET_GNS_lookup_with_tld (struct GNUNET_GNS_Handle *handle,
327 * @return closure from the lookup result processor 325 * @return closure from the lookup result processor
328 */ 326 */
329void * 327void *
330GNUNET_GNS_lookup_with_tld_cancel (struct GNUNET_GNS_LookupWithTldRequest *ltr) 328GNUNET_GNS_lookup_with_tld_cancel(struct GNUNET_GNS_LookupWithTldRequest *ltr)
331{ 329{
332 void *ret = ltr->lookup_proc_cls; 330 void *ret = ltr->lookup_proc_cls;
333 331
334 if (NULL != ltr->id_co) 332 if (NULL != ltr->id_co)
335 { 333 {
336 GNUNET_IDENTITY_ego_lookup_by_suffix_cancel (ltr->id_co); 334 GNUNET_IDENTITY_ego_lookup_by_suffix_cancel(ltr->id_co);
337 ltr->id_co = NULL; 335 ltr->id_co = NULL;
338 } 336 }
339 if (NULL != ltr->lr) 337 if (NULL != ltr->lr)
340 { 338 {
341 GNUNET_GNS_lookup_cancel (ltr->lr); 339 GNUNET_GNS_lookup_cancel(ltr->lr);
342 ltr->lr = NULL; 340 ltr->lr = NULL;
343 } 341 }
344 GNUNET_free_non_null (ltr->longest_match); 342 GNUNET_free_non_null(ltr->longest_match);
345 GNUNET_free (ltr->name); 343 GNUNET_free(ltr->name);
346 GNUNET_free (ltr); 344 GNUNET_free(ltr);
347 return ret; 345 return ret;
348} 346}
349 347