change space indention

This commit is contained in:
fcarli3 2020-12-21 12:46:30 +01:00
parent 102ff18aaa
commit 067f675faa
13 changed files with 6279 additions and 6270 deletions

View File

@ -112,7 +112,7 @@ static int scan_address (char * ip_addr, size_t addr_size,
/* *************************************************** */ /* *************************************************** */
static void help() { static void help () {
print_n2n_version(); print_n2n_version();
printf("edge <config file> (see edge.conf)\n" printf("edge <config file> (see edge.conf)\n"
@ -211,6 +211,7 @@ static void help() {
/* *************************************************** */ /* *************************************************** */
static void setPayloadCompression (n2n_edge_conf_t *conf, int compression) { static void setPayloadCompression (n2n_edge_conf_t *conf, int compression) {
/* even though 'compression' and 'conf->compression' share the same encoding scheme, /* even though 'compression' and 'conf->compression' share the same encoding scheme,
* a switch-statement under conditional compilation is used to sort out the * a switch-statement under conditional compilation is used to sort out the
* unsupported optarguments */ * unsupported optarguments */
@ -238,6 +239,7 @@ static void setPayloadCompression (n2n_edge_conf_t *conf, int compression) {
/* *************************************************** */ /* *************************************************** */
static void setPayloadEncryption (n2n_edge_conf_t *conf, int cipher) { static void setPayloadEncryption (n2n_edge_conf_t *conf, int cipher) {
/* even though 'cipher' and 'conf->transop_id' share the same encoding scheme, /* even though 'cipher' and 'conf->transop_id' share the same encoding scheme,
* a switch-statement under conditional compilation is used to sort out the * a switch-statement under conditional compilation is used to sort out the
* unsupported ciphers */ * unsupported ciphers */
@ -278,6 +280,7 @@ static void setPayloadEncryption (n2n_edge_conf_t *conf, int cipher) {
/* *************************************************** */ /* *************************************************** */
static int setOption (int optkey, char *optargument, n2n_tuntap_priv_config_t *ec, n2n_edge_conf_t *conf) { static int setOption (int optkey, char *optargument, n2n_tuntap_priv_config_t *ec, n2n_edge_conf_t *conf) {
/* traceEvent(TRACE_NORMAL, "Option %c = %s", optkey, optargument ? optargument : ""); */ /* traceEvent(TRACE_NORMAL, "Option %c = %s", optkey, optargument ? optargument : ""); */
switch(optkey) { switch(optkey) {
@ -536,6 +539,7 @@ static const struct option long_options[] =
/* read command line options */ /* read command line options */
static int loadFromCLI (int argc, char *argv[], n2n_edge_conf_t *conf, n2n_tuntap_priv_config_t *ec) { static int loadFromCLI (int argc, char *argv[], n2n_edge_conf_t *conf, n2n_tuntap_priv_config_t *ec) {
u_char c; u_char c;
while ((c = getopt_long(argc, argv, while ((c = getopt_long(argc, argv,
@ -555,6 +559,7 @@ static int loadFromCLI (int argc, char *argv[], n2n_edge_conf_t *conf, n2n_tunta
/* *************************************************** */ /* *************************************************** */
static char *trim (char *s) { static char *trim (char *s) {
char *end; char *end;
while(isspace(s[0]) || (s[0] == '"') || (s[0] == '\'')) s++; while(isspace(s[0]) || (s[0] == '"') || (s[0] == '\'')) s++;
@ -573,6 +578,7 @@ static char *trim (char *s) {
/* parse the configuration file */ /* parse the configuration file */
static int loadFromFile (const char *path, n2n_edge_conf_t *conf, n2n_tuntap_priv_config_t *ec) { static int loadFromFile (const char *path, n2n_edge_conf_t *conf, n2n_tuntap_priv_config_t *ec) {
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;
@ -733,6 +739,7 @@ BOOL WINAPI term_handler(DWORD sig)
/** Entry point to program from kernel. */ /** Entry point to program from kernel. */
int main (int argc, char* argv[]) { int main (int argc, char* argv[]) {
int rc; int rc;
tuntap_dev tuntap; /* a tuntap device */ tuntap_dev tuntap; /* a tuntap device */
n2n_edge_t *eee; /* single instance for this program */ n2n_edge_t *eee; /* single instance for this program */
@ -860,7 +867,7 @@ int main (int argc, char* argv[]) {
traceEvent(TRACE_NORMAL, "Local tap IP: %s, Mask: %s", traceEvent(TRACE_NORMAL, "Local tap IP: %s, Mask: %s",
eee->tuntap_priv_conf.ip_addr, eee->tuntap_priv_conf.netmask); eee->tuntap_priv_conf.ip_addr, eee->tuntap_priv_conf.netmask);
memcpy(&eee->device, &tuntap, sizeof(tuntap)); memcpy(&eee->device, &tuntap, sizeof(tuntap));
// hexdump((unsigned char*)&tuntap,sizeof(tuntap_dev)); //hexdump((unsigned char*)&tuntap,sizeof(tuntap_dev));
#ifndef WIN32 #ifndef WIN32
if(eee->tuntap_priv_conf.daemon) { if(eee->tuntap_priv_conf.daemon) {

View File

@ -116,7 +116,7 @@ const char* transop_str (enum n2n_transform tr) {
case N2N_TRANSFORM_ID_TWOFISH: return("Twofish"); case N2N_TRANSFORM_ID_TWOFISH: return("Twofish");
case N2N_TRANSFORM_ID_AES: return("AES"); case N2N_TRANSFORM_ID_AES: return("AES");
case N2N_TRANSFORM_ID_CHACHA20:return("ChaCha20"); case N2N_TRANSFORM_ID_CHACHA20:return("ChaCha20");
case N2N_TRANSFORM_ID_SPECK :return("Speck"); case N2N_TRANSFORM_ID_SPECK: return("Speck");
default: return("invalid"); default: return("invalid");
}; };
} }
@ -275,8 +275,9 @@ n2n_edge_t* edge_init (const n2n_edge_conf_t *conf, int *rv) {
eee->conf.auth.scheme = n2n_auth_simple_id; eee->conf.auth.scheme = n2n_auth_simple_id;
for (idx = 0; idx < N2N_AUTH_TOKEN_SIZE; ++idx) for(idx = 0; idx < N2N_AUTH_TOKEN_SIZE; ++idx) {
eee->conf.auth.token[idx] = n2n_rand() % 0xff; eee->conf.auth.token[idx] = n2n_rand() % 0xff;
}
eee->conf.auth.toksize = sizeof(eee->conf.auth.token); eee->conf.auth.toksize = sizeof(eee->conf.auth.token);
@ -443,6 +444,7 @@ static void register_with_new_peer (n2n_edge_t *eee,
const n2n_ip_subnet_t *dev_addr, const n2n_ip_subnet_t *dev_addr,
const n2n_desc_t *dev_desc, const n2n_desc_t *dev_desc,
const n2n_sock_t *peer) { const n2n_sock_t *peer) {
/* REVISIT: purge of pending_peers not yet done. */ /* REVISIT: purge of pending_peers not yet done. */
struct peer_info *scan; struct peer_info *scan;
macstr_t mac_buf; macstr_t mac_buf;
@ -467,7 +469,6 @@ static void register_with_new_peer (n2n_edge_t *eee,
traceEvent(TRACE_DEBUG, "Pending peers list size=%u", traceEvent(TRACE_DEBUG, "Pending peers list size=%u",
HASH_COUNT(eee->pending_peers)); HASH_COUNT(eee->pending_peers));
/* trace Sending REGISTER */ /* trace Sending REGISTER */
if(from_supernode) { if(from_supernode) {
/* UDP NAT hole punching through supernode. Send to peer first(punch local UDP hole) /* UDP NAT hole punching through supernode. Send to peer first(punch local UDP hole)
@ -825,8 +826,9 @@ static void send_register_super (n2n_edge_t *eee) {
cmn.flags = 0; cmn.flags = 0;
memcpy(cmn.community, eee->conf.community_name, N2N_COMMUNITY_SIZE); memcpy(cmn.community, eee->conf.community_name, N2N_COMMUNITY_SIZE);
for(idx = 0; idx < N2N_COOKIE_SIZE; ++idx) for(idx = 0; idx < N2N_COOKIE_SIZE; ++idx) {
eee->curr_sn->last_cookie[idx] = n2n_rand() % 0xff; eee->curr_sn->last_cookie[idx] = n2n_rand() % 0xff;
}
memcpy(reg.cookie, eee->curr_sn->last_cookie, N2N_COOKIE_SIZE); memcpy(reg.cookie, eee->curr_sn->last_cookie, N2N_COOKIE_SIZE);
reg.dev_addr.net_addr = ntohl(eee->device.ip_addr); reg.dev_addr.net_addr = ntohl(eee->device.ip_addr);
@ -974,9 +976,9 @@ static void send_register (n2n_edge_t * eee,
sock_to_cstr(sockbuf, remote_peer)); sock_to_cstr(sockbuf, remote_peer));
if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED) if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED)
packet_header_encrypt (pktbuf, idx, eee->conf.header_encryption_ctx, packet_header_encrypt(pktbuf, idx, eee->conf.header_encryption_ctx,
eee->conf.header_iv_ctx, eee->conf.header_iv_ctx,
time_stamp (), pearson_hash_16 (pktbuf, idx)); time_stamp(), pearson_hash_16(pktbuf, idx));
/* sent = */ sendto_sock(eee->udp_sock, pktbuf, idx, remote_peer); /* sent = */ sendto_sock(eee->udp_sock, pktbuf, idx, remote_peer);
} }
@ -1019,9 +1021,9 @@ static void send_register_ack (n2n_edge_t * eee,
sock_to_cstr(sockbuf, remote_peer)); sock_to_cstr(sockbuf, remote_peer));
if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED) if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED)
packet_header_encrypt (pktbuf, idx, eee->conf.header_encryption_ctx, packet_header_encrypt(pktbuf, idx, eee->conf.header_encryption_ctx,
eee->conf.header_iv_ctx, eee->conf.header_iv_ctx,
time_stamp (), pearson_hash_16 (pktbuf, idx)); time_stamp(), pearson_hash_16(pktbuf, idx));
/* sent = */ sendto_sock(eee->udp_sock, pktbuf, idx, remote_peer); /* sent = */ sendto_sock(eee->udp_sock, pktbuf, idx, remote_peer);
} }
@ -1111,9 +1113,9 @@ void update_supernode_reg (n2n_edge_t * eee, time_t nowTime) {
} }
} }
} } else {
else
--(eee->sup_attempts); --(eee->sup_attempts);
}
if(supernode2sock(&(eee->supernode), eee->curr_sn->ip_addr) == 0) { if(supernode2sock(&(eee->supernode), eee->curr_sn->ip_addr) == 0) {
traceEvent(TRACE_INFO, "Registering with supernode [%s][number of supernodes %d][attempts left %u]", traceEvent(TRACE_INFO, "Registering with supernode [%s][number of supernodes %d][attempts left %u]",
@ -1169,7 +1171,7 @@ static int handle_PACKET (n2n_edge_t * eee,
++(eee->stats.rx_sup_broadcast); ++(eee->stats.rx_sup_broadcast);
++(eee->stats.rx_sup); ++(eee->stats.rx_sup);
eee->last_sup=now; eee->last_sup = now;
} else { } else {
++(eee->stats.rx_p2p); ++(eee->stats.rx_p2p);
eee->last_p2p=now; eee->last_p2p=now;
@ -1443,7 +1445,7 @@ static void readFromMgmtSocket (n2n_edge_t *eee, int *keep_running) {
if(peer->dev_addr.net_addr == 0) continue; if(peer->dev_addr.net_addr == 0) continue;
net = htonl(peer->dev_addr.net_addr); net = htonl(peer->dev_addr.net_addr);
msg_len += snprintf((char *) (udp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len), msg_len += snprintf((char *) (udp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
" %-4u %-15s %-17s %-21s %-15s %lu\n", "%-4u %-15s %-17s %-21s %-15s %lu\n",
++num, inet_ntoa(*(struct in_addr *) &net), ++num, inet_ntoa(*(struct in_addr *) &net),
macaddr_str(mac_buf, peer->mac_addr), macaddr_str(mac_buf, peer->mac_addr),
sock_to_cstr(sockbuf, &(peer->sock)), sock_to_cstr(sockbuf, &(peer->sock)),
@ -1463,7 +1465,7 @@ static void readFromMgmtSocket (n2n_edge_t *eee, int *keep_running) {
if(peer->dev_addr.net_addr == 0) continue; if(peer->dev_addr.net_addr == 0) continue;
net = htonl(peer->dev_addr.net_addr); net = htonl(peer->dev_addr.net_addr);
msg_len += snprintf((char *) (udp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len), msg_len += snprintf((char *) (udp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
" %-4u %-15s %-17s %-21s %-15s %lu\n", "%-4u %-15s %-17s %-21s %-15s %lu\n",
++num, inet_ntoa(*(struct in_addr *) &net), ++num, inet_ntoa(*(struct in_addr *) &net),
macaddr_str(mac_buf, peer->mac_addr), macaddr_str(mac_buf, peer->mac_addr),
sock_to_cstr(sockbuf, &(peer->sock)), sock_to_cstr(sockbuf, &(peer->sock)),
@ -1485,9 +1487,9 @@ static void readFromMgmtSocket (n2n_edge_t *eee, int *keep_running) {
HASH_ITER(hh, eee->conf.supernodes, peer, tmpPeer) { HASH_ITER(hh, eee->conf.supernodes, peer, tmpPeer) {
net = htonl(peer->dev_addr.net_addr); net = htonl(peer->dev_addr.net_addr);
msg_len += snprintf((char *) (udp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len), msg_len += snprintf((char *) (udp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
" %-4u %-15s %-17s %-21s %-14s %lu\n", "%-4u %-15s %-17s %-21s %-14s %lu\n",
++num, ++num,
(peer->purgeable == SN_UNPURGEABLE)?"-l ":" ", (peer->purgeable == SN_UNPURGEABLE) ? "-l " : " ",
macaddr_str(mac_buf, peer->mac_addr), macaddr_str(mac_buf, peer->mac_addr),
sock_to_cstr(sockbuf, &(peer->sock)), sock_to_cstr(sockbuf, &(peer->sock)),
sn_selection_criterion_str(sel_buf, peer), sn_selection_criterion_str(sel_buf, peer),
@ -1749,7 +1751,7 @@ void edge_send_packet2net (n2n_edge_t * eee,
pkt.compression = N2N_COMPRESSION_ID_ZSTD; pkt.compression = N2N_COMPRESSION_ID_ZSTD;
} }
} else { } else {
traceEvent (TRACE_ERROR, "payload compression failed with zstd error '%s'.", traceEvent(TRACE_ERROR, "payload compression failed with zstd error '%s'.",
ZSTD_getErrorName(compression_len)); ZSTD_getErrorName(compression_len));
free(compression_buffer); free(compression_buffer);
// continue with unset without pkt.compression --> will send uncompressed // continue with unset without pkt.compression --> will send uncompressed
@ -1761,10 +1763,10 @@ void edge_send_packet2net (n2n_edge_t * eee,
} }
if(pkt.compression != N2N_COMPRESSION_ID_NONE) { if(pkt.compression != N2N_COMPRESSION_ID_NONE) {
traceEvent (TRACE_DEBUG, "payload compression [%s]: compressed %u bytes to %u bytes\n", traceEvent(TRACE_DEBUG, "payload compression [%s]: compressed %u bytes to %u bytes\n",
compression_str(pkt.compression), len, compression_len); compression_str(pkt.compression), len, compression_len);
memcpy (tap_pkt, compression_buffer, compression_len); memcpy(tap_pkt, compression_buffer, compression_len);
len = compression_len; len = compression_len;
} }
@ -1779,16 +1781,16 @@ void edge_send_packet2net (n2n_edge_t * eee,
uint16_t headerIdx = idx; uint16_t headerIdx = idx;
idx += eee->transop.fwd(&eee->transop, idx += eee->transop.fwd(&eee->transop,
pktbuf+idx, N2N_PKT_BUF_SIZE-idx, pktbuf + idx, N2N_PKT_BUF_SIZE - idx,
tap_pkt, len, pkt.dstMac); tap_pkt, len, pkt.dstMac);
traceEvent(TRACE_DEBUG, "Encode %u B PACKET [%u B data, %u B overhead] transform %u", traceEvent(TRACE_DEBUG, "Encode %u B PACKET [%u B data, %u B overhead] transform %u",
(u_int)idx, (u_int)len, (u_int)(idx-len), tx_transop_idx); (u_int)idx, (u_int)len, (u_int)(idx - len), tx_transop_idx);
if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED) if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED)
packet_header_encrypt (pktbuf, headerIdx, eee->conf.header_encryption_ctx, packet_header_encrypt(pktbuf, headerIdx, eee->conf.header_encryption_ctx,
eee->conf.header_iv_ctx, eee->conf.header_iv_ctx,
time_stamp (), pearson_hash_16 (pktbuf, idx)); time_stamp(), pearson_hash_16(pktbuf, idx));
#ifdef MTU_ASSERT_VALUE #ifdef MTU_ASSERT_VALUE
{ {
@ -2129,8 +2131,8 @@ void readFromIPSocket (n2n_edge_t * eee, int in_sock) {
sn->ip_addr = calloc(1,N2N_EDGE_SN_HOST_SIZE); sn->ip_addr = calloc(1,N2N_EDGE_SN_HOST_SIZE);
if(sn->ip_addr != NULL) { if(sn->ip_addr != NULL) {
inet_ntop(payload->sock.family, inet_ntop(payload->sock.family,
(payload->sock.family == AF_INET)?(void*)&(payload->sock.addr.v4):(void*)&(payload->sock.addr.v6), (payload->sock.family == AF_INET) ? (void*)&(payload->sock.addr.v4) : (void*)&(payload->sock.addr.v6),
sn->ip_addr, N2N_EDGE_SN_HOST_SIZE-1); sn->ip_addr, N2N_EDGE_SN_HOST_SIZE - 1);
sprintf (sn->ip_addr, "%s:%u", sn->ip_addr, (uint16_t)(payload->sock.port)); sprintf (sn->ip_addr, "%s:%u", sn->ip_addr, (uint16_t)(payload->sock.port));
} }
sn_selection_criterion_default(&(sn->selection_criterion)); sn_selection_criterion_default(&(sn->selection_criterion));
@ -2226,7 +2228,7 @@ void readFromIPSocket (n2n_edge_t * eee, int in_sock) {
if(!is_valid_peer_sock(&pi.sock)) { if(!is_valid_peer_sock(&pi.sock)) {
traceEvent(TRACE_DEBUG, "Skip invalid PEER_INFO %s [%s]", traceEvent(TRACE_DEBUG, "Skip invalid PEER_INFO %s [%s]",
sock_to_cstr(sockbuf1, &pi.sock), sock_to_cstr(sockbuf1, &pi.sock),
macaddr_str(mac_buf1, pi.mac) ); macaddr_str(mac_buf1, pi.mac));
break; break;
} }
@ -2252,7 +2254,7 @@ void readFromIPSocket (n2n_edge_t * eee, int in_sock) {
} else { } else {
traceEvent(TRACE_INFO, "Rx PEER_INFO unknown peer %s", traceEvent(TRACE_INFO, "Rx PEER_INFO unknown peer %s",
macaddr_str(mac_buf1, pi.mac) ); macaddr_str(mac_buf1, pi.mac));
} }
} }
break; break;

View File

@ -26,8 +26,9 @@ static DWORD* tunReadThread (LPVOID lpArg) {
struct tunread_arg *arg = (struct tunread_arg*)lpArg; struct tunread_arg *arg = (struct tunread_arg*)lpArg;
while(*arg->keep_running) while(*arg->keep_running) {
edge_read_from_tap(arg->eee); edge_read_from_tap(arg->eee);
}
return((DWORD*)NULL); return((DWORD*)NULL);
} }

View File

@ -20,8 +20,8 @@
static int keep_running; static int keep_running;
int main() int main () {
{
n2n_sn_t sss_node; n2n_sn_t sss_node;
int rc; int rc;
@ -30,14 +30,12 @@ int main()
sss_node.lport = 1234; // Main UDP listen port sss_node.lport = 1234; // Main UDP listen port
sss_node.sock = open_socket(sss_node.lport, 1); sss_node.sock = open_socket(sss_node.lport, 1);
if (-1 == sss_node.sock) if(-1 == sss_node.sock) {
{
exit(-2); exit(-2);
} }
sss_node.mgmt_sock = open_socket(5645, 0); // Main UDP management port sss_node.mgmt_sock = open_socket(5645, 0); // Main UDP management port
if (-1 == sss_node.mgmt_sock) if(-1 == sss_node.mgmt_sock) {
{
exit(-2); exit(-2);
} }

View File

@ -232,7 +232,7 @@ uint8_t mask2bitlen (uint32_t mask) {
uint8_t i, bitlen = 0; uint8_t i, bitlen = 0;
for (i = 0; i < 32; ++i) { for (i = 0; i < 32; ++i) {
if ((mask << i) & 0x80000000) { if((mask << i) & 0x80000000) {
++bitlen; ++bitlen;
} else { } else {
break; break;
@ -399,8 +399,8 @@ void hexdump (const uint8_t *buf, size_t len) {
} }
printf("-----------------------------------------------\n"); printf("-----------------------------------------------\n");
for (i = 0; i < len; i++) { for(i = 0; i < len; i++) {
if ((i > 0) && ((i % 16) == 0)) { if((i > 0) && ((i % 16) == 0)) {
printf("\n"); printf("\n");
} }
printf("%02X ", buf[i] & 0xFF); printf("%02X ", buf[i] & 0xFF);
@ -611,8 +611,8 @@ uint64_t time_stamp (void) {
fraction never exceeds 1,000,000,) encode the value tv_usec. The remaining lowest 12 bits fraction never exceeds 1,000,000,) encode the value tv_usec. The remaining lowest 12 bits
are kept random for use in IV */ are kept random for use in IV */
micro_seconds = n2n_rand(); micro_seconds = n2n_rand();
micro_seconds = ( (((uint64_t)(tod.tv_sec) << 32) + (tod.tv_usec << 12)) micro_seconds = ((((uint64_t)(tod.tv_sec) << 32) + (tod.tv_usec << 12))
| (micro_seconds >> 52) ); | (micro_seconds >> 52));
// more exact but more costly due to the multiplication: // more exact but more costly due to the multiplication:
// micro_seconds = (tod.tv_sec * 1000000 + tod.tv_usec) << 12) | ... // micro_seconds = (tod.tv_sec * 1000000 + tod.tv_usec) << 12) | ...

View File

@ -140,22 +140,22 @@ re_t re_compile (const char* pattern) {
int i = 0; /* index into pattern */ int i = 0; /* index into pattern */
int j = 0; /* index into re_compiled */ int j = 0; /* index into re_compiled */
while(pattern[i] != '\0' && (j+1 < MAX_REGEXP_OBJECTS)) { while(pattern[i] != '\0' && (j + 1 < MAX_REGEXP_OBJECTS)) {
c = pattern[i]; c = pattern[i];
switch(c) { switch(c) {
/* Meta-characters: */ /* Meta-characters: */
// case '^': { re_compiled[j].type = BEGIN; } break; <-- disabled (always full matches) // case '^': { re_compiled[j].type = BEGIN; } break; <-- disabled (always full matches)
// case '$': { re_compiled[j].type = END; } break; <-- disabled (always full matches) // case '$': { re_compiled[j].type = END; } break; <-- disabled (always full matches)
case '.': { re_compiled[j].type = DOT; } break; case '.': { re_compiled[j].type = DOT; } break;
case '*': { re_compiled[j].type = STAR; } break; case '*': { re_compiled[j].type = STAR; } break;
case '+': { re_compiled[j].type = PLUS; } break; case '+': { re_compiled[j].type = PLUS; } break;
case '?': { re_compiled[j].type = QUESTIONMARK; } break; case '?': { re_compiled[j].type = QUESTIONMARK; } break;
/* case '|': { re_compiled[j].type = BRANCH; } break; <-- not working properly */ /* case '|': { re_compiled[j].type = BRANCH; } break; <-- not working properly */
/* Escaped character-classes (\s \w ...): */ /* Escaped character-classes (\s \w ...): */
case '\\': { case '\\': {
if(pattern[i+1] != '\0') { if(pattern[i + 1] != '\0') {
/* Skip the escape-char '\\' */ /* Skip the escape-char '\\' */
i += 1; i += 1;
/* ... and check the next */ /* ... and check the next */
@ -176,13 +176,13 @@ re_t re_compile (const char* pattern) {
} }
} }
/* '\\' as last char in pattern -> invalid regular expression. */ /* '\\' as last char in pattern -> invalid regular expression. */
/* /*
else else
{ {
re_compiled[j].type = CHAR_TYPE; re_compiled[j].type = CHAR_TYPE;
re_compiled[j].ch = pattern[i]; re_compiled[j].ch = pattern[i];
} }
*/ */
} break; } break;
/* Character class: */ /* Character class: */
@ -199,7 +199,7 @@ re_t re_compile (const char* pattern) {
} }
/* Copy characters inside [..] to buffer */ /* Copy characters inside [..] to buffer */
while( (pattern[++i] != ']') while((pattern[++i] != ']')
&& (pattern[i] != '\0')) /* Missing ] */ && (pattern[i] != '\0')) /* Missing ] */
{ {
if(pattern[i] == '\\') { if(pattern[i] == '\\') {

View File

@ -265,7 +265,6 @@ uint8_t march_rule_and_cache_key (filter_rule_key_t *rule_key, packet_address_pr
/* for [-Wmissing-declarations] */ /* for [-Wmissing-declarations] */
filter_rule_t* get_filter_rule (filter_rule_t **rules, packet_address_proto_info_t *pkt_addr_info); filter_rule_t* get_filter_rule (filter_rule_t **rules, packet_address_proto_info_t *pkt_addr_info);
filter_rule_t* get_filter_rule (filter_rule_t **rules, packet_address_proto_info_t *pkt_addr_info) { filter_rule_t* get_filter_rule (filter_rule_t **rules, packet_address_proto_info_t *pkt_addr_info) {
filter_rule_t *item = 0, *tmp = 0, *marched_rule = 0; filter_rule_t *item = 0, *tmp = 0, *marched_rule = 0;

View File

@ -54,7 +54,7 @@ static int load_allowed_sn_community (n2n_sn_t *sss, char *path) {
} }
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);
} }
@ -67,11 +67,11 @@ 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';
@ -88,7 +88,7 @@ static int load_allowed_sn_community (n2n_sn_t *sss, char *path) {
// 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);
@ -99,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) { if(s != NULL) {
comm_init(s,cmn_str); comm_init(s,cmn_str);
@ -125,7 +125,7 @@ static int load_allowed_sn_community (n2n_sn_t *sss, char *path) {
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;
@ -207,7 +207,7 @@ static void help () {
#if defined(N2N_HAVE_DAEMON) #if defined(N2N_HAVE_DAEMON)
printf("-f | Run in foreground.\n"); printf("-f | Run in foreground.\n");
#endif /* #if defined(N2N_HAVE_DAEMON) */ #endif /* #if defined(N2N_HAVE_DAEMON) */
printf("-F <fed_name> | Name of the supernodes federation (otherwise use '%s' by default)\n",(char *)FEDERATION_NAME); printf("-F <fed_name> | Name of the supernodes federation (otherwise use '%s' by default)\n", (char *)FEDERATION_NAME);
#if 0 #if 0
printf("-m <mac_addr> | Fix MAC address for the supernode (otherwise it may be random)\n" printf("-m <mac_addr> | Fix MAC address for the supernode (otherwise it may be random)\n"
" | eg. -m 01:02:03:04:05:06\n"); " | eg. -m 01:02:03:04:05:06\n");
@ -229,7 +229,6 @@ 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) {
@ -282,17 +281,19 @@ static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) {
} }
if(sss->federation != NULL) { if(sss->federation != NULL) {
skip_add = SN_ADD; 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); 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) { if(anchor_sn != NULL) {
anchor_sn->ip_addr = calloc(1, N2N_EDGE_SN_HOST_SIZE); anchor_sn->ip_addr = calloc(1, N2N_EDGE_SN_HOST_SIZE);
if(anchor_sn->ip_addr) { if(anchor_sn->ip_addr) {
strncpy(anchor_sn->ip_addr,_optarg,N2N_EDGE_SN_HOST_SIZE-1); strncpy(anchor_sn->ip_addr, _optarg, N2N_EDGE_SN_HOST_SIZE - 1);
memcpy(&(anchor_sn->sock), socket, sizeof(n2n_sock_t)); 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->purgeable = SN_UNPURGEABLE;
anchor_sn->last_valid_time_stamp = initial_time_stamp(); anchor_sn->last_valid_time_stamp = initial_time_stamp();
} }
} }
} }
@ -319,7 +320,7 @@ static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) {
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) || (net_max == (in_addr_t)(-1)) || (net_max == INADDR_NONE) || (net_max == INADDR_ANY)
|| (ntohl(net_min) > ntohl(net_max)) || (ntohl(net_min) > ntohl(net_max))
|| ((ntohl(net_min) & ~mask) != 0) || ((ntohl(net_max) & ~mask) != 0) ) { || ((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'", traceEvent(TRACE_WARNING, "Bad network range '%s...%s/%u' in '%s', defaulting to '%s...%s/%d'",
ip_min_str, ip_max_str, bitlen, _optarg, 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); N2N_SN_MIN_AUTO_IP_NET_DEFAULT, N2N_SN_MAX_AUTO_IP_NET_DEFAULT, N2N_SN_AUTO_IP_NET_BIT_DEFAULT);
@ -354,7 +355,8 @@ static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) {
#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;
@ -384,7 +386,7 @@ static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) {
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);
} }
@ -464,6 +466,7 @@ 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;
@ -499,12 +502,8 @@ static int loadFromFile (const char *path, n2n_sn_t *sss) {
} }
} 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) { if(line_len > 1) key[1] = '\0';
key[1] = '\0'; if(line_len > 2) value = trim(&key[2]);
}
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);
@ -528,7 +527,9 @@ static int add_federation_to_communities (n2n_sn_t *sss) {
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);
} }
@ -558,11 +559,11 @@ static void dump_registrations (int signo) {
++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);
} }
} }
} }
@ -577,9 +578,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;
@ -659,6 +660,7 @@ int main (int argc, char * const argv[]) {
} 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;

View File

@ -879,7 +879,7 @@ static int process_udp (n2n_sn_t * sss,
udp_size, intoa(ntohl(sender_sock->sin_addr.s_addr), buf, sizeof(buf)), udp_size, intoa(ntohl(sender_sock->sin_addr.s_addr), buf, sizeof(buf)),
ntohs(sender_sock->sin_port)); ntohs(sender_sock->sin_port));
/* check if header is unenrypted. the following check is around 99.99962 percent reliable. /* check if header is unencrypted. the following check is around 99.99962 percent reliable.
* it heavily relies on the structure of packet's common part * it heavily relies on the structure of packet's common part
* changes to wire.c:encode/decode_common need to go together with this code */ * changes to wire.c:encode/decode_common need to go together with this code */
if(udp_size < 20) { if(udp_size < 20) {
@ -888,8 +888,8 @@ static int process_udp (n2n_sn_t * sss,
} }
if((udp_buf[19] == (uint8_t)0x00) // null terminated community name if((udp_buf[19] == (uint8_t)0x00) // null terminated community name
&& (udp_buf[00] == N2N_PKT_VERSION) // correct packet version && (udp_buf[00] == N2N_PKT_VERSION) // correct packet version
&& ((be16toh (*(uint16_t*)&(udp_buf[02])) & N2N_FLAGS_TYPE_MASK ) <= MSG_TYPE_MAX_TYPE ) // message type && ((be16toh(*(uint16_t*)&(udp_buf[02])) & N2N_FLAGS_TYPE_MASK) <= MSG_TYPE_MAX_TYPE) // message type
&& ( be16toh (*(uint16_t*)&(udp_buf[02])) < N2N_FLAGS_OPTIONS) // flags && ( be16toh(*(uint16_t*)&(udp_buf[02])) < N2N_FLAGS_OPTIONS) // flags
) { ) {
/* most probably unencrypted */ /* most probably unencrypted */
/* make sure, no downgrading happens here and no unencrypted packets can be /* make sure, no downgrading happens here and no unencrypted packets can be
@ -968,7 +968,7 @@ static int process_udp (n2n_sn_t * sss,
} }
msg_type = cmn.pc; /* packet code */ msg_type = cmn.pc; /* packet code */
from_supernode= cmn.flags & N2N_FLAGS_FROM_SUPERNODE; from_supernode = cmn.flags & N2N_FLAGS_FROM_SUPERNODE;
if(cmn.ttl < 1) { if(cmn.ttl < 1) {
traceEvent(TRACE_WARNING, "Expired TTL"); traceEvent(TRACE_WARNING, "Expired TTL");
@ -995,12 +995,12 @@ static int process_udp (n2n_sn_t * sss,
return -1; return -1;
} }
sss->stats.last_fwd=now; sss->stats.last_fwd = now;
decode_PACKET(&pkt, &cmn, udp_buf, &rem, &idx); decode_PACKET(&pkt, &cmn, udp_buf, &rem, &idx);
// already checked for valid comm // already checked for valid comm
if(comm->header_encryption == HEADER_ENCRYPTION_ENABLED) { if(comm->header_encryption == HEADER_ENCRYPTION_ENABLED) {
if(!find_edge_time_stamp_and_verify (comm->edges, from_supernode, pkt.srcMac, stamp, TIME_STAMP_ALLOW_JITTER)) { if(!find_edge_time_stamp_and_verify(comm->edges, from_supernode, pkt.srcMac, stamp, TIME_STAMP_ALLOW_JITTER)) {
traceEvent(TRACE_DEBUG, "process_udp dropped PACKET due to time stamp error."); traceEvent(TRACE_DEBUG, "process_udp dropped PACKET due to time stamp error.");
return -1; return -1;
} }
@ -1077,7 +1077,7 @@ static int process_udp (n2n_sn_t * sss,
return -1; return -1;
} }
sss->stats.last_fwd=now; sss->stats.last_fwd = now;
decode_REGISTER(&reg, &cmn, udp_buf, &rem, &idx); decode_REGISTER(&reg, &cmn, udp_buf, &rem, &idx);
// already checked for valid comm // already checked for valid comm

View File

@ -217,13 +217,13 @@ int decode_common (n2n_common_t * out,
decode_uint8(&dummy, base, rem, idx); decode_uint8(&dummy, base, rem, idx);
if (N2N_PKT_VERSION != dummy) { if(N2N_PKT_VERSION != dummy) {
return -1; return -1;
} }
decode_uint8(&(out->ttl), base, rem, idx); decode_uint8(&(out->ttl), base, rem, idx);
decode_uint16(&(out->flags), base, rem, idx); decode_uint16(&(out->flags), base, rem, idx);
out->pc = ( out->flags & N2N_FLAGS_TYPE_MASK ); out->pc = (out->flags & N2N_FLAGS_TYPE_MASK);
out->flags &= N2N_FLAGS_BITS_MASK; out->flags &= N2N_FLAGS_BITS_MASK;
decode_buf(out->community, N2N_COMMUNITY_SIZE, base, rem, idx); decode_buf(out->community, N2N_COMMUNITY_SIZE, base, rem, idx);
@ -288,7 +288,7 @@ int decode_sock (n2n_sock_t * sock,
decode_buf(sock->addr.v4, IPV4_SIZE, base, rem, idx); decode_buf(sock->addr.v4, IPV4_SIZE, base, rem, idx);
} }
return (idx-idx0); return (idx - idx0);
} }
@ -511,7 +511,7 @@ int decode_REGISTER_SUPER_ACK (n2n_REGISTER_SUPER_ACK_t *reg,
/* Following the edge socket are an array of backup supernodes. */ /* Following the edge socket are an array of backup supernodes. */
retval += decode_uint8(&(reg->num_sn), base, rem, idx); retval += decode_uint8(&(reg->num_sn), base, rem, idx);
retval += decode_buf(tmpbuf, (reg->num_sn*REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE), base, rem, idx); retval += decode_buf(tmpbuf, (reg->num_sn * REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE), base, rem, idx);
return retval; return retval;
} }