-adding distance change notification from service to plugin
[oweals/gnunet.git] / src / dv / plugin_transport_dv.c
1 /*
2      This file is part of GNUnet
3      (C) 2002--2013 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.  FIXME: use hash map!
118    */
119   struct Session *sessions;
120
121   /**
122    * Copy of the handler array where the closures are
123    * set to this struct's instance.
124    */
125   struct GNUNET_SERVER_MessageHandler *handlers;
126
127   /**
128    * Handle to the DV service
129    */
130   struct GNUNET_DV_ServiceHandle *dvh;
131
132 };
133
134
135 /**
136  * Handler for messages received from the DV service.
137  *
138  * @param cls closure with the plugin
139  * @param sender sender of the message
140  * @param distance how far did the message travel
141  * @param msg actual message payload 
142  */
143 static void
144 handle_dv_message_received (void *cls,
145                             const struct GNUNET_PeerIdentity *sender,
146                             uint32_t distance,
147                             const struct GNUNET_MessageHeader *msg)
148 {
149   struct Plugin *plugin = cls;
150   struct GNUNET_ATS_Information ats;
151
152   ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
153   ats.value = htonl (distance);
154
155   plugin->env->receive (plugin->env->cls, sender,
156                         msg,
157                         &ats, 1,
158                         NULL, NULL, 0);
159 }
160
161
162 /* Question: how does the transport service learn of a newly connected (gossipped about)
163  * DV peer?  Should the plugin (here) create a HELLO for that peer and send it along,
164  * or should the DV service create a HELLO and send it to us via the other part?
165  */
166
167 /**
168  * Function that can be used by the transport service to transmit
169  * a message using the plugin.
170  *
171  * @param cls closure
172  * @param session the session used
173  * @param priority how important is the message
174  * @param msgbuf the message to transmit
175  * @param msgbuf_size number of bytes in 'msgbuf'
176  * @param timeout when should we time out
177  * @param cont continuation to call once the message has
178  *        been transmitted (or if the transport is ready
179  *        for the next transmission call; or if the
180  *        peer disconnected...)
181  * @param cont_cls closure for cont
182  * @return number of bytes used (on the physical network, with overheads);
183  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
184  *         and does NOT mean that the message was not transmitted (DV)
185  */
186 static ssize_t
187 dv_plugin_send (void *cls, 
188                 struct Session *session,
189                 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
190                 struct GNUNET_TIME_Relative timeout, 
191                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
192 {
193   int ret = -1;
194
195   return ret;
196 }
197
198
199
200 /**
201  * Function that can be used to force the plugin to disconnect
202  * from the given peer and cancel all previous transmissions
203  * (and their continuations).
204  *
205  * @param cls closure
206  * @param target peer from which to disconnect
207  */
208 static void
209 dv_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
210 {
211   // struct Plugin *plugin = cls;
212   // TODO: Add message type to send to dv service to "disconnect" a peer
213 }
214
215
216 /**
217  * Convert the transports address to a nice, human-readable
218  * format.
219  *
220  * @param cls closure
221  * @param type name of the transport that generated the address
222  * @param addr one of the addresses of the host, NULL for the last address
223  *        the specific address format depends on the transport
224  * @param addrlen length of the address
225  * @param numeric should (IP) addresses be displayed in numeric form?
226  * @param timeout after how long should we give up?
227  * @param asc function to call on each string
228  * @param asc_cls closure for asc
229  */
230 static void
231 dv_plugin_address_pretty_printer (void *cls, const char *type, const void *addr,
232                                   size_t addrlen, int numeric,
233                                   struct GNUNET_TIME_Relative timeout,
234                                   GNUNET_TRANSPORT_AddressStringCallback asc,
235                                   void *asc_cls)
236 {
237   asc (asc_cls, NULL);
238 }
239
240
241 /**
242  * Convert the DV address to a pretty string.
243  *
244  * @param cls closure
245  * @param addr the (hopefully) DV address
246  * @param addrlen the length of the address
247  *
248  * @return string representing the DV address
249  */
250 static const char *
251 address_to_string (void *cls, const void *addr, size_t addrlen)
252 {
253   return "<not implemented>";
254 }
255
256
257 /**
258  * Another peer has suggested an address for this peer and transport
259  * plugin.  Check that this could be a valid address.  This function
260  * is not expected to 'validate' the address in the sense of trying to
261  * connect to it but simply to see if the binary format is technically
262  * legal for establishing a connection to this peer (and make sure that
263  * the address really corresponds to our network connection/settings
264  * and not some potential man-in-the-middle).
265  *
266  * @param cls closure
267  * @param addr pointer to the address
268  * @param addrlen length of addr
269  * @return GNUNET_OK if this is a plausible address for this peer
270  *         and transport, GNUNET_SYSERR if not
271  *
272  */
273 static int
274 dv_plugin_check_address (void *cls, const void *addr, size_t addrlen)
275 {
276   return GNUNET_SYSERR;
277 }
278
279
280
281 /**
282  * Create a new session to transmit data to the target
283  * This session will used to send data to this peer and the plugin will
284  * notify us by calling the env->session_end function
285  *
286  * @param cls the plugin
287  * @param address the address
288  * @return the session if the address is valid, NULL otherwise
289  */
290 static struct Session * 
291 dv_get_session (void *cls,
292                 const struct GNUNET_HELLO_Address *address)
293 {
294   return NULL;
295 }
296
297
298 /**
299  * Entry point for the plugin.
300  */
301 void *
302 libgnunet_plugin_transport_dv_init (void *cls)
303 {
304   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
305   struct GNUNET_TRANSPORT_PluginFunctions *api;
306   struct Plugin *plugin;
307
308   plugin = GNUNET_malloc (sizeof (struct Plugin));
309   plugin->env = env;
310   plugin->dvh = GNUNET_DV_service_connect (env->cfg,
311                                            plugin,
312                                            NULL, NULL, NULL, /*FIXME! */
313                                            &handle_dv_message_received);
314   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
315   api->cls = plugin;
316   api->send = &dv_plugin_send;
317   api->disconnect = &dv_plugin_disconnect;
318   api->address_pretty_printer = &dv_plugin_address_pretty_printer;
319   api->check_address = &dv_plugin_check_address;
320   api->address_to_string = &address_to_string;
321   api->string_to_address = NULL; /* FIXME */
322   api->get_session = dv_get_session;
323   return api;
324 }
325
326
327 /**
328  * Exit point from the plugin.
329  */
330 void *
331 libgnunet_plugin_transport_dv_done (void *cls)
332 {
333   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
334   struct Plugin *plugin = api->cls;
335
336   GNUNET_DV_service_disconnect (plugin->dvh);
337   GNUNET_free (plugin);
338   GNUNET_free (api);
339   return NULL;
340 }
341
342 /* end of plugin_transport_dv.c */