+#ifdef O_NONBLOCK
+ {
+ int flags = fcntl(nfd, F_GETFL);
+
+ if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
+ closesocket(nfd);
+ logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
+ strerror(errno));
+ return -1;
+ }
+ }
+#elif defined(WIN32)
+ {
+ unsigned long arg = 1;
+ if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
+ closesocket(nfd);
+ logger(LOG_ERR, _("Call to `%s' failed: WSA error %d"), "ioctlsocket",
+ WSAGetLastError());
+ return -1;
+ }
+ }
+#endif
+
+ 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 defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
+ if(myself->options & OPTION_PMTU_DISCOVERY) {
+ option = IP_PMTUDISC_DO;
+ setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, &option, sizeof(option));
+ }
+#endif
+
+#if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
+ if(myself->options & OPTION_PMTU_DISCOVERY) {
+ option = IPV6_PMTUDISC_DO;
+ setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, &option, sizeof(option));
+ }
+#endif
+
+ if (!bind_to_interface(nfd)) {
+ closesocket(nfd);
+ return -1;
+ }
+
+ if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
+ closesocket(nfd);
+ addrstr = sockaddr2hostname(sa);
+ logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
+ strerror(errno));
+ free(addrstr);
+ return -1;
+ }
+
+ return nfd;
+} /* int setup_vpn_in_socket */
+
+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 = now + outgoing->timeout;
+ event->data = outgoing;
+ event_add(event);
+
+ ifdebug(CONNECTIONS) logger(LOG_NOTICE,
+ _("Trying to re-establish outgoing connection in %d seconds"),
+ outgoing->timeout);