server side connection accepting and merging
[oweals/gnunet.git] / src / transport / plugin_transport_http.h
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  */
63 struct 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
119   /* Plugin values */
120
121
122   int cur_connections;
123   uint32_t last_tag;
124
125   /*
126    * Server handles
127    */
128
129   struct MHD_Daemon *server_v4;
130   GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
131
132   struct MHD_Daemon *server_v6;
133   GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
134
135   char *crypto_init;
136   char *key;
137   char *cert;
138
139   struct Session *server_semi_head;
140
141   struct Session *server_semi_tail;
142
143
144
145   /*
146    * Client handles
147    */
148
149   /**
150    * cURL Multihandle
151    */
152   CURLM *client_mh;
153
154   GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
155
156 };
157
158 /**
159  * Session handle for connections.
160  */
161 struct Session
162 {
163
164   /**
165    * Stored in a linked list.
166    */
167   struct Session *next;
168
169   /**
170    * Stored in a linked list.
171    */
172   struct Session *prev;
173
174   /**
175    * Pointer to the global plugin struct.
176    */
177   struct Plugin *plugin;
178
179   /**
180    * The client (used to identify this connection)
181    */
182   /* void *client; */
183
184   /**
185    * Continuation function to call once the transmission buffer
186    * has again space available.  NULL if there is no
187    * continuation to call.
188    */
189   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
190
191
192   void *addr;
193
194   size_t addrlen;
195
196   /**
197    * Closure for transmit_cont.
198    */
199   void *transmit_cont_cls;
200
201   /**
202    * To whom are we talking to (set to our identity
203    * if we are still waiting for the welcome message)
204    */
205   struct GNUNET_PeerIdentity target;
206
207   /**
208    * At what time did we reset last_received last?
209    */
210   //struct GNUNET_TIME_Absolute last_quota_update;
211
212   /**
213    * How many bytes have we received since the "last_quota_update"
214    * timestamp?
215    */
216   //uint64_t last_received;
217
218   /**
219    * Number of bytes per ms that this peer is allowed
220    * to send to us.
221    */
222   //uint32_t quota;
223
224
225   int inbound;
226
227   void *client_put;
228   void *client_get;
229
230   void *server_put;
231   void *server_get;
232
233   uint32_t tag;
234
235 };
236
237 struct Session *
238 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
239                 const void *addr, size_t addrlen,
240                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls);
241
242 const char *
243 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
244
245 int
246 client_disconnect (struct Session *s);
247
248 int
249 client_connect (struct Session *s);
250
251 int
252 client_send (struct Session *s, const char *msgbuf, size_t msgbuf_size);
253
254 int
255 client_start (struct Plugin *plugin);
256
257 void
258 client_stop (struct Plugin *plugin);
259
260 int
261 server_disconnect (struct Session *s);
262
263 int
264 server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size);
265
266 int
267 server_start (struct Plugin *plugin);
268
269 void
270 server_stop (struct Plugin *plugin);
271
272 void
273 notify_session_end (void *cls,
274                     const struct GNUNET_PeerIdentity *
275                     peer, struct Session * s);
276
277 /* end of plugin_transport_http.h */