- if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
- {
- memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
- if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
+ 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;
+}
+
+int setup_vpn_in_socket(const sockaddr_t *sa) {
+ int nfd;
+ char *addrstr;
+ int option;
+
+ nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
+
+ if(nfd < 0) {
+ logger(LOG_ERR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
+ return -1;
+ }
+
+#ifdef FD_CLOEXEC
+ fcntl(nfd, F_SETFD, FD_CLOEXEC);
+#endif
+
+#ifdef O_NONBLOCK