b7b89e6e67f8429cfc789b248ced5ed40a6bb2a7
[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 #define INBOUND  GNUNET_YES
58 #define OUTBOUND GNUNET_NO
59
60
61 #define HTTP_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
62
63 /**
64  * Encapsulation of all of the state of the plugin.
65  */
66 struct Plugin
67 {
68   /**
69    * Our environment.
70    */
71   struct GNUNET_TRANSPORT_PluginEnvironment *env;
72
73   /**
74    * List of open sessions.
75    */
76   struct Session *head;
77
78   struct Session *tail;
79
80   /**
81    * NAT handle & address management
82    */
83   struct GNUNET_NAT_Handle *nat;
84
85
86   /**
87    * ipv4 DLL head
88    */
89   struct IPv4HttpAddressWrapper *ipv4_addr_head;
90
91   /**
92    * ipv4 DLL tail
93    */
94   struct IPv4HttpAddressWrapper *ipv4_addr_tail;
95
96   /**
97    * ipv6 DLL head
98    */
99   struct IPv6HttpAddressWrapper *ipv6_addr_head;
100
101   /**
102    * ipv6 DLL tail
103    */
104   struct IPv6HttpAddressWrapper *ipv6_addr_tail;
105
106
107   /* Plugin configuration */
108
109   char *name;
110
111   char *protocol;
112
113   int ipv4;
114
115   int ipv6;
116
117   uint16_t port;
118
119   int max_connections;
120
121
122
123   /* Plugin values */
124
125
126   int cur_connections;
127   uint32_t last_tag;
128   /*
129    * Server handles
130    */
131
132   struct MHD_Daemon *server_v4;
133   GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
134
135   struct MHD_Daemon *server_v6;
136   GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
137
138   char *crypto_init;
139   char *key;
140   char *cert;
141
142   struct Session *server_semi_head;
143
144   struct Session *server_semi_tail;
145
146
147
148   /*
149    * Client handles
150    */
151
152   /**
153    * cURL Multihandle
154    */
155   CURLM *client_mh;
156
157   GNUNET_SCHEDULER_TaskIdentifier client_perform_task;
158
159 };
160
161 /**
162  * Session handle for connections.
163  */
164 struct Session
165 {
166
167   /**
168    * Stored in a linked list.
169    */
170   struct Session *next;
171
172   /**
173    * Stored in a linked list.
174    */
175   struct Session *prev;
176
177   /**
178    * Pointer to the global plugin struct.
179    */
180   struct Plugin *plugin;
181
182   /**
183    * message stream tokenizer for incoming data
184    */
185   struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
186
187   /**
188    * Continuation function to call once the transmission buffer
189    * has again space available.  NULL if there is no
190    * continuation to call.
191    */
192   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
193
194
195   void *addr;
196
197   size_t addrlen;
198
199   /**
200    * Closure for transmit_cont.
201    */
202   void *transmit_cont_cls;
203
204   /**
205    * To whom are we talking to (set to our identity
206    * if we are still waiting for the welcome message)
207    */
208   struct GNUNET_PeerIdentity target;
209
210   /**
211    * At what time did we reset last_received last?
212    */
213   //struct GNUNET_TIME_Absolute last_quota_update;
214
215   /**
216    * How many bytes have we received since the "last_quota_update"
217    * timestamp?
218    */
219   //uint64_t last_received;
220
221   /**
222    * Number of bytes per ms that this peer is allowed
223    * to send to us.
224    */
225   //uint32_t quota;
226
227
228   int inbound;
229
230   void *client_put;
231   void *client_get;
232
233   void *server_recv;
234   void *server_send;
235   struct GNUNET_TIME_Absolute delay;
236   GNUNET_SCHEDULER_TaskIdentifier reset_task;
237   uint32_t tag;
238
239 };
240
241 void
242 delete_session (struct Session *s);
243
244 struct Session *
245 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
246                 const void *addr, size_t addrlen,
247                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls);
248
249 const char *
250 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
251
252 int
253 client_disconnect (struct Session *s);
254
255 int
256 client_connect (struct Session *s);
257
258 int
259 client_send (struct Session *s, const char *msgbuf, size_t msgbuf_size);
260
261 int
262 client_start (struct Plugin *plugin);
263
264 void
265 client_stop (struct Plugin *plugin);
266
267 int
268 server_disconnect (struct Session *s);
269
270 int
271 server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size);
272
273 int
274 server_start (struct Plugin *plugin);
275
276 void
277 server_stop (struct Plugin *plugin);
278
279 void
280 notify_session_end (void *cls,
281                     const struct GNUNET_PeerIdentity *
282                     peer, struct Session * s);
283
284 /* end of plugin_transport_http.h */