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 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.
17 * @file transport/gnunet-service-transport_hello.c
18 * @brief hello management implementation
19 * @author Christian Grothoff
22 #include "gnunet_constants.h"
23 #include "gnunet_hello_lib.h"
24 #include "gnunet_peerinfo_service.h"
25 #include "gnunet_statistics_service.h"
26 #include "gnunet-service-transport_hello.h"
27 #include "gnunet-service-transport.h"
28 #include "gnunet-service-transport_plugins.h"
32 * How often do we refresh our HELLO (due to expiration concerns)?
34 #define HELLO_REFRESH_PERIOD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
37 * Hello address expiration
39 extern struct GNUNET_TIME_Relative hello_expiration;
43 * Entry in linked list of network addresses for ourselves. Also
44 * includes a cached signature for 'struct TransportPongMessage's.
49 * This is a doubly-linked list.
51 struct OwnAddressList *next;
54 * This is a doubly-linked list.
56 struct OwnAddressList *prev;
61 struct GNUNET_HELLO_Address *address;
64 * How long until the current signature expires? (ZERO if the
65 * signature was never created).
67 struct GNUNET_TIME_Absolute pong_sig_expires;
70 * Signature for a 'struct TransportPongMessage' for this address.
72 struct GNUNET_CRYPTO_EddsaSignature pong_signature;
75 * How often has this address been added/removed? Used as
76 * some plugins may learn the same external address from
87 static struct GNUNET_HELLO_Message *our_hello;
90 * Function to call on HELLO changes.
92 static GST_HelloCallback hello_cb;
95 * Closure for #hello_cb.
97 static void *hello_cb_cls;
100 * Head of my addresses.
102 static struct OwnAddressList *oal_head;
105 * Tail of my addresses.
107 static struct OwnAddressList *oal_tail;
110 * Should we use a friend-only HELLO?
112 static int friend_option;
115 * Identifier of #refresh_hello_task().
117 static struct GNUNET_SCHEDULER_Task *hello_task;
121 * Closure for #address_generator().
123 struct GeneratorContext
126 * Where are we in the DLL?
128 struct OwnAddressList *addr_pos;
131 * When do addresses expire?
133 struct GNUNET_TIME_Absolute expiration;
138 * Add an address from the `struct OwnAddressList` to the buffer.
140 * @param cls the `struct GeneratorContext`
141 * @param max maximum number of bytes left
142 * @param buf where to write the address
143 * @return bytes written or #GNUNET_SYSERR to signal the
144 * end of the iteration.
147 address_generator (void *cls,
151 struct GeneratorContext *gc = cls;
154 if (NULL == gc->addr_pos)
155 return GNUNET_SYSERR; /* Done */
156 ret = GNUNET_HELLO_add_address (gc->addr_pos->address,
160 gc->addr_pos = gc->addr_pos->next;
166 * Construct our HELLO message from all of the addresses of
167 * all of the transports.
172 refresh_hello_task (void *cls)
174 struct GeneratorContext gc;
177 gc.addr_pos = oal_head;
178 gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
180 GNUNET_free_non_null (our_hello);
181 our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Refreshed my %s HELLO, new size is %d\n",
187 (GNUNET_YES == friend_option) ? "friend-only" : "public",
188 GNUNET_HELLO_size (our_hello));
189 GNUNET_STATISTICS_update (GST_stats,
190 gettext_noop ("# refreshed my HELLO"),
193 if (NULL != hello_cb)
194 hello_cb (hello_cb_cls,
196 GNUNET_PEERINFO_add_peer (GST_peerinfo,
201 GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
208 * Schedule task to refresh hello (but only if such a
209 * task exists already, as otherwise the module might
210 * have been shutdown).
215 if (NULL != hello_task)
217 GNUNET_SCHEDULER_cancel (hello_task);
218 hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
225 * Initialize the HELLO module.
227 * @param friend_only use a friend only hello
228 * @param cb function to call whenever our HELLO changes
229 * @param cb_cls closure for @a cb
232 GST_hello_start (int friend_only,
233 GST_HelloCallback cb,
237 hello_cb_cls = cb_cls;
238 friend_option = friend_only;
239 refresh_hello_task (NULL);
244 * Shutdown the HELLO module.
251 if (NULL != hello_task)
253 GNUNET_SCHEDULER_cancel (hello_task);
256 if (NULL != our_hello)
258 GNUNET_free (our_hello);
265 * Obtain this peers HELLO message.
267 * @return our HELLO message
269 const struct GNUNET_MessageHeader *
272 return (const struct GNUNET_MessageHeader *) our_hello;
277 * Add or remove an address from this peer's HELLO message.
279 * @param addremove #GNUNET_YES to add, #GNUNET_NO to remove
280 * @param address address to add or remove
283 GST_hello_modify_addresses (int addremove,
284 const struct GNUNET_HELLO_Address *address)
286 struct OwnAddressList *al;
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 (GNUNET_YES == addremove)
290 ? "Adding `%s' to the set of our addresses\n"
291 : "Removing `%s' from the set of our addresses\n",
292 GST_plugins_a2s (address));
293 GNUNET_assert (NULL != address);
294 for (al = oal_head; al != NULL; al = al->next)
295 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
297 if (GNUNET_NO == addremove)
301 /* address to be removed not found!? */
307 return; /* RC not yet zero */
308 GNUNET_CONTAINER_DLL_remove (oal_head,
311 GNUNET_HELLO_address_free (al->address);
318 /* address added twice or more */
322 al = GNUNET_new (struct OwnAddressList);
324 GNUNET_CONTAINER_DLL_insert (oal_head,
327 al->address = GNUNET_HELLO_address_copy (address);
333 * Test if a particular address is one of ours.
335 * @param address address to test
336 * @param sig location where to cache PONG signatures for this address [set]
337 * @param sig_expiration how long until the current 'sig' expires?
338 * (ZERO if sig was never created) [set]
339 * @return #GNUNET_YES if this is one of our addresses,
343 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
344 struct GNUNET_CRYPTO_EddsaSignature **sig,
345 struct GNUNET_TIME_Absolute **sig_expiration)
347 struct OwnAddressList *al;
349 for (al = oal_head; al != NULL; al = al->next)
350 if (0 == GNUNET_HELLO_address_cmp (address,
353 *sig = &al->pong_signature;
354 *sig_expiration = &al->pong_sig_expires;
358 *sig_expiration = NULL;
363 /* end of file gnunet-service-transport_hello.c */