plugin_transport_http_common.h 7.5 KB

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