generate our own HELLO before starting validations
[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., 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  * 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
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 static struct OwnAddressList *oal_head;
101
102 /**
103  * Tail of my addresses.
104  */
105 static struct OwnAddressList *oal_tail;
106
107 /**
108  * Should we use a friend-only HELLO?
109  */
110 static int friend_option;
111
112 /**
113  * Identifier of #refresh_hello_task().
114  */
115 static struct GNUNET_SCHEDULER_Task *hello_task;
116
117
118 /**
119  * Closure for #address_generator().
120  */
121 struct GeneratorContext
122 {
123   /**
124    * Where are we in the DLL?
125    */
126   struct OwnAddressList *addr_pos;
127
128   /**
129    * When do addresses expire?
130    */
131   struct GNUNET_TIME_Absolute expiration;
132 };
133
134
135 /**
136  * Add an address from the 'OwnAddressList' to the buffer.
137  *
138  * @param cls the `struct GeneratorContext`
139  * @param max maximum number of bytes left
140  * @param buf where to write the address
141  * @return bytes written or #GNUNET_SYSERR to signal the
142  *         end of the iteration.
143  */
144 static ssize_t
145 address_generator (void *cls,
146                    size_t max,
147                    void *buf)
148 {
149   struct GeneratorContext *gc = cls;
150   ssize_t ret;
151
152   if (NULL == gc->addr_pos)
153     return GNUNET_SYSERR; /* Done */
154   ret = GNUNET_HELLO_add_address (gc->addr_pos->address, gc->expiration, buf,
155                                 max);
156   gc->addr_pos = gc->addr_pos->next;
157   return ret;
158 }
159
160
161 /**
162  * Construct our HELLO message from all of the addresses of
163  * all of the transports.
164  *
165  * @param cls unused
166  * @param tc scheduler context
167  */
168 static void
169 refresh_hello_task (void *cls,
170                     const struct GNUNET_SCHEDULER_TaskContext *tc)
171 {
172   struct GeneratorContext gc;
173
174   hello_task = NULL;
175   gc.addr_pos = oal_head;
176   gc.expiration = GNUNET_TIME_relative_to_absolute (hello_expiration);
177
178   GNUNET_free_non_null (our_hello);
179   our_hello = GNUNET_HELLO_create (&GST_my_identity.public_key,
180                                    &address_generator,
181                                    &gc,
182                                    friend_option);
183   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184               "Refreshed my %s HELLO, new size is %d\n",
185               (GNUNET_YES == friend_option) ? "friend-only" : "public",
186               GNUNET_HELLO_size (our_hello));
187   GNUNET_STATISTICS_update (GST_stats,
188                             gettext_noop ("# refreshed my HELLO"),
189                             1,
190                             GNUNET_NO);
191   if (NULL != hello_cb)
192     hello_cb (hello_cb_cls,
193               GST_hello_get ());
194   GNUNET_PEERINFO_add_peer (GST_peerinfo,
195                             our_hello,
196                             NULL,
197                             NULL);
198   hello_task =
199       GNUNET_SCHEDULER_add_delayed (HELLO_REFRESH_PERIOD,
200                                     &refresh_hello_task,
201                                     NULL);
202 }
203
204
205 /**
206  * Schedule task to refresh hello (unless such a
207  * task exists already).
208  */
209 static void
210 refresh_hello ()
211 {
212   if (NULL != hello_task)
213     GNUNET_SCHEDULER_cancel (hello_task);
214   hello_task = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
215                                          NULL);
216 }
217
218
219 /**
220  * Initialize the HELLO module.
221  *
222  * @param friend_only use a friend only hello
223  * @param cb function to call whenever our HELLO changes
224  * @param cb_cls closure for @a cb
225  */
226 void
227 GST_hello_start (int friend_only,
228                  GST_HelloCallback cb,
229                  void *cb_cls)
230 {
231   hello_cb = cb;
232   hello_cb_cls = cb_cls;
233   friend_option = friend_only;
234   refresh_hello_task (NULL, NULL);
235 }
236
237
238 /**
239  * Shutdown the HELLO module.
240  */
241 void
242 GST_hello_stop ()
243 {
244   hello_cb = NULL;
245   hello_cb_cls = NULL;
246   if (NULL != hello_task)
247   {
248     GNUNET_SCHEDULER_cancel (hello_task);
249     hello_task = NULL;
250   }
251   if (NULL != our_hello)
252   {
253     GNUNET_free (our_hello);
254     our_hello = NULL;
255   }
256 }
257
258
259 /**
260  * Obtain this peers HELLO message.
261  *
262  * @return our HELLO message
263  */
264 const struct GNUNET_MessageHeader *
265 GST_hello_get ()
266 {
267   return (struct GNUNET_MessageHeader *) our_hello;
268 }
269
270
271 /**
272  * Add or remove an address from this peer's HELLO message.
273  *
274  * @param addremove #GNUNET_YES to add, #GNUNET_NO to remove
275  * @param address address to add or remove
276  */
277 void
278 GST_hello_modify_addresses (int addremove,
279                             const struct GNUNET_HELLO_Address *address)
280 {
281   struct OwnAddressList *al;
282
283   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284               (addremove ==
285                GNUNET_YES) ? "Adding `%s' to the set of our addresses\n" :
286               "Removing `%s' from the set of our addresses\n",
287               GST_plugins_a2s (address));
288   GNUNET_assert (address != NULL);
289   if (GNUNET_NO == addremove)
290   {
291     for (al = oal_head; al != NULL; al = al->next)
292       if (0 == GNUNET_HELLO_address_cmp (address, al->address))
293       {
294         GNUNET_CONTAINER_DLL_remove (oal_head, oal_tail, al);
295         GNUNET_HELLO_address_free (al->address);
296         GNUNET_free (al);
297         refresh_hello ();
298         return;
299       }
300     /* address to be removed not found!? */
301     GNUNET_break (0);
302     return;
303   }
304   al = GNUNET_new (struct OwnAddressList);
305   GNUNET_CONTAINER_DLL_insert (oal_head, oal_tail, al);
306   al->address = GNUNET_HELLO_address_copy (address);
307   refresh_hello ();
308 }
309
310
311 /**
312  * Test if a particular address is one of ours.
313  *
314  * @param address address to test
315  * @param sig location where to cache PONG signatures for this address [set]
316  * @param sig_expiration how long until the current 'sig' expires?
317  *            (ZERO if sig was never created) [set]
318  * @return #GNUNET_YES if this is one of our addresses,
319  *         #GNUNET_NO if not
320  */
321 int
322 GST_hello_test_address (const struct GNUNET_HELLO_Address *address,
323                         struct GNUNET_CRYPTO_EddsaSignature **sig,
324                         struct GNUNET_TIME_Absolute **sig_expiration)
325 {
326   struct OwnAddressList *al;
327
328   for (al = oal_head; al != NULL; al = al->next)
329     if (0 == GNUNET_HELLO_address_cmp (address,
330                                        al->address))
331     {
332       *sig = &al->pong_signature;
333       *sig_expiration = &al->pong_sig_expires;
334       return GNUNET_YES;
335     }
336   *sig = NULL;
337   *sig_expiration = NULL;
338   return GNUNET_NO;
339 }
340
341
342 /* end of file gnunet-service-transport_hello.c */