2 This file is part of GNUnet.
3 Copyright (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
22 * @file transport/gnunet-service-transport_hello.c
23 * @brief hello management implementation
24 * @author Christian Grothoff
27 #include "gnunet_constants.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_peerinfo_service.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet-service-transport_hello.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet-service-transport_plugins.h"
37 * How often do we refresh our HELLO (due to expiration concerns)?
39 #define HELLO_REFRESH_PERIOD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
42 * Hello address expiration
44 extern struct GNUNET_TIME_Relative hello_expiration;
48 * Entry in linked list of network addresses for ourselves. Also
49 * includes a cached signature for 'struct TransportPongMessage's.
54 * This is a doubly-linked list.
56 struct OwnAddressList *next;
59 * This is a doubly-linked list.
61 struct OwnAddressList *prev;
66 struct GNUNET_HELLO_Address *address;
69 * How long until the current signature expires? (ZERO if the
70 * signature was never created).
72 struct GNUNET_TIME_Absolute pong_sig_expires;
75 * Signature for a 'struct TransportPongMessage' for this address.
77 struct GNUNET_CRYPTO_EddsaSignature pong_signature;
80 * How often has this address been added/removed? Used as
81 * some plugins may learn the same external address from
92 static struct GNUNET_HELLO_Message *our_hello;
95 * Function to call on HELLO changes.
97 static GST_HelloCallback hello_cb;
100 * Closure for #hello_cb.
102 static void *hello_cb_cls;
105 * Head of my addresses.
107 static struct OwnAddressList *oal_head;
110 * Tail of my addresses.
112 static struct OwnAddressList *oal_tail;
115 * Should we use a friend-only HELLO?
117 static int friend_option;
120 * Identifier of #refresh_hello_task().
122 static struct GNUNET_SCHEDULER_Task *hello_task;
126 * Closure for #address_generator().
128 struct GeneratorContext
131 * Where are we in the DLL?
133 struct OwnAddressList *addr_pos;
136 * When do addresses expire?
138 struct GNUNET_TIME_Absolute expiration;
143 * Add an address from the `struct OwnAddressList` to the buffer.
145 * @param cls the `struct GeneratorContext`
146 * @param max maximum number of bytes left
147 * @param buf where to write the address
148 * @return bytes written or #GNUNET_SYSERR to signal the
149 * end of the iteration.
152 address_generator (void *cls,
156 struct GeneratorContext *gc = cls;
159 if (NULL == gc->addr_pos)
160 return GNUNET_SYSERR; /* Done */
161 ret = GNUNET_HELLO_add_address (gc->addr_pos->address,
165 gc->addr_pos = gc->addr_pos->next;
171 * Construct our HELLO message from all of the addresses of
172 * all of the transports.
175 * @param tc scheduler context
178 refresh_hello_task (void *cls,
179 const struct GNUNET_SCHEDULER_TaskContext *tc)
181 struct GeneratorContext gc;
184 gc.addr_pos = oal_head;
185 gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
187 GNUNET_free_non_null (our_hello);
188 our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "Refreshed my %s HELLO, new size is %d\n",
194 (GNUNET_YES == friend_option) ? "friend-only" : "public",
195 GNUNET_HELLO_size (our_hello));
196 GNUNET_STATISTICS_update (GST_stats,
197 gettext_noop ("# refreshed my HELLO"),
200 if (NULL != hello_cb)
201 hello_cb (hello_cb_cls,
203 GNUNET_PEERINFO_add_peer (GST_peerinfo,
208 GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
215 * Schedule task to refresh hello (but only if such a
216 * task exists already, as otherwise the module might
217 * have been shutdown).
222 if (NULL != hello_task)
224 GNUNET_SCHEDULER_cancel (hello_task);
225 hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
232 * Initialize the HELLO module.
234 * @param friend_only use a friend only hello
235 * @param cb function to call whenever our HELLO changes
236 * @param cb_cls closure for @a cb
239 GST_hello_start (int friend_only,
240 GST_HelloCallback cb,
244 hello_cb_cls = cb_cls;
245 friend_option = friend_only;
246 refresh_hello_task (NULL, NULL);
251 * Shutdown the HELLO module.
258 if (NULL != hello_task)
260 GNUNET_SCHEDULER_cancel (hello_task);
263 if (NULL != our_hello)
265 GNUNET_free (our_hello);
272 * Obtain this peers HELLO message.
274 * @return our HELLO message
276 const struct GNUNET_MessageHeader *
279 return (const struct GNUNET_MessageHeader *) our_hello;
284 * Add or remove an address from this peer's HELLO message.
286 * @param addremove #GNUNET_YES to add, #GNUNET_NO to remove
287 * @param address address to add or remove
290 GST_hello_modify_addresses (int addremove,
291 const struct GNUNET_HELLO_Address *address)
293 struct OwnAddressList *al;
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 (GNUNET_YES == addremove)
297 ? "Adding `%s' to the set of our addresses\n"
298 : "Removing `%s' from the set of our addresses\n",
299 GST_plugins_a2s (address));
300 GNUNET_assert (NULL != address);
301 for (al = oal_head; al != NULL; al = al->next)
302 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
304 if (GNUNET_NO == addremove)
308 /* address to be removed not found!? */
314 return; /* RC not yet zero */
315 GNUNET_CONTAINER_DLL_remove (oal_head,
318 GNUNET_HELLO_address_free (al->address);
325 /* address added twice or more */
329 al = GNUNET_new (struct OwnAddressList);
331 GNUNET_CONTAINER_DLL_insert (oal_head,
334 al->address = GNUNET_HELLO_address_copy (address);
340 * Test if a particular address is one of ours.
342 * @param address address to test
343 * @param sig location where to cache PONG signatures for this address [set]
344 * @param sig_expiration how long until the current 'sig' expires?
345 * (ZERO if sig was never created) [set]
346 * @return #GNUNET_YES if this is one of our addresses,
350 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
351 struct GNUNET_CRYPTO_EddsaSignature **sig,
352 struct GNUNET_TIME_Absolute **sig_expiration)
354 struct OwnAddressList *al;
356 for (al = oal_head; al != NULL; al = al->next)
357 if (0 == GNUNET_HELLO_address_cmp (address,
360 *sig = &al->pong_signature;
361 *sig_expiration = &al->pong_sig_expires;
365 *sig_expiration = NULL;
370 /* end of file gnunet-service-transport_hello.c */