glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / transport / gnunet-service-transport_hello.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010,2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file transport/gnunet-service-transport_hello.c
18  * @brief hello management implementation
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_constants.h"
23 #include "gnunet_hello_lib.h"
24 #include "gnunet_peerinfo_service.h"
25 #include "gnunet_statistics_service.h"
26 #include "gnunet-service-transport_hello.h"
27 #include "gnunet-service-transport.h"
28 #include "gnunet-service-transport_plugins.h"
29
30
31 /**
32  * How often do we refresh our HELLO (due to expiration concerns)?
33  */
34 #define HELLO_REFRESH_PERIOD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
35
36 /**
37  * Hello address expiration
38  */
39 extern struct GNUNET_TIME_Relative hello_expiration;
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    * The address.
60    */
61   struct GNUNET_HELLO_Address *address;
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_EddsaSignature pong_signature;
73
74   /**
75    * How often has this address been added/removed? Used as
76    * some plugins may learn the same external address from
77    * multiple origins.
78    */
79   unsigned int rc;
80
81 };
82
83
84 /**
85  * Our HELLO message.
86  */
87 static struct GNUNET_HELLO_Message *our_hello;
88
89 /**
90  * Function to call on HELLO changes.
91  */
92 static GST_HelloCallback hello_cb;
93
94 /**
95  * Closure for #hello_cb.
96  */
97 static void *hello_cb_cls;
98
99 /**
100  * Head of my addresses.
101  */
102 static struct OwnAddressList *oal_head;
103
104 /**
105  * Tail of my addresses.
106  */
107 static struct OwnAddressList *oal_tail;
108
109 /**
110  * Should we use a friend-only HELLO?
111  */
112 static int friend_option;
113
114 /**
115  * Identifier of #refresh_hello_task().
116  */
117 static struct GNUNET_SCHEDULER_Task *hello_task;
118
119
120 /**
121  * Closure for #address_generator().
122  */
123 struct GeneratorContext
124 {
125   /**
126    * Where are we in the DLL?
127    */
128   struct OwnAddressList *addr_pos;
129
130   /**
131    * When do addresses expire?
132    */
133   struct GNUNET_TIME_Absolute expiration;
134 };
135
136
137 /**
138  * Add an address from the `struct OwnAddressList` to the buffer.
139  *
140  * @param cls the `struct GeneratorContext`
141  * @param max maximum number of bytes left
142  * @param buf where to write the address
143  * @return bytes written or #GNUNET_SYSERR to signal the
144  *         end of the iteration.
145  */
146 static ssize_t
147 address_generator (void *cls,
148                    size_t max,
149                    void *buf)
150 {
151   struct GeneratorContext *gc = cls;
152   ssize_t ret;
153
154   if (NULL == gc->addr_pos)
155     return GNUNET_SYSERR; /* Done */
156   ret = GNUNET_HELLO_add_address (gc->addr_pos->address,
157                                   gc->expiration,
158                                   buf,
159                                   max);
160   gc->addr_pos = gc->addr_pos->next;
161   return ret;
162 }
163
164
165 /**
166  * Construct our HELLO message from all of the addresses of
167  * all of the transports.
168  *
169  * @param cls unused
170  */
171 static void
172 refresh_hello_task (void *cls)
173 {
174   struct GeneratorContext gc;
175
176   hello_task = NULL;
177   gc.addr_pos = oal_head;
178   gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
179
180   GNUNET_free_non_null (our_hello);
181   our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
182                                    &address_generator,
183                                    &gc,
184                                    friend_option);
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186               "Refreshed my %s HELLO, new size is %d\n",
187               (GNUNET_YES == friend_option) ? "friend-only" : "public",
188               GNUNET_HELLO_size (our_hello));
189   GNUNET_STATISTICS_update (GST_stats,
190                             gettext_noop ("# refreshed my HELLO"),
191                             1,
192                             GNUNET_NO);
193   if (NULL != hello_cb)
194     hello_cb (hello_cb_cls,
195               GST_hello_get ());
196   GNUNET_PEERINFO_add_peer (GST_peerinfo,
197                             our_hello,
198                             NULL,
199                             NULL);
200   hello_task =
201       GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
202                                     &refresh_hello_task,
203                                     NULL);
204 }
205
206
207 /**
208  * Schedule task to refresh hello (but only if such a
209  * task exists already, as otherwise the module might
210  * have been shutdown).
211  */
212 static void
213 refresh_hello ()
214 {
215   if (NULL != hello_task)
216   {
217     GNUNET_SCHEDULER_cancel (hello_task);
218     hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
219                                            NULL);
220   }
221 }
222
223
224 /**
225  * Initialize the HELLO module.
226  *
227  * @param friend_only use a friend only hello
228  * @param cb function to call whenever our HELLO changes
229  * @param cb_cls closure for @a cb
230  */
231 void
232 GST_hello_start (int friend_only,
233                  GST_HelloCallback cb,
234                  void *cb_cls)
235 {
236   hello_cb = cb;
237   hello_cb_cls = cb_cls;
238   friend_option = friend_only;
239   refresh_hello_task (NULL);
240 }
241
242
243 /**
244  * Shutdown the HELLO module.
245  */
246 void
247 GST_hello_stop ()
248 {
249   hello_cb = NULL;
250   hello_cb_cls = NULL;
251   if (NULL != hello_task)
252   {
253     GNUNET_SCHEDULER_cancel (hello_task);
254     hello_task = NULL;
255   }
256   if (NULL != our_hello)
257   {
258     GNUNET_free (our_hello);
259     our_hello = NULL;
260   }
261 }
262
263
264 /**
265  * Obtain this peers HELLO message.
266  *
267  * @return our HELLO message
268  */
269 const struct GNUNET_MessageHeader *
270 GST_hello_get ()
271 {
272   return (const struct GNUNET_MessageHeader *) our_hello;
273 }
274
275
276 /**
277  * Add or remove an address from this peer's HELLO message.
278  *
279  * @param addremove #GNUNET_YES to add, #GNUNET_NO to remove
280  * @param address address to add or remove
281  */
282 void
283 GST_hello_modify_addresses (int addremove,
284                             const struct GNUNET_HELLO_Address *address)
285 {
286   struct OwnAddressList *al;
287
288   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289               (GNUNET_YES == addremove)
290               ? "Adding `%s' to the set of our addresses\n"
291               : "Removing `%s' from the set of our addresses\n",
292               GST_plugins_a2s (address));
293   GNUNET_assert (NULL != address);
294   for (al = oal_head; al != NULL; al = al->next)
295     if (0 == GNUNET_HELLO_address_cmp (address, al->address))
296       break;
297   if (GNUNET_NO == addremove)
298   {
299     if (NULL == al)
300     {
301       /* address to be removed not found!? */
302       GNUNET_break (0);
303       return;
304     }
305     al->rc--;
306     if (0 != al->rc)
307       return; /* RC not yet zero */
308     GNUNET_CONTAINER_DLL_remove (oal_head,
309                                  oal_tail,
310                                  al);
311     GNUNET_HELLO_address_free (al->address);
312     GNUNET_free (al);
313     refresh_hello ();
314     return;
315   }
316   if (NULL != al)
317   {
318     /* address added twice or more */
319     al->rc++;
320     return;
321   }
322   al = GNUNET_new (struct OwnAddressList);
323   al->rc = 1;
324   GNUNET_CONTAINER_DLL_insert (oal_head,
325                                oal_tail,
326                                al);
327   al->address = GNUNET_HELLO_address_copy (address);
328   refresh_hello ();
329 }
330
331
332 /**
333  * Test if a particular address is one of ours.
334  *
335  * @param address address to test
336  * @param sig location where to cache PONG signatures for this address [set]
337  * @param sig_expiration how long until the current 'sig' expires?
338  *            (ZERO if sig was never created) [set]
339  * @return #GNUNET_YES if this is one of our addresses,
340  *         #GNUNET_NO if not
341  */
342 int
343 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
344                         struct GNUNET_CRYPTO_EddsaSignature **sig,
345                         struct GNUNET_TIME_Absolute **sig_expiration)
346 {
347   struct OwnAddressList *al;
348
349   for (al = oal_head; al != NULL; al = al->next)
350     if (0 == GNUNET_HELLO_address_cmp (address,
351                                        al->address))
352     {
353       *sig = &al->pong_signature;
354       *sig_expiration = &al->pong_sig_expires;
355       return GNUNET_YES;
356     }
357   *sig = NULL;
358   *sig_expiration = NULL;
359   return GNUNET_NO;
360 }
361
362
363 /* end of file gnunet-service-transport_hello.c */