/*
net_setup.c -- Setup.
Copyright (C) 1998-2005 Ivo Timmermans,
- 2000-2011 Guus Sliepen <guus@tinc-vpn.org>
+ 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
2006 Scott Lamb <slamb@slamb.org>
2010 Brandon Black <blblack@gmail.com>
config_t *cfg;
subnet_t *s, *s2;
node_t *n;
- bool result;
xasprintf(&dname, "%s/hosts", confbase);
dir = opendir(dname);
xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
init_configuration(&config_tree);
- result = read_config_file(config_tree, fname);
+ read_config_options(config_tree, ent->d_name);
+ read_config_file(config_tree, fname);
free(fname);
- if(!result)
- continue;
if(!n) {
n = new_node();
get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
+ get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
strictsubnets |= tunnelserver;
if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
myself->options |= OPTION_CLAMP_MSS;
get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
+ get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
+ get_config_bool(lookup_config(config_tree, "Broadcast"), &broadcast);
#if !defined(SOL_IP) || !defined(IP_TOS)
if(priorityinheritance)
/* Open device */
+ devops = os_devops;
+
if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
if(!strcasecmp(type, "dummy"))
devops = dummy_devops;
else if(!strcasecmp(type, "raw_socket"))
devops = raw_socket_devops;
+ else if(!strcasecmp(type, "multicast"))
+ devops = multicast_devops;
#ifdef ENABLE_UML
else if(!strcasecmp(type, "uml"))
devops = uml_devops;
else if(!strcasecmp(type, "vde"))
devops = vde_devops;
#endif
- else {
- logger(LOG_ERR, "Unknown device type %s!", type);
- return false;
- }
- } else {
- devops = os_devops;
}
if(!devops.setup())
/* Open sockets */
- cfg = lookup_config(config_tree, "BindToAddress");
-
- do {
- get_config_string(cfg, &address);
- if(cfg)
- cfg = lookup_config_next(config_tree, cfg);
-
- hint.ai_family = addressfamily;
- hint.ai_socktype = SOCK_STREAM;
- hint.ai_protocol = IPPROTO_TCP;
- hint.ai_flags = AI_PASSIVE;
+ if(!do_detach && getenv("LISTEN_FDS")) {
+ sockaddr_t sa;
+ socklen_t salen;
- err = getaddrinfo(address, myport, &hint, &ai);
- free(address);
+ listen_sockets = atoi(getenv("LISTEN_FDS"));
+#ifdef HAVE_UNSETENV
+ unsetenv("LISTEN_FDS");
+#endif
- if(err || !ai) {
- logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
- gai_strerror(err));
+ if(listen_sockets > MAXSOCKETS) {
+ logger(LOG_ERR, "Too many listening sockets");
return false;
}
- listen_sockets = 0;
-
- for(aip = ai; aip; aip = aip->ai_next) {
- listen_socket[listen_sockets].tcp =
- setup_listen_socket((sockaddr_t *) aip->ai_addr);
+ for(i = 0; i < listen_sockets; i++) {
+ salen = sizeof sa;
+ if(getsockname(i + 3, &sa.sa, &salen) < 0) {
+ logger(LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
+ return false;
+ }
- if(listen_socket[listen_sockets].tcp < 0)
- continue;
+ listen_socket[i].tcp = i + 3;
- listen_socket[listen_sockets].udp =
- setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
+#ifdef FD_CLOEXEC
+ fcntl(i + 3, F_SETFD, FD_CLOEXEC);
+#endif
- if(listen_socket[listen_sockets].udp < 0)
- continue;
+ listen_socket[i].udp = setup_vpn_in_socket(&sa);
+ if(listen_socket[i].udp < 0)
+ return false;
ifdebug(CONNECTIONS) {
- hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
+ hostname = sockaddr2hostname(&sa);
logger(LOG_NOTICE, "Listening on %s", hostname);
free(hostname);
}
- memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
- listen_sockets++;
+ memcpy(&listen_socket[i].sa, &sa, salen);
}
+ } else {
+ listen_sockets = 0;
+ cfg = lookup_config(config_tree, "BindToAddress");
- freeaddrinfo(ai);
- } while(cfg);
+ do {
+ get_config_string(cfg, &address);
+ if(cfg)
+ cfg = lookup_config_next(config_tree, cfg);
+
+ char *port = myport;
+
+ if(address) {
+ char *space = strchr(address, ' ');
+ if(space) {
+ *space++ = 0;
+ port = space;
+ }
+
+ if(!strcmp(address, "*"))
+ *address = 0;
+ }
+
+ hint.ai_family = addressfamily;
+ hint.ai_socktype = SOCK_STREAM;
+ hint.ai_protocol = IPPROTO_TCP;
+ hint.ai_flags = AI_PASSIVE;
+
+ err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
+ free(address);
+
+ if(err || !ai) {
+ logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
+ gai_strerror(err));
+ return false;
+ }
+
+ for(aip = ai; aip; aip = aip->ai_next) {
+ if(listen_sockets >= MAXSOCKETS) {
+ logger(LOG_ERR, "Too many listening sockets");
+ return false;
+ }
+
+ listen_socket[listen_sockets].tcp =
+ setup_listen_socket((sockaddr_t *) aip->ai_addr);
+
+ if(listen_socket[listen_sockets].tcp < 0)
+ continue;
+
+ listen_socket[listen_sockets].udp =
+ setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
+
+ if(listen_socket[listen_sockets].udp < 0)
+ continue;
+
+ ifdebug(CONNECTIONS) {
+ hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
+ logger(LOG_NOTICE, "Listening on %s", hostname);
+ free(hostname);
+ }
+
+ memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+ listen_sockets++;
+ }
+
+ freeaddrinfo(ai);
+ } while(cfg);
+ }
if(listen_sockets)
logger(LOG_NOTICE, "Ready");