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 2, 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 transport/plugin_transport_template.c
23 * @brief template for a new transport service
24 * @author Christian Grothoff
28 #include "gnunet_protocols.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_transport_service.h"
34 #include "plugin_transport.h"
35 #include "microhttpd.h"
36 #include <curl/curl.h>
38 #define DEBUG_HTTP GNUNET_NO
41 * Text of the response sent back after the last bytes of a PUT
42 * request have been received (just to formally obey the HTTP
45 #define HTTP_PUT_RESPONSE "Thank you!"
49 * After how long do we expire an address that we
50 * learned from another peer if it is not reconfirmed
53 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
57 * Encapsulation of all of the state of the plugin.
63 * Session handle for connections.
69 * Stored in a linked list.
74 * Pointer to the global plugin struct.
76 struct Plugin *plugin;
79 * Continuation function to call once the transmission buffer
80 * has again space available. NULL if there is no
81 * continuation to call.
83 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
86 * Closure for transmit_cont.
88 void *transmit_cont_cls;
91 * To whom are we talking to (set to our identity
92 * if we are still waiting for the welcome message)
94 struct GNUNET_PeerIdentity sender;
102 * Sender's ip address to distinguish between incoming connections
107 * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
109 unsigned int is_client;
112 * At what time did we reset last_received last?
114 struct GNUNET_TIME_Absolute last_quota_update;
117 * How many bytes have we received since the "last_quota_update"
120 uint64_t last_received;
123 * Number of bytes per ms that this peer is allowed
131 * Encapsulation of all of the state of the plugin.
138 struct GNUNET_TRANSPORT_PluginEnvironment *env;
141 * List of open sessions.
143 struct Session *sessions;
148 * Daemon for listening for new IPv4 connections.
150 static struct MHD_Daemon *http_daemon_v4;
153 * Daemon for listening for new IPv6connections.
155 static struct MHD_Daemon *http_daemon_v6;
158 * Our primary task for http daemon handling IPv4 connections
160 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
163 * Our primary task for http daemon handling IPv6 connections
165 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
171 static struct Plugin *plugin;
176 static CURLM *multi_handle;
179 * session of current incoming connection
181 static struct Session * current_session;
183 static unsigned int locked;
186 * Finds a http session in our linked list using peer identity as a key
187 * @param peer peeridentity
188 * @return http session corresponding to peer identity
190 static struct Session * find_session_by_pi( const struct GNUNET_PeerIdentity *peer )
192 struct Session * cur;
193 GNUNET_HashCode hc_peer;
194 GNUNET_HashCode hc_current;
196 cur = plugin->sessions;
197 hc_peer = peer->hashPubKey;
200 hc_current = cur->sender.hashPubKey;
201 if ( 0 == GNUNET_CRYPTO_hash_cmp( &hc_peer, &hc_current))
203 cur = plugin->sessions->next;
210 * Finds a http session in our linked list using peer identity as a key
211 * @param peer peeridentity
212 * @return http session corresponding to peer identity
214 static struct Session * find_session_by_ip( struct sockaddr_in * addr )
217 struct Session * cur;
219 cur = plugin->sessions;
222 hc_current = cur->sender.hashPubKey;
223 if ( 0 == GNUNET_CRYPTO_hash_cmp( &hc_peer, &hc_current))
225 cur = plugin->sessions->next;
233 * Creates a http session in our linked list by peer identity
234 * Only peer is set here, all other fields have to be set by calling method
235 * @param peer peeridentity
236 * @return created http session
238 static struct Session * create_session_by_pi( const struct GNUNET_PeerIdentity *peer )
240 struct Session * cur;
241 struct Session * last_in_list;
242 /* Create a new session object */
243 cur = GNUNET_malloc (sizeof (struct Session));
244 memcpy( &(cur->sender), peer, sizeof( struct GNUNET_PeerIdentity ) );
248 /* Insert into linked list */
249 last_in_list = plugin->sessions;
250 while (last_in_list->next != NULL)
252 last_in_list = last_in_list->next;
254 last_in_list->next = cur;
261 * Creates a http session in our linked list by ip address
262 * Only ip is set here, all other fields have to be set by calling method
263 * @param peer peeridentity
264 * @return created http session
266 static struct Session * create_session_by_ip ( struct sockaddr_in * addr )
268 struct Session * cur;
269 struct Session * last_in_list;
270 /* Create a new session object */
271 cur = GNUNET_malloc (sizeof (struct Session));
272 // FIXME: memcpy( &(cur->ip), , sizeof( struct GNUNET_PeerIdentity ) );
276 /* Insert into linked list */
277 last_in_list = plugin->sessions;
278 while (last_in_list->next != NULL)
280 last_in_list = last_in_list->next;
282 last_in_list->next = cur;
289 * Callback called by MHD when a connection is terminated
291 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection was terminated\n");
294 /* clean up session here*/
299 * Check if we are allowed to connect to the given IP.
302 acceptPolicyCallback (void *cls,
303 const struct sockaddr *addr, socklen_t addr_len)
305 struct sockaddr_in *addrin;
306 struct sockaddr_in6 *addrin6;
307 char * address = NULL;
309 if ( GNUNET_YES == locked )
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming connections not accepted, rejecting connection\n");
315 /* something went wrong since last attempt to connect, lost session to free */
316 if ( NULL != current_session )
318 GNUNET_free ( current_session->ip );
319 GNUNET_free ( current_session );
324 if (addr->sa_family == AF_INET6)
326 address = GNUNET_malloc (INET6_ADDRSTRLEN);
327 addrin6 = (struct sockaddr_in6 *) addr;
328 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming IPv6 connection from `%s'\n",address);
331 else if (addr->sa_family == AF_INET)
333 address = GNUNET_malloc(INET_ADDRSTRLEN);
334 addrin = (struct sockaddr_in *) addr;
335 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming IPv4 connection from `%s'\n",address);
338 /* are there any socket types besides ipv4 and ipv6 we want to support? */
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unsupported connection type for incoming connection \n");
342 current_session = NULL;
347 /* create current session object */
348 current_session = GNUNET_malloc ( sizeof( struct Session) );
349 current_session->ip = address;
351 /* Every connection is accepted, nothing more to do here */
357 * Process GET or PUT request received via MHD. For
358 * GET, queue response that will send back our pending
359 * messages. For PUT, process incoming data and send
360 * to GNUnet core. In either case, check if a session
361 * already exists and create a new one if not.
364 accessHandlerCallback (void *cls,
365 struct MHD_Connection *session,
369 const char *upload_data,
370 size_t * upload_data_size, void **httpSessionCache)
372 struct Session * http_session;
373 struct MHD_Response *response;
374 http_session = *httpSessionCache;
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from `%s'\n",method, current_session->ip);
378 /* Check if new or already known session */
379 if ( NULL == http_session )
381 /* Create a new session */
383 /* Insert session into linked list*/
388 /* Since connection is established, we can unlock */
390 current_session = NULL;
392 /* Is it a PUT or a GET request */
393 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
395 /* PUT method here */
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %lu \n",(*upload_data_size));
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT Request: `%s'\n",upload_data);
399 /* FIXME: GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO); */
401 *upload_data_size = 0;
402 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
403 MHD_queue_response (session, MHD_HTTP_OK, response);
404 MHD_destroy_response (response);
406 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
409 //GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO);
417 * Call MHD to process pending requests and then go back
418 * and schedule the next run.
420 static void http_daemon_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
423 * Function that queries MHD's select sets and
424 * starts the task waiting for them.
426 static GNUNET_SCHEDULER_TaskIdentifier
427 http_daemon_prepare (struct MHD_Daemon *daemon_handle)
429 GNUNET_SCHEDULER_TaskIdentifier ret;
433 struct GNUNET_NETWORK_FDSet *wrs;
434 struct GNUNET_NETWORK_FDSet *wws;
435 struct GNUNET_NETWORK_FDSet *wes;
437 unsigned long long timeout;
439 struct GNUNET_TIME_Relative tv;
444 wrs = GNUNET_NETWORK_fdset_create ();
445 wes = GNUNET_NETWORK_fdset_create ();
446 wws = GNUNET_NETWORK_fdset_create ();
448 GNUNET_assert (MHD_YES ==
449 MHD_get_fdset (daemon_handle,
454 haveto = MHD_get_timeout (daemon_handle, &timeout);
455 if (haveto == MHD_YES)
456 tv.value = (uint64_t) timeout;
458 tv = GNUNET_TIME_UNIT_FOREVER_REL;
459 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
460 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
461 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
462 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
463 GNUNET_SCHEDULER_PRIORITY_HIGH,
464 GNUNET_SCHEDULER_NO_TASK,
470 GNUNET_NETWORK_fdset_destroy (wrs);
471 GNUNET_NETWORK_fdset_destroy (wws);
472 GNUNET_NETWORK_fdset_destroy (wes);
477 * Call MHD to process pending requests and then go back
478 * and schedule the next run.
481 http_daemon_run (void *cls,
482 const struct GNUNET_SCHEDULER_TaskContext *tc)
484 struct MHD_Daemon *daemon_handle = cls;
486 if (daemon_handle == http_daemon_v4)
487 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
489 if (daemon_handle == http_daemon_v6)
490 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
492 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
497 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
498 if (daemon_handle == http_daemon_v4)
499 http_task_v4 = http_daemon_prepare (daemon_handle);
500 if (daemon_handle == http_daemon_v6)
501 http_task_v6 = http_daemon_prepare (daemon_handle);
505 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
509 fprintf(stdout, "*** Read callback: size %u, size nmemb: %u \n", size, nmemb);
510 retcode = fread(ptr, size, nmemb, stream);
517 * Function that can be used by the transport service to transmit
518 * a message using the plugin.
521 * @param target who should receive this message
522 * @param priority how important is the message
523 * @param msgbuf the message to transmit
524 * @param msgbuf_size number of bytes in 'msgbuf'
525 * @param timeout when should we time out
526 * @param session which session must be used (or NULL for "any")
527 * @param addr the address to use (can be NULL if the plugin
528 * is "on its own" (i.e. re-use existing TCP connection))
529 * @param addrlen length of the address in bytes
530 * @param force_address GNUNET_YES if the plugin MUST use the given address,
531 * otherwise the plugin may use other addresses or
532 * existing connections (if available)
533 * @param cont continuation to call once the message has
534 * been transmitted (or if the transport is ready
535 * for the next transmission call; or if the
536 * peer disconnected...)
537 * @param cont_cls closure for cont
538 * @return number of bytes used (on the physical network, with overheads);
539 * -1 on hard errors (i.e. address invalid); 0 is a legal value
540 * and does NOT mean that the message was not transmitted (DV)
543 template_plugin_send (void *cls,
544 const struct GNUNET_PeerIdentity *
548 unsigned int priority,
549 struct GNUNET_TIME_Relative timeout,
550 struct Session *session,
554 GNUNET_TRANSPORT_TransmitContinuation
555 cont, void *cont_cls)
559 /* struct Plugin *plugin = cls; */
563 /* find session for peer */
564 ses = find_session_by_pi (target);
565 if ( ses == NULL) create_session_by_pi (target);
567 char *url = "http://localhost:12389";
569 curl_handle = curl_easy_init();
570 if( NULL == curl_handle)
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
575 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
576 curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_callback);
577 curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1L);
578 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
579 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
580 curl_easy_setopt(curl_handle, CURLOPT_READDATA, msgbuf);
581 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE,
582 (curl_off_t)msgbuf_size);
592 * Function that can be used to force the plugin to disconnect
593 * from the given peer and cancel all previous transmissions
594 * (and their continuationc).
597 * @param target peer from which to disconnect
600 template_plugin_disconnect (void *cls,
601 const struct GNUNET_PeerIdentity *target)
603 // struct Plugin *plugin = cls;
609 * Convert the transports address to a nice, human-readable
613 * @param type name of the transport that generated the address
614 * @param addr one of the addresses of the host, NULL for the last address
615 * the specific address format depends on the transport
616 * @param addrlen length of the address
617 * @param numeric should (IP) addresses be displayed in numeric form?
618 * @param timeout after how long should we give up?
619 * @param asc function to call on each string
620 * @param asc_cls closure for asc
623 template_plugin_address_pretty_printer (void *cls,
628 struct GNUNET_TIME_Relative timeout,
629 GNUNET_TRANSPORT_AddressStringCallback
638 * Another peer has suggested an address for this
639 * peer and transport plugin. Check that this could be a valid
640 * address. If so, consider adding it to the list
644 * @param addr pointer to the address
645 * @param addrlen length of addr
646 * @return GNUNET_OK if this is a plausible address for this peer
650 template_plugin_address_suggested (void *cls,
651 void *addr, size_t addrlen)
653 /* struct Plugin *plugin = cls; */
655 /* check if the address is plausible; if so,
656 add it to our list! */
662 * Function called for a quick conversion of the binary address to
663 * a numeric address. Note that the caller must not free the
664 * address and that the next call to this function is allowed
665 * to override the address again.
668 * @param addr binary address
669 * @param addrlen length of the address
670 * @return string representing the same address
673 template_plugin_address_to_string (void *cls,
682 * Exit point from the plugin.
685 libgnunet_plugin_transport_http_done (void *cls)
687 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
688 struct Plugin *plugin = api->cls;
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unloading http plugin...\n");
692 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
694 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
695 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
698 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
700 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
701 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
704 if (http_daemon_v4 != NULL)
706 MHD_stop_daemon (http_daemon_v4);
707 http_daemon_v4 = NULL;
709 if (http_daemon_v6 != NULL)
711 MHD_stop_daemon (http_daemon_v6);
712 http_daemon_v6 = NULL;
715 curl_multi_cleanup(multi_handle);
717 GNUNET_free (plugin);
724 * Entry point for the plugin.
727 libgnunet_plugin_transport_http_init (void *cls)
729 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
730 struct GNUNET_TRANSPORT_PluginFunctions *api;
732 long long unsigned int port;
734 plugin = GNUNET_malloc (sizeof (struct Plugin));
736 plugin->sessions = NULL;
737 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
739 api->send = &template_plugin_send;
740 api->disconnect = &template_plugin_disconnect;
741 api->address_pretty_printer = &template_plugin_address_pretty_printer;
742 api->check_address = &template_plugin_address_suggested;
743 api->address_to_string = &template_plugin_address_to_string;
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
746 /* Reading port number from config file */
748 GNUNET_CONFIGURATION_get_value_number (env->cfg,
754 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
757 ("Require valid port number for transport plugin `%s' in configuration!\n"),
759 libgnunet_plugin_transport_http_done (api);
763 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
765 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
767 &acceptPolicyCallback,
768 NULL , &accessHandlerCallback, NULL,
769 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
770 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
771 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
772 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
773 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
775 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
777 &acceptPolicyCallback,
778 NULL , &accessHandlerCallback, NULL,
779 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
780 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
781 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
782 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
783 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
788 if (http_daemon_v4 != NULL)
789 http_task_v4 = http_daemon_prepare (http_daemon_v4);
790 if (http_daemon_v6 != NULL)
791 http_task_v6 = http_daemon_prepare (http_daemon_v6);
793 if (http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
795 if (http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
798 /* Initializing cURL */
799 multi_handle = curl_multi_init();
804 /* end of plugin_transport_template.c */