- if(debug_lvl >= DEBUG_TRAFFIC)
- syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
- packet->len, from->name, from->hostname);
-
- for(node = connection_tree->head; node; node = node->next)
- {
- c = (connection_t *)node->data;
- if(c->status.active && c != from->nexthop->connection)
- send_packet(c->node, packet);
- }
-cp
-}
-
-void flush_queue(node_t *n)
-{
- list_node_t *node, *next;
-cp
- if(debug_lvl >= DEBUG_TRAFFIC)
- syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
-
- for(node = n->queue->head; node; node = next)
- {
- next = node->next;
- send_udppacket(n, (vpn_packet_t *)node->data);
- list_delete_node(n->queue, node);
- }
-cp
-}
-
-/* Setup sockets */
-
-int setup_listen_socket(node_t *n)
-{
- int nfd, flags;
- int option;
- char *address;
- int r;
- struct addrinfo hints, *ai, *aitop;
- int ipv6preferred;
-#ifdef HAVE_LINUX
- char *interface;
-#endif
-
-cp
-
- if(!get_config_string(lookup_config(config_tree, "BindToAddress"), &address))
- {
- address = NULL;
- }
-
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_family = AF_INET;
- if(get_config_bool(lookup_config(config_tree, "IPv6Preferred"), &ipv6preferred))
- {
- if(ipv6preferred)
- hints.ai_family = PF_UNSPEC;
- }
- if((r = getaddrinfo(address, n->port, &hints, &aitop)) != 0)
- {
- syslog(LOG_ERR, _("Looking up `%s' failed: %s\n"),
- address, gai_strerror(r));
- return -1;
- }
-
- /* Try to create a listening socket for all alternatives we got from
- getaddrinfo. */
- for(ai = aitop; ai != NULL; ai = ai->ai_next)
- {
- if((nfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0)
- {
- syslog(LOG_ERR, _("Creating metasocket failed: %m"));
- continue;
- }
-
- flags = fcntl(nfd, F_GETFL);
- if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
- {
- close(nfd);
- syslog(LOG_ERR, _("System call `%s' failed: %m"),
- "fcntl");
- continue;
- }
-
- /* Optimize TCP settings */
-
- option = 1;
- setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
- setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
-#ifdef HAVE_LINUX
- setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
-
- option = IPTOS_LOWDELAY;
- setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
-
- if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
- if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
- {
- close(nfd);
- syslog(LOG_ERR, _("Can't bind to interface %s: %m"), interface);
- continue;
- }
-#endif
-
- if(bind(nfd, ai->ai_addr, ai->ai_addrlen))
- {
- close(nfd);
- syslog(LOG_ERR, _("Can't bind to %s port %s/tcp: %m"),
- ai->ai_canonname, n->port);
- continue;
- }
-
- if(listen(nfd, 3))
- {
- close(nfd);
- syslog(LOG_ERR, _("System call `%s' failed: %m"),
- "listen");
- continue;
- }
-
- break; /* We have successfully bound to a socket */
- }
-
- if(ai == NULL) /* None of the alternatives succeeded */
- {
- syslog(LOG_ERR, _("Failed to open a listening socket."));
- return -1;
- }
-cp
- return nfd;
-}
-
-int setup_vpn_in_socket(node_t *n)
-{
- const int one = 1;
- int nfd, flags;
- int option;
- char *address;
- int r;
- struct addrinfo hints, *ai, *aitop;
- int ipv6preferred;
-#ifdef HAVE_LINUX
- char *interface;
-#endif
-
-cp
-
- if(!get_config_string(lookup_config(config_tree, "BindToAddress"), &address))
- {
- address = NULL;
- }
-
- hints.ai_socktype = SOCK_DGRAM;
- hints.ai_protocol = IPPROTO_UDP;
- hints.ai_family = AF_INET;
- if(get_config_bool(lookup_config(config_tree, "IPv6Preferred"), &ipv6preferred))
- {
- if(ipv6preferred)
- hints.ai_family = PF_UNSPEC;
- }
- if((r = getaddrinfo(address, n->port, &hints, &aitop)) != 0)
- {
- syslog(LOG_ERR, _("Looking up `%s' failed: %s\n"),
- address, gai_strerror(r));
- return -1;
- }
-
- setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
-
- /* Try to create a listening socket for all alternatives we got from
- getaddrinfo. */
- for(ai = aitop; ai != NULL; ai = ai->ai_next)
- {
- if((nfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0)
- {
- syslog(LOG_ERR, _("Creating metasocket failed: %m"));
- continue;
- }
-
- flags = fcntl(nfd, F_GETFL);
- if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
- {
- close(nfd);
- syslog(LOG_ERR, _("System call `%s' failed: %m"),
- "fcntl");
- continue;
- }
-
- /* Optimize UDP settings */
-
- option = 1;
- setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
-#ifdef HAVE_LINUX
- if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
- if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
- {
- close(nfd);
- syslog(LOG_ERR, _("Can't bind to interface %s: %m"), interface);
- continue;
- }
-#endif
-
- if(bind(nfd, ai->ai_addr, ai->ai_addrlen))
- {
- close(nfd);
- syslog(LOG_ERR, _("Can't bind to %s port %s/tcp: %m"),
- ai->ai_canonname, n->port);
- continue;
- }
-
- break; /* We have successfully bound to a socket */
- }
-
- if(ai == NULL) /* None of the alternatives succeeded */
- {
- syslog(LOG_ERR, _("Failed to open a listening socket."));
- return -1;
- }
-cp
- return nfd;
-}
-
-int setup_outgoing_socket(connection_t *c)
-{
- int flags;
-cp
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
-
- c->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-
- if(c->socket == -1)
- {
- syslog(LOG_ERR, _("Creating socket for %s port %s failed: %m"),
- c->hostname, c->port);
- return -1;
- }
-
- /* Bind first to get a fix on our source port???
-
- a.sin_family = AF_INET;
- a.sin_port = htons(0);
- a.sin_addr.s_addr = htonl(INADDR_ANY);
-
- if(bind(c->socket, (struct sockaddr *)&a, sizeof(struct sockaddr)))
- {
- close(c->socket);
- syslog(LOG_ERR, _("System call `%s' failed: %m"), "bind");
- return -1;
- }
-
- */
-
- /* Optimize TCP settings?
-
- option = 1;
- setsockopt(c->socket, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
-#ifdef HAVE_LINUX
- setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
-
- option = IPTOS_LOWDELAY;
- setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
-#endif