plugin_transport_udp.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2010-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_udp.h
  18. * @brief Implementation of the UDP transport protocol
  19. * @author Christian Grothoff
  20. * @author Nathan Evans
  21. * @author Matthias Wachs
  22. */
  23. #ifndef PLUGIN_TRANSPORT_UDP_H
  24. #define PLUGIN_TRANSPORT_UDP_H
  25. #include "platform.h"
  26. #include "gnunet_hello_lib.h"
  27. #include "gnunet_util_lib.h"
  28. #include "gnunet_fragmentation_lib.h"
  29. #include "gnunet_protocols.h"
  30. #include "gnunet_resolver_service.h"
  31. #include "gnunet_signatures.h"
  32. #include "gnunet_constants.h"
  33. #include "gnunet_statistics_service.h"
  34. #include "gnunet_transport_service.h"
  35. #include "gnunet_transport_plugin.h"
  36. #include "transport.h"
  37. #define LOG(kind, ...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
  38. #define PLUGIN_NAME "udp"
  39. #define DEBUG_UDP GNUNET_NO
  40. #define DEBUG_UDP_BROADCASTING GNUNET_NO
  41. /**
  42. * MTU for fragmentation subsystem. Should be conservative since
  43. * all communicating peers MUST work with this MTU.
  44. */
  45. #define UDP_MTU 1400
  46. GNUNET_NETWORK_STRUCT_BEGIN
  47. /**
  48. * Network format for IPv4 addresses.
  49. */
  50. struct IPv4UdpAddress
  51. {
  52. /**
  53. * Optional options and flags for this address
  54. */
  55. uint32_t options GNUNET_PACKED;
  56. /**
  57. * IPv4 address, in network byte order.
  58. */
  59. uint32_t ipv4_addr GNUNET_PACKED;
  60. /**
  61. * Port number, in network byte order.
  62. */
  63. uint16_t u4_port GNUNET_PACKED;
  64. };
  65. /**
  66. * Network format for IPv6 addresses.
  67. */
  68. struct IPv6UdpAddress
  69. {
  70. /**
  71. * Optional options and flags for this address
  72. */
  73. uint32_t options GNUNET_PACKED;
  74. /**
  75. * IPv6 address.
  76. */
  77. struct in6_addr ipv6_addr GNUNET_PACKED;
  78. /**
  79. * Port number, in network byte order.
  80. */
  81. uint16_t u6_port GNUNET_PACKED;
  82. };
  83. GNUNET_NETWORK_STRUCT_END
  84. /**
  85. * Either an IPv4 or IPv6 UDP address. Note that without a "length",
  86. * one cannot tell which one of the two types this address represents.
  87. */
  88. union UdpAddress
  89. {
  90. /**
  91. * IPv4 case.
  92. */
  93. struct IPv4UdpAddress v4;
  94. /**
  95. * IPv6 case.
  96. */
  97. struct IPv6UdpAddress v6;
  98. };
  99. /**
  100. * Information we track for each message in the queue.
  101. */
  102. struct UDP_MessageWrapper;
  103. /**
  104. * Closure for #append_port().
  105. */
  106. struct PrettyPrinterContext;
  107. /**
  108. * Encapsulation of all of the state of the plugin.
  109. */
  110. struct Plugin
  111. {
  112. /**
  113. * Our environment.
  114. */
  115. struct GNUNET_TRANSPORT_PluginEnvironment *env;
  116. /**
  117. * Session of peers with whom we are currently connected,
  118. * map of peer identity to `struct GNUNET_ATS_Session *`.
  119. */
  120. struct GNUNET_CONTAINER_MultiPeerMap *sessions;
  121. /**
  122. * Heap with all of our defragmentation activities.
  123. */
  124. struct GNUNET_CONTAINER_Heap *defrag_ctxs;
  125. /**
  126. * ID of select task for IPv4
  127. */
  128. struct GNUNET_SCHEDULER_Task *select_task_v4;
  129. /**
  130. * ID of select task for IPv6
  131. */
  132. struct GNUNET_SCHEDULER_Task *select_task_v6;
  133. /**
  134. * Bandwidth tracker to limit global UDP traffic.
  135. */
  136. struct GNUNET_BANDWIDTH_Tracker tracker;
  137. /**
  138. * Address we were told to bind to exclusively (IPv4).
  139. */
  140. char *bind4_address;
  141. /**
  142. * Address we were told to bind to exclusively (IPv6).
  143. */
  144. char *bind6_address;
  145. /**
  146. * Handle to NAT traversal support.
  147. */
  148. struct GNUNET_NAT_Handle *nat;
  149. /**
  150. * Handle to NAT traversal support.
  151. */
  152. struct GNUNET_NAT_STUN_Handle *stun;
  153. /**
  154. * The read socket for IPv4
  155. */
  156. struct GNUNET_NETWORK_Handle *sockv4;
  157. /**
  158. * The read socket for IPv6
  159. */
  160. struct GNUNET_NETWORK_Handle *sockv6;
  161. /**
  162. * Head of DLL of broadcast addresses
  163. */
  164. struct BroadcastAddress *broadcast_tail;
  165. /**
  166. * Tail of DLL of broadcast addresses
  167. */
  168. struct BroadcastAddress *broadcast_head;
  169. /**
  170. * Head of messages in IPv4 queue.
  171. */
  172. struct UDP_MessageWrapper *ipv4_queue_head;
  173. /**
  174. * Tail of messages in IPv4 queue.
  175. */
  176. struct UDP_MessageWrapper *ipv4_queue_tail;
  177. /**
  178. * Head of messages in IPv6 queue.
  179. */
  180. struct UDP_MessageWrapper *ipv6_queue_head;
  181. /**
  182. * Tail of messages in IPv6 queue.
  183. */
  184. struct UDP_MessageWrapper *ipv6_queue_tail;
  185. /**
  186. * Running pretty printers: head
  187. */
  188. struct PrettyPrinterContext *ppc_dll_head;
  189. /**
  190. * Running pretty printers: tail
  191. */
  192. struct PrettyPrinterContext *ppc_dll_tail;
  193. /**
  194. * Function to call about session status changes.
  195. */
  196. GNUNET_TRANSPORT_SessionInfoCallback sic;
  197. /**
  198. * Closure for @e sic.
  199. */
  200. void *sic_cls;
  201. /**
  202. * IPv6 multicast address
  203. */
  204. struct sockaddr_in6 ipv6_multicast_address;
  205. /**
  206. * Broadcast interval
  207. */
  208. struct GNUNET_TIME_Relative broadcast_interval;
  209. /**
  210. * Bytes currently in buffer
  211. */
  212. int64_t bytes_in_buffer;
  213. /**
  214. * Address options
  215. */
  216. uint32_t myoptions;
  217. /**
  218. * Is IPv6 enabled: #GNUNET_YES or #GNUNET_NO
  219. */
  220. int enable_ipv6;
  221. /**
  222. * Is IPv4 enabled: #GNUNET_YES or #GNUNET_NO
  223. */
  224. int enable_ipv4;
  225. /**
  226. * Is broadcasting enabled: #GNUNET_YES or #GNUNET_NO
  227. */
  228. int enable_broadcasting;
  229. /**
  230. * Is receiving broadcasts enabled: #GNUNET_YES or #GNUNET_NO
  231. */
  232. int enable_broadcasting_receiving;
  233. /**
  234. * Port we broadcasting on.
  235. */
  236. uint16_t broadcast_port;
  237. /**
  238. * Port we listen on.
  239. */
  240. uint16_t port;
  241. /**
  242. * Port we advertise on.
  243. */
  244. uint16_t aport;
  245. };
  246. /**
  247. * Function called for a quick conversion of the binary address to
  248. * a numeric address. Note that the caller must not free the
  249. * address and that the next call to this function is allowed
  250. * to override the address again.
  251. *
  252. * @param cls closure
  253. * @param addr binary address (a `union UdpAddress`)
  254. * @param addrlen length of the @a addr
  255. * @return string representing the same address
  256. */
  257. const char *
  258. udp_address_to_string (void *cls,
  259. const void *addr,
  260. size_t addrlen);
  261. /**
  262. * We received a broadcast message. Process it and all subsequent
  263. * messages in the same packet.
  264. *
  265. * @param plugin the UDP plugin
  266. * @param buf the buffer with the message(s)
  267. * @param size number of bytes in @a buf
  268. * @param udp_addr address of the sender
  269. * @param udp_addr_len number of bytes in @a udp_addr
  270. * @param network_type network type of the sender's address
  271. */
  272. void
  273. udp_broadcast_receive (struct Plugin *plugin,
  274. const char *buf,
  275. ssize_t size,
  276. const union UdpAddress *udp_addr,
  277. size_t udp_addr_len,
  278. enum GNUNET_NetworkType network_type);
  279. void
  280. setup_broadcast (struct Plugin *plugin,
  281. struct sockaddr_in6 *server_addrv6,
  282. struct sockaddr_in *server_addrv4);
  283. void
  284. stop_broadcast (struct Plugin *plugin);
  285. /*#ifndef PLUGIN_TRANSPORT_UDP_H*/
  286. #endif
  287. /* end of plugin_transport_udp.h */