net_socket.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
  64. option = IPTOS_LOWDELAY;
  65. setsockopt(c->socket, SOL_IP, IP_TOS, (void *)&option, sizeof(option));
  66. #endif
  67. #if defined(IPPROTO_IPV6) && 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(SOL_IPV6) && defined(IPV6_V6ONLY)
  113. if(sa->sa.sa_family == AF_INET6) {
  114. setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
  115. }
  116. #endif
  117. if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
  118. #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
  119. struct ifreq ifr;
  120. memset(&ifr, 0, sizeof(ifr));
  121. strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
  122. ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
  123. free(iface);
  124. if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) {
  125. closesocket(nfd);
  126. logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(sockerrno));
  127. return -1;
  128. }
  129. #else
  130. logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
  131. #endif
  132. }
  133. if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
  134. closesocket(nfd);
  135. addrstr = sockaddr2hostname(sa);
  136. logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
  137. free(addrstr);
  138. return -1;
  139. }
  140. if(listen(nfd, 3)) {
  141. closesocket(nfd);
  142. logger(LOG_ERR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
  143. return -1;
  144. }
  145. return nfd;
  146. }
  147. int setup_vpn_in_socket(const sockaddr_t *sa) {
  148. int nfd;
  149. char *addrstr;
  150. int option;
  151. nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
  152. if(nfd < 0) {
  153. logger(LOG_ERR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
  154. return -1;
  155. }
  156. #ifdef FD_CLOEXEC
  157. fcntl(nfd, F_SETFD, FD_CLOEXEC);
  158. #endif
  159. #ifdef O_NONBLOCK
  160. {
  161. int flags = fcntl(nfd, F_GETFL);
  162. if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
  163. closesocket(nfd);
  164. logger(LOG_ERR, "System call `%s' failed: %s", "fcntl",
  165. strerror(errno));
  166. return -1;
  167. }
  168. }
  169. #elif defined(WIN32)
  170. {
  171. unsigned long arg = 1;
  172. if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
  173. closesocket(nfd);
  174. logger(LOG_ERR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
  175. return -1;
  176. }
  177. }
  178. #endif
  179. option = 1;
  180. setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
  181. setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
  182. if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf))) {
  183. logger(LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, strerror(errno));
  184. }
  185. if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf))) {
  186. logger(LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, strerror(errno));
  187. }
  188. #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
  189. if(sa->sa.sa_family == AF_INET6) {
  190. setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
  191. }
  192. #endif
  193. #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
  194. #define IP_DONTFRAGMENT IP_DONTFRAG
  195. #endif
  196. #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
  197. if(myself->options & OPTION_PMTU_DISCOVERY) {
  198. option = IP_PMTUDISC_DO;
  199. setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
  200. }
  201. #elif defined(IPPROTO_IP) && defined(IP_DONTFRAGMENT)
  202. if(myself->options & OPTION_PMTU_DISCOVERY) {
  203. option = 1;
  204. setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
  205. }
  206. #endif
  207. #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
  208. if(myself->options & OPTION_PMTU_DISCOVERY) {
  209. option = IPV6_PMTUDISC_DO;
  210. setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
  211. }
  212. #elif defined(IPPROTO_IPV6) && defined(IPV6_DONTFRAG)
  213. if(myself->options & OPTION_PMTU_DISCOVERY) {
  214. option = 1;
  215. setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
  216. }
  217. #endif
  218. if(!bind_to_interface(nfd)) {
  219. closesocket(nfd);
  220. return -1;
  221. }
  222. if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
  223. closesocket(nfd);
  224. addrstr = sockaddr2hostname(sa);
  225. logger(LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
  226. free(addrstr);
  227. return -1;
  228. }
  229. return nfd;
  230. } /* int setup_vpn_in_socket */
  231. void retry_outgoing(outgoing_t *outgoing) {
  232. outgoing->timeout += 5;
  233. if(outgoing->timeout < mintimeout) {
  234. outgoing->timeout = mintimeout;
  235. }
  236. if(outgoing->timeout > maxtimeout) {
  237. outgoing->timeout = maxtimeout;
  238. }
  239. if(outgoing->event) {
  240. event_del(outgoing->event);
  241. }
  242. outgoing->event = new_event();
  243. outgoing->event->handler = (event_handler_t) setup_outgoing_connection;
  244. outgoing->event->time = now + outgoing->timeout;
  245. outgoing->event->data = outgoing;
  246. event_add(outgoing->event);
  247. ifdebug(CONNECTIONS) logger(LOG_NOTICE,
  248. "Trying to re-establish outgoing connection in %d seconds",
  249. outgoing->timeout);
  250. }
  251. void finish_connecting(connection_t *c) {
  252. ifdebug(CONNECTIONS) logger(LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
  253. c->last_ping_time = now;
  254. send_id(c);
  255. }
  256. static void do_outgoing_pipe(connection_t *c, char *command) {
  257. #ifndef HAVE_MINGW
  258. int fd[2];
  259. if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
  260. logger(LOG_ERR, "Could not create socketpair: %s\n", strerror(errno));
  261. return;
  262. }
  263. if(fork()) {
  264. c->socket = fd[0];
  265. close(fd[1]);
  266. ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Using proxy %s", command);
  267. return;
  268. }
  269. close(0);
  270. close(1);
  271. close(fd[0]);
  272. dup2(fd[1], 0);
  273. dup2(fd[1], 1);
  274. close(fd[1]);
  275. // Other filedescriptors should be closed automatically by CLOEXEC
  276. char *host = NULL;
  277. char *port = NULL;
  278. sockaddr2str(&c->address, &host, &port);
  279. setenv("REMOTEADDRESS", host, true);
  280. setenv("REMOTEPORT", port, true);
  281. setenv("NODE", c->name, true);
  282. setenv("NAME", myself->name, true);
  283. if(netname) {
  284. setenv("NETNAME", netname, true);
  285. }
  286. int result = system(command);
  287. if(result < 0) {
  288. logger(LOG_ERR, "Could not execute %s: %s\n", command, strerror(errno));
  289. } else if(result) {
  290. logger(LOG_ERR, "%s exited with non-zero status %d", command, result);
  291. }
  292. exit(result);
  293. #else
  294. logger(LOG_ERR, "Proxy type exec not supported on this platform!");
  295. return;
  296. #endif
  297. }
  298. static bool is_valid_host_port(const char *host, const char *port) {
  299. for(const char *p = host; *p; p++)
  300. if(!isalnum(*p) && *p != '-' && *p != '.') {
  301. return false;
  302. }
  303. for(const char *p = port; *p; p++)
  304. if(!isalnum(*p)) {
  305. return false;
  306. }
  307. return true;
  308. }
  309. void do_outgoing_connection(connection_t *c) {
  310. struct addrinfo *proxyai = NULL;
  311. int result;
  312. if(!c->outgoing) {
  313. logger(LOG_ERR, "do_outgoing_connection() for %s called without c->outgoing", c->name);
  314. abort();
  315. }
  316. begin:
  317. if(!c->outgoing->ai) {
  318. if(!c->outgoing->cfg) {
  319. ifdebug(CONNECTIONS) logger(LOG_ERR, "Could not set up a meta connection to %s",
  320. c->name);
  321. c->status.remove = true;
  322. retry_outgoing(c->outgoing);
  323. c->outgoing = NULL;
  324. return;
  325. }
  326. char *address, *port, *space;
  327. get_config_string(c->outgoing->cfg, &address);
  328. space = strchr(address, ' ');
  329. if(space) {
  330. port = xstrdup(space + 1);
  331. *space = 0;
  332. } else {
  333. if(!get_config_string(lookup_config(c->config_tree, "Port"), &port)) {
  334. port = xstrdup("655");
  335. }
  336. }
  337. c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
  338. // If we cannot resolve the address, maybe we are using a proxy that can?
  339. if(!c->outgoing->ai && proxytype != PROXY_NONE && is_valid_host_port(address, port)) {
  340. memset(&c->address, 0, sizeof(c->address));
  341. c->address.sa.sa_family = AF_UNKNOWN;
  342. c->address.unknown.address = address;
  343. c->address.unknown.port = port;
  344. } else {
  345. free(address);
  346. free(port);
  347. }
  348. c->outgoing->aip = c->outgoing->ai;
  349. c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
  350. if(!c->outgoing->ai && proxytype != PROXY_NONE) {
  351. goto connect;
  352. }
  353. }
  354. if(!c->outgoing->aip) {
  355. if(c->outgoing->ai) {
  356. freeaddrinfo(c->outgoing->ai);
  357. }
  358. c->outgoing->ai = NULL;
  359. goto begin;
  360. }
  361. memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
  362. c->outgoing->aip = c->outgoing->aip->ai_next;
  363. connect:
  364. if(c->hostname) {
  365. free(c->hostname);
  366. }
  367. c->hostname = sockaddr2hostname(&c->address);
  368. ifdebug(CONNECTIONS) logger(LOG_INFO, "Trying to connect to %s (%s)", c->name,
  369. c->hostname);
  370. if(!proxytype) {
  371. c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
  372. } else if(proxytype == PROXY_EXEC) {
  373. c->status.proxy_passed = true;
  374. do_outgoing_pipe(c, proxyhost);
  375. } else {
  376. proxyai = str2addrinfo(proxyhost, proxyport, SOCK_STREAM);
  377. if(!proxyai) {
  378. goto begin;
  379. }
  380. ifdebug(CONNECTIONS) logger(LOG_INFO, "Using proxy at %s port %s", proxyhost, proxyport);
  381. c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
  382. }
  383. if(c->socket == -1) {
  384. ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
  385. goto begin;
  386. }
  387. if(proxytype != PROXY_EXEC) {
  388. configure_tcp(c);
  389. }
  390. #ifdef FD_CLOEXEC
  391. fcntl(c->socket, F_SETFD, FD_CLOEXEC);
  392. #endif
  393. if(proxytype != PROXY_EXEC) {
  394. #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
  395. int option = 1;
  396. if(c->address.sa.sa_family == AF_INET6) {
  397. setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
  398. }
  399. #endif
  400. bind_to_interface(c->socket);
  401. int b = -1;
  402. for(int i = 0; i < listen_sockets; i++) {
  403. if(listen_socket[i].sa.sa.sa_family == c->address.sa.sa_family) {
  404. if(b == -1) {
  405. b = i;
  406. } else {
  407. b = -1;
  408. break;
  409. }
  410. }
  411. }
  412. if(b != -1) {
  413. sockaddr_t sa = listen_socket[b].sa;
  414. if(sa.sa.sa_family == AF_INET) {
  415. sa.in.sin_port = 0;
  416. } else if(sa.sa.sa_family == AF_INET6) {
  417. sa.in6.sin6_port = 0;
  418. }
  419. if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
  420. char *addrstr = sockaddr2hostname(&sa);
  421. logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
  422. free(addrstr);
  423. }
  424. }
  425. }
  426. /* Connect */
  427. if(!proxytype) {
  428. result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
  429. } else if(proxytype == PROXY_EXEC) {
  430. result = 0;
  431. } else {
  432. result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
  433. freeaddrinfo(proxyai);
  434. }
  435. now = time(NULL);
  436. if(result == -1) {
  437. if(sockinprogress(sockerrno)) {
  438. c->last_ping_time = now;
  439. c->status.connecting = true;
  440. return;
  441. }
  442. closesocket(c->socket);
  443. ifdebug(CONNECTIONS) logger(LOG_ERR, "%s: %s", c->hostname, sockstrerror(sockerrno));
  444. goto begin;
  445. }
  446. finish_connecting(c);
  447. return;
  448. }
  449. void setup_outgoing_connection(outgoing_t *outgoing) {
  450. connection_t *c;
  451. node_t *n;
  452. outgoing->event = NULL;
  453. n = lookup_node(outgoing->name);
  454. if(n)
  455. if(n->connection) {
  456. ifdebug(CONNECTIONS) logger(LOG_INFO, "Already connected to %s", outgoing->name);
  457. n->connection->outgoing = outgoing;
  458. return;
  459. }
  460. c = new_connection();
  461. c->name = xstrdup(outgoing->name);
  462. c->outcipher = myself->connection->outcipher;
  463. c->outdigest = myself->connection->outdigest;
  464. c->outmaclength = myself->connection->outmaclength;
  465. c->outcompression = myself->connection->outcompression;
  466. init_configuration(&c->config_tree);
  467. if(!read_connection_config(c)) {
  468. free_connection(c);
  469. outgoing->timeout = maxtimeout;
  470. retry_outgoing(outgoing);
  471. return;
  472. }
  473. outgoing->cfg = lookup_config(c->config_tree, "Address");
  474. if(!outgoing->cfg) {
  475. logger(LOG_ERR, "No address specified for %s", c->name);
  476. free_connection(c);
  477. outgoing->timeout = maxtimeout;
  478. retry_outgoing(outgoing);
  479. return;
  480. }
  481. c->outgoing = outgoing;
  482. c->last_ping_time = now;
  483. connection_add(c);
  484. do_outgoing_connection(c);
  485. }
  486. /*
  487. accept a new tcp connect and create a
  488. new connection
  489. */
  490. bool handle_new_meta_connection(int sock) {
  491. connection_t *c;
  492. sockaddr_t sa;
  493. int fd;
  494. socklen_t len = sizeof(sa);
  495. fd = accept(sock, &sa.sa, &len);
  496. if(fd < 0) {
  497. logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
  498. return false;
  499. }
  500. sockaddrunmap(&sa);
  501. c = new_connection();
  502. c->name = xstrdup("<unknown>");
  503. c->outcipher = myself->connection->outcipher;
  504. c->outdigest = myself->connection->outdigest;
  505. c->outmaclength = myself->connection->outmaclength;
  506. c->outcompression = myself->connection->outcompression;
  507. c->address = sa;
  508. c->hostname = sockaddr2hostname(&sa);
  509. c->socket = fd;
  510. c->last_ping_time = now;
  511. ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);
  512. configure_tcp(c);
  513. connection_add(c);
  514. c->allow_request = ID;
  515. send_id(c);
  516. return true;
  517. }
  518. static void free_outgoing(outgoing_t *outgoing) {
  519. if(outgoing->ai) {
  520. freeaddrinfo(outgoing->ai);
  521. }
  522. if(outgoing->name) {
  523. free(outgoing->name);
  524. }
  525. free(outgoing);
  526. }
  527. void try_outgoing_connections(void) {
  528. static config_t *cfg = NULL;
  529. char *name;
  530. outgoing_t *outgoing;
  531. outgoing_list = list_alloc((list_action_t)free_outgoing);
  532. for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
  533. get_config_string(cfg, &name);
  534. if(!check_id(name)) {
  535. logger(LOG_ERR,
  536. "Invalid name for outgoing connection in %s line %d",
  537. cfg->file, cfg->line);
  538. free(name);
  539. continue;
  540. }
  541. outgoing = xmalloc_and_zero(sizeof(*outgoing));
  542. outgoing->name = name;
  543. list_insert_tail(outgoing_list, outgoing);
  544. setup_outgoing_connection(outgoing);
  545. }
  546. }