2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file dv/plugin_transport_dv.c
23 * @brief DV transport service, takes incoming DV requests and deals with
25 * @author Nathan Evans
26 * @author Christian Grothoff
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"
40 #define DEBUG_TEMPLATE GNUNET_EXTRA_LOGGING
43 * Encapsulation of all of the state of the plugin.
49 * Session handle for connections.
55 * Stored in a linked list.
60 * Pointer to the global plugin struct.
62 struct Plugin *plugin;
65 * The client (used to identify this connection)
70 * Continuation function to call once the transmission buffer
71 * has again space available. NULL if there is no
72 * continuation to call.
74 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
77 * Closure for transmit_cont.
79 void *transmit_cont_cls;
82 * To whom are we talking to (set to our identity
83 * if we are still waiting for the welcome message)
85 struct GNUNET_PeerIdentity sender;
88 * At what time did we reset last_received last?
90 struct GNUNET_TIME_Absolute last_quota_update;
93 * How many bytes have we received since the "last_quota_update"
96 uint64_t last_received;
99 * Number of bytes per ms that this peer is allowed
107 * Encapsulation of all of the state of the plugin.
114 struct GNUNET_TRANSPORT_PluginEnvironment *env;
117 * List of open sessions.
119 struct Session *sessions;
124 //struct GNUNET_SERVER_Handle *server;
127 * Handle to the running service.
129 //struct GNUNET_SERVICE_Context *service;
132 * Copy of the handler array where the closures are
133 * set to this struct's instance.
135 struct GNUNET_SERVER_MessageHandler *handlers;
138 * Handle to the DV service
140 struct GNUNET_DV_Handle *dv_handle;
145 * Handler for messages received from the DV service.
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)
152 struct Plugin *plugin = cls;
154 #if DEBUG_DV_MESSAGES
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),
163 GNUNET_free_non_null (my_id);
165 struct GNUNET_ATS_Information ats[2];
167 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
168 ats[0].value = htonl (distance);
169 ats[1].type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
170 ats[1].value = htonl (0);
172 plugin->env->receive (plugin->env->cls, sender,
173 (struct GNUNET_MessageHeader *) msg,
174 (const struct GNUNET_ATS_Information *) &ats,
175 2, NULL, sender_address, sender_address_len);
180 /* Question: how does the transport service learn of a newly connected (gossipped about)
181 * DV peer? Should the plugin (here) create a HELLO for that peer and send it along,
182 * or should the DV service create a HELLO and send it to us via the other part?
186 * Function that can be used by the transport service to transmit
187 * a message using the plugin.
190 * @param target who should receive this message
191 * @param priority how important is the message
192 * @param msgbuf the message to transmit
193 * @param msgbuf_size number of bytes in 'msgbuf'
194 * @param timeout when should we time out
195 * @param session the session used
196 * @param addr the address to use (can be NULL if the plugin
197 * is "on its own" (i.e. re-use existing TCP connection))
198 * @param addrlen length of the address in bytes
199 * @param force_address GNUNET_YES if the plugin MUST use the given address,
200 * otherwise the plugin may use other addresses or
201 * existing connections (if available)
202 * @param cont continuation to call once the message has
203 * been transmitted (or if the transport is ready
204 * for the next transmission call; or if the
205 * peer disconnected...)
206 * @param cont_cls closure for cont
207 * @return number of bytes used (on the physical network, with overheads);
208 * -1 on hard errors (i.e. address invalid); 0 is a legal value
209 * and does NOT mean that the message was not transmitted (DV)
212 dv_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
213 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
214 struct GNUNET_TIME_Relative timeout, struct Session *session,
215 const void *addr, size_t addrlen, int force_address,
216 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
219 struct Plugin *plugin = cls;
222 GNUNET_DV_send (plugin->dv_handle, target, msgbuf, msgbuf_size, priority,
223 timeout, addr, addrlen, cont, cont_cls);
230 * Function that can be used to force the plugin to disconnect
231 * from the given peer and cancel all previous transmissions
232 * (and their continuations).
235 * @param target peer from which to disconnect
238 dv_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
240 // struct Plugin *plugin = cls;
241 // TODO: Add message type to send to dv service to "disconnect" a peer
246 * Convert the transports address to a nice, human-readable
250 * @param type name of the transport that generated the address
251 * @param addr one of the addresses of the host, NULL for the last address
252 * the specific address format depends on the transport
253 * @param addrlen length of the address
254 * @param numeric should (IP) addresses be displayed in numeric form?
255 * @param timeout after how long should we give up?
256 * @param asc function to call on each string
257 * @param asc_cls closure for asc
260 dv_plugin_address_pretty_printer (void *cls, const char *type, const void *addr,
261 size_t addrlen, int numeric,
262 struct GNUNET_TIME_Relative timeout,
263 GNUNET_TRANSPORT_AddressStringCallback asc,
269 char *addr_buf = (char *) addr;
271 if (addrlen != sizeof (struct GNUNET_PeerIdentity) * 2)
278 GNUNET_strdup (GNUNET_i2s ((struct GNUNET_PeerIdentity *) addr));
280 GNUNET_strdup (GNUNET_i2s
281 ((struct GNUNET_PeerIdentity *)
282 &addr_buf[sizeof (struct GNUNET_PeerIdentity)]));
283 GNUNET_asprintf (&print_string, "DV Peer `%s' via peer`%s'", dest_peer,
285 asc (asc_cls, print_string);
287 GNUNET_free (via_peer);
288 GNUNET_free (dest_peer);
289 GNUNET_free (print_string);
294 * Convert the DV address to a pretty string.
297 * @param addr the (hopefully) DV address
298 * @param addrlen the length of the address
300 * @return string representing the DV address
303 address_to_string (void *cls, const void *addr, size_t addrlen)
305 static char return_buffer[2 * 4 + 2]; // Two four character peer identity prefixes a ':' and '\0'
307 struct GNUNET_CRYPTO_HashAsciiEncoded peer_hash;
308 struct GNUNET_CRYPTO_HashAsciiEncoded via_hash;
309 struct GNUNET_PeerIdentity *peer;
310 struct GNUNET_PeerIdentity *via;
311 char *addr_buf = (char *) addr;
313 if (addrlen == (2 * sizeof (struct GNUNET_PeerIdentity)))
315 peer = (struct GNUNET_PeerIdentity *) addr_buf;
317 (struct GNUNET_PeerIdentity *)
318 &addr_buf[sizeof (struct GNUNET_PeerIdentity)];
320 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &peer_hash);
321 peer_hash.encoding[4] = '\0';
322 GNUNET_CRYPTO_hash_to_enc (&via->hashPubKey, &via_hash);
323 via_hash.encoding[4] = '\0';
324 GNUNET_snprintf (return_buffer, sizeof (return_buffer), "%s:%s", &peer_hash,
330 return return_buffer;
334 * Another peer has suggested an address for this peer and transport
335 * plugin. Check that this could be a valid address. This function
336 * is not expected to 'validate' the address in the sense of trying to
337 * connect to it but simply to see if the binary format is technically
338 * legal for establishing a connection to this peer (and make sure that
339 * the address really corresponds to our network connection/settings
340 * and not some potential man-in-the-middle).
343 * @param addr pointer to the address
344 * @param addrlen length of addr
345 * @return GNUNET_OK if this is a plausible address for this peer
346 * and transport, GNUNET_SYSERR if not
350 dv_plugin_check_address (void *cls, const void *addr, size_t addrlen)
352 struct Plugin *plugin = cls;
354 /* Verify that the first peer of this address matches our peer id! */
355 if ((addrlen != (2 * sizeof (struct GNUNET_PeerIdentity))) ||
357 memcmp (addr, plugin->env->my_identity,
358 sizeof (struct GNUNET_PeerIdentity))))
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "%s: Address not correct size or identity doesn't match ours!\n",
362 GNUNET_i2s (plugin->env->my_identity));
363 if (addrlen == (2 * sizeof (struct GNUNET_PeerIdentity)))
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer in address is %s\n",
368 return GNUNET_SYSERR;
375 * Entry point for the plugin.
378 libgnunet_plugin_transport_dv_init (void *cls)
380 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
381 struct GNUNET_TRANSPORT_PluginFunctions *api;
382 struct Plugin *plugin;
384 plugin = GNUNET_malloc (sizeof (struct Plugin));
388 GNUNET_DV_connect (env->cfg, &handle_dv_message_received, plugin);
390 if (plugin->dv_handle == NULL)
392 GNUNET_free (plugin);
396 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
398 api->send = &dv_plugin_send;
399 api->disconnect = &dv_plugin_disconnect;
400 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
401 api->check_address = &dv_plugin_check_address;
402 api->address_to_string = &address_to_string;
408 * Exit point from the plugin.
411 libgnunet_plugin_transport_dv_done (void *cls)
413 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
414 struct Plugin *plugin = api->cls;
416 if (plugin->dv_handle != NULL)
417 GNUNET_DV_disconnect (plugin->dv_handle);
419 GNUNET_free (plugin);
424 /* end of plugin_transport_template.c */