2 This file is part of GNUnet.
3 Copyright (C) 2010,2011 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/>.
20 * @file transport/gnunet-service-transport_hello.c
21 * @brief hello management implementation
22 * @author Christian Grothoff
25 #include "gnunet_constants.h"
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_peerinfo_service.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet-service-transport_hello.h"
30 #include "gnunet-service-transport.h"
31 #include "gnunet-service-transport_plugins.h"
35 * How often do we refresh our HELLO (due to expiration concerns)?
37 #define HELLO_REFRESH_PERIOD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
40 * Hello address expiration
42 extern struct GNUNET_TIME_Relative hello_expiration;
46 * Entry in linked list of network addresses for ourselves. Also
47 * includes a cached signature for 'struct TransportPongMessage's.
52 * This is a doubly-linked list.
54 struct OwnAddressList *next;
57 * This is a doubly-linked list.
59 struct OwnAddressList *prev;
64 struct GNUNET_HELLO_Address *address;
67 * How long until the current signature expires? (ZERO if the
68 * signature was never created).
70 struct GNUNET_TIME_Absolute pong_sig_expires;
73 * Signature for a 'struct TransportPongMessage' for this address.
75 struct GNUNET_CRYPTO_EddsaSignature pong_signature;
78 * How often has this address been added/removed? Used as
79 * some plugins may learn the same external address from
90 static struct GNUNET_HELLO_Message *our_hello;
93 * Function to call on HELLO changes.
95 static GST_HelloCallback hello_cb;
98 * Closure for #hello_cb.
100 static void *hello_cb_cls;
103 * Head of my addresses.
105 static struct OwnAddressList *oal_head;
108 * Tail of my addresses.
110 static struct OwnAddressList *oal_tail;
113 * Should we use a friend-only HELLO?
115 static int friend_option;
118 * Identifier of #refresh_hello_task().
120 static struct GNUNET_SCHEDULER_Task *hello_task;
124 * Closure for #address_generator().
126 struct GeneratorContext
129 * Where are we in the DLL?
131 struct OwnAddressList *addr_pos;
134 * When do addresses expire?
136 struct GNUNET_TIME_Absolute expiration;
141 * Add an address from the `struct OwnAddressList` to the buffer.
143 * @param cls the `struct GeneratorContext`
144 * @param max maximum number of bytes left
145 * @param buf where to write the address
146 * @return bytes written or #GNUNET_SYSERR to signal the
147 * end of the iteration.
150 address_generator (void *cls,
154 struct GeneratorContext *gc = cls;
157 if (NULL == gc->addr_pos)
158 return GNUNET_SYSERR; /* Done */
159 ret = GNUNET_HELLO_add_address (gc->addr_pos->address,
163 gc->addr_pos = gc->addr_pos->next;
169 * Construct our HELLO message from all of the addresses of
170 * all of the transports.
175 refresh_hello_task (void *cls)
177 struct GeneratorContext gc;
180 gc.addr_pos = oal_head;
181 gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
183 GNUNET_free_non_null (our_hello);
184 our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189 "Refreshed my %s HELLO, new size is %d\n",
190 (GNUNET_YES == friend_option) ? "friend-only" : "public",
191 GNUNET_HELLO_size (our_hello));
192 GNUNET_STATISTICS_update (GST_stats,
193 gettext_noop ("# refreshed my HELLO"),
196 if (NULL != hello_cb)
197 hello_cb (hello_cb_cls,
199 GNUNET_PEERINFO_add_peer (GST_peerinfo,
204 GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
211 * Schedule task to refresh hello (but only if such a
212 * task exists already, as otherwise the module might
213 * have been shutdown).
218 if (NULL != hello_task)
220 GNUNET_SCHEDULER_cancel (hello_task);
221 hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
228 * Initialize the HELLO module.
230 * @param friend_only use a friend only hello
231 * @param cb function to call whenever our HELLO changes
232 * @param cb_cls closure for @a cb
235 GST_hello_start (int friend_only,
236 GST_HelloCallback cb,
240 hello_cb_cls = cb_cls;
241 friend_option = friend_only;
242 refresh_hello_task (NULL);
247 * Shutdown the HELLO module.
254 if (NULL != hello_task)
256 GNUNET_SCHEDULER_cancel (hello_task);
259 if (NULL != our_hello)
261 GNUNET_free (our_hello);
268 * Obtain this peers HELLO message.
270 * @return our HELLO message
272 const struct GNUNET_MessageHeader *
275 return (const struct GNUNET_MessageHeader *) our_hello;
280 * Add or remove an address from this peer's HELLO message.
282 * @param addremove #GNUNET_YES to add, #GNUNET_NO to remove
283 * @param address address to add or remove
286 GST_hello_modify_addresses (int addremove,
287 const struct GNUNET_HELLO_Address *address)
289 struct OwnAddressList *al;
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 (GNUNET_YES == addremove)
293 ? "Adding `%s' to the set of our addresses\n"
294 : "Removing `%s' from the set of our addresses\n",
295 GST_plugins_a2s (address));
296 GNUNET_assert (NULL != address);
297 for (al = oal_head; al != NULL; al = al->next)
298 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
300 if (GNUNET_NO == addremove)
304 /* address to be removed not found!? */
310 return; /* RC not yet zero */
311 GNUNET_CONTAINER_DLL_remove (oal_head,
314 GNUNET_HELLO_address_free (al->address);
321 /* address added twice or more */
325 al = GNUNET_new (struct OwnAddressList);
327 GNUNET_CONTAINER_DLL_insert (oal_head,
330 al->address = GNUNET_HELLO_address_copy (address);
336 * Test if a particular address is one of ours.
338 * @param address address to test
339 * @param sig location where to cache PONG signatures for this address [set]
340 * @param sig_expiration how long until the current 'sig' expires?
341 * (ZERO if sig was never created) [set]
342 * @return #GNUNET_YES if this is one of our addresses,
346 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
347 struct GNUNET_CRYPTO_EddsaSignature **sig,
348 struct GNUNET_TIME_Absolute **sig_expiration)
350 struct OwnAddressList *al;
352 for (al = oal_head; al != NULL; al = al->next)
353 if (0 == GNUNET_HELLO_address_cmp (address,
356 *sig = &al->pong_signature;
357 *sig_expiration = &al->pong_sig_expires;
361 *sig_expiration = NULL;
366 /* end of file gnunet-service-transport_hello.c */