2793ce5eb0698b97c476dcb96f7037e86a58427e
[oweals/gnunet.git] / src / transport / gnunet-service-transport_hello.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/gnunet-service-transport_hello.c
23  * @brief hello management implementation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
33
34
35 /**
36  * How often do we refresh our HELLO (due to expiration concerns)?
37  */
38 #define HELLO_REFRESH_PERIOD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
39
40
41 /**
42  * Entry in linked list of network addresses for ourselves.  Also
43  * includes a cached signature for 'struct TransportPongMessage's.
44  */
45 struct OwnAddressList
46 {
47   /**
48    * This is a doubly-linked list.
49    */
50   struct OwnAddressList *next;
51
52   /**
53    * This is a doubly-linked list.
54    */
55   struct OwnAddressList *prev;
56
57   /**
58    * Name of the plugin.
59    */
60   char *plugin_name;
61
62   /**
63    * How long until the current signature expires? (ZERO if the
64    * signature was never created).
65    */
66   struct GNUNET_TIME_Absolute pong_sig_expires;
67
68   /**
69    * Signature for a 'struct TransportPongMessage' for this address.
70    */
71   struct GNUNET_CRYPTO_RsaSignature pong_signature;
72
73   /**
74    * Length of addr.
75    */
76   size_t addrlen;
77
78 };
79
80
81 /**
82  * Our HELLO message.
83  */
84 static struct GNUNET_HELLO_Message *our_hello;
85
86 /**
87  * Function to call on HELLO changes.
88  */
89 static GST_HelloCallback hello_cb;
90
91 /**
92  * Closure for 'hello_cb'.
93  */
94 static void *hello_cb_cls;
95
96 /**
97  * Head of my addresses.
98  */
99 struct OwnAddressList *oal_head;
100
101 /**
102  * Tail of my addresses.
103  */
104 struct OwnAddressList *oal_tail;
105
106 /**
107  * Identifier of 'refresh_hello' task.
108  */
109 static GNUNET_SCHEDULER_TaskIdentifier hello_task;
110
111
112 /**
113  * Closure for 'address_generator'.
114  */
115 struct GeneratorContext
116 {
117   /**
118    * Where are we in the DLL?
119    */
120   struct OwnAddressList *addr_pos;
121
122   /**
123    * When do addresses expire?
124    */
125   struct GNUNET_TIME_Absolute expiration;
126 };
127
128
129 /**
130  * Add an address from the 'OwnAddressList' to the buffer.
131  *
132  * @param cls the 'struct GeneratorContext'
133  * @param max maximum number of bytes left
134  * @param buf where to write the address
135  */
136 static size_t
137 address_generator (void *cls, size_t max, void *buf)
138 {
139   struct GeneratorContext *gc = cls;
140   size_t ret;
141
142   if (NULL == gc->addr_pos)
143     return 0;
144   ret =
145       GNUNET_HELLO_add_address (gc->addr_pos->plugin_name, gc->expiration,
146                                 &gc->addr_pos[1], gc->addr_pos->addrlen, buf,
147                                 max);
148   gc->addr_pos = gc->addr_pos->next;
149   return ret;
150 }
151
152
153 /**
154  * Construct our HELLO message from all of the addresses of
155  * all of the transports.
156  *
157  * @param cls unused
158  * @param tc scheduler context
159  */
160 static void
161 refresh_hello_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 {
163   struct GeneratorContext gc;
164
165   hello_task = GNUNET_SCHEDULER_NO_TASK;
166   gc.addr_pos = oal_head;
167   gc.expiration =
168       GNUNET_TIME_relative_to_absolute (GST_HELLO_ADDRESS_EXPIRATION);
169   GNUNET_free (our_hello);
170   our_hello = GNUNET_HELLO_create (&GST_my_public_key, &address_generator, &gc);
171 #if DEBUG_TRANSPORT
172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
173               "Refreshed my `%s', new size is %d\n", "HELLO",
174               GNUNET_HELLO_size (our_hello));
175 #endif
176   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# refreshed my HELLO"), 1,
177                             GNUNET_NO);
178   if (NULL != hello_cb)
179     hello_cb (hello_cb_cls, GST_hello_get ());
180   GNUNET_PEERINFO_add_peer (GST_peerinfo, our_hello);
181   hello_task =
182       GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD, &refresh_hello_task,
183                                     NULL);
184
185 }
186
187
188 /**
189  * Schedule task to refresh hello (unless such a
190  * task exists already).
191  */
192 static void
193 refresh_hello ()
194 {
195   if (hello_task != GNUNET_SCHEDULER_NO_TASK)
196     GNUNET_SCHEDULER_cancel (hello_task);
197   hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task, NULL);
198 }
199
200
201 /**
202  * Initialize the HELLO module.
203  *
204  * @param cb function to call whenever our HELLO changes
205  * @param cb_cls closure for cb
206  */
207 void
208 GST_hello_start (GST_HelloCallback cb, void *cb_cls)
209 {
210   hello_cb = cb;
211   hello_cb_cls = cb_cls;
212   our_hello = GNUNET_HELLO_create (&GST_my_public_key, NULL, NULL);
213   refresh_hello ();
214 }
215
216
217 /**
218  * Shutdown the HELLO module.
219  */
220 void
221 GST_hello_stop ()
222 {
223   hello_cb = NULL;
224   hello_cb_cls = NULL;
225   if (GNUNET_SCHEDULER_NO_TASK != hello_task)
226   {
227     GNUNET_SCHEDULER_cancel (hello_task);
228     hello_task = GNUNET_SCHEDULER_NO_TASK;
229   }
230   if (NULL != our_hello)
231   {
232     GNUNET_free (our_hello);
233     our_hello = NULL;
234   }
235 }
236
237
238 /**
239  * Obtain this peers HELLO message.
240  *
241  * @return our HELLO message
242  */
243 const struct GNUNET_MessageHeader *
244 GST_hello_get ()
245 {
246   return (struct GNUNET_MessageHeader *) our_hello;
247 }
248
249
250 /**
251  * Add or remove an address from this peer's HELLO message.
252  *
253  * @param addremove GNUNET_YES to add, GNUNET_NO to remove
254  * @param plugin_name name of the plugin for which this is an address
255  * @param plugin_address address in a plugin-specific format
256  * @param plugin_address_len number of bytes in plugin_address
257  */
258 void
259 GST_hello_modify_addresses (int addremove, const char *plugin_name,
260                             const void *plugin_address,
261                             size_t plugin_address_len)
262 {
263   struct OwnAddressList *al;
264
265 #if DEBUG_TRANSPORT
266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267               (add_remove ==
268                GNUNET_YES) ? "Adding `%s':%s to the set of our addresses\n" :
269               "Removing `%s':%s from the set of our addresses\n",
270               GST_plugins_a2s (plugin_name, addr, addrlen), p->short_name);
271 #endif
272   GNUNET_assert (plugin_address != NULL);
273   if (GNUNET_NO == addremove)
274   {
275     for (al = oal_head; al != NULL; al = al->next)
276       if ((plugin_address_len == al->addrlen) &&
277           (0 == strcmp (al->plugin_name, plugin_name)) &&
278           (0 == memcmp (plugin_address, &al[1], plugin_address_len)))
279       {
280         GNUNET_CONTAINER_DLL_remove (oal_head, oal_tail, al);
281         GNUNET_free (al->plugin_name);
282         GNUNET_free (al);
283         refresh_hello ();
284         return;
285       }
286     /* address to be removed not found!? */
287     GNUNET_break (0);
288     return;
289   }
290   al = GNUNET_malloc (sizeof (struct OwnAddressList) + plugin_address_len);
291   GNUNET_CONTAINER_DLL_insert (oal_head, oal_tail, al);
292   al->plugin_name = GNUNET_strdup (plugin_name);
293   al->addrlen = plugin_address_len;
294   memcpy (&al[1], plugin_address, plugin_address_len);
295   refresh_hello ();
296 }
297
298
299 /**
300  * Test if a particular address is one of ours.
301  *
302  * @param plugin_name name of the plugin for which this is an address
303  * @param plugin_address address in a plugin-specific format
304  * @param plugin_address_len number of bytes in plugin_address
305  * @param sig location where to cache PONG signatures for this address [set]
306  * @param sig_expiration how long until the current 'sig' expires?
307  *            (ZERO if sig was never created) [set]
308  * @return GNUNET_YES if this is one of our addresses,
309  *         GNUNET_NO if not
310  */
311 int
312 GST_hello_test_address (const char *plugin_name, const void *plugin_address,
313                         size_t plugin_address_len,
314                         struct GNUNET_CRYPTO_RsaSignature **sig,
315                         struct GNUNET_TIME_Absolute **sig_expiration)
316 {
317   struct OwnAddressList *al;
318
319   for (al = oal_head; al != NULL; al = al->next)
320     if ((plugin_address_len == al->addrlen) &&
321         (0 == strcmp (al->plugin_name, plugin_name)) &&
322         (0 == memcmp (plugin_address, &al[1], plugin_address_len)))
323     {
324       *sig = &al->pong_signature;
325       *sig_expiration = &al->pong_sig_expires;
326       return GNUNET_YES;
327     }
328   *sig = NULL;
329   *sig_expiration = NULL;
330   return GNUNET_NO;
331 }
332
333
334 /* end of file gnunet-service-transport_hello.c */