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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
177 refresh_hello_task (void *cls)
179 struct GeneratorContext gc;
182 gc.addr_pos = oal_head;
183 gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
185 GNUNET_free_non_null (our_hello);
186 our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "Refreshed my %s HELLO, new size is %d\n",
192 (GNUNET_YES == friend_option) ? "friend-only" : "public",
193 GNUNET_HELLO_size (our_hello));
194 GNUNET_STATISTICS_update (GST_stats,
195 gettext_noop ("# refreshed my HELLO"),
198 if (NULL != hello_cb)
199 hello_cb (hello_cb_cls,
201 GNUNET_PEERINFO_add_peer (GST_peerinfo,
206 GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
213 * Schedule task to refresh hello (but only if such a
214 * task exists already, as otherwise the module might
215 * have been shutdown).
220 if (NULL != hello_task)
222 GNUNET_SCHEDULER_cancel (hello_task);
223 hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
230 * Initialize the HELLO module.
232 * @param friend_only use a friend only hello
233 * @param cb function to call whenever our HELLO changes
234 * @param cb_cls closure for @a cb
237 GST_hello_start (int friend_only,
238 GST_HelloCallback cb,
242 hello_cb_cls = cb_cls;
243 friend_option = friend_only;
244 refresh_hello_task (NULL);
249 * Shutdown the HELLO module.
256 if (NULL != hello_task)
258 GNUNET_SCHEDULER_cancel (hello_task);
261 if (NULL != our_hello)
263 GNUNET_free (our_hello);
270 * Obtain this peers HELLO message.
272 * @return our HELLO message
274 const struct GNUNET_MessageHeader *
277 return (const struct GNUNET_MessageHeader *) our_hello;
282 * Add or remove an address from this peer's HELLO message.
284 * @param addremove #GNUNET_YES to add, #GNUNET_NO to remove
285 * @param address address to add or remove
288 GST_hello_modify_addresses (int addremove,
289 const struct GNUNET_HELLO_Address *address)
291 struct OwnAddressList *al;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 (GNUNET_YES == addremove)
295 ? "Adding `%s' to the set of our addresses\n"
296 : "Removing `%s' from the set of our addresses\n",
297 GST_plugins_a2s (address));
298 GNUNET_assert (NULL != address);
299 for (al = oal_head; al != NULL; al = al->next)
300 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
302 if (GNUNET_NO == addremove)
306 /* address to be removed not found!? */
312 return; /* RC not yet zero */
313 GNUNET_CONTAINER_DLL_remove (oal_head,
316 GNUNET_HELLO_address_free (al->address);
323 /* address added twice or more */
327 al = GNUNET_new (struct OwnAddressList);
329 GNUNET_CONTAINER_DLL_insert (oal_head,
332 al->address = GNUNET_HELLO_address_copy (address);
338 * Test if a particular address is one of ours.
340 * @param address address to test
341 * @param sig location where to cache PONG signatures for this address [set]
342 * @param sig_expiration how long until the current 'sig' expires?
343 * (ZERO if sig was never created) [set]
344 * @return #GNUNET_YES if this is one of our addresses,
348 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
349 struct GNUNET_CRYPTO_EddsaSignature **sig,
350 struct GNUNET_TIME_Absolute **sig_expiration)
352 struct OwnAddressList *al;
354 for (al = oal_head; al != NULL; al = al->next)
355 if (0 == GNUNET_HELLO_address_cmp (address,
358 *sig = &al->pong_signature;
359 *sig_expiration = &al->pong_sig_expires;
363 *sig_expiration = NULL;
368 /* end of file gnunet-service-transport_hello.c */