Minor code cleanup

This commit is contained in:
Luca Deri 2018-06-07 21:45:33 +02:00
parent 1daf46275e
commit 41401a131f
3 changed files with 103 additions and 100 deletions

42
edge.c
View File

@ -549,7 +549,7 @@ static void send_register(n2n_edge_t * eee,
{
uint8_t pktbuf[N2N_PKT_BUF_SIZE];
size_t idx;
ssize_t sent;
/* ssize_t sent; */
n2n_common_t cmn;
n2n_REGISTER_t reg;
n2n_sock_str_t sockbuf;
@ -572,19 +572,16 @@ static void send_register(n2n_edge_t * eee,
traceEvent(TRACE_INFO, "send REGISTER %s",
sock_to_cstr(sockbuf, remote_peer));
sent = sendto_sock(eee->udp_sock, pktbuf, idx, remote_peer);
/* sent = */ sendto_sock(eee->udp_sock, pktbuf, idx, remote_peer);
}
/** Send a REGISTER_SUPER packet to the current supernode. */
static void send_register_super(n2n_edge_t * eee,
const n2n_sock_t * supernode)
{
const n2n_sock_t * supernode) {
uint8_t pktbuf[N2N_PKT_BUF_SIZE];
size_t idx;
ssize_t sent;
/* ssize_t sent; */
n2n_common_t cmn;
n2n_REGISTER_SUPER_t reg;
n2n_sock_str_t sockbuf;
@ -614,19 +611,17 @@ static void send_register_super(n2n_edge_t * eee,
sock_to_cstr(sockbuf, supernode));
sent = sendto_sock(eee->udp_sock, pktbuf, idx, supernode);
/* sent = */ sendto_sock(eee->udp_sock, pktbuf, idx, supernode);
}
/** Send a REGISTER_ACK packet to a peer edge. */
static void send_register_ack(n2n_edge_t * eee,
const n2n_sock_t * remote_peer,
const n2n_REGISTER_t * reg)
{
const n2n_REGISTER_t * reg) {
uint8_t pktbuf[N2N_PKT_BUF_SIZE];
size_t idx;
ssize_t sent;
/* ssize_t sent; */
n2n_common_t cmn;
n2n_REGISTER_ACK_t ack;
n2n_sock_str_t sockbuf;
@ -650,7 +645,7 @@ static void send_register_ack(n2n_edge_t * eee,
sock_to_cstr(sockbuf, remote_peer));
sent = sendto_sock(eee->udp_sock, pktbuf, idx, remote_peer);
/* sent = */ sendto_sock(eee->udp_sock, pktbuf, idx, remote_peer);
}
@ -1128,7 +1123,7 @@ static int send_PACKET(n2n_edge_t * eee,
size_t pktlen)
{
int dest;
ssize_t s;
/*ssize_t s; */
n2n_sock_str_t sockbuf;
n2n_sock_t destination;
@ -1137,17 +1132,13 @@ static int send_PACKET(n2n_edge_t * eee,
dest = find_peer_destination(eee, dstMac, &destination);
if(dest)
{
++(eee->tx_p2p);
}
++(eee->tx_p2p);
else
{
++(eee->tx_sup);
}
++(eee->tx_sup);
traceEvent(TRACE_INFO, "send_PACKET to %s", sock_to_cstr(sockbuf, &destination));
s = sendto_sock(eee->udp_sock, pktbuf, pktlen, &destination);
/* s = */ sendto_sock(eee->udp_sock, pktbuf, pktlen, &destination);
return 0;
}
@ -1414,7 +1405,7 @@ static void readFromMgmtSocket(n2n_edge_t * eee, int * keep_running)
{
uint8_t udp_buf[N2N_PKT_BUF_SIZE]; /* Compete UDP packet */
ssize_t recvlen;
ssize_t sendlen;
/* ssize_t sendlen; */
struct sockaddr_in sender_sock;
socklen_t i;
size_t msg_len;
@ -1563,9 +1554,8 @@ static void readFromMgmtSocket(n2n_edge_t * eee, int * keep_running)
traceEvent(TRACE_DEBUG, "mgmt status sending: %s", udp_buf);
sendlen = sendto(eee->udp_mgmt_sock, udp_buf, msg_len, 0/*flags*/,
(struct sockaddr *)&sender_sock, sizeof(struct sockaddr_in));
/* sendlen = */ sendto(eee->udp_mgmt_sock, udp_buf, msg_len, 0/*flags*/,
(struct sockaddr *)&sender_sock, sizeof(struct sockaddr_in));
}
@ -1920,7 +1910,7 @@ static int run_loop(n2n_edge_t * eee);
/* *************************************************** */
void daemonize() {
static void daemonize() {
#ifndef WIN32
int childpid;

96
n2n.c
View File

@ -167,7 +167,7 @@ char* intoa(uint32_t /* host order */ addr, char* buf, uint16_t buf_len) {
/* *********************************************** */
char * macaddr_str(macstr_t buf,
const n2n_mac_t mac )
const n2n_mac_t mac)
{
snprintf(buf, N2N_MACSTR_SIZE, "%02X:%02X:%02X:%02X:%02X:%02X",
mac[0] & 0xFF, mac[1] & 0xFF, mac[2] & 0xFF,
@ -179,9 +179,9 @@ char * macaddr_str(macstr_t buf,
uint8_t is_multi_broadcast(const uint8_t * dest_mac) {
int is_broadcast =(memcmp(broadcast_addr, dest_mac, 6) == 0 );
int is_multicast =(memcmp(multicast_addr, dest_mac, 3) == 0 );
int is_ipv6_multicast =(memcmp(ipv6_multicast_addr, dest_mac, 2) == 0 );
int is_broadcast =(memcmp(broadcast_addr, dest_mac, 6) == 0);
int is_multicast =(memcmp(multicast_addr, dest_mac, 3) == 0);
int is_ipv6_multicast =(memcmp(ipv6_multicast_addr, dest_mac, 2) == 0);
return is_broadcast || is_multicast || is_ipv6_multicast;
@ -214,7 +214,7 @@ void hexdump(const uint8_t * buf, size_t len)
{
size_t i;
if(0 == len ) { return; }
if(0 == len) { return; }
for(i=0; i<len; i++)
{
@ -242,11 +242,11 @@ void print_n2n_version() {
*
* @return NULL if not found; otherwise pointer to peer entry.
*/
struct peer_info * find_peer_by_mac(struct peer_info * list, const n2n_mac_t mac )
struct peer_info * find_peer_by_mac(struct peer_info * list, const n2n_mac_t mac)
{
while(list != NULL)
{
if(0 == memcmp(mac, list->mac_addr, 6) )
if(0 == memcmp(mac, list->mac_addr, 6))
{
return list;
}
@ -260,11 +260,11 @@ struct peer_info * find_peer_by_mac(struct peer_info * list, const n2n_mac_t mac
/** Return the number of elements in the list.
*
*/
size_t peer_list_size(const struct peer_info * list )
size_t peer_list_size(const struct peer_info * list)
{
size_t retval=0;
while(list )
while(list)
{
++retval;
list = list->next;
@ -279,7 +279,7 @@ size_t peer_list_size(const struct peer_info * list )
* insertion. list takes ownership of new.
*/
void peer_list_add(struct peer_info * * list,
struct peer_info * new )
struct peer_info * new)
{
new->next = *list;
new->last_seen = time(NULL);
@ -287,7 +287,7 @@ void peer_list_add(struct peer_info * * list,
}
size_t purge_expired_registrations(struct peer_info ** peer_list ) {
size_t purge_expired_registrations(struct peer_info ** peer_list) {
static time_t last_purge = 0;
time_t now = time(NULL);
size_t num_reg = 0;
@ -296,7 +296,7 @@ size_t purge_expired_registrations(struct peer_info ** peer_list ) {
traceEvent(TRACE_INFO, "Purging old registrations");
num_reg = purge_peer_list(peer_list, now-REGISTRATION_TIMEOUT );
num_reg = purge_peer_list(peer_list, now-REGISTRATION_TIMEOUT);
last_purge = now;
traceEvent(TRACE_INFO, "Remove %ld registrations", num_reg);
@ -306,7 +306,7 @@ size_t purge_expired_registrations(struct peer_info ** peer_list ) {
/** Purge old items from the peer_list and return the number of items that were removed. */
size_t purge_peer_list(struct peer_info ** peer_list,
time_t purge_before )
time_t purge_before)
{
struct peer_info *scan;
struct peer_info *prev;
@ -344,7 +344,7 @@ size_t purge_peer_list(struct peer_info ** peer_list,
}
/** Purge all items from the peer_list and return the number of items that were removed. */
size_t clear_peer_list(struct peer_info ** peer_list )
size_t clear_peer_list(struct peer_info ** peer_list)
{
struct peer_info *scan;
struct peer_info *prev;
@ -373,17 +373,17 @@ size_t clear_peer_list(struct peer_info ** peer_list )
return retval;
}
static uint8_t hex2byte(const char * s )
static uint8_t hex2byte(const char * s)
{
char tmp[3];
tmp[0]=s[0];
tmp[1]=s[1];
tmp[2]=0; /* NULL term */
return((uint8_t)strtol(tmp, NULL, 16 ));
return((uint8_t)strtol(tmp, NULL, 16));
}
extern int str2mac(uint8_t * outmac /* 6 bytes */, const char * s )
extern int str2mac(uint8_t * outmac /* 6 bytes */, const char * s)
{
size_t i;
@ -394,7 +394,7 @@ extern int str2mac(uint8_t * outmac /* 6 bytes */, const char * s )
++outmac;
s+=2; /* don't skip colon yet - helps generalise loop. */
for(i=1; i<6; ++i )
for(i=1; i<6; ++i)
{
s+=1;
*outmac=hex2byte(s);
@ -406,45 +406,45 @@ extern int str2mac(uint8_t * outmac /* 6 bytes */, const char * s )
}
extern char * sock_to_cstr(n2n_sock_str_t out,
const n2n_sock_t * sock )
{
int r;
if(NULL == out ) { return NULL; }
const n2n_sock_t * sock) {
if(NULL == out) { return NULL; }
memset(out, 0, N2N_SOCKBUF_SIZE);
if(AF_INET6 == sock->family )
{
/* INET6 not written yet */
r = snprintf(out, N2N_SOCKBUF_SIZE, "XXXX:%hu", sock->port );
return out;
}
else
{
const uint8_t * a = sock->addr.v4;
r = snprintf(out, N2N_SOCKBUF_SIZE, "%hu.%hu.%hu.%hu:%hu",
(unsigned short)(a[0] & 0xff),
(unsigned short)(a[1] & 0xff),
(unsigned short)(a[2] & 0xff),
(unsigned short)(a[3] & 0xff),
(unsigned short)sock->port );
return out;
}
if(AF_INET6 == sock->family) {
/* INET6 not written yet */
snprintf(out, N2N_SOCKBUF_SIZE, "XXXX:%hu", sock->port);
return out;
} else {
const uint8_t * a = sock->addr.v4;
snprintf(out, N2N_SOCKBUF_SIZE, "%hu.%hu.%hu.%hu:%hu",
(unsigned short)(a[0] & 0xff),
(unsigned short)(a[1] & 0xff),
(unsigned short)(a[2] & 0xff),
(unsigned short)(a[3] & 0xff),
(unsigned short)sock->port);
return out;
}
}
/* @return zero if the two sockets are equivalent. */
int sock_equal(const n2n_sock_t * a,
const n2n_sock_t * b )
{
if(a->port != b->port ) { return 1; }
if(a->family != b->family ) { return 1; }
switch(a->family) /* they are the same */
{
const n2n_sock_t * b) {
if(a->port != b->port) { return 1; }
if(a->family != b->family) { return 1; }
switch(a->family) {
case AF_INET:
if(0 != memcmp(a->addr.v4, b->addr.v4, IPV4_SIZE ) ) { return 1;};
if(0 != memcmp(a->addr.v4, b->addr.v4, IPV4_SIZE)) {
return 1;
}
break;
default:
if(0 != memcmp(a->addr.v6, b->addr.v6, IPV6_SIZE ) ) { return 1;};
if(0 != memcmp(a->addr.v6, b->addr.v6, IPV6_SIZE)) {
return 1;
}
break;
}

View File

@ -20,6 +20,8 @@
#ifdef __linux__
/* *************************************************** */
static void read_mac(char *ifname, n2n_mac_t mac_addr) {
int _sock, res;
struct ifreq ifr;
@ -95,24 +97,24 @@ int tuntap_open(tuntap_dev *device,
strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ) );
if ( device_mac && device_mac[0] != '\0' )
{
{
/* Set the hw address before bringing the if up. */
snprintf(buf, sizeof(buf), "/sbin/ifconfig %s hw ether %s",
ifr.ifr_name, device_mac );
system(buf);
traceEvent(TRACE_INFO, "Setting MAC: %s", buf);
}
}
if ( 0 == strncmp( "dhcp", address_mode, 5 ) )
{
{
snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s mtu %d up",
ifr.ifr_name, device_ip, mtu);
}
}
else
{
{
snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s netmask %s mtu %d up",
ifr.ifr_name, device_ip, device_mask, mtu);
}
}
system(buf);
traceEvent(TRACE_INFO, "Bringing up: %s", buf);
@ -123,45 +125,56 @@ int tuntap_open(tuntap_dev *device,
return(device->fd);
}
/* *************************************************** */
int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(read(tuntap->fd, buf, len));
}
/* *************************************************** */
int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(write(tuntap->fd, buf, len));
}
/* *************************************************** */
void tuntap_close(struct tuntap_dev *tuntap) {
close(tuntap->fd);
}
/* *************************************************** */
/* Fill out the ip_addr value from the interface. Called to pick up dynamic
* address changes. */
void tuntap_get_address(struct tuntap_dev *tuntap)
{
FILE * fp=NULL;
ssize_t nread=0;
char buf[N2N_LINUX_SYSTEMCMD_SIZE];
void tuntap_get_address(struct tuntap_dev *tuntap) {
FILE * fp=NULL;
ssize_t nread=0;
char buf[N2N_LINUX_SYSTEMCMD_SIZE];
/* Would rather have a more direct way to get the inet address but a netlink
* socket is overkill and probably less portable than ifconfig and sed. */
/* Would rather have a more direct way to get the inet address but a netlink
* socket is overkill and probably less portable than ifconfig and sed. */
/* If the interface has no address (0.0.0.0) there will be no inet addr
* line and the returned string will be empty. */
snprintf( buf, sizeof(buf), "/sbin/ifconfig %s | /bin/sed -e '/inet addr:/!d' -e 's/^.*inet addr://' -e 's/ .*$//'",
tuntap->dev_name );
fp=popen(buf, "r");
if (fp )
{
memset(buf,0,N2N_LINUX_SYSTEMCMD_SIZE); /* make sure buf is NULL terminated. */
nread=fread(buf, 1, 15, fp);
fclose(fp);
fp=NULL;
/* If the interface has no address (0.0.0.0) there will be no inet addr
* line and the returned string will be empty. */
snprintf( buf, sizeof(buf),
"/sbin/ifconfig %s | /bin/sed -e '/inet addr:/!d' -e 's/^.*inet addr://' -e 's/ .*$//'",
tuntap->dev_name);
fp = popen(buf, "r");
traceEvent(TRACE_INFO, "ifconfig address = %s", buf);
if (fp) {
memset(buf, 0, N2N_LINUX_SYSTEMCMD_SIZE); /* make sure buf is NULL terminated. */
nread = fread(buf, N2N_LINUX_SYSTEMCMD_SIZE-1, 1, fp);
fclose(fp);
fp = NULL;
tuntap->ip_addr = inet_addr(buf);
traceEvent(TRACE_INFO, "ifconfig address = %s", buf);
if(nread > 0) {
buf[nread] = '\0';
tuntap->ip_addr = inet_addr(buf);
}
}
}