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_EgoLookup *id_op;
77 * Desired result record type.
84 enum GNUNET_GNS_LocalOptions options;
89 * Obtain the TLD of the given @a name.
92 * @return the part of @a name after the last ".",
93 * or @a name if @a name does not contain a "."
96 get_tld (const char *name)
101 (unsigned char) '.');
105 tld++; /* skip the '.' */
106 return GNUNET_strdup (tld);
111 * Eat the TLD of the given @a name.
113 * @param[in,out] name a name
120 GNUNET_assert (0 < strlen (name));
122 (unsigned char) '.');
125 GNUNET_GNS_EMPTY_LABEL_AT);
132 * Function called with the result of a GNS lookup.
134 * @param cls a `struct GNUNET_GNS_LookupWithTldRequest *`
135 * @param rd_count number of records returned
136 * @param rd array of @a rd_count records with the results
139 process_lookup_result (void *cls,
141 const struct GNUNET_GNSRECORD_Data *rd)
143 struct GNUNET_GNS_LookupWithTldRequest *ltr = cls;
146 ltr->lookup_proc (ltr->lookup_proc_cls,
150 GNUNET_GNS_lookup_with_tld_cancel (ltr);
155 * Perform the actual resolution, starting with the zone
156 * identified by the given public key.
158 * @param pkey public key to use for the zone, can be NULL
161 lookup_with_public_key (struct GNUNET_GNS_LookupWithTldRequest *ltr,
162 const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
164 ltr->lr = GNUNET_GNS_lookup (ltr->gns_handle,
169 &process_lookup_result,
175 * Method called to with the ego we are to use for the lookup,
176 * when the ego is determined by a name.
178 * @param cls a `struct GNUNET_GNS_LookupWithTldRequest *`
179 * @param ego ego handle, NULL if not found
182 identity_zone_cb (void *cls,
183 const struct GNUNET_IDENTITY_Ego *ego)
185 struct GNUNET_GNS_LookupWithTldRequest *ltr = cls;
186 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
191 ltr->lookup_proc (ltr->lookup_proc_cls,
195 GNUNET_GNS_lookup_with_tld_cancel (ltr);
200 GNUNET_IDENTITY_ego_get_public_key (ego,
202 lookup_with_public_key (ltr,
209 * Perform an asynchronous lookup operation on the GNS,
210 * determining the zone using the TLD of the given name
211 * and the current configuration to resolve TLDs to zones.
213 * @param handle handle to the GNS service
214 * @param name the name to look up, including TLD
215 * @param type the record type to look up
216 * @param options local options for the lookup
217 * @param proc processor to call on result
218 * @param proc_cls closure for @a proc
219 * @return handle to the get request, NULL on error (i.e. bad configuration)
221 struct GNUNET_GNS_LookupWithTldRequest *
222 GNUNET_GNS_lookup_with_tld (struct GNUNET_GNS_Handle *handle,
225 enum GNUNET_GNS_LocalOptions options,
226 GNUNET_GNS_LookupResultProcessor2 proc,
229 struct GNUNET_GNS_LookupWithTldRequest *ltr;
233 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
235 ltr = GNUNET_new (struct GNUNET_GNS_LookupWithTldRequest);
236 ltr->gns_handle = handle;
237 ltr->name = GNUNET_strdup (name);
239 ltr->options = options;
240 ltr->lookup_proc = proc;
241 ltr->lookup_proc_cls = proc_cls;
242 /* start with trivial case: TLD is zkey */
243 tld = get_tld (ltr->name);
245 GNUNET_CRYPTO_ecdsa_public_key_from_string (tld,
250 lookup_with_public_key (ltr,
256 /* second case: TLD is mapped in our configuration file */
257 GNUNET_asprintf (&dot_tld,
261 GNUNET_CONFIGURATION_get_value_string (handle->cfg,
267 GNUNET_CRYPTO_ecdsa_public_key_from_string (zonestr,
271 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
274 _("Expected a base32-encoded public zone key\n"));
275 GNUNET_free (zonestr);
276 GNUNET_free (dot_tld);
277 GNUNET_free (ltr->name);
282 GNUNET_free (dot_tld);
283 GNUNET_free (zonestr);
285 lookup_with_public_key (ltr,
290 GNUNET_free (dot_tld);
292 /* Final case: TLD matches one of our egos */
295 /* if the name is of the form 'label' (and not 'label.SUBDOMAIN'), never go to the DHT */
296 if (NULL == strchr (ltr->name,
297 (unsigned char) '.'))
298 ltr->options = GNUNET_GNS_LO_NO_DHT;
300 ltr->options = GNUNET_GNS_LO_LOCAL_MASTER;
301 ltr->id_op = GNUNET_IDENTITY_ego_lookup (ltr->gns_handle->cfg,
306 if (NULL == ltr->id_op)
308 GNUNET_free (ltr->name);
317 * Cancel pending lookup request
319 * @param ltr the lookup request to cancel
322 GNUNET_GNS_lookup_with_tld_cancel (struct GNUNET_GNS_LookupWithTldRequest *ltr)
324 if (NULL != ltr->id_op)
326 GNUNET_IDENTITY_ego_lookup_cancel (ltr->id_op);
331 GNUNET_GNS_lookup_cancel (ltr->lr);
334 GNUNET_free (ltr->name);
338 /* end of gns_tld_api.c */