From d299d558acc10f2cb485e8c53df60dc7e2a5fcd4 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Fri, 17 Aug 2018 10:17:09 +0200 Subject: [PATCH] Packaging and parameter parsing fixes --- edge.c | 93 +-- packages/debian/{Makefile => Makefile.in} | 0 packages/debian/configure | 3 + packages/debian/configure.in | 1 + packages/etc/n2n/edge.conf | 12 +- packages/etc/n2n/supernode.conf | 2 +- sn.c | 813 +++++++++++----------- 7 files changed, 466 insertions(+), 458 deletions(-) rename packages/debian/{Makefile => Makefile.in} (100%) diff --git a/edge.c b/edge.c index 5336c44..3599f00 100644 --- a/edge.c +++ b/edge.c @@ -110,6 +110,9 @@ static int scan_address(char * ip_addr, size_t addr_size, static void help() { print_n2n_version(); + printf("edge (see edge.conf)\n" + "or\n" + ); printf("edge " #if defined(N2N_CAN_NAME_IFACE) "-d " @@ -173,7 +176,7 @@ static int setOption(int optkey, char *optarg, edge_conf_t *ec, n2n_edge_t *eee) //traceEvent(TRACE_NORMAL, "Option %c = %s", optkey, optarg ? optarg : ""); switch(optkey) { - case'K': + case'K': { if(ec->encrypt_key) { fprintf(stderr, "Error: -K and -k options are mutually exclusive.\n"); @@ -189,7 +192,7 @@ static int setOption(int optkey, char *optarg, edge_conf_t *ec, n2n_edge_t *eee) break; } - case 'a': /* IP address and mode of TUNTAP interface */ + case 'a': /* IP address and mode of TUNTAP interface */ { scan_address(ec->ip_addr, N2N_NETMASK_STR_SIZE, ec->ip_mode, N2N_IF_MODE_SIZE, @@ -197,14 +200,14 @@ static int setOption(int optkey, char *optarg, edge_conf_t *ec, n2n_edge_t *eee) break; } - case 'c': /* community as a string */ + case 'c': /* community as a string */ { memset(eee->community_name, 0, N2N_COMMUNITY_SIZE); strncpy((char *)eee->community_name, optarg, N2N_COMMUNITY_SIZE); break; } - case 'E': /* multicast ethernet addresses accepted. */ + case 'E': /* multicast ethernet addresses accepted. */ { eee->drop_multicast=0; traceEvent(TRACE_DEBUG, "Enabling ethernet multicast traffic\n"); @@ -212,13 +215,13 @@ static int setOption(int optkey, char *optarg, edge_conf_t *ec, n2n_edge_t *eee) } #ifndef WIN32 - case 'u': /* unprivileged uid */ + case 'u': /* unprivileged uid */ { ec->userid = atoi(optarg); break; } - case 'g': /* unprivileged uid */ + case 'g': /* unprivileged uid */ { ec->groupid = atoi(optarg); break; @@ -226,26 +229,26 @@ static int setOption(int optkey, char *optarg, edge_conf_t *ec, n2n_edge_t *eee) #endif #ifndef WIN32 - case 'f' : /* do not fork as daemon */ + case 'f' : /* do not fork as daemon */ { eee->daemon=0; break; } #endif /* #ifndef WIN32 */ - case 'm' : /* TUNTAP MAC address */ + case 'm' : /* TUNTAP MAC address */ { strncpy(ec->device_mac,optarg,N2N_MACNAMSIZ); break; } - case 'M' : /* TUNTAP MTU */ + case 'M' : /* TUNTAP MTU */ { ec->mtu = atoi(optarg); break; } - case 'k': /* encrypt key */ + case 'k': /* encrypt key */ { if(strlen(eee->keyschedule) > 0) { fprintf(stderr, "Error: -K and -k options are mutually exclusive.\n"); @@ -257,13 +260,13 @@ static int setOption(int optkey, char *optarg, edge_conf_t *ec, n2n_edge_t *eee) break; } - case 'r': /* enable packet routing across n2n endpoints */ + case 'r': /* enable packet routing across n2n endpoints */ { eee->allow_routing = 1; break; } - case 'l': /* supernode-list */ + case 'l': /* supernode-list */ { if(eee->sn_num < N2N_EDGE_NUM_SUPERNODES) { strncpy((eee->sn_ip_array[eee->sn_num]), optarg, N2N_EDGE_SN_HOST_SIZE); @@ -277,32 +280,32 @@ static int setOption(int optkey, char *optarg, edge_conf_t *ec, n2n_edge_t *eee) } #if defined(N2N_CAN_NAME_IFACE) - case 'd': /* TUNTAP name */ + case 'd': /* TUNTAP name */ { strncpy(ec->tuntap_dev_name, optarg, N2N_IFNAMSIZ); break; } #endif - case 'b': + case 'b': { eee->re_resolve_supernode_ip = 1; break; } - case 'p': + case 'p': { ec->local_port = atoi(optarg); break; } - case 't': + case 't': { ec->mgmt_port = atoi(optarg); break; } - case 's': /* Subnet Mask */ + case 's': /* Subnet Mask */ { if(0 != ec->got_s) { traceEvent(TRACE_WARNING, "Multiple subnet masks supplied."); @@ -312,19 +315,19 @@ static int setOption(int optkey, char *optarg, edge_conf_t *ec, n2n_edge_t *eee) break; } - case 'h': /* help */ + case 'h': /* help */ { help(); break; } - case 'v': /* verbose */ + case 'v': /* verbose */ { ++traceLevel; /* do 2 -v flags to increase verbosity to DEBUG level*/ break; } - default: + default: { traceEvent(TRACE_WARNING, "Unknown option -%c: Ignored.", (char)optkey); return(-1); @@ -551,10 +554,12 @@ int main(int argc, char* argv[]) { int keep_on_running = 1; int rc; int i; - n2n_edge_t eee; /* single instance for this program */ edge_conf_t ec; + if(argc == 1) + help(); + ec.local_port = 0 /* any port */; ec.mgmt_port = N2N_EDGE_MGMT_PORT; /* 5644 by default */ snprintf(ec.tuntap_dev_name, sizeof(ec.tuntap_dev_name), "edge0"); @@ -570,17 +575,15 @@ int main(int argc, char* argv[]) { ec.groupid = 0; /* root is the only guaranteed ID */ #endif - if(-1 == edge_init(&eee)) - { - traceEvent(TRACE_ERROR, "Failed in edge_init"); - exit(1); - } - - if(getenv("N2N_KEY")) - { - ec.encrypt_key = strdup(getenv("N2N_KEY")); - } - + if(-1 == edge_init(&eee)) { + traceEvent(TRACE_ERROR, "Failed in edge_init"); + exit(1); + } + + if(getenv("N2N_KEY")) { + ec.encrypt_key = strdup(getenv("N2N_KEY")); + } + #ifdef WIN32 ec.tuntap_dev_name[0] = '\0'; #endif @@ -596,19 +599,12 @@ int main(int argc, char* argv[]) { #endif rc = loadFromCLI(argc, argv, &ec, &eee); - if(rc < 0) - return(-1); - -#ifndef WIN32 - if(eee.daemon) { - useSyslog = 1; /* traceEvent output now goes to syslog. */ - daemonize(); - } -#endif /* #ifndef WIN32 */ - + if((rc < 0) || (eee.sn_num == 0)) + help(); + traceEvent(TRACE_NORMAL, "Starting n2n edge %s %s", n2n_sw_version, n2n_sw_buildDate); - for (i=0; i< N2N_EDGE_NUM_SUPERNODES; ++i) + for (i=0; i %s\n", i, (eee.sn_ip_array[i])); supernode2addr(&(eee.supernode), eee.sn_ip_array[eee.sn_idx]); @@ -624,13 +620,20 @@ int main(int argc, char* argv[]) { help(); } +#ifndef WIN32 + if(eee.daemon) { + useSyslog = 1; /* traceEvent output now goes to syslog. */ + daemonize(); + } +#endif /* #ifndef WIN32 */ + if((NULL == ec.encrypt_key) && (0 == strlen(eee.keyschedule))) { traceEvent(TRACE_WARNING, "Encryption is disabled in edge."); - + eee.null_transop = 1; } - + #ifndef WIN32 /* If running suid root then we need to setuid before using the force. */ setuid(0); diff --git a/packages/debian/Makefile b/packages/debian/Makefile.in similarity index 100% rename from packages/debian/Makefile rename to packages/debian/Makefile.in diff --git a/packages/debian/configure b/packages/debian/configure index a7b5257..cfe9fcb 100755 --- a/packages/debian/configure +++ b/packages/debian/configure @@ -1728,6 +1728,8 @@ ac_config_files="$ac_config_files ../etc/systemd/system/edge.service" ac_config_files="$ac_config_files ../etc/systemd/system/supernode.service" +ac_config_files="$ac_config_files Makefile" + cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure # tests run on this system so they can be shared between configure @@ -2439,6 +2441,7 @@ do "debian/rules") CONFIG_FILES="$CONFIG_FILES debian/rules" ;; "../etc/systemd/system/edge.service") CONFIG_FILES="$CONFIG_FILES ../etc/systemd/system/edge.service" ;; "../etc/systemd/system/supernode.service") CONFIG_FILES="$CONFIG_FILES ../etc/systemd/system/supernode.service" ;; + "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;; *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;; esac diff --git a/packages/debian/configure.in b/packages/debian/configure.in index 19488ad..d24ab49 100644 --- a/packages/debian/configure.in +++ b/packages/debian/configure.in @@ -41,4 +41,5 @@ AC_CONFIG_FILES(debian/control) AC_CONFIG_FILES(debian/rules) AC_CONFIG_FILES(../etc/systemd/system/edge.service) AC_CONFIG_FILES(../etc/systemd/system/supernode.service) +AC_CONFIG_FILES(Makefile) AC_OUTPUT diff --git a/packages/etc/n2n/edge.conf b/packages/etc/n2n/edge.conf index 8075f46..74d50a8 100644 --- a/packages/etc/n2n/edge.conf +++ b/packages/etc/n2n/edge.conf @@ -7,17 +7,17 @@ # -d|--tun-device # Specifies the name of the TUN interface. # -# -d=n2n0 +-d=n2n0 # # -c|--community # Specifies the n2n community name the edge belongs to. # -# -c=mynetwork +-c=mynetwork # # -k # Sets the encryption key (ASCII). The environment variable N2N_KEY= can also be used. # -# -k=mypassword +-k=mypassword # # -m # Specified the MAC address for the TAP interface (random otherwise). @@ -27,15 +27,15 @@ # -a # Sets the interface address. For DHCP use '-r -a dhcp:0.0.0.0'. # -# -a=1.2.3.4 +-a=1.2.3.4 # # -p # Sets the local UDP port to a fixed port. # -# -p=50001 +-p=50001 # # -l|--supernode-list # Specifies the supernode IP and port. # -# -l=7.8.9.0:7777 +-l=7.8.9.0:7777 # diff --git a/packages/etc/n2n/supernode.conf b/packages/etc/n2n/supernode.conf index 0ef798d..dc968c0 100644 --- a/packages/etc/n2n/supernode.conf +++ b/packages/etc/n2n/supernode.conf @@ -1,5 +1,5 @@ # -# The configuration file is similar to the command line, with one option per line. An equal +# The configuration file is similar to the command line, with one option per line. An equal # sign '=' should be used between key and value. Example: -l=7777 or --local-port=7777 # This file contains a basic configuration example, please refer to the help (-h) for the full # list of available options. diff --git a/sn.c b/sn.c index 1de6db7..029ccbd 100644 --- a/sn.c +++ b/sn.c @@ -26,29 +26,26 @@ #define N2N_SN_MGMT_PORT 5645 - -struct sn_stats -{ - size_t errors; /* Number of errors encountered. */ - size_t reg_super; /* Number of REGISTER_SUPER requests received. */ - size_t reg_super_nak; /* Number of REGISTER_SUPER requests declined. */ - size_t fwd; /* Number of messages forwarded. */ - size_t broadcast; /* Number of messages broadcast to a community. */ - time_t last_fwd; /* Time when last message was forwarded. */ - time_t last_reg_super; /* Time when last REGISTER_SUPER was received. */ +struct sn_stats { + size_t errors; /* Number of errors encountered. */ + size_t reg_super; /* Number of REGISTER_SUPER requests received. */ + size_t reg_super_nak; /* Number of REGISTER_SUPER requests declined. */ + size_t fwd; /* Number of messages forwarded. */ + size_t broadcast; /* Number of messages broadcast to a community. */ + time_t last_fwd; /* Time when last message was forwarded. */ + time_t last_reg_super; /* Time when last REGISTER_SUPER was received. */ }; typedef struct sn_stats sn_stats_t; -struct n2n_sn -{ - time_t start_time; /* Used to measure uptime. */ - sn_stats_t stats; - int daemon; /* If non-zero then daemonise. */ - uint16_t lport; /* Local UDP port to bind to. */ - int sock; /* Main socket for UDP traffic with edges. */ - int mgmt_sock; /* management socket. */ - struct peer_info * edges; /* Link list of registered edges. */ +struct n2n_sn { + time_t start_time; /* Used to measure uptime. */ + sn_stats_t stats; + int daemon; /* If non-zero then daemonise. */ + uint16_t lport; /* Local UDP port to bind to. */ + int sock; /* Main socket for UDP traffic with edges. */ + int mgmt_sock; /* management socket. */ + struct peer_info * edges; /* Link list of registered edges. */ }; typedef struct n2n_sn n2n_sn_t; @@ -72,36 +69,36 @@ static int try_broadcast( n2n_sn_t * sss, static int init_sn( n2n_sn_t * sss ) { #ifdef WIN32 - initWin32(); + initWin32(); #endif - memset( sss, 0, sizeof(n2n_sn_t) ); + memset( sss, 0, sizeof(n2n_sn_t) ); - sss->daemon = 1; /* By defult run as a daemon. */ - sss->lport = N2N_SN_LPORT_DEFAULT; - sss->sock = -1; - sss->mgmt_sock = -1; - sss->edges = NULL; + sss->daemon = 1; /* By defult run as a daemon. */ + sss->lport = N2N_SN_LPORT_DEFAULT; + sss->sock = -1; + sss->mgmt_sock = -1; + sss->edges = NULL; - return 0; /* OK */ + return 0; /* OK */ } /** Deinitialise the supernode structure and deallocate any memory owned by * it. */ static void deinit_sn( n2n_sn_t * sss ) { - if (sss->sock >= 0) + if (sss->sock >= 0) { - closesocket(sss->sock); + closesocket(sss->sock); } - sss->sock=-1; + sss->sock=-1; - if ( sss->mgmt_sock >= 0 ) + if ( sss->mgmt_sock >= 0 ) { - closesocket(sss->mgmt_sock); + closesocket(sss->mgmt_sock); } - sss->mgmt_sock=-1; + sss->mgmt_sock=-1; - purge_peer_list( &(sss->edges), 0xffffffff ); + purge_peer_list( &(sss->edges), 0xffffffff ); } @@ -112,7 +109,7 @@ static void deinit_sn( n2n_sn_t * sss ) */ static uint16_t reg_lifetime( n2n_sn_t * sss ) { - return 120; + return 120; } @@ -124,58 +121,58 @@ static int update_edge( n2n_sn_t * sss, const n2n_sock_t * sender_sock, time_t now) { - macstr_t mac_buf; - n2n_sock_str_t sockbuf; - struct peer_info * scan; + macstr_t mac_buf; + n2n_sock_str_t sockbuf; + struct peer_info * scan; - traceEvent( TRACE_DEBUG, "update_edge for %s [%s]", - macaddr_str( mac_buf, edgeMac ), - sock_to_cstr( sockbuf, sender_sock ) ); + traceEvent( TRACE_DEBUG, "update_edge for %s [%s]", + macaddr_str( mac_buf, edgeMac ), + sock_to_cstr( sockbuf, sender_sock ) ); - scan = find_peer_by_mac( sss->edges, edgeMac ); + scan = find_peer_by_mac( sss->edges, edgeMac ); - if ( NULL == scan ) + if ( NULL == scan ) { - /* Not known */ + /* Not known */ - scan = (struct peer_info*)calloc(1, sizeof(struct peer_info)); /* deallocated in purge_expired_registrations */ + scan = (struct peer_info*)calloc(1, sizeof(struct peer_info)); /* deallocated in purge_expired_registrations */ - memcpy(scan->community_name, community, sizeof(n2n_community_t) ); - memcpy(&(scan->mac_addr), edgeMac, sizeof(n2n_mac_t)); - memcpy(&(scan->sock), sender_sock, sizeof(n2n_sock_t)); + memcpy(scan->community_name, community, sizeof(n2n_community_t) ); + memcpy(&(scan->mac_addr), edgeMac, sizeof(n2n_mac_t)); + memcpy(&(scan->sock), sender_sock, sizeof(n2n_sock_t)); - /* insert this guy at the head of the edges list */ - scan->next = sss->edges; /* first in list */ - sss->edges = scan; /* head of list points to new scan */ + /* insert this guy at the head of the edges list */ + scan->next = sss->edges; /* first in list */ + sss->edges = scan; /* head of list points to new scan */ - traceEvent( TRACE_INFO, "update_edge created %s ==> %s", - macaddr_str( mac_buf, edgeMac ), - sock_to_cstr( sockbuf, sender_sock ) ); + traceEvent( TRACE_INFO, "update_edge created %s ==> %s", + macaddr_str( mac_buf, edgeMac ), + sock_to_cstr( sockbuf, sender_sock ) ); } - else + else { - /* Known */ - if ( (0 != memcmp(community, scan->community_name, sizeof(n2n_community_t))) || - (0 != sock_equal(sender_sock, &(scan->sock) )) ) + /* Known */ + if ( (0 != memcmp(community, scan->community_name, sizeof(n2n_community_t))) || + (0 != sock_equal(sender_sock, &(scan->sock) )) ) { - memcpy(scan->community_name, community, sizeof(n2n_community_t) ); - memcpy(&(scan->sock), sender_sock, sizeof(n2n_sock_t)); + memcpy(scan->community_name, community, sizeof(n2n_community_t) ); + memcpy(&(scan->sock), sender_sock, sizeof(n2n_sock_t)); - traceEvent( TRACE_INFO, "update_edge updated %s ==> %s", - macaddr_str( mac_buf, edgeMac ), - sock_to_cstr( sockbuf, sender_sock ) ); + traceEvent( TRACE_INFO, "update_edge updated %s ==> %s", + macaddr_str( mac_buf, edgeMac ), + sock_to_cstr( sockbuf, sender_sock ) ); } - else + else { - traceEvent( TRACE_DEBUG, "update_edge unchanged %s ==> %s", - macaddr_str( mac_buf, edgeMac ), - sock_to_cstr( sockbuf, sender_sock ) ); + traceEvent( TRACE_DEBUG, "update_edge unchanged %s ==> %s", + macaddr_str( mac_buf, edgeMac ), + sock_to_cstr( sockbuf, sender_sock ) ); } } - scan->last_seen = now; - return 0; + scan->last_seen = now; + return 0; } @@ -188,28 +185,28 @@ static ssize_t sendto_sock(n2n_sn_t * sss, const uint8_t * pktbuf, size_t pktsize) { - n2n_sock_str_t sockbuf; + n2n_sock_str_t sockbuf; - if ( AF_INET == sock->family ) + if ( AF_INET == sock->family ) { - struct sockaddr_in udpsock; + struct sockaddr_in udpsock; - udpsock.sin_family = AF_INET; - udpsock.sin_port = htons( sock->port ); - memcpy( &(udpsock.sin_addr.s_addr), &(sock->addr.v4), IPV4_SIZE ); + udpsock.sin_family = AF_INET; + udpsock.sin_port = htons( sock->port ); + memcpy( &(udpsock.sin_addr.s_addr), &(sock->addr.v4), IPV4_SIZE ); - traceEvent( TRACE_DEBUG, "sendto_sock %lu to [%s]", - pktsize, - sock_to_cstr( sockbuf, sock ) ); + traceEvent( TRACE_DEBUG, "sendto_sock %lu to [%s]", + pktsize, + sock_to_cstr( sockbuf, sock ) ); - return sendto( sss->sock, pktbuf, pktsize, 0, - (const struct sockaddr *)&udpsock, sizeof(struct sockaddr_in) ); + return sendto( sss->sock, pktbuf, pktsize, 0, + (const struct sockaddr *)&udpsock, sizeof(struct sockaddr_in) ); } - else + else { - /* AF_INET6 not implemented */ - errno = EAFNOSUPPORT; - return -1; + /* AF_INET6 not implemented */ + errno = EAFNOSUPPORT; + return -1; } } @@ -224,43 +221,43 @@ static int try_forward( n2n_sn_t * sss, const uint8_t * pktbuf, size_t pktsize ) { - struct peer_info * scan; - macstr_t mac_buf; - n2n_sock_str_t sockbuf; + struct peer_info * scan; + macstr_t mac_buf; + n2n_sock_str_t sockbuf; - scan = find_peer_by_mac( sss->edges, dstMac ); + scan = find_peer_by_mac( sss->edges, dstMac ); - if ( NULL != scan ) + if ( NULL != scan ) { - int data_sent_len; - data_sent_len = sendto_sock( sss, &(scan->sock), pktbuf, pktsize ); + int data_sent_len; + data_sent_len = sendto_sock( sss, &(scan->sock), pktbuf, pktsize ); - if ( data_sent_len == pktsize ) + if ( data_sent_len == pktsize ) { - ++(sss->stats.fwd); - traceEvent(TRACE_DEBUG, "unicast %lu to [%s] %s", - pktsize, - sock_to_cstr( sockbuf, &(scan->sock) ), - macaddr_str(mac_buf, scan->mac_addr)); + ++(sss->stats.fwd); + traceEvent(TRACE_DEBUG, "unicast %lu to [%s] %s", + pktsize, + sock_to_cstr( sockbuf, &(scan->sock) ), + macaddr_str(mac_buf, scan->mac_addr)); } - else + else { - ++(sss->stats.errors); - traceEvent(TRACE_ERROR, "unicast %lu to [%s] %s FAILED (%d: %s)", - pktsize, - sock_to_cstr( sockbuf, &(scan->sock) ), - macaddr_str(mac_buf, scan->mac_addr), - errno, strerror(errno) ); + ++(sss->stats.errors); + traceEvent(TRACE_ERROR, "unicast %lu to [%s] %s FAILED (%d: %s)", + pktsize, + sock_to_cstr( sockbuf, &(scan->sock) ), + macaddr_str(mac_buf, scan->mac_addr), + errno, strerror(errno) ); } } - else + else { - traceEvent( TRACE_DEBUG, "try_forward unknown MAC" ); + traceEvent( TRACE_DEBUG, "try_forward unknown MAC" ); - /* Not a known MAC so drop. */ + /* Not a known MAC so drop. */ } - return 0; + return 0; } @@ -275,46 +272,46 @@ static int try_broadcast( n2n_sn_t * sss, const uint8_t * pktbuf, size_t pktsize ) { - struct peer_info * scan; - macstr_t mac_buf; - n2n_sock_str_t sockbuf; + struct peer_info * scan; + macstr_t mac_buf; + n2n_sock_str_t sockbuf; - traceEvent( TRACE_DEBUG, "try_broadcast" ); + traceEvent( TRACE_DEBUG, "try_broadcast" ); - scan = sss->edges; - while(scan != NULL) + scan = sss->edges; + while(scan != NULL) { - if( 0 == (memcmp(scan->community_name, cmn->community, sizeof(n2n_community_t)) ) - && (0 != memcmp(srcMac, scan->mac_addr, sizeof(n2n_mac_t)) ) ) - /* REVISIT: exclude if the destination socket is where the packet came from. */ + if( 0 == (memcmp(scan->community_name, cmn->community, sizeof(n2n_community_t)) ) + && (0 != memcmp(srcMac, scan->mac_addr, sizeof(n2n_mac_t)) ) ) + /* REVISIT: exclude if the destination socket is where the packet came from. */ { - int data_sent_len; + int data_sent_len; - data_sent_len = sendto_sock(sss, &(scan->sock), pktbuf, pktsize); + data_sent_len = sendto_sock(sss, &(scan->sock), pktbuf, pktsize); - if(data_sent_len != pktsize) + if(data_sent_len != pktsize) { - ++(sss->stats.errors); - traceEvent(TRACE_WARNING, "multicast %lu to [%s] %s failed %s", - pktsize, - sock_to_cstr( sockbuf, &(scan->sock) ), - macaddr_str(mac_buf, scan->mac_addr), - strerror(errno)); + ++(sss->stats.errors); + traceEvent(TRACE_WARNING, "multicast %lu to [%s] %s failed %s", + pktsize, + sock_to_cstr( sockbuf, &(scan->sock) ), + macaddr_str(mac_buf, scan->mac_addr), + strerror(errno)); } - else + else { - ++(sss->stats.broadcast); - traceEvent(TRACE_DEBUG, "multicast %lu to [%s] %s", - pktsize, - sock_to_cstr( sockbuf, &(scan->sock) ), - macaddr_str(mac_buf, scan->mac_addr)); + ++(sss->stats.broadcast); + traceEvent(TRACE_DEBUG, "multicast %lu to [%s] %s", + pktsize, + sock_to_cstr( sockbuf, &(scan->sock) ), + macaddr_str(mac_buf, scan->mac_addr)); } } - scan = scan->next; + scan = scan->next; } /* while */ - return 0; + return 0; } @@ -324,61 +321,61 @@ static int process_mgmt( n2n_sn_t * sss, size_t mgmt_size, time_t now) { - char resbuf[N2N_SN_PKTBUF_SIZE]; - size_t ressize=0; - ssize_t r; + char resbuf[N2N_SN_PKTBUF_SIZE]; + size_t ressize=0; + ssize_t r; - traceEvent( TRACE_DEBUG, "process_mgmt" ); + traceEvent( TRACE_DEBUG, "process_mgmt" ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "----------------\n" ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "----------------\n" ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "uptime %lu\n", (now - sss->start_time) ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "uptime %lu\n", (now - sss->start_time) ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "edges %u\n", - (unsigned int)peer_list_size( sss->edges ) ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "edges %u\n", + (unsigned int)peer_list_size( sss->edges ) ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "errors %u\n", - (unsigned int)sss->stats.errors ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "errors %u\n", + (unsigned int)sss->stats.errors ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "reg_sup %u\n", - (unsigned int)sss->stats.reg_super ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "reg_sup %u\n", + (unsigned int)sss->stats.reg_super ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "reg_nak %u\n", - (unsigned int)sss->stats.reg_super_nak ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "reg_nak %u\n", + (unsigned int)sss->stats.reg_super_nak ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "fwd %u\n", - (unsigned int) sss->stats.fwd ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "fwd %u\n", + (unsigned int) sss->stats.fwd ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "broadcast %u\n", - (unsigned int) sss->stats.broadcast ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "broadcast %u\n", + (unsigned int) sss->stats.broadcast ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "last fwd %lu sec ago\n", - (long unsigned int)(now - sss->stats.last_fwd) ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "last fwd %lu sec ago\n", + (long unsigned int)(now - sss->stats.last_fwd) ); - ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "last reg %lu sec ago\n", - (long unsigned int) (now - sss->stats.last_reg_super) ); + ressize += snprintf( resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + "last reg %lu sec ago\n", + (long unsigned int) (now - sss->stats.last_reg_super) ); - r = sendto( sss->mgmt_sock, resbuf, ressize, 0/*flags*/, - (struct sockaddr *)sender_sock, sizeof(struct sockaddr_in) ); + r = sendto( sss->mgmt_sock, resbuf, ressize, 0/*flags*/, + (struct sockaddr *)sender_sock, sizeof(struct sockaddr_in) ); - if ( r <= 0 ) + if ( r <= 0 ) { - ++(sss->stats.errors); - traceEvent( TRACE_ERROR, "process_mgmt : sendto failed. %s", strerror(errno) ); + ++(sss->stats.errors); + traceEvent( TRACE_ERROR, "process_mgmt : sendto failed. %s", strerror(errno) ); } - return 0; + return 0; } @@ -391,223 +388,222 @@ static int process_udp( n2n_sn_t * sss, size_t udp_size, time_t now) { - n2n_common_t cmn; /* common fields in the packet header */ - size_t rem; - size_t idx; - size_t msg_type; - uint8_t from_supernode; - macstr_t mac_buf; - macstr_t mac_buf2; - n2n_sock_str_t sockbuf; - - - traceEvent( TRACE_DEBUG, "process_udp(%lu)", udp_size ); - - /* Use decode_common() to determine the kind of packet then process it: - * - * REGISTER_SUPER adds an edge and generate a return REGISTER_SUPER_ACK - * - * REGISTER, REGISTER_ACK and PACKET messages are forwarded to their - * destination edge. If the destination is not known then PACKETs are - * broadcast. - */ - - rem = udp_size; /* Counts down bytes of packet to protect against buffer overruns. */ - idx = 0; /* marches through packet header as parts are decoded. */ - if ( decode_common(&cmn, udp_buf, &rem, &idx) < 0 ) + n2n_common_t cmn; /* common fields in the packet header */ + size_t rem; + size_t idx; + size_t msg_type; + uint8_t from_supernode; + macstr_t mac_buf; + macstr_t mac_buf2; + n2n_sock_str_t sockbuf; + + + traceEvent( TRACE_DEBUG, "process_udp(%lu)", udp_size ); + + /* Use decode_common() to determine the kind of packet then process it: + * + * REGISTER_SUPER adds an edge and generate a return REGISTER_SUPER_ACK + * + * REGISTER, REGISTER_ACK and PACKET messages are forwarded to their + * destination edge. If the destination is not known then PACKETs are + * broadcast. + */ + + rem = udp_size; /* Counts down bytes of packet to protect against buffer overruns. */ + idx = 0; /* marches through packet header as parts are decoded. */ + if ( decode_common(&cmn, udp_buf, &rem, &idx) < 0 ) { - traceEvent( TRACE_ERROR, "Failed to decode common section" ); - return -1; /* failed to decode packet */ + traceEvent( TRACE_ERROR, "Failed to decode common section" ); + return -1; /* failed to decode packet */ } - msg_type = cmn.pc; /* packet code */ - from_supernode= cmn.flags & N2N_FLAGS_FROM_SUPERNODE; + msg_type = cmn.pc; /* packet code */ + from_supernode= cmn.flags & N2N_FLAGS_FROM_SUPERNODE; - if ( cmn.ttl < 1 ) + if ( cmn.ttl < 1 ) { - traceEvent( TRACE_WARNING, "Expired TTL" ); - return 0; /* Don't process further */ + traceEvent( TRACE_WARNING, "Expired TTL" ); + return 0; /* Don't process further */ } - --(cmn.ttl); /* The value copied into all forwarded packets. */ + --(cmn.ttl); /* The value copied into all forwarded packets. */ - if ( msg_type == MSG_TYPE_PACKET ) + if ( msg_type == MSG_TYPE_PACKET ) { - /* PACKET from one edge to another edge via supernode. */ + /* PACKET from one edge to another edge via supernode. */ - /* pkt will be modified in place and recoded to an output of potentially - * different size due to addition of the socket.*/ - n2n_PACKET_t pkt; - n2n_common_t cmn2; - uint8_t encbuf[N2N_SN_PKTBUF_SIZE]; - size_t encx=0; - int unicast; /* non-zero if unicast */ - const uint8_t * rec_buf; /* either udp_buf or encbuf */ + /* pkt will be modified in place and recoded to an output of potentially + * different size due to addition of the socket.*/ + n2n_PACKET_t pkt; + n2n_common_t cmn2; + uint8_t encbuf[N2N_SN_PKTBUF_SIZE]; + size_t encx=0; + int unicast; /* non-zero if unicast */ + const uint8_t * rec_buf; /* either udp_buf or encbuf */ - sss->stats.last_fwd=now; - decode_PACKET( &pkt, &cmn, udp_buf, &rem, &idx ); + sss->stats.last_fwd=now; + decode_PACKET( &pkt, &cmn, udp_buf, &rem, &idx ); - unicast = (0 == is_multi_broadcast(pkt.dstMac) ); + unicast = (0 == is_multi_broadcast(pkt.dstMac) ); - traceEvent( TRACE_DEBUG, "Rx PACKET (%s) %s -> %s %s", - (unicast?"unicast":"multicast"), - macaddr_str( mac_buf, pkt.srcMac ), - macaddr_str( mac_buf2, pkt.dstMac ), - (from_supernode?"from sn":"local") ); + traceEvent( TRACE_DEBUG, "Rx PACKET (%s) %s -> %s %s", + (unicast?"unicast":"multicast"), + macaddr_str( mac_buf, pkt.srcMac ), + macaddr_str( mac_buf2, pkt.dstMac ), + (from_supernode?"from sn":"local") ); - if ( !from_supernode ) + if ( !from_supernode ) { - memcpy( &cmn2, &cmn, sizeof( n2n_common_t ) ); + memcpy( &cmn2, &cmn, sizeof( n2n_common_t ) ); - /* We are going to add socket even if it was not there before */ - cmn2.flags |= N2N_FLAGS_SOCKET | N2N_FLAGS_FROM_SUPERNODE; + /* We are going to add socket even if it was not there before */ + cmn2.flags |= N2N_FLAGS_SOCKET | N2N_FLAGS_FROM_SUPERNODE; - pkt.sock.family = AF_INET; - pkt.sock.port = ntohs(sender_sock->sin_port); - memcpy( pkt.sock.addr.v4, &(sender_sock->sin_addr.s_addr), IPV4_SIZE ); + pkt.sock.family = AF_INET; + pkt.sock.port = ntohs(sender_sock->sin_port); + memcpy( pkt.sock.addr.v4, &(sender_sock->sin_addr.s_addr), IPV4_SIZE ); - rec_buf = encbuf; + rec_buf = encbuf; - /* Re-encode the header. */ - encode_PACKET( encbuf, &encx, &cmn2, &pkt ); + /* Re-encode the header. */ + encode_PACKET( encbuf, &encx, &cmn2, &pkt ); - /* Copy the original payload unchanged */ - encode_buf( encbuf, &encx, (udp_buf + idx), (udp_size - idx ) ); + /* Copy the original payload unchanged */ + encode_buf( encbuf, &encx, (udp_buf + idx), (udp_size - idx ) ); } - else + else { - /* Already from a supernode. Nothing to modify, just pass to - * destination. */ + /* Already from a supernode. Nothing to modify, just pass to + * destination. */ - traceEvent( TRACE_DEBUG, "Rx PACKET fwd unmodified" ); + traceEvent( TRACE_DEBUG, "Rx PACKET fwd unmodified" ); - rec_buf = udp_buf; - encx = udp_size; + rec_buf = udp_buf; + encx = udp_size; } - /* Common section to forward the final product. */ - if ( unicast ) + /* Common section to forward the final product. */ + if ( unicast ) { - try_forward( sss, &cmn, pkt.dstMac, rec_buf, encx ); + try_forward( sss, &cmn, pkt.dstMac, rec_buf, encx ); } - else + else { - try_broadcast( sss, &cmn, pkt.srcMac, rec_buf, encx ); + try_broadcast( sss, &cmn, pkt.srcMac, rec_buf, encx ); } }/* MSG_TYPE_PACKET */ - else if ( msg_type == MSG_TYPE_REGISTER ) + else if ( msg_type == MSG_TYPE_REGISTER ) { - /* Forwarding a REGISTER from one edge to the next */ + /* Forwarding a REGISTER from one edge to the next */ - n2n_REGISTER_t reg; - n2n_common_t cmn2; - uint8_t encbuf[N2N_SN_PKTBUF_SIZE]; - size_t encx=0; - int unicast; /* non-zero if unicast */ - const uint8_t * rec_buf; /* either udp_buf or encbuf */ + n2n_REGISTER_t reg; + n2n_common_t cmn2; + uint8_t encbuf[N2N_SN_PKTBUF_SIZE]; + size_t encx=0; + int unicast; /* non-zero if unicast */ + const uint8_t * rec_buf; /* either udp_buf or encbuf */ - sss->stats.last_fwd=now; - decode_REGISTER( ®, &cmn, udp_buf, &rem, &idx ); + sss->stats.last_fwd=now; + decode_REGISTER( ®, &cmn, udp_buf, &rem, &idx ); - unicast = (0 == is_multi_broadcast(reg.dstMac) ); + unicast = (0 == is_multi_broadcast(reg.dstMac) ); - if ( unicast ) + if ( unicast ) { - traceEvent( TRACE_DEBUG, "Rx REGISTER %s -> %s %s", - macaddr_str( mac_buf, reg.srcMac ), - macaddr_str( mac_buf2, reg.dstMac ), - ((cmn.flags & N2N_FLAGS_FROM_SUPERNODE)?"from sn":"local") ); + traceEvent( TRACE_DEBUG, "Rx REGISTER %s -> %s %s", + macaddr_str( mac_buf, reg.srcMac ), + macaddr_str( mac_buf2, reg.dstMac ), + ((cmn.flags & N2N_FLAGS_FROM_SUPERNODE)?"from sn":"local") ); - if ( 0 != (cmn.flags & N2N_FLAGS_FROM_SUPERNODE) ) - { - memcpy( &cmn2, &cmn, sizeof( n2n_common_t ) ); + if ( 0 != (cmn.flags & N2N_FLAGS_FROM_SUPERNODE) ) + { + memcpy( &cmn2, &cmn, sizeof( n2n_common_t ) ); - /* We are going to add socket even if it was not there before */ - cmn2.flags |= N2N_FLAGS_SOCKET | N2N_FLAGS_FROM_SUPERNODE; + /* We are going to add socket even if it was not there before */ + cmn2.flags |= N2N_FLAGS_SOCKET | N2N_FLAGS_FROM_SUPERNODE; - reg.sock.family = AF_INET; - reg.sock.port = ntohs(sender_sock->sin_port); - memcpy( reg.sock.addr.v4, &(sender_sock->sin_addr.s_addr), IPV4_SIZE ); + reg.sock.family = AF_INET; + reg.sock.port = ntohs(sender_sock->sin_port); + memcpy( reg.sock.addr.v4, &(sender_sock->sin_addr.s_addr), IPV4_SIZE ); - rec_buf = encbuf; + rec_buf = encbuf; - /* Re-encode the header. */ - encode_REGISTER( encbuf, &encx, &cmn2, ® ); + /* Re-encode the header. */ + encode_REGISTER( encbuf, &encx, &cmn2, ® ); - /* Copy the original payload unchanged */ - encode_buf( encbuf, &encx, (udp_buf + idx), (udp_size - idx ) ); - } - else - { - /* Already from a supernode. Nothing to modify, just pass to - * destination. */ + /* Copy the original payload unchanged */ + encode_buf( encbuf, &encx, (udp_buf + idx), (udp_size - idx ) ); + } + else + { + /* Already from a supernode. Nothing to modify, just pass to + * destination. */ - rec_buf = udp_buf; - encx = udp_size; - } + rec_buf = udp_buf; + encx = udp_size; + } - try_forward( sss, &cmn, reg.dstMac, rec_buf, encx ); /* unicast only */ + try_forward( sss, &cmn, reg.dstMac, rec_buf, encx ); /* unicast only */ } - else + else { - traceEvent( TRACE_ERROR, "Rx REGISTER with multicast destination" ); + traceEvent( TRACE_ERROR, "Rx REGISTER with multicast destination" ); } } - else if ( msg_type == MSG_TYPE_REGISTER_ACK ) + else if ( msg_type == MSG_TYPE_REGISTER_ACK ) { - traceEvent( TRACE_DEBUG, "Rx REGISTER_ACK (NOT IMPLEMENTED) SHould not be via supernode" ); + traceEvent( TRACE_DEBUG, "Rx REGISTER_ACK (NOT IMPLEMENTED) SHould not be via supernode" ); } - else if ( msg_type == MSG_TYPE_REGISTER_SUPER ) + else if ( msg_type == MSG_TYPE_REGISTER_SUPER ) { - n2n_REGISTER_SUPER_t reg; - n2n_REGISTER_SUPER_ACK_t ack; - n2n_common_t cmn2; - uint8_t ackbuf[N2N_SN_PKTBUF_SIZE]; - size_t encx=0; + n2n_REGISTER_SUPER_t reg; + n2n_REGISTER_SUPER_ACK_t ack; + n2n_common_t cmn2; + uint8_t ackbuf[N2N_SN_PKTBUF_SIZE]; + size_t encx=0; - /* Edge requesting registration with us. */ + /* Edge requesting registration with us. */ - sss->stats.last_reg_super=now; - ++(sss->stats.reg_super); - decode_REGISTER_SUPER( ®, &cmn, udp_buf, &rem, &idx ); + sss->stats.last_reg_super=now; + ++(sss->stats.reg_super); + decode_REGISTER_SUPER( ®, &cmn, udp_buf, &rem, &idx ); - cmn2.ttl = N2N_DEFAULT_TTL; - cmn2.pc = n2n_register_super_ack; - cmn2.flags = N2N_FLAGS_SOCKET | N2N_FLAGS_FROM_SUPERNODE; - memcpy( cmn2.community, cmn.community, sizeof(n2n_community_t) ); + cmn2.ttl = N2N_DEFAULT_TTL; + cmn2.pc = n2n_register_super_ack; + cmn2.flags = N2N_FLAGS_SOCKET | N2N_FLAGS_FROM_SUPERNODE; + memcpy( cmn2.community, cmn.community, sizeof(n2n_community_t) ); - memcpy( &(ack.cookie), &(reg.cookie), sizeof(n2n_cookie_t) ); - memcpy( ack.edgeMac, reg.edgeMac, sizeof(n2n_mac_t) ); - ack.lifetime = reg_lifetime( sss ); + memcpy( &(ack.cookie), &(reg.cookie), sizeof(n2n_cookie_t) ); + memcpy( ack.edgeMac, reg.edgeMac, sizeof(n2n_mac_t) ); + ack.lifetime = reg_lifetime( sss ); - ack.sock.family = AF_INET; - ack.sock.port = ntohs(sender_sock->sin_port); - memcpy( ack.sock.addr.v4, &(sender_sock->sin_addr.s_addr), IPV4_SIZE ); + ack.sock.family = AF_INET; + ack.sock.port = ntohs(sender_sock->sin_port); + memcpy( ack.sock.addr.v4, &(sender_sock->sin_addr.s_addr), IPV4_SIZE ); - ack.num_sn=0; /* No backup */ - memset( &(ack.sn_bak), 0, sizeof(n2n_sock_t) ); + ack.num_sn=0; /* No backup */ + memset( &(ack.sn_bak), 0, sizeof(n2n_sock_t) ); - traceEvent( TRACE_DEBUG, "Rx REGISTER_SUPER for %s [%s]", - macaddr_str( mac_buf, reg.edgeMac ), - sock_to_cstr( sockbuf, &(ack.sock) ) ); + traceEvent( TRACE_DEBUG, "Rx REGISTER_SUPER for %s [%s]", + macaddr_str( mac_buf, reg.edgeMac ), + sock_to_cstr( sockbuf, &(ack.sock) ) ); - update_edge( sss, reg.edgeMac, cmn.community, &(ack.sock), now ); + update_edge( sss, reg.edgeMac, cmn.community, &(ack.sock), now ); - encode_REGISTER_SUPER_ACK( ackbuf, &encx, &cmn2, &ack ); + encode_REGISTER_SUPER_ACK( ackbuf, &encx, &cmn2, &ack ); - sendto( sss->sock, ackbuf, encx, 0, - (struct sockaddr *)sender_sock, sizeof(struct sockaddr_in) ); + sendto( sss->sock, ackbuf, encx, 0, + (struct sockaddr *)sender_sock, sizeof(struct sockaddr_in) ); - traceEvent( TRACE_DEBUG, "Tx REGISTER_SUPER_ACK for %s [%s]", - macaddr_str( mac_buf, reg.edgeMac ), - sock_to_cstr( sockbuf, &(ack.sock) ) ); + traceEvent( TRACE_DEBUG, "Tx REGISTER_SUPER_ACK for %s [%s]", + macaddr_str( mac_buf, reg.edgeMac ), + sock_to_cstr( sockbuf, &(ack.sock) ) ); } - - return 0; + return 0; } /* *************************************************** */ @@ -617,6 +613,9 @@ static void help() { print_n2n_version(); + printf("supernode (see supernode.conf)\n" + "or\n" + ); printf("supernode "); printf("-l "); printf("[-f] "); @@ -645,31 +644,31 @@ static int setOption(int optkey, char *optarg, n2n_sn_t *sss) { //traceEvent(TRACE_NORMAL, "Option %c = %s", optkey, optarg ? optarg : ""); switch(optkey) { - case 'l': /* local-port */ + case 'l': /* local-port */ { sss->lport = atoi(optarg); break; } - case 'f': /* foreground */ + case 'f': /* foreground */ { sss->daemon = 0; break; } - case 'h': /* help */ + case 'h': /* help */ { help(); break; } - case 'v': /* verbose */ + case 'v': /* verbose */ { ++traceLevel; break; } - default: + default: { traceEvent(TRACE_WARNING, "Unknown option -%c: Ignored.", (char)optkey); return(-1); @@ -791,63 +790,66 @@ static int loadFromFile(const char *path, n2n_sn_t *sss) { /** Main program entry point from kernel. */ int main( int argc, char * const argv[] ) { - int rc; - n2n_sn_t sss; + int rc; + n2n_sn_t sss; + + if(argc == 1) + help(); - init_sn( &sss ); + init_sn(&sss); #ifndef WIN32 - if((argc >= 2) && (argv[1][0] != '-')) + if((argc >= 2) && (argv[1][0] != '-')) { - rc = loadFromFile(argv[1], &sss); - if (argc > 2) - rc = loadFromCLI(argc, argv, &sss); + rc = loadFromFile(argv[1], &sss); + if (argc > 2) + rc = loadFromCLI(argc, argv, &sss); } else #endif rc = loadFromCLI(argc, argv, &sss); - if (rc < 0) - return(-1); + if (rc < 0) + return(-1); #if defined(N2N_HAVE_DAEMON) - if (sss.daemon) + if (sss.daemon) { - useSyslog=1; /* traceEvent output now goes to syslog. */ - if ( -1 == daemon( 0, 0 ) ) + useSyslog=1; /* traceEvent output now goes to syslog. */ + if ( -1 == daemon( 0, 0 ) ) { - traceEvent( TRACE_ERROR, "Failed to become daemon." ); - exit(-5); + traceEvent( TRACE_ERROR, "Failed to become daemon." ); + exit(-5); } } #endif /* #if defined(N2N_HAVE_DAEMON) */ - traceEvent( TRACE_DEBUG, "traceLevel is %d", traceLevel); + traceEvent( TRACE_DEBUG, "traceLevel is %d", traceLevel); - sss.sock = open_socket(sss.lport, 1 /*bind ANY*/ ); - if ( -1 == sss.sock ) + sss.sock = open_socket(sss.lport, 1 /*bind ANY*/ ); + if ( -1 == sss.sock ) { - traceEvent( TRACE_ERROR, "Failed to open main socket. %s", strerror(errno) ); - exit(-2); + traceEvent( TRACE_ERROR, "Failed to open main socket. %s", strerror(errno) ); + exit(-2); } - else + else { - traceEvent( TRACE_NORMAL, "supernode is listening on UDP %u (main)", sss.lport ); + traceEvent( TRACE_NORMAL, "supernode is listening on UDP %u (main)", sss.lport ); } - sss.mgmt_sock = open_socket(N2N_SN_MGMT_PORT, 0 /* bind LOOPBACK */ ); - if ( -1 == sss.mgmt_sock ) + sss.mgmt_sock = open_socket(N2N_SN_MGMT_PORT, 0 /* bind LOOPBACK */ ); + if ( -1 == sss.mgmt_sock ) { - traceEvent( TRACE_ERROR, "Failed to open management socket. %s", strerror(errno) ); - exit(-2); + 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)", N2N_SN_MGMT_PORT ); + traceEvent( TRACE_NORMAL, "supernode is listening on UDP %u (management)", N2N_SN_MGMT_PORT ); } - traceEvent(TRACE_NORMAL, "supernode started"); + traceEvent(TRACE_NORMAL, "supernode started"); - return run_loop(&sss); + return run_loop(&sss); } @@ -855,89 +857,88 @@ int main( int argc, char * const argv[] ) * daemonisation on some platforms. */ static int run_loop( n2n_sn_t * sss ) { - uint8_t pktbuf[N2N_SN_PKTBUF_SIZE]; - int keep_running=1; + uint8_t pktbuf[N2N_SN_PKTBUF_SIZE]; + int keep_running=1; - sss->start_time = time(NULL); + sss->start_time = time(NULL); - while(keep_running) + while(keep_running) { - int rc; - ssize_t bread; - int max_sock; - fd_set socket_mask; - struct timeval wait_time; - time_t now=0; + int rc; + ssize_t bread; + int max_sock; + fd_set socket_mask; + struct timeval wait_time; + time_t now=0; - FD_ZERO(&socket_mask); - max_sock = MAX(sss->sock, sss->mgmt_sock); + FD_ZERO(&socket_mask); + max_sock = MAX(sss->sock, sss->mgmt_sock); - FD_SET(sss->sock, &socket_mask); - FD_SET(sss->mgmt_sock, &socket_mask); + FD_SET(sss->sock, &socket_mask); + FD_SET(sss->mgmt_sock, &socket_mask); - wait_time.tv_sec = 10; wait_time.tv_usec = 0; - rc = select(max_sock+1, &socket_mask, NULL, NULL, &wait_time); + wait_time.tv_sec = 10; wait_time.tv_usec = 0; + rc = select(max_sock+1, &socket_mask, NULL, NULL, &wait_time); - now = time(NULL); + now = time(NULL); - if(rc > 0) + if(rc > 0) { - if (FD_ISSET(sss->sock, &socket_mask)) + if (FD_ISSET(sss->sock, &socket_mask)) { - struct sockaddr_in sender_sock; - socklen_t i; + struct sockaddr_in sender_sock; + socklen_t i; - i = sizeof(sender_sock); - bread = recvfrom( sss->sock, pktbuf, N2N_SN_PKTBUF_SIZE, 0/*flags*/, - (struct sockaddr *)&sender_sock, (socklen_t*)&i); + i = sizeof(sender_sock); + bread = recvfrom( sss->sock, pktbuf, N2N_SN_PKTBUF_SIZE, 0/*flags*/, + (struct sockaddr *)&sender_sock, (socklen_t*)&i); - if ( bread < 0 ) /* For UDP bread of zero just means no data (unlike TCP). */ + if ( bread < 0 ) /* For UDP bread of zero just means no data (unlike TCP). */ { - /* The fd is no good now. Maybe we lost our interface. */ - traceEvent( TRACE_ERROR, "recvfrom() failed %d errno %d (%s)", bread, errno, strerror(errno) ); - keep_running=0; - break; + /* The fd is no good now. Maybe we lost our interface. */ + traceEvent( TRACE_ERROR, "recvfrom() failed %d errno %d (%s)", bread, errno, strerror(errno) ); + keep_running=0; + break; } - /* We have a datagram to process */ - if ( bread > 0 ) + /* We have a datagram to process */ + if ( bread > 0 ) { - /* And the datagram has data (not just a header) */ - process_udp( sss, &sender_sock, pktbuf, bread, now ); + /* And the datagram has data (not just a header) */ + process_udp( sss, &sender_sock, pktbuf, bread, now ); } } - if (FD_ISSET(sss->mgmt_sock, &socket_mask)) + if (FD_ISSET(sss->mgmt_sock, &socket_mask)) { - struct sockaddr_in sender_sock; - size_t i; + struct sockaddr_in sender_sock; + size_t i; - i = sizeof(sender_sock); - bread = recvfrom( sss->mgmt_sock, pktbuf, N2N_SN_PKTBUF_SIZE, 0/*flags*/, - (struct sockaddr *)&sender_sock, (socklen_t*)&i); + i = sizeof(sender_sock); + bread = recvfrom( sss->mgmt_sock, pktbuf, N2N_SN_PKTBUF_SIZE, 0/*flags*/, + (struct sockaddr *)&sender_sock, (socklen_t*)&i); - if ( bread <= 0 ) + if ( bread <= 0 ) { - traceEvent( TRACE_ERROR, "recvfrom() failed %d errno %d (%s)", bread, errno, strerror(errno) ); - keep_running=0; - break; + traceEvent( TRACE_ERROR, "recvfrom() failed %d errno %d (%s)", bread, errno, strerror(errno) ); + keep_running=0; + break; } - /* We have a datagram to process */ - process_mgmt( sss, &sender_sock, pktbuf, bread, now ); + /* We have a datagram to process */ + process_mgmt( sss, &sender_sock, pktbuf, bread, now ); } } - else + else { - traceEvent( TRACE_DEBUG, "timeout" ); + traceEvent( TRACE_DEBUG, "timeout" ); } - purge_expired_registrations( &(sss->edges) ); + purge_expired_registrations( &(sss->edges) ); } /* while */ - deinit_sn( sss ); + deinit_sn( sss ); - return 0; + return 0; } -