codesonar fixes, some other changes
[oweals/gnunet.git] / src / dv / plugin_transport_dv.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 2, 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 dv/plugin_transport_dv.c
23  * @brief DV transport service, takes incoming DV requests and deals with
24  * the DV service
25  * @author Christian Grothoff
26  */
27
28 /**
29  * TODO:
30  *
31  * As a start, the dv plugin needs to listen for information from the dv
32  * service.  The plugin (?) will be notified by core (?) when a tcp/udp/whatever
33  * message comes in that should be for dv.  The plugin will then hand off the message
34  * to the dv service which will decrypt/validate the message (?) and then send the
35  * result back to us (the transport) which will then send the message to the transport
36  * service (yikes).
37  *
38  * Or, core will notify the dv service directly which will validate,
39  * etc. and then just send a message to us.
40  *
41  * For starters, this plugin needs to have a client which will listen for messages from
42  * the dv service that need to be sent up to the gnunet-transport-service.
43  *
44  * Messages sent from the dv transport get passed to the dv service which deals
45  * with the actual sending (how much state does this transport need? should it know
46  * which peers it is currently connected to and their distances, or just assume that
47  * anything should be passed along to the dv service?).
48  */
49 #include "platform.h"
50 #include "gnunet_protocols.h"
51 #include "gnunet_connection_lib.h"
52 #include "gnunet_server_lib.h"
53 #include "gnunet_service_lib.h"
54 #include "gnunet_statistics_service.h"
55 #include "gnunet_dv_service.h"
56 #include "gnunet_transport_service.h"
57 #include "../transport/plugin_transport.h"
58 #include "dv.h"
59
60 #define DEBUG_TEMPLATE GNUNET_NO
61
62 /**
63  * Encapsulation of all of the state of the plugin.
64  */
65 struct Plugin;
66
67
68 /**
69  * Session handle for connections.
70  */
71 struct Session
72 {
73
74   /**
75    * Stored in a linked list.
76    */
77   struct Session *next;
78
79   /**
80    * Pointer to the global plugin struct.
81    */
82   struct Plugin *plugin;
83
84   /**
85    * The client (used to identify this connection)
86    */
87   /* void *client; */
88
89   /**
90    * Continuation function to call once the transmission buffer
91    * has again space available.  NULL if there is no
92    * continuation to call.
93    */
94   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
95
96   /**
97    * Closure for transmit_cont.
98    */
99   void *transmit_cont_cls;
100
101   /**
102    * To whom are we talking to (set to our identity
103    * if we are still waiting for the welcome message)
104    */
105   struct GNUNET_PeerIdentity sender;
106
107   /**
108    * At what time did we reset last_received last?
109    */
110   struct GNUNET_TIME_Absolute last_quota_update;
111
112   /**
113    * How many bytes have we received since the "last_quota_update"
114    * timestamp?
115    */
116   uint64_t last_received;
117
118   /**
119    * Number of bytes per ms that this peer is allowed
120    * to send to us.
121    */
122   uint32_t quota;
123
124 };
125
126 /**
127  * Encapsulation of all of the state of the plugin.
128  */
129 struct Plugin
130 {
131   /**
132    * Our environment.
133    */
134   struct GNUNET_TRANSPORT_PluginEnvironment *env;
135
136   /**
137    * List of open sessions.
138    */
139   struct Session *sessions;
140
141   /**
142    * Our server.
143    */
144   //struct GNUNET_SERVER_Handle *server;
145
146   /*
147    * Handle to the running service.
148    */
149   //struct GNUNET_SERVICE_Context *service;
150
151   /**
152    * Copy of the handler array where the closures are
153    * set to this struct's instance.
154    */
155   struct GNUNET_SERVER_MessageHandler *handlers;
156
157   /**
158    * Handle to the DV service
159    */
160   struct GNUNET_DV_Handle *dv_handle;
161
162 };
163
164 /**
165  * Handler for messages received from the DV service.
166  */
167 void handle_dv_message_received (void *cls,
168                                  struct GNUNET_PeerIdentity *sender,
169                                  char *msg,
170                                  size_t msg_len,
171                                  uint32_t distance,
172                                  char *sender_address,
173                                  size_t sender_address_len)
174 {
175   struct Plugin *plugin = cls;
176 #if DEBUG_DV_MESSAGES
177   char *my_id;
178   my_id = GNUNET_strdup(GNUNET_i2s(plugin->env->my_identity));
179   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
180                    "plugin_transport_dv",
181                    _("%s Received message from %s) of type %d, distance %u!\n"),
182                    my_id, GNUNET_i2s(sender), ntohs(((struct GNUNET_MessageHeader *)msg)->type), distance);
183   GNUNET_free_non_null(my_id);
184 #endif
185   plugin->env->receive(plugin->env->cls,
186                        sender,
187                        (struct GNUNET_MessageHeader *)msg,
188                        distance,
189                        NULL,
190                        sender_address,
191                        sender_address_len);
192
193 }
194
195
196 /* Question: how does the transport service learn of a newly connected (gossipped about)
197  * DV peer?  Should the plugin (here) create a HELLO for that peer and send it along,
198  * or should the DV service create a HELLO and send it to us via the other part?
199  */
200
201 /**
202  * Function that can be used by the transport service to transmit
203  * a message using the plugin.
204  *
205  * @param cls closure
206  * @param target who should receive this message
207  * @param priority how important is the message
208  * @param msgbuf the message to transmit
209  * @param msgbuf_size number of bytes in 'msgbuf'
210  * @param timeout when should we time out
211  * @param session the session used
212  * @param addr the address to use (can be NULL if the plugin
213  *                is "on its own" (i.e. re-use existing TCP connection))
214  * @param addrlen length of the address in bytes
215  * @param force_address GNUNET_YES if the plugin MUST use the given address,
216  *                otherwise the plugin may use other addresses or
217  *                existing connections (if available)
218  * @param cont continuation to call once the message has
219  *        been transmitted (or if the transport is ready
220  *        for the next transmission call; or if the
221  *        peer disconnected...)
222  * @param cont_cls closure for cont
223  * @return number of bytes used (on the physical network, with overheads);
224  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
225  *         and does NOT mean that the message was not transmitted (DV)
226  */
227 static ssize_t
228 dv_plugin_send (void *cls,
229                 const struct GNUNET_PeerIdentity *target,
230                 const char *msgbuf,
231                 size_t msgbuf_size,
232                 unsigned int priority,
233                 struct GNUNET_TIME_Relative timeout,
234                 struct Session *session,
235                 const void *addr,
236                 size_t addrlen,
237                 int force_address,
238                 GNUNET_TRANSPORT_TransmitContinuation
239                 cont, void *cont_cls)
240 {
241   int ret = 0;
242   struct Plugin *plugin = cls;
243
244   ret = GNUNET_DV_send(plugin->dv_handle,
245                        target,
246                        msgbuf,
247                        msgbuf_size,
248                        priority,
249                        timeout,
250                        addr,
251                        addrlen,
252                        cont,
253                        cont_cls);
254 #if DEBUG_DV
255   GNUNET_free_non_null(my_identity);
256 #endif
257
258   return ret;
259 }
260
261
262
263 /**
264  * Function that can be used to force the plugin to disconnect
265  * from the given peer and cancel all previous transmissions
266  * (and their continuations).
267  *
268  * @param cls closure
269  * @param target peer from which to disconnect
270  */
271 static void
272 dv_plugin_disconnect (void *cls,
273                       const struct GNUNET_PeerIdentity *target)
274 {
275   // struct Plugin *plugin = cls;
276   // TODO: Add message type to send to dv service to "disconnect" a peer
277 }
278
279
280 /**
281  * Convert the transports address to a nice, human-readable
282  * format.
283  *
284  * @param cls closure
285  * @param type name of the transport that generated the address
286  * @param addr one of the addresses of the host, NULL for the last address
287  *        the specific address format depends on the transport
288  * @param addrlen length of the address
289  * @param numeric should (IP) addresses be displayed in numeric form?
290  * @param timeout after how long should we give up?
291  * @param asc function to call on each string
292  * @param asc_cls closure for asc
293  */
294 static void
295 dv_plugin_address_pretty_printer (void *cls,
296                                   const char *type,
297                                   const void *addr,
298                                   size_t addrlen,
299                                   int numeric,
300                                   struct GNUNET_TIME_Relative timeout,
301                                   GNUNET_TRANSPORT_AddressStringCallback
302                                   asc, void *asc_cls)
303 {
304   char *dest_peer;
305   char *via_peer;
306   char *print_string;
307   char *addr_buf = (char *)addr;
308
309   if (addrlen != sizeof(struct GNUNET_PeerIdentity) * 2)
310     {
311       asc (asc_cls, NULL);
312     }
313   else
314     {
315       dest_peer = GNUNET_strdup(GNUNET_i2s((struct GNUNET_PeerIdentity *)addr));
316       via_peer = GNUNET_strdup(GNUNET_i2s((struct GNUNET_PeerIdentity *)&addr_buf[sizeof(struct GNUNET_PeerIdentity)]));
317       GNUNET_asprintf(&print_string, "DV Peer `%s' via peer`%s'", dest_peer, via_peer);
318       asc (asc_cls, print_string);
319       asc (asc_cls, NULL);
320       GNUNET_free(via_peer);
321       GNUNET_free(dest_peer);
322       GNUNET_free(print_string);
323     }
324 }
325
326 /**
327  * Convert the DV address to a pretty string.
328  *
329  * @param cls closure
330  * @param addr the (hopefully) DV address
331  * @param addrlen the length of the address
332  *
333  * @return string representing the DV address
334  */
335 static const char *address_to_string (void *cls,
336                                        const void *addr,
337                                        size_t addrlen)
338 {
339   static char return_buffer[2 * 4 + 2]; // Two four character peer identity prefixes a ':' and '\0'
340
341   struct GNUNET_CRYPTO_HashAsciiEncoded peer_hash;
342   struct GNUNET_CRYPTO_HashAsciiEncoded via_hash;
343   struct GNUNET_PeerIdentity *peer;
344   struct GNUNET_PeerIdentity *via;
345   char *addr_buf = (char *)addr;
346
347   if (addrlen == (2 * sizeof(struct GNUNET_PeerIdentity)))
348     {
349       peer = (struct GNUNET_PeerIdentity *)addr_buf;
350       via = (struct GNUNET_PeerIdentity *)&addr_buf[sizeof(struct GNUNET_PeerIdentity)];
351
352       GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &peer_hash);
353       peer_hash.encoding[4] = '\0';
354       GNUNET_CRYPTO_hash_to_enc (&via->hashPubKey, &via_hash);
355       via_hash.encoding[4] = '\0';
356       GNUNET_snprintf (return_buffer,
357                        sizeof (return_buffer),
358                        "%s:%s",
359                        &peer_hash,
360                        &via_hash);
361     }
362   else
363     return NULL;
364
365   return return_buffer;
366 }
367
368 /**
369  * Another peer has suggested an address for this
370  * peer and transport plugin.  Check that this could be a valid
371  * address.  If so, consider adding it to the list
372  * of addresses.
373  *
374  * @param cls closure
375  * @param addr pointer to the address
376  * @param addrlen length of addr
377  * @return GNUNET_OK if this is a plausible address for this peer
378  *         and transport
379  *
380  * FIXME: does this mean anything for the DV plugin?
381  */
382 static int
383 dv_plugin_address_suggested (void *cls,
384                                   void *addr, size_t addrlen)
385 {
386   /* struct Plugin *plugin = cls; */
387
388   /* check if the address is plausible; if so,
389      add it to our list! */
390   return GNUNET_NO;
391 }
392
393
394 /**
395  * Entry point for the plugin.
396  */
397 void *
398 libgnunet_plugin_transport_dv_init (void *cls)
399 {
400   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
401   struct GNUNET_TRANSPORT_PluginFunctions *api;
402   struct Plugin *plugin;
403
404   plugin = GNUNET_malloc (sizeof (struct Plugin));
405   plugin->env = env;
406   //plugin->service = service;
407   //plugin->server = GNUNET_SERVICE_get_server (service);
408
409   plugin->dv_handle = GNUNET_DV_connect(env->sched, env->cfg, &handle_dv_message_received, plugin);
410
411   if (plugin->dv_handle == NULL)
412   {
413     GNUNET_free(plugin);
414     return NULL;
415   }
416
417   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
418   api->cls = plugin;
419   api->send = &dv_plugin_send;
420   api->disconnect = &dv_plugin_disconnect;
421   api->address_pretty_printer = &dv_plugin_address_pretty_printer;
422   api->check_address = &dv_plugin_address_suggested;
423   api->address_to_string = &address_to_string;
424   return api;
425 }
426
427
428 /**
429  * Exit point from the plugin.
430  */
431 void *
432 libgnunet_plugin_transport_dv_done (void *cls)
433 {
434   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
435   struct Plugin *plugin = api->cls;
436
437   GNUNET_free (plugin);
438   GNUNET_free (api);
439   return NULL;
440 }
441
442 /* end of plugin_transport_template.c */