mirror of https://github.com/ntop/n2n.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
655 lines
22 KiB
655 lines
22 KiB
7 years ago
|
/**
|
||
4 years ago
|
* (C) 2007-21 - ntop.org and contributors
|
||
7 years ago
|
*
|
||
|
* This program is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation; either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
4 years ago
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
7 years ago
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not see see <http://www.gnu.org/licenses/>
|
||
8 years ago
|
*
|
||
|
*/
|
||
|
|
||
7 years ago
|
/* Supernode for n2n-2.x */
|
||
8 years ago
|
|
||
|
#include "n2n.h"
|
||
4 years ago
|
#include "header_encryption.h"
|
||
8 years ago
|
|
||
4 years ago
|
#define HASH_FIND_COMMUNITY(head, name, out) HASH_FIND_STR(head, name, out)
|
||
|
|
||
6 years ago
|
static n2n_sn_t sss_node;
|
||
8 years ago
|
|
||
3 years ago
|
void close_tcp_connection (n2n_sn_t *sss, n2n_tcp_connection_t *conn);
|
||
|
void calculate_shared_secrets (n2n_sn_t *sss);
|
||
|
int load_allowed_sn_community (n2n_sn_t *sss);
|
||
3 years ago
|
int resolve_create_thread (n2n_resolve_parameter_t **param, struct peer_info *sn_list);
|
||
|
|
||
8 years ago
|
|
||
|
/** Help message to print if the command line arguments are not valid. */
|
||
4 years ago
|
static void help (int level) {
|
||
4 years ago
|
|
||
3 years ago
|
if(level == 0) /* no help required */
|
||
|
return;
|
||
|
|
||
4 years ago
|
printf("\n");
|
||
4 years ago
|
print_n2n_version();
|
||
4 years ago
|
|
||
3 years ago
|
if(level == 1) /* short help */ {
|
||
4 years ago
|
|
||
|
printf(" basic usage: supernode <config file> (see supernode.conf)\n"
|
||
|
"\n"
|
||
|
" or supernode "
|
||
4 years ago
|
"[optional parameters, at least one] "
|
||
4 years ago
|
"\n "
|
||
4 years ago
|
"\n technically, all parameters are optional, but the supernode executable"
|
||
|
"\n requires at least one parameter to run, .e.g. -v or -f, as otherwise this"
|
||
|
"\n short help text is displayed"
|
||
|
"\n\n -h shows a quick reference including all available options"
|
||
4 years ago
|
"\n --help gives a detailed parameter description"
|
||
|
"\n man files for n2n, edge, and superndode contain in-depth information"
|
||
|
"\n\n");
|
||
|
|
||
3 years ago
|
} else if(level == 2) /* quick reference */ {
|
||
4 years ago
|
|
||
|
printf(" general usage: supernode <config file> (see supernode.conf)\n"
|
||
|
"\n"
|
||
|
" or supernode "
|
||
4 years ago
|
"[-p <local port>] "
|
||
4 years ago
|
"\n "
|
||
|
"[-F <federation name>] "
|
||
|
"\n options for under- "
|
||
|
"[-l <supernode host:port>] "
|
||
|
"\n lying connection "
|
||
|
#ifdef SN_MANUAL_MAC
|
||
|
"[-m <mac address>] "
|
||
5 years ago
|
#endif
|
||
3 years ago
|
"[-M] "
|
||
4 years ago
|
"\n\n overlay network "
|
||
|
"[-c <community list file>] "
|
||
|
"\n configuration "
|
||
|
"[-a <net ip>-<net ip>/<cidr suffix>] "
|
||
|
"\n\n local options "
|
||
|
#if defined(N2N_HAVE_DAEMON)
|
||
|
"[-f] "
|
||
4 years ago
|
#endif
|
||
4 years ago
|
"[-t <management port>] "
|
||
|
"[-v] "
|
||
4 years ago
|
#ifndef WIN32
|
||
4 years ago
|
"\n "
|
||
|
"[-u <numerical user id>]"
|
||
|
"[-g <numerical group id>]"
|
||
|
#endif
|
||
|
"\n\n meaning of the "
|
||
3 years ago
|
"[-M] disable MAC and IP address spoofing protection"
|
||
|
"\n flag options "
|
||
4 years ago
|
#if defined(N2N_HAVE_DAEMON)
|
||
|
"[-f] do not fork but run in foreground"
|
||
3 years ago
|
"\n "
|
||
4 years ago
|
#endif
|
||
|
"[-v] make more verbose, repeat as required"
|
||
|
"\n "
|
||
4 years ago
|
"\n technically, all parameters are optional, but the supernode executable"
|
||
|
"\n requires at least one parameter to run, .e.g. -v or -f, as otherwise a"
|
||
|
"\n short help text is displayed"
|
||
|
"\n\n -h shows this quick reference including all available options"
|
||
4 years ago
|
"\n --help gives a detailed parameter description"
|
||
|
"\n man files for n2n, edge, and superndode contain in-depth information"
|
||
|
"\n\n");
|
||
|
|
||
|
} else /* long help */ {
|
||
|
|
||
|
printf(" general usage: supernode <config file> (see supernode.conf)\n"
|
||
|
"\n"
|
||
4 years ago
|
" or supernode [optional parameters, at least one]\n\n"
|
||
4 years ago
|
);
|
||
|
printf (" OPTIONS FOR THE UNDERLYING NETWORK CONNECTION\n");
|
||
|
printf (" ---------------------------------------------\n\n");
|
||
3 years ago
|
printf(" -p <local port> | fixed local UDP port, defaults to %u\n", N2N_SN_LPORT_DEFAULT);
|
||
4 years ago
|
printf(" -F <fed name> | name of the supernode's federation, defaults to\n"
|
||
|
" | '%s'\n", (char *)FEDERATION_NAME);
|
||
|
printf(" -l <host:port> | ip address or name, and port of known supernode\n");
|
||
|
#ifdef SN_MANUAL_MAC
|
||
|
printf(" -m <mac> | fixed MAC address for the supernode, e.g.\n"
|
||
|
" | '-m 10:20:30:40:50:60', random otherwise\n");
|
||
|
#endif
|
||
3 years ago
|
printf(" -M | disable MAC and IP address spoofing protection for all\n"
|
||
|
" | non-username-password-authenticating communities\n");
|
||
4 years ago
|
printf ("\n");
|
||
|
printf (" TAP DEVICE AND OVERLAY NETWORK CONFIGURATION\n");
|
||
|
printf (" --------------------------------------------\n\n");
|
||
|
printf(" -c <path> | file containing the allowed communities\n");
|
||
|
printf(" -a <net-net/n> | subnet range for auto ip address service, e.g.\n"
|
||
|
" | '-a 192.168.0.0-192.168.255.0/24', defaults\n"
|
||
|
" | to '10.128.255.0-10.255.255.0/24'\n");
|
||
|
printf ("\n");
|
||
|
printf (" LOCAL OPTIONS\n");
|
||
|
printf (" -------------\n\n");
|
||
8 years ago
|
#if defined(N2N_HAVE_DAEMON)
|
||
4 years ago
|
printf(" -f | do not fork and run as a daemon, rather run in foreground\n");
|
||
|
#endif
|
||
4 years ago
|
printf(" -t <port> | management UDP port, for multiple supernodes on a machine,\n"
|
||
3 years ago
|
" | defaults to %u\n", N2N_SN_MGMT_PORT);
|
||
4 years ago
|
printf(" -v | make more verbose, repeat as required\n");
|
||
4 years ago
|
#ifndef WIN32
|
||
4 years ago
|
printf(" -u <UID> | numeric user ID to use when privileges are dropped\n");
|
||
|
printf(" -g <GID> | numeric group ID to use when privileges are dropped\n");
|
||
|
#endif
|
||
4 years ago
|
printf("\n technically, all parameters are optional, but the supernode executable"
|
||
|
"\n requires at least one parameter to run, .e.g. -v or -f, as otherwise a"
|
||
|
"\n short help text is displayed"
|
||
|
"\n\n -h shows a quick reference including all available options"
|
||
|
"\n --help gives this detailed parameter description"
|
||
|
"\n man files for n2n, edge, and superndode contain in-depth information"
|
||
|
"\n\n");
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
exit(0);
|
||
8 years ago
|
}
|
||
|
|
||
4 years ago
|
|
||
6 years ago
|
/* *************************************************** */
|
||
|
|
||
4 years ago
|
static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) {
|
||
|
|
||
4 years ago
|
//traceEvent(TRACE_NORMAL, "Option %c = %s", optkey, _optarg ? _optarg : "");
|
||
|
|
||
|
switch(optkey) {
|
||
|
case 'p': /* local-port */
|
||
|
sss->lport = atoi(_optarg);
|
||
|
|
||
3 years ago
|
if(sss->lport == 0)
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "bad local port format, defaulting to %u", N2N_SN_LPORT_DEFAULT);
|
||
3 years ago
|
// default is made sure in sn_init()
|
||
4 years ago
|
|
||
|
break;
|
||
|
|
||
|
case 't': /* mgmt-port */
|
||
|
sss->mport = atoi(_optarg);
|
||
|
|
||
3 years ago
|
if(sss->mport == 0)
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "bad management port format, defaulting to %u", N2N_SN_MGMT_PORT);
|
||
3 years ago
|
// default is made sure in sn_init()
|
||
4 years ago
|
|
||
|
break;
|
||
|
|
||
|
case 'l': { /* supernode:port */
|
||
|
n2n_sock_t *socket;
|
||
|
struct peer_info *anchor_sn;
|
||
|
size_t length;
|
||
|
int rv = -1;
|
||
|
int skip_add;
|
||
|
char *double_column = strchr(_optarg, ':');
|
||
|
|
||
|
length = strlen(_optarg);
|
||
|
if(length >= N2N_EDGE_SN_HOST_SIZE) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "size of -l argument too long: %zu; maximum size is %d", length, N2N_EDGE_SN_HOST_SIZE);
|
||
3 years ago
|
return 1;
|
||
4 years ago
|
}
|
||
|
|
||
|
if(!double_column) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "invalid -l format, missing port");
|
||
3 years ago
|
return 1;
|
||
4 years ago
|
}
|
||
|
|
||
|
socket = (n2n_sock_t *)calloc(1, sizeof(n2n_sock_t));
|
||
|
rv = supernode2sock(socket, _optarg);
|
||
|
|
||
3 years ago
|
if(rv < -2) { /* we accept resolver failure as it might resolve later */
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "invalid supernode parameter");
|
||
4 years ago
|
free(socket);
|
||
3 years ago
|
return 1;
|
||
4 years ago
|
}
|
||
|
|
||
|
if(sss->federation != NULL) {
|
||
|
skip_add = SN_ADD;
|
||
4 years ago
|
anchor_sn = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), socket, null_mac, &skip_add);
|
||
4 years ago
|
|
||
|
if(anchor_sn != NULL) {
|
||
|
anchor_sn->ip_addr = calloc(1, N2N_EDGE_SN_HOST_SIZE);
|
||
|
if(anchor_sn->ip_addr) {
|
||
|
strncpy(anchor_sn->ip_addr, _optarg, N2N_EDGE_SN_HOST_SIZE - 1);
|
||
|
memcpy(&(anchor_sn->sock), socket, sizeof(n2n_sock_t));
|
||
4 years ago
|
memcpy(anchor_sn->mac_addr, null_mac, sizeof(n2n_mac_t));
|
||
4 years ago
|
anchor_sn->purgeable = SN_UNPURGEABLE;
|
||
|
anchor_sn->last_valid_time_stamp = initial_time_stamp();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
free(socket);
|
||
|
break;
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
case 'a': {
|
||
|
dec_ip_str_t ip_min_str = {'\0'};
|
||
|
dec_ip_str_t ip_max_str = {'\0'};
|
||
|
in_addr_t net_min, net_max;
|
||
|
uint8_t bitlen;
|
||
|
uint32_t mask;
|
||
|
|
||
|
if(sscanf(_optarg, "%15[^\\-]-%15[^/]/%hhu", ip_min_str, ip_max_str, &bitlen) != 3) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "bad net-net/bit format '%s'.", _optarg);
|
||
3 years ago
|
return 2;
|
||
4 years ago
|
}
|
||
|
|
||
|
net_min = inet_addr(ip_min_str);
|
||
|
net_max = inet_addr(ip_max_str);
|
||
|
mask = bitlen2mask(bitlen);
|
||
|
if((net_min == (in_addr_t)(-1)) || (net_min == INADDR_NONE) || (net_min == INADDR_ANY)
|
||
|
|| (net_max == (in_addr_t)(-1)) || (net_max == INADDR_NONE) || (net_max == INADDR_ANY)
|
||
|
|| (ntohl(net_min) > ntohl(net_max))
|
||
|
|| ((ntohl(net_min) & ~mask) != 0) || ((ntohl(net_max) & ~mask) != 0)) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "bad network range '%s...%s/%u' in '%s', defaulting to '%s...%s/%d'",
|
||
4 years ago
|
ip_min_str, ip_max_str, bitlen, _optarg,
|
||
|
N2N_SN_MIN_AUTO_IP_NET_DEFAULT, N2N_SN_MAX_AUTO_IP_NET_DEFAULT, N2N_SN_AUTO_IP_NET_BIT_DEFAULT);
|
||
3 years ago
|
return 2;
|
||
4 years ago
|
}
|
||
|
|
||
|
if((bitlen > 30) || (bitlen == 0)) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "bad prefix '%hhu' in '%s', defaulting to '%s...%s/%d'",
|
||
4 years ago
|
bitlen, _optarg,
|
||
|
N2N_SN_MIN_AUTO_IP_NET_DEFAULT, N2N_SN_MAX_AUTO_IP_NET_DEFAULT, N2N_SN_AUTO_IP_NET_BIT_DEFAULT);
|
||
3 years ago
|
return 2;
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
traceEvent(TRACE_NORMAL, "the network range for community ip address service is '%s...%s/%hhu'", ip_min_str, ip_max_str, bitlen);
|
||
4 years ago
|
|
||
|
sss->min_auto_ip_net.net_addr = ntohl(net_min);
|
||
|
sss->min_auto_ip_net.net_bitlen = bitlen;
|
||
|
sss->max_auto_ip_net.net_addr = ntohl(net_max);
|
||
|
sss->max_auto_ip_net.net_bitlen = bitlen;
|
||
|
|
||
|
break;
|
||
|
}
|
||
4 years ago
|
#ifndef WIN32
|
||
4 years ago
|
case 'u': /* unprivileged uid */
|
||
|
sss->userid = atoi(_optarg);
|
||
|
break;
|
||
4 years ago
|
|
||
4 years ago
|
case 'g': /* unprivileged uid */
|
||
|
sss->groupid = atoi(_optarg);
|
||
|
break;
|
||
4 years ago
|
#endif
|
||
4 years ago
|
case 'F': { /* federation name */
|
||
|
snprintf(sss->federation->community, N2N_COMMUNITY_SIZE - 1 ,"*%s", _optarg);
|
||
|
sss->federation->community[N2N_COMMUNITY_SIZE - 1] = '\0';
|
||
|
break;
|
||
|
}
|
||
4 years ago
|
#ifdef SN_MANUAL_MAC
|
||
4 years ago
|
case 'm': {/* MAC address */
|
||
|
str2mac(sss->mac_addr,_optarg);
|
||
|
break;
|
||
|
}
|
||
4 years ago
|
#endif
|
||
3 years ago
|
case 'M': /* override spoofing protection */
|
||
|
sss->override_spoofing_protection = 1;
|
||
|
break;
|
||
4 years ago
|
case 'c': /* community file */
|
||
4 years ago
|
sss->community_file = calloc(1, strlen(_optarg) + 1);
|
||
|
if(sss->community_file)
|
||
|
strcpy(sss->community_file, _optarg);
|
||
4 years ago
|
break;
|
||
4 years ago
|
#if defined(N2N_HAVE_DAEMON)
|
||
4 years ago
|
case 'f': /* foreground */
|
||
|
sss->daemon = 0;
|
||
|
break;
|
||
4 years ago
|
#endif
|
||
|
case 'h': /* quick reference */
|
||
3 years ago
|
return 2;
|
||
6 years ago
|
|
||
4 years ago
|
case '@': /* long help */
|
||
3 years ago
|
return 3;
|
||
6 years ago
|
|
||
4 years ago
|
case 'v': /* verbose */
|
||
|
setTraceLevel(getTraceLevel() + 1);
|
||
|
break;
|
||
6 years ago
|
|
||
4 years ago
|
default:
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "unknown option -%c:", (char) optkey);
|
||
3 years ago
|
return 2;
|
||
4 years ago
|
}
|
||
6 years ago
|
|
||
3 years ago
|
return 0;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
|
||
8 years ago
|
/* *********************************************** */
|
||
|
|
||
|
static const struct option long_options[] = {
|
||
4 years ago
|
{"communities", required_argument, NULL, 'c'},
|
||
4 years ago
|
#if defined(N2N_HAVE_DAEMON)
|
||
4 years ago
|
{"foreground", no_argument, NULL, 'f'},
|
||
4 years ago
|
#endif
|
||
4 years ago
|
{"local-port", required_argument, NULL, 'p'},
|
||
|
{"mgmt-port", required_argument, NULL, 't'},
|
||
|
{"autoip", required_argument, NULL, 'a'},
|
||
4 years ago
|
{"help", no_argument, NULL, '@'}, /* special character '@' to identify long help case */
|
||
4 years ago
|
{"verbose", no_argument, NULL, 'v'},
|
||
|
{NULL, 0, NULL, 0}
|
||
8 years ago
|
};
|
||
|
|
||
6 years ago
|
/* *************************************************** */
|
||
|
|
||
|
/* read command line options */
|
||
4 years ago
|
static int loadFromCLI (int argc, char * const argv[], n2n_sn_t *sss) {
|
||
|
|
||
4 years ago
|
u_char c;
|
||
6 years ago
|
|
||
4 years ago
|
while((c = getopt_long(argc, argv,
|
||
3 years ago
|
"p:l:t:a:c:F:vhM"
|
||
4 years ago
|
#ifdef SN_MANUAL_MAC
|
||
|
"m:"
|
||
|
#endif
|
||
|
#if defined(N2N_HAVE_DAEMON)
|
||
|
"f"
|
||
|
#endif
|
||
|
#ifndef WIN32
|
||
|
"u:g:"
|
||
|
#endif
|
||
|
,
|
||
|
long_options, NULL)) != '?') {
|
||
4 years ago
|
if(c == 255) {
|
||
|
break;
|
||
|
}
|
||
3 years ago
|
help(setOption(c, optarg, sss));
|
||
4 years ago
|
}
|
||
6 years ago
|
|
||
4 years ago
|
return 0;
|
||
6 years ago
|
}
|
||
|
|
||
|
/* *************************************************** */
|
||
|
|
||
4 years ago
|
static char *trim (char *s) {
|
||
|
|
||
4 years ago
|
char *end;
|
||
6 years ago
|
|
||
4 years ago
|
while(isspace(s[0]) || (s[0] == '"') || (s[0] == '\'')) {
|
||
|
s++;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
if(s[0] == 0) {
|
||
|
return s;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
end = &s[strlen(s) - 1];
|
||
|
while(end > s && (isspace(end[0])|| (end[0] == '"') || (end[0] == '\''))) {
|
||
|
end--;
|
||
|
}
|
||
|
end[1] = 0;
|
||
6 years ago
|
|
||
4 years ago
|
return s;
|
||
6 years ago
|
}
|
||
|
|
||
|
/* *************************************************** */
|
||
|
|
||
|
/* parse the configuration file */
|
||
4 years ago
|
static int loadFromFile (const char *path, n2n_sn_t *sss) {
|
||
|
|
||
4 years ago
|
char buffer[4096], *line;
|
||
|
char *line_vec[3];
|
||
|
int tmp;
|
||
|
|
||
4 years ago
|
FILE *fd;
|
||
6 years ago
|
|
||
4 years ago
|
fd = fopen(path, "r");
|
||
6 years ago
|
|
||
4 years ago
|
if(fd == NULL) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "config file %s not found", path);
|
||
4 years ago
|
return -1;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
// we mess around with optind, better save it
|
||
|
tmp = optind;
|
||
6 years ago
|
|
||
4 years ago
|
while((line = fgets(buffer, sizeof(buffer), fd)) != NULL) {
|
||
4 years ago
|
line = trim(line);
|
||
6 years ago
|
|
||
4 years ago
|
if(strlen(line) < 2 || line[0] == '#') {
|
||
4 years ago
|
continue;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
// executable, cannot be omitted, content can be anything
|
||
|
line_vec[0] = line;
|
||
|
// first token, e.g. `-p`, eventually followed by a whitespace or '=' delimiter
|
||
|
line_vec[1] = strtok(line, "\t =");
|
||
|
// separate parameter option, if present
|
||
|
line_vec[2] = strtok(NULL, "\t ");
|
||
|
|
||
|
// not to duplicate the option parser code, call loadFromCLI and pretend we have no option read yet
|
||
4 years ago
|
optind = 0;
|
||
4 years ago
|
// if separate second token present (optional argument, not part of first), then announce 3 vector members
|
||
|
loadFromCLI(line_vec[2] ? 3 : 2, line_vec, sss);
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
fclose(fd);
|
||
4 years ago
|
optind = tmp;
|
||
6 years ago
|
|
||
4 years ago
|
return 0;
|
||
6 years ago
|
}
|
||
|
|
||
|
/* *************************************************** */
|
||
|
|
||
4 years ago
|
/* Add the federation to the communities list of a supernode */
|
||
4 years ago
|
static int add_federation_to_communities (n2n_sn_t *sss) {
|
||
|
|
||
4 years ago
|
uint32_t num_communities = 0;
|
||
|
|
||
|
if(sss->federation != NULL) {
|
||
|
HASH_ADD_STR(sss->communities, community, sss->federation);
|
||
4 years ago
|
|
||
4 years ago
|
num_communities = HASH_COUNT(sss->communities);
|
||
4 years ago
|
|
||
3 years ago
|
traceEvent(TRACE_INFO, "added federation '%s' to the list of communities [total: %u]",
|
||
4 years ago
|
(char*)sss->federation->community, num_communities);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
4 years ago
|
}
|
||
|
|
||
|
/* *************************************************** */
|
||
|
|
||
5 years ago
|
#ifdef __linux__
|
||
4 years ago
|
static void dump_registrations (int signo) {
|
||
|
|
||
4 years ago
|
struct sn_community *comm, *ctmp;
|
||
|
struct peer_info *list, *tmp;
|
||
|
char buf[32];
|
||
|
time_t now = time(NULL);
|
||
|
u_int num = 0;
|
||
|
|
||
|
traceEvent(TRACE_NORMAL, "====================================");
|
||
|
|
||
|
HASH_ITER(hh, sss_node.communities, comm, ctmp) {
|
||
3 years ago
|
traceEvent(TRACE_NORMAL, "dumping community: %s", comm->community);
|
||
4 years ago
|
|
||
|
HASH_ITER(hh, comm->edges, list, tmp) {
|
||
|
if(list->sock.family == AF_INET) {
|
||
|
traceEvent(TRACE_NORMAL, "[id: %u][MAC: %s][edge: %u.%u.%u.%u:%u][last seen: %u sec ago]",
|
||
|
++num, macaddr_str(buf, list->mac_addr),
|
||
|
list->sock.addr.v4[0], list->sock.addr.v4[1], list->sock.addr.v4[2], list->sock.addr.v4[3],
|
||
|
list->sock.port,
|
||
|
now - list->last_seen);
|
||
|
} else {
|
||
|
traceEvent(TRACE_NORMAL, "[id: %u][MAC: %s][edge: IPv6:%u][last seen: %u sec ago]",
|
||
|
++num, macaddr_str(buf, list->mac_addr), list->sock.port,
|
||
|
now - list->last_seen);
|
||
|
}
|
||
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
4 years ago
|
traceEvent(TRACE_NORMAL, "====================================");
|
||
6 years ago
|
}
|
||
5 years ago
|
#endif
|
||
6 years ago
|
|
||
|
/* *************************************************** */
|
||
|
|
||
6 years ago
|
static int keep_running;
|
||
|
|
||
5 years ago
|
#if defined(__linux__) || defined(WIN32)
|
||
5 years ago
|
#ifdef WIN32
|
||
4 years ago
|
BOOL WINAPI term_handler (DWORD sig)
|
||
5 years ago
|
#else
|
||
4 years ago
|
static void term_handler(int sig)
|
||
5 years ago
|
#endif
|
||
|
{
|
||
4 years ago
|
static int called = 0;
|
||
6 years ago
|
|
||
4 years ago
|
if(called) {
|
||
3 years ago
|
traceEvent(TRACE_NORMAL, "ok, I am leaving now");
|
||
4 years ago
|
_exit(0);
|
||
|
} else {
|
||
3 years ago
|
traceEvent(TRACE_NORMAL, "shutting down...");
|
||
4 years ago
|
called = 1;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
keep_running = 0;
|
||
5 years ago
|
#ifdef WIN32
|
||
4 years ago
|
return(TRUE);
|
||
6 years ago
|
#endif
|
||
5 years ago
|
}
|
||
5 years ago
|
#endif /* defined(__linux__) || defined(WIN32) */
|
||
6 years ago
|
|
||
|
/* *************************************************** */
|
||
|
|
||
8 years ago
|
/** Main program entry point from kernel. */
|
||
4 years ago
|
int main (int argc, char * const argv[]) {
|
||
|
|
||
4 years ago
|
int rc;
|
||
4 years ago
|
#ifndef WIN32
|
||
4 years ago
|
struct passwd *pw = NULL;
|
||
4 years ago
|
#endif
|
||
4 years ago
|
struct peer_info *scan, *tmp;
|
||
|
|
||
6 years ago
|
|
||
3 years ago
|
sn_init_defaults(&sss_node);
|
||
4 years ago
|
add_federation_to_communities(&sss_node);
|
||
8 years ago
|
|
||
4 years ago
|
if((argc >= 2) && (argv[1][0] != '-')) {
|
||
|
rc = loadFromFile(argv[1], &sss_node);
|
||
|
if(argc > 2) {
|
||
|
rc = loadFromCLI(argc, argv, &sss_node);
|
||
|
}
|
||
|
} else if(argc > 1) {
|
||
|
rc = loadFromCLI(argc, argv, &sss_node);
|
||
|
} else
|
||
4 years ago
|
|
||
5 years ago
|
#ifdef WIN32
|
||
4 years ago
|
// load from current directory
|
||
4 years ago
|
rc = loadFromFile("supernode.conf", &sss_node);
|
||
5 years ago
|
#else
|
||
4 years ago
|
rc = -1;
|
||
5 years ago
|
#endif
|
||
8 years ago
|
|
||
4 years ago
|
if(rc < 0) {
|
||
3 years ago
|
help(1); /* short help */
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
if(sss_node.community_file)
|
||
|
load_allowed_sn_community(&sss_node);
|
||
|
|
||
8 years ago
|
#if defined(N2N_HAVE_DAEMON)
|
||
4 years ago
|
if(sss_node.daemon) {
|
||
|
setUseSyslog(1); /* traceEvent output now goes to syslog. */
|
||
6 years ago
|
|
||
4 years ago
|
if(-1 == daemon(0, 0)) {
|
||
3 years ago
|
traceEvent(TRACE_ERROR, "failed to become daemon");
|
||
4 years ago
|
exit(-5);
|
||
|
}
|
||
8 years ago
|
}
|
||
|
#endif /* #if defined(N2N_HAVE_DAEMON) */
|
||
|
|
||
3 years ago
|
// warn on default federation name
|
||
|
if(!strcmp(sss_node.federation->community, FEDERATION_NAME)) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "using default federation name; FOR TESTING ONLY, usage of a custom federation name (-F) is highly recommended!");
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
if(sss_node.override_spoofing_protection) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "disabled MAC and IP address spoofing protection; FOR TESTING ONLY, usage of user-password authentication (-I, -J, -P) recommended instead!");
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
calculate_shared_secrets(&sss_node);
|
||
3 years ago
|
|
||
4 years ago
|
traceEvent(TRACE_DEBUG, "traceLevel is %d", getTraceLevel());
|
||
|
|
||
3 years ago
|
sss_node.sock = open_socket(sss_node.lport, INADDR_ANY, 0 /* UDP */);
|
||
4 years ago
|
if(-1 == sss_node.sock) {
|
||
3 years ago
|
traceEvent(TRACE_ERROR, "failed to open main socket. %s", strerror(errno));
|
||
4 years ago
|
exit(-2);
|
||
|
} else {
|
||
|
traceEvent(TRACE_NORMAL, "supernode is listening on UDP %u (main)", sss_node.lport);
|
||
|
}
|
||
|
|
||
4 years ago
|
#ifdef N2N_HAVE_TCP
|
||
3 years ago
|
sss_node.tcp_sock = open_socket(sss_node.lport, INADDR_ANY, 1 /* TCP */);
|
||
4 years ago
|
if(-1 == sss_node.tcp_sock) {
|
||
3 years ago
|
traceEvent(TRACE_ERROR, "failed to open auxiliary TCP socket, %s", strerror(errno));
|
||
4 years ago
|
exit(-2);
|
||
|
} else {
|
||
|
traceEvent(TRACE_NORMAL, "supernode opened TCP %u (aux)", sss_node.lport);
|
||
|
}
|
||
|
|
||
|
if(-1 == listen(sss_node.tcp_sock, N2N_TCP_BACKLOG_QUEUE_SIZE)) {
|
||
3 years ago
|
traceEvent(TRACE_ERROR, "failed to listen on auxiliary TCP socket, %s", strerror(errno));
|
||
4 years ago
|
exit(-2);
|
||
|
} else {
|
||
|
traceEvent(TRACE_NORMAL, "supernode is listening on TCP %u (aux)", sss_node.lport);
|
||
|
}
|
||
|
#endif
|
||
|
|
||
3 years ago
|
sss_node.mgmt_sock = open_socket(sss_node.mport, INADDR_LOOPBACK, 0 /* UDP */);
|
||
4 years ago
|
if(-1 == sss_node.mgmt_sock) {
|
||
3 years ago
|
traceEvent(TRACE_ERROR, "failed to open management socket, %s", strerror(errno));
|
||
4 years ago
|
exit(-2);
|
||
|
} else {
|
||
|
traceEvent(TRACE_NORMAL, "supernode is listening on UDP %u (management)", sss_node.mport);
|
||
|
}
|
||
|
|
||
4 years ago
|
HASH_ITER(hh, sss_node.federation->edges, scan, tmp)
|
||
|
scan->socket_fd = sss_node.sock;
|
||
|
|
||
4 years ago
|
#ifndef WIN32
|
||
4 years ago
|
if(((pw = getpwnam ("n2n")) != NULL) || ((pw = getpwnam ("nobody")) != NULL)) {
|
||
|
sss_node.userid = sss_node.userid == 0 ? pw->pw_uid : 0;
|
||
|
sss_node.groupid = sss_node.groupid == 0 ? pw->pw_gid : 0;
|
||
|
}
|
||
|
if((sss_node.userid != 0) || (sss_node.groupid != 0)) {
|
||
3 years ago
|
traceEvent(TRACE_NORMAL, "dropping privileges to uid=%d, gid=%d",
|
||
4 years ago
|
(signed int)sss_node.userid, (signed int)sss_node.groupid);
|
||
|
|
||
|
/* Finished with the need for root privileges. Drop to unprivileged user. */
|
||
|
if((setgid(sss_node.groupid) != 0)
|
||
|
|| (setuid(sss_node.userid) != 0)) {
|
||
3 years ago
|
traceEvent(TRACE_ERROR, "unable to drop privileges [%u/%s]", errno, strerror(errno));
|
||
4 years ago
|
exit(1);
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
if((getuid() == 0) || (getgid() == 0)) {
|
||
3 years ago
|
traceEvent(TRACE_WARNING, "running as root is discouraged, check out the -u/-g options");
|
||
4 years ago
|
}
|
||
4 years ago
|
#endif
|
||
|
|
||
3 years ago
|
sn_init(&sss_node);
|
||
3 years ago
|
|
||
4 years ago
|
traceEvent(TRACE_NORMAL, "supernode started");
|
||
8 years ago
|
|
||
6 years ago
|
#ifdef __linux__
|
||
4 years ago
|
signal(SIGPIPE, SIG_IGN);
|
||
4 years ago
|
signal(SIGTERM, term_handler);
|
||
|
signal(SIGINT, term_handler);
|
||
|
signal(SIGHUP, dump_registrations);
|
||
6 years ago
|
#endif
|
||
5 years ago
|
#ifdef WIN32
|
||
4 years ago
|
SetConsoleCtrlHandler(term_handler, TRUE);
|
||
5 years ago
|
#endif
|
||
6 years ago
|
|
||
4 years ago
|
keep_running = 1;
|
||
|
return run_sn_loop(&sss_node, &keep_running);
|
||
8 years ago
|
}
|