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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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;
85 static struct GNUNET_HELLO_Message *our_hello;
88 * Function to call on HELLO changes.
90 static GST_HelloCallback hello_cb;
93 * Closure for #hello_cb.
95 static void *hello_cb_cls;
98 * Head of my addresses.
100 static struct OwnAddressList *oal_head;
103 * Tail of my addresses.
105 static struct OwnAddressList *oal_tail;
108 * Should we use a friend-only HELLO?
110 static int friend_option;
113 * Identifier of #refresh_hello_task().
115 static struct GNUNET_SCHEDULER_Task *hello_task;
119 * Closure for #address_generator().
121 struct GeneratorContext
124 * Where are we in the DLL?
126 struct OwnAddressList *addr_pos;
129 * When do addresses expire?
131 struct GNUNET_TIME_Absolute expiration;
136 * Add an address from the 'OwnAddressList' to the buffer.
138 * @param cls the `struct GeneratorContext`
139 * @param max maximum number of bytes left
140 * @param buf where to write the address
141 * @return bytes written or #GNUNET_SYSERR to signal the
142 * end of the iteration.
145 address_generator (void *cls,
149 struct GeneratorContext *gc = cls;
152 if (NULL == gc->addr_pos)
153 return GNUNET_SYSERR; /* Done */
154 ret = GNUNET_HELLO_add_address (gc->addr_pos->address, gc->expiration, buf,
156 gc->addr_pos = gc->addr_pos->next;
162 * Construct our HELLO message from all of the addresses of
163 * all of the transports.
166 * @param tc scheduler context
169 refresh_hello_task (void *cls,
170 const struct GNUNET_SCHEDULER_TaskContext *tc)
172 struct GeneratorContext gc;
175 gc.addr_pos = oal_head;
176 gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
178 GNUNET_free_non_null (our_hello);
179 our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Refreshed my %s HELLO, new size is %d\n",
185 (GNUNET_YES == friend_option) ? "friend-only" : "public",
186 GNUNET_HELLO_size (our_hello));
187 GNUNET_STATISTICS_update (GST_stats,
188 gettext_noop ("# refreshed my HELLO"),
191 if (NULL != hello_cb)
192 hello_cb (hello_cb_cls,
194 GNUNET_PEERINFO_add_peer (GST_peerinfo,
199 GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
206 * Schedule task to refresh hello (but only if such a
207 * task exists already, as otherwise the module might
208 * have been shutdown).
213 if (NULL != hello_task)
215 GNUNET_SCHEDULER_cancel (hello_task);
216 hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
223 * Initialize the HELLO module.
225 * @param friend_only use a friend only hello
226 * @param cb function to call whenever our HELLO changes
227 * @param cb_cls closure for @a cb
230 GST_hello_start (int friend_only,
231 GST_HelloCallback cb,
235 hello_cb_cls = cb_cls;
236 friend_option = friend_only;
237 refresh_hello_task (NULL, NULL);
242 * Shutdown the HELLO module.
249 if (NULL != hello_task)
251 GNUNET_SCHEDULER_cancel (hello_task);
254 if (NULL != our_hello)
256 GNUNET_free (our_hello);
263 * Obtain this peers HELLO message.
265 * @return our HELLO message
267 const struct GNUNET_MessageHeader *
270 return (struct GNUNET_MessageHeader *) our_hello;
275 * Add or remove an address from this peer's HELLO message.
277 * @param addremove #GNUNET_YES to add, #GNUNET_NO to remove
278 * @param address address to add or remove
281 GST_hello_modify_addresses (int addremove,
282 const struct GNUNET_HELLO_Address *address)
284 struct OwnAddressList *al;
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 GNUNET_YES) ? "Adding `%s' to the set of our addresses\n" :
289 "Removing `%s' from the set of our addresses\n",
290 GST_plugins_a2s (address));
291 GNUNET_assert (address != NULL);
292 if (GNUNET_NO == addremove)
294 for (al = oal_head; al != NULL; al = al->next)
295 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
297 GNUNET_CONTAINER_DLL_remove (oal_head, oal_tail, al);
298 GNUNET_HELLO_address_free (al->address);
303 /* address to be removed not found!? */
307 al = GNUNET_new (struct OwnAddressList);
308 GNUNET_CONTAINER_DLL_insert (oal_head, oal_tail, al);
309 al->address = GNUNET_HELLO_address_copy (address);
315 * Test if a particular address is one of ours.
317 * @param address address to test
318 * @param sig location where to cache PONG signatures for this address [set]
319 * @param sig_expiration how long until the current 'sig' expires?
320 * (ZERO if sig was never created) [set]
321 * @return #GNUNET_YES if this is one of our addresses,
325 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
326 struct GNUNET_CRYPTO_EddsaSignature **sig,
327 struct GNUNET_TIME_Absolute **sig_expiration)
329 struct OwnAddressList *al;
331 for (al = oal_head; al != NULL; al = al->next)
332 if (0 == GNUNET_HELLO_address_cmp (address,
335 *sig = &al->pong_signature;
336 *sig_expiration = &al->pong_sig_expires;
340 *sig_expiration = NULL;
345 /* end of file gnunet-service-transport_hello.c */