Browse Source

readability code clean-up (#552)

* readability code clean-up

* readability code clean-up

* change space indention

* Revert "change space indention"

This reverts commit 067f675faa.
pull/569/head
Francesco Carli 4 years ago
committed by GitHub
parent
commit
ef2d3a5237
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 441
      src/sn.c
  2. 1941
      src/sn_utils.c

441
src/sn.c

@ -29,7 +29,8 @@ static const n2n_mac_t null_mac = {0, 0, 0, 0, 0, 0};
/** Load the list of allowed communities. Existing/previous ones will be removed /** Load the list of allowed communities. Existing/previous ones will be removed
* *
*/ */
static int load_allowed_sn_community(n2n_sn_t *sss, char *path) { static int load_allowed_sn_community (n2n_sn_t *sss, char *path) {
char buffer[4096], *line, *cmn_str, net_str[20]; char buffer[4096], *line, *cmn_str, net_str[20];
dec_ip_str_t ip_str = {'\0'}; dec_ip_str_t ip_str = {'\0'};
uint8_t bitlen; uint8_t bitlen;
@ -48,10 +49,13 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
} }
HASH_ITER(hh, sss->communities, s, tmp) { HASH_ITER(hh, sss->communities, s, tmp) {
if(s->is_federation) continue; if(s->is_federation) {
continue;
}
HASH_DEL(sss->communities, s); HASH_DEL(sss->communities, s);
if (NULL != s->header_encryption_ctx) if(NULL != s->header_encryption_ctx) {
free (s->header_encryption_ctx); free (s->header_encryption_ctx);
}
free(s); free(s);
} }
@ -63,37 +67,39 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
while((line = fgets(buffer, sizeof(buffer), fd)) != NULL) { while((line = fgets(buffer, sizeof(buffer), fd)) != NULL) {
int len = strlen(line); int len = strlen(line);
if((len < 2) || line[0] == '#') if(((len < 2) || line[0]) == '#') {
continue; continue;
}
len--; len--;
while(len > 0) { while(len > 0) {
if((line[len] == '\n') || (line[len] == '\r')) { if((line[len] == '\n') || (line[len] == '\r')) {
line[len] = '\0'; line[len] = '\0';
len--; len--;
} else } else {
break; break;
}
} }
// cut off any IP sub-network upfront // cut off any IP sub-network upfront
cmn_str = (char*)calloc(len+1, sizeof(char)); cmn_str = (char*)calloc(len + 1, sizeof(char));
has_net = ( sscanf (line, "%s %s", cmn_str, net_str) == 2 ); has_net = (sscanf(line, "%s %s", cmn_str, net_str) == 2);
// if it contains typical characters... // if it contains typical characters...
if(NULL != strpbrk(cmn_str, ".*+?[]\\")) { if(NULL != strpbrk(cmn_str, ".*+?[]\\")) {
// ...it is treated as regular expression // ...it is treated as regular expression
re = (struct sn_community_regular_expression*)calloc(1,sizeof(struct sn_community_regular_expression)); re = (struct sn_community_regular_expression*)calloc(1, sizeof(struct sn_community_regular_expression));
if (re) { if(re) {
re->rule = re_compile(cmn_str); re->rule = re_compile(cmn_str);
HASH_ADD_PTR(sss->rules, rule, re); HASH_ADD_PTR(sss->rules, rule, re);
num_regex++; num_regex++;
traceEvent(TRACE_INFO, "Added regular expression for allowed communities '%s'", cmn_str); traceEvent(TRACE_INFO, "Added regular expression for allowed communities '%s'", cmn_str);
free(cmn_str); free(cmn_str);
continue; continue;
} }
} }
s = (struct sn_community*)calloc(1,sizeof(struct sn_community)); s = (struct sn_community*)calloc(1, sizeof(struct sn_community));
if(s != NULL) { if(s != NULL) {
comm_init(s,cmn_str); comm_init(s,cmn_str);
@ -107,26 +113,26 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
num_communities++; num_communities++;
traceEvent(TRACE_INFO, "Added allowed community '%s' [total: %u]", traceEvent(TRACE_INFO, "Added allowed community '%s' [total: %u]",
(char*)s->community, num_communities); (char*)s->community, num_communities);
// check for sub-network address // check for sub-network address
if(has_net) { if(has_net) {
if(sscanf(net_str, "%15[^/]/%hhu", ip_str, &bitlen) != 2) { if(sscanf(net_str, "%15[^/]/%hhu", ip_str, &bitlen) != 2) {
traceEvent(TRACE_WARNING, "Bad net/bit format '%s' for community '%c', ignoring. See comments inside community.list file.", traceEvent(TRACE_WARNING, "Bad net/bit format '%s' for community '%c', ignoring. See comments inside community.list file.",
net_str, cmn_str); net_str, cmn_str);
has_net = 0; has_net = 0;
} }
net = inet_addr(ip_str); net = inet_addr(ip_str);
mask = bitlen2mask(bitlen); mask = bitlen2mask(bitlen);
if((net == (in_addr_t)(-1)) || (net == INADDR_NONE) || (net == INADDR_ANY) if((net == (in_addr_t)(-1)) || (net == INADDR_NONE) || (net == INADDR_ANY)
|| ((ntohl(net) & ~mask) != 0) ) { || ((ntohl(net) & ~mask) != 0) ) {
traceEvent(TRACE_WARNING, "Bad network '%s/%u' in '%s' for community '%s', ignoring.", traceEvent(TRACE_WARNING, "Bad network '%s/%u' in '%s' for community '%s', ignoring.",
ip_str, bitlen, net_str, cmn_str); ip_str, bitlen, net_str, cmn_str);
has_net = 0; has_net = 0;
} }
if ((bitlen > 30) || (bitlen == 0)) { if((bitlen > 30) || (bitlen == 0)) {
traceEvent(TRACE_WARNING, "Bad prefix '%hhu' in '%s' for community '%s', ignoring.", traceEvent(TRACE_WARNING, "Bad prefix '%hhu' in '%s' for community '%s', ignoring.",
bitlen, net_str, cmn_str); bitlen, net_str, cmn_str);
has_net = 0; has_net = 0;
} }
} }
@ -134,9 +140,9 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
s->auto_ip_net.net_addr = ntohl(net); s->auto_ip_net.net_addr = ntohl(net);
s->auto_ip_net.net_bitlen = bitlen; s->auto_ip_net.net_bitlen = bitlen;
traceEvent(TRACE_INFO, "Assigned sub-network %s/%u to community '%s'.", traceEvent(TRACE_INFO, "Assigned sub-network %s/%u to community '%s'.",
inet_ntoa(*(struct in_addr *) &net), inet_ntoa(*(struct in_addr *) &net),
s->auto_ip_net.net_bitlen, s->auto_ip_net.net_bitlen,
s->community); s->community);
} else { } else {
assign_one_ip_subnet(sss, s); assign_one_ip_subnet(sss, s);
} }
@ -148,17 +154,16 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
fclose(fd); fclose(fd);
if ((num_regex + num_communities) == 0) if((num_regex + num_communities) == 0) {
{ traceEvent(TRACE_WARNING, "File %s does not contain any valid community names or regular expressions", path);
traceEvent(TRACE_WARNING, "File %s does not contain any valid community names or regular expressions", path); return -1;
return -1; }
}
traceEvent(TRACE_NORMAL, "Loaded %u fixed-name communities from %s", traceEvent(TRACE_NORMAL, "Loaded %u fixed-name communities from %s",
num_communities, path); num_communities, path);
traceEvent(TRACE_NORMAL, "Loaded %u regular expressions for community name matching from %s", traceEvent(TRACE_NORMAL, "Loaded %u regular expressions for community name matching from %s",
num_regex, path); num_regex, path);
/* No new communities will be allowed */ /* No new communities will be allowed */
sss->lock_communities = 1; sss->lock_communities = 1;
@ -170,12 +175,13 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
/* *************************************************** */ /* *************************************************** */
/** Help message to print if the command line arguments are not valid. */ /** Help message to print if the command line arguments are not valid. */
static void help() { static void help () {
print_n2n_version(); print_n2n_version();
printf("supernode <config file> (see supernode.conf)\n" printf("supernode <config file> (see supernode.conf)\n"
"or\n" "or\n"
); );
printf("supernode "); printf("supernode ");
printf("-p <local port> "); printf("-p <local port> ");
printf("-c <path> "); printf("-c <path> ");
@ -222,166 +228,164 @@ static void help() {
/* *************************************************** */ /* *************************************************** */
static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) { static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) {
//traceEvent(TRACE_NORMAL, "Option %c = %s", optkey, _optarg ? _optarg : ""); //traceEvent(TRACE_NORMAL, "Option %c = %s", optkey, _optarg ? _optarg : "");
switch (optkey) { switch(optkey) {
case 'p': /* local-port */ case 'p': /* local-port */
sss->lport = atoi(_optarg); sss->lport = atoi(_optarg);
if(sss->lport == 0) {
traceEvent(TRACE_WARNING, "Bad local port format");
break;
}
if(sss->lport == 0){
traceEvent(TRACE_WARNING, "Bad local port format");
break; break;
}
break; case 't': /* mgmt-port */
sss->mport = atoi(_optarg);
case 't': /* mgmt-port */ if(sss->mport == 0) {
sss->mport = atoi(_optarg); traceEvent(TRACE_WARNING, "Bad management port format");
break;
}
if(sss->mport == 0){
traceEvent(TRACE_WARNING, "Bad management port format");
break; break;
}
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) {
traceEvent(TRACE_WARNING, "Size of -l argument too long: %zu. Maximum size is %d", length, N2N_EDGE_SN_HOST_SIZE);
break;
}
case 'l': { /* supernode:port */ if(!double_column) {
n2n_sock_t *socket; traceEvent(TRACE_WARNING, "Invalid -l format: ignored");
struct peer_info *anchor_sn; return (-1);
size_t length; }
int rv = -1;
int skip_add;
char *double_column = strchr(_optarg, ':');
length = strlen(_optarg); socket = (n2n_sock_t *)calloc(1, sizeof(n2n_sock_t));
if(length >= N2N_EDGE_SN_HOST_SIZE) { rv = supernode2sock(socket, _optarg);
traceEvent(TRACE_WARNING, "Size of -l argument too long: %zu. Maximum size is %d", length, N2N_EDGE_SN_HOST_SIZE);
break;
}
if(!double_column){ if(rv != 0) {
traceEvent(TRACE_WARNING, "Invalid -l format: ignored"); traceEvent(TRACE_WARNING, "Invalid socket");
return (-1); free(socket);
} break;
}
socket = (n2n_sock_t *)calloc(1,sizeof(n2n_sock_t)); if(sss->federation != NULL) {
rv = supernode2sock(socket, _optarg); skip_add = SN_ADD;
anchor_sn = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), socket, (n2n_mac_t*) null_mac, &skip_add);
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));
memcpy(&(anchor_sn->mac_addr), null_mac, sizeof(n2n_mac_t));
anchor_sn->purgeable = SN_UNPURGEABLE;
anchor_sn->last_valid_time_stamp = initial_time_stamp();
}
}
}
if(rv != 0){
traceEvent(TRACE_WARNING, "Invalid socket");
free(socket); free(socket);
break; break;
} }
if(sss->federation != NULL) { 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;
skip_add = SN_ADD; if(sscanf(_optarg, "%15[^\\-]-%15[^/]/%hhu", ip_min_str, ip_max_str, &bitlen) != 3) {
anchor_sn = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), socket, (n2n_mac_t*) null_mac, &skip_add); traceEvent(TRACE_WARNING, "Bad net-net/bit format '%s'. See -h.", _optarg);
break;
}
if(anchor_sn != NULL){ net_min = inet_addr(ip_min_str);
anchor_sn->ip_addr = calloc(1,N2N_EDGE_SN_HOST_SIZE); net_max = inet_addr(ip_max_str);
if(anchor_sn->ip_addr){ mask = bitlen2mask(bitlen);
strncpy(anchor_sn->ip_addr,_optarg,N2N_EDGE_SN_HOST_SIZE-1); if((net_min == (in_addr_t)(-1)) || (net_min == INADDR_NONE) || (net_min == INADDR_ANY)
memcpy(&(anchor_sn->sock), socket, sizeof(n2n_sock_t)); || (net_max == (in_addr_t)(-1)) || (net_max == INADDR_NONE) || (net_max == INADDR_ANY)
memcpy(&(anchor_sn->mac_addr),null_mac,sizeof(n2n_mac_t)); || (ntohl(net_min) > ntohl(net_max))
anchor_sn->purgeable = SN_UNPURGEABLE; || ((ntohl(net_min) & ~mask) != 0) || ((ntohl(net_max) & ~mask) != 0) ) {
anchor_sn->last_valid_time_stamp = initial_time_stamp(); traceEvent(TRACE_WARNING, "Bad network range '%s...%s/%u' in '%s', defaulting to '%s...%s/%d'",
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);
break;
}
} if((bitlen > 30) || (bitlen == 0)) {
traceEvent(TRACE_WARNING, "Bad prefix '%hhu' in '%s', defaulting to '%s...%s/%d'",
bitlen, _optarg,
N2N_SN_MIN_AUTO_IP_NET_DEFAULT, N2N_SN_MAX_AUTO_IP_NET_DEFAULT, N2N_SN_AUTO_IP_NET_BIT_DEFAULT);
break;
} }
}
free(socket); traceEvent(TRACE_NORMAL, "The network range for community ip address service is '%s...%s/%hhu'.", ip_min_str, ip_max_str, bitlen);
break;
}
case 'a': { sss->min_auto_ip_net.net_addr = ntohl(net_min);
dec_ip_str_t ip_min_str = {'\0'}; sss->min_auto_ip_net.net_bitlen = bitlen;
dec_ip_str_t ip_max_str = {'\0'}; sss->max_auto_ip_net.net_addr = ntohl(net_max);
in_addr_t net_min, net_max; sss->max_auto_ip_net.net_bitlen = bitlen;
uint8_t bitlen;
uint32_t mask;
if (sscanf(_optarg, "%15[^\\-]-%15[^/]/%hhu", ip_min_str, ip_max_str, &bitlen) != 3) {
traceEvent(TRACE_WARNING, "Bad net-net/bit format '%s'. See -h.", _optarg);
break; break;
} }
net_min = inet_addr(ip_min_str); #ifndef WIN32
net_max = inet_addr(ip_max_str); case 'u': /* unprivileged uid */
mask = bitlen2mask(bitlen); sss->userid = atoi(_optarg);
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) ) {
traceEvent(TRACE_WARNING, "Bad network range '%s...%s/%u' in '%s', defaulting to '%s...%s/%d'",
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);
break; break;
}
if ((bitlen > 30) || (bitlen == 0)) { case 'g': /* unprivileged uid */
traceEvent(TRACE_WARNING, "Bad prefix '%hhu' in '%s', defaulting to '%s...%s/%d'", sss->groupid = atoi(_optarg);
bitlen, _optarg,
N2N_SN_MIN_AUTO_IP_NET_DEFAULT, N2N_SN_MAX_AUTO_IP_NET_DEFAULT, N2N_SN_AUTO_IP_NET_BIT_DEFAULT);
break; break;
}
traceEvent(TRACE_NORMAL, "The network range for community ip address service is '%s...%s/%hhu'.", ip_min_str, ip_max_str, bitlen);
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;
}
#ifndef WIN32
case 'u': /* unprivileged uid */
sss->userid = atoi(_optarg);
break;
case 'g': /* unprivileged uid */
sss->groupid = atoi(_optarg);
break;
#endif #endif
case 'F': { /* federation name */ case 'F': { /* federation name */
snprintf(sss->federation->community, N2N_COMMUNITY_SIZE - 1, "*%s", _optarg);
snprintf(sss->federation->community,N2N_COMMUNITY_SIZE-1,"*%s",_optarg); sss->federation->community[N2N_COMMUNITY_SIZE - 1] = '\0';
sss->federation->community[N2N_COMMUNITY_SIZE-1] = '\0';
break; break;
} }
#if 0 #if 0
case 'm': {/* MAC address */ case 'm': {/* MAC address */
str2mac(sss->mac_addr,_optarg); str2mac(sss->mac_addr,_optarg);
break; break;
} }
#endif /* #if 0 */ #endif /* #if 0 */
case 'c': /* community file */ case 'c': /* community file */
load_allowed_sn_community(sss, _optarg); load_allowed_sn_community(sss, _optarg);
break; break;
case 'f': /* foreground */ case 'f': /* foreground */
sss->daemon = 0; sss->daemon = 0;
break; break;
case 'h': /* help */ case 'h': /* help */
help(); help();
break; break;
case 'v': /* verbose */ case 'v': /* verbose */
setTraceLevel(getTraceLevel() + 1); setTraceLevel(getTraceLevel() + 1);
break; break;
default: default:
traceEvent(TRACE_WARNING, "Unknown option -%c: Ignored.", (char) optkey); traceEvent(TRACE_WARNING, "Unknown option -%c: Ignored.", (char)optkey);
return (-1); return (-1);
} }
return (0); return (0);
@ -404,12 +408,15 @@ static const struct option long_options[] = {
/* *************************************************** */ /* *************************************************** */
/* read command line options */ /* read command line options */
static int loadFromCLI(int argc, char * const argv[], n2n_sn_t *sss) { static int loadFromCLI (int argc, char * const argv[], n2n_sn_t *sss) {
u_char c; u_char c;
while((c = getopt_long(argc, argv, "fp:l:u:g:t:a:c:F:m:vh", while((c = getopt_long(argc, argv, "fp:l:u:g:t:a:c:F:m:vh",
long_options, NULL)) != '?') { long_options, NULL)) != '?') {
if(c == 255) break; if(c == 255) {
break;
}
setOption(c, optarg, sss); setOption(c, optarg, sss);
} }
@ -418,18 +425,22 @@ static int loadFromCLI(int argc, char * const argv[], n2n_sn_t *sss) {
/* *************************************************** */ /* *************************************************** */
static char *trim(char *s) { static char *trim (char *s) {
char *end; char *end;
while(isspace(s[0]) || (s[0] == '"') || (s[0] == '\'')) while(isspace(s[0]) || (s[0] == '"') || (s[0] == '\'')) {
s++; s++;
}
if(s[0] == 0) return s; if(s[0] == 0) {
return s;
}
end = &s[strlen(s) - 1]; end = &s[strlen(s) - 1];
while(end > s while(end > s && (isspace(end[0])|| (end[0] == '"') || (end[0] == '\''))) {
&& (isspace(end[0])|| (end[0] == '"') || (end[0] == '\'')))
end--; end--;
}
end[1] = 0; end[1] = 0;
return s; return s;
@ -438,7 +449,8 @@ static char *trim(char *s) {
/* *************************************************** */ /* *************************************************** */
/* parse the configuration file */ /* parse the configuration file */
static int loadFromFile(const char *path, n2n_sn_t *sss) { static int loadFromFile (const char *path, n2n_sn_t *sss) {
char buffer[4096], *line, *key, *value; char buffer[4096], *line, *key, *value;
u_int line_len, opt_name_len; u_int line_len, opt_name_len;
FILE *fd; FILE *fd;
@ -452,39 +464,47 @@ static int loadFromFile(const char *path, n2n_sn_t *sss) {
} }
while((line = fgets(buffer, sizeof(buffer), fd)) != NULL) { while((line = fgets(buffer, sizeof(buffer), fd)) != NULL) {
line = trim(line); line = trim(line);
value = NULL; value = NULL;
if((line_len = strlen(line)) < 2 || line[0] == '#') if((line_len = strlen(line)) < 2 || line[0] == '#') {
continue; continue;
}
if(!strncmp(line, "--", 2)) { /* long opt */ if(!strncmp(line, "--", 2)) { /* long opt */
key = &line[2], line_len -= 2; key = &line[2], line_len -= 2;
opt = long_options; opt = long_options;
while(opt->name != NULL) { while(opt->name != NULL) {
opt_name_len = strlen(opt->name); opt_name_len = strlen(opt->name);
if(!strncmp(key, opt->name, opt_name_len) if(!strncmp(key, opt->name, opt_name_len)
&& (line_len <= opt_name_len && (line_len <= opt_name_len
|| key[opt_name_len] == '\0' || key[opt_name_len] == '\0'
|| key[opt_name_len] == ' ' || key[opt_name_len] == ' '
|| key[opt_name_len] == '=')) { || key[opt_name_len] == '=')) {
if(line_len > opt_name_len) key[opt_name_len] = '\0'; if(line_len > opt_name_len) {
if(line_len > opt_name_len + 1) value = trim(&key[opt_name_len + 1]); key[opt_name_len] = '\0';
}
// traceEvent(TRACE_NORMAL, "long key: %s value: %s", key, value); if(line_len > opt_name_len + 1) {
setOption(opt->val, value, sss); value = trim(&key[opt_name_len + 1]);
break; }
}
// traceEvent(TRACE_NORMAL, "long key: %s value: %s", key, value);
opt++; setOption(opt->val, value, sss);
break;
}
opt++;
} }
} else if(line[0] == '-') { /* short opt */ } else if(line[0] == '-') { /* short opt */
key = &line[1], line_len--; key = &line[1], line_len--;
if(line_len > 1) key[1] = '\0'; if(line_len > 1) {
if(line_len > 2) value = trim(&key[2]); key[1] = '\0';
}
if(line_len > 2) {
value = trim(&key[2]);
}
// traceEvent(TRACE_NORMAL, "key: %c value: %s", key[0], value); // traceEvent(TRACE_NORMAL, "key: %c value: %s", key[0], value);
setOption(key[0], value, sss); setOption(key[0], value, sss);
@ -502,16 +522,15 @@ static int loadFromFile(const char *path, n2n_sn_t *sss) {
/* *************************************************** */ /* *************************************************** */
/* Add the federation to the communities list of a supernode */ /* Add the federation to the communities list of a supernode */
static int add_federation_to_communities(n2n_sn_t *sss){ static int add_federation_to_communities (n2n_sn_t *sss) {
uint32_t num_communities = 0; uint32_t num_communities = 0;
if(sss->federation != NULL) { if(sss->federation != NULL) {
HASH_ADD_STR(sss->communities, community, sss->federation); HASH_ADD_STR(sss->communities, community, sss->federation);
num_communities = HASH_COUNT(sss->communities); num_communities = HASH_COUNT(sss->communities);
traceEvent(TRACE_INFO, "Added federation '%s' to the list of communities [total: %u]", traceEvent(TRACE_INFO, "Added federation '%s' to the list of communities [total: %u]",
(char*)sss->federation->community, num_communities); (char*)sss->federation->community, num_communities);
} }
return 0; return 0;
@ -520,7 +539,8 @@ static int add_federation_to_communities(n2n_sn_t *sss){
/* *************************************************** */ /* *************************************************** */
#ifdef __linux__ #ifdef __linux__
static void dump_registrations(int signo) { static void dump_registrations (int signo) {
struct sn_community *comm, *ctmp; struct sn_community *comm, *ctmp;
struct peer_info *list, *tmp; struct peer_info *list, *tmp;
char buf[32]; char buf[32];
@ -533,16 +553,17 @@ static void dump_registrations(int signo) {
traceEvent(TRACE_NORMAL, "Dumping community: %s", comm->community); traceEvent(TRACE_NORMAL, "Dumping community: %s", comm->community);
HASH_ITER(hh, comm->edges, list, tmp) { HASH_ITER(hh, comm->edges, list, tmp) {
if(list->sock.family == AF_INET) if(list->sock.family == AF_INET) {
traceEvent(TRACE_NORMAL, "[id: %u][MAC: %s][edge: %u.%u.%u.%u:%u][last seen: %u sec ago]", 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), ++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.addr.v4[0], list->sock.addr.v4[1], list->sock.addr.v4[2], list->sock.addr.v4[3],
list->sock.port, list->sock.port,
now-list->last_seen); now-list->last_seen);
else } else {
traceEvent(TRACE_NORMAL, "[id: %u][MAC: %s][edge: IPv6:%u][last seen: %u sec ago]", 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, ++num, macaddr_str(buf, list->mac_addr), list->sock.port,
now-list->last_seen); now-list->last_seen);
}
} }
} }
@ -556,9 +577,9 @@ static int keep_running;
#if defined(__linux__) || defined(WIN32) #if defined(__linux__) || defined(WIN32)
#ifdef WIN32 #ifdef WIN32
BOOL WINAPI term_handler(DWORD sig) BOOL WINAPI term_handler (DWORD sig)
#else #else
static void term_handler(int sig) static void term_handler (int sig)
#endif #endif
{ {
static int called = 0; static int called = 0;
@ -581,7 +602,8 @@ BOOL WINAPI term_handler(DWORD sig)
/* *************************************************** */ /* *************************************************** */
/** Main program entry point from kernel. */ /** Main program entry point from kernel. */
int main(int argc, char * const argv[]) { int main (int argc, char * const argv[]) {
int rc; int rc;
#ifndef WIN32 #ifndef WIN32
struct passwd *pw = NULL; struct passwd *pw = NULL;
@ -592,11 +614,12 @@ int main(int argc, char * const argv[]) {
if((argc >= 2) && (argv[1][0] != '-')) { if((argc >= 2) && (argv[1][0] != '-')) {
rc = loadFromFile(argv[1], &sss_node); rc = loadFromFile(argv[1], &sss_node);
if(argc > 2) if(argc > 2) {
rc = loadFromCLI(argc, argv, &sss_node); rc = loadFromCLI(argc, argv, &sss_node);
} else if(argc > 1) }
} else if(argc > 1) {
rc = loadFromCLI(argc, argv, &sss_node); rc = loadFromCLI(argc, argv, &sss_node);
else } else
#ifdef WIN32 #ifdef WIN32
/* Load from current directory */ /* Load from current directory */
rc = loadFromFile("supernode.conf", &sss_node); rc = loadFromFile("supernode.conf", &sss_node);
@ -604,8 +627,9 @@ int main(int argc, char * const argv[]) {
rc = -1; rc = -1;
#endif #endif
if(rc < 0) if(rc < 0) {
help(); help();
}
#if defined(N2N_HAVE_DAEMON) #if defined(N2N_HAVE_DAEMON)
if(sss_node.daemon) { if(sss_node.daemon) {
@ -632,17 +656,17 @@ int main(int argc, char * const argv[]) {
if(-1 == sss_node.mgmt_sock) { if(-1 == sss_node.mgmt_sock) {
traceEvent(TRACE_ERROR, "Failed to open management socket. %s", strerror(errno)); traceEvent(TRACE_ERROR, "Failed to open management socket. %s", strerror(errno));
exit(-2); exit(-2);
} else } else {
traceEvent(TRACE_NORMAL, "supernode is listening on UDP %u (management)", sss_node.mport); traceEvent(TRACE_NORMAL, "supernode is listening on UDP %u (management)", sss_node.mport);
}
#ifndef WIN32 #ifndef WIN32
if (((pw = getpwnam ("n2n")) != NULL) || ((pw = getpwnam ("nobody")) != NULL)) { if(((pw = getpwnam ("n2n")) != NULL) || ((pw = getpwnam ("nobody")) != NULL)) {
sss_node.userid = sss_node.userid == 0 ? pw->pw_uid : 0; sss_node.userid = sss_node.userid == 0 ? pw->pw_uid : 0;
sss_node.groupid = sss_node.groupid == 0 ? pw->pw_gid : 0; sss_node.groupid = sss_node.groupid == 0 ? pw->pw_gid : 0;
} }
if((sss_node.userid != 0) || (sss_node.groupid != 0)) { if((sss_node.userid != 0) || (sss_node.groupid != 0)) {
traceEvent(TRACE_NORMAL, "Dropping privileges to uid=%d, gid=%d", traceEvent(TRACE_NORMAL, "Dropping privileges to uid=%d, gid=%d",
(signed int)sss_node.userid, (signed int)sss_node.groupid); (signed int)sss_node.userid, (signed int)sss_node.groupid);
/* Finished with the need for root privileges. Drop to unprivileged user. */ /* Finished with the need for root privileges. Drop to unprivileged user. */
if((setgid(sss_node.groupid) != 0) if((setgid(sss_node.groupid) != 0)
@ -652,8 +676,9 @@ int main(int argc, char * const argv[]) {
} }
} }
if((getuid() == 0) || (getgid() == 0)) if((getuid() == 0) || (getgid() == 0)) {
traceEvent(TRACE_WARNING, "Running as root is discouraged, check out the -u/-g options"); traceEvent(TRACE_WARNING, "Running as root is discouraged, check out the -u/-g options");
}
#endif #endif
traceEvent(TRACE_NORMAL, "supernode started"); traceEvent(TRACE_NORMAL, "supernode started");

1941
src/sn_utils.c

File diff suppressed because it is too large
Loading…
Cancel
Save