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"
36 * How often do we refresh our HELLO (due to expiration concerns)?
38 #define HELLO_REFRESH_PERIOD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
42 * Entry in linked list of network addresses for ourselves. Also
43 * includes a cached signature for 'struct TransportPongMessage's.
48 * This is a doubly-linked list.
50 struct OwnAddressList *next;
53 * This is a doubly-linked list.
55 struct OwnAddressList *prev;
63 * How long until the current signature expires? (ZERO if the
64 * signature was never created).
66 struct GNUNET_TIME_Absolute pong_sig_expires;
69 * Signature for a 'struct TransportPongMessage' for this address.
71 struct GNUNET_CRYPTO_RsaSignature pong_signature;
84 static struct GNUNET_HELLO_Message *our_hello;
87 * Function to call on HELLO changes.
89 static GST_HelloCallback hello_cb;
92 * Closure for 'hello_cb'.
94 static void *hello_cb_cls;
97 * Head of my addresses.
99 struct OwnAddressList *oal_head;
102 * Tail of my addresses.
104 struct OwnAddressList *oal_tail;
107 * Identifier of 'refresh_hello' task.
109 static GNUNET_SCHEDULER_TaskIdentifier hello_task;
113 * Closure for 'address_generator'.
115 struct GeneratorContext
118 * Where are we in the DLL?
120 struct OwnAddressList *addr_pos;
123 * When do addresses expire?
125 struct GNUNET_TIME_Absolute expiration;
130 * Add an address from the 'OwnAddressList' to the buffer.
132 * @param cls the 'struct GeneratorContext'
133 * @param max maximum number of bytes left
134 * @param buf where to write the address
137 address_generator (void *cls, size_t max, void *buf)
139 struct GeneratorContext *gc = cls;
142 if (NULL == gc->addr_pos)
144 ret = GNUNET_HELLO_add_address (gc->addr_pos->plugin_name,
147 gc->addr_pos->addrlen,
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,
163 const struct GNUNET_SCHEDULER_TaskContext *tc)
165 struct GeneratorContext gc;
167 hello_task = GNUNET_SCHEDULER_NO_TASK;
168 gc.addr_pos = oal_head;
169 gc.expiration = GNUNET_TIME_relative_to_absolute (GST_HELLO_ADDRESS_EXPIRATION);
170 GNUNET_free (our_hello);
171 our_hello = GNUNET_HELLO_create (&GST_my_public_key,
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
176 "Refreshed my `%s', new size is %d\n", "HELLO",
177 GNUNET_HELLO_size(our_hello));
179 GNUNET_STATISTICS_update (GST_stats,
180 gettext_noop ("# refreshed my HELLO"),
183 if (NULL != hello_cb)
184 hello_cb (hello_cb_cls, GST_hello_get());
185 GNUNET_PEERINFO_add_peer (GST_peerinfo, our_hello);
187 = GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
195 * Schedule task to refresh hello (unless such a
196 * task exists already).
201 if (hello_task != GNUNET_SCHEDULER_NO_TASK)
202 GNUNET_SCHEDULER_cancel (hello_task);
204 = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
210 * Initialize the HELLO module.
212 * @param cb function to call whenever our HELLO changes
213 * @param cb_cls closure for cb
216 GST_hello_start (GST_HelloCallback cb,
220 hello_cb_cls = cb_cls;
221 our_hello = GNUNET_HELLO_create (&GST_my_public_key,
228 * Shutdown the HELLO module.
235 if (GNUNET_SCHEDULER_NO_TASK != hello_task)
237 GNUNET_SCHEDULER_cancel (hello_task);
238 hello_task = GNUNET_SCHEDULER_NO_TASK;
240 if (NULL != our_hello)
242 GNUNET_free (our_hello);
249 * Obtain this peers HELLO message.
251 * @return our HELLO message
253 const struct GNUNET_MessageHeader *
256 return (struct GNUNET_MessageHeader*) our_hello;
261 * Add or remove an address from this peer's HELLO message.
263 * @param addremove GNUNET_YES to add, GNUNET_NO to remove
264 * @param plugin_name name of the plugin for which this is an address
265 * @param plugin_address address in a plugin-specific format
266 * @param plugin_address_len number of bytes in plugin_address
269 GST_hello_modify_addresses (int addremove,
270 const char *plugin_name,
271 const void *plugin_address,
272 size_t plugin_address_len)
274 struct OwnAddressList *al;
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 (add_remove == GNUNET_YES)
279 ? "Adding `%s':%s to the set of our addresses\n"
280 : "Removing `%s':%s from the set of our addresses\n",
281 GST_plugins_a2s (plugin_name,
285 GNUNET_assert (plugin_address != NULL);
286 if (GNUNET_NO == addremove)
288 for (al = oal_head; al != NULL; al = al->next)
289 if ( (plugin_address_len == al->addrlen) &&
290 (0 == strcmp (al->plugin_name, plugin_name)) &&
291 (0 == memcmp (plugin_address, &al[1], plugin_address_len)) )
293 GNUNET_CONTAINER_DLL_remove (oal_head,
296 GNUNET_free (al->plugin_name);
301 /* address to be removed not found!? */
305 al = GNUNET_malloc (sizeof (struct OwnAddressList) + plugin_address_len);
306 GNUNET_CONTAINER_DLL_insert (oal_head,
309 al->plugin_name = GNUNET_strdup (plugin_name);
310 al->addrlen = plugin_address_len;
311 memcpy (&al[1], plugin_address, plugin_address_len);
317 * Test if a particular address is one of ours.
319 * @param plugin_name name of the plugin for which this is an address
320 * @param plugin_address address in a plugin-specific format
321 * @param plugin_address_len number of bytes in plugin_address
322 * @param sig location where to cache PONG signatures for this address [set]
323 * @param sig_expiration how long until the current 'sig' expires?
324 * (ZERO if sig was never created) [set]
325 * @return GNUNET_YES if this is one of our addresses,
329 GST_hello_test_address (const char *plugin_name,
330 const void *plugin_address,
331 size_t plugin_address_len,
332 struct GNUNET_CRYPTO_RsaSignature **sig,
333 struct GNUNET_TIME_Absolute **sig_expiration)
335 struct OwnAddressList *al;
337 for (al = oal_head; al != NULL; al = al->next)
338 if ( (plugin_address_len == al->addrlen) &&
339 (0 == strcmp (al->plugin_name, plugin_name)) &&
340 (0 == memcmp (plugin_address, &al[1], plugin_address_len)) )
342 *sig = &al->pong_signature;
343 *sig_expiration = &al->pong_sig_expires;
347 *sig_expiration = NULL;
352 /* end of file gnunet-service-transport_hello.c */