gnunet_nat_service.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2007-2017 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. * @author Christian Grothoff
  18. * @author Milan Bouchet-Valat
  19. *
  20. * @file
  21. * Service for handling UPnP and NAT-PMP port forwarding
  22. * and external IP address retrieval
  23. *
  24. * @defgroup nat NAT library
  25. * Service for handling UPnP and NAT-PMP port forwarding
  26. * and external IP address retrieval
  27. *
  28. * @{
  29. */
  30. #ifndef GNUNET_NAT_SERVICE_H
  31. #define GNUNET_NAT_SERVICE_H
  32. #include "gnunet_util_lib.h"
  33. /**
  34. * Some addresses contain sensitive information or are
  35. * not suitable for global distribution. We use address
  36. * classes to filter addresses by which domain they make
  37. * sense to be used in. These are used in a bitmask.
  38. *
  39. * FIXME: might want to define this elsewhere; we have
  40. * an equivalent enum in gnunet_transport_hello_service.h;
  41. * might ultimately belong with the new HELLO definition.
  42. */
  43. enum GNUNET_NAT_AddressClass
  44. {
  45. /**
  46. * No address.
  47. */
  48. GNUNET_NAT_AC_NONE = 0,
  49. /**
  50. * Addresses that fall into no other category
  51. * (i.e. incoming which we cannot use elsewhere).
  52. */
  53. GNUNET_NAT_AC_OTHER = 1,
  54. /**
  55. * Flag for addresses that are highly sensitive
  56. * (i.e. IPv6 with our MAC).
  57. */
  58. GNUNET_NAT_AC_PRIVATE = 2,
  59. /**
  60. * Addresses that are global (i.e. IPv4).
  61. */
  62. GNUNET_NAT_AC_GLOBAL = 4,
  63. /**
  64. * Addresses that are global and are sensitive
  65. * (i.e. IPv6 with our MAC).
  66. */
  67. GNUNET_NAT_AC_GLOBAL_PRIVATE = 6,
  68. /**
  69. * Addresses useful in the local wired network,
  70. * i.e. a MAC. Sensitive, but obvious to people nearby.
  71. *
  72. * Useful for broadcasts.
  73. */
  74. GNUNET_NAT_AC_LAN = 8,
  75. /**
  76. * Addresses useful in the local wired network,
  77. * i.e. a MAC. Sensitive, but obvious to people nearby.
  78. * Useful for broadcasts.
  79. */
  80. GNUNET_NAT_AC_LAN_PRIVATE = 10,
  81. /**
  82. * Addresses useful in the local wireless network,
  83. * i.e. a MAC. Sensitive, but obvious to people nearby.
  84. * Useful for broadcasts.
  85. */
  86. GNUNET_NAT_AC_WLAN = 16,
  87. /**
  88. * Addresses useful in the local bluetooth network. Sensitive, but
  89. * obvious to people nearby. Useful for broadcasts.
  90. */
  91. GNUNET_NAT_AC_BT = 32,
  92. /**
  93. * Loopback addresses, only useful under special cirumstances.
  94. */
  95. GNUNET_NAT_AC_LOOPBACK = 64,
  96. /**
  97. * Addresses that should be our external IP address
  98. * on the outside of a NAT. Might be incorrectly determined.
  99. * Used as a bit in combination with #GNUNET_NAT_AC_GLOBAL,
  100. * or in case of double-NAT with
  101. * #GNUNET_NAT_AC_LAN.
  102. */
  103. GNUNET_NAT_AC_EXTERN = 128,
  104. /**
  105. * Addresses that were manually configured by the user.
  106. * Used as a bit in combination with #GNUNET_NAT_AC_GLOBAL.
  107. */
  108. GNUNET_NAT_AC_MANUAL = 256,
  109. /**
  110. * Bitmask for "any" address.
  111. */
  112. GNUNET_NAT_AC_ANY = 65535
  113. };
  114. /**
  115. * Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
  116. */
  117. enum GNUNET_NAT_StatusCode
  118. {
  119. /**
  120. * Just the default
  121. */
  122. GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
  123. /**
  124. * IPC Failure
  125. */
  126. GNUNET_NAT_ERROR_IPC_FAILURE,
  127. /**
  128. * Failure in network subsystem, check permissions
  129. */
  130. GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR,
  131. /**
  132. * test timed out
  133. */
  134. GNUNET_NAT_ERROR_TIMEOUT,
  135. /**
  136. * detected that we are offline
  137. */
  138. GNUNET_NAT_ERROR_NOT_ONLINE,
  139. /**
  140. * `upnpc` command not found
  141. */
  142. GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
  143. /**
  144. * Failed to run `upnpc` command
  145. */
  146. GNUNET_NAT_ERROR_UPNPC_FAILED,
  147. /**
  148. * `upnpc' command took too long, process killed
  149. */
  150. GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
  151. /**
  152. * `upnpc' command failed to establish port mapping
  153. */
  154. GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
  155. /**
  156. * `external-ip' command not found
  157. */
  158. GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
  159. /**
  160. * Failed to run `external-ip` command
  161. */
  162. GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED,
  163. /**
  164. * `external-ip' command output invalid
  165. */
  166. GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
  167. /**
  168. * "no valid address was returned by `external-ip'"
  169. */
  170. GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID,
  171. /**
  172. * Could not determine interface with internal/local network address
  173. */
  174. GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO,
  175. /**
  176. * No working gnunet-helper-nat-server found
  177. */
  178. GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND,
  179. /**
  180. * NAT test could not be initialized
  181. */
  182. GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
  183. /**
  184. * NAT test timeout
  185. */
  186. GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT,
  187. /**
  188. * NAT test failed to initiate
  189. */
  190. GNUNET_NAT_ERROR_NAT_REGISTER_FAILED,
  191. /**
  192. *
  193. */
  194. GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND
  195. };
  196. /**
  197. * What the situation of the NAT connectivity
  198. */
  199. enum GNUNET_NAT_Type
  200. {
  201. /**
  202. * We have a direct connection
  203. */
  204. GNUNET_NAT_TYPE_NO_NAT = GNUNET_OK,
  205. /**
  206. * We are under a NAT but cannot traverse it
  207. */
  208. GNUNET_NAT_TYPE_UNREACHABLE_NAT,
  209. /**
  210. * We can traverse using STUN
  211. */
  212. GNUNET_NAT_TYPE_STUN_PUNCHED_NAT,
  213. /**
  214. * We can traverse using UPNP
  215. */
  216. GNUNET_NAT_TYPE_UPNP_NAT,
  217. /**
  218. * We know nothing about the NAT.
  219. */
  220. GNUNET_NAT_TYPE_UNKNOWN
  221. };
  222. /**
  223. * Signature of the callback passed to #GNUNET_NAT_register() for
  224. * a function to call whenever our set of 'valid' addresses changes.
  225. *
  226. * @param cls closure
  227. * @param app_ctx[in,out] location where the app can store stuff
  228. * on add and retrieve it on remove
  229. * @param add_remove #GNUNET_YES to add a new public IP address,
  230. * #GNUNET_NO to remove a previous (now invalid) one
  231. * @param ac address class the address belongs to
  232. * @param addr either the previous or the new public IP address
  233. * @param addrlen actual length of the @a addr
  234. */
  235. typedef void
  236. (*GNUNET_NAT_AddressCallback) (void *cls,
  237. void **app_ctx,
  238. int add_remove,
  239. enum GNUNET_NAT_AddressClass ac,
  240. const struct sockaddr *addr,
  241. socklen_t addrlen);
  242. /**
  243. * Signature of the callback passed to #GNUNET_NAT_register().
  244. * for a function to call whenever someone asks us to do connection
  245. * reversal.
  246. *
  247. * @param cls closure
  248. * @param remote_addr public IP address of the other peer
  249. * @param remote_addrlen actual length of the @a remote_addr
  250. */
  251. typedef void
  252. (*GNUNET_NAT_ReversalCallback) (void *cls,
  253. const struct sockaddr *remote_addr,
  254. socklen_t remote_addrlen);
  255. /**
  256. * Handle for active NAT registrations.
  257. */
  258. struct GNUNET_NAT_Handle;
  259. /**
  260. * Attempt to enable port redirection and detect public IP address
  261. * contacting UPnP or NAT-PMP routers on the local network. Use @a
  262. * addr to specify to which of the local host's addresses should the
  263. * external port be mapped. The port is taken from the corresponding
  264. * sockaddr_in[6] field. The NAT module should call the given @a
  265. * address_callback for any 'plausible' external address.
  266. *
  267. * @param cfg configuration to use
  268. * @param config_section name of the configuration section for options
  269. * @param proto protocol this is about, IPPROTO_TCP or IPPROTO_UDP
  270. * @param num_addrs number of addresses in @a addrs
  271. * @param addrs list of local addresses packets should be redirected to
  272. * @param addrlens actual lengths of the addresses in @a addrs
  273. * @param address_callback function to call everytime the public IP address changes
  274. * @param reversal_callback function to call if someone wants connection reversal from us,
  275. * NULL if connection reversal is not supported
  276. * @param callback_cls closure for callbacks
  277. * @return NULL on error, otherwise handle that can be used to unregister
  278. */
  279. struct GNUNET_NAT_Handle *
  280. GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
  281. const char *config_section,
  282. uint8_t proto,
  283. unsigned int num_addrs,
  284. const struct sockaddr **addrs,
  285. const socklen_t *addrlens,
  286. GNUNET_NAT_AddressCallback address_callback,
  287. GNUNET_NAT_ReversalCallback reversal_callback,
  288. void *callback_cls);
  289. /**
  290. * Test if the given address is (currently) a plausible IP address for
  291. * this peer. Mostly a convenience function so that clients do not
  292. * have to explicitly track all IPs that the #GNUNET_NAT_AddressCallback
  293. * has returned so far.
  294. *
  295. * @param nh the handle returned by register
  296. * @param addr IP address to test (IPv4 or IPv6)
  297. * @param addrlen number of bytes in @a addr
  298. * @return #GNUNET_YES if the address is plausible,
  299. * #GNUNET_NO if the address is not plausible,
  300. * #GNUNET_SYSERR if the address is malformed
  301. */
  302. int
  303. GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
  304. const void *addr,
  305. socklen_t addrlen);
  306. /**
  307. * We learned about a peer (possibly behind NAT) so run the
  308. * gnunet-nat-client to send dummy ICMP responses to cause
  309. * that peer to connect to us (connection reversal).
  310. *
  311. * @param nh handle (used for configuration)
  312. * @param local_sa our local address of the peer (IPv4-only)
  313. * @param remote_sa the remote address of the peer (IPv4-only)
  314. * @return #GNUNET_SYSERR on error,
  315. * #GNUNET_NO if connection reversal is unavailable,
  316. * #GNUNET_OK otherwise (presumably in progress)
  317. */
  318. int
  319. GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
  320. const struct sockaddr_in *local_sa,
  321. const struct sockaddr_in *remote_sa);
  322. /**
  323. * Stop port redirection and public IP address detection for the given
  324. * handle. This frees the handle, after having sent the needed
  325. * commands to close open ports.
  326. *
  327. * @param nh the handle to unregister
  328. */
  329. void
  330. GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh);
  331. /**
  332. * Handle an incoming STUN message. This function is useful as
  333. * some GNUnet service may be listening on a UDP port and might
  334. * thus receive STUN messages while trying to receive other data.
  335. * In this case, this function can be used to process replies
  336. * to STUN requests.
  337. *
  338. * The function does some basic sanity checks on packet size and
  339. * content, try to extract a bit of information.
  340. *
  341. * At the moment this only processes BIND requests, and returns the
  342. * externally visible address of the request to the rest of the
  343. * NAT logic.
  344. *
  345. * @param nh handle to the NAT service
  346. * @param sender_addr address from which we got @a data
  347. * @param sender_addr_len number of bytes in @a sender_addr
  348. * @param data the packet
  349. * @param data_size number of bytes in @a data
  350. * @return #GNUNET_OK on success
  351. * #GNUNET_NO if the packet is not a STUN packet
  352. * #GNUNET_SYSERR on internal error handling the packet
  353. */
  354. int
  355. GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
  356. const struct sockaddr *sender_addr,
  357. size_t sender_addr_len,
  358. const void *data,
  359. size_t data_size);
  360. /**
  361. * Handle to a request given to the resolver. Can be used to cancel
  362. * the request prior to the timeout or successful execution. Also
  363. * used to track our internal state for the request.
  364. */
  365. struct GNUNET_NAT_STUN_Handle;
  366. /**
  367. * Function called to report success or failure for
  368. * NAT configuration test.
  369. *
  370. * @param cls closure
  371. * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
  372. */
  373. typedef void
  374. (*GNUNET_NAT_TestCallback) (void *cls,
  375. enum GNUNET_NAT_StatusCode result);
  376. /**
  377. * Make Generic STUN request. Sends a generic stun request to the
  378. * server specified using the specified socket. If we do this,
  379. * we need to watch for possible responses and call
  380. * #GNUNET_NAT_stun_handle_packet() on incoming packets.
  381. *
  382. * @param server the address of the stun server
  383. * @param port port of the stun server, in host byte order
  384. * @param sock the socket used to send the request, must be a
  385. * UDP socket
  386. * @param cb callback in case of error
  387. * @param cb_cls closure for @a cb
  388. * @return NULL on error
  389. */
  390. struct GNUNET_NAT_STUN_Handle *
  391. GNUNET_NAT_stun_make_request (const char *server,
  392. uint16_t port,
  393. struct GNUNET_NETWORK_Handle *sock,
  394. GNUNET_NAT_TestCallback cb,
  395. void *cb_cls);
  396. /**
  397. * Cancel active STUN request. Frees associated resources
  398. * and ensures that the callback is no longer invoked.
  399. *
  400. * @param rh request to cancel
  401. */
  402. void
  403. GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh);
  404. #endif
  405. /** @} */ /* end of group */
  406. /* end of gnunet_nat_service.h */