-void retry_outgoing(outgoing_t *outgoing)
-{
- 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 setup_listen_socket(const sockaddr_t *sa) {
+ int nfd;
+ char *addrstr;
+ int option;
+ char *iface;
+
+ nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
+
+ if(nfd < 0) {
+ ifdebug(STATUS) logger(LOG_ERR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
+ return -1;
+ }
+
+#ifdef FD_CLOEXEC
+ fcntl(nfd, F_SETFD, FD_CLOEXEC);
+#endif
+
+ /* Optimize TCP settings */
+
+ option = 1;
+ setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
+
+#if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
+
+ if(sa->sa.sa_family == AF_INET6) {
+ setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&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);
+ ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
+ free(iface);
+
+ if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) {
+ closesocket(nfd);
+ logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(sockerrno));
+ 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, sockstrerror(sockerrno));
+ free(addrstr);
+ return -1;
+ }
+
+ if(listen(nfd, 3)) {
+ closesocket(nfd);
+ logger(LOG_ERR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
+ return -1;
+ }
+
+ return nfd;