@ -60,13 +60,13 @@ struct n2n_allowed_communities {
static struct n2n_allowed_communities * allowed_communities = NULL ;
static int try_forward ( n2n_sn_t * sss ,
static int try_forward ( n2n_sn_t * sss ,
const n2n_common_t * cmn ,
const n2n_mac_t dstMac ,
const uint8_t * pktbuf ,
size_t pktsize ) ;
static int try_broadcast ( n2n_sn_t * sss ,
static int try_broadcast ( n2n_sn_t * sss ,
const n2n_common_t * cmn ,
const n2n_mac_t srcMac ,
const uint8_t * pktbuf ,
@ -123,7 +123,7 @@ static uint16_t reg_lifetime(n2n_sn_t * sss) {
/** Update the edge table with the details of the edge which contacted the
* supernode . */
static int update_edge ( n2n_sn_t * sss ,
static int update_edge ( n2n_sn_t * sss ,
const n2n_mac_t edgeMac ,
const n2n_community_t community ,
const n2n_sock_t * sender_sock ,
@ -184,9 +184,9 @@ static int update_edge(n2n_sn_t * sss,
*
* @ return - 1 on error otherwise number of bytes sent
*/
static ssize_t sendto_sock ( n2n_sn_t * sss ,
const n2n_sock_t * sock ,
const uint8_t * pktbuf ,
static ssize_t sendto_sock ( n2n_sn_t * sss ,
const n2n_sock_t * sock ,
const uint8_t * pktbuf ,
size_t pktsize )
{
n2n_sock_str_t sockbuf ;
@ -203,7 +203,7 @@ static ssize_t sendto_sock(n2n_sn_t * sss,
pktsize ,
sock_to_cstr ( sockbuf , sock ) ) ;
return sendto ( sss - > sock , pktbuf , pktsize , 0 ,
return sendto ( sss - > sock , pktbuf , pktsize , 0 ,
( const struct sockaddr * ) & udpsock , sizeof ( struct sockaddr_in ) ) ;
}
else
@ -219,7 +219,7 @@ static ssize_t sendto_sock(n2n_sn_t * sss,
/** Try to forward a message to a unicast MAC. If the MAC is unknown then
* broadcast to all edges in the destination community .
*/
static int try_forward ( n2n_sn_t * sss ,
static int try_forward ( n2n_sn_t * sss ,
const n2n_common_t * cmn ,
const n2n_mac_t dstMac ,
const uint8_t * pktbuf ,
@ -260,7 +260,7 @@ static int try_forward(n2n_sn_t * sss,
/* Not a known MAC so drop. */
}
return 0 ;
}
@ -270,7 +270,7 @@ static int try_forward(n2n_sn_t * sss,
* This will send the exact same datagram to zero or more edges registered to
* the supernode .
*/
static int try_broadcast ( n2n_sn_t * sss ,
static int try_broadcast ( n2n_sn_t * sss ,
const n2n_common_t * cmn ,
const n2n_mac_t srcMac ,
const uint8_t * pktbuf ,
@ -283,14 +283,14 @@ static int try_broadcast(n2n_sn_t * sss,
traceEvent ( TRACE_DEBUG , " try_broadcast " ) ;
scan = sss - > edges ;
while ( scan ! = NULL )
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. */
{
int data_sent_len ;
data_sent_len = sendto_sock ( sss , & ( scan - > sock ) , pktbuf , pktsize ) ;
if ( data_sent_len ! = pktsize )
@ -302,7 +302,7 @@ static int try_broadcast(n2n_sn_t * sss,
macaddr_str ( mac_buf , scan - > mac_addr ) ,
strerror ( errno ) ) ;
}
else
else
{
+ + ( sss - > stats . broadcast ) ;
traceEvent ( TRACE_DEBUG , " multicast %lu to [%s] %s " ,
@ -314,14 +314,14 @@ static int try_broadcast(n2n_sn_t * sss,
scan = scan - > next ;
} /* while */
return 0 ;
}
static int process_mgmt ( n2n_sn_t * sss ,
static int process_mgmt ( n2n_sn_t * sss ,
const struct sockaddr_in * sender_sock ,
const uint8_t * mgmt_buf ,
const uint8_t * mgmt_buf ,
size_t mgmt_size ,
time_t now )
{
@ -331,46 +331,46 @@ static int process_mgmt(n2n_sn_t * sss,
traceEvent ( TRACE_DEBUG , " process_mgmt " ) ;
ressize + = snprintf ( resbuf + ressize , N2N_SN_PKTBUF_SIZE - ressize ,
ressize + = snprintf ( resbuf + ressize , N2N_SN_PKTBUF_SIZE - ressize ,
" ---------------- \n " ) ;
ressize + = snprintf ( resbuf + ressize , N2N_SN_PKTBUF_SIZE - ressize ,
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 " ,
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 " ,
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 " ,
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 " ,
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 ,
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 ,
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 " ,
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 ,
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*/ ,
r = sendto ( sss - > mgmt_sock , resbuf , ressize , 0 /*flags*/ ,
( struct sockaddr * ) sender_sock , sizeof ( struct sockaddr_in ) ) ;
if ( r < = 0 )
@ -390,13 +390,13 @@ static int process_mgmt(n2n_sn_t * sss,
static int allowed_n2n_community ( n2n_common_t * cmn ) {
if ( allowed_communities ! = NULL ) {
struct n2n_allowed_communities * c ;
HASH_FIND_STR ( allowed_communities , ( const char * ) cmn - > community , c ) ;
return ( ( c = = NULL ) ? 0 : 1 ) ;
} else {
/* If no allowed community is defined, all communities are allowed */
}
return ( 1 ) ;
}
@ -408,18 +408,18 @@ static int load_allowed_n2n_communities(char *path) {
FILE * fd = fopen ( path , " r " ) ;
struct n2n_allowed_communities * s , * tmp ;
uint32_t num_communities = 0 ;
if ( fd = = NULL ) {
traceEvent ( TRACE_WARNING , " File %s not found " , path ) ;
return - 1 ;
}
HASH_ITER ( hh , allowed_communities , s , tmp )
free ( s ) ;
while ( ( line = fgets ( buffer , sizeof ( buffer ) , fd ) ) ! = NULL ) {
int len = strlen ( line ) ;
if ( ( len < 2 ) | | line [ 0 ] = = ' # ' )
continue ;
@ -431,7 +431,7 @@ static int load_allowed_n2n_communities(char *path) {
} else
break ;
}
s = ( struct n2n_allowed_communities * ) malloc ( sizeof ( struct n2n_allowed_communities ) ) ;
if ( s ! = NULL ) {
@ -448,16 +448,16 @@ static int load_allowed_n2n_communities(char *path) {
traceEvent ( TRACE_NORMAL , " Loaded %u communities from %s " ,
num_communities , path ) ;
return ( 0 ) ;
}
/** Examine a datagram and determine what to do with it.
*
*/
static int process_udp ( n2n_sn_t * sss ,
static int process_udp ( n2n_sn_t * sss ,
const struct sockaddr_in * sender_sock ,
const uint8_t * udp_buf ,
const uint8_t * udp_buf ,
size_t udp_size ,
time_t now )
{
@ -508,7 +508,7 @@ static int process_udp(n2n_sn_t * sss,
/* 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_PACKET_t pkt ;
n2n_common_t cmn2 ;
uint8_t encbuf [ N2N_SN_PKTBUF_SIZE ] ;
size_t encx = 0 ;
@ -576,7 +576,7 @@ static int process_udp(n2n_sn_t * sss,
decode_REGISTER ( & reg , & cmn , udp_buf , & rem , & idx ) ;
unicast = ( 0 = = is_multi_broadcast ( reg . dstMac ) ) ;
if ( unicast ) {
traceEvent ( TRACE_DEBUG , " Rx REGISTER %s -> %s %s " ,
macaddr_str ( mac_buf , reg . srcMac ) ,
@ -624,7 +624,7 @@ static int process_udp(n2n_sn_t * sss,
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 ( & reg , & cmn , udp_buf , & rem , & idx ) ;
@ -660,7 +660,7 @@ static int process_udp(n2n_sn_t * sss,
encode_REGISTER_SUPER_ACK ( ackbuf , & encx , & cmn2 , & ack ) ;
sendto ( sss - > sock , ackbuf , encx , 0 ,
sendto ( sss - > sock , ackbuf , encx , 0 ,
( struct sockaddr * ) sender_sock , sizeof ( struct sockaddr_in ) ) ;
traceEvent ( TRACE_DEBUG , " Tx REGISTER_SUPER_ACK for %s [%s] " ,
@ -670,6 +670,42 @@ static int process_udp(n2n_sn_t * sss,
traceEvent ( TRACE_INFO , " Discarded registration: unallowed community '%s' " ,
( char * ) cmn . community ) ;
break ;
} case MSG_TYPE_QUERY_PEER : {
n2n_QUERY_PEER_t query ;
uint8_t encbuf [ N2N_SN_PKTBUF_SIZE ] ;
size_t encx = 0 ;
n2n_common_t cmn2 ;
n2n_PEER_INFO_t pi ;
struct peer_info * scan ;
decode_QUERY_PEER ( & query , & cmn , udp_buf , & rem , & idx ) ;
traceEvent ( TRACE_DEBUG , " Rx QUERY_PEER from %s for %s " ,
macaddr_str ( mac_buf , query . srcMac ) ,
macaddr_str ( mac_buf2 , query . targetMac ) ) ;
scan = find_peer_by_mac ( sss - > edges , query . targetMac ) ;
if ( scan ) {
cmn2 . ttl = N2N_DEFAULT_TTL ;
cmn2 . pc = n2n_peer_info ;
cmn2 . flags = N2N_FLAGS_FROM_SUPERNODE ;
memcpy ( cmn2 . community , cmn . community , sizeof ( n2n_community_t ) ) ;
pi . aflags = 0 ;
memcpy ( pi . mac , query . targetMac , sizeof ( n2n_mac_t ) ) ;
pi . sock = scan - > sock ;
encode_PEER_INFO ( encbuf , & encx , & cmn2 , & pi ) ;
sendto ( sss - > sock , encbuf , encx , 0 ,
( struct sockaddr * ) sender_sock , sizeof ( struct sockaddr_in ) ) ;
traceEvent ( TRACE_DEBUG , " Tx PEER_INFO to %s " ,
macaddr_str ( mac_buf , query . srcMac ) ) ;
} else {
traceEvent ( TRACE_DEBUG , " Ignoring QUERY_PEER for unknown edge %s " ,
macaddr_str ( mac_buf , query . targetMac ) ) ;
}
}
default :
/* Not a known message type */
@ -720,11 +756,11 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
case ' l ' : /* local-port */
sss - > lport = atoi ( _optarg ) ;
break ;
case ' c ' : /* community file */
load_allowed_n2n_communities ( optarg ) ;
break ;
case ' f ' : /* foreground */
sss - > daemon = 0 ;
break ;
@ -778,7 +814,7 @@ static char *trim(char *s) {
while ( isspace ( s [ 0 ] ) | | ( s [ 0 ] = = ' " ' ) | | ( s [ 0 ] = = ' \' ' ) )
s + + ;
if ( s [ 0 ] = = 0 ) return s ;
end = & s [ strlen ( s ) - 1 ] ;
@ -861,9 +897,9 @@ static void dump_registrations(int signo) {
char buf [ 32 ] ;
time_t now = time ( NULL ) ;
u_int num = 0 ;
traceEvent ( TRACE_NORMAL , " ==================================== " ) ;
while ( list ! = NULL ) {
if ( list - > sock . family = = AF_INET )
traceEvent ( TRACE_NORMAL , " [id: %u][MAC: %s][edge: %u.%u.%u.%u:%u][community: %s][last seen: %u sec ago] " ,
@ -910,7 +946,7 @@ int main(int argc, char * const argv[]) {
# if defined(N2N_HAVE_DAEMON)
if ( sss_node . daemon ) {
useSyslog = 1 ; /* traceEvent output now goes to syslog. */
if ( - 1 = = daemon ( 0 , 0 ) ) {
traceEvent ( TRACE_ERROR , " Failed to become daemon. " ) ;
exit ( - 5 ) ;
@ -940,7 +976,7 @@ int main(int argc, char * const argv[]) {
# ifndef WIN32
signal ( SIGHUP , dump_registrations ) ;
# endif
return run_loop ( & sss_node ) ;
}