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