fixed:
[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 #define VERBOSE_CURL GNUNET_YES
49
50 #if BUILD_HTTPS
51 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
52 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
53 #else
54 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
55 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
56 #endif
57
58 #define INBOUND  GNUNET_YES
59 #define OUTBOUND GNUNET_NO
60
61
62 #define HTTP_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
63
64 /**
65  * Encapsulation of all of the state of the plugin.
66  */
67 struct Plugin
68 {
69   /**
70    * Our environment.
71    */
72   struct GNUNET_TRANSPORT_PluginEnvironment *env;
73
74   /**
75    * List of open sessions.
76    */
77   struct Session *head;
78
79   struct Session *tail;
80
81   /**
82    * NAT handle & address management
83    */
84   struct GNUNET_NAT_Handle *nat;
85
86
87   /**
88    * ipv4 DLL head
89    */
90   struct IPv4HttpAddressWrapper *ipv4_addr_head;
91
92   /**
93    * ipv4 DLL tail
94    */
95   struct IPv4HttpAddressWrapper *ipv4_addr_tail;
96
97   /**
98    * ipv6 DLL head
99    */
100   struct IPv6HttpAddressWrapper *ipv6_addr_head;
101
102   /**
103    * ipv6 DLL tail
104    */
105   struct IPv6HttpAddressWrapper *ipv6_addr_tail;
106
107
108   /* Plugin configuration */
109
110   char *name;
111
112   char *protocol;
113
114   int ipv4;
115
116   int ipv6;
117
118   uint16_t port;
119
120   int max_connections;
121
122
123
124   /* Plugin values */
125
126
127   int cur_connections;
128   uint32_t last_tag;
129   /*
130    * Server handles
131    */
132
133   struct MHD_Daemon *server_v4;
134   GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
135
136   struct MHD_Daemon *server_v6;
137   GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
138
139   struct sockaddr_in * server_addr_v4;
140   struct sockaddr_in6 * server_addr_v6;
141
142   char *crypto_init;
143   char *key;
144   char *cert;
145
146   struct Session *server_semi_head;
147
148   struct Session *server_semi_tail;
149
150
151
152   /*
153    * Client handles
154    */
155
156   /**
157    * cURL Multihandle
158    */
159   CURLM *client_mh;
160
161   GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
162
163 };
164
165 /**
166  * Session handle for connections.
167  */
168 struct Session
169 {
170
171   /**
172    * Stored in a linked list.
173    */
174   struct Session *next;
175
176   /**
177    * Stored in a linked list.
178    */
179   struct Session *prev;
180
181   /**
182    * Pointer to the global plugin struct.
183    */
184   struct Plugin *plugin;
185
186   /**
187    * next pointer for double linked list
188    */
189   struct HTTP_Message *msg_head;
190
191   /**
192    * previous pointer for double linked list
193    */
194   struct HTTP_Message *msg_tail;
195
196
197   /**
198    * message stream tokenizer for incoming data
199    */
200   struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
201
202   /**
203    * Continuation function to call once the transmission buffer
204    * has again space available.  NULL if there is no
205    * continuation to call.
206    */
207   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
208
209
210   void *addr;
211
212   size_t addrlen;
213
214   /**
215    * Closure for transmit_cont.
216    */
217   void *transmit_cont_cls;
218
219   /**
220    * To whom are we talking to (set to our identity
221    * if we are still waiting for the welcome message)
222    */
223   struct GNUNET_PeerIdentity target;
224
225   /**
226    * At what time did we reset last_received last?
227    */
228   //struct GNUNET_TIME_Absolute last_quota_update;
229
230   /**
231    * How many bytes have we received since the "last_quota_update"
232    * timestamp?
233    */
234   //uint64_t last_received;
235
236   /**
237    * Number of bytes per ms that this peer is allowed
238    * to send to us.
239    */
240   //uint32_t quota;
241
242
243   int inbound;
244
245   void *client_put;
246   void *client_get;
247   int client_put_paused;
248
249   void *server_recv;
250   void *server_send;
251   struct GNUNET_TIME_Absolute delay;
252   GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
253   uint32_t tag;
254
255 };
256
257 /**
258  *  Message to send using http
259  */
260 struct HTTP_Message
261 {
262   /**
263    * next pointer for double linked list
264    */
265   struct HTTP_Message *next;
266
267   /**
268    * previous pointer for double linked list
269    */
270   struct HTTP_Message *prev;
271
272   /**
273    * buffer containing data to send
274    */
275   char *buf;
276
277   /**
278    * amount of data already sent
279    */
280   size_t pos;
281
282   /**
283    * buffer length
284    */
285   size_t size;
286
287   /**
288    * Continuation function to call once the transmission buffer
289    * has again space available.  NULL if there is no
290    * continuation to call.
291    */
292   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
293
294   /**
295    * Closure for transmit_cont.
296    */
297   void *transmit_cont_cls;
298 };
299
300 void
301 delete_session (struct Session *s);
302
303 struct Session *
304 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
305                 const void *addr, size_t addrlen,
306                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls);
307
308 struct GNUNET_TIME_Relative
309 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity * peer,
310     const struct  GNUNET_MessageHeader * message,
311     struct Session * session,
312     const char *sender_address,
313     uint16_t sender_address_len);
314
315 const char *
316 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
317
318 int
319 client_disconnect (struct Session *s);
320
321 int
322 client_connect (struct Session *s);
323
324 int
325 client_send (struct Session *s, struct HTTP_Message *msg);
326
327 int
328 client_start (struct Plugin *plugin);
329
330 void
331 client_stop (struct Plugin *plugin);
332
333 int
334 server_disconnect (struct Session *s);
335
336 int
337 server_send (struct Session *s, struct HTTP_Message * msg);
338
339 int
340 server_start (struct Plugin *plugin);
341
342 void
343 server_stop (struct Plugin *plugin);
344
345 void
346 notify_session_end (void *cls,
347                     const struct GNUNET_PeerIdentity *
348                     peer, struct Session * s);
349
350 /* end of plugin_transport_http.h */