-fix NPE
[oweals/gnunet.git] / src / transport / gnunet-service-transport_hello.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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  * Hello address expiration
43  */
44 extern struct GNUNET_TIME_Relative hello_expiration;
45
46
47 /**
48  * Entry in linked list of network addresses for ourselves.  Also
49  * includes a cached signature for 'struct TransportPongMessage's.
50  */
51 struct OwnAddressList
52 {
53   /**
54    * This is a doubly-linked list.
55    */
56   struct OwnAddressList *next;
57
58   /**
59    * This is a doubly-linked list.
60    */
61   struct OwnAddressList *prev;
62
63   /**
64    * The address.
65    */
66   struct GNUNET_HELLO_Address *address;
67
68   /**
69    * How long until the current signature expires? (ZERO if the
70    * signature was never created).
71    */
72   struct GNUNET_TIME_Absolute pong_sig_expires;
73
74   /**
75    * Signature for a 'struct TransportPongMessage' for this address.
76    */
77   struct GNUNET_CRYPTO_EddsaSignature pong_signature;
78
79   /**
80    * How often has this address been added/removed? Used as
81    * some plugins may learn the same external address from
82    * multiple origins.
83    */
84   unsigned int rc;
85
86 };
87
88
89 /**
90  * Our HELLO message.
91  */
92 static struct GNUNET_HELLO_Message *our_hello;
93
94 /**
95  * Function to call on HELLO changes.
96  */
97 static GST_HelloCallback hello_cb;
98
99 /**
100  * Closure for #hello_cb.
101  */
102 static void *hello_cb_cls;
103
104 /**
105  * Head of my addresses.
106  */
107 static struct OwnAddressList *oal_head;
108
109 /**
110  * Tail of my addresses.
111  */
112 static struct OwnAddressList *oal_tail;
113
114 /**
115  * Should we use a friend-only HELLO?
116  */
117 static int friend_option;
118
119 /**
120  * Identifier of #refresh_hello_task().
121  */
122 static struct GNUNET_SCHEDULER_Task *hello_task;
123
124
125 /**
126  * Closure for #address_generator().
127  */
128 struct GeneratorContext
129 {
130   /**
131    * Where are we in the DLL?
132    */
133   struct OwnAddressList *addr_pos;
134
135   /**
136    * When do addresses expire?
137    */
138   struct GNUNET_TIME_Absolute expiration;
139 };
140
141
142 /**
143  * Add an address from the `struct OwnAddressList` to the buffer.
144  *
145  * @param cls the `struct GeneratorContext`
146  * @param max maximum number of bytes left
147  * @param buf where to write the address
148  * @return bytes written or #GNUNET_SYSERR to signal the
149  *         end of the iteration.
150  */
151 static ssize_t
152 address_generator (void *cls,
153                    size_t max,
154                    void *buf)
155 {
156   struct GeneratorContext *gc = cls;
157   ssize_t ret;
158
159   if (NULL == gc->addr_pos)
160     return GNUNET_SYSERR; /* Done */
161   ret = GNUNET_HELLO_add_address (gc->addr_pos->address,
162                                   gc->expiration,
163                                   buf,
164                                   max);
165   gc->addr_pos = gc->addr_pos->next;
166   return ret;
167 }
168
169
170 /**
171  * Construct our HELLO message from all of the addresses of
172  * all of the transports.
173  *
174  * @param cls unused
175  * @param tc scheduler context
176  */
177 static void
178 refresh_hello_task (void *cls,
179                     const struct GNUNET_SCHEDULER_TaskContext *tc)
180 {
181   struct GeneratorContext gc;
182
183   hello_task = NULL;
184   gc.addr_pos = oal_head;
185   gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
186
187   GNUNET_free_non_null (our_hello);
188   our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
189                                    &address_generator,
190                                    &gc,
191                                    friend_option);
192   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193               "Refreshed my %s HELLO, new size is %d\n",
194               (GNUNET_YES == friend_option) ? "friend-only" : "public",
195               GNUNET_HELLO_size (our_hello));
196   GNUNET_STATISTICS_update (GST_stats,
197                             gettext_noop ("# refreshed my HELLO"),
198                             1,
199                             GNUNET_NO);
200   if (NULL != hello_cb)
201     hello_cb (hello_cb_cls,
202               GST_hello_get ());
203   GNUNET_PEERINFO_add_peer (GST_peerinfo,
204                             our_hello,
205                             NULL,
206                             NULL);
207   hello_task =
208       GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
209                                     &refresh_hello_task,
210                                     NULL);
211 }
212
213
214 /**
215  * Schedule task to refresh hello (but only if such a
216  * task exists already, as otherwise the module might
217  * have been shutdown).
218  */
219 static void
220 refresh_hello ()
221 {
222   if (NULL != hello_task)
223   {
224     GNUNET_SCHEDULER_cancel (hello_task);
225     hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
226                                            NULL);
227   }
228 }
229
230
231 /**
232  * Initialize the HELLO module.
233  *
234  * @param friend_only use a friend only hello
235  * @param cb function to call whenever our HELLO changes
236  * @param cb_cls closure for @a cb
237  */
238 void
239 GST_hello_start (int friend_only,
240                  GST_HelloCallback cb,
241                  void *cb_cls)
242 {
243   hello_cb = cb;
244   hello_cb_cls = cb_cls;
245   friend_option = friend_only;
246   refresh_hello_task (NULL, NULL);
247 }
248
249
250 /**
251  * Shutdown the HELLO module.
252  */
253 void
254 GST_hello_stop ()
255 {
256   hello_cb = NULL;
257   hello_cb_cls = NULL;
258   if (NULL != hello_task)
259   {
260     GNUNET_SCHEDULER_cancel (hello_task);
261     hello_task = NULL;
262   }
263   if (NULL != our_hello)
264   {
265     GNUNET_free (our_hello);
266     our_hello = NULL;
267   }
268 }
269
270
271 /**
272  * Obtain this peers HELLO message.
273  *
274  * @return our HELLO message
275  */
276 const struct GNUNET_MessageHeader *
277 GST_hello_get ()
278 {
279   return (const struct GNUNET_MessageHeader *) our_hello;
280 }
281
282
283 /**
284  * Add or remove an address from this peer's HELLO message.
285  *
286  * @param addremove #GNUNET_YES to add, #GNUNET_NO to remove
287  * @param address address to add or remove
288  */
289 void
290 GST_hello_modify_addresses (int addremove,
291                             const struct GNUNET_HELLO_Address *address)
292 {
293   struct OwnAddressList *al;
294
295   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296               (GNUNET_YES == addremove)
297               ? "Adding `%s' to the set of our addresses\n"
298               : "Removing `%s' from the set of our addresses\n",
299               GST_plugins_a2s (address));
300   GNUNET_assert (NULL != address);
301   for (al = oal_head; al != NULL; al = al->next)
302     if (0 == GNUNET_HELLO_address_cmp (address, al->address))
303       break;
304   if (GNUNET_NO == addremove)
305   {
306     if (NULL == al)
307     {
308       /* address to be removed not found!? */
309       GNUNET_break (0);
310       return;
311     }
312     al->rc--;
313     if (0 != al->rc)
314       return; /* RC not yet zero */
315     GNUNET_CONTAINER_DLL_remove (oal_head,
316                                  oal_tail,
317                                  al);
318     GNUNET_HELLO_address_free (al->address);
319     GNUNET_free (al);
320     refresh_hello ();
321     return;
322   }
323   if (NULL != al)
324   {
325     /* address added twice or more */
326     al->rc++;
327     return;
328   }
329   al = GNUNET_new (struct OwnAddressList);
330   al->rc = 1;
331   GNUNET_CONTAINER_DLL_insert (oal_head,
332                                oal_tail,
333                                al);
334   al->address = GNUNET_HELLO_address_copy (address);
335   refresh_hello ();
336 }
337
338
339 /**
340  * Test if a particular address is one of ours.
341  *
342  * @param address address to test
343  * @param sig location where to cache PONG signatures for this address [set]
344  * @param sig_expiration how long until the current 'sig' expires?
345  *            (ZERO if sig was never created) [set]
346  * @return #GNUNET_YES if this is one of our addresses,
347  *         #GNUNET_NO if not
348  */
349 int
350 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
351                         struct GNUNET_CRYPTO_EddsaSignature **sig,
352                         struct GNUNET_TIME_Absolute **sig_expiration)
353 {
354   struct OwnAddressList *al;
355
356   for (al = oal_head; al != NULL; al = al->next)
357     if (0 == GNUNET_HELLO_address_cmp (address,
358                                        al->address))
359     {
360       *sig = &al->pong_signature;
361       *sig_expiration = &al->pong_sig_expires;
362       return GNUNET_YES;
363     }
364   *sig = NULL;
365   *sig_expiration = NULL;
366   return GNUNET_NO;
367 }
368
369
370 /* end of file gnunet-service-transport_hello.c */