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