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