readability code clean-up

This commit is contained in:
fcarli3 2020-12-19 16:51:42 +01:00
parent df14d54a29
commit 521a22f90b
2 changed files with 1213 additions and 1173 deletions

157
src/sn.c
View File

@ -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
*
*/
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];
dec_ip_str_t ip_str = {'\0'};
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) {
if(s->is_federation) continue;
if(s->is_federation) {
continue;
}
HASH_DEL(sss->communities, s);
if (NULL != s->header_encryption_ctx)
if(NULL != s->header_encryption_ctx) {
free (s->header_encryption_ctx);
}
free(s);
}
@ -63,27 +67,29 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
while((line = fgets(buffer, sizeof(buffer), fd)) != NULL) {
int len = strlen(line);
if((len < 2) || line[0] == '#')
if(((len < 2) || line[0]) == '#') {
continue;
}
len--;
while(len > 0) {
if((line[len] == '\n') || (line[len] == '\r')) {
line[len] = '\0';
len--;
} else
} else {
break;
}
}
// cut off any IP sub-network upfront
cmn_str = (char*)calloc(len+1, sizeof(char));
has_net = ( sscanf (line, "%s %s", cmn_str, net_str) == 2 );
cmn_str = (char*)calloc(len + 1, sizeof(char));
has_net = (sscanf(line, "%s %s", cmn_str, net_str) == 2);
// if it contains typical characters...
if(NULL != strpbrk(cmn_str, ".*+?[]\\")) {
// ...it is treated as regular expression
re = (struct sn_community_regular_expression*)calloc(1,sizeof(struct sn_community_regular_expression));
if (re) {
re = (struct sn_community_regular_expression*)calloc(1, sizeof(struct sn_community_regular_expression));
if(re) {
re->rule = re_compile(cmn_str);
HASH_ADD_PTR(sss->rules, rule, re);
num_regex++;
@ -93,7 +99,7 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
}
}
s = (struct sn_community*)calloc(1,sizeof(struct sn_community));
s = (struct sn_community*)calloc(1, sizeof(struct sn_community));
if(s != NULL) {
comm_init(s,cmn_str);
@ -124,7 +130,7 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
ip_str, bitlen, net_str, cmn_str);
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.",
bitlen, net_str, cmn_str);
has_net = 0;
@ -148,8 +154,7 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
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);
return -1;
}
@ -170,7 +175,8 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
/* *************************************************** */
/** Help message to print if the command line arguments are not valid. */
static void help() {
static void help () {
print_n2n_version();
printf("supernode <config file> (see supernode.conf)\n"
@ -222,14 +228,15 @@ 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 : "");
switch (optkey) {
switch(optkey) {
case 'p': /* local-port */
sss->lport = atoi(_optarg);
if(sss->lport == 0){
if(sss->lport == 0) {
traceEvent(TRACE_WARNING, "Bad local port format");
break;
}
@ -239,7 +246,7 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
case 't': /* mgmt-port */
sss->mport = atoi(_optarg);
if(sss->mport == 0){
if(sss->mport == 0) {
traceEvent(TRACE_WARNING, "Bad management port format");
break;
}
@ -260,34 +267,32 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
break;
}
if(!double_column){
if(!double_column) {
traceEvent(TRACE_WARNING, "Invalid -l format: ignored");
return (-1);
}
socket = (n2n_sock_t *)calloc(1,sizeof(n2n_sock_t));
socket = (n2n_sock_t *)calloc(1, sizeof(n2n_sock_t));
rv = supernode2sock(socket, _optarg);
if(rv != 0){
if(rv != 0) {
traceEvent(TRACE_WARNING, "Invalid socket");
free(socket);
break;
}
if(sss->federation != NULL) {
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){
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));
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();
}
}
}
@ -303,7 +308,7 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
uint8_t bitlen;
uint32_t mask;
if (sscanf(_optarg, "%15[^\\-]-%15[^/]/%hhu", ip_min_str, ip_max_str, &bitlen) != 3) {
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;
}
@ -311,7 +316,7 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
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)
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) ) {
@ -321,7 +326,7 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
break;
}
if ((bitlen > 30) || (bitlen == 0)) {
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);
@ -349,9 +354,8 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
#endif
case 'F': { /* federation name */
snprintf(sss->federation->community,N2N_COMMUNITY_SIZE-1,"*%s",_optarg);
sss->federation->community[N2N_COMMUNITY_SIZE-1] = '\0';
snprintf(sss->federation->community, N2N_COMMUNITY_SIZE - 1, "*%s", _optarg);
sss->federation->community[N2N_COMMUNITY_SIZE - 1] = '\0';
break;
}
@ -380,7 +384,7 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
break;
default:
traceEvent(TRACE_WARNING, "Unknown option -%c: Ignored.", (char) optkey);
traceEvent(TRACE_WARNING, "Unknown option -%c: Ignored.", (char)optkey);
return (-1);
}
@ -404,12 +408,15 @@ static const struct option long_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;
while((c = getopt_long(argc, argv, "fp:l:u:g:t:a:c:F:m:vh",
long_options, NULL)) != '?') {
if(c == 255) break;
if(c == 255) {
break;
}
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;
while(isspace(s[0]) || (s[0] == '"') || (s[0] == '\''))
while(isspace(s[0]) || (s[0] == '"') || (s[0] == '\'')) {
s++;
}
if(s[0] == 0) return s;
if(s[0] == 0) {
return s;
}
end = &s[strlen(s) - 1];
while(end > s
&& (isspace(end[0])|| (end[0] == '"') || (end[0] == '\'')))
while(end > s && (isspace(end[0])|| (end[0] == '"') || (end[0] == '\''))) {
end--;
}
end[1] = 0;
return s;
@ -438,7 +449,8 @@ static char *trim(char *s) {
/* *************************************************** */
/* 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;
u_int line_len, opt_name_len;
FILE *fd;
@ -452,12 +464,12 @@ static int loadFromFile(const char *path, n2n_sn_t *sss) {
}
while((line = fgets(buffer, sizeof(buffer), fd)) != NULL) {
line = trim(line);
value = NULL;
if((line_len = strlen(line)) < 2 || line[0] == '#')
if((line_len = strlen(line)) < 2 || line[0] == '#') {
continue;
}
if(!strncmp(line, "--", 2)) { /* long opt */
key = &line[2], line_len -= 2;
@ -471,8 +483,12 @@ static int loadFromFile(const char *path, n2n_sn_t *sss) {
|| key[opt_name_len] == '\0'
|| 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 + 1) value = trim(&key[opt_name_len + 1]);
if(line_len > opt_name_len) {
key[opt_name_len] = '\0';
}
if(line_len > opt_name_len + 1) {
value = trim(&key[opt_name_len + 1]);
}
// traceEvent(TRACE_NORMAL, "long key: %s value: %s", key, value);
setOption(opt->val, value, sss);
@ -483,8 +499,12 @@ static int loadFromFile(const char *path, n2n_sn_t *sss) {
}
} else if(line[0] == '-') { /* short opt */
key = &line[1], line_len--;
if(line_len > 1) key[1] = '\0';
if(line_len > 2) value = trim(&key[2]);
if(line_len > 1) {
key[1] = '\0';
}
if(line_len > 2) {
value = trim(&key[2]);
}
// traceEvent(TRACE_NORMAL, "key: %c value: %s", key[0], value);
setOption(key[0], value, sss);
@ -502,14 +522,13 @@ static int loadFromFile(const char *path, n2n_sn_t *sss) {
/* *************************************************** */
/* 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;
if(sss->federation != NULL) {
HASH_ADD_STR(sss->communities, community, sss->federation);
num_communities = HASH_COUNT(sss->communities);
traceEvent(TRACE_INFO, "Added federation '%s' to the list of communities [total: %u]",
(char*)sss->federation->community, num_communities);
}
@ -520,7 +539,8 @@ static int add_federation_to_communities(n2n_sn_t *sss){
/* *************************************************** */
#ifdef __linux__
static void dump_registrations(int signo) {
static void dump_registrations (int signo) {
struct sn_community *comm, *ctmp;
struct peer_info *list, *tmp;
char buf[32];
@ -533,18 +553,19 @@ static void dump_registrations(int signo) {
traceEvent(TRACE_NORMAL, "Dumping community: %s", comm->community);
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]",
++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
} 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);
}
}
}
traceEvent(TRACE_NORMAL, "====================================");
}
@ -556,9 +577,9 @@ static int keep_running;
#if defined(__linux__) || defined(WIN32)
#ifdef WIN32
BOOL WINAPI term_handler(DWORD sig)
BOOL WINAPI term_handler (DWORD sig)
#else
static void term_handler(int sig)
static void term_handler (int sig)
#endif
{
static int called = 0;
@ -581,7 +602,8 @@ BOOL WINAPI term_handler(DWORD sig)
/* *************************************************** */
/** Main program entry point from kernel. */
int main(int argc, char * const argv[]) {
int main (int argc, char * const argv[]) {
int rc;
#ifndef WIN32
struct passwd *pw = NULL;
@ -592,11 +614,12 @@ int main(int argc, char * const argv[]) {
if((argc >= 2) && (argv[1][0] != '-')) {
rc = loadFromFile(argv[1], &sss_node);
if(argc > 2)
if(argc > 2) {
rc = loadFromCLI(argc, argv, &sss_node);
} else if(argc > 1)
}
} else if(argc > 1) {
rc = loadFromCLI(argc, argv, &sss_node);
else
} else
#ifdef WIN32
/* Load from current directory */
rc = loadFromFile("supernode.conf", &sss_node);
@ -604,8 +627,9 @@ int main(int argc, char * const argv[]) {
rc = -1;
#endif
if(rc < 0)
if(rc < 0) {
help();
}
#if defined(N2N_HAVE_DAEMON)
if(sss_node.daemon) {
@ -632,11 +656,11 @@ int main(int argc, char * const argv[]) {
if(-1 == sss_node.mgmt_sock) {
traceEvent(TRACE_ERROR, "Failed to open management socket. %s", strerror(errno));
exit(-2);
} else
} else {
traceEvent(TRACE_NORMAL, "supernode is listening on UDP %u (management)", sss_node.mport);
}
#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.groupid = sss_node.groupid == 0 ? pw->pw_gid : 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");
}
#endif
traceEvent(TRACE_NORMAL, "supernode started");

File diff suppressed because it is too large Load Diff