2 This file is part of GNUnet
3 (C) 2003, 2004, 2005, 2006, 2007, 2008 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_http.c
23 * @brief Implementation of the HTTP transport service
24 * @author Christian Grothoff
28 #include "gnunet_util.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_transport.h"
31 #include "gnunet_stats_service.h"
32 #include "gnunet_upnp_service.h"
34 #include <microhttpd.h>
35 #include <curl/curl.h>
38 #define DEBUG_HTTP GNUNET_NO
41 * Disable GET (for debugging only!). Must be GNUNET_YES
44 #define DO_GET GNUNET_YES
47 * After how much time of the core not being associated with a http
48 * connection anymore do we close it?
50 * Needs to be larger than SECONDS_INACTIVE_DROP in
53 #define HTTP_TIMEOUT (600 * GNUNET_CRON_SECONDS)
56 * How often do we re-issue GET requests?
58 #define HTTP_GET_REFRESH (5 * GNUNET_CRON_SECONDS)
61 * Default maximum size of the HTTP read and write buffer.
63 #define HTTP_BUF_SIZE (64 * 1024)
66 * Text of the response sent back after the last bytes of a PUT
67 * request have been received (just to formally obey the HTTP
70 #define HTTP_PUT_RESPONSE "Thank you!"
72 #define MY_TRANSPORT_NAME "HTTP"
76 * Client-side data per PUT request.
81 * This is a linked list.
83 struct HTTPPutData *next;
86 * Handle to our CURL request.
91 * Last time we made progress with the PUT.
93 GNUNET_CronTime last_activity;
96 * The message we are sending.
106 * Current position in msg.
111 * Are we done sending? Set to 1 after we
112 * completed sending and started to receive
113 * a response ("Thank you!") or once the
114 * timeout has been reached.
121 * Server-side data per PUT request.
126 * This is a linked list.
128 struct MHDPutData *next;
131 * MHD connection handle for this request.
133 struct MHD_Connection *session;
136 * Last time we received data on this PUT
139 GNUNET_CronTime last_activity;
142 * Read buffer for the header (from PUT)
144 char rbuff1[sizeof (GNUNET_MessageHeader)];
147 * The read buffer (used only receiving PUT data).
152 * Number of valid bytes in rbuff1
157 * Number of valid bytes in rbuff2
163 * Size of the rbuff2 buffer.
168 * Should we sent a response for this PUT yet?
173 * Have we sent a response for this PUT yet?
180 * Server-side data for a GET request.
186 * This is a linked list.
188 struct MHDGetData *next;
191 * MHD connection handle for this request.
193 struct MHD_Connection *session;
196 * GET session response handle
198 struct MHD_Response *get;
203 struct HTTPSession *httpsession;
206 * The write buffer (for sending GET response)
211 * What was the last time we were able to
212 * transmit data using the current get handle?
214 GNUNET_CronTime last_get_activity;
217 * Current write position in wbuff
222 * Number of valid bytes in wbuff (starting at woff)
227 * Size of the write buffer.
234 * Transport Session handle.
236 typedef struct HTTPSession
240 * GNUNET_TSession for this session.
242 GNUNET_TSession *tsession;
245 * To whom are we talking to.
247 GNUNET_PeerIdentity sender;
250 * number of users of this session
255 * Has this session been destroyed?
260 * Are we client or server? Determines which of the
261 * structs in the union below is being used for this
267 * Is MHD still using this session handle?
272 * Data maintained for the http client-server connection
273 * (depends on if we are client or server).
281 * Active PUT requests (linked list).
283 struct MHDPutData *puts;
287 * Active GET requests (linked list; most
288 * recent received GET is the head of the list).
290 struct MHDGetData *gets;
299 * Address of the other peer.
305 * Last time the GET was active.
307 GNUNET_CronTime last_get_activity;
310 * What was the last time we were able to
311 * transmit data using the current get handle?
313 GNUNET_CronTime last_get_initiated;
321 * Read buffer for the header (from GET).
323 char rbuff1[sizeof (GNUNET_MessageHeader)];
326 * The read buffer (used only receiving GET data).
331 * Number of valid bytes in rbuff1
336 * Number of valid bytes in rbuff2
341 * Current size of the read buffer rbuff2.
347 * URL of the get and put operations.
352 * Linked list of PUT operations.
354 struct HTTPPutData *puts;
362 /* *********** globals ************* */
364 static int stat_bytesReceived;
366 static int stat_bytesSent;
368 static int stat_bytesDropped;
370 static int stat_get_issued;
372 static int stat_get_received;
374 static int stat_put_issued;
376 static int stat_put_received;
378 static int stat_select_calls;
380 static int stat_send_calls;
382 static int stat_connect_calls;
384 static int stat_curl_send_callbacks;
386 static int stat_curl_receive_callbacks;
388 static int stat_mhd_access_callbacks;
390 static int stat_mhd_read_callbacks;
392 static int stat_mhd_close_callbacks;
394 static int stat_connect_calls;
397 * How many requests do we have currently pending
400 static unsigned int http_requests_pending;
402 static struct GNUNET_DISK_FileHandle signal_pipe[2];
407 * Daemon for listening for new connections.
409 static struct MHD_Daemon *mhd_daemon;
412 * Curl multi for managing client operations.
414 static CURLM *curl_multi;
417 * Set to GNUNET_YES while the transport is running.
419 static int http_running;
422 * Thread running libcurl activities.
424 static struct GNUNET_ThreadHandle *curl_thread;
427 * Array of currently active HTTP sessions.
429 static GNUNET_TSession **tsessions;
432 * Number of valid entries in tsessions.
434 static unsigned int tsessionCount;
437 * Sie of the tsessions array.
439 static unsigned int tsessionArrayLength;
442 * Lock for concurrent access to all structures used
443 * by http, including CURL.
445 static struct GNUNET_Mutex *lock;
449 * Signal select thread that its selector
450 * set may have changed.
456 GNUNET_DISK_file_write (signal_pipe[1], &c, sizeof (c));
460 * Check if we are allowed to connect to the given IP.
463 acceptPolicyCallback (void *cls,
464 const struct sockaddr *addr, socklen_t addr_len)
466 if (GNUNET_NO != is_rejected_tester (addr, addr_len))
472 * Disconnect from a remote node. May only be called
473 * on sessions that were acquired by the caller first.
474 * For the core, aquiration means to call associate or
475 * connect. The number of disconnects must match the
476 * number of calls to connect+associate.
478 * Sessions are actually discarded in cleanup_connections.
481 * @param tsession the session that is closed
482 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
485 httpDisconnect (GNUNET_TSession * tsession)
487 HTTPSession *httpsession = tsession->internal;
488 if (httpsession == NULL)
490 GNUNET_free (tsession);
493 GNUNET_mutex_lock (lock);
494 httpsession->users--;
495 GNUNET_mutex_unlock (lock);
500 destroy_tsession (GNUNET_TSession * tsession)
502 HTTPSession *httpsession = tsession->internal;
503 struct HTTPPutData *pos;
504 struct HTTPPutData *next;
506 struct MHDGetData *gpos;
507 struct MHDGetData *gnext;
509 struct MHD_Response *r;
512 GNUNET_mutex_lock (lock);
513 for (i = 0; i < tsessionCount; i++)
515 if (tsessions[i] == tsession)
517 tsessions[i] = tsessions[--tsessionCount];
521 if (httpsession->is_client)
524 curl_multi_remove_handle (curl_multi, httpsession->cs.client.get);
525 http_requests_pending--;
527 curl_easy_cleanup (httpsession->cs.client.get);
528 GNUNET_array_grow (httpsession->cs.client.rbuff2,
529 httpsession->cs.client.rsize2, 0);
531 GNUNET_free_non_null (httpsession->cs.client.url);
532 pos = httpsession->cs.client.puts;
536 curl_multi_remove_handle (curl_multi, pos->curl_put);
537 http_requests_pending--;
539 curl_easy_cleanup (pos->curl_put);
540 GNUNET_free (pos->msg);
544 GNUNET_free (httpsession);
545 GNUNET_free (tsession);
549 httpsession->destroyed = GNUNET_YES;
550 GNUNET_GE_BREAK (NULL, httpsession->cs.server.puts == NULL);
552 gpos = httpsession->cs.server.gets;
555 GNUNET_array_grow (gpos->wbuff, gpos->wsize, 0);
559 MHD_destroy_response (r);
562 httpsession->cs.server.gets = NULL;
564 GNUNET_free (httpsession->tsession);
565 GNUNET_free (httpsession);
567 GNUNET_mutex_unlock (lock);
571 * MHD is done handling a request. Cleanup
572 * the respective transport state.
575 requestCompletedCallback (void *unused,
576 struct MHD_Connection *session,
577 void **httpSessionCache)
579 HTTPSession *httpsession = *httpSessionCache;
580 struct MHDPutData *pprev;
581 struct MHDPutData *ppos;
583 struct MHDGetData *gprev;
584 struct MHDGetData *gpos;
588 stats->change (stat_mhd_close_callbacks, 1);
589 if (httpsession == NULL)
591 GNUNET_GE_ASSERT (NULL, !httpsession->is_client);
593 ppos = httpsession->cs.server.puts;
596 if (ppos->session == session)
598 ppos->last_activity = 0;
607 gpos = httpsession->cs.server.gets;
610 if (gpos->session == session)
612 gpos->last_get_activity = 0;
620 httpsession->is_mhd_active--;
624 * A (core) Session is to be associated with a transport session. The
625 * transport service may want to know in order to call back on the
626 * core if the connection is being closed. Associate can also be
627 * called to test if it would be possible to associate the session
628 * later, in this case the argument session is NULL. This can be used
629 * to test if the connection must be closed by the core or if the core
630 * can assume that it is going to be self-managed (if associate
631 * returns GNUNET_OK and session was NULL, the transport layer is responsible
632 * for eventually freeing resources associated with the tesession). If
633 * session is not NULL, the core takes responsbility for eventually
634 * calling disconnect.
636 * @param tsession the session handle passed along
637 * from the call to receive that was made by the transport
639 * @return GNUNET_OK if the session could be associated,
640 * GNUNET_SYSERR if not.
643 httpAssociate (GNUNET_TSession * tsession)
645 HTTPSession *httpSession;
647 if (tsession == NULL)
649 GNUNET_GE_BREAK (NULL, 0);
650 return GNUNET_SYSERR;
652 httpSession = tsession->internal;
653 GNUNET_mutex_lock (lock);
654 if (httpSession->destroyed == GNUNET_YES)
656 GNUNET_mutex_unlock (lock);
657 return GNUNET_SYSERR;
659 httpSession->users++;
660 GNUNET_mutex_unlock (lock);
665 * Add a new session to the array watched by the select thread. Grows
666 * the array if needed. If the caller wants to do anything useful
667 * with the return value, it must have the lock before
668 * calling. It is ok to call this function without holding lock if
669 * the return value is ignored.
672 addTSession (GNUNET_TSession * tsession)
676 GNUNET_mutex_lock (lock);
677 if (tsessionCount == tsessionArrayLength)
678 GNUNET_array_grow (tsessions, tsessionArrayLength,
679 tsessionArrayLength * 2);
681 tsessions[tsessionCount++] = tsession;
682 GNUNET_mutex_unlock (lock);
688 * Callback for processing GET requests if our side is the
691 * @param cls the HTTP session
692 * @param pos read-offset in the stream
693 * @param buf where to write the data
694 * @param max how much data to write (at most)
695 * @return number of bytes written, 0 is allowed!
698 contentReaderCallback (void *cls, uint64_t pos, char *buf, int max)
700 struct MHDGetData *mgd = cls;
703 stats->change (stat_mhd_read_callbacks, 1);
704 GNUNET_mutex_lock (lock);
707 memcpy (buf, &mgd->wbuff[mgd->woff], max);
711 mgd->last_get_activity = GNUNET_get_time ();
714 GNUNET_mutex_unlock (lock);
716 GNUNET_GE_LOG (coreAPI->ectx,
717 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
718 "HTTP returns %u bytes in MHD's GET handler.\n", max);
721 stats->change (stat_bytesSent, max);
722 if ((max == 0) && (mgd->httpsession->cs.server.gets != mgd))
723 return -1; /* end of response (another GET replaces this one) */
730 * Notification that libmicrohttpd no longer needs the
734 contentReaderFreeCallback (void *cls)
736 struct MHDGetData *mgd = cls;
738 GNUNET_GE_ASSERT (NULL, mgd->get == NULL);
739 GNUNET_array_grow (mgd->wbuff, mgd->wsize, 0);
745 * Process GET or PUT request received via MHD. For
746 * GET, queue response that will send back our pending
747 * messages. For PUT, process incoming data and send
748 * to GNUnet core. In either case, check if a session
749 * already exists and create a new one if not.
752 accessHandlerCallback (void *cls,
753 struct MHD_Connection *session,
757 const char *upload_data,
758 size_t * upload_data_size, void **httpSessionCache)
760 GNUNET_TSession *tsession;
761 struct MHDPutData *put;
762 struct MHDGetData *get;
763 HTTPSession *httpSession;
764 struct MHD_Response *response;
765 GNUNET_HashCode client;
768 GNUNET_MessageHeader *hdr;
769 GNUNET_TransportPacket *mp;
774 stats->change (stat_mhd_access_callbacks, 1);
776 GNUNET_GE_LOG (coreAPI->ectx,
777 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
778 "HTTP/MHD receives `%s' request.\n", method);
780 /* convert URL to sender peer id */
781 if ((strlen (url) < 2)
782 || (GNUNET_OK != GNUNET_enc_to_hash (&url[1], &client)))
784 /* invalid request */
785 /* GNUNET_GE_BREAK_OP (NULL, 0); -- this happens a lot, most likely
786 somebody scanning for MyDoom.X-opened backdoors */
790 /* check if we already have a session for this */
791 httpSession = *httpSessionCache;
792 if (httpSession == NULL)
794 /* new http connection */
797 if (0 == strcasecmp (MHD_HTTP_METHOD_PUT, method))
798 stats->change (stat_put_received, 1);
800 stats->change (stat_get_received, 1);
802 GNUNET_mutex_lock (lock);
803 for (i = 0; i < tsessionCount; i++)
805 tsession = tsessions[i];
806 httpSession = tsession->internal;
808 memcmp (&httpSession->sender, &client,
809 sizeof (GNUNET_HashCode)))
810 && (httpSession->is_client == GNUNET_NO))
815 GNUNET_mutex_unlock (lock);
817 /* create new session if necessary */
818 if (httpSession == NULL)
821 GNUNET_GE_LOG (coreAPI->ectx,
822 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
823 "HTTP/MHD creates new session for request from `%s'.\n",
826 httpSession = GNUNET_malloc (sizeof (HTTPSession));
827 memset (httpSession, 0, sizeof (HTTPSession));
828 httpSession->sender.hashPubKey = client;
829 httpSession->users = 0; /* MHD */
830 tsession = GNUNET_malloc (sizeof (GNUNET_TSession));
831 memset (tsession, 0, sizeof (GNUNET_TSession));
832 tsession->ttype = GNUNET_TRANSPORT_PROTOCOL_NUMBER_HTTP;
833 tsession->internal = httpSession;
834 tsession->peer.hashPubKey = client;
835 httpSession->tsession = tsession;
836 addTSession (tsession);
838 if (*httpSessionCache == NULL)
840 httpSession->is_mhd_active++;
841 *httpSessionCache = httpSession;
843 GNUNET_mutex_lock (lock);
845 if (0 == strcasecmp (MHD_HTTP_METHOD_GET, method))
848 GNUNET_GE_LOG (coreAPI->ectx,
849 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
850 "HTTP/MHD receives GET request from `%s'.\n", &url[1]);
853 /* handle get; create response object if we do not
855 get = GNUNET_malloc (sizeof (struct MHDGetData));
856 memset (get, 0, sizeof (struct MHDGetData));
857 get->next = httpSession->cs.server.gets;
858 httpSession->cs.server.gets = get;
859 get->session = session;
860 get->httpsession = httpSession;
861 get->last_get_activity = GNUNET_get_time ();
862 get->get = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
864 contentReaderCallback,
866 contentReaderFreeCallback);
867 MHD_queue_response (session, MHD_HTTP_OK, get->get);
868 GNUNET_mutex_unlock (lock);
872 if (0 == strcasecmp (MHD_HTTP_METHOD_PUT, method))
875 GNUNET_GE_LOG (coreAPI->ectx,
876 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
877 "HTTP/MHD receives PUT request from `%s' with %u bytes.\n",
878 &url[1], *upload_data_size);
880 put = httpSession->cs.server.puts;
881 while ((put != NULL) && (put->session != session))
885 put = GNUNET_malloc (sizeof (struct MHDPutData));
886 memset (put, 0, sizeof (struct MHDPutData));
887 put->next = httpSession->cs.server.puts;
888 httpSession->cs.server.puts = put;
889 put->session = session;
891 put->last_activity = GNUNET_get_time ();
893 /* handle put (upload_data!) */
895 have = *upload_data_size;
897 stats->change (stat_bytesReceived, have);
898 *upload_data_size = 0; /* we will always process everything */
899 if ((have == 0) && (put->done == GNUNET_NO)
900 && (put->ready == GNUNET_YES))
902 put->done = GNUNET_YES;
903 /* end of upload, send response! */
905 GNUNET_GE_LOG (coreAPI->ectx,
906 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
907 "HTTP/MHD queues dummy response to completed PUT request.\n");
910 MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
911 HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
912 MHD_queue_response (session, MHD_HTTP_OK, response);
913 MHD_destroy_response (response);
914 GNUNET_mutex_unlock (lock);
919 put->ready = GNUNET_NO;
920 if (put->rpos1 < sizeof (GNUNET_MessageHeader))
922 cpy = sizeof (GNUNET_MessageHeader) - put->rpos1;
925 memcpy (&put->rbuff1[put->rpos1], &upload_data[poff], cpy);
931 if (put->rpos1 < sizeof (GNUNET_MessageHeader))
933 hdr = (GNUNET_MessageHeader *) put->rbuff1;
934 GNUNET_array_grow (put->rbuff2,
937 sizeof (GNUNET_MessageHeader));
938 if (put->rpos2 < ntohs (hdr->size) - sizeof (GNUNET_MessageHeader))
941 ntohs (hdr->size) - sizeof (GNUNET_MessageHeader) -
945 memcpy (&put->rbuff2[put->rpos2], &upload_data[poff], cpy);
950 if (put->rpos2 < ntohs (hdr->size) - sizeof (GNUNET_MessageHeader))
952 mp = GNUNET_malloc (sizeof (GNUNET_TransportPacket));
953 mp->msg = put->rbuff2;
954 mp->sender = httpSession->sender;
955 mp->tsession = httpSession->tsession;
956 mp->size = ntohs (hdr->size) - sizeof (GNUNET_MessageHeader);
958 GNUNET_GE_LOG (coreAPI->ectx,
959 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
960 "HTTP/MHD passes %u bytes to core (received via PUT request).\n",
963 coreAPI->receive (mp);
968 put->ready = GNUNET_YES;
970 GNUNET_mutex_unlock (lock);
973 GNUNET_mutex_unlock (lock);
974 GNUNET_GE_BREAK_OP (NULL, 0); /* invalid request */
980 * Process downloaded bits (from GET via CURL).
983 receiveContentCallback (void *ptr, size_t size, size_t nmemb, void *ctx)
985 HTTPSession *httpSession = ctx;
986 const char *inbuf = ptr;
987 size_t have = size * nmemb;
990 GNUNET_MessageHeader *hdr;
991 GNUNET_TransportPacket *mp;
994 stats->change (stat_curl_receive_callbacks, 1);
995 httpSession->cs.client.last_get_activity = GNUNET_get_time ();
997 GNUNET_GE_LOG (coreAPI->ectx,
998 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
999 "HTTP/CURL receives %u bytes as response to GET.\n",
1004 if (httpSession->cs.client.rpos1 < sizeof (GNUNET_MessageHeader))
1006 cpy = sizeof (GNUNET_MessageHeader) - httpSession->cs.client.rpos1;
1009 memcpy (&httpSession->cs.
1010 client.rbuff1[httpSession->cs.client.rpos1], &inbuf[poff],
1012 httpSession->cs.client.rpos1 += cpy;
1015 httpSession->cs.client.rpos2 = 0;
1017 if (httpSession->cs.client.rpos1 < sizeof (GNUNET_MessageHeader))
1019 hdr = (GNUNET_MessageHeader *) httpSession->cs.client.rbuff1;
1020 GNUNET_array_grow (httpSession->cs.client.rbuff2,
1021 httpSession->cs.client.rsize2,
1022 ntohs (hdr->size) - sizeof (GNUNET_MessageHeader));
1023 if (httpSession->cs.client.rpos2 <
1024 ntohs (hdr->size) - sizeof (GNUNET_MessageHeader))
1027 ntohs (hdr->size) - sizeof (GNUNET_MessageHeader) -
1028 httpSession->cs.client.rpos2;
1031 memcpy (&httpSession->cs.
1032 client.rbuff2[httpSession->cs.client.rpos2], &inbuf[poff],
1036 httpSession->cs.client.rpos2 += cpy;
1038 if (httpSession->cs.client.rpos2 <
1039 ntohs (hdr->size) - sizeof (GNUNET_MessageHeader))
1041 mp = GNUNET_malloc (sizeof (GNUNET_TransportPacket));
1042 mp->msg = httpSession->cs.client.rbuff2;
1043 mp->sender = httpSession->sender;
1044 mp->tsession = httpSession->tsession;
1045 mp->size = ntohs (hdr->size) - sizeof (GNUNET_MessageHeader);
1046 coreAPI->receive (mp);
1047 httpSession->cs.client.rbuff2 = NULL;
1048 httpSession->cs.client.rpos2 = 0;
1049 httpSession->cs.client.rsize2 = 0;
1050 httpSession->cs.client.rpos1 = 0;
1053 stats->change (stat_bytesReceived, size * nmemb);
1054 return size * nmemb;
1059 * Provide bits for upload: we're using CURL for a PUT request
1060 * and now need to provide data from the message we are transmitting.
1063 sendContentCallback (void *ptr, size_t size, size_t nmemb, void *ctx)
1065 struct HTTPPutData *put = ctx;
1066 size_t max = size * nmemb;
1069 stats->change (stat_curl_send_callbacks, 1);
1070 put->last_activity = GNUNET_get_time ();
1071 if (max > put->size - put->pos)
1072 max = put->size - put->pos;
1073 memcpy (ptr, &put->msg[put->pos], max);
1076 GNUNET_GE_LOG (coreAPI->ectx,
1077 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
1078 "HTTP/CURL sends %u bytes in PUT request.\n", max);
1081 stats->change (stat_bytesSent, max);
1085 #define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt(c, a, b); if (ret != CURLE_OK) GNUNET_GE_LOG(coreAPI->ectx, GNUNET_GE_WARNING | GNUNET_GE_USER | GNUNET_GE_BULK, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret)); } while (0);
1086 #define IP_BUF_LEN 128
1089 create_session_url (HTTPSession * httpSession)
1091 char buf[IP_BUF_LEN];
1094 unsigned short available;
1097 const HostAddress *haddr =
1098 (const HostAddress *) &httpSession->cs.client.address;
1100 url = httpSession->cs.client.url;
1103 GNUNET_hash_to_enc (&coreAPI->my_identity->hashPubKey, &enc);
1104 available = ntohs (haddr->availability) & available_protocols;
1105 if (available == (VERSION_AVAILABLE_IPV4 | VERSION_AVAILABLE_IPV6))
1107 if (GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 2) == 0)
1108 available = VERSION_AVAILABLE_IPV4;
1110 available = VERSION_AVAILABLE_IPV6;
1112 if ((available & VERSION_AVAILABLE_IPV4) > 0)
1114 if (NULL == inet_ntop (AF_INET, &haddr->ipv4, buf, IP_BUF_LEN))
1122 else if ((available & VERSION_AVAILABLE_IPV6) > 0)
1124 if (NULL == inet_ntop (AF_INET6, &haddr->ipv6, buf, IP_BUF_LEN))
1134 url = GNUNET_malloc (64 + sizeof (GNUNET_EncName) + strlen (buf));
1135 GNUNET_snprintf (url,
1136 64 + sizeof (GNUNET_EncName),
1137 "http://%s%s%s:%u/%s", obr, buf, cbr,
1138 ntohs (haddr->port), &enc);
1139 httpSession->cs.client.url = url;
1145 * Try to do a GET on the other peer of the given
1148 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1151 create_curl_get (HTTPSession * httpSession)
1156 GNUNET_CronTime now;
1158 if (httpSession->cs.client.url == NULL)
1159 return GNUNET_SYSERR;
1160 curl_get = httpSession->cs.client.get;
1161 if (curl_get != NULL)
1163 GNUNET_mutex_lock (lock);
1164 curl_multi_remove_handle (curl_multi, curl_get);
1165 http_requests_pending--;
1167 curl_easy_cleanup (curl_get);
1168 GNUNET_mutex_unlock (lock);
1169 httpSession->cs.client.get = NULL;
1171 curl_get = curl_easy_init ();
1172 if (curl_get == NULL)
1173 return GNUNET_SYSERR;
1175 CURL_EASY_SETOPT (curl_get, CURLOPT_FAILONERROR, 1);
1176 CURL_EASY_SETOPT (curl_get, CURLOPT_URL, httpSession->cs.client.url);
1177 if (strlen (proxy) > 0)
1178 CURL_EASY_SETOPT (curl_get, CURLOPT_PROXY, proxy);
1179 CURL_EASY_SETOPT (curl_get, CURLOPT_BUFFERSIZE, 32 * 1024);
1180 if (0 == strncmp (httpSession->cs.client.url, "http", 4))
1181 CURL_EASY_SETOPT (curl_get, CURLOPT_USERAGENT, "GNUnet-http");
1183 CURL_EASY_SETOPT (curl_get, CURLOPT_VERBOSE, 1);
1185 CURL_EASY_SETOPT (curl_get, CURLOPT_CONNECTTIMEOUT, 150L);
1186 /* NOTE: use of CONNECTTIMEOUT without also
1187 setting NOSIGNAL results in really weird
1188 crashes on my system! */
1189 CURL_EASY_SETOPT (curl_get, CURLOPT_NOSIGNAL, 1);
1190 CURL_EASY_SETOPT (curl_get, CURLOPT_TIMEOUT, 150L);
1191 CURL_EASY_SETOPT (curl_get, CURLOPT_WRITEFUNCTION, &receiveContentCallback);
1192 CURL_EASY_SETOPT (curl_get, CURLOPT_WRITEDATA, httpSession);
1193 CURL_EASY_SETOPT (curl_get, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
1194 if (ret != CURLE_OK)
1196 curl_easy_cleanup (curl_get);
1197 return GNUNET_SYSERR;
1199 GNUNET_mutex_lock (lock);
1200 mret = curl_multi_add_handle (curl_multi, curl_get);
1201 http_requests_pending++;
1202 GNUNET_mutex_unlock (lock);
1204 stats->change (stat_get_issued, 1);
1205 if (mret != CURLM_OK)
1207 GNUNET_GE_LOG (coreAPI->ectx,
1208 GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
1209 GNUNET_GE_BULK, _("%s failed at %s:%d: `%s'\n"),
1210 "curl_multi_add_handle", __FILE__, __LINE__,
1211 curl_multi_strerror (mret));
1212 curl_easy_cleanup (curl_get);
1213 return GNUNET_SYSERR;
1216 now = GNUNET_get_time ();
1217 httpSession->cs.client.last_get_activity = now;
1218 httpSession->cs.client.get = curl_get;
1219 httpSession->cs.client.last_get_initiated = now;
1221 GNUNET_GE_LOG (coreAPI->ectx,
1222 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
1223 "HTTP/CURL initiated GET request.\n");
1230 * Establish a connection to a remote node.
1232 * @param hello the hello-Message for the target node
1233 * @param tsessionPtr the session handle that is set
1234 * @param may_reuse are we allowed to re-use an existing connection?
1235 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1238 httpConnect (const GNUNET_MessageHello * hello,
1239 GNUNET_TSession ** tsessionPtr, int may_reuse)
1241 const HostAddress *haddr = (const HostAddress *) &hello[1];
1242 GNUNET_TSession *tsession;
1243 HTTPSession *httpSession;
1247 stats->change (stat_connect_calls, 1);
1248 /* check if we have a session pending for this peer */
1252 GNUNET_mutex_lock (lock);
1253 for (i = 0; i < tsessionCount; i++)
1255 if (0 == memcmp (&hello->senderIdentity,
1256 &tsessions[i]->peer, sizeof (GNUNET_PeerIdentity)))
1258 tsession = tsessions[i];
1262 if ((tsession != NULL) && (GNUNET_OK == httpAssociate (tsession)))
1264 *tsessionPtr = tsession;
1265 GNUNET_mutex_unlock (lock);
1268 GNUNET_mutex_unlock (lock);
1270 /* no session pending, initiate a new one! */
1271 httpSession = GNUNET_malloc (sizeof (HTTPSession));
1272 memset (httpSession, 0, sizeof (HTTPSession));
1273 httpSession->sender = hello->senderIdentity;
1274 httpSession->users = 1; /* us only, core has not seen this tsession! */
1275 httpSession->is_client = GNUNET_YES;
1276 httpSession->cs.client.address = *haddr;
1277 tsession = GNUNET_malloc (sizeof (GNUNET_TSession));
1278 memset (tsession, 0, sizeof (GNUNET_TSession));
1279 httpSession->tsession = tsession;
1280 tsession->ttype = GNUNET_TRANSPORT_PROTOCOL_NUMBER_HTTP;
1281 tsession->internal = httpSession;
1282 tsession->peer = hello->senderIdentity;
1283 create_session_url (httpSession);
1285 if (GNUNET_OK != create_curl_get (httpSession))
1287 GNUNET_free (tsession);
1288 GNUNET_free (httpSession);
1289 return GNUNET_SYSERR;
1292 /* PUTs will be created as needed */
1293 addTSession (tsession);
1294 *tsessionPtr = tsession;
1296 GNUNET_GE_LOG (coreAPI->ectx,
1297 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
1298 "HTTP/CURL initiated connection to `%s'.\n",
1299 httpSession->cs.client.url);
1305 * We received the "Thank you!" response to a PUT.
1306 * Discard the data (not useful) and mark the PUT
1307 * operation as completed.
1310 discardContentCallback (void *data, size_t size, size_t nmemb, void *put_cls)
1312 struct HTTPPutData *put = put_cls;
1313 /* this condition should pretty much always be
1314 true; just checking here in case the PUT
1315 response comes early somehow */
1316 if (put->pos == put->size)
1317 put->done = GNUNET_YES;
1318 return size * nmemb;
1322 * Create a new PUT request for the given PUT data.
1325 create_curl_put (HTTPSession * httpSession, struct HTTPPutData *put)
1332 /* we should have initiated a GET earlier,
1333 so URL must not be NULL here */
1334 if (httpSession->cs.client.url == NULL)
1335 return GNUNET_SYSERR;
1336 curl_put = curl_easy_init ();
1337 if (curl_put == NULL)
1338 return GNUNET_SYSERR;
1339 CURL_EASY_SETOPT (curl_put, CURLOPT_FAILONERROR, 1);
1340 CURL_EASY_SETOPT (curl_put, CURLOPT_URL, httpSession->cs.client.url);
1341 if (strlen (proxy) > 0)
1342 CURL_EASY_SETOPT (curl_put, CURLOPT_PROXY, proxy);
1343 CURL_EASY_SETOPT (curl_put, CURLOPT_BUFFERSIZE, put->size);
1344 if (0 == strncmp (httpSession->cs.client.url, "http", 4))
1345 CURL_EASY_SETOPT (curl_put, CURLOPT_USERAGENT, "GNUnet-http");
1346 CURL_EASY_SETOPT (curl_put, CURLOPT_UPLOAD, 1);
1348 CURL_EASY_SETOPT (curl_put, CURLOPT_VERBOSE, 1);
1350 CURL_EASY_SETOPT (curl_put, CURLOPT_CONNECTTIMEOUT, 150L);
1351 /* NOTE: use of CONNECTTIMEOUT without also
1352 setting NOSIGNAL results in really weird
1353 crashes on my system! */
1354 CURL_EASY_SETOPT (curl_put, CURLOPT_NOSIGNAL, 1);
1355 CURL_EASY_SETOPT (curl_put, CURLOPT_TIMEOUT, 150L);
1357 CURL_EASY_SETOPT (curl_put, CURLOPT_INFILESIZE, size);
1358 CURL_EASY_SETOPT (curl_put, CURLOPT_READFUNCTION, &sendContentCallback);
1359 CURL_EASY_SETOPT (curl_put, CURLOPT_READDATA, put);
1360 CURL_EASY_SETOPT (curl_put, CURLOPT_WRITEFUNCTION, &discardContentCallback);
1361 CURL_EASY_SETOPT (curl_put, CURLOPT_WRITEDATA, put);
1362 CURL_EASY_SETOPT (curl_put, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
1363 if (ret != CURLE_OK)
1365 curl_easy_cleanup (curl_put);
1366 return GNUNET_SYSERR;
1368 GNUNET_mutex_lock (lock);
1369 mret = curl_multi_add_handle (curl_multi, curl_put);
1370 http_requests_pending++;
1371 GNUNET_mutex_unlock (lock);
1373 stats->change (stat_put_issued, 1);
1374 if (mret != CURLM_OK)
1376 GNUNET_GE_LOG (coreAPI->ectx,
1377 GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
1378 GNUNET_GE_BULK, _("%s failed at %s:%d: `%s'\n"),
1379 "curl_multi_add_handle", __FILE__, __LINE__,
1380 curl_multi_strerror (mret));
1381 return GNUNET_SYSERR;
1384 put->curl_put = curl_put;
1386 GNUNET_GE_LOG (coreAPI->ectx,
1387 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
1388 "HTTP/CURL initiated PUT request to `%s'.\n",
1389 httpSession->cs.client.url);
1396 * Test if the transport would even try to send
1397 * a message of the given size and importance
1398 * for the given session.<br>
1399 * This function is used to check if the core should
1400 * even bother to construct (and encrypt) this kind
1403 * @return GNUNET_YES if the transport would try (i.e. queue
1404 * the message or call the OS to send),
1405 * GNUNET_NO if the transport would just drop the message,
1406 * GNUNET_SYSERR if the size/session is invalid
1409 httpTestWouldTry (GNUNET_TSession * tsession, const unsigned int size,
1412 HTTPSession *httpSession = tsession->internal;
1413 struct MHDGetData *get;
1416 if (size >= GNUNET_MAX_BUFFER_SIZE - sizeof (GNUNET_MessageHeader))
1418 GNUNET_GE_BREAK (coreAPI->ectx, 0);
1419 return GNUNET_SYSERR;
1423 GNUNET_GE_BREAK (coreAPI->ectx, 0);
1424 return GNUNET_SYSERR;
1426 if (httpSession->is_client)
1429 if ((important != GNUNET_YES) && (httpSession->cs.client.puts != NULL))
1436 GNUNET_mutex_lock (lock);
1437 get = httpSession->cs.server.gets;
1442 if (get->wsize == 0)
1444 else if ((get->wpos + size > get->wsize)
1445 && (important != GNUNET_YES))
1450 GNUNET_mutex_unlock (lock);
1457 * Send a message to the specified remote node.
1459 * @param tsession the GNUNET_MessageHello identifying the remote node
1460 * @param msg the message
1461 * @param size the size of the message
1462 * @param important is this message so important that usual restrictions do not apply?
1463 * @return GNUNET_SYSERR on error, GNUNET_OK on success, GNUNET_NO if queue is full
1466 httpSend (GNUNET_TSession * tsession,
1467 const void *msg, unsigned int size, int important)
1469 HTTPSession *httpSession = tsession->internal;
1470 struct HTTPPutData *putData;
1471 GNUNET_MessageHeader *hdr;
1473 struct MHDGetData *getData;
1478 stats->change (stat_send_calls, 1);
1479 if (httpSession->is_client)
1481 /* we need to do a PUT (we are the client) */
1482 if (size >= GNUNET_MAX_BUFFER_SIZE)
1483 return GNUNET_SYSERR;
1486 GNUNET_GE_BREAK (NULL, 0);
1487 return GNUNET_SYSERR;
1489 if (important != GNUNET_YES)
1491 GNUNET_mutex_lock (lock);
1492 if (httpSession->cs.client.puts != NULL)
1494 /* do not queue more than one unimportant PUT at a time */
1495 signal_select (); /* do clean up now! */
1496 GNUNET_mutex_unlock (lock);
1498 stats->change (stat_bytesDropped, size);
1502 GNUNET_mutex_unlock (lock);
1504 putData = GNUNET_malloc (sizeof (struct HTTPPutData));
1505 memset (putData, 0, sizeof (struct HTTPPutData));
1506 putData->msg = GNUNET_malloc (size + sizeof (GNUNET_MessageHeader));
1507 hdr = (GNUNET_MessageHeader *) putData->msg;
1508 hdr->size = htons (size + sizeof (GNUNET_MessageHeader));
1509 hdr->type = htons (0);
1510 memcpy (&putData->msg[sizeof (GNUNET_MessageHeader)], msg, size);
1511 putData->size = size + sizeof (GNUNET_MessageHeader);
1512 putData->last_activity = GNUNET_get_time ();
1513 if (GNUNET_OK != create_curl_put (httpSession, putData))
1515 GNUNET_free (putData->msg);
1516 GNUNET_free (putData);
1517 return GNUNET_SYSERR;
1519 GNUNET_mutex_lock (lock);
1520 putData->next = httpSession->cs.client.puts;
1521 httpSession->cs.client.puts = putData;
1522 GNUNET_mutex_unlock (lock);
1526 /* httpSession->isClient == false, respond to a GET (we
1527 hopefully have one or will have one soon) */
1529 GNUNET_GE_LOG (coreAPI->ectx,
1530 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
1531 "HTTP/MHD queues %u bytes to be sent as response to GET as soon as possible.\n",
1535 GNUNET_mutex_lock (lock);
1536 getData = httpSession->cs.server.gets;
1537 if (getData == NULL)
1539 GNUNET_mutex_unlock (lock);
1540 return GNUNET_SYSERR;
1542 if (getData->wsize == 0)
1543 GNUNET_array_grow (getData->wbuff, getData->wsize, HTTP_BUF_SIZE);
1544 size += sizeof (GNUNET_MessageHeader);
1545 if (getData->wpos + size > getData->wsize)
1547 /* need to grow or discard */
1550 GNUNET_mutex_unlock (lock);
1553 tmp = GNUNET_malloc (getData->wpos + size);
1554 memcpy (tmp, &getData->wbuff[getData->woff], getData->wpos);
1555 hdr = (GNUNET_MessageHeader *) & tmp[getData->wpos];
1556 hdr->type = htons (0);
1557 hdr->size = htons (size);
1558 memcpy (&hdr[1], msg, size - sizeof (GNUNET_MessageHeader));
1559 GNUNET_free (getData->wbuff);
1560 getData->wbuff = tmp;
1561 getData->wsize = getData->wpos + size;
1563 getData->wpos = getData->wpos + size;
1567 /* fits without growing */
1568 if (getData->wpos + getData->woff + size > getData->wsize)
1570 /* need to compact first */
1571 memmove (getData->wbuff,
1572 &getData->wbuff[getData->woff], getData->wpos);
1577 (GNUNET_MessageHeader *) & getData->wbuff[getData->woff +
1579 hdr->size = htons (size);
1580 hdr->type = htons (0);
1581 memcpy (&hdr[1], msg, size - sizeof (GNUNET_MessageHeader));
1582 getData->wpos += size;
1585 GNUNET_mutex_unlock (lock);
1591 * Function called to cleanup dead connections
1592 * (completed PUTs, GETs that have timed out,
1593 * etc.). Also re-vives GETs that have timed out
1594 * if we are still interested in the connection.
1597 cleanup_connections ()
1601 struct HTTPPutData *prev;
1602 struct HTTPPutData *pos;
1603 struct MHDPutData *mpos;
1604 struct MHDPutData *mprev;
1606 struct MHD_Response *r;
1607 struct MHDGetData *gpos;
1608 struct MHDGetData *gnext;
1610 GNUNET_CronTime now;
1612 GNUNET_mutex_lock (lock);
1613 now = GNUNET_get_time ();
1614 for (i = 0; i < tsessionCount; i++)
1616 s = tsessions[i]->internal;
1619 if ((s->cs.client.puts == NULL) && (s->users == 0)
1621 && (s->cs.client.last_get_activity + HTTP_TIMEOUT < now)
1627 GNUNET_GE_LOG (coreAPI->ectx,
1628 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST |
1630 "HTTP transport destroys old (%llu ms) unused client session\n",
1631 now - s->cs.client.last_get_activity);
1634 destroy_tsession (tsessions[i]);
1640 pos = s->cs.client.puts;
1643 if (pos->last_activity + HTTP_TIMEOUT < now)
1644 pos->done = GNUNET_YES;
1648 s->cs.client.puts = pos->next;
1650 prev->next = pos->next;
1651 GNUNET_free (pos->msg);
1652 curl_multi_remove_handle (curl_multi, pos->curl_put);
1653 http_requests_pending--;
1655 curl_easy_cleanup (pos->curl_put);
1658 pos = s->cs.client.puts;
1667 if ((s->cs.client.last_get_activity + HTTP_TIMEOUT < now) &&
1668 ((s->users > 0) || (s->cs.client.puts != NULL)) &&
1669 ((s->cs.client.last_get_initiated + HTTP_GET_REFRESH > now) ||
1670 (s->cs.client.get == NULL)) &&
1671 ((s->cs.client.get == NULL) ||
1672 (s->cs.client.last_get_activity + HTTP_GET_REFRESH / 2 < now)))
1673 create_curl_get (s);
1678 mpos = s->cs.server.puts;
1680 while (mpos != NULL)
1682 if (mpos->last_activity == 0)
1685 s->cs.server.puts = mpos->next;
1687 mprev->next = mpos->next;
1688 GNUNET_array_grow (mpos->rbuff2, mpos->rsize2, 0);
1691 mpos = s->cs.server.puts;
1700 /* ! s->is_client */
1702 gpos = s->cs.server.gets;
1703 while (gpos != NULL)
1707 if ((gpos->last_get_activity + HTTP_TIMEOUT < now) ||
1708 (gpos != s->cs.server.gets))
1710 if (gpos == s->cs.server.gets)
1711 s->cs.server.gets = NULL;
1714 MHD_destroy_response (r);
1721 (s->cs.server.gets == NULL) &&
1723 (s->is_mhd_active == 0) && (s->users == 0))
1727 GNUNET_GE_LOG (coreAPI->ectx,
1728 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST |
1730 "HTTP transport destroys unused server session\n");
1733 destroy_tsession (tsessions[i]);
1739 GNUNET_mutex_unlock (lock);
1743 * Thread that runs the CURL and MHD requests.
1746 curl_runner (void *unused)
1752 struct GNUNET_NETWORK_FDSet *hrs;
1753 struct GNUNET_NETWORK_FDSet *hws;
1754 struct GNUNET_NETWORK_FDSet *hes;
1757 unsigned long long timeout;
1760 char buf[128]; /* for reading from pipe */
1764 GNUNET_GE_LOG (coreAPI->ectx,
1765 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
1766 "HTTP transport select thread started\n");
1769 hrs = GNUNET_net_fdset_create ();
1770 hws = GNUNET_net_fdset_create ();
1771 hes = GNUNET_net_fdset_create ();
1773 while (GNUNET_YES == http_running)
1779 GNUNET_mutex_lock (lock);
1780 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1781 GNUNET_mutex_unlock (lock);
1782 if (mret != CURLM_OK)
1784 GNUNET_GE_LOG (coreAPI->ectx,
1785 GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
1786 GNUNET_GE_BULK, _("%s failed at %s:%d: `%s'\n"),
1787 "curl_multi_fdset", __FILE__, __LINE__,
1788 curl_multi_strerror (mret));
1791 if (mhd_daemon != NULL)
1792 MHD_get_fdset (mhd_daemon, &rs, &ws, &es, &max);
1795 if (mhd_daemon != NULL)
1796 have_tv = MHD_get_timeout (mhd_daemon, &timeout);
1797 GNUNET_mutex_lock (lock);
1798 if ((CURLM_OK == curl_multi_timeout (curl_multi, &ms)) &&
1799 (ms != -1) && ((ms < timeout) || (have_tv == MHD_NO)))
1804 GNUNET_mutex_unlock (lock);
1806 GNUNET_net_fdset_zero (hws);
1807 GNUNET_net_fdset_zero (hrs);
1808 GNUNET_net_fdset_zero (hes);
1809 GNUNET_net_fdset_copy_native (hws, ws);
1810 GNUNET_net_fdset_copy_native (hrs, rs);
1811 GNUNET_net_fdset_copy_native (hes, es);
1813 GNUNET_net_fdset_handle_set (signal_pipe[0], hrs);
1815 stats->change (stat_select_calls, 1);
1817 GNUNET_net_select (hrs, hws, hes,
1819 MHD_YES) ? timeout :
1820 GNUNET_TIME_UNIT_FOREVER_REL);
1821 if (ret == GNUNET_SYSERR)
1823 GNUNET_GE_LOG_STRERROR (coreAPI->ectx,
1824 GNUNET_GE_ERROR | GNUNET_GE_ADMIN |
1825 GNUNET_GE_DEVELOPER, "select");
1827 if (GNUNET_YES != http_running)
1832 GNUNET_mutex_lock (lock);
1833 mret = curl_multi_perform (curl_multi, &running);
1834 GNUNET_mutex_unlock (lock);
1836 while ((mret == CURLM_CALL_MULTI_PERFORM)
1837 && (http_running == GNUNET_YES));
1838 if (GNUNET_net_fdset_handle_isset (signal_pipe[0], hrs))
1839 GNUNET_DISK_file_read (signal_pipe[0], buf, sizeof (buf));
1840 if ((mret != CURLM_OK) && (mret != CURLM_CALL_MULTI_PERFORM))
1841 GNUNET_GE_LOG (coreAPI->ectx,
1842 GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
1843 GNUNET_GE_BULK, _("%s failed at %s:%d: `%s'\n"),
1844 "curl_multi_perform", __FILE__, __LINE__,
1845 curl_multi_strerror (mret));
1846 if (mhd_daemon != NULL)
1847 MHD_run (mhd_daemon);
1848 cleanup_connections ();
1851 GNUNET_GE_LOG (coreAPI->ectx,
1852 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
1853 "HTTP transport select thread exits.\n");
1860 * Start the server process to receive inbound traffic.
1861 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1864 startTransportServer ()
1866 unsigned short port;
1868 if ((curl_multi != NULL) || (http_running == GNUNET_YES))
1869 return GNUNET_SYSERR;
1870 curl_multi = curl_multi_init ();
1871 if (curl_multi == NULL)
1872 return GNUNET_SYSERR;
1874 if ((mhd_daemon == NULL) && (port != 0))
1877 GNUNET_GC_get_configuration_value_yesno (cfg, "GNUNETD",
1881 mhd_daemon = MHD_start_daemon (MHD_USE_IPv6,
1883 &acceptPolicyCallback,
1884 NULL, &accessHandlerCallback, NULL,
1885 MHD_OPTION_CONNECTION_TIMEOUT,
1886 (unsigned int) HTTP_TIMEOUT,
1887 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1888 (unsigned int) 1024 * 128,
1889 MHD_OPTION_CONNECTION_LIMIT,
1891 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
1893 MHD_OPTION_NOTIFY_COMPLETED,
1894 &requestCompletedCallback, NULL,
1897 if (mhd_daemon == NULL)
1899 /* try without IPv6 */
1900 mhd_daemon = MHD_start_daemon (MHD_NO_FLAG,
1902 &acceptPolicyCallback,
1903 NULL, &accessHandlerCallback, NULL,
1904 MHD_OPTION_CONNECTION_TIMEOUT,
1905 (unsigned int) HTTP_TIMEOUT,
1906 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1907 (unsigned int) 1024 * 128,
1908 MHD_OPTION_CONNECTION_LIMIT,
1910 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
1912 MHD_OPTION_NOTIFY_COMPLETED,
1913 &requestCompletedCallback, NULL,
1918 available_protocols |= VERSION_AVAILABLE_IPV6;
1920 if (mhd_daemon != NULL)
1921 available_protocols |= VERSION_AVAILABLE_IPV4;
1926 available_protocols |= VERSION_AVAILABLE_IPV4;
1928 GNUNET_GC_get_configuration_value_yesno (cfg, "GNUNETD",
1931 available_protocols |= VERSION_AVAILABLE_IPV6;
1933 if (GNUNET_OK != GNUNET_DISK_pipe (signal_pipe, GNUNET_NO))
1935 MHD_stop_daemon (mhd_daemon);
1936 curl_multi_cleanup (curl_multi);
1939 return GNUNET_SYSERR;
1941 http_running = GNUNET_YES;
1942 curl_thread = GNUNET_thread_create (&curl_runner, NULL, 32 * 1024);
1943 if (curl_thread == NULL)
1944 GNUNET_GE_DIE_STRERROR (coreAPI->ectx,
1945 GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
1946 GNUNET_GE_IMMEDIATE, "pthread_create");
1951 * Shutdown the server process (stop receiving inbound
1952 * traffic). May be restarted later!
1955 stopTransportServer ()
1961 if ((http_running == GNUNET_NO) || (curl_multi == NULL))
1962 return GNUNET_SYSERR;
1963 http_running = GNUNET_NO;
1965 GNUNET_thread_stop_sleep (curl_thread);
1966 GNUNET_thread_join (curl_thread, &unused);
1967 GNUNET_DISK_close (signal_pipe[0]);
1968 GNUNET_DISK_close (signal_pipe[1]);
1969 if (mhd_daemon != NULL)
1971 MHD_stop_daemon (mhd_daemon);
1974 cleanup_connections ();
1975 for (i = 0; i < tsessionCount; i++)
1977 s = tsessions[i]->internal;
1980 destroy_tsession (tsessions[i]);
1984 curl_multi_cleanup (curl_multi);
1989 /* ******************** public API ******************** */
1992 * The exported method. Makes the core api available
1993 * via a global and returns the udp transport API.
1995 GNUNET_TransportAPI *
1996 inittransport_http (GNUNET_CoreAPIForTransport * core)
1998 GNUNET_GE_ASSERT (coreAPI->ectx, sizeof (HostAddress) == 24);
2001 lock = GNUNET_mutex_create (GNUNET_YES);
2002 if (0 != GNUNET_GC_attach_change_listener (coreAPI->cfg,
2003 &reload_configuration, NULL))
2005 GNUNET_mutex_destroy (lock);
2009 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2011 GNUNET_GE_BREAK (NULL, 0);
2012 GNUNET_GC_detach_change_listener (coreAPI->cfg, &reload_configuration,
2014 GNUNET_mutex_destroy (lock);
2019 tsessionArrayLength = 0;
2020 GNUNET_array_grow (tsessions, tsessionArrayLength, 32);
2021 if (GNUNET_GC_get_configuration_value_yesno (coreAPI->cfg,
2023 GNUNET_YES) == GNUNET_YES)
2025 upnp = coreAPI->service_request ("upnp");
2029 GNUNET_GE_LOG (coreAPI->ectx,
2030 GNUNET_GE_ERROR | GNUNET_GE_USER |
2031 GNUNET_GE_IMMEDIATE,
2033 ("The UPnP service could not be loaded. To disable UPnP, set the "
2034 "configuration option \"UPNP\" in section \"%s\" to \"NO\"\n"),
2038 stats = coreAPI->service_request ("stats");
2042 = stats->create (gettext_noop ("# bytes received via HTTP"));
2043 stat_bytesSent = stats->create (gettext_noop ("# bytes sent via HTTP"));
2045 = stats->create (gettext_noop ("# bytes dropped by HTTP (outgoing)"));
2046 stat_get_issued = stats->create (gettext_noop ("# HTTP GET issued"));
2048 = stats->create (gettext_noop ("# HTTP GET received"));
2049 stat_put_issued = stats->create (gettext_noop ("# HTTP PUT issued"));
2051 = stats->create (gettext_noop ("# HTTP PUT received"));
2053 = stats->create (gettext_noop ("# HTTP select calls"));
2055 stat_send_calls = stats->create (gettext_noop ("# HTTP send calls"));
2057 stat_curl_send_callbacks
2058 = stats->create (gettext_noop ("# HTTP curl send callbacks"));
2059 stat_curl_receive_callbacks
2060 = stats->create (gettext_noop ("# HTTP curl receive callbacks"));
2061 stat_mhd_access_callbacks
2062 = stats->create (gettext_noop ("# HTTP mhd access callbacks"));
2063 stat_mhd_read_callbacks
2064 = stats->create (gettext_noop ("# HTTP mhd read callbacks"));
2065 stat_mhd_close_callbacks
2066 = stats->create (gettext_noop ("# HTTP mhd close callbacks"));
2068 = stats->create (gettext_noop ("# HTTP connect calls"));
2070 GNUNET_GC_get_configuration_value_string (coreAPI->cfg,
2071 "GNUNETD", "HTTP-PROXY", "",
2074 myAPI.protocol_number = GNUNET_TRANSPORT_PROTOCOL_NUMBER_HTTP;
2076 myAPI.cost = 20000; /* about equal to udp */
2077 myAPI.hello_verify = &verify_hello;
2078 myAPI.hello_create = &create_hello;
2079 myAPI.connect = &httpConnect;
2080 myAPI.associate = &httpAssociate;
2081 myAPI.send = &httpSend;
2082 myAPI.disconnect = &httpDisconnect;
2083 myAPI.server_start = &startTransportServer;
2084 myAPI.server_stop = &stopTransportServer;
2085 myAPI.hello_to_address = &hello_to_address;
2086 myAPI.send_now_test = &httpTestWouldTry;
2092 donetransport_http ()
2094 curl_global_cleanup ();
2095 GNUNET_free_non_null (proxy);
2097 GNUNET_array_grow (tsessions, tsessionArrayLength, 0);