glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / transport / plugin_transport_http.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file transport/plugin_transport_http.h
18  * @brief http transport service plugin
19  * @author Matthias Wachs
20  */
21 #ifndef PLUGIN_TRANSPORT_HTTP_H
22 #define PLUGIN_TRANSPORT_HTTP_H
23
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_connection_lib.h"
29 #include "gnunet_service_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet_transport_plugin.h"
36 #include "gnunet_os_lib.h"
37 #include "gnunet_nat_lib.h"
38 #include "microhttpd.h"
39 #if HAVE_CURL_CURL_H
40 #include <curl/curl.h>
41 #elif HAVE_GNURL_CURL_H
42 #include <gnurl/curl.h>
43 #endif
44
45
46 #define DEBUG_HTTP GNUNET_EXTRA_LOGGING
47 #define VERBOSE_SERVER GNUNET_EXTRA_LOGGING
48 #define VERBOSE_CLIENT GNUNET_EXTRA_LOGGING
49 #define VERBOSE_CURL GNUNET_NO
50
51 #if BUILD_HTTPS
52 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
53 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
54 #else
55 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
56 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
57 #endif
58
59 #define INBOUND  GNUNET_YES
60 #define OUTBOUND GNUNET_NO
61
62
63 #define HTTP_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
64
65 /**
66  * Encapsulation of all of the state of the plugin.
67  */
68 struct Plugin
69 {
70   /**
71    * Our environment.
72    */
73   struct GNUNET_TRANSPORT_PluginEnvironment *env;
74
75   /**
76    * Head of linked list of open sessions.
77    */
78   struct GNUNET_ATS_Session *head;
79
80   /**
81    * Tail of linked list of open sessions.
82    */
83   struct GNUNET_ATS_Session *tail;
84
85   /**
86    * NAT handle & address management
87    */
88   struct GNUNET_NAT_Handle *nat;
89
90   /**
91    * Our own IPv4 addresses DLL head
92    */
93   struct HttpAddressWrapper *addr_head;
94
95   /**
96    * Our own IPv4 addresses DLL tail
97    */
98   struct HttpAddressWrapper *addr_tail;
99
100   /**
101    * External hostname the plugin can be connected to, can be different to
102    * the host's FQDN, used e.g. for reverse proxying
103    */
104   char *external_hostname;
105
106   /**
107    * External hostname the plugin can be connected to, can be different to
108    * the host's FQDN, used e.g. for reverse proxying
109    */
110   struct HttpAddress *ext_addr;
111
112   /**
113    * External address length
114    */
115   size_t ext_addr_len;
116
117   /**
118    * Task calling transport service about external address
119    */
120   struct GNUNET_SCHEDULER_Task * notify_ext_task;
121
122   /**
123    * Plugin name.
124    * Equals configuration section: transport-http, transport-https
125    */
126   char *name;
127
128   /**
129    * Plugin protocol
130    * http, https
131    */
132   char *protocol;
133
134   /**
135    * Use IPv4? #GNUNET_YES or #GNUNET_NO
136    */
137   int ipv4;
138
139   /**
140    * Use IPv6? #GNUNET_YES or #GNUNET_NO
141    */
142   int ipv6;
143
144   /**
145    * Does plugin just use outbound connections and not accept inbound?
146    */
147   int client_only;
148
149   /**
150    * Port used
151    */
152   uint16_t port;
153
154   /**
155    * Maximum number of sockets the plugin can use
156    * Each http inbound /outbound connections are two connections
157    */
158   int max_connections;
159
160   /**
161    * Number of outbound sessions
162    */
163   unsigned int outbound_sessions;
164
165   /**
166    * Number of inbound sessions
167    */
168   unsigned int inbound_sessions;
169
170   /**
171    * libCurl TLS crypto init string, can be set to enhance performance
172    *
173    * Example:
174    *
175    * Use RC4-128 instead of AES:
176    * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
177    */
178   char *crypto_init;
179
180   /**
181    * TLS key
182    */
183   char *key;
184
185   /**
186    * TLS certificate
187    */
188   char *cert;
189
190   /**
191    * Current number of establishes connections
192    */
193   int cur_connections;
194
195   /**
196    * Last used unique HTTP connection tag
197    */
198   uint32_t last_tag;
199
200   /**
201    * MHD IPv4 daemon
202    */
203   struct MHD_Daemon *server_v4;
204
205   /**
206    * MHD IPv4 task
207    */
208   struct GNUNET_SCHEDULER_Task * server_v4_task;
209
210   /**
211    * The IPv4 server is scheduled to run asap
212    */
213   int server_v4_immediately;
214
215   /**
216    * MHD IPv6 daemon
217    */
218   struct MHD_Daemon *server_v6;
219
220   /**
221    * MHD IPv4 task
222    */
223   struct GNUNET_SCHEDULER_Task * server_v6_task;
224
225   /**
226    * The IPv6 server is scheduled to run asap
227    */
228   int server_v6_immediately;
229
230   /**
231    * IPv4 server socket to bind to
232    */
233   struct sockaddr_in *server_addr_v4;
234
235   /**
236    * IPv6 server socket to bind to
237    */
238   struct sockaddr_in6 *server_addr_v6;
239
240   /**
241    * Head of server semi connections
242    * A full session consists of 2 semi-connections: send and receive
243    * If not both directions are established the server keeps this sessions here
244    */
245   struct GNUNET_ATS_Session *server_semi_head;
246
247   /**
248    * Tail of server semi connections
249    * A full session consists of 2 semi-connections: send and receive
250    * If not both directions are established the server keeps this sessions here
251    */
252   struct GNUNET_ATS_Session *server_semi_tail;
253
254   /**
255    * cURL Multihandle
256    */
257   CURLM *client_mh;
258
259   /**
260    * curl perform task
261    */
262   struct GNUNET_SCHEDULER_Task * client_perform_task;
263
264 };
265
266 GNUNET_NETWORK_STRUCT_BEGIN
267
268 /**
269  * HTTP addresses including a full URI
270  */
271 struct HttpAddress
272 {
273   /**
274    * Length of the address following in NBO
275    */
276   uint32_t addr_len GNUNET_PACKED;
277
278   /**
279    * Address following
280    */
281   void *addr GNUNET_PACKED;
282 };
283
284 /**
285  * IPv4 addresses
286  */
287 struct IPv4HttpAddress
288 {
289   /**
290    * IPv4 address, in network byte order.
291    */
292   uint32_t ipv4_addr GNUNET_PACKED;
293
294   /**
295    * Port number, in network byte order.
296    */
297   uint16_t u4_port GNUNET_PACKED;
298 };
299
300 /**
301  * IPv4 addresses
302  */
303 struct IPv6HttpAddress
304 {
305   /**
306    * IPv6 address.
307    */
308   struct in6_addr ipv6_addr GNUNET_PACKED;
309
310   /**
311    * Port number, in network byte order.
312    */
313   uint16_t u6_port GNUNET_PACKED;
314 };
315 GNUNET_NETWORK_STRUCT_END
316
317
318 struct ServerRequest
319 {
320   /**
321    * _RECV or _SEND
322    */
323   int direction;
324
325   /**
326    * Should this connection get disconnected? #GNUNET_YES / #GNUNET_NO
327    */
328   int disconnect;
329
330   /**
331    * The session this server connection belongs to
332    */
333   struct GNUNET_ATS_Session *session;
334
335   /**
336    * The MHD connection
337    */
338   struct MHD_Connection *mhd_conn;
339 };
340
341
342 /**
343  * Session handle for connections.
344  */
345 struct GNUNET_ATS_Session
346 {
347   /**
348    * To whom are we talking to
349    */
350   struct GNUNET_PeerIdentity target;
351
352   /**
353    * Stored in a linked list.
354    */
355   struct GNUNET_ATS_Session *next;
356
357   /**
358    * Stored in a linked list.
359    */
360   struct GNUNET_ATS_Session *prev;
361
362   /**
363    * Pointer to the global plugin struct.
364    */
365   struct Plugin *plugin;
366
367   /**
368    * Address
369    */
370   void *addr;
371
372   /**
373    * Address length
374    */
375   size_t addrlen;
376
377   /**
378    * ATS network type in NBO
379    */
380   uint32_t ats_address_network_type;
381
382   /**
383    * next pointer for double linked list
384    */
385   struct HTTP_Message *msg_head;
386
387   /**
388    * previous pointer for double linked list
389    */
390   struct HTTP_Message *msg_tail;
391
392   /**
393    * Message stream tokenizer for incoming data
394    */
395   struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
396
397   /**
398    * Absolute time when to receive data again
399    * Used for receive throttling
400    */
401   struct GNUNET_TIME_Absolute next_receive;
402
403   /**
404    * Inbound or outbound connection
405    * Outbound: #GNUNET_NO (client is used to send and receive)
406    * Inbound : #GNUNET_YES (server is used to send and receive)
407    */
408   int inbound;
409
410   /**
411    * Unique HTTP/S connection tag for this connection
412    */
413   uint32_t tag;
414
415   /**
416    * Client send handle
417    */
418   void *client_put;
419
420   /**
421    * Client receive handle
422    */
423   void *client_get;
424
425   /**
426    * Task to wake up client receive handle when receiving is allowed again
427    */
428   struct GNUNET_SCHEDULER_Task * recv_wakeup_task;
429
430   /**
431    * Session timeout task
432    */
433   struct GNUNET_SCHEDULER_Task * timeout_task;
434
435   /**
436    * Is client send handle paused since there are no data to send?
437    * #GNUNET_YES or #GNUNET_NO
438    */
439   int client_put_paused;
440
441   /**
442    * Client send handle
443    */
444   struct ServerRequest *server_recv;
445
446   /**
447    * Client send handle
448    */
449   struct ServerRequest *server_send;
450 };
451
452
453 /**
454  *  Message to send using http
455  */
456 struct HTTP_Message
457 {
458   /**
459    * next pointer for double linked list
460    */
461   struct HTTP_Message *next;
462
463   /**
464    * previous pointer for double linked list
465    */
466   struct HTTP_Message *prev;
467
468   /**
469    * buffer containing data to send
470    */
471   char *buf;
472
473   /**
474    * amount of data already sent
475    */
476   size_t pos;
477
478   /**
479    * buffer length
480    */
481   size_t size;
482
483   /**
484    * Continuation function to call once the transmission buffer
485    * has again space available.  NULL if there is no
486    * continuation to call.
487    */
488   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
489
490   /**
491    * Closure for @e transmit_cont.
492    */
493   void *transmit_cont_cls;
494 };
495
496
497 struct GNUNET_ATS_Session *
498 create_session (struct Plugin *plugin,
499                 const struct GNUNET_PeerIdentity *target,
500                 const void *addr,
501                 size_t addrlen);
502
503
504 int
505 exist_session (struct Plugin *plugin,
506                struct GNUNET_ATS_Session *s);
507
508
509 void
510 delete_session (struct GNUNET_ATS_Session *s);
511
512
513 int
514 exist_session (struct Plugin *plugin,
515                struct GNUNET_ATS_Session *s);
516
517
518 struct GNUNET_TIME_Relative
519 http_plugin_receive (void *cls,
520                      const struct GNUNET_PeerIdentity *peer,
521                      const struct GNUNET_MessageHeader *message,
522                      struct GNUNET_ATS_Session *session,
523                      const char *sender_address,
524                      uint16_t sender_address_len);
525
526
527 const char *
528 http_plugin_address_to_string (void *cls,
529                                const void *addr,
530                                size_t addrlen);
531
532
533 int
534 client_disconnect (struct GNUNET_ATS_Session *s);
535
536
537 int
538 client_connect (struct GNUNET_ATS_Session *s);
539
540
541 int
542 client_send (struct GNUNET_ATS_Session *s, struct HTTP_Message *msg);
543
544
545 int
546 client_start (struct Plugin *plugin);
547
548
549 void
550 client_stop (struct Plugin *plugin);
551
552
553 int
554 server_disconnect (struct GNUNET_ATS_Session *s);
555
556
557 int
558 server_send (struct GNUNET_ATS_Session *s, struct HTTP_Message *msg);
559
560
561 int
562 server_start (struct Plugin *plugin);
563
564
565 void
566 server_stop (struct Plugin *plugin);
567
568
569 void
570 notify_session_end (void *cls,
571                     const struct GNUNET_PeerIdentity *peer,
572                     struct GNUNET_ATS_Session *s);
573
574
575 /*#ifndef PLUGIN_TRANSPORT_HTTP_H*/
576 #endif
577 /* end of plugin_transport_http.h */