30e5b6ac1278aa7ab6bde9900e5c5cfabf4d8068
[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 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 "../transport/plugin_transport.h"
38 #include "dv.h"
39
40 #define DEBUG_TEMPLATE GNUNET_NO
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 handle_dv_message_received (void *cls,
148                                  struct GNUNET_PeerIdentity *sender,
149                                  char *msg,
150                                  size_t msg_len,
151                                  uint32_t distance,
152                                  char *sender_address,
153                                  size_t sender_address_len)
154 {
155   struct Plugin *plugin = cls;
156 #if DEBUG_DV_MESSAGES
157   char *my_id;
158   my_id = GNUNET_strdup(GNUNET_i2s(plugin->env->my_identity));
159   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
160                    "plugin_transport_dv",
161                    _("%s Received message from %s) of type %d, distance %u!\n"),
162                    my_id, GNUNET_i2s(sender), ntohs(((struct GNUNET_MessageHeader *)msg)->type), distance);
163   GNUNET_free_non_null(my_id);
164 #endif
165   plugin->env->receive(plugin->env->cls,
166                        sender,
167                        (struct GNUNET_MessageHeader *)msg,
168                        distance,
169                        NULL,
170                        sender_address,
171                        sender_address_len);
172
173 }
174
175
176 /* Question: how does the transport service learn of a newly connected (gossipped about)
177  * DV peer?  Should the plugin (here) create a HELLO for that peer and send it along,
178  * or should the DV service create a HELLO and send it to us via the other part?
179  */
180
181 /**
182  * Function that can be used by the transport service to transmit
183  * a message using the plugin.
184  *
185  * @param cls closure
186  * @param target who should receive this message
187  * @param priority how important is the message
188  * @param msgbuf the message to transmit
189  * @param msgbuf_size number of bytes in 'msgbuf'
190  * @param timeout when should we time out
191  * @param session the session used
192  * @param addr the address to use (can be NULL if the plugin
193  *                is "on its own" (i.e. re-use existing TCP connection))
194  * @param addrlen length of the address in bytes
195  * @param force_address GNUNET_YES if the plugin MUST use the given address,
196  *                otherwise the plugin may use other addresses or
197  *                existing connections (if available)
198  * @param cont continuation to call once the message has
199  *        been transmitted (or if the transport is ready
200  *        for the next transmission call; or if the
201  *        peer disconnected...)
202  * @param cont_cls closure for cont
203  * @return number of bytes used (on the physical network, with overheads);
204  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
205  *         and does NOT mean that the message was not transmitted (DV)
206  */
207 static ssize_t
208 dv_plugin_send (void *cls,
209                 const struct GNUNET_PeerIdentity *target,
210                 const char *msgbuf,
211                 size_t msgbuf_size,
212                 unsigned int priority,
213                 struct GNUNET_TIME_Relative timeout,
214                 struct Session *session,
215                 const void *addr,
216                 size_t addrlen,
217                 int force_address,
218                 GNUNET_TRANSPORT_TransmitContinuation
219                 cont, void *cont_cls)
220 {
221   int ret = 0;
222   struct Plugin *plugin = cls;
223
224   ret = GNUNET_DV_send(plugin->dv_handle,
225                        target,
226                        msgbuf,
227                        msgbuf_size,
228                        priority,
229                        timeout,
230                        addr,
231                        addrlen,
232                        cont,
233                        cont_cls);
234 #if DEBUG_DV
235   GNUNET_free_non_null(my_identity);
236 #endif
237
238   return ret;
239 }
240
241
242
243 /**
244  * Function that can be used to force the plugin to disconnect
245  * from the given peer and cancel all previous transmissions
246  * (and their continuations).
247  *
248  * @param cls closure
249  * @param target peer from which to disconnect
250  */
251 static void
252 dv_plugin_disconnect (void *cls,
253                       const struct GNUNET_PeerIdentity *target)
254 {
255   // struct Plugin *plugin = cls;
256   // TODO: Add message type to send to dv service to "disconnect" a peer
257 }
258
259
260 /**
261  * Convert the transports address to a nice, human-readable
262  * format.
263  *
264  * @param cls closure
265  * @param type name of the transport that generated the address
266  * @param addr one of the addresses of the host, NULL for the last address
267  *        the specific address format depends on the transport
268  * @param addrlen length of the address
269  * @param numeric should (IP) addresses be displayed in numeric form?
270  * @param timeout after how long should we give up?
271  * @param asc function to call on each string
272  * @param asc_cls closure for asc
273  */
274 static void
275 dv_plugin_address_pretty_printer (void *cls,
276                                   const char *type,
277                                   const void *addr,
278                                   size_t addrlen,
279                                   int numeric,
280                                   struct GNUNET_TIME_Relative timeout,
281                                   GNUNET_TRANSPORT_AddressStringCallback
282                                   asc, void *asc_cls)
283 {
284   char *dest_peer;
285   char *via_peer;
286   char *print_string;
287   char *addr_buf = (char *)addr;
288
289   if (addrlen != sizeof(struct GNUNET_PeerIdentity) * 2)
290     {
291       asc (asc_cls, NULL);
292     }
293   else
294     {
295       dest_peer = GNUNET_strdup(GNUNET_i2s((struct GNUNET_PeerIdentity *)addr));
296       via_peer = GNUNET_strdup(GNUNET_i2s((struct GNUNET_PeerIdentity *)&addr_buf[sizeof(struct GNUNET_PeerIdentity)]));
297       GNUNET_asprintf(&print_string, "DV Peer `%s' via peer`%s'", dest_peer, via_peer);
298       asc (asc_cls, print_string);
299       asc (asc_cls, NULL);
300       GNUNET_free(via_peer);
301       GNUNET_free(dest_peer);
302       GNUNET_free(print_string);
303     }
304 }
305
306 /**
307  * Convert the DV address to a pretty string.
308  *
309  * @param cls closure
310  * @param addr the (hopefully) DV address
311  * @param addrlen the length of the address
312  *
313  * @return string representing the DV address
314  */
315 static const char *address_to_string (void *cls,
316                                        const void *addr,
317                                        size_t addrlen)
318 {
319   static char return_buffer[2 * 4 + 2]; // Two four character peer identity prefixes a ':' and '\0'
320
321   struct GNUNET_CRYPTO_HashAsciiEncoded peer_hash;
322   struct GNUNET_CRYPTO_HashAsciiEncoded via_hash;
323   struct GNUNET_PeerIdentity *peer;
324   struct GNUNET_PeerIdentity *via;
325   char *addr_buf = (char *)addr;
326
327   if (addrlen == (2 * sizeof(struct GNUNET_PeerIdentity)))
328     {
329       peer = (struct GNUNET_PeerIdentity *)addr_buf;
330       via = (struct GNUNET_PeerIdentity *)&addr_buf[sizeof(struct GNUNET_PeerIdentity)];
331
332       GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &peer_hash);
333       peer_hash.encoding[4] = '\0';
334       GNUNET_CRYPTO_hash_to_enc (&via->hashPubKey, &via_hash);
335       via_hash.encoding[4] = '\0';
336       GNUNET_snprintf (return_buffer,
337                        sizeof (return_buffer),
338                        "%s:%s",
339                        &peer_hash,
340                        &via_hash);
341     }
342   else
343     return NULL;
344
345   return return_buffer;
346 }
347
348 /**
349  * Another peer has suggested an address for this
350  * peer and transport plugin.  Check that this could be a valid
351  * address.  If so, consider adding it to the list
352  * of addresses.
353  *
354  * @param cls closure
355  * @param addr pointer to the address
356  * @param addrlen length of addr
357  * @return GNUNET_OK if this is a plausible address for this peer
358  *         and transport
359  *
360  * FIXME: does this mean anything for the DV plugin?
361  */
362 static int
363 dv_plugin_address_suggested (void *cls,
364                                   void *addr, size_t addrlen)
365 {
366   /* struct Plugin *plugin = cls; */
367
368   /* check if the address is plausible; if so,
369      add it to our list! */
370   return GNUNET_NO;
371 }
372
373
374 /**
375  * Entry point for the plugin.
376  */
377 void *
378 libgnunet_plugin_transport_dv_init (void *cls)
379 {
380   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
381   struct GNUNET_TRANSPORT_PluginFunctions *api;
382   struct Plugin *plugin;
383
384   plugin = GNUNET_malloc (sizeof (struct Plugin));
385   plugin->env = env;
386   //plugin->service = service;
387   //plugin->server = GNUNET_SERVICE_get_server (service);
388
389   plugin->dv_handle = GNUNET_DV_connect(env->sched, env->cfg, &handle_dv_message_received, plugin);
390
391   if (plugin->dv_handle == NULL)
392   {
393     GNUNET_free(plugin);
394     return NULL;
395   }
396
397   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
398   api->cls = plugin;
399   api->send = &dv_plugin_send;
400   api->disconnect = &dv_plugin_disconnect;
401   api->address_pretty_printer = &dv_plugin_address_pretty_printer;
402   api->check_address = &dv_plugin_address_suggested;
403   api->address_to_string = &address_to_string;
404   return api;
405 }
406
407
408 /**
409  * Exit point from the plugin.
410  */
411 void *
412 libgnunet_plugin_transport_dv_done (void *cls)
413 {
414   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
415   struct Plugin *plugin = api->cls;
416
417   if (plugin->dv_handle != NULL)
418     GNUNET_DV_disconnect(plugin->dv_handle);
419
420   GNUNET_free (plugin);
421   GNUNET_free (api);
422   return NULL;
423 }
424
425 /* end of plugin_transport_template.c */