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