2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016, 2018 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/>.
19 * @file gns/gns_tld_api.c
20 * @brief library to access the GNS service, including TLD lookup
21 * @author Martin Schanzenbach
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_identity_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_dht_service.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-tld-api",__VA_ARGS__)
41 * Handle to a lookup request
43 struct GNUNET_GNS_LookupWithTldRequest
49 struct GNUNET_GNS_Handle *gns_handle;
52 * processor to call on lookup result
54 GNUNET_GNS_LookupResultProcessor2 lookup_proc;
57 * Domain name we are resolving.
62 * @e lookup_proc closure
64 void *lookup_proc_cls;
67 * Underlying GNS lookup.
69 struct GNUNET_GNS_LookupRequest *lr;
72 * Lookup an ego with the identity service.
74 struct GNUNET_IDENTITY_Handle *id_co;
77 * Name of the longest matching ego found so far.
78 * Must be freed on termination.
83 * Ego corresponding to @e longest_match.
85 struct GNUNET_IDENTITY_Ego *longest_match_ego;
88 * Desired result record type.
95 enum GNUNET_GNS_LocalOptions options;
100 * Obtain the TLD of the given @a name.
103 * @return the part of @a name after the last ".",
104 * or @a name if @a name does not contain a "."
107 get_tld (const char *name)
112 (unsigned char) '.');
116 tld++; /* skip the '.' */
122 * Eat the "TLD" (last bit) of the given @a name.
124 * @param[in,out] name a name
125 * @param tld what to eat (can be more than just the tld)
131 GNUNET_assert (0 < strlen (name));
135 GNUNET_GNS_EMPTY_LABEL_AT);
139 GNUNET_assert (strlen (tld) < strlen (name));
140 name[strlen(name) - strlen(tld) - 1] = '\0';
146 * Function called with the result of a GNS lookup.
148 * @param cls a `struct GNUNET_GNS_LookupWithTldRequest *`
149 * @param rd_count number of records returned
150 * @param rd array of @a rd_count records with the results
153 process_lookup_result (void *cls,
155 const struct GNUNET_GNSRECORD_Data *rd)
157 struct GNUNET_GNS_LookupWithTldRequest *ltr = cls;
160 ltr->lookup_proc (ltr->lookup_proc_cls,
164 GNUNET_GNS_lookup_with_tld_cancel (ltr);
169 * Perform the actual resolution, starting with the zone
170 * identified by the given public key.
172 * @param pkey public key to use for the zone, can be NULL
175 lookup_with_public_key (struct GNUNET_GNS_LookupWithTldRequest *ltr,
176 const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
178 ltr->lr = GNUNET_GNS_lookup (ltr->gns_handle,
183 &process_lookup_result,
189 * Method called to with the ego we are to use for the lookup,
190 * when the ego is determined by a name.
192 * @param cls a `struct GNUNET_GNS_LookupWithTldRequest *`
193 * @param ego ego handle, NULL at the end of the iteration
194 * @param ctx context we could store data to associate with @e ego
195 * @param name name of the ego
198 identity_zone_cb (void *cls,
199 struct GNUNET_IDENTITY_Ego *ego,
203 struct GNUNET_GNS_LookupWithTldRequest *ltr = cls;
204 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
208 if (NULL != ltr->longest_match)
210 /* Final case: TLD matches one of our egos */
211 // FIXME: eat all of the match (not just TLD!)
212 if (0 == strcmp (ltr->name,
215 /* name matches ego name perfectly, only "@" remains */
217 GNUNET_GNS_EMPTY_LABEL_AT);
221 GNUNET_assert (strlen (ltr->longest_match) < strlen (ltr->name));
222 ltr->name[strlen(ltr->name) - strlen (ltr->longest_match) - 1] = '\0';
225 /* if the name is of the form 'label' (and not 'label.SUBDOMAIN'), never go to the DHT */
226 GNUNET_free (ltr->longest_match);
227 ltr->longest_match = NULL;
228 if (NULL == strchr (ltr->name,
229 (unsigned char) '.'))
230 ltr->options = GNUNET_GNS_LO_NO_DHT;
232 ltr->options = GNUNET_GNS_LO_LOCAL_MASTER;
234 GNUNET_IDENTITY_ego_get_public_key (ltr->longest_match_ego,
236 GNUNET_IDENTITY_disconnect (ltr->id_co);
238 lookup_with_public_key (ltr,
243 /* no matching ego found */
244 GNUNET_IDENTITY_disconnect (ltr->id_co);
246 ltr->lookup_proc (ltr->lookup_proc_cls,
250 GNUNET_GNS_lookup_with_tld_cancel (ltr);
254 else if (NULL != name)
256 if ( (strlen (name) <= strlen (ltr->name)) &&
258 <r->name[strlen(ltr->name) - strlen (name)])) &&
259 ( (strlen (name) == strlen (ltr->name)) ||
260 ('.' == ltr->name[strlen(ltr->name) - strlen (name) - 1]) ) &&
261 ( (NULL == ltr->longest_match) ||
262 (strlen (name) > strlen (ltr->longest_match)) ) )
264 /* found better match, update! */
265 GNUNET_free_non_null (ltr->longest_match);
266 ltr->longest_match = GNUNET_strdup (name);
267 ltr->longest_match_ego = ego;
274 * Perform an asynchronous lookup operation on the GNS,
275 * determining the zone using the TLD of the given name
276 * and the current configuration to resolve TLDs to zones.
278 * @param handle handle to the GNS service
279 * @param name the name to look up, including TLD
280 * @param type the record type to look up
281 * @param options local options for the lookup
282 * @param proc processor to call on result
283 * @param proc_cls closure for @a proc
284 * @return handle to the get request, NULL on error (i.e. bad configuration)
286 struct GNUNET_GNS_LookupWithTldRequest *
287 GNUNET_GNS_lookup_with_tld (struct GNUNET_GNS_Handle *handle,
290 enum GNUNET_GNS_LocalOptions options,
291 GNUNET_GNS_LookupResultProcessor2 proc,
294 struct GNUNET_GNS_LookupWithTldRequest *ltr;
298 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
300 ltr = GNUNET_new (struct GNUNET_GNS_LookupWithTldRequest);
301 ltr->gns_handle = handle;
302 ltr->name = GNUNET_strdup (name);
304 ltr->options = options;
305 ltr->lookup_proc = proc;
306 ltr->lookup_proc_cls = proc_cls;
307 /* start with trivial case: TLD is zkey */
308 tld = get_tld (ltr->name);
310 GNUNET_CRYPTO_ecdsa_public_key_from_string (tld,
316 lookup_with_public_key (ltr,
321 /* second case: domain is mapped in our configuration file */
322 for (const char *domain = name;
324 domain = strchr (domain,
325 (unsigned char) '.'))
327 if ('.' == domain[0])
329 GNUNET_asprintf (&dot_tld,
333 GNUNET_CONFIGURATION_get_value_string (handle->cfg,
339 GNUNET_CRYPTO_ecdsa_public_key_from_string (zonestr,
343 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
346 _("Expected a base32-encoded public zone key\n"));
347 GNUNET_free (zonestr);
348 GNUNET_free (dot_tld);
349 GNUNET_free (ltr->name);
355 GNUNET_free (zonestr);
356 GNUNET_free (dot_tld);
357 lookup_with_public_key (ltr,
361 GNUNET_free (dot_tld);
364 ltr->id_co = GNUNET_IDENTITY_connect (ltr->gns_handle->cfg,
367 if (NULL == ltr->id_co)
369 GNUNET_free (ltr->name);
378 * Cancel pending lookup request
380 * @param ltr the lookup request to cancel
381 * @return closure from the lookup result processor
384 GNUNET_GNS_lookup_with_tld_cancel (struct GNUNET_GNS_LookupWithTldRequest *ltr)
386 void *ret = ltr->lookup_proc_cls;
388 if (NULL != ltr->id_co)
390 GNUNET_IDENTITY_disconnect (ltr->id_co);
395 GNUNET_GNS_lookup_cancel (ltr->lr);
398 GNUNET_free_non_null (ltr->longest_match);
399 GNUNET_free (ltr->name);
404 /* end of gns_tld_api.c */