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