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;
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;
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 GNUNET_SCHEDULER_TaskIdentifier 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
138 address_generator (void *cls, size_t max, void *buf)
140 struct GeneratorContext *gc = cls;
143 if (NULL == gc->addr_pos)
146 GNUNET_HELLO_add_address (gc->addr_pos->plugin_name, gc->expiration,
147 &gc->addr_pos[1], gc->addr_pos->addrlen, 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;
166 hello_task = GNUNET_SCHEDULER_NO_TASK;
167 gc.addr_pos = oal_head;
169 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION);
170 GNUNET_free (our_hello);
171 our_hello = GNUNET_HELLO_create (&GST_my_public_key, &address_generator, &gc);
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
174 "Refreshed my `%s', new size is %d\n", "HELLO",
175 GNUNET_HELLO_size (our_hello));
177 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# refreshed my HELLO"), 1,
179 if (NULL != hello_cb)
180 hello_cb (hello_cb_cls, GST_hello_get ());
181 GNUNET_PEERINFO_add_peer (GST_peerinfo, our_hello);
183 GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD, &refresh_hello_task,
190 * Schedule task to refresh hello (unless such a
191 * task exists already).
196 if (hello_task != GNUNET_SCHEDULER_NO_TASK)
197 GNUNET_SCHEDULER_cancel (hello_task);
198 hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task, NULL);
203 * Initialize the HELLO module.
205 * @param cb function to call whenever our HELLO changes
206 * @param cb_cls closure for cb
209 GST_hello_start (GST_HelloCallback cb, void *cb_cls)
212 hello_cb_cls = cb_cls;
213 our_hello = GNUNET_HELLO_create (&GST_my_public_key, NULL, NULL);
219 * Shutdown the HELLO module.
226 if (GNUNET_SCHEDULER_NO_TASK != hello_task)
228 GNUNET_SCHEDULER_cancel (hello_task);
229 hello_task = GNUNET_SCHEDULER_NO_TASK;
231 if (NULL != our_hello)
233 GNUNET_free (our_hello);
240 * Obtain this peers HELLO message.
242 * @return our HELLO message
244 const struct GNUNET_MessageHeader *
247 return (struct GNUNET_MessageHeader *) our_hello;
252 * Add or remove an address from this peer's HELLO message.
254 * @param addremove GNUNET_YES to add, GNUNET_NO to remove
255 * @param plugin_name name of the plugin for which this is an address
256 * @param plugin_address address in a plugin-specific format
257 * @param plugin_address_len number of bytes in plugin_address
260 GST_hello_modify_addresses (int addremove, const char *plugin_name,
261 const void *plugin_address,
262 size_t plugin_address_len)
264 struct OwnAddressList *al;
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 GNUNET_YES) ? "Adding `%s':%s to the set of our addresses\n" :
270 "Removing `%s':%s from the set of our addresses\n",
271 GST_plugins_a2s (plugin_name, addr, addrlen), p->short_name);
273 GNUNET_assert (plugin_address != NULL);
274 if (GNUNET_NO == addremove)
276 for (al = oal_head; al != NULL; al = al->next)
277 if ((plugin_address_len == al->addrlen) &&
278 (0 == strcmp (al->plugin_name, plugin_name)) &&
279 (0 == memcmp (plugin_address, &al[1], plugin_address_len)))
281 GNUNET_CONTAINER_DLL_remove (oal_head, oal_tail, al);
282 GNUNET_free (al->plugin_name);
287 /* address to be removed not found!? */
291 al = GNUNET_malloc (sizeof (struct OwnAddressList) + plugin_address_len);
292 GNUNET_CONTAINER_DLL_insert (oal_head, oal_tail, al);
293 al->plugin_name = GNUNET_strdup (plugin_name);
294 al->addrlen = plugin_address_len;
295 memcpy (&al[1], plugin_address, plugin_address_len);
301 * Test if a particular address is one of ours.
303 * @param plugin_name name of the plugin for which this is an address
304 * @param plugin_address address in a plugin-specific format
305 * @param plugin_address_len number of bytes in plugin_address
306 * @param sig location where to cache PONG signatures for this address [set]
307 * @param sig_expiration how long until the current 'sig' expires?
308 * (ZERO if sig was never created) [set]
309 * @return GNUNET_YES if this is one of our addresses,
313 GST_hello_test_address (const char *plugin_name, const void *plugin_address,
314 size_t plugin_address_len,
315 struct GNUNET_CRYPTO_RsaSignature **sig,
316 struct GNUNET_TIME_Absolute **sig_expiration)
318 struct OwnAddressList *al;
320 for (al = oal_head; al != NULL; al = al->next)
321 if ((plugin_address_len == al->addrlen) &&
322 (0 == strcmp (al->plugin_name, plugin_name)) &&
323 (0 == memcmp (plugin_address, &al[1], plugin_address_len)))
325 *sig = &al->pong_signature;
326 *sig_expiration = &al->pong_sig_expires;
330 *sig_expiration = NULL;
335 /* end of file gnunet-service-transport_hello.c */