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_hello_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet-service-transport_hello.h"
31 #include "gnunet-service-transport.h"
32 #include "gnunet-service-transport_plugins.h"
35 * After how long do we expire an address in a HELLO that we just
36 * validated? This value is also used for our own addresses when we
39 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
42 * How often do we refresh our HELLO (due to expiration concerns)?
44 #define HELLO_REFRESH_PERIOD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
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;
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_RsaSignature pong_signature;
90 static struct GNUNET_HELLO_Message *our_hello;
93 * Function to call on HELLO changes.
95 static GST_HelloCallback hello_cb;
98 * Closure for 'hello_cb'.
100 static void *hello_cb_cls;
103 * Head of my addresses.
105 struct OwnAddressList *oal_head;
108 * Tail of my addresses.
110 struct OwnAddressList *oal_tail;
113 * Identifier of 'refresh_hello' task.
115 static GNUNET_SCHEDULER_TaskIdentifier 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
143 address_generator (void *cls, size_t max, void *buf)
145 struct GeneratorContext *gc = cls;
148 if (NULL == gc->addr_pos)
150 ret = GNUNET_HELLO_add_address (gc->addr_pos->plugin_name,
153 gc->addr_pos->addrlen,
155 gc->addr_pos = gc->addr_pos->next;
161 * Construct our HELLO message from all of the addresses of
162 * all of the transports.
165 * @param tc scheduler context
168 refresh_hello_task (void *cls,
169 const struct GNUNET_SCHEDULER_TaskContext *tc)
171 struct GeneratorContext gc;
173 hello_task = GNUNET_SCHEDULER_NO_TASK;
174 gc.addr_pos = oal_head;
175 gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
176 GNUNET_free (our_hello);
177 our_hello = GNUNET_HELLO_create (&GST_my_public_key,
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
182 "Refreshed my `%s', new size is %d\n", "HELLO",
183 GNUNET_HELLO_size(our_hello));
185 GNUNET_STATISTICS_update (GST_stats,
186 gettext_noop ("# refreshed my HELLO"),
189 if (NULL != hello_cb)
190 hello_cb (hello_cb_cls, GST_hello_get());
191 GNUNET_PEERINFO_add_peer (GST_peerinfo, our_hello);
193 = GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
201 * Schedule task to refresh hello (unless such a
202 * task exists already).
207 if (hello_task != GNUNET_SCHEDULER_NO_TASK)
208 GNUNET_SCHEDULER_cancel (hello_task);
210 = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
216 * Initialize the HELLO module.
218 * @param cb function to call whenever our HELLO changes
219 * @param cb_cls closure for cb
222 GST_hello_start (GST_HelloCallback cb,
226 hello_cb_cls = cb_cls;
227 our_hello = GNUNET_HELLO_create (&GST_my_public_key,
234 * Shutdown the HELLO module.
241 if (GNUNET_SCHEDULER_NO_TASK != hello_task)
243 GNUNET_SCHEDULER_cancel (hello_task);
244 hello_task = GNUNET_SCHEDULER_NO_TASK;
246 if (NULL != our_hello)
248 GNUNET_free (our_hello);
255 * Obtain this peers HELLO message.
257 * @return our HELLO message
259 const struct GNUNET_MessageHeader *
262 return (struct GNUNET_MessageHeader*) our_hello;
267 * Add or remove an address from this peer's HELLO message.
269 * @param addremove GNUNET_YES to add, GNUNET_NO to remove
270 * @param plugin_name name of the plugin for which this is an address
271 * @param plugin_address address in a plugin-specific format
272 * @param plugin_address_len number of bytes in plugin_address
275 GST_hello_modify_addresses (int addremove,
276 const char *plugin_name,
277 const void *plugin_address,
278 size_t plugin_address_len)
280 struct OwnAddressList *al;
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 (add_remove == GNUNET_YES)
285 ? "Adding `%s':%s to the set of our addresses\n"
286 : "Removing `%s':%s from the set of our addresses\n",
287 GST_plugins_a2s (plugin_name,
291 GNUNET_assert (plugin_address != NULL);
292 if (GNUNET_NO == addremove)
294 for (al = oal_head; al != NULL; al = al->next)
295 if ( (plugin_address_len == al->addrlen) &&
296 (0 == strcmp (al->plugin_name, plugin_name)) &&
297 (0 == memcmp (plugin_address, &al[1], plugin_address_len)) )
299 GNUNET_CONTAINER_DLL_remove (oal_head,
302 GNUNET_free (al->plugin_name);
307 /* address to be removed not found!? */
311 al = GNUNET_malloc (sizeof (struct OwnAddressList) + plugin_address_len);
312 GNUNET_CONTAINER_DLL_insert (oal_head,
315 al->plugin_name = GNUNET_strdup (plugin_name);
316 al->addrlen = plugin_address_len;
317 memcpy (&al[1], plugin_address, plugin_address_len);
323 * Test if a particular address is one of ours.
325 * @param plugin_name name of the plugin for which this is an address
326 * @param plugin_address address in a plugin-specific format
327 * @param plugin_address_len number of bytes in plugin_address
328 * @param sig location where to cache PONG signatures for this address [set]
329 * @param sig_expiration how long until the current 'sig' expires?
330 * (ZERO if sig was never created) [set]
331 * @return GNUNET_YES if this is one of our addresses,
335 GST_hello_test_address (const char *plugin_name,
336 const void *plugin_address,
337 size_t plugin_address_len,
338 struct GNUNET_CRYPTO_RsaSignature **sig,
339 struct GNUNET_TIME_Absolute **sig_expiration)
341 struct OwnAddressList *al;
343 for (al = oal_head; al != NULL; al = al->next)
344 if ( (plugin_address_len == al->addrlen) &&
345 (0 == strcmp (al->plugin_name, plugin_name)) &&
346 (0 == memcmp (plugin_address, &al[1], plugin_address_len)) )
348 *sig = &al->pong_signature;
349 *sig_expiration = &al->pong_sig_expires;
353 *sig_expiration = NULL;
358 /* end of file gnunet-service-transport_hello.c */