net_socket.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. net_socket.c -- Handle various kinds of sockets.
  3. Copyright (C) 1998-2005 Ivo Timmermans,
  4. 2000-2017 Guus Sliepen <guus@tinc-vpn.org>
  5. 2006 Scott Lamb <slamb@slamb.org>
  6. 2009 Florian Forster <octo@verplant.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along
  16. with this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "system.h"
  20. #include "avl_tree.h"
  21. #include "conf.h"
  22. #include "connection.h"
  23. #include "event.h"
  24. #include "logger.h"
  25. #include "meta.h"
  26. #include "net.h"
  27. #include "netutl.h"
  28. #include "protocol.h"
  29. #include "proxy.h"
  30. #include "utils.h"
  31. #include "xalloc.h"
  32. /* Needed on Mac OS/X */
  33. #ifndef SOL_TCP
  34. #define SOL_TCP IPPROTO_TCP
  35. #endif
  36. int addressfamily = AF_UNSPEC;
  37. int mintimeout = 0;
  38. int maxtimeout = 900;
  39. int seconds_till_retry = 5;
  40. int udp_rcvbuf = 0;
  41. int udp_sndbuf = 0;
  42. listen_socket_t listen_socket[MAXSOCKETS];
  43. int listen_sockets;
  44. list_t *outgoing_list = NULL;
  45. /* Setup sockets */
  46. static void configure_tcp(connection_t *c) {
  47. int option;
  48. #ifdef O_NONBLOCK
  49. int flags = fcntl(c->socket, F_GETFL);
  50. if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
  51. logger(LOG_ERR, "fcntl for %s: %s", c->hostname, strerror(errno));
  52. }
  53. #elif defined(WIN32)
  54. unsigned long arg = 1;
  55. if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
  56. logger(LOG_ERR, "ioctlsocket for %s: %s", c->hostname, sockstrerror(sockerrno));
  57. }
  58. #endif
  59. #if defined(SOL_TCP) && defined(TCP_NODELAY)
  60. option = 1;
  61. setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&option, sizeof(option));
  62. #endif
  63. #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
  64. option = IPTOS_LOWDELAY;
  65. setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&option, sizeof(option));
  66. #endif
  67. #if defined(IPV6_TCLASS) && defined(IPTOS_LOWDELAY)
  68. option = IPTOS_LOWDELAY;
  69. setsockopt(c->socket, IPPROTO_IPV6, IPV6_TCLASS, (void *)&option, sizeof(option));
  70. #endif
  71. }
  72. static bool bind_to_interface(int sd) {
  73. char *iface;
  74. #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
  75. struct ifreq ifr;
  76. int status;
  77. #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */
  78. if(!get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
  79. return true;
  80. }
  81. #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
  82. memset(&ifr, 0, sizeof(ifr));
  83. strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
  84. ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
  85. free(iface);
  86. status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
  87. if(status) {
  88. logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(errno));
  89. return false;
  90. }
  91. #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
  92. logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
  93. #endif
  94. return true;
  95. }
  96. int setup_listen_socket(const sockaddr_t *sa) {
  97. int nfd;
  98. char *addrstr;
  99. int option;
  100. char *iface;
  101. nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
  102. if(nfd < 0) {
  103. ifdebug(STATUS) logger(LOG_ERR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
  104. return -1;
  105. }
  106. #ifdef FD_CLOEXEC
  107. fcntl(nfd, F_SETFD, FD_CLOEXEC);
  108. #endif
  109. /* Optimize TCP settings */
  110. option = 1;
  111. setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
  112. #if defined(IPV6_V6ONLY)
  113. if(sa->sa.sa_family == AF_INET6) {
  114. setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
  115. }
  116. #else
  117. #warning IPV6_V6ONLY not defined
  118. #endif
  119. if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
  120. #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
  121. struct ifreq ifr;
  122. memset(&ifr, 0, sizeof(ifr));
  123. strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
  124. ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
  125. free(iface);
  126. if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) {
  127. closesocket(nfd);
  128. logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(sockerrno));
  129. return -1;
  130. }
  131. #else
  132. logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
  133. #endif
  134. }
  135. if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
  136. closesocket(nfd);
  137. addrstr = sockaddr2hostname(sa);
  138. logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
  139. free(addrstr);
  140. return -1;
  141. }
  142. if(listen(nfd, 3)) {
  143. closesocket(nfd);
  144. logger(LOG_ERR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
  145. return -1;
  146. }
  147. return nfd;
  148. }
  149. int setup_vpn_in_socket(const sockaddr_t *sa) {
  150. int nfd;
  151. char *addrstr;
  152. int option;
  153. nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
  154. if(nfd < 0) {
  155. logger(LOG_ERR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
  156. return -1;
  157. }
  158. #ifdef FD_CLOEXEC
  159. fcntl(nfd, F_SETFD, FD_CLOEXEC);
  160. #endif
  161. #ifdef O_NONBLOCK
  162. {
  163. int flags = fcntl(nfd, F_GETFL);
  164. if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
  165. closesocket(nfd);
  166. logger(LOG_ERR, "System call `%s' failed: %s", "fcntl",
  167. strerror(errno));
  168. return -1;
  169. }
  170. }
  171. #elif defined(WIN32)
  172. {
  173. unsigned long arg = 1;
  174. if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
  175. closesocket(nfd);
  176. logger(LOG_ERR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
  177. return -1;
  178. }
  179. }
  180. #endif
  181. option = 1;
  182. setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
  183. setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
  184. if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf))) {
  185. logger(LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, strerror(errno));
  186. }
  187. if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf))) {
  188. logger(LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, strerror(errno));
  189. }
  190. #if defined(IPV6_V6ONLY)
  191. if(sa->sa.sa_family == AF_INET6) {
  192. setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
  193. }
  194. #endif
  195. #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
  196. #define IP_DONTFRAGMENT IP_DONTFRAG
  197. #endif
  198. #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
  199. if(myself->options & OPTION_PMTU_DISCOVERY) {
  200. option = IP_PMTUDISC_DO;
  201. setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
  202. }
  203. #elif defined(IP_DONTFRAGMENT)
  204. if(myself->options & OPTION_PMTU_DISCOVERY) {
  205. option = 1;
  206. setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
  207. }
  208. #endif
  209. #if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
  210. if(myself->options & OPTION_PMTU_DISCOVERY) {
  211. option = IPV6_PMTUDISC_DO;
  212. setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
  213. }
  214. #elif defined(IPV6_DONTFRAG)
  215. if(myself->options & OPTION_PMTU_DISCOVERY) {
  216. option = 1;
  217. setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
  218. }
  219. #endif
  220. if(!bind_to_interface(nfd)) {
  221. closesocket(nfd);
  222. return -1;
  223. }
  224. if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
  225. closesocket(nfd);
  226. addrstr = sockaddr2hostname(sa);
  227. logger(LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
  228. free(addrstr);
  229. return -1;
  230. }
  231. return nfd;
  232. } /* int setup_vpn_in_socket */
  233. void retry_outgoing(outgoing_t *outgoing) {
  234. outgoing->timeout += 5;
  235. if(outgoing->timeout < mintimeout) {
  236. outgoing->timeout = mintimeout;
  237. }
  238. if(outgoing->timeout > maxtimeout) {
  239. outgoing->timeout = maxtimeout;
  240. }
  241. if(outgoing->event) {
  242. event_del(outgoing->event);
  243. }
  244. outgoing->event = new_event();
  245. outgoing->event->handler = (event_handler_t) setup_outgoing_connection;
  246. outgoing->event->time = now + outgoing->timeout;
  247. outgoing->event->data = outgoing;
  248. event_add(outgoing->event);
  249. ifdebug(CONNECTIONS) logger(LOG_NOTICE,
  250. "Trying to re-establish outgoing connection in %d seconds",
  251. outgoing->timeout);
  252. }
  253. void finish_connecting(connection_t *c) {
  254. ifdebug(CONNECTIONS) logger(LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
  255. c->last_ping_time = now;
  256. send_id(c);
  257. }
  258. static void do_outgoing_pipe(connection_t *c, char *command) {
  259. #ifndef HAVE_MINGW
  260. int fd[2];
  261. if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
  262. logger(LOG_ERR, "Could not create socketpair: %s\n", strerror(errno));
  263. return;
  264. }
  265. if(fork()) {
  266. c->socket = fd[0];
  267. close(fd[1]);
  268. ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Using proxy %s", command);
  269. return;
  270. }
  271. close(0);
  272. close(1);
  273. close(fd[0]);
  274. dup2(fd[1], 0);
  275. dup2(fd[1], 1);
  276. close(fd[1]);
  277. // Other filedescriptors should be closed automatically by CLOEXEC
  278. char *host = NULL;
  279. char *port = NULL;
  280. sockaddr2str(&c->address, &host, &port);
  281. setenv("REMOTEADDRESS", host, true);
  282. setenv("REMOTEPORT", port, true);
  283. setenv("NODE", c->name, true);
  284. setenv("NAME", myself->name, true);
  285. if(netname) {
  286. setenv("NETNAME", netname, true);
  287. }
  288. int result = system(command);
  289. if(result < 0) {
  290. logger(LOG_ERR, "Could not execute %s: %s\n", command, strerror(errno));
  291. } else if(result) {
  292. logger(LOG_ERR, "%s exited with non-zero status %d", command, result);
  293. }
  294. exit(result);
  295. #else
  296. logger(LOG_ERR, "Proxy type exec not supported on this platform!");
  297. return;
  298. #endif
  299. }
  300. static bool is_valid_host_port(const char *host, const char *port) {
  301. for(const char *p = host; *p; p++)
  302. if(!isalnum(*p) && *p != '-' && *p != '.') {
  303. return false;
  304. }
  305. for(const char *p = port; *p; p++)
  306. if(!isalnum(*p)) {
  307. return false;
  308. }
  309. return true;
  310. }
  311. void do_outgoing_connection(connection_t *c) {
  312. struct addrinfo *proxyai = NULL;
  313. int result;
  314. if(!c->outgoing) {
  315. logger(LOG_ERR, "do_outgoing_connection() for %s called without c->outgoing", c->name);
  316. abort();
  317. }
  318. begin:
  319. if(!c->outgoing->ai) {
  320. if(!c->outgoing->cfg) {
  321. ifdebug(CONNECTIONS) logger(LOG_ERR, "Could not set up a meta connection to %s",
  322. c->name);
  323. c->status.remove = true;
  324. retry_outgoing(c->outgoing);
  325. c->outgoing = NULL;
  326. return;
  327. }
  328. char *address, *port, *space;
  329. get_config_string(c->outgoing->cfg, &address);
  330. space = strchr(address, ' ');
  331. if(space) {
  332. port = xstrdup(space + 1);
  333. *space = 0;
  334. } else {
  335. if(!get_config_string(lookup_config(c->config_tree, "Port"), &port)) {
  336. port = xstrdup("655");
  337. }
  338. }
  339. c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
  340. // If we cannot resolve the address, maybe we are using a proxy that can?
  341. if(!c->outgoing->ai && proxytype != PROXY_NONE && is_valid_host_port(address, port)) {
  342. memset(&c->address, 0, sizeof(c->address));
  343. c->address.sa.sa_family = AF_UNKNOWN;
  344. c->address.unknown.address = address;
  345. c->address.unknown.port = port;
  346. } else {
  347. free(address);
  348. free(port);
  349. }
  350. c->outgoing->aip = c->outgoing->ai;
  351. c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
  352. if(!c->outgoing->ai && proxytype != PROXY_NONE) {
  353. goto connect;
  354. }
  355. }
  356. if(!c->outgoing->aip) {
  357. if(c->outgoing->ai) {
  358. freeaddrinfo(c->outgoing->ai);
  359. }
  360. c->outgoing->ai = NULL;
  361. goto begin;
  362. }
  363. memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
  364. c->outgoing->aip = c->outgoing->aip->ai_next;
  365. connect:
  366. if(c->hostname) {
  367. free(c->hostname);
  368. }
  369. c->hostname = sockaddr2hostname(&c->address);
  370. ifdebug(CONNECTIONS) logger(LOG_INFO, "Trying to connect to %s (%s)", c->name,
  371. c->hostname);
  372. if(!proxytype) {
  373. c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
  374. } else if(proxytype == PROXY_EXEC) {
  375. c->status.proxy_passed = true;
  376. do_outgoing_pipe(c, proxyhost);
  377. } else {
  378. proxyai = str2addrinfo(proxyhost, proxyport, SOCK_STREAM);
  379. if(!proxyai) {
  380. goto begin;
  381. }
  382. ifdebug(CONNECTIONS) logger(LOG_INFO, "Using proxy at %s port %s", proxyhost, proxyport);
  383. c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
  384. }
  385. if(c->socket == -1) {
  386. ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
  387. goto begin;
  388. }
  389. if(proxytype != PROXY_EXEC) {
  390. configure_tcp(c);
  391. }
  392. #ifdef FD_CLOEXEC
  393. fcntl(c->socket, F_SETFD, FD_CLOEXEC);
  394. #endif
  395. if(proxytype != PROXY_EXEC) {
  396. #if defined(IPV6_V6ONLY)
  397. int option = 1;
  398. if(c->address.sa.sa_family == AF_INET6) {
  399. setsockopt(c->socket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
  400. }
  401. #endif
  402. bind_to_interface(c->socket);
  403. int b = -1;
  404. for(int i = 0; i < listen_sockets; i++) {
  405. if(listen_socket[i].sa.sa.sa_family == c->address.sa.sa_family) {
  406. if(b == -1) {
  407. b = i;
  408. } else {
  409. b = -1;
  410. break;
  411. }
  412. }
  413. }
  414. if(b != -1) {
  415. sockaddr_t sa = listen_socket[b].sa;
  416. if(sa.sa.sa_family == AF_INET) {
  417. sa.in.sin_port = 0;
  418. } else if(sa.sa.sa_family == AF_INET6) {
  419. sa.in6.sin6_port = 0;
  420. }
  421. if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
  422. char *addrstr = sockaddr2hostname(&sa);
  423. logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
  424. free(addrstr);
  425. }
  426. }
  427. }
  428. /* Connect */
  429. if(!proxytype) {
  430. result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
  431. } else if(proxytype == PROXY_EXEC) {
  432. result = 0;
  433. } else {
  434. result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
  435. freeaddrinfo(proxyai);
  436. }
  437. now = time(NULL);
  438. if(result == -1) {
  439. if(sockinprogress(sockerrno)) {
  440. c->last_ping_time = now;
  441. c->status.connecting = true;
  442. return;
  443. }
  444. closesocket(c->socket);
  445. ifdebug(CONNECTIONS) logger(LOG_ERR, "%s: %s", c->hostname, sockstrerror(sockerrno));
  446. goto begin;
  447. }
  448. finish_connecting(c);
  449. return;
  450. }
  451. void setup_outgoing_connection(outgoing_t *outgoing) {
  452. connection_t *c;
  453. node_t *n;
  454. outgoing->event = NULL;
  455. n = lookup_node(outgoing->name);
  456. if(n)
  457. if(n->connection) {
  458. ifdebug(CONNECTIONS) logger(LOG_INFO, "Already connected to %s", outgoing->name);
  459. n->connection->outgoing = outgoing;
  460. return;
  461. }
  462. c = new_connection();
  463. c->name = xstrdup(outgoing->name);
  464. c->outcipher = myself->connection->outcipher;
  465. c->outdigest = myself->connection->outdigest;
  466. c->outmaclength = myself->connection->outmaclength;
  467. c->outcompression = myself->connection->outcompression;
  468. init_configuration(&c->config_tree);
  469. if(!read_connection_config(c)) {
  470. free_connection(c);
  471. outgoing->timeout = maxtimeout;
  472. retry_outgoing(outgoing);
  473. return;
  474. }
  475. outgoing->cfg = lookup_config(c->config_tree, "Address");
  476. if(!outgoing->cfg) {
  477. logger(LOG_ERR, "No address specified for %s", c->name);
  478. free_connection(c);
  479. outgoing->timeout = maxtimeout;
  480. retry_outgoing(outgoing);
  481. return;
  482. }
  483. c->outgoing = outgoing;
  484. c->last_ping_time = now;
  485. connection_add(c);
  486. do_outgoing_connection(c);
  487. }
  488. /*
  489. accept a new tcp connect and create a
  490. new connection
  491. */
  492. bool handle_new_meta_connection(int sock) {
  493. static const int max_accept_burst = 10;
  494. static int last_accept_burst;
  495. static int last_accept_time;
  496. connection_t *c;
  497. sockaddr_t sa;
  498. int fd;
  499. socklen_t len = sizeof(sa);
  500. fd = accept(sock, &sa.sa, &len);
  501. if(fd < 0) {
  502. logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
  503. return false;
  504. }
  505. if(last_accept_time == now) {
  506. last_accept_burst++;
  507. if(last_accept_burst >= max_accept_burst) {
  508. if(last_accept_burst == max_accept_burst) {
  509. ifdebug(CONNECTIONS) logger(LOG_WARNING, "Throttling incoming connections");
  510. }
  511. tarpit(fd);
  512. return false;
  513. }
  514. } else {
  515. last_accept_burst = 0;
  516. last_accept_time = now;
  517. }
  518. sockaddrunmap(&sa);
  519. c = new_connection();
  520. c->name = xstrdup("<unknown>");
  521. c->outcipher = myself->connection->outcipher;
  522. c->outdigest = myself->connection->outdigest;
  523. c->outmaclength = myself->connection->outmaclength;
  524. c->outcompression = myself->connection->outcompression;
  525. c->address = sa;
  526. c->hostname = sockaddr2hostname(&sa);
  527. c->socket = fd;
  528. c->last_ping_time = now;
  529. ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);
  530. configure_tcp(c);
  531. connection_add(c);
  532. c->allow_request = ID;
  533. return true;
  534. }
  535. static void free_outgoing(outgoing_t *outgoing) {
  536. if(outgoing->ai) {
  537. freeaddrinfo(outgoing->ai);
  538. }
  539. if(outgoing->name) {
  540. free(outgoing->name);
  541. }
  542. free(outgoing);
  543. }
  544. void try_outgoing_connections(void) {
  545. static config_t *cfg = NULL;
  546. char *name;
  547. outgoing_t *outgoing;
  548. outgoing_list = list_alloc((list_action_t)free_outgoing);
  549. for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
  550. get_config_string(cfg, &name);
  551. if(!check_id(name)) {
  552. logger(LOG_ERR,
  553. "Invalid name for outgoing connection in %s line %d",
  554. cfg->file, cfg->line);
  555. free(name);
  556. continue;
  557. }
  558. outgoing = xmalloc_and_zero(sizeof(*outgoing));
  559. outgoing->name = name;
  560. list_insert_tail(outgoing_list, outgoing);
  561. setup_outgoing_connection(outgoing);
  562. }
  563. }