-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / transport / plugin_transport_http_common.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2002-2014 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file transport/plugin_transport_http_common.c
20  * @brief functionality shared by http client and server transport service plugin
21  * @author Matthias Wachs
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_transport_plugin.h"
27
28 /**
29  * Timeout values for testing
30  */
31 #define TESTING GNUNET_NO
32
33 #if TESTING
34 #define HTTP_SERVER_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
35 #define HTTP_CLIENT_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
36 #define HTTP_CLIENT_SESSION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 7)
37 #define SERVER_SESSION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 7)
38 #define TIMEOUT_LOG GNUNET_ERROR_TYPE_DEBUG
39
40 #else
41
42 #if BUILD_HTTPS
43 #define PROTOCOL "https"
44 #else
45 #define PROTOCOL "http"
46 #endif
47
48 #define HTTP_SERVER_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
49 #define HTTP_CLIENT_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
50 #define HTTP_CLIENT_SESSION_TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
51 #define HTTP_SERVER_SESSION_TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
52 #define TIMEOUT_LOG GNUNET_ERROR_TYPE_DEBUG
53
54 #endif
55
56 #define HTTP_DEFAULT_PORT 80
57 #define HTTPS_DEFAULT_PORT 443
58
59 /**
60  * Bits in the `options` field of HTTP addresses.
61  */
62 enum HttpAddressOptions
63 {
64   /**
65    * No bits set.
66    */
67   HTTP_OPTIONS_NONE = 0,
68
69   /**
70    * Verify X509 server certificate, it should be valid.
71    * (if this bit is not set, it is probably just self-
72    * signed and not expected to be verified).
73    */
74   HTTP_OPTIONS_VERIFY_CERTIFICATE = 1,
75
76   /**
77    * Enable TCP Stealth-style port knocking.
78    */
79   HTTP_OPTIONS_TCP_STEALTH = 2
80 };
81
82
83 GNUNET_NETWORK_STRUCT_BEGIN
84
85 /**
86  * HttpAddress
87  */
88 struct HttpAddress
89 {
90   /**
91    * Address options
92    * see `enum HttpAddressOptions`
93    */
94   uint32_t options GNUNET_PACKED;
95
96   /**
97    * Length of URL located after struct
98    */
99   uint32_t urlen GNUNET_PACKED;
100 };
101
102 GNUNET_NETWORK_STRUCT_END
103
104 /**
105  * Representation of HTTP URL split into its components.
106  */
107 struct SplittedHTTPAddress
108 {
109   char *protocol;
110   char *host;
111   char *path;
112   int port;
113 };
114
115
116 /**
117  * Split an HTTP address into protocol, hostname, port
118  * and path components.
119  */
120 struct SplittedHTTPAddress *
121 http_split_address (const char *addr);
122
123
124 /**
125  * Convert the transports address to a nice, human-readable
126  * format.
127  *
128  * @param cls closure
129  * @param type name of the transport that generated the address
130  * @param addr one of the addresses of the host, NULL for the last address
131  *        the specific address format depends on the transport
132  * @param addrlen length of the address
133  * @param numeric should (IP) addresses be displayed in numeric form?
134  * @param timeout after how long should we give up?
135  * @param asc function to call on each string
136  * @param asc_cls closure for @a asc
137  */
138 void
139 http_common_plugin_address_pretty_printer (void *cls,
140                                            const char *type,
141                                            const void *addr,
142                                            size_t addrlen,
143                                            int numeric,
144                                            struct GNUNET_TIME_Relative timeout,
145                                            GNUNET_TRANSPORT_AddressStringCallback asc,
146                                            void *asc_cls);
147
148
149 /**
150  * Function called for a quick conversion of the binary address to
151  * a numeric address.  Note that the caller must not free the
152  * address and that the next call to this function is allowed
153  * to override the address again.
154  *
155  * @param plugin name of the plugin
156  * @param addr binary address
157  * @param addrlen length of @a addr
158  * @return string representing the same address
159  */
160 const char *
161 http_common_plugin_address_to_string (const char *plugin,
162                                       const void *addr,
163                                       size_t addrlen);
164
165
166 /**
167  * Function called to convert a string address to
168  * a binary address.
169  *
170  * @param cls closure (`struct Plugin*`)
171  * @param addr string address
172  * @param addrlen length of the address
173  * @param buf location to store the buffer
174  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
175  * @param added length of created address
176  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
177  */
178 int
179 http_common_plugin_string_to_address (void *cls,
180                                       const char *addr,
181                                       uint16_t addrlen,
182                                       void **buf,
183                                       size_t *added);
184
185
186 /**
187  * Create a HTTP address from a socketaddr
188  *
189  * @param protocol protocol
190  * @param addr `sockaddr *` address
191  * @param addrlen length of the @a addr
192  * @return the string
193  */
194 struct HttpAddress *
195 http_common_address_from_socket (const char *protocol,
196                                  const struct sockaddr *addr,
197                                  socklen_t addrlen);
198
199
200 /**
201  * Create a socketaddr from a HTTP address
202  *
203  * @param addr a `sockaddr *` address
204  * @param addrlen length of the @a addr
205  * @param res the result:
206  *   #GNUNET_SYSERR, invalid input,
207  *   #GNUNET_YES: could convert to ip,
208  *   #GNUNET_NO: valid input but could not convert to ip (hostname?)
209  * @return the string
210  */
211 struct sockaddr *
212 http_common_socket_from_address (const void *addr,
213                                  size_t addrlen,
214                                  int *res);
215
216
217 const char *
218 http_common_plugin_address_to_url (void *cls,
219                                    const void *addr,
220                                    size_t addrlen);
221
222
223 /**
224  * Get the length of an address
225  *
226  * @param addr address
227  * @return the size
228  */
229 size_t
230 http_common_address_get_size (const struct HttpAddress * addr);
231
232
233 /**
234  * Compare addr1 to addr2
235  *
236  * @param addr1 address1
237  * @param addrlen1 length of @a address1
238  * @param addr2 address2
239  * @param addrlen2 length of @a address2
240  * @return #GNUNET_YES if equal, #GNUNET_NO else
241  */
242 size_t
243 http_common_cmp_addresses (const void *addr1,
244                            size_t addrlen1,
245                            const void *addr2,
246                            size_t addrlen2);
247
248
249 /**
250  * Function obtain the network type for an address.
251  *
252  * @param env the environment
253  * @param address the address
254  * @return the network type
255  */
256 enum GNUNET_ATS_Network_Type
257 http_common_get_network_for_address (struct GNUNET_TRANSPORT_PluginEnvironment *env,
258                                      const struct GNUNET_HELLO_Address *address);
259
260
261 /* end of plugin_transport_http_common.h */