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