2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file gns/gnunet-service-gns_reverser.c
22 * @brief GNUnet GNS service
23 * @author Martin Schanzenbach
28 #include "gnunet_gns_service.h"
29 #include "gnunet-service-gns_resolver.h"
30 #include "gnunet-service-gns_reverser.h"
32 struct ReverseTreeNode
37 struct ReverseTreeNode *next;
42 struct ReverseTreeNode *prev;
45 * Resolved name until now
50 * Depth of the resolution at this node
55 * The pkey of the namespace
57 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
62 struct GNS_ReverserHandle
67 struct GNS_ResolverHandle *rh;
70 * The authority to look for
72 struct GNUNET_CRYPTO_EcdsaPublicKey authority;
75 * Resolution candidate queue
77 struct ReverseTreeNode *node_queue_head;
80 * Resolution candidate queue
82 struct ReverseTreeNode *node_queue_tail;
85 * Max depth for the resolution
92 GNS_ReverseResultProcessor proc;
101 cleanup_handle (struct GNS_ReverserHandle *rh)
103 struct ReverseTreeNode *rtn;
105 for (rtn = rh->node_queue_head; NULL != rtn; rtn = rh->node_queue_head)
107 if (NULL != rtn->name)
108 GNUNET_free (rtn->name);
109 GNUNET_CONTAINER_DLL_remove (rh->node_queue_head,
117 handle_gns_result (void *cls,
119 const struct GNUNET_GNSRECORD_Data *rd)
121 struct GNS_ReverserHandle *rh = cls;
122 const struct GNUNET_GNSRECORD_ReverseRecord *rr;
123 struct ReverseTreeNode *rtn;
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Got result (%d)\n", rd_count);
129 for (int i = 0; i < rd_count; i++)
132 * Check if we are in the delegation set
134 if (GNUNET_GNSRECORD_TYPE_REVERSE != rd[i].record_type)
137 name = (const char*) &rr[1];
138 if (0 == memcmp (&rh->authority,
140 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
143 GNUNET_asprintf (&result,
145 rh->node_queue_head->name,
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Found path from %s\n", result);
150 rh->proc (rh->proc_cls, result);
152 GNUNET_free (result);
155 if (rh->node_queue_head->depth >= rh->max_depth)
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Found REVERSE from %s\n", name);
160 rtn = GNUNET_new (struct ReverseTreeNode);
161 if (NULL == rh->node_queue_head->name)
162 rtn->name = GNUNET_strdup (name);
164 GNUNET_asprintf (&rtn->name,
166 rh->node_queue_head->name,
168 rtn->depth = rh->node_queue_head->depth + 1;
169 rtn->pkey = rr->pkey;
170 GNUNET_CONTAINER_DLL_insert_tail (rh->node_queue_head,
177 * Done here remove node from queue
179 rtn = rh->node_queue_head;
180 GNUNET_CONTAINER_DLL_remove (rh->node_queue_head,
183 if (NULL == rh->node_queue_head)
186 rh->proc (rh->proc_cls, NULL);
190 rh->rh = GNS_resolver_lookup (&rh->node_queue_head->pkey,
191 GNUNET_GNSRECORD_TYPE_REVERSE,
194 GNUNET_GNS_LO_DEFAULT,
199 struct GNS_ReverserHandle *
200 GNS_reverse_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *target,
201 const struct GNUNET_CRYPTO_EcdsaPublicKey *authority,
202 GNS_ReverseResultProcessor proc,
205 struct GNS_ReverserHandle *rh;
206 struct ReverseTreeNode *rtn;
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209 "Starting reverse resolution\n");
210 rh = GNUNET_new (struct GNS_ReverserHandle);
212 rh->proc_cls = proc_cls;
213 rtn = GNUNET_new (struct ReverseTreeNode);
217 GNUNET_CONTAINER_DLL_insert (rh->node_queue_head,
220 rh->authority = *authority;
221 rh->max_depth = 3; //TODO make argument
222 rh->rh = GNS_resolver_lookup (target,
223 GNUNET_GNSRECORD_TYPE_REVERSE,
226 GNUNET_GNS_LO_DEFAULT,
233 * Cancel active resolution (i.e. client disconnected).
235 * @param rh resolution to abort
238 GNS_reverse_lookup_cancel (struct GNS_ReverserHandle *rh)