2 This file is part of GNUnet.
3 (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 struct OwnAddressList *oal_head;
103 * Tail of my addresses.
105 struct OwnAddressList *oal_tail;
108 * Identifier of 'refresh_hello' task.
110 static struct GNUNET_SCHEDULER_Task * hello_task;
114 * Closure for 'address_generator'.
116 struct GeneratorContext
119 * Where are we in the DLL?
121 struct OwnAddressList *addr_pos;
124 * When do addresses expire?
126 struct GNUNET_TIME_Absolute expiration;
131 * Add an address from the 'OwnAddressList' to the buffer.
133 * @param cls the 'struct GeneratorContext'
134 * @param max maximum number of bytes left
135 * @param buf where to write the address
136 * @return bytes written or GNUNET_SYSERR to signal the
137 * end of the iteration.
140 address_generator (void *cls, size_t max, void *buf)
142 struct GeneratorContext *gc = cls;
145 if (NULL == gc->addr_pos)
146 return GNUNET_SYSERR; /* Done */
147 ret = GNUNET_HELLO_add_address (gc->addr_pos->address, gc->expiration, buf,
149 gc->addr_pos = gc->addr_pos->next;
155 * Construct our HELLO message from all of the addresses of
156 * all of the transports.
159 * @param tc scheduler context
162 refresh_hello_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 struct GeneratorContext gc;
168 gc.addr_pos = oal_head;
169 gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
172 friend_only = GNUNET_HELLO_is_friend_only (our_hello);
173 GNUNET_free (our_hello);
174 our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
177 GNUNET_assert (NULL != our_hello);
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Refreshed my %s `%s', new size is %d\n",
180 (GNUNET_YES == GNUNET_HELLO_is_friend_only (our_hello)) ? "friend-only" : "public",
181 "HELLO", GNUNET_HELLO_size (our_hello));
182 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# refreshed my HELLO"), 1,
184 if (NULL != hello_cb)
185 hello_cb (hello_cb_cls, GST_hello_get ());
186 GNUNET_PEERINFO_add_peer (GST_peerinfo, our_hello, NULL, NULL);
188 GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD, &refresh_hello_task,
195 * Schedule task to refresh hello (unless such a
196 * task exists already).
201 if (hello_task != NULL)
202 GNUNET_SCHEDULER_cancel (hello_task);
203 hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task, NULL);
208 * Initialize the HELLO module.
210 * @param friend_only use a friend only hello
211 * @param cb function to call whenever our HELLO changes
212 * @param cb_cls closure for cb
215 GST_hello_start (int friend_only, GST_HelloCallback cb, void *cb_cls)
218 hello_cb_cls = cb_cls;
219 our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
220 NULL, NULL, friend_only);
221 GNUNET_assert (NULL != our_hello);
227 * Shutdown the HELLO module.
234 if (NULL != hello_task)
236 GNUNET_SCHEDULER_cancel (hello_task);
239 if (NULL != our_hello)
241 GNUNET_free (our_hello);
248 * Obtain this peers HELLO message.
250 * @return our HELLO message
252 const struct GNUNET_MessageHeader *
255 return (struct GNUNET_MessageHeader *) our_hello;
260 * Add or remove an address from this peer's HELLO message.
262 * @param addremove GNUNET_YES to add, GNUNET_NO to remove
263 * @param address address to add or remove
266 GST_hello_modify_addresses (int addremove,
267 const struct GNUNET_HELLO_Address *address)
269 struct OwnAddressList *al;
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 GNUNET_YES) ? "Adding `%s' to the set of our addresses\n" :
274 "Removing `%s' from the set of our addresses\n",
275 GST_plugins_a2s (address));
276 GNUNET_assert (address != NULL);
277 if (GNUNET_NO == addremove)
279 for (al = oal_head; al != NULL; al = al->next)
280 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
282 GNUNET_CONTAINER_DLL_remove (oal_head, oal_tail, al);
283 GNUNET_HELLO_address_free (al->address);
288 /* address to be removed not found!? */
292 al = GNUNET_new (struct OwnAddressList);
293 GNUNET_CONTAINER_DLL_insert (oal_head, oal_tail, al);
294 al->address = GNUNET_HELLO_address_copy (address);
300 * Test if a particular address is one of ours.
302 * @param address address to test
303 * @param sig location where to cache PONG signatures for this address [set]
304 * @param sig_expiration how long until the current 'sig' expires?
305 * (ZERO if sig was never created) [set]
306 * @return GNUNET_YES if this is one of our addresses,
310 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
311 struct GNUNET_CRYPTO_EddsaSignature **sig,
312 struct GNUNET_TIME_Absolute **sig_expiration)
314 struct OwnAddressList *al;
316 for (al = oal_head; al != NULL; al = al->next)
317 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
319 *sig = &al->pong_signature;
320 *sig_expiration = &al->pong_sig_expires;
324 *sig_expiration = NULL;
329 /* end of file gnunet-service-transport_hello.c */