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