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