- event_t *event;
-cp
- outgoing->timeout += 5;
- if(outgoing->timeout > maxtimeout)
- outgoing->timeout = maxtimeout;
-
- event = new_event();
- event->handler = (event_handler_t)setup_outgoing_connection;
- event->time = time(NULL) + outgoing->timeout;
- event->data = outgoing;
- event_add(event);
-
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), outgoing->timeout);
-cp
+ int nfd;
+ char *addrstr;
+ int option;
+ char *iface;
+
+ cp();
+
+ nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
+
+ if(nfd < 0) {
+ ifdebug(STATUS) logger(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
+ return -1;
+ }
+
+ /* Optimize TCP settings */
+
+ option = 1;
+ setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
+
+#if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
+ if(sa->sa.sa_family == AF_INET6)
+ setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, &option, sizeof option);
+#endif
+
+ if(get_config_string
+ (lookup_config(config_tree, "BindToInterface"), &iface)) {
+#if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
+ struct ifreq ifr;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
+
+ if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
+ closesocket(nfd);
+ logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
+ strerror(errno));
+ return -1;
+ }
+#else
+ logger(LOG_WARNING, _("%s not supported on this platform"), "BindToInterface");
+#endif
+ }
+
+ if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
+ closesocket(nfd);
+ addrstr = sockaddr2hostname(sa);
+ logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
+ strerror(errno));
+ free(addrstr);
+ return -1;
+ }
+
+ if(listen(nfd, 3)) {
+ closesocket(nfd);
+ logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
+ strerror(errno));
+ return -1;
+ }
+
+ return nfd;