aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-09-14 16:46:38 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-09-14 16:46:38 +0000
commitf078bf6f9b737e52d3fb7f944be0d0b600686e64 (patch)
treef542e1eff8408c77ec13cea499314b769c8748a1 /src
parenta86d442e62e83ef0a9744ea32ad8e09251096675 (diff)
downloadgnunet-f078bf6f9b737e52d3fb7f944be0d0b600686e64.tar.gz
gnunet-f078bf6f9b737e52d3fb7f944be0d0b600686e64.zip
http plugin revisited
Diffstat (limited to 'src')
-rw-r--r--src/transport/plugin_transport_http.h245
-rw-r--r--src/transport/plugin_transport_http_client.c236
-rw-r--r--src/transport/plugin_transport_http_new.c963
-rw-r--r--src/transport/plugin_transport_http_server.c374
4 files changed, 1818 insertions, 0 deletions
diff --git a/src/transport/plugin_transport_http.h b/src/transport/plugin_transport_http.h
new file mode 100644
index 000000000..84af33a59
--- /dev/null
+++ b/src/transport/plugin_transport_http.h
@@ -0,0 +1,245 @@
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 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 transport/plugin_transport_http.h
23 * @brief http transport service plugin
24 * @author Matthias Wachs
25 */
26
27#include "platform.h"
28#include "gnunet_common.h"
29#include "gnunet_constants.h"
30#include "gnunet_protocols.h"
31#include "gnunet_connection_lib.h"
32#include "gnunet_service_lib.h"
33#include "gnunet_statistics_service.h"
34#include "gnunet_transport_service.h"
35#include "gnunet_resolver_service.h"
36#include "gnunet_server_lib.h"
37#include "gnunet_container_lib.h"
38#include "gnunet_transport_plugin.h"
39#include "gnunet_os_lib.h"
40#include "gnunet_nat_lib.h"
41#include "microhttpd.h"
42#include <curl/curl.h>
43
44
45#define DEBUG_HTTP GNUNET_YES
46#define VERBOSE_SERVER GNUNET_YES
47#define VERBOSE_CLIENT GNUNET_YES
48
49#if BUILD_HTTPS
50#define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
51#define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
52#else
53#define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
54#define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
55#endif
56
57
58#define HTTP_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
59
60/**
61 * Encapsulation of all of the state of the plugin.
62 */
63struct Plugin
64{
65 /**
66 * Our environment.
67 */
68 struct GNUNET_TRANSPORT_PluginEnvironment *env;
69
70 /**
71 * List of open sessions.
72 */
73 struct Session *head;
74
75 struct Session *tail;
76
77 /**
78 * NAT handle & address management
79 */
80 struct GNUNET_NAT_Handle *nat;
81
82 /**
83 * ipv4 DLL head
84 */
85 struct IPv4HttpAddressWrapper *ipv4_addr_head;
86
87 /**
88 * ipv4 DLL tail
89 */
90 struct IPv4HttpAddressWrapper *ipv4_addr_tail;
91
92 /**
93 * ipv6 DLL head
94 */
95 struct IPv6HttpAddressWrapper *ipv6_addr_head;
96
97 /**
98 * ipv6 DLL tail
99 */
100 struct IPv6HttpAddressWrapper *ipv6_addr_tail;
101
102
103 /* Plugin configuration */
104
105 char *name;
106
107 char *protocol;
108
109 int ipv4;
110
111 int ipv6;
112
113 uint16_t port;
114
115 int max_connections;
116
117 /*
118 * Server handles
119 */
120
121 struct MHD_Daemon *server_v4;
122
123 struct MHD_Daemon *server_v6;
124
125 char *crypto_init;
126 char *key;
127 char *cert;
128
129 /*
130 * Client handles
131 */
132
133 /**
134 * cURL Multihandle
135 */
136 CURLM *client_mh;
137
138};
139
140/**
141 * Session handle for connections.
142 */
143struct Session
144{
145
146 /**
147 * Stored in a linked list.
148 */
149 struct Session *next;
150
151 /**
152 * Stored in a linked list.
153 */
154 struct Session *prev;
155
156 /**
157 * Pointer to the global plugin struct.
158 */
159 struct Plugin *plugin;
160
161 /**
162 * The client (used to identify this connection)
163 */
164 /* void *client; */
165
166 /**
167 * Continuation function to call once the transmission buffer
168 * has again space available. NULL if there is no
169 * continuation to call.
170 */
171 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
172
173
174 void *addr;
175
176 size_t addrlen;
177
178 /**
179 * Closure for transmit_cont.
180 */
181 void *transmit_cont_cls;
182
183 /**
184 * To whom are we talking to (set to our identity
185 * if we are still waiting for the welcome message)
186 */
187 struct GNUNET_PeerIdentity target;
188
189 /**
190 * At what time did we reset last_received last?
191 */
192 //struct GNUNET_TIME_Absolute last_quota_update;
193
194 /**
195 * How many bytes have we received since the "last_quota_update"
196 * timestamp?
197 */
198 //uint64_t last_received;
199
200 /**
201 * Number of bytes per ms that this peer is allowed
202 * to send to us.
203 */
204 //uint32_t quota;
205
206
207 int inbound;
208
209 void *client_put;
210 void *client_get;
211
212
213};
214
215const char *
216http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
217
218int
219client_disconnect (struct Session *s);
220
221int
222client_connect (struct Session *s);
223
224int
225client_send (struct Session *s, const char *msgbuf, size_t msgbuf_size);
226
227int
228client_start (struct Plugin *plugin);
229
230void
231client_stop (struct Plugin *plugin);
232
233int
234server_disconnect (struct Session *s);
235
236int
237server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size);
238
239int
240server_start (struct Plugin *plugin);
241
242void
243server_stop (struct Plugin *plugin);
244
245/* end of plugin_transport_http.h */
diff --git a/src/transport/plugin_transport_http_client.c b/src/transport/plugin_transport_http_client.c
new file mode 100644
index 000000000..9a5d4b261
--- /dev/null
+++ b/src/transport/plugin_transport_http_client.c
@@ -0,0 +1,236 @@
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 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 transport/plugin_transport_http_client.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
25 */
26
27#include "plugin_transport_http.h"
28
29#if VERBOSE_CLIENT
30/**
31 * Function to log curl debug messages with GNUNET_log
32 * @param curl handle
33 * @param type curl_infotype
34 * @param data data
35 * @param size size
36 * @param cls closure
37 * @return 0
38 */
39static int
40client_log (CURL * curl, curl_infotype type, char *data, size_t size, void *cls)
41{
42 if (type == CURLINFO_TEXT)
43 {
44 char text[size + 2];
45
46 memcpy (text, data, size);
47 if (text[size - 1] == '\n')
48 text[size] = '\0';
49 else
50 {
51 text[size] = '\n';
52 text[size + 1] = '\0';
53 }
54 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client: %X - %s", cls, text);
55 }
56 return 0;
57}
58#endif
59
60int
61client_disconnect (struct Session *s)
62{
63 int res = GNUNET_OK;
64 CURLMcode mret;
65
66#if DEBUG_HTTP
67 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
68 "Deleting outbound session peer `%s'\n",
69 GNUNET_i2s (&s->target));
70#endif
71
72 mret = curl_multi_remove_handle (s->plugin->client_mh, s->client_put);
73 if (mret != CURLM_OK)
74 {
75 curl_easy_cleanup (s->client_put);
76 res = GNUNET_SYSERR;
77 GNUNET_break (0);
78 }
79 curl_easy_cleanup (s->client_put);
80
81 mret = curl_multi_remove_handle (s->plugin->client_mh, s->client_get);
82 if (mret != CURLM_OK)
83 {
84 curl_easy_cleanup (s->client_get);
85 res = GNUNET_SYSERR;
86 GNUNET_break (0);
87 }
88 curl_easy_cleanup (s->client_get);
89
90 return res;
91}
92
93int
94client_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
95{
96 return GNUNET_OK;
97}
98
99int
100client_connect (struct Session *s)
101{
102 int res = GNUNET_OK;
103 char *url;
104 CURLMcode mret;
105
106#if DEBUG_HTTP
107 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
108 "Initiating outbound session peer `%s'\n",
109 GNUNET_i2s (&s->target));
110#endif
111
112 s->inbound = GNUNET_NO;
113
114 /* create url */
115 GNUNET_asprintf (&url, "%s://%s/", s->plugin->protocol,
116 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
117
118#if DEBUG_HTTP
119 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name, "URL `%s'\n", url);
120#endif
121
122 /* create get connection */
123 s->client_get = curl_easy_init ();
124#if VERBOSE_CLIENT
125 curl_easy_setopt (s->client_get, CURLOPT_VERBOSE, 1L);
126 curl_easy_setopt (s->client_get, CURLOPT_DEBUGFUNCTION, &client_log);
127 curl_easy_setopt (s->client_get, CURLOPT_DEBUGDATA, s->client_get);
128#endif
129#if BUILD_HTTPS
130 curl_easy_setopt (s->client_get, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
131 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYPEER, 0);
132 curl_easy_setopt (s->client_get, CURLOPT_SSL_VERIFYHOST, 0);
133#endif
134 curl_easy_setopt (s->client_get, CURLOPT_URL, url);
135 //curl_easy_setopt (s->client_get, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
136 //curl_easy_setopt (s->client_get, CURLOPT_WRITEHEADER, ps);
137 //curl_easy_setopt (s->client_get, CURLOPT_READFUNCTION, curl_send_cb);
138 //curl_easy_setopt (s->client_get, CURLOPT_READDATA, ps);
139 //curl_easy_setopt (s->client_get, CURLOPT_WRITEFUNCTION, curl_receive_cb);
140 //curl_easy_setopt (s->client_get, CURLOPT_WRITEDATA, ps);
141 curl_easy_setopt (s->client_get, CURLOPT_TIMEOUT,
142 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
143 //curl_easy_setopt (s->client_get, CURLOPT_PRIVATE, ps);
144 curl_easy_setopt (s->client_get, CURLOPT_CONNECTTIMEOUT,
145 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
146 curl_easy_setopt (s->client_get, CURLOPT_BUFFERSIZE,
147 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
148#if CURL_TCP_NODELAY
149 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
150#endif
151
152 /* create put connection */
153 s->client_put = curl_easy_init ();
154#if VERBOSE_CLIENT
155 curl_easy_setopt (s->client_put, CURLOPT_VERBOSE, 1L);
156 curl_easy_setopt (s->client_put, CURLOPT_DEBUGFUNCTION, &client_log);
157 curl_easy_setopt (s->client_put, CURLOPT_DEBUGDATA, s->client_put);
158#endif
159#if BUILD_HTTPS
160 curl_easy_setopt (s->client_put, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
161 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYPEER, 0);
162 curl_easy_setopt (s->client_put, CURLOPT_SSL_VERIFYHOST, 0);
163#endif
164 curl_easy_setopt (s->client_put, CURLOPT_URL, url);
165 curl_easy_setopt (s->client_put, CURLOPT_PUT, 1L);
166 //curl_easy_setopt (s->client_put, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
167 //curl_easy_setopt (s->client_put, CURLOPT_WRITEHEADER, ps);
168 //curl_easy_setopt (s->client_put, CURLOPT_READFUNCTION, curl_send_cb);
169 //curl_easy_setopt (s->client_put, CURLOPT_READDATA, ps);
170 //curl_easy_setopt (s->client_put, CURLOPT_WRITEFUNCTION, curl_receive_cb);
171 //curl_easy_setopt (s->client_put, CURLOPT_WRITEDATA, ps);
172 curl_easy_setopt (s->client_put, CURLOPT_TIMEOUT,
173 (long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
174 //curl_easy_setopt (s->client_put, CURLOPT_PRIVATE, ps);
175 curl_easy_setopt (s->client_put, CURLOPT_CONNECTTIMEOUT,
176 (long) HTTP_NOT_VALIDATED_TIMEOUT.rel_value);
177 curl_easy_setopt (s->client_put, CURLOPT_BUFFERSIZE,
178 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
179#if CURL_TCP_NODELAY
180 curl_easy_setopt (s->client_put, CURLOPT_TCP_NODELAY, 1);
181#endif
182
183 GNUNET_free (url);
184
185 mret = curl_multi_add_handle (s->plugin->client_mh, s->client_get);
186 if (mret != CURLM_OK)
187 {
188 curl_easy_cleanup (s->client_get);
189 res = GNUNET_SYSERR;
190 GNUNET_break (0);
191 }
192
193 mret = curl_multi_add_handle (s->plugin->client_mh, s->client_put);
194 if (mret != CURLM_OK)
195 {
196 curl_multi_remove_handle (s->plugin->client_mh, s->client_get);
197 curl_easy_cleanup (s->client_get);
198 curl_easy_cleanup (s->client_put);
199 res = GNUNET_SYSERR;
200 GNUNET_break (0);
201 }
202
203 /* Perform connect */
204
205 return res;
206}
207
208int
209client_start (struct Plugin *plugin)
210{
211 int res = GNUNET_OK;
212
213 curl_global_init (CURL_GLOBAL_ALL);
214 plugin->client_mh = curl_multi_init ();
215
216 if (NULL == plugin->client_mh)
217 {
218 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
219 _
220 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
221 plugin->name);
222 res = GNUNET_SYSERR;
223 }
224 return res;
225}
226
227void
228client_stop (struct Plugin *plugin)
229{
230 curl_multi_cleanup (plugin->client_mh);
231 curl_global_cleanup ();
232}
233
234
235
236/* end of plugin_transport_http_client.c */
diff --git a/src/transport/plugin_transport_http_new.c b/src/transport/plugin_transport_http_new.c
new file mode 100644
index 000000000..3ad7dbac4
--- /dev/null
+++ b/src/transport/plugin_transport_http_new.c
@@ -0,0 +1,963 @@
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 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 transport/plugin_transport_http.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
25 */
26
27#include "plugin_transport_http.h"
28
29/**
30 * After how long do we expire an address that we
31 * learned from another peer if it is not reconfirmed
32 * by anyone?
33 */
34#define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
35
36/**
37 * Network format for IPv4 addresses.
38 */
39struct IPv4HttpAddress
40{
41 /**
42 * IPv4 address, in network byte order.
43 */
44 uint32_t ipv4_addr GNUNET_PACKED;
45
46 /**
47 * Port number, in network byte order.
48 */
49 uint16_t port GNUNET_PACKED;
50};
51
52/**
53 * Wrapper to manage IPv4 addresses
54 */
55struct IPv4HttpAddressWrapper
56{
57 /**
58 * Linked list next
59 */
60 struct IPv4HttpAddressWrapper *next;
61
62 /**
63 * Linked list previous
64 */
65 struct IPv4HttpAddressWrapper *prev;
66
67 struct IPv4HttpAddress *addr;
68};
69
70/**
71 * Network format for IPv6 addresses.
72 */
73struct IPv6HttpAddress
74{
75 /**
76 * IPv6 address.
77 */
78 struct in6_addr ipv6_addr GNUNET_PACKED;
79
80 /**
81 * Port number, in network byte order.
82 */
83 uint16_t port GNUNET_PACKED;
84
85};
86
87/**
88 * Wrapper for IPv4 addresses.
89 */
90struct IPv6HttpAddressWrapper
91{
92 /**
93 * Linked list next
94 */
95 struct IPv6HttpAddressWrapper *next;
96
97 /**
98 * Linked list previous
99 */
100 struct IPv6HttpAddressWrapper *prev;
101
102 struct IPv6HttpAddress *addr;
103};
104
105
106/**
107 * Context for address to string conversion.
108 */
109struct PrettyPrinterContext
110{
111 /**
112 * Function to call with the result.
113 */
114 GNUNET_TRANSPORT_AddressStringCallback asc;
115
116 /**
117 * Plugin
118 */
119 struct Plugin *plugin;
120
121 /**
122 * Clsoure for 'asc'.
123 */
124 void *asc_cls;
125
126 /**
127 * Port to add after the IP address.
128 */
129 uint16_t port;
130};
131
132
133/**
134 * Encapsulation of all of the state of the plugin.
135 */
136struct Plugin;
137
138
139
140/**
141 * Append our port and forward the result.
142 *
143 * @param cls the 'struct PrettyPrinterContext*'
144 * @param hostname hostname part of the address
145 */
146static void
147append_port (void *cls, const char *hostname)
148{
149 struct PrettyPrinterContext *ppc = cls;
150 char *ret;
151
152 if (hostname == NULL)
153 {
154 ppc->asc (ppc->asc_cls, NULL);
155 GNUNET_free (ppc);
156 return;
157 }
158 GNUNET_asprintf (&ret, "%s://%s:%d", ppc->plugin->protocol, hostname,
159 ppc->plugin->port);
160 ppc->asc (ppc->asc_cls, ret);
161 GNUNET_free (ret);
162}
163
164
165/**
166 * Convert the transports address to a nice, human-readable
167 * format.
168 *
169 * @param cls closure
170 * @param type name of the transport that generated the address
171 * @param addr one of the addresses of the host, NULL for the last address
172 * the specific address format depends on the transport
173 * @param addrlen length of the address
174 * @param numeric should (IP) addresses be displayed in numeric form?
175 * @param timeout after how long should we give up?
176 * @param asc function to call on each string
177 * @param asc_cls closure for asc
178 */
179static void
180http_plugin_address_pretty_printer (void *cls, const char *type,
181 const void *addr, size_t addrlen,
182 int numeric,
183 struct GNUNET_TIME_Relative timeout,
184 GNUNET_TRANSPORT_AddressStringCallback asc,
185 void *asc_cls)
186{
187 GNUNET_assert (cls != NULL);
188 struct PrettyPrinterContext *ppc;
189 const void *sb;
190 size_t sbs;
191 struct sockaddr_in a4;
192 struct sockaddr_in6 a6;
193 const struct IPv4HttpAddress *t4;
194 const struct IPv6HttpAddress *t6;
195 uint16_t port;
196
197 if (addrlen == sizeof (struct IPv6HttpAddress))
198 {
199 t6 = addr;
200 memset (&a6, 0, sizeof (a6));
201 a6.sin6_family = AF_INET6;
202 a6.sin6_port = t6->port;
203 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
204 port = ntohs (t6->port);
205 sb = &a6;
206 sbs = sizeof (a6);
207 }
208 else if (addrlen == sizeof (struct IPv4HttpAddress))
209 {
210 t4 = addr;
211 memset (&a4, 0, sizeof (a4));
212 a4.sin_family = AF_INET;
213 a4.sin_port = t4->port;
214 a4.sin_addr.s_addr = t4->ipv4_addr;
215 port = ntohs (t4->ipv4_addr);
216 sb = &a4;
217 sbs = sizeof (a4);
218 }
219 else
220 {
221 /* invalid address */
222 GNUNET_break_op (0);
223 asc (asc_cls, NULL);
224 return;
225 }
226 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
227 ppc->asc = asc;
228 ppc->asc_cls = asc_cls;
229 ppc->port = port;
230 ppc->plugin = cls;
231 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
232}
233
234
235
236/**
237 * Another peer has suggested an address for this
238 * peer and transport plugin. Check that this could be a valid
239 * address. If so, consider adding it to the list
240 * of addresses.
241 *
242 * @param cls closure
243 * @param addr pointer to the address
244 * @param addrlen length of addr
245 * @return GNUNET_OK if this is a plausible address for this peer
246 * and transport
247 */
248static int
249http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
250{
251 struct Plugin *plugin = cls;
252 struct IPv4HttpAddress *v4;
253 struct IPv6HttpAddress *v6;
254 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
255 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
256
257 GNUNET_assert (cls != NULL);
258 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
259 (addrlen != sizeof (struct IPv6HttpAddress)))
260 return GNUNET_SYSERR;
261 if (addrlen == sizeof (struct IPv4HttpAddress))
262 {
263 v4 = (struct IPv4HttpAddress *) addr;
264 while (w_tv4 != NULL)
265 {
266 if (0 ==
267 memcmp (&w_tv4->addr->ipv4_addr, &v4->ipv4_addr, sizeof (uint32_t)))
268 break;
269 w_tv4 = w_tv4->next;
270 }
271 if (w_tv4 != NULL)
272 return GNUNET_OK;
273 else
274 return GNUNET_SYSERR;
275 }
276 if (addrlen == sizeof (struct IPv6HttpAddress))
277 {
278 v6 = (struct IPv6HttpAddress *) addr;
279 while (w_tv6 != NULL)
280 {
281 if (0 ==
282 memcmp (&w_tv6->addr->ipv6_addr, &v6->ipv6_addr,
283 sizeof (struct in6_addr)))
284 break;
285 w_tv6 = w_tv6->next;
286 }
287 if (w_tv6 != NULL)
288 return GNUNET_OK;
289 else
290 return GNUNET_SYSERR;
291 }
292 return GNUNET_SYSERR;
293}
294
295/**
296 * Function called for a quick conversion of the binary address to
297 * a numeric address. Note that the caller must not free the
298 * address and that the next call to this function is allowed
299 * to override the address again.
300 *
301 * @param cls closure
302 * @param addr binary address
303 * @param addrlen length of the address
304 * @return string representing the same address
305 */
306const char *
307http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
308{
309 const struct IPv4HttpAddress *t4;
310 const struct IPv6HttpAddress *t6;
311 struct sockaddr_in a4;
312 struct sockaddr_in6 a6;
313 char *address;
314 static char rbuf[INET6_ADDRSTRLEN + 13];
315 uint16_t port;
316 int res;
317
318 if (addrlen == sizeof (struct IPv6HttpAddress))
319 {
320 address = GNUNET_malloc (INET6_ADDRSTRLEN);
321 t6 = addr;
322 a6.sin6_addr = t6->ipv6_addr;
323 inet_ntop (AF_INET6, &(a6.sin6_addr), address, INET6_ADDRSTRLEN);
324 port = ntohs (t6->port);
325 }
326 else if (addrlen == sizeof (struct IPv4HttpAddress))
327 {
328 address = GNUNET_malloc (INET_ADDRSTRLEN);
329 t4 = addr;
330 a4.sin_addr.s_addr = t4->ipv4_addr;
331 inet_ntop (AF_INET, &(a4.sin_addr), address, INET_ADDRSTRLEN);
332 port = ntohs (t4->port);
333 }
334 else
335 {
336 /* invalid address */
337 return NULL;
338 }
339
340 GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
341
342 res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s:%u", address, port);
343
344 GNUNET_free (address);
345 GNUNET_assert (res != 0);
346 return rbuf;
347}
348
349struct Session *
350lookup_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
351 const void *addr, size_t addrlen, int force_address)
352{
353 struct Session *s = NULL;
354 struct Session *t = NULL;
355 int e_peer;
356 int e_addr;
357
358 t = plugin->head;
359 if (t == NULL)
360 return NULL;
361 while (t->next != NULL)
362 {
363 e_peer = GNUNET_NO;
364 e_addr = GNUNET_NO;
365 if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
366 {
367 e_peer = GNUNET_YES;
368 if (addrlen == t->addrlen)
369 {
370 if (0 == memcmp (addr, &t->addr, addrlen))
371 e_addr = GNUNET_YES;
372 }
373 }
374
375 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
376 {
377 s = t;
378 break;
379 }
380 else if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) &&
381 (e_addr == GNUNET_YES))
382 {
383 s = t;
384 break;
385 }
386 else if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
387 {
388 s = t;
389 break;
390 }
391 t = t->next;
392 }
393
394 return s;
395}
396
397void
398delete_session (struct Session *s)
399{
400 GNUNET_free (s->addr);
401 GNUNET_free (s);
402}
403
404struct Session *
405create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
406 const void *addr, size_t addrlen,
407 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
408{
409 struct Session *s = NULL;
410
411 s = GNUNET_malloc (sizeof (struct Session));
412 memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
413 s->plugin = plugin;
414 s->addr = GNUNET_malloc (addrlen);
415 memcpy (s->addr, addr, addrlen);
416 s->addrlen = addrlen;
417 s->transmit_cont = cont;
418 s->transmit_cont_cls = cont_cls;
419 s->next = NULL;
420
421 return s;
422}
423
424/**
425 * Function that can be used by the transport service to transmit
426 * a message using the plugin. Note that in the case of a
427 * peer disconnecting, the continuation MUST be called
428 * prior to the disconnect notification itself. This function
429 * will be called with this peer's HELLO message to initiate
430 * a fresh connection to another peer.
431 *
432 * @param cls closure
433 * @param target who should receive this message
434 * @param msgbuf the message to transmit
435 * @param msgbuf_size number of bytes in 'msgbuf'
436 * @param priority how important is the message (most plugins will
437 * ignore message priority and just FIFO)
438 * @param to how long to wait at most for the transmission (does not
439 * require plugins to discard the message after the timeout,
440 * just advisory for the desired delay; most plugins will ignore
441 * this as well)
442 * @param session which session must be used (or NULL for "any")
443 * @param addr the address to use (can be NULL if the plugin
444 * is "on its own" (i.e. re-use existing TCP connection))
445 * @param addrlen length of the address in bytes
446 * @param force_address GNUNET_YES if the plugin MUST use the given address,
447 * GNUNET_NO means the plugin may use any other address and
448 * GNUNET_SYSERR means that only reliable existing
449 * bi-directional connections should be used (regardless
450 * of address)
451 * @param cont continuation to call once the message has
452 * been transmitted (or if the transport is ready
453 * for the next transmission call; or if the
454 * peer disconnected...); can be NULL
455 * @param cont_cls closure for cont
456 * @return number of bytes used (on the physical network, with overheads);
457 * -1 on hard errors (i.e. address invalid); 0 is a legal value
458 * and does NOT mean that the message was not transmitted (DV)
459 */
460static ssize_t
461http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
462 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
463 struct GNUNET_TIME_Relative to, struct Session *session,
464 const void *addr, size_t addrlen, int force_address,
465 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
466{
467 struct Plugin *plugin = cls;
468
469 GNUNET_assert (plugin != NULL);
470
471 int res = GNUNET_SYSERR;
472
473#if DEBUG_HTTP
474 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
475 "Sending %u bytes to peer `%s'\n", msgbuf_size,
476 GNUNET_i2s (target));
477#endif
478
479 struct Session *s = NULL;
480
481 /* look for existing connection */
482 s = lookup_session (plugin, target, addr, addrlen, force_address);
483
484 /* create new connection */
485 if (s == NULL)
486 {
487#if DEBUG_HTTP
488 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
489 "Initiiating new connection to peer `%s'\n",
490 GNUNET_i2s (target));
491#endif
492 s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
493 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
494 // initiate new connection
495 client_connect (s);
496 }
497 else if (s->inbound == GNUNET_NO)
498 res = client_send (s, msgbuf, msgbuf_size);
499 else if (s->inbound == GNUNET_YES)
500 res = server_send (s, msgbuf, msgbuf_size);
501
502 return res;
503}
504
505
506/**
507 * Function that can be used to force the plugin to disconnect
508 * from the given peer and cancel all previous transmissions
509 * (and their continuationc).
510 *
511 * @param cls closure
512 * @param target peer from which to disconnect
513 */
514static void
515http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
516{
517 struct Plugin *plugin = cls;
518 struct Session *next = NULL;
519 struct Session *s = plugin->head;
520
521 while (s != NULL)
522 {
523 next = s->next;
524 if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
525 {
526 if (s->inbound == GNUNET_NO)
527 GNUNET_assert (GNUNET_OK == client_disconnect (s));
528 else
529 GNUNET_assert (GNUNET_OK == server_disconnect (s));
530 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
531 delete_session (s);
532 }
533 s = next;
534 }
535}
536
537/**
538 * Function called by the NAT subsystem suggesting another peer wants
539 * to connect to us via connection reversal. Try to connect back to the
540 * given IP.
541 *
542 * @param cls closure
543 * @param addr address to try
544 * @param addrlen number of bytes in addr
545 */
546static void
547nat_connection_reversal (void *cls, const struct sockaddr *addr,
548 socklen_t addrlen)
549{
550
551}
552
553static void
554nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
555 socklen_t addrlen)
556{
557 struct Plugin *plugin = cls;
558 struct IPv4HttpAddress *t4 = NULL;
559 struct IPv4HttpAddressWrapper *w_t4 = NULL;
560 struct IPv6HttpAddress *t6 = NULL;
561 struct IPv6HttpAddressWrapper *w_t6 = NULL;
562 int af;
563
564 af = addr->sa_family;
565 switch (af)
566 {
567 case AF_INET:
568 w_t4 = plugin->ipv4_addr_head;
569 while (w_t4 != NULL)
570 {
571 int res = memcmp (&w_t4->addr->ipv4_addr,
572 &((struct sockaddr_in *) addr)->sin_addr,
573 sizeof (struct in_addr));
574
575 if (0 == res)
576 break;
577 w_t4 = w_t4->next;
578 }
579 if (w_t4 == NULL)
580 {
581 w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
582 t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddress));
583 memcpy (&t4->ipv4_addr, &((struct sockaddr_in *) addr)->sin_addr,
584 sizeof (struct in_addr));
585 t4->port = htons (plugin->port);
586
587 w_t4->addr = t4;
588
589 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
590 plugin->ipv4_addr_tail, w_t4);
591 }
592 plugin->env->notify_address (plugin->env->cls, add_remove, w_t4->addr,
593 sizeof (struct IPv4HttpAddress));
594
595 break;
596 case AF_INET6:
597 w_t6 = plugin->ipv6_addr_head;
598 while (w_t6)
599 {
600 int res = memcmp (&w_t6->addr->ipv6_addr,
601 &((struct sockaddr_in6 *) addr)->sin6_addr,
602 sizeof (struct in6_addr));
603
604 if (0 == res)
605 break;
606 w_t6 = w_t6->next;
607 }
608 if (w_t6 == NULL)
609 {
610 w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
611 t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddress));
612
613 memcpy (&t6->ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
614 sizeof (struct in6_addr));
615 t6->port = htons (plugin->port);
616
617 w_t6->addr = t6;
618
619 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
620 plugin->ipv6_addr_tail, w_t6);
621 }
622 plugin->env->notify_address (plugin->env->cls, add_remove, w_t6->addr,
623 sizeof (struct IPv6HttpAddress));
624 break;
625 default:
626 return;
627 }
628
629}
630
631static void
632nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
633 socklen_t addrlen)
634{
635 struct Plugin *plugin = cls;
636 struct IPv4HttpAddressWrapper *w_t4 = NULL;
637 struct IPv6HttpAddressWrapper *w_t6 = NULL;
638 int af;
639
640 af = addr->sa_family;
641 switch (af)
642 {
643 case AF_INET:
644 w_t4 = plugin->ipv4_addr_head;
645 while (w_t4 != NULL)
646 {
647 int res = memcmp (&w_t4->addr->ipv4_addr,
648 &((struct sockaddr_in *) addr)->sin_addr,
649 sizeof (struct in_addr));
650
651 if (0 == res)
652 break;
653 w_t4 = w_t4->next;
654 }
655 if (w_t4 == NULL)
656 return;
657 plugin->env->notify_address (plugin->env->cls, add_remove, w_t4->addr,
658 sizeof (struct IPv4HttpAddress));
659
660 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
661 w_t4);
662 GNUNET_free (w_t4->addr);
663 GNUNET_free (w_t4);
664 break;
665 case AF_INET6:
666 w_t6 = plugin->ipv6_addr_head;
667 while (w_t6 != NULL)
668 {
669 int res = memcmp (&w_t6->addr->ipv6_addr,
670 &((struct sockaddr_in6 *) addr)->sin6_addr,
671 sizeof (struct in6_addr));
672
673 if (0 == res)
674 break;
675 w_t6 = w_t6->next;
676 }
677 if (w_t6 == NULL)
678 return;
679 plugin->env->notify_address (plugin->env->cls, add_remove, w_t6->addr,
680 sizeof (struct IPv6HttpAddress));
681
682 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
683 w_t6);
684 GNUNET_free (w_t6->addr);
685 GNUNET_free (w_t6);
686 break;
687 default:
688 return;
689 }
690
691}
692
693/**
694 * Our external IP address/port mapping has changed.
695 *
696 * @param cls closure, the 'struct LocalAddrList'
697 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
698 * the previous (now invalid) one
699 * @param addr either the previous or the new public IP address
700 * @param addrlen actual lenght of the address
701 */
702static void
703nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
704 socklen_t addrlen)
705{
706 GNUNET_assert (cls != NULL);
707 struct Plugin *plugin = cls;
708
709#if DEBUG_HTTP
710 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
711 "NPMC called %s to address `%s'\n",
712 (add_remove == GNUNET_NO) ? "remove" : "add",
713 GNUNET_a2s (addr, addrlen));
714#endif
715 /* convert 'addr' to our internal format */
716 switch (add_remove)
717 {
718 case GNUNET_YES:
719 nat_add_address (cls, add_remove, addr, addrlen);
720 break;
721 case GNUNET_NO:
722 nat_remove_address (cls, add_remove, addr, addrlen);
723 break;
724 }
725}
726
727
728static void
729start_report_addresses (struct Plugin *plugin)
730{
731 int res = GNUNET_OK;
732 struct sockaddr **addrs;
733 socklen_t *addrlens;
734
735 res =
736 GNUNET_SERVICE_get_server_addresses (plugin->name, plugin->env->cfg,
737 &addrs, &addrlens);
738
739 if (res != GNUNET_SYSERR)
740 {
741 plugin->nat =
742 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
743 (unsigned int) res,
744 (const struct sockaddr **) addrs, addrlens,
745 &nat_port_map_callback, &nat_connection_reversal,
746 plugin);
747 while (res > 0)
748 {
749 res--;
750 GNUNET_assert (addrs[res] != NULL);
751 GNUNET_free (addrs[res]);
752 }
753 GNUNET_free_non_null (addrs);
754 GNUNET_free_non_null (addrlens);
755 }
756 else
757 {
758 plugin->nat =
759 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
760 NULL, &nat_connection_reversal, plugin);
761 }
762}
763
764static void
765stop_report_addresses (struct Plugin *plugin)
766{
767 /* Stop NAT handle */
768 GNUNET_NAT_unregister (plugin->nat);
769
770 /* Clean up addresses */
771 struct IPv4HttpAddressWrapper *w_t4;
772 struct IPv6HttpAddressWrapper *w_t6;
773
774 while (plugin->ipv4_addr_head != NULL)
775 {
776 w_t4 = plugin->ipv4_addr_head;
777 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
778 w_t4);
779 GNUNET_free (w_t4->addr);
780 GNUNET_free (w_t4);
781 }
782
783 while (plugin->ipv6_addr_head != NULL)
784 {
785 w_t6 = plugin->ipv6_addr_head;
786 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
787 w_t6);
788 GNUNET_free (w_t6->addr);
789 GNUNET_free (w_t6);
790 }
791}
792
793static int
794configure_plugin (struct Plugin *plugin)
795{
796 int res = GNUNET_OK;
797
798 /* Use IPv4? */
799 if (GNUNET_CONFIGURATION_have_value
800 (plugin->env->cfg, plugin->name, "USE_IPv4"))
801 {
802 plugin->ipv4 =
803 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
804 "USE_IPv4");
805 }
806 else
807 plugin->ipv4 = GNUNET_YES;
808
809 /* Use IPv6? */
810 if (GNUNET_CONFIGURATION_have_value
811 (plugin->env->cfg, plugin->name, "USE_IPv6"))
812 {
813 plugin->ipv6 =
814 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
815 "USE_IPv6");
816 }
817 else
818 plugin->ipv6 = GNUNET_YES;
819
820 if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
821 {
822 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
823 _
824 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
825 plugin->name);
826 res = GNUNET_SYSERR;
827 }
828 /* Reading port number from config file */
829 unsigned long long port;
830
831 if ((GNUNET_OK !=
832 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
833 "PORT", &port)) || (port > 65535))
834 {
835 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
836 _("Port is required! Fix in configuration\n"),
837 plugin->name);
838 res = GNUNET_SYSERR;
839 }
840 plugin->port = port;
841
842 return res;
843}
844
845/**
846 * Entry point for the plugin.
847 */
848void *
849LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
850{
851 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
852 struct GNUNET_TRANSPORT_PluginFunctions *api;
853 struct Plugin *plugin;
854 int res;
855
856 plugin = GNUNET_malloc (sizeof (struct Plugin));
857 plugin->env = env;
858 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
859 api->cls = plugin;
860 api->send = &http_plugin_send;
861 api->disconnect = &http_plugin_disconnect;
862 api->address_pretty_printer = &http_plugin_address_pretty_printer;
863 api->check_address = &http_plugin_address_suggested;
864 api->address_to_string = &http_plugin_address_to_string;
865
866#if BUILD_HTTPS
867 plugin->name = "transport-https";
868 plugin->protocol = "https";
869#else
870 plugin->name = "transport-http";
871 plugin->protocol = "http";
872#endif
873 /* Configure plugin from configuration */
874
875 res = configure_plugin (plugin);
876 if (res == GNUNET_SYSERR)
877 {
878 GNUNET_free (plugin);
879 GNUNET_free (api);
880 return NULL;
881 }
882
883 /* Start client */
884 res = client_start (plugin);
885 if (res == GNUNET_SYSERR)
886 {
887 GNUNET_free (plugin);
888 GNUNET_free (api);
889 return NULL;
890 }
891
892 /* Start server */
893 res = server_start (plugin);
894 if (res == GNUNET_SYSERR)
895 {
896 server_stop (plugin);
897 client_stop (plugin);
898
899 GNUNET_free (plugin);
900 GNUNET_free (api);
901 return NULL;
902 }
903
904 /* Report addresses to transport service */
905 start_report_addresses (plugin);
906
907#if DEBUG_HTTP
908 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
909 "Plugin `%s' loaded\n", plugin->name);
910#endif
911
912 return api;
913}
914
915
916/**
917 * Exit point from the plugin.
918 */
919void *
920LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
921{
922 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
923 struct Plugin *plugin = api->cls;
924 struct Session *s = NULL;
925
926 /* Stop reporting addresses to transport service */
927 stop_report_addresses (plugin);
928
929 /* cleaning up sessions */
930 s = plugin->head;
931 while (s != NULL)
932 {
933 struct Session *t = s->next;
934
935 if (s->inbound == GNUNET_NO)
936 GNUNET_assert (GNUNET_OK == client_disconnect (s));
937 else
938 GNUNET_assert (GNUNET_OK == server_disconnect (s));
939
940 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
941 delete_session (s);
942 s = t;
943 }
944
945 /* Stop server */
946 server_stop (plugin);
947
948 /* Stop client */
949 client_stop (plugin);
950
951
952#if DEBUG_HTTP
953 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
954 "Plugin `%s' unloaded\n", plugin->name);
955#endif
956
957 GNUNET_free (plugin);
958 GNUNET_free (api);
959
960 return NULL;
961}
962
963/* end of plugin_transport_http.c */
diff --git a/src/transport/plugin_transport_http_server.c b/src/transport/plugin_transport_http_server.c
new file mode 100644
index 000000000..96d3e27ae
--- /dev/null
+++ b/src/transport/plugin_transport_http_server.c
@@ -0,0 +1,374 @@
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 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 transport/plugin_transport_http.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
25 */
26
27#include "plugin_transport_http.h"
28
29static void
30server_log (void *arg, const char *fmt, va_list ap)
31{
32 char text[1024];
33
34 vsnprintf (text, sizeof (text), fmt, ap);
35 va_end (ap);
36 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: %s\n", text);
37}
38
39/**
40 * Check if incoming connection is accepted.
41 * NOTE: Here every connection is accepted
42 * @param cls plugin as closure
43 * @param addr address of incoming connection
44 * @param addr_len address length of incoming connection
45 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
46 *
47 */
48static int
49server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
50{
51 return 0;
52}
53
54
55/**
56 * Callback called by MHD when it needs data to send
57 * @param cls current session
58 * @param pos position in buffer
59 * @param buf the buffer to write data to
60 * @param max max number of bytes available in buffer
61 * @return bytes written to buffer
62 */
63#if 0
64static ssize_t
65server_send_cb (void *cls, uint64_t pos, char *buf, size_t max)
66{
67
68 return 0;
69}
70#endif
71
72
73#if BUILD_HTTPS
74static char *
75server_load_file (const char *file)
76{
77 struct GNUNET_DISK_FileHandle *gn_file;
78 struct stat fstat;
79 char *text = NULL;
80
81 if (0 != STAT (file, &fstat))
82 return NULL;
83 text = GNUNET_malloc (fstat.st_size + 1);
84 gn_file =
85 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
86 GNUNET_DISK_PERM_USER_READ);
87 if (gn_file == NULL)
88 {
89 GNUNET_free (text);
90 return NULL;
91 }
92 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
93 {
94 GNUNET_free (text);
95 GNUNET_DISK_file_close (gn_file);
96 return NULL;
97 }
98 text[fstat.st_size] = '\0';
99 GNUNET_DISK_file_close (gn_file);
100 return text;
101}
102#endif
103
104
105#if BUILD_HTTPS
106
107static int
108server_load_certificate (struct Plugin *plugin)
109{
110 int res = GNUNET_OK;
111
112 char *key_file;
113 char *cert_file;
114
115 /* Get crypto init string from config
116 * If not present just use default values */
117 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
118 "CRYPTO_INIT", &plugin->crypto_init);
119
120 if (GNUNET_OK !=
121 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
122 "KEY_FILE", &key_file))
123 {
124 key_file = "https_key.key";
125 }
126
127 if (GNUNET_OK !=
128 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
129 "CERT_FILE", &cert_file))
130 {
131 cert_file = "https_cert.crt";
132 }
133
134 /* read key & certificates from file */
135#if VERBOSE_SERVER
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
138 key_file, cert_file);
139#endif
140
141 plugin->key = server_load_file (key_file);
142 plugin->cert = server_load_file (cert_file);
143
144 if ((plugin->key == NULL) || (plugin->cert == NULL))
145 {
146 struct GNUNET_OS_Process *cert_creation;
147
148 GNUNET_free_non_null (plugin->key);
149 plugin->key = NULL;
150 GNUNET_free_non_null (plugin->cert);
151 plugin->cert = NULL;
152
153#if VERBOSE_SERVER
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "No usable TLS certificate found, creating certificate\n");
156#endif
157 errno = 0;
158 cert_creation =
159 GNUNET_OS_start_process (NULL, NULL,
160 "gnunet-transport-certificate-creation",
161 "gnunet-transport-certificate-creation",
162 key_file, cert_file, NULL);
163 if (cert_creation == NULL)
164 {
165 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
166 _
167 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
168 GNUNET_free (key_file);
169 GNUNET_free (cert_file);
170
171 GNUNET_free_non_null (plugin->key);
172 GNUNET_free_non_null (plugin->cert);
173 GNUNET_free_non_null (plugin->crypto_init);
174
175 return GNUNET_SYSERR;
176 }
177 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
178 GNUNET_OS_process_close (cert_creation);
179
180 plugin->key = server_load_file (key_file);
181 plugin->cert = server_load_file (cert_file);
182 }
183
184 if ((plugin->key == NULL) || (plugin->cert == NULL))
185 {
186 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
187 _
188 ("No usable TLS certificate found and creating one failed!\n"),
189 "transport-https");
190 GNUNET_free (key_file);
191 GNUNET_free (cert_file);
192
193 GNUNET_free_non_null (plugin->key);
194 GNUNET_free_non_null (plugin->cert);
195 GNUNET_free_non_null (plugin->crypto_init);
196
197 return GNUNET_SYSERR;
198 }
199 GNUNET_free (key_file);
200 GNUNET_free (cert_file);
201#if DEBUG_HTTP
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
203#endif
204
205 return res;
206}
207#endif
208
209
210/**
211 * Process GET or PUT request received via MHD. For
212 * GET, queue response that will send back our pending
213 * messages. For PUT, process incoming data and send
214 * to GNUnet core. In either case, check if a session
215 * already exists and create a new one if not.
216 */
217static int
218server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
219 const char *url, const char *method, const char *version,
220 const char *upload_data, size_t * upload_data_size,
221 void **httpSessionCache)
222{
223 return 0;
224}
225
226static void
227server_disconnect_cb (void *cls, struct MHD_Connection *connection,
228 void **httpSessionCache)
229{
230}
231
232int
233server_disconnect (struct Session *s)
234{
235 return GNUNET_OK;
236}
237
238int
239server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
240{
241 return GNUNET_OK;
242}
243
244int
245server_start (struct Plugin *plugin)
246{
247 int res = GNUNET_OK;
248
249#if BUILD_HTTPS
250 res = server_load_certificate (plugin);
251 if (res == GNUNET_SYSERR)
252 {
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TABORT\n");
254 return res;
255 }
256#endif
257
258 plugin->server_v4 = NULL;
259 if (plugin->ipv4 == GNUNET_YES)
260 {
261 plugin->server_v4 = MHD_start_daemon (
262#if VERBOSE_SERVER
263 MHD_USE_DEBUG |
264#endif
265#if BUILD_HTTPS
266 MHD_USE_SSL |
267#endif
268 MHD_NO_FLAG, plugin->port,
269 &server_accept_cb, plugin,
270 &server_access_cb, plugin,
271 //MHD_OPTION_SOCK_ADDR,
272 //(struct sockaddr_in *)
273 //plugin->bind4_address,
274 MHD_OPTION_CONNECTION_LIMIT,
275 (unsigned int)
276 plugin->max_connections,
277#if BUILD_HTTPS
278 MHD_OPTION_HTTPS_PRIORITIES,
279 plugin->crypto_init,
280 MHD_OPTION_HTTPS_MEM_KEY,
281 plugin->key,
282 MHD_OPTION_HTTPS_MEM_CERT,
283 plugin->cert,
284#endif
285 MHD_OPTION_CONNECTION_TIMEOUT,
286 (unsigned int) 3,
287 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
288 (size_t) (2 *
289 GNUNET_SERVER_MAX_MESSAGE_SIZE),
290 MHD_OPTION_NOTIFY_COMPLETED,
291 &server_disconnect_cb, plugin,
292 MHD_OPTION_EXTERNAL_LOGGER,
293 server_log, NULL, MHD_OPTION_END);
294 if (plugin->server_v4 == NULL)
295 res = GNUNET_SYSERR;
296 }
297 plugin->server_v6 = NULL;
298 if (plugin->ipv6 == GNUNET_YES)
299 {
300 plugin->server_v6 = MHD_start_daemon (
301#if VERBOSE_SERVER
302 MHD_USE_DEBUG |
303#endif
304#if BUILD_HTTPS
305 MHD_USE_SSL |
306#endif
307 MHD_USE_IPv6, plugin->port,
308 &server_accept_cb, plugin,
309 &server_access_cb, plugin,
310 //MHD_OPTION_SOCK_ADDR,
311 //tmp,
312 MHD_OPTION_CONNECTION_LIMIT,
313 (unsigned int)
314 plugin->max_connections,
315#if BUILD_HTTPS
316 MHD_OPTION_HTTPS_PRIORITIES,
317 plugin->crypto_init,
318 MHD_OPTION_HTTPS_MEM_KEY,
319 plugin->key,
320 MHD_OPTION_HTTPS_MEM_CERT,
321 plugin->cert,
322#endif
323 MHD_OPTION_CONNECTION_TIMEOUT,
324 (unsigned int) 3,
325 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
326 (size_t) (2 *
327 GNUNET_SERVER_MAX_MESSAGE_SIZE),
328 MHD_OPTION_NOTIFY_COMPLETED,
329 &server_disconnect_cb, plugin,
330 MHD_OPTION_EXTERNAL_LOGGER,
331 server_log, NULL, MHD_OPTION_END);
332
333 if (plugin->server_v6 == NULL)
334 res = GNUNET_SYSERR;
335 }
336
337#if DEBUG_HTTP
338 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
339 "%s server component started on port %u\n", plugin->name,
340 plugin->port);
341#endif
342 return res;
343}
344
345void
346server_stop (struct Plugin *plugin)
347{
348
349 if (plugin->server_v4 != NULL)
350 {
351 MHD_stop_daemon (plugin->server_v4);
352 plugin->server_v4 = NULL;
353 }
354 if (plugin->server_v6 != NULL)
355 {
356 MHD_stop_daemon (plugin->server_v6);
357 plugin->server_v6 = NULL;
358 }
359
360#if BUILD_HTTPS
361 GNUNET_free_non_null (plugin->crypto_init);
362 GNUNET_free_non_null (plugin->cert);
363 GNUNET_free_non_null (plugin->key);
364#endif
365
366#if DEBUG_HTTP
367 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
368 "%s server component stopped\n", plugin->name);
369#endif
370}
371
372
373
374/* end of plugin_transport_http.c */