gnunet-service-nat-auto.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2016, 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. * @file nat-auto/gnunet-service-nat-auto.c
  18. * @brief NAT autoconfiguration service
  19. * @author Christian Grothoff
  20. *
  21. * TODO:
  22. * - merge client handle and autoconfig context
  23. * - implement "more" autoconfig:
  24. * + re-work gnunet-nat-server & integrate!
  25. * + integrate "legacy" code
  26. * + test manually punched NAT (how?)
  27. */
  28. #include "platform.h"
  29. #include <math.h>
  30. #include "gnunet_util_lib.h"
  31. #include "gnunet_protocols.h"
  32. #include "gnunet_signatures.h"
  33. #include "gnunet_nat_service.h"
  34. #include "gnunet_statistics_service.h"
  35. #include "gnunet_resolver_service.h"
  36. #include "nat-auto.h"
  37. #include <gcrypt.h>
  38. /**
  39. * How long do we wait until we forcefully terminate autoconfiguration?
  40. */
  41. #define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  42. /**
  43. * Internal data structure we track for each of our clients.
  44. */
  45. struct ClientHandle
  46. {
  47. /**
  48. * Kept in a DLL.
  49. */
  50. struct ClientHandle *next;
  51. /**
  52. * Kept in a DLL.
  53. */
  54. struct ClientHandle *prev;
  55. /**
  56. * Underlying handle for this client with the service.
  57. */
  58. struct GNUNET_SERVICE_Client *client;
  59. /**
  60. * Message queue for communicating with the client.
  61. */
  62. struct GNUNET_MQ_Handle *mq;
  63. };
  64. /**
  65. * Context for autoconfiguration operations.
  66. */
  67. struct AutoconfigContext
  68. {
  69. /**
  70. * Kept in a DLL.
  71. */
  72. struct AutoconfigContext *prev;
  73. /**
  74. * Kept in a DLL.
  75. */
  76. struct AutoconfigContext *next;
  77. /**
  78. * Which client asked the question.
  79. */
  80. struct ClientHandle *ch;
  81. /**
  82. * Configuration we are creating.
  83. */
  84. struct GNUNET_CONFIGURATION_Handle *c;
  85. /**
  86. * Original configuration (for diffing).
  87. */
  88. struct GNUNET_CONFIGURATION_Handle *orig;
  89. /**
  90. * Timeout task to force termination.
  91. */
  92. struct GNUNET_SCHEDULER_Task *timeout_task;
  93. /**
  94. * #GNUNET_YES if upnpc should be used,
  95. * #GNUNET_NO if upnpc should not be used,
  96. * #GNUNET_SYSERR if we should simply not change the option.
  97. */
  98. int enable_upnpc;
  99. /**
  100. * Status code to return to the client.
  101. */
  102. enum GNUNET_NAT_StatusCode status_code;
  103. /**
  104. * NAT type to return to the client.
  105. */
  106. enum GNUNET_NAT_Type type;
  107. };
  108. /**
  109. * Head of client DLL.
  110. */
  111. static struct ClientHandle *ch_head;
  112. /**
  113. * Tail of client DLL.
  114. */
  115. static struct ClientHandle *ch_tail;
  116. /**
  117. * DLL of our autoconfiguration operations.
  118. */
  119. static struct AutoconfigContext *ac_head;
  120. /**
  121. * DLL of our autoconfiguration operations.
  122. */
  123. static struct AutoconfigContext *ac_tail;
  124. /**
  125. * Handle to our current configuration.
  126. */
  127. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  128. /**
  129. * Handle to the statistics service.
  130. */
  131. static struct GNUNET_STATISTICS_Handle *stats;
  132. /**
  133. * Check validity of #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message
  134. * from client.
  135. *
  136. * @param cls client who sent the message
  137. * @param message the message received
  138. * @return #GNUNET_OK if message is well-formed
  139. */
  140. static int
  141. check_autoconfig_request (void *cls,
  142. const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
  143. {
  144. return GNUNET_OK; /* checked later */
  145. }
  146. /**
  147. * Stop all pending activities with respect to the @a ac
  148. *
  149. * @param ac autoconfiguration to terminate activities for
  150. */
  151. static void
  152. terminate_ac_activities (struct AutoconfigContext *ac)
  153. {
  154. if (NULL != ac->timeout_task)
  155. {
  156. GNUNET_SCHEDULER_cancel (ac->timeout_task);
  157. ac->timeout_task = NULL;
  158. }
  159. }
  160. /**
  161. * Finish handling the autoconfiguration request and send
  162. * the response to the client.
  163. *
  164. * @param cls the `struct AutoconfigContext` to conclude
  165. */
  166. static void
  167. conclude_autoconfig_request (void *cls)
  168. {
  169. struct AutoconfigContext *ac = cls;
  170. struct ClientHandle *ch = ac->ch;
  171. struct GNUNET_NAT_AUTO_AutoconfigResultMessage *arm;
  172. struct GNUNET_MQ_Envelope *env;
  173. size_t c_size;
  174. char *buf;
  175. struct GNUNET_CONFIGURATION_Handle *diff;
  176. ac->timeout_task = NULL;
  177. terminate_ac_activities (ac);
  178. /* Send back response */
  179. diff = GNUNET_CONFIGURATION_get_diff (ac->orig,
  180. ac->c);
  181. buf = GNUNET_CONFIGURATION_serialize (diff,
  182. &c_size);
  183. GNUNET_CONFIGURATION_destroy (diff);
  184. env = GNUNET_MQ_msg_extra (arm,
  185. c_size,
  186. GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
  187. arm->status_code = htonl ((uint32_t) ac->status_code);
  188. arm->type = htonl ((uint32_t) ac->type);
  189. GNUNET_memcpy (&arm[1],
  190. buf,
  191. c_size);
  192. GNUNET_free (buf);
  193. GNUNET_MQ_send (ch->mq,
  194. env);
  195. /* clean up */
  196. GNUNET_CONFIGURATION_destroy (ac->orig);
  197. GNUNET_CONFIGURATION_destroy (ac->c);
  198. GNUNET_CONTAINER_DLL_remove (ac_head,
  199. ac_tail,
  200. ac);
  201. GNUNET_free (ac);
  202. GNUNET_SERVICE_client_continue (ch->client);
  203. }
  204. /**
  205. * Check if all autoconfiguration operations have concluded,
  206. * and if they have, send the result back to the client.
  207. *
  208. * @param ac autoconfiguation context to check
  209. */
  210. static void
  211. check_autoconfig_finished (struct AutoconfigContext *ac)
  212. {
  213. GNUNET_SCHEDULER_cancel (ac->timeout_task);
  214. ac->timeout_task
  215. = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request,
  216. ac);
  217. }
  218. /**
  219. * Update ENABLE_UPNPC configuration option.
  220. *
  221. * @param ac autoconfiguration to update
  222. */
  223. static void
  224. update_enable_upnpc_option (struct AutoconfigContext *ac)
  225. {
  226. switch (ac->enable_upnpc)
  227. {
  228. case GNUNET_YES:
  229. GNUNET_CONFIGURATION_set_value_string (ac->c,
  230. "NAT",
  231. "ENABLE_UPNP",
  232. "YES");
  233. break;
  234. case GNUNET_NO:
  235. GNUNET_CONFIGURATION_set_value_string (ac->c,
  236. "NAT",
  237. "ENABLE_UPNP",
  238. "NO");
  239. break;
  240. case GNUNET_SYSERR:
  241. /* We are unsure, do not change option */
  242. break;
  243. }
  244. }
  245. /**
  246. * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from
  247. * client.
  248. *
  249. * @param cls client who sent the message
  250. * @param message the message received
  251. */
  252. static void
  253. handle_autoconfig_request (void *cls,
  254. const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
  255. {
  256. struct ClientHandle *ch = cls;
  257. size_t left = ntohs (message->header.size) - sizeof (*message);
  258. struct AutoconfigContext *ac;
  259. ac = GNUNET_new (struct AutoconfigContext);
  260. ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
  261. ac->ch = ch;
  262. ac->c = GNUNET_CONFIGURATION_create ();
  263. if (GNUNET_OK !=
  264. GNUNET_CONFIGURATION_deserialize (ac->c,
  265. (const char *) &message[1],
  266. left,
  267. NULL))
  268. {
  269. GNUNET_break (0);
  270. GNUNET_SERVICE_client_drop (ch->client);
  271. GNUNET_CONFIGURATION_destroy (ac->c);
  272. GNUNET_free (ac);
  273. return;
  274. }
  275. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  276. "Received REQUEST_AUTO_CONFIG message from client\n");
  277. GNUNET_CONTAINER_DLL_insert (ac_head,
  278. ac_tail,
  279. ac);
  280. ac->orig
  281. = GNUNET_CONFIGURATION_dup (ac->c);
  282. ac->timeout_task
  283. = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT,
  284. &conclude_autoconfig_request,
  285. ac);
  286. ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
  287. /* Probe for upnpc */
  288. if (GNUNET_SYSERR ==
  289. GNUNET_OS_check_helper_binary ("upnpc",
  290. GNUNET_NO,
  291. NULL))
  292. {
  293. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  294. _("UPnP client `upnpc` command not found, disabling UPnP\n"));
  295. ac->enable_upnpc = GNUNET_NO;
  296. }
  297. else
  298. {
  299. /* We might at some point be behind NAT, try upnpc */
  300. ac->enable_upnpc = GNUNET_YES;
  301. }
  302. update_enable_upnpc_option (ac);
  303. /* Finally, check if we are already done */
  304. check_autoconfig_finished (ac);
  305. }
  306. /**
  307. * Task run during shutdown.
  308. *
  309. * @param cls unused
  310. */
  311. static void
  312. shutdown_task (void *cls)
  313. {
  314. struct AutoconfigContext *ac;
  315. while (NULL != (ac = ac_head))
  316. {
  317. GNUNET_CONTAINER_DLL_remove (ac_head,
  318. ac_tail,
  319. ac);
  320. terminate_ac_activities (ac);
  321. GNUNET_free (ac);
  322. }
  323. if (NULL != stats)
  324. {
  325. GNUNET_STATISTICS_destroy (stats,
  326. GNUNET_NO);
  327. stats = NULL;
  328. }
  329. }
  330. /**
  331. * Setup NAT service.
  332. *
  333. * @param cls closure
  334. * @param c configuration to use
  335. * @param service the initialized service
  336. */
  337. static void
  338. run (void *cls,
  339. const struct GNUNET_CONFIGURATION_Handle *c,
  340. struct GNUNET_SERVICE_Handle *service)
  341. {
  342. cfg = c;
  343. GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
  344. NULL);
  345. stats = GNUNET_STATISTICS_create ("nat-auto",
  346. cfg);
  347. }
  348. /**
  349. * Callback called when a client connects to the service.
  350. *
  351. * @param cls closure for the service
  352. * @param c the new client that connected to the service
  353. * @param mq the message queue used to send messages to the client
  354. * @return a `struct ClientHandle`
  355. */
  356. static void *
  357. client_connect_cb (void *cls,
  358. struct GNUNET_SERVICE_Client *c,
  359. struct GNUNET_MQ_Handle *mq)
  360. {
  361. struct ClientHandle *ch;
  362. ch = GNUNET_new (struct ClientHandle);
  363. ch->mq = mq;
  364. ch->client = c;
  365. GNUNET_CONTAINER_DLL_insert (ch_head,
  366. ch_tail,
  367. ch);
  368. return ch;
  369. }
  370. /**
  371. * Callback called when a client disconnected from the service
  372. *
  373. * @param cls closure for the service
  374. * @param c the client that disconnected
  375. * @param internal_cls a `struct ClientHandle *`
  376. */
  377. static void
  378. client_disconnect_cb (void *cls,
  379. struct GNUNET_SERVICE_Client *c,
  380. void *internal_cls)
  381. {
  382. struct ClientHandle *ch = internal_cls;
  383. GNUNET_CONTAINER_DLL_remove (ch_head,
  384. ch_tail,
  385. ch);
  386. GNUNET_free (ch);
  387. }
  388. /**
  389. * Define "main" method using service macro.
  390. */
  391. GNUNET_SERVICE_MAIN
  392. ("nat-auto",
  393. GNUNET_SERVICE_OPTION_NONE,
  394. &run,
  395. &client_connect_cb,
  396. &client_disconnect_cb,
  397. NULL,
  398. GNUNET_MQ_hd_var_size (autoconfig_request,
  399. GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
  400. struct GNUNET_NAT_AUTO_AutoconfigRequestMessage,
  401. NULL),
  402. GNUNET_MQ_handler_end ());
  403. #if defined(LINUX) && defined(__GLIBC__)
  404. #include <malloc.h>
  405. /**
  406. * MINIMIZE heap size (way below 128k) since this process doesn't need much.
  407. */
  408. void __attribute__ ((constructor))
  409. GNUNET_ARM_memory_init ()
  410. {
  411. mallopt (M_TRIM_THRESHOLD, 4 * 1024);
  412. mallopt (M_TOP_PAD, 1 * 1024);
  413. malloc_trim (0);
  414. }
  415. #endif
  416. /* end of gnunet-service-nat.c */