renaming GNUNET_TIME_relative_get_forever and GNUNET_TIME_absolute_get_forever method...
[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_EXTRA_LOGGING
46 #define VERBOSE_SERVER GNUNET_EXTRA_LOGGING
47 #define VERBOSE_CLIENT GNUNET_EXTRA_LOGGING
48 #define VERBOSE_CURL GNUNET_NO
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, 15)
63
64 /**
65  * Encapsulation of all of the state of the plugin.
66  */
67 struct Plugin
68 {
69   /**
70    * General handles
71    * ---------------
72    */
73
74   /**
75    * Our environment.
76    */
77   struct GNUNET_TRANSPORT_PluginEnvironment *env;
78
79   /**
80    * Linked list of open sessions.
81    */
82
83   struct Session *head;
84
85   struct Session *tail;
86
87   /**
88    * NAT handle & address management
89    */
90   struct GNUNET_NAT_Handle *nat;
91
92   /**
93    * List of own addresses
94    */
95
96   /**
97    * IPv4 addresses DLL head
98    */
99   struct IPv4HttpAddressWrapper *ipv4_addr_head;
100
101   /**
102    * IPv4 addresses DLL tail
103    */
104   struct IPv4HttpAddressWrapper *ipv4_addr_tail;
105
106   /**
107    * IPv6 addresses DLL head
108    */
109   struct IPv6HttpAddressWrapper *ipv6_addr_head;
110
111   /**
112    * IPv6 addresses DLL tail
113    */
114   struct IPv6HttpAddressWrapper *ipv6_addr_tail;
115
116   /**
117    * Plugin configuration
118    * --------------------
119    */
120
121   /**
122    * Plugin name
123    * Equals configuration section: transport-http, transport-https
124    */
125   char *name;
126
127   /**
128    * Plugin protocol
129    * http, https
130    */
131   char *protocol;
132
133   /**
134    * Use IPv4?
135    * GNUNET_YES or GNUNET_NO
136    */
137   int ipv4;
138
139   /**
140    * Use IPv6?
141    * GNUNET_YES or GNUNET_NO
142    */
143   int ipv6;
144
145   /**
146    * Does plugin just use outbound connections and not accept inbound?
147    */
148
149   int client_only;
150
151   /**
152    * Port used
153    */
154   uint16_t port;
155
156   /**
157    * Maximum number of sockets the plugin can use
158    * Each http inbound /outbound connections are two connections
159    */
160   int max_connections;
161
162   /**
163    * Number of outbound sessions
164    */
165   unsigned int outbound_sessions;
166
167   /**
168    * Number of inbound sessions
169    */
170   unsigned int inbound_sessions;
171
172   /**
173    * Plugin HTTPS SSL/TLS options
174    * ----------------------------
175    */
176
177   /**
178    * libCurl TLS crypto init string, can be set to enhance performance
179    *
180    * Example:
181    *
182    * Use RC4-128 instead of AES:
183    * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
184    *
185    */
186   char *crypto_init;
187
188   /**
189    * TLS key
190    */
191   char *key;
192
193   /**
194    * TLS certificate
195    */
196   char *cert;
197
198   /**
199    * Plugin values
200    * -------------
201    */
202
203   /**
204    * Current number of establishes connections
205    */
206   int cur_connections;
207
208   /**
209    * Last used unique HTTP connection tag
210    */
211   uint32_t last_tag;
212
213   /**
214    * Server handles
215    * --------------
216    */
217
218   /**
219    * MHD IPv4 daemon
220    */
221   struct MHD_Daemon *server_v4;
222
223   /**
224    * MHD IPv4 task
225    */
226   GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
227
228   /**
229    * MHD IPv6 daemon
230    */
231   struct MHD_Daemon *server_v6;
232
233   /**
234    * MHD IPv4 task
235    */
236   GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
237
238   /**
239    * IPv4 server socket to bind to
240    */
241   struct sockaddr_in *server_addr_v4;
242
243   /**
244    * IPv6 server socket to bind to
245    */
246   struct sockaddr_in6 *server_addr_v6;
247
248   /**
249    * Server semi connections
250    * A full session consists of 2 semi-connections: send and receive
251    * If not both directions are established the server keeps this sessions here
252    */
253   struct Session *server_semi_head;
254
255   struct Session *server_semi_tail;
256
257   /*
258    * Client handles
259    */
260
261   /**
262    * cURL Multihandle
263    */
264   CURLM *client_mh;
265
266   /**
267    * curl perform task
268    */
269   GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
270
271 };
272
273 GNUNET_NETWORK_STRUCT_BEGIN
274
275 /**
276  * IPv4 addresses
277  */
278 struct IPv4HttpAddress
279 {
280   /**
281    * IPv4 address, in network byte order.
282    */
283   uint32_t ipv4_addr GNUNET_PACKED;
284
285   /**
286    * Port number, in network byte order.
287    */
288   uint16_t u4_port GNUNET_PACKED;
289 };
290
291 /**
292  * IPv4 addresses
293  */
294 struct IPv6HttpAddress
295 {
296   /**
297    * IPv6 address.
298    */
299   struct in6_addr ipv6_addr GNUNET_PACKED;
300
301   /**
302    * Port number, in network byte order.
303    */
304   uint16_t u6_port GNUNET_PACKED;
305 };
306 GNUNET_NETWORK_STRUCT_END
307
308 /**
309  * Session handle for connections.
310  */
311 struct Session
312 {
313
314   /**
315    * Stored in a linked list.
316    */
317   struct Session *next;
318
319   /**
320    * Stored in a linked list.
321    */
322   struct Session *prev;
323
324   /**
325    * Pointer to the global plugin struct.
326    */
327   struct Plugin *plugin;
328
329   /**
330    * Address
331    */
332   void *addr;
333
334   /**
335    * Address length
336    */
337   size_t addrlen;
338
339   /**
340    * ATS network type in NBO
341    */
342   uint32_t ats_address_network_type;
343
344   /**
345    * To whom are we talking to
346    */
347   struct GNUNET_PeerIdentity target;
348
349   /**
350    * next pointer for double linked list
351    */
352   struct HTTP_Message *msg_head;
353
354   /**
355    * previous pointer for double linked list
356    */
357   struct HTTP_Message *msg_tail;
358
359
360   /**
361    * Message stream tokenizer for incoming data
362    */
363   struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
364
365   /**
366    * Absolute time when to receive data again
367    * Used for receive throttling
368    */
369   struct GNUNET_TIME_Absolute next_receive;
370
371   /**
372    * Inbound or outbound connection
373    * Outbound: GNUNET_NO (client is used to send and receive)
374    * Inbound : GNUNET_YES (server is used to send and receive)
375    */
376   int inbound;
377
378   /**
379    * Unique HTTP/S connection tag for this connection
380    */
381   uint32_t tag;
382
383   /**
384    * Client handles
385    */
386
387   /**
388    * Client send handle
389    */
390   void *client_put;
391
392   /**
393    * Client receive handle
394    */
395   void *client_get;
396
397   /**
398    * Task to wake up client receive handle when receiving is allowed again
399    */
400   GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
401
402   /**
403    * Session timeout task
404    */
405   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
406
407   /**
408    * Is client send handle paused since there are no data to send?
409    * GNUNET_YES/NO
410    */
411   int client_put_paused;
412
413   /**
414    * Server handles
415    */
416
417   /**
418    * Client send handle
419    */
420   void *server_recv;
421
422   /**
423    * Client send handle
424    */
425   void *server_send;
426 };
427
428 /**
429  *  Message to send using http
430  */
431 struct HTTP_Message
432 {
433   /**
434    * next pointer for double linked list
435    */
436   struct HTTP_Message *next;
437
438   /**
439    * previous pointer for double linked list
440    */
441   struct HTTP_Message *prev;
442
443   /**
444    * buffer containing data to send
445    */
446   char *buf;
447
448   /**
449    * amount of data already sent
450    */
451   size_t pos;
452
453   /**
454    * buffer length
455    */
456   size_t size;
457
458   /**
459    * Continuation function to call once the transmission buffer
460    * has again space available.  NULL if there is no
461    * continuation to call.
462    */
463   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
464
465   /**
466    * Closure for transmit_cont.
467    */
468   void *transmit_cont_cls;
469 };
470
471 struct Session *
472 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
473                 const void *addr, size_t addrlen);
474
475 int
476 exist_session (struct Plugin *plugin, struct Session *s);
477
478 void
479 delete_session (struct Session *s);
480
481 int
482 exist_session (struct Plugin *plugin, struct Session *s);
483
484 struct GNUNET_TIME_Relative
485 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
486                      const struct GNUNET_MessageHeader *message,
487                      struct Session *session, const char *sender_address,
488                      uint16_t sender_address_len);
489
490 const char *
491 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
492
493 int
494 client_disconnect (struct Session *s);
495
496 int
497 client_connect (struct Session *s);
498
499 int
500 client_send (struct Session *s, struct HTTP_Message *msg);
501
502 int
503 client_start (struct Plugin *plugin);
504
505 void
506 client_stop (struct Plugin *plugin);
507
508 int
509 server_disconnect (struct Session *s);
510
511 int
512 server_send (struct Session *s, struct HTTP_Message *msg);
513
514 int
515 server_start (struct Plugin *plugin);
516
517 void
518 server_stop (struct Plugin *plugin);
519
520 void
521 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
522                     struct Session *s);
523
524 /* end of plugin_transport_http.h */