nt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2015, 2018 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 nt/nt_api_scanner.c
  18. * @brief LAN interface scanning to determine IPs in LAN
  19. * @author Christian Grothoff
  20. * @author Matthias Wachs
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_nt_lib.h"
  25. /**
  26. * How frequently do we scan the interfaces for changes to the addresses?
  27. */
  28. #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply ( \
  29. GNUNET_TIME_UNIT_MINUTES, 2)
  30. /**
  31. * Convert a `enum GNUNET_NetworkType` to a string
  32. *
  33. * @param net the network type
  34. * @return a string or NULL if invalid
  35. */
  36. const char *
  37. GNUNET_NT_to_string (enum GNUNET_NetworkType net)
  38. {
  39. switch (net)
  40. {
  41. case GNUNET_NT_UNSPECIFIED:
  42. return "UNSPECIFIED";
  43. case GNUNET_NT_LOOPBACK:
  44. return "LOOPBACK";
  45. case GNUNET_NT_LAN:
  46. return "LAN";
  47. case GNUNET_NT_WAN:
  48. return "WAN";
  49. case GNUNET_NT_WLAN:
  50. return "WLAN";
  51. case GNUNET_NT_BT:
  52. return "BLUETOOTH";
  53. default:
  54. return NULL;
  55. }
  56. }
  57. /**
  58. * We keep a list of our local networks so we can answer
  59. * LAN vs. WAN questions. Note: WLAN is not detected yet.
  60. * (maybe we can do that heuristically based on interface
  61. * name in the future?).
  62. */
  63. struct NT_Network
  64. {
  65. /**
  66. * Kept in a DLL.
  67. */
  68. struct NT_Network *next;
  69. /**
  70. * Kept in a DLL.
  71. */
  72. struct NT_Network *prev;
  73. /**
  74. * Network address.
  75. */
  76. struct sockaddr *network;
  77. /**
  78. * Netmask to determine what is in the LAN.
  79. */
  80. struct sockaddr *netmask;
  81. /**
  82. * How long are @e network and @e netmask?
  83. */
  84. socklen_t length;
  85. };
  86. /**
  87. * Handle to the interface scanner.
  88. */
  89. struct GNUNET_NT_InterfaceScanner
  90. {
  91. /**
  92. * Head of LAN networks list.
  93. */
  94. struct NT_Network *net_head;
  95. /**
  96. * Tail of LAN networks list.
  97. */
  98. struct NT_Network *net_tail;
  99. /**
  100. * Task for periodically refreshing our LAN network list.
  101. */
  102. struct GNUNET_SCHEDULER_Task *interface_task;
  103. };
  104. /**
  105. * Delete all entries from the current network list.
  106. *
  107. * @param is scanner to clean up
  108. */
  109. static void
  110. delete_networks (struct GNUNET_NT_InterfaceScanner *is)
  111. {
  112. struct NT_Network *cur;
  113. while (NULL != (cur = is->net_head))
  114. {
  115. GNUNET_CONTAINER_DLL_remove (is->net_head,
  116. is->net_tail,
  117. cur);
  118. GNUNET_free (cur);
  119. }
  120. }
  121. /**
  122. * Function invoked for each interface found. Adds the interface's
  123. * network addresses to the respective DLL, so we can distinguish
  124. * between LAN and WAN.
  125. *
  126. * @param cls closure with the `struct GNUNET_NT_InterfaceScanner`
  127. * @param name name of the interface (can be NULL for unknown)
  128. * @param isDefault is this presumably the default interface
  129. * @param addr address of this interface (can be NULL for unknown or unassigned)
  130. * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
  131. * @param netmask the network mask (can be NULL for unknown or unassigned)
  132. * @param addrlen length of the address
  133. * @return #GNUNET_OK to continue iteration
  134. */
  135. static int
  136. interface_proc (void *cls,
  137. const char *name,
  138. int isDefault,
  139. const struct sockaddr *addr,
  140. const struct sockaddr *broadcast_addr,
  141. const struct sockaddr *netmask,
  142. socklen_t addrlen)
  143. {
  144. struct GNUNET_NT_InterfaceScanner *is = cls;
  145. /* Calculate network */
  146. struct NT_Network *net = NULL;
  147. (void) name;
  148. (void) isDefault;
  149. (void) broadcast_addr;
  150. /* Skipping IPv4 loopback addresses since we have special check */
  151. if (addr->sa_family == AF_INET)
  152. {
  153. const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
  154. if ((a4->sin_addr.s_addr & htonl (0xff000000)) == htonl (0x7f000000))
  155. return GNUNET_OK;
  156. }
  157. /* Skipping IPv6 loopback addresses since we have special check */
  158. if (addr->sa_family == AF_INET6)
  159. {
  160. const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
  161. if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
  162. return GNUNET_OK;
  163. }
  164. if (addr->sa_family == AF_INET)
  165. {
  166. const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
  167. const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
  168. struct sockaddr_in *tmp;
  169. struct sockaddr_in network4;
  170. net = GNUNET_malloc (sizeof(struct NT_Network) + 2 * sizeof(struct
  171. sockaddr_in));
  172. tmp = (struct sockaddr_in *) &net[1];
  173. net->network = (struct sockaddr *) &tmp[0];
  174. net->netmask = (struct sockaddr *) &tmp[1];
  175. net->length = addrlen;
  176. memset (&network4,
  177. 0,
  178. sizeof(network4));
  179. network4.sin_family = AF_INET;
  180. #if HAVE_SOCKADDR_IN_SIN_LEN
  181. network4.sin_len = sizeof(network4);
  182. #endif
  183. network4.sin_addr.s_addr = (addr4->sin_addr.s_addr
  184. & netmask4->sin_addr.s_addr);
  185. GNUNET_memcpy (net->netmask,
  186. netmask4,
  187. sizeof(struct sockaddr_in));
  188. GNUNET_memcpy (net->network,
  189. &network4,
  190. sizeof(struct sockaddr_in));
  191. }
  192. if (addr->sa_family == AF_INET6)
  193. {
  194. const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
  195. const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
  196. struct sockaddr_in6 *tmp;
  197. struct sockaddr_in6 network6;
  198. net = GNUNET_malloc (sizeof(struct NT_Network) + 2 * sizeof(struct
  199. sockaddr_in6));
  200. tmp = (struct sockaddr_in6 *) &net[1];
  201. net->network = (struct sockaddr *) &tmp[0];
  202. net->netmask = (struct sockaddr *) &tmp[1];
  203. net->length = addrlen;
  204. memset (&network6, 0, sizeof(network6));
  205. network6.sin6_family = AF_INET6;
  206. #if HAVE_SOCKADDR_IN_SIN_LEN
  207. network6.sin6_len = sizeof(network6);
  208. #endif
  209. unsigned int c = 0;
  210. uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
  211. uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
  212. uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
  213. for (c = 0; c < 4; c++)
  214. net_elem[c] = addr_elem[c] & mask_elem[c];
  215. GNUNET_memcpy (net->netmask,
  216. netmask6,
  217. sizeof(struct sockaddr_in6));
  218. GNUNET_memcpy (net->network,
  219. &network6,
  220. sizeof(struct sockaddr_in6));
  221. }
  222. if (NULL == net)
  223. return GNUNET_OK; /* odd / unsupported address family */
  224. /* Store in list */
  225. #if VERBOSE_NT
  226. char *netmask = GNUNET_strdup (GNUNET_a2s ((struct sockaddr *) net->netmask,
  227. addrlen));
  228. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  229. "nt",
  230. "Adding network `%s', netmask `%s'\n",
  231. GNUNET_a2s ((struct sockaddr *) net->network,
  232. addrlen),
  233. netmask);
  234. GNUNET_free (netmask);
  235. #endif
  236. GNUNET_CONTAINER_DLL_insert (is->net_head,
  237. is->net_tail,
  238. net);
  239. return GNUNET_OK;
  240. }
  241. /**
  242. * Periodically get list of network addresses from our interfaces.
  243. *
  244. * @param cls closure
  245. */
  246. static void
  247. get_addresses (void *cls)
  248. {
  249. struct GNUNET_NT_InterfaceScanner *is = cls;
  250. is->interface_task = NULL;
  251. delete_networks (is);
  252. GNUNET_OS_network_interfaces_list (&interface_proc,
  253. is);
  254. is->interface_task = GNUNET_SCHEDULER_add_delayed (
  255. INTERFACE_PROCESSING_INTERVAL,
  256. &get_addresses,
  257. is);
  258. }
  259. /**
  260. * Returns where the address is located: LAN or WAN or ...
  261. *
  262. * @param is the interface scanner handle
  263. * @param addr address
  264. * @param addrlen address length
  265. * @return type of the network the address belongs to
  266. */
  267. enum GNUNET_NetworkType
  268. GNUNET_NT_scanner_get_type (struct GNUNET_NT_InterfaceScanner *is,
  269. const struct sockaddr *addr,
  270. socklen_t addrlen)
  271. {
  272. struct NT_Network *cur = is->net_head;
  273. enum GNUNET_NetworkType type = GNUNET_NT_UNSPECIFIED;
  274. switch (addr->sa_family)
  275. {
  276. case AF_UNIX:
  277. type = GNUNET_NT_LOOPBACK;
  278. break;
  279. case AF_INET:
  280. {
  281. const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
  282. if ((a4->sin_addr.s_addr & htonl (0xff000000)) == htonl (0x7f000000))
  283. type = GNUNET_NT_LOOPBACK;
  284. break;
  285. }
  286. case AF_INET6:
  287. {
  288. const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
  289. if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
  290. type = GNUNET_NT_LOOPBACK;
  291. break;
  292. }
  293. default:
  294. GNUNET_break (0);
  295. break;
  296. }
  297. /* Check local networks */
  298. while ((NULL != cur) && (GNUNET_NT_UNSPECIFIED == type))
  299. {
  300. if (addrlen != cur->length)
  301. {
  302. cur = cur->next;
  303. continue;
  304. }
  305. if (addr->sa_family == AF_INET)
  306. {
  307. const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
  308. const struct sockaddr_in *net4 = (const struct
  309. sockaddr_in *) cur->network;
  310. const struct sockaddr_in *mask4 = (const struct
  311. sockaddr_in *) cur->netmask;
  312. if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) ==
  313. net4->sin_addr.s_addr)
  314. type = GNUNET_NT_LAN;
  315. }
  316. if (addr->sa_family == AF_INET6)
  317. {
  318. const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
  319. const struct sockaddr_in6 *net6 = (const struct
  320. sockaddr_in6 *) cur->network;
  321. const struct sockaddr_in6 *mask6 = (const struct
  322. sockaddr_in6 *) cur->netmask;
  323. int res = GNUNET_YES;
  324. int c = 0;
  325. uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
  326. uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
  327. uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
  328. for (c = 0; c < 4; c++)
  329. if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
  330. res = GNUNET_NO;
  331. if (res == GNUNET_YES)
  332. type = GNUNET_NT_LAN;
  333. }
  334. cur = cur->next;
  335. }
  336. /* no local network found for this address, default: WAN */
  337. if (type == GNUNET_NT_UNSPECIFIED)
  338. type = GNUNET_NT_WAN;
  339. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  340. "nt-scanner-api",
  341. "`%s' is in network `%s'\n",
  342. GNUNET_a2s (addr,
  343. addrlen),
  344. GNUNET_NT_to_string (type));
  345. return type;
  346. }
  347. /**
  348. * Initialize the interface scanner.
  349. *
  350. * @return interface scanner
  351. */
  352. struct GNUNET_NT_InterfaceScanner *
  353. GNUNET_NT_scanner_init ()
  354. {
  355. struct GNUNET_NT_InterfaceScanner *is;
  356. is = GNUNET_new (struct GNUNET_NT_InterfaceScanner);
  357. GNUNET_OS_network_interfaces_list (&interface_proc,
  358. is);
  359. is->interface_task = GNUNET_SCHEDULER_add_delayed (
  360. INTERFACE_PROCESSING_INTERVAL,
  361. &get_addresses,
  362. is);
  363. return is;
  364. }
  365. /**
  366. * Client is done with the interface scanner, release resources.
  367. *
  368. * @param is handle to release
  369. */
  370. void
  371. GNUNET_NT_scanner_done (struct GNUNET_NT_InterfaceScanner *is)
  372. {
  373. if (NULL != is->interface_task)
  374. {
  375. GNUNET_SCHEDULER_cancel (is->interface_task);
  376. is->interface_task = NULL;
  377. }
  378. delete_networks (is);
  379. GNUNET_free (is);
  380. }
  381. /* end of nt.c */