plugin_transport_http.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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.h
  18. * @brief http transport service plugin
  19. * @author Matthias Wachs
  20. */
  21. #ifndef PLUGIN_TRANSPORT_HTTP_H
  22. #define PLUGIN_TRANSPORT_HTTP_H
  23. #include "platform.h"
  24. #include "gnunet_common.h"
  25. #include "gnunet_constants.h"
  26. #include "gnunet_protocols.h"
  27. #include "gnunet_connection_lib.h"
  28. #include "gnunet_service_lib.h"
  29. #include "gnunet_statistics_service.h"
  30. #include "gnunet_transport_service.h"
  31. #include "gnunet_resolver_service.h"
  32. #include "gnunet_server_lib.h"
  33. #include "gnunet_container_lib.h"
  34. #include "gnunet_transport_plugin.h"
  35. #include "gnunet_os_lib.h"
  36. #include "gnunet_nat_lib.h"
  37. #include "microhttpd.h"
  38. /* Just included for the right curl.h */
  39. #include "gnunet_curl_lib.h"
  40. #define DEBUG_HTTP GNUNET_EXTRA_LOGGING
  41. #define VERBOSE_SERVER GNUNET_EXTRA_LOGGING
  42. #define VERBOSE_CLIENT GNUNET_EXTRA_LOGGING
  43. #define VERBOSE_CURL GNUNET_NO
  44. #if BUILD_HTTPS
  45. #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
  46. #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
  47. #else
  48. #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
  49. #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
  50. #endif
  51. #define INBOUND GNUNET_YES
  52. #define OUTBOUND GNUNET_NO
  53. #define HTTP_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply ( \
  54. GNUNET_TIME_UNIT_SECONDS, 15)
  55. /**
  56. * Encapsulation of all of the state of the plugin.
  57. */
  58. struct Plugin
  59. {
  60. /**
  61. * Our environment.
  62. */
  63. struct GNUNET_TRANSPORT_PluginEnvironment *env;
  64. /**
  65. * Head of linked list of open sessions.
  66. */
  67. struct GNUNET_ATS_Session *head;
  68. /**
  69. * Tail of linked list of open sessions.
  70. */
  71. struct GNUNET_ATS_Session *tail;
  72. /**
  73. * NAT handle & address management
  74. */
  75. struct GNUNET_NAT_Handle *nat;
  76. /**
  77. * Our own IPv4 addresses DLL head
  78. */
  79. struct HttpAddressWrapper *addr_head;
  80. /**
  81. * Our own IPv4 addresses DLL tail
  82. */
  83. struct HttpAddressWrapper *addr_tail;
  84. /**
  85. * External hostname the plugin can be connected to, can be different to
  86. * the host's FQDN, used e.g. for reverse proxying
  87. */
  88. char *external_hostname;
  89. /**
  90. * External hostname the plugin can be connected to, can be different to
  91. * the host's FQDN, used e.g. for reverse proxying
  92. */
  93. struct HttpAddress *ext_addr;
  94. /**
  95. * External address length
  96. */
  97. size_t ext_addr_len;
  98. /**
  99. * Task calling transport service about external address
  100. */
  101. struct GNUNET_SCHEDULER_Task *notify_ext_task;
  102. /**
  103. * Plugin name.
  104. * Equals configuration section: transport-http, transport-https
  105. */
  106. char *name;
  107. /**
  108. * Plugin protocol
  109. * http, https
  110. */
  111. char *protocol;
  112. /**
  113. * Use IPv4? #GNUNET_YES or #GNUNET_NO
  114. */
  115. int ipv4;
  116. /**
  117. * Use IPv6? #GNUNET_YES or #GNUNET_NO
  118. */
  119. int ipv6;
  120. /**
  121. * Does plugin just use outbound connections and not accept inbound?
  122. */
  123. int client_only;
  124. /**
  125. * Port used
  126. */
  127. uint16_t port;
  128. /**
  129. * Maximum number of sockets the plugin can use
  130. * Each http inbound /outbound connections are two connections
  131. */
  132. int max_connections;
  133. /**
  134. * Number of outbound sessions
  135. */
  136. unsigned int outbound_sessions;
  137. /**
  138. * Number of inbound sessions
  139. */
  140. unsigned int inbound_sessions;
  141. /**
  142. * libCurl TLS crypto init string, can be set to enhance performance
  143. *
  144. * Example:
  145. *
  146. * Use RC4-128 instead of AES:
  147. * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
  148. */
  149. char *crypto_init;
  150. /**
  151. * TLS key
  152. */
  153. char *key;
  154. /**
  155. * TLS certificate
  156. */
  157. char *cert;
  158. /**
  159. * Current number of establishes connections
  160. */
  161. int cur_connections;
  162. /**
  163. * Last used unique HTTP connection tag
  164. */
  165. uint32_t last_tag;
  166. /**
  167. * MHD IPv4 daemon
  168. */
  169. struct MHD_Daemon *server_v4;
  170. /**
  171. * MHD IPv4 task
  172. */
  173. struct GNUNET_SCHEDULER_Task *server_v4_task;
  174. /**
  175. * The IPv4 server is scheduled to run asap
  176. */
  177. int server_v4_immediately;
  178. /**
  179. * MHD IPv6 daemon
  180. */
  181. struct MHD_Daemon *server_v6;
  182. /**
  183. * MHD IPv4 task
  184. */
  185. struct GNUNET_SCHEDULER_Task *server_v6_task;
  186. /**
  187. * The IPv6 server is scheduled to run asap
  188. */
  189. int server_v6_immediately;
  190. /**
  191. * IPv4 server socket to bind to
  192. */
  193. struct sockaddr_in *server_addr_v4;
  194. /**
  195. * IPv6 server socket to bind to
  196. */
  197. struct sockaddr_in6 *server_addr_v6;
  198. /**
  199. * Head of server semi connections
  200. * A full session consists of 2 semi-connections: send and receive
  201. * If not both directions are established the server keeps this sessions here
  202. */
  203. struct GNUNET_ATS_Session *server_semi_head;
  204. /**
  205. * Tail of server semi connections
  206. * A full session consists of 2 semi-connections: send and receive
  207. * If not both directions are established the server keeps this sessions here
  208. */
  209. struct GNUNET_ATS_Session *server_semi_tail;
  210. /**
  211. * cURL Multihandle
  212. */
  213. CURLM *client_mh;
  214. /**
  215. * curl perform task
  216. */
  217. struct GNUNET_SCHEDULER_Task *client_perform_task;
  218. };
  219. GNUNET_NETWORK_STRUCT_BEGIN
  220. /**
  221. * HTTP addresses including a full URI
  222. */
  223. struct HttpAddress
  224. {
  225. /**
  226. * Length of the address following in NBO
  227. */
  228. uint32_t addr_len GNUNET_PACKED;
  229. /**
  230. * Address following
  231. */
  232. void *addr GNUNET_PACKED;
  233. };
  234. /**
  235. * IPv4 addresses
  236. */
  237. struct IPv4HttpAddress
  238. {
  239. /**
  240. * IPv4 address, in network byte order.
  241. */
  242. uint32_t ipv4_addr GNUNET_PACKED;
  243. /**
  244. * Port number, in network byte order.
  245. */
  246. uint16_t u4_port GNUNET_PACKED;
  247. };
  248. /**
  249. * IPv4 addresses
  250. */
  251. struct IPv6HttpAddress
  252. {
  253. /**
  254. * IPv6 address.
  255. */
  256. struct in6_addr ipv6_addr GNUNET_PACKED;
  257. /**
  258. * Port number, in network byte order.
  259. */
  260. uint16_t u6_port GNUNET_PACKED;
  261. };
  262. GNUNET_NETWORK_STRUCT_END
  263. struct ServerRequest
  264. {
  265. /**
  266. * _RECV or _SEND
  267. */
  268. int direction;
  269. /**
  270. * Should this connection get disconnected? #GNUNET_YES / #GNUNET_NO
  271. */
  272. int disconnect;
  273. /**
  274. * The session this server connection belongs to
  275. */
  276. struct GNUNET_ATS_Session *session;
  277. /**
  278. * The MHD connection
  279. */
  280. struct MHD_Connection *mhd_conn;
  281. };
  282. /**
  283. * Session handle for connections.
  284. */
  285. struct GNUNET_ATS_Session
  286. {
  287. /**
  288. * To whom are we talking to
  289. */
  290. struct GNUNET_PeerIdentity target;
  291. /**
  292. * Stored in a linked list.
  293. */
  294. struct GNUNET_ATS_Session *next;
  295. /**
  296. * Stored in a linked list.
  297. */
  298. struct GNUNET_ATS_Session *prev;
  299. /**
  300. * Pointer to the global plugin struct.
  301. */
  302. struct Plugin *plugin;
  303. /**
  304. * Address
  305. */
  306. void *addr;
  307. /**
  308. * Address length
  309. */
  310. size_t addrlen;
  311. /**
  312. * ATS network type in NBO
  313. */
  314. uint32_t ats_address_network_type;
  315. /**
  316. * next pointer for double linked list
  317. */
  318. struct HTTP_Message *msg_head;
  319. /**
  320. * previous pointer for double linked list
  321. */
  322. struct HTTP_Message *msg_tail;
  323. /**
  324. * Message stream tokenizer for incoming data
  325. */
  326. struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
  327. /**
  328. * Absolute time when to receive data again
  329. * Used for receive throttling
  330. */
  331. struct GNUNET_TIME_Absolute next_receive;
  332. /**
  333. * Inbound or outbound connection
  334. * Outbound: #GNUNET_NO (client is used to send and receive)
  335. * Inbound : #GNUNET_YES (server is used to send and receive)
  336. */
  337. int inbound;
  338. /**
  339. * Unique HTTP/S connection tag for this connection
  340. */
  341. uint32_t tag;
  342. /**
  343. * Client send handle
  344. */
  345. void *client_put;
  346. /**
  347. * Client receive handle
  348. */
  349. void *client_get;
  350. /**
  351. * Task to wake up client receive handle when receiving is allowed again
  352. */
  353. struct GNUNET_SCHEDULER_Task *recv_wakeup_task;
  354. /**
  355. * Session timeout task
  356. */
  357. struct GNUNET_SCHEDULER_Task *timeout_task;
  358. /**
  359. * Is client send handle paused since there are no data to send?
  360. * #GNUNET_YES or #GNUNET_NO
  361. */
  362. int client_put_paused;
  363. /**
  364. * Client send handle
  365. */
  366. struct ServerRequest *server_recv;
  367. /**
  368. * Client send handle
  369. */
  370. struct ServerRequest *server_send;
  371. };
  372. /**
  373. * Message to send using http
  374. */
  375. struct HTTP_Message
  376. {
  377. /**
  378. * next pointer for double linked list
  379. */
  380. struct HTTP_Message *next;
  381. /**
  382. * previous pointer for double linked list
  383. */
  384. struct HTTP_Message *prev;
  385. /**
  386. * buffer containing data to send
  387. */
  388. char *buf;
  389. /**
  390. * amount of data already sent
  391. */
  392. size_t pos;
  393. /**
  394. * buffer length
  395. */
  396. size_t size;
  397. /**
  398. * Continuation function to call once the transmission buffer
  399. * has again space available. NULL if there is no
  400. * continuation to call.
  401. */
  402. GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
  403. /**
  404. * Closure for @e transmit_cont.
  405. */
  406. void *transmit_cont_cls;
  407. };
  408. struct GNUNET_ATS_Session *
  409. create_session (struct Plugin *plugin,
  410. const struct GNUNET_PeerIdentity *target,
  411. const void *addr,
  412. size_t addrlen);
  413. int
  414. exist_session (struct Plugin *plugin,
  415. struct GNUNET_ATS_Session *s);
  416. void
  417. delete_session (struct GNUNET_ATS_Session *s);
  418. int
  419. exist_session (struct Plugin *plugin,
  420. struct GNUNET_ATS_Session *s);
  421. struct GNUNET_TIME_Relative
  422. http_plugin_receive (void *cls,
  423. const struct GNUNET_PeerIdentity *peer,
  424. const struct GNUNET_MessageHeader *message,
  425. struct GNUNET_ATS_Session *session,
  426. const char *sender_address,
  427. uint16_t sender_address_len);
  428. const char *
  429. http_plugin_address_to_string (void *cls,
  430. const void *addr,
  431. size_t addrlen);
  432. int
  433. client_disconnect (struct GNUNET_ATS_Session *s);
  434. int
  435. client_connect (struct GNUNET_ATS_Session *s);
  436. int
  437. client_send (struct GNUNET_ATS_Session *s, struct HTTP_Message *msg);
  438. int
  439. client_start (struct Plugin *plugin);
  440. void
  441. client_stop (struct Plugin *plugin);
  442. int
  443. server_disconnect (struct GNUNET_ATS_Session *s);
  444. int
  445. server_send (struct GNUNET_ATS_Session *s, struct HTTP_Message *msg);
  446. int
  447. server_start (struct Plugin *plugin);
  448. void
  449. server_stop (struct Plugin *plugin);
  450. void
  451. notify_session_end (void *cls,
  452. const struct GNUNET_PeerIdentity *peer,
  453. struct GNUNET_ATS_Session *s);
  454. /*#ifndef PLUGIN_TRANSPORT_HTTP_H*/
  455. #endif
  456. /* end of plugin_transport_http.h */