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)
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_RsaSignature pong_signature;
80 static struct GNUNET_HELLO_Message *our_hello;
83 * Function to call on HELLO changes.
85 static GST_HelloCallback hello_cb;
88 * Closure for 'hello_cb'.
90 static void *hello_cb_cls;
93 * Head of my addresses.
95 struct OwnAddressList *oal_head;
98 * Tail of my addresses.
100 struct OwnAddressList *oal_tail;
103 * Identifier of 'refresh_hello' task.
105 static GNUNET_SCHEDULER_TaskIdentifier hello_task;
109 * Closure for 'address_generator'.
111 struct GeneratorContext
114 * Where are we in the DLL?
116 struct OwnAddressList *addr_pos;
119 * When do addresses expire?
121 struct GNUNET_TIME_Absolute expiration;
126 * Add an address from the 'OwnAddressList' to the buffer.
128 * @param cls the 'struct GeneratorContext'
129 * @param max maximum number of bytes left
130 * @param buf where to write the address
133 address_generator (void *cls, size_t max, void *buf)
135 struct GeneratorContext *gc = cls;
138 if (NULL == gc->addr_pos)
141 GNUNET_HELLO_add_address (gc->addr_pos->address, gc->expiration, buf,
143 gc->addr_pos = gc->addr_pos->next;
149 * Construct our HELLO message from all of the addresses of
150 * all of the transports.
153 * @param tc scheduler context
156 refresh_hello_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 struct GeneratorContext gc;
160 hello_task = GNUNET_SCHEDULER_NO_TASK;
161 gc.addr_pos = oal_head;
163 GNUNET_TIME_relative_to_absolute
164 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION);
165 GNUNET_free (our_hello);
166 our_hello = GNUNET_HELLO_create (&GST_my_public_key, &address_generator, &gc);
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
169 "Refreshed my `%s', new size is %d\n", "HELLO",
170 GNUNET_HELLO_size (our_hello));
172 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# refreshed my HELLO"), 1,
174 if (NULL != hello_cb)
175 hello_cb (hello_cb_cls, GST_hello_get ());
176 GNUNET_PEERINFO_add_peer (GST_peerinfo, our_hello);
178 GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD, &refresh_hello_task,
185 * Schedule task to refresh hello (unless such a
186 * task exists already).
191 if (hello_task != GNUNET_SCHEDULER_NO_TASK)
192 GNUNET_SCHEDULER_cancel (hello_task);
193 hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task, NULL);
198 * Initialize the HELLO module.
200 * @param cb function to call whenever our HELLO changes
201 * @param cb_cls closure for cb
204 GST_hello_start (GST_HelloCallback cb, void *cb_cls)
207 hello_cb_cls = cb_cls;
208 our_hello = GNUNET_HELLO_create (&GST_my_public_key, NULL, NULL);
214 * Shutdown the HELLO module.
221 if (GNUNET_SCHEDULER_NO_TASK != hello_task)
223 GNUNET_SCHEDULER_cancel (hello_task);
224 hello_task = GNUNET_SCHEDULER_NO_TASK;
226 if (NULL != our_hello)
228 GNUNET_free (our_hello);
235 * Obtain this peers HELLO message.
237 * @return our HELLO message
239 const struct GNUNET_MessageHeader *
242 return (struct GNUNET_MessageHeader *) our_hello;
247 * Add or remove an address from this peer's HELLO message.
249 * @param addremove GNUNET_YES to add, GNUNET_NO to remove
250 * @param address address to add or remove
253 GST_hello_modify_addresses (int addremove,
254 const struct GNUNET_HELLO_Address *address)
256 struct OwnAddressList *al;
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 GNUNET_YES) ? "Adding `%s':%s to the set of our addresses\n" :
262 "Removing `%s':%s from the set of our addresses\n",
263 GST_plugins_a2s (address), p->short_name);
265 GNUNET_assert (address != NULL);
266 if (GNUNET_NO == addremove)
268 for (al = oal_head; al != NULL; al = al->next)
269 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
271 GNUNET_CONTAINER_DLL_remove (oal_head, oal_tail, al);
272 GNUNET_HELLO_address_free (al->address);
277 /* address to be removed not found!? */
281 al = GNUNET_malloc (sizeof (struct OwnAddressList));
282 GNUNET_CONTAINER_DLL_insert (oal_head, oal_tail, al);
283 al->address = GNUNET_HELLO_address_copy (address);
289 * Test if a particular address is one of ours.
291 * @param address address to test
292 * @param sig location where to cache PONG signatures for this address [set]
293 * @param sig_expiration how long until the current 'sig' expires?
294 * (ZERO if sig was never created) [set]
295 * @return GNUNET_YES if this is one of our addresses,
299 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
300 struct GNUNET_CRYPTO_RsaSignature **sig,
301 struct GNUNET_TIME_Absolute **sig_expiration)
303 struct OwnAddressList *al;
305 for (al = oal_head; al != NULL; al = al->next)
306 if (0 == GNUNET_HELLO_address_cmp (address, al->address))
308 *sig = &al->pong_signature;
309 *sig_expiration = &al->pong_sig_expires;
313 *sig_expiration = NULL;
318 /* end of file gnunet-service-transport_hello.c */