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