From 24b2f2762f277c2bdab60defeeda6cceff035cce Mon Sep 17 00:00:00 2001 From: Francesco Carli <62562180+fcarli3@users.noreply.github.com> Date: Sat, 19 Dec 2020 12:28:39 +0100 Subject: [PATCH] readability code clean-up (#544) --- include/n2n_wire.h | 170 +++++++------- src/wire.c | 555 ++++++++++++++++++++++++--------------------- 2 files changed, 385 insertions(+), 340 deletions(-) diff --git a/include/n2n_wire.h b/include/n2n_wire.h index fb970ef..07e6016 100644 --- a/include/n2n_wire.h +++ b/include/n2n_wire.h @@ -35,183 +35,183 @@ #include "sn_selection.h" -int encode_uint8( uint8_t * base, +int encode_uint8 (uint8_t * base, size_t * idx, - const uint8_t v ); + const uint8_t v); -int decode_uint8( uint8_t * out, +int decode_uint8 (uint8_t * out, const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_uint16( uint8_t * base, +int encode_uint16 (uint8_t * base, size_t * idx, - const uint16_t v ); + const uint16_t v); -int decode_uint16( uint16_t * out, +int decode_uint16 (uint16_t * out, const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_uint32( uint8_t * base, +int encode_uint32 (uint8_t * base, size_t * idx, - const uint32_t v ); + const uint32_t v); -int decode_uint32( uint32_t * out, +int decode_uint32 (uint32_t * out, const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_uint64( uint8_t * base, +int encode_uint64 (uint8_t * base, size_t * idx, - const uint64_t v ); + const uint64_t v); -int decode_uint64( uint64_t * out, +int decode_uint64 (uint64_t * out, const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_buf( uint8_t * base, +int encode_buf (uint8_t * base, size_t * idx, const void * p, size_t s); -int decode_buf( uint8_t * out, +int decode_buf (uint8_t * out, size_t bufsize, const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_mac( uint8_t * base, +int encode_mac (uint8_t * base, size_t * idx, - const n2n_mac_t m ); + const n2n_mac_t m); -int decode_mac( uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passing a n2n_mac_t */ +int decode_mac (uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passing a n2n_mac_t */ const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_common( uint8_t * base, +int encode_common (uint8_t * base, size_t * idx, - const n2n_common_t * common ); + const n2n_common_t * common); -int decode_common( n2n_common_t * out, +int decode_common (n2n_common_t * out, const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_sock( uint8_t * base, +int encode_sock (uint8_t * base, size_t * idx, - const n2n_sock_t * sock ); + const n2n_sock_t * sock); -int decode_sock( n2n_sock_t * sock, +int decode_sock (n2n_sock_t * sock, const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_REGISTER( uint8_t * base, +int encode_REGISTER (uint8_t * base, size_t * idx, const n2n_common_t * common, - const n2n_REGISTER_t * reg ); + const n2n_REGISTER_t * reg); -int decode_REGISTER( n2n_REGISTER_t * pkt, +int decode_REGISTER (n2n_REGISTER_t * pkt, const n2n_common_t * cmn, /* info on how to interpret it */ const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_REGISTER_SUPER( uint8_t * base, +int encode_REGISTER_SUPER (uint8_t * base, size_t * idx, const n2n_common_t * common, - const n2n_REGISTER_SUPER_t * reg ); + const n2n_REGISTER_SUPER_t * reg); -int decode_REGISTER_SUPER( n2n_REGISTER_SUPER_t * pkt, +int decode_REGISTER_SUPER (n2n_REGISTER_SUPER_t * pkt, const n2n_common_t * cmn, /* info on how to interpret it */ const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_UNREGISTER_SUPER(uint8_t *base, - size_t *idx, - const n2n_common_t *common, - const n2n_UNREGISTER_SUPER_t *unreg); +int encode_UNREGISTER_SUPER (uint8_t *base, + size_t *idx, + const n2n_common_t *common, + const n2n_UNREGISTER_SUPER_t *unreg); -int decode_UNREGISTER_SUPER(n2n_UNREGISTER_SUPER_t *unreg, - const n2n_common_t *cmn, /* info on how to interpret it */ - const uint8_t *base, - size_t *rem, - size_t *idx); +int decode_UNREGISTER_SUPER (n2n_UNREGISTER_SUPER_t *unreg, + const n2n_common_t *cmn, /* info on how to interpret it */ + const uint8_t *base, + size_t *rem, + size_t *idx); -int encode_REGISTER_ACK( uint8_t * base, +int encode_REGISTER_ACK (uint8_t * base, size_t * idx, const n2n_common_t * common, - const n2n_REGISTER_ACK_t * reg ); + const n2n_REGISTER_ACK_t * reg); -int decode_REGISTER_ACK( n2n_REGISTER_ACK_t * pkt, +int decode_REGISTER_ACK (n2n_REGISTER_ACK_t * pkt, const n2n_common_t * cmn, /* info on how to interpret it */ const uint8_t * base, size_t * rem, - size_t * idx ); + size_t * idx); -int encode_REGISTER_SUPER_ACK( uint8_t * base, +int encode_REGISTER_SUPER_ACK (uint8_t * base, size_t * idx, const n2n_common_t * cmn, const n2n_REGISTER_SUPER_ACK_t * reg, uint8_t * tmpbuf); -int decode_REGISTER_SUPER_ACK( n2n_REGISTER_SUPER_ACK_t * reg, +int decode_REGISTER_SUPER_ACK (n2n_REGISTER_SUPER_ACK_t * reg, const n2n_common_t * cmn, /* info on how to interpret it */ const uint8_t * base, size_t * rem, size_t * idx, uint8_t * tmpbuf); - -int encode_REGISTER_SUPER_NAK( uint8_t * base, + +int encode_REGISTER_SUPER_NAK (uint8_t * base, size_t * idx, const n2n_common_t * cmn, const n2n_REGISTER_SUPER_NAK_t * nak); -int decode_REGISTER_SUPER_NAK( n2n_REGISTER_SUPER_NAK_t * nak, +int decode_REGISTER_SUPER_NAK (n2n_REGISTER_SUPER_NAK_t * nak, const n2n_common_t * cmn, /* info on how to interpret it */ const uint8_t * base, size_t * rem, size_t * idx); -int fill_sockaddr( struct sockaddr * addr, +int fill_sockaddr (struct sockaddr * addr, size_t addrlen, - const n2n_sock_t * sock ); + const n2n_sock_t * sock); -int encode_PACKET( uint8_t * base, +int encode_PACKET (uint8_t * base, size_t * idx, const n2n_common_t * common, - const n2n_PACKET_t * pkt ); + const n2n_PACKET_t * pkt); -int decode_PACKET( n2n_PACKET_t * pkt, +int decode_PACKET (n2n_PACKET_t * pkt, const n2n_common_t * cmn, /* info on how to interpret it */ const uint8_t * base, size_t * rem, - size_t * idx ); - -int encode_PEER_INFO( uint8_t * base, - size_t * idx, - const n2n_common_t * common, - const n2n_PEER_INFO_t * pkt); - -int decode_PEER_INFO( n2n_PEER_INFO_t * pkt, - const n2n_common_t * cmn, /* info on how to interpret it */ - const uint8_t * base, - size_t * rem, - size_t * idx ); - -int encode_QUERY_PEER( uint8_t * base, - size_t * idx, - const n2n_common_t * common, - const n2n_QUERY_PEER_t * pkt ); - -int decode_QUERY_PEER( n2n_QUERY_PEER_t * pkt, - const n2n_common_t * cmn, /* info on how to interpret it */ - const uint8_t * base, - size_t * rem, - size_t * idx ); + size_t * idx); + +int encode_PEER_INFO (uint8_t * base, + size_t * idx, + const n2n_common_t * common, + const n2n_PEER_INFO_t * pkt); + +int decode_PEER_INFO (n2n_PEER_INFO_t * pkt, + const n2n_common_t * cmn, /* info on how to interpret it */ + const uint8_t * base, + size_t * rem, + size_t * idx); + +int encode_QUERY_PEER (uint8_t * base, + size_t * idx, + const n2n_common_t * common, + const n2n_QUERY_PEER_t * pkt); + +int decode_QUERY_PEER (n2n_QUERY_PEER_t * pkt, + const n2n_common_t * cmn, /* info on how to interpret it */ + const uint8_t * base, + size_t * rem, + size_t * idx); #endif /* #if !defined( N2N_WIRE_H_ ) */ diff --git a/src/wire.c b/src/wire.c index 5043322..4b7356c 100644 --- a/src/wire.c +++ b/src/wire.c @@ -29,70 +29,81 @@ #include "n2n.h" -int encode_uint8( uint8_t * base, +int encode_uint8 (uint8_t * base, size_t * idx, - const uint8_t v ) -{ + const uint8_t v) { + *(base + (*idx)) = (v & 0xff); ++(*idx); + return 1; } -int decode_uint8( uint8_t * out, +int decode_uint8 (uint8_t * out, const uint8_t * base, size_t * rem, - size_t * idx ) -{ - if (*rem < 1 ) { return 0; } + size_t * idx) { + + if(*rem < 1) { + return 0; + } *out = ( base[*idx] & 0xff ); ++(*idx); --(*rem); + return 1; } -int encode_uint16( uint8_t * base, +int encode_uint16 (uint8_t * base, size_t * idx, - const uint16_t v ) -{ + const uint16_t v) { + *(base + (*idx)) = ( v >> 8) & 0xff; *(base + (1 + *idx)) = ( v & 0xff ); *idx += 2; + return 2; } -int decode_uint16( uint16_t * out, +int decode_uint16 (uint16_t * out, const uint8_t * base, size_t * rem, - size_t * idx ) -{ - if (*rem < 2 ) { return 0; } + size_t * idx) { + + if (*rem < 2) { + return 0; + } *out = ( base[*idx] & 0xff ) << 8; *out |= ( base[1 + *idx] & 0xff ); *idx += 2; *rem -= 2; + return 2; } -int encode_uint32( uint8_t * base, +int encode_uint32 (uint8_t * base, size_t * idx, - const uint32_t v ) -{ + const uint32_t v) { + *(base + (0 + *idx)) = ( v >> 24) & 0xff; *(base + (1 + *idx)) = ( v >> 16) & 0xff; *(base + (2 + *idx)) = ( v >> 8) & 0xff; *(base + (3 + *idx)) = ( v & 0xff ); *idx += 4; + return 4; } -int decode_uint32( uint32_t * out, +int decode_uint32 (uint32_t * out, const uint8_t * base, size_t * rem, - size_t * idx ) -{ - if (*rem < 4 ) { return 0; } + size_t * idx) { + + if (*rem < 4) { + return 0; + } *out = ( base[0 + *idx] & 0xff ) << 24; *out |= ( base[1 + *idx] & 0xff ) << 16; @@ -100,190 +111,199 @@ int decode_uint32( uint32_t * out, *out |= ( base[3 + *idx] & 0xff ); *idx += 4; *rem -= 4; + return 4; } -int encode_uint64( uint8_t * base, +int encode_uint64 (uint8_t * base, size_t * idx, - const uint64_t v ) -{ + const uint64_t v) { + *(uint64_t*)(base + *idx) = htobe64(v); *idx += 8; + return 8; } -int decode_uint64( uint64_t * out, +int decode_uint64 (uint64_t * out, const uint8_t * base, size_t * rem, - size_t * idx ) -{ - if (*rem < 8 ) { return 0; } + size_t * idx) { + + if (*rem < 8) { + return 0; + } *out = be64toh(*(uint64_t*)base + *idx); *idx += 8; *rem -= 8; + return 8; } -int encode_buf( uint8_t * base, +int encode_buf (uint8_t * base, size_t * idx, const void * p, - size_t s) -{ - memcpy( (base + (*idx)), p, s ); + size_t s) { + + memcpy((base + (*idx)), p, s); *idx += s; + return s; } /* Copy from base to out of size bufsize */ -int decode_buf( uint8_t * out, +int decode_buf (uint8_t * out, size_t bufsize, const uint8_t * base, size_t * rem, - size_t * idx ) -{ - if (*rem < bufsize ) { return 0; } + size_t * idx) { + + if (*rem < bufsize) { + return 0; + } - memcpy( out, (base + *idx), bufsize ); + memcpy(out, (base + *idx), bufsize); *idx += bufsize; *rem -= bufsize; + return bufsize; } -int encode_mac( uint8_t * base, +int encode_mac (uint8_t * base, size_t * idx, - const n2n_mac_t m ) -{ - return encode_buf( base, idx, m, N2N_MAC_SIZE ); + const n2n_mac_t m) { + + return encode_buf(base, idx, m, N2N_MAC_SIZE); } -int decode_mac( uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passing a n2n_mac_t */ +int decode_mac (uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passing a n2n_mac_t */ const uint8_t * base, size_t * rem, - size_t * idx ) -{ - return decode_buf( out, N2N_MAC_SIZE, base, rem, idx ); + size_t * idx) { + + return decode_buf(out, N2N_MAC_SIZE, base, rem, idx); } -int encode_common( uint8_t * base, +int encode_common (uint8_t * base, size_t * idx, - const n2n_common_t * common ) -{ - uint16_t flags=0; - encode_uint8( base, idx, N2N_PKT_VERSION ); - encode_uint8( base, idx, common->ttl ); + const n2n_common_t * common) { + + uint16_t flags = 0; + + encode_uint8(base, idx, N2N_PKT_VERSION); + encode_uint8(base, idx, common->ttl); flags = common->pc & N2N_FLAGS_TYPE_MASK; flags |= common->flags & N2N_FLAGS_BITS_MASK; - encode_uint16( base, idx, flags ); - encode_buf( base, idx, common->community, N2N_COMMUNITY_SIZE ); + encode_uint16(base, idx, flags); + encode_buf(base, idx, common->community, N2N_COMMUNITY_SIZE); return -1; } -int decode_common( n2n_common_t * out, +int decode_common (n2n_common_t * out, const uint8_t * base, size_t * rem, - size_t * idx ) -{ - size_t idx0=*idx; - uint8_t dummy=0; - decode_uint8( &dummy, base, rem, idx ); - - if ( N2N_PKT_VERSION != dummy ) - { - return -1; - } + size_t * idx) { + + size_t idx0 = *idx; + uint8_t dummy = 0; - decode_uint8( &(out->ttl), base, rem, idx ); - decode_uint16( &(out->flags), base, rem, idx ); + decode_uint8(&dummy, base, rem, idx); + + if (N2N_PKT_VERSION != dummy) { + return -1; + } + + decode_uint8(&(out->ttl), base, rem, idx); + decode_uint16(&(out->flags), base, rem, idx); out->pc = ( out->flags & N2N_FLAGS_TYPE_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); return (*idx - idx0); } -int encode_sock( uint8_t * base, +int encode_sock (uint8_t * base, size_t * idx, - const n2n_sock_t * sock ) -{ - int retval=0; + const n2n_sock_t * sock) { + + int retval = 0; uint16_t f; - switch (sock->family) - { - case AF_INET: - { - f = 0; - retval += encode_uint16(base,idx,f); - retval += encode_uint16(base,idx,sock->port); - retval += encode_buf(base,idx,sock->addr.v4,IPV4_SIZE); - break; - } - case AF_INET6: - { - f = 0x8000; - retval += encode_uint16(base,idx,f); - retval += encode_uint16(base,idx,sock->port); - retval += encode_buf(base,idx,sock->addr.v6,IPV6_SIZE); - break; - } - default: - retval=-1; + switch(sock->family) { + case AF_INET: { + f = 0; + retval += encode_uint16(base, idx, f); + retval += encode_uint16(base, idx, sock->port); + retval += encode_buf(base, idx, sock->addr.v4, IPV4_SIZE); + break; + } + + case AF_INET6: { + f = 0x8000; + retval += encode_uint16(base, idx, f); + retval += encode_uint16(base, idx, sock->port); + retval += encode_buf(base, idx, sock->addr.v6, IPV6_SIZE); + break; } + default: + retval = -1; + } + return retval; } -int decode_sock( n2n_sock_t * sock, +int decode_sock (n2n_sock_t * sock, const uint8_t * base, size_t * rem, - size_t * idx ) -{ - size_t * idx0=idx; - uint16_t f=0; - - decode_uint16( &f, base, rem, idx ); - - if( f & 0x8000 ) - { - /* IPv6 */ - sock->family = AF_INET6; - decode_uint16( &(sock->port), base, rem, idx ); - decode_buf( sock->addr.v6, IPV6_SIZE, base, rem, idx ); - } - else - { - /* IPv4 */ - sock->family = AF_INET; - decode_uint16( &(sock->port), base, rem, idx ); - memset( sock->addr.v6, 0, IPV6_SIZE ); /* so memcmp() works for equality. */ - decode_buf( sock->addr.v4, IPV4_SIZE, base, rem, idx ); - } + size_t * idx) { + + size_t * idx0 = idx; + uint16_t f = 0; + + decode_uint16(&f, base, rem, idx); + + if(f & 0x8000) { + + /* IPv6 */ + sock->family = AF_INET6; + decode_uint16(&(sock->port), base, rem, idx); + decode_buf(sock->addr.v6, IPV6_SIZE, base, rem, idx); + } else { + /* IPv4 */ + sock->family = AF_INET; + decode_uint16(&(sock->port), base, rem, idx); + memset(sock->addr.v6, 0, IPV6_SIZE); /* so memcmp() works for equality. */ + decode_buf(sock->addr.v4, IPV4_SIZE, base, rem, idx); + } return (idx-idx0); } -int encode_REGISTER(uint8_t *base, - size_t *idx, - const n2n_common_t *common, - const n2n_REGISTER_t *reg) { +int encode_REGISTER (uint8_t *base, + size_t *idx, + const n2n_common_t *common, + const n2n_REGISTER_t *reg) { + int retval = 0; + retval += encode_common(base, idx, common); retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE); retval += encode_mac(base, idx, reg->srcMac); retval += encode_mac(base, idx, reg->dstMac); - if (0 != reg->sock.family) { + if(0 != reg->sock.family) { retval += encode_sock(base, idx, &(reg->sock)); } retval += encode_uint32(base, idx, reg->dev_addr.net_addr); @@ -294,18 +314,19 @@ int encode_REGISTER(uint8_t *base, } -int decode_REGISTER(n2n_REGISTER_t *reg, - const n2n_common_t *cmn, /* info on how to interpret it */ - const uint8_t *base, - size_t *rem, - size_t *idx) { +int decode_REGISTER (n2n_REGISTER_t *reg, + const n2n_common_t *cmn, /* info on how to interpret it */ + const uint8_t *base, + size_t *rem, + size_t *idx) { + size_t retval = 0; memset(reg, 0, sizeof(n2n_REGISTER_t)); + retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_mac(reg->srcMac, base, rem, idx); retval += decode_mac(reg->dstMac, base, rem, idx); - - if (cmn->flags & N2N_FLAGS_SOCKET) { + if(cmn->flags & N2N_FLAGS_SOCKET) { retval += decode_sock(&(reg->sock), base, rem, idx); } retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx); @@ -316,21 +337,23 @@ int decode_REGISTER(n2n_REGISTER_t *reg, } -int encode_REGISTER_SUPER(uint8_t *base, - size_t *idx, - const n2n_common_t *common, - const n2n_REGISTER_SUPER_t *reg) { +int encode_REGISTER_SUPER (uint8_t *base, + size_t *idx, + const n2n_common_t *common, + const n2n_REGISTER_SUPER_t *reg) { + int retval = 0; + retval += encode_common(base, idx, common); retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE); retval += encode_mac(base, idx, reg->edgeMac); - if (0 != reg->sock.family) { + if(0 != reg->sock.family) { retval += encode_sock(base, idx, &(reg->sock)); } retval += encode_uint32(base, idx, reg->dev_addr.net_addr); retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen); retval += encode_buf(base, idx, reg->dev_desc, N2N_DESC_SIZE); - retval += encode_uint16(base, idx, reg->auth.scheme); + retval += encode_uint16(base, idx, reg->auth.scheme); retval += encode_uint16(base, idx, reg->auth.toksize); retval += encode_buf(base, idx, reg->auth.token, reg->auth.toksize); @@ -338,16 +361,18 @@ int encode_REGISTER_SUPER(uint8_t *base, } -int decode_REGISTER_SUPER(n2n_REGISTER_SUPER_t *reg, - const n2n_common_t *cmn, /* info on how to interpret it */ - const uint8_t *base, - size_t *rem, - size_t *idx) { +int decode_REGISTER_SUPER (n2n_REGISTER_SUPER_t *reg, + const n2n_common_t *cmn, /* info on how to interpret it */ + const uint8_t *base, + size_t *rem, + size_t *idx) { + size_t retval = 0; memset(reg, 0, sizeof(n2n_REGISTER_SUPER_t)); + retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_mac(reg->edgeMac, base, rem, idx); - if (cmn->flags & N2N_FLAGS_SOCKET) { + if(cmn->flags & N2N_FLAGS_SOCKET) { retval += decode_sock(&(reg->sock), base, rem, idx); } retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx); @@ -356,17 +381,20 @@ int decode_REGISTER_SUPER(n2n_REGISTER_SUPER_t *reg, retval += decode_uint16(&(reg->auth.scheme), base, rem, idx); retval += decode_uint16(&(reg->auth.toksize), base, rem, idx); retval += decode_buf(reg->auth.token, reg->auth.toksize, base, rem, idx); + return retval; } -int encode_UNREGISTER_SUPER(uint8_t *base, - size_t *idx, - const n2n_common_t *common, - const n2n_UNREGISTER_SUPER_t *unreg){ +int encode_UNREGISTER_SUPER (uint8_t *base, + size_t *idx, + const n2n_common_t *common, + const n2n_UNREGISTER_SUPER_t *unreg) { + int retval = 0; + retval += encode_common(base, idx, common); - retval += encode_uint16(base, idx, unreg->auth.scheme); + retval += encode_uint16(base, idx, unreg->auth.scheme); retval += encode_uint16(base, idx, unreg->auth.toksize); retval += encode_buf(base, idx, unreg->auth.token, unreg->auth.toksize); retval += encode_mac(base, idx, unreg->srcMac); @@ -375,13 +403,15 @@ int encode_UNREGISTER_SUPER(uint8_t *base, } -int decode_UNREGISTER_SUPER(n2n_UNREGISTER_SUPER_t *unreg, - const n2n_common_t *cmn, /* info on how to interpret it */ - const uint8_t *base, - size_t *rem, - size_t *idx){ +int decode_UNREGISTER_SUPER (n2n_UNREGISTER_SUPER_t *unreg, + const n2n_common_t *cmn, /* info on how to interpret it */ + const uint8_t *base, + size_t *rem, + size_t *idx) { + size_t retval = 0; memset(unreg, 0, sizeof(n2n_UNREGISTER_SUPER_t)); + retval += decode_uint16(&(unreg->auth.scheme), base, rem, idx); retval += decode_uint16(&(unreg->auth.toksize), base, rem, idx); retval += decode_buf(unreg->auth.token, unreg->auth.toksize, base, rem, idx); @@ -391,11 +421,13 @@ int decode_UNREGISTER_SUPER(n2n_UNREGISTER_SUPER_t *unreg, } -int encode_REGISTER_ACK(uint8_t *base, - size_t *idx, - const n2n_common_t *common, - const n2n_REGISTER_ACK_t *reg) { +int encode_REGISTER_ACK (uint8_t *base, + size_t *idx, + const n2n_common_t *common, + const n2n_REGISTER_ACK_t *reg) { + int retval = 0; + retval += encode_common(base, idx, common); retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE); retval += encode_mac(base, idx, reg->dstMac); @@ -404,7 +436,7 @@ int encode_REGISTER_ACK(uint8_t *base, /* The socket in REGISTER_ACK is the socket from which the REGISTER * arrived. This is sent back to the sender so it knows what its public * socket is. */ - if (0 != reg->sock.family) { + if(0 != reg->sock.family) { retval += encode_sock(base, idx, &(reg->sock)); } @@ -412,13 +444,15 @@ int encode_REGISTER_ACK(uint8_t *base, } -int decode_REGISTER_ACK(n2n_REGISTER_ACK_t *reg, - const n2n_common_t *cmn, /* info on how to interpret it */ - const uint8_t *base, - size_t *rem, - size_t *idx) { +int decode_REGISTER_ACK (n2n_REGISTER_ACK_t *reg, + const n2n_common_t *cmn, /* info on how to interpret it */ + const uint8_t *base, + size_t *rem, + size_t *idx) { + size_t retval = 0; memset(reg, 0, sizeof(n2n_REGISTER_ACK_t)); + retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_mac(reg->dstMac, base, rem, idx); retval += decode_mac(reg->srcMac, base, rem, idx); @@ -426,7 +460,7 @@ int decode_REGISTER_ACK(n2n_REGISTER_ACK_t *reg, /* The socket in REGISTER_ACK is the socket from which the REGISTER * arrived. This is sent back to the sender so it knows what its public * socket is. */ - if (cmn->flags & N2N_FLAGS_SOCKET) { + if(cmn->flags & N2N_FLAGS_SOCKET) { retval += decode_sock(&(reg->sock), base, rem, idx); } @@ -434,12 +468,14 @@ int decode_REGISTER_ACK(n2n_REGISTER_ACK_t *reg, } -int encode_REGISTER_SUPER_ACK(uint8_t *base, - size_t *idx, - const n2n_common_t *common, - const n2n_REGISTER_SUPER_ACK_t *reg, - uint8_t *tmpbuf) { +int encode_REGISTER_SUPER_ACK (uint8_t *base, + size_t *idx, + const n2n_common_t *common, + const n2n_REGISTER_SUPER_ACK_t *reg, + uint8_t *tmpbuf) { + int retval = 0; + retval += encode_common(base, idx, common); retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE); retval += encode_mac(base, idx, reg->edgeMac); @@ -454,15 +490,16 @@ int encode_REGISTER_SUPER_ACK(uint8_t *base, } -int decode_REGISTER_SUPER_ACK(n2n_REGISTER_SUPER_ACK_t *reg, - const n2n_common_t *cmn, /* info on how to interpret it */ - const uint8_t *base, - size_t *rem, - size_t *idx, - uint8_t *tmpbuf) { - size_t retval = 0; +int decode_REGISTER_SUPER_ACK (n2n_REGISTER_SUPER_ACK_t *reg, + const n2n_common_t *cmn, /* info on how to interpret it */ + const uint8_t *base, + size_t *rem, + size_t *idx, + uint8_t *tmpbuf) { + size_t retval = 0; memset(reg, 0, sizeof(n2n_REGISTER_SUPER_ACK_t)); + retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_mac(reg->edgeMac, base, rem, idx); retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx); @@ -480,103 +517,107 @@ int decode_REGISTER_SUPER_ACK(n2n_REGISTER_SUPER_ACK_t *reg, } -int encode_REGISTER_SUPER_NAK(uint8_t *base, - size_t *idx, - const n2n_common_t *common, - const n2n_REGISTER_SUPER_NAK_t *nak) { +int encode_REGISTER_SUPER_NAK (uint8_t *base, + size_t *idx, + const n2n_common_t *common, + const n2n_REGISTER_SUPER_NAK_t *nak) { + int retval = 0; + retval += encode_common(base, idx, common); - retval += encode_buf(base, idx, nak->cookie, N2N_COOKIE_SIZE); + retval += encode_buf(base, idx, nak->cookie, N2N_COOKIE_SIZE); retval += encode_mac(base, idx, nak->srcMac); - - return retval; + + return retval; } -int decode_REGISTER_SUPER_NAK(n2n_REGISTER_SUPER_NAK_t *nak, - const n2n_common_t *cmn, /* info on how to interpret it */ - const uint8_t *base, - size_t *rem, - size_t *idx) { +int decode_REGISTER_SUPER_NAK (n2n_REGISTER_SUPER_NAK_t *nak, + const n2n_common_t *cmn, /* info on how to interpret it */ + const uint8_t *base, + size_t *rem, + size_t *idx) { + size_t retval = 0; memset(nak, 0, sizeof(n2n_REGISTER_SUPER_NAK_t)); + retval += decode_buf(nak->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_mac(nak->srcMac, base, rem, idx); - - return retval; + + return retval; } -int fill_sockaddr( struct sockaddr * addr, +int fill_sockaddr (struct sockaddr * addr, size_t addrlen, - const n2n_sock_t * sock ) -{ - int retval=-1; - - if ( AF_INET == sock->family ) - { - if ( addrlen >= sizeof(struct sockaddr_in) ) - { - struct sockaddr_in * si = (struct sockaddr_in *)addr; - si->sin_family = sock->family; - si->sin_port = htons( sock->port ); - memcpy( &(si->sin_addr.s_addr), sock->addr.v4, IPV4_SIZE ); - retval=0; - } + const n2n_sock_t * sock) { + + int retval = -1; + + if(AF_INET == sock->family) { + if(addrlen >= sizeof(struct sockaddr_in)) { + struct sockaddr_in * si = (struct sockaddr_in *)addr; + si->sin_family = sock->family; + si->sin_port = htons(sock->port); + memcpy(&(si->sin_addr.s_addr), sock->addr.v4, IPV4_SIZE); + retval = 0; } + } return retval; } -int encode_PACKET( uint8_t * base, +int encode_PACKET (uint8_t * base, size_t * idx, const n2n_common_t * common, - const n2n_PACKET_t * pkt ) -{ - int retval=0; - retval += encode_common( base, idx, common ); - retval += encode_mac( base, idx, pkt->srcMac ); - retval += encode_mac( base, idx, pkt->dstMac ); - if ( 0 != pkt->sock.family ) - { - retval += encode_sock( base, idx, &(pkt->sock) ); - } - retval += encode_uint8( base, idx, pkt->compression ); - retval += encode_uint8( base, idx, pkt->transform ); + const n2n_PACKET_t * pkt) { + + int retval = 0; + + retval += encode_common(base, idx, common); + retval += encode_mac(base, idx, pkt->srcMac); + retval += encode_mac(base, idx, pkt->dstMac); + if(0 != pkt->sock.family) { + retval += encode_sock(base, idx, &(pkt->sock)); + } + retval += encode_uint8(base, idx, pkt->compression); + retval += encode_uint8(base, idx, pkt->transform); return retval; } -int decode_PACKET( n2n_PACKET_t * pkt, +int decode_PACKET (n2n_PACKET_t * pkt, const n2n_common_t * cmn, /* info on how to interpret it */ const uint8_t * base, size_t * rem, - size_t * idx ) -{ - size_t retval=0; - memset( pkt, 0, sizeof(n2n_PACKET_t) ); - retval += decode_mac( pkt->srcMac, base, rem, idx ); - retval += decode_mac( pkt->dstMac, base, rem, idx ); - - if ( cmn->flags & N2N_FLAGS_SOCKET ) - { - retval += decode_sock( &(pkt->sock), base, rem, idx ); - } + size_t * idx) { + + size_t retval = 0; + memset(pkt, 0, sizeof(n2n_PACKET_t)); - retval += decode_uint8( &(pkt->compression), base, rem, idx ); - retval += decode_uint8( &(pkt->transform), base, rem, idx ); + retval += decode_mac(pkt->srcMac, base, rem, idx); + retval += decode_mac(pkt->dstMac, base, rem, idx); + + if(cmn->flags & N2N_FLAGS_SOCKET) { + retval += decode_sock(&(pkt->sock), base, rem, idx); + } + + retval += decode_uint8(&(pkt->compression), base, rem, idx); + retval += decode_uint8(&(pkt->transform), base, rem, idx); return retval; } -int encode_PEER_INFO(uint8_t *base, - size_t *idx, - const n2n_common_t *common, - const n2n_PEER_INFO_t *pkt) { +int encode_PEER_INFO (uint8_t *base, + size_t *idx, + const n2n_common_t *common, + const n2n_PEER_INFO_t *pkt) { + int retval = 0; + retval += encode_common(base, idx, common); retval += encode_uint16(base, idx, pkt->aflags); retval += encode_mac(base, idx, pkt->srcMac); @@ -588,13 +629,15 @@ int encode_PEER_INFO(uint8_t *base, } -int decode_PEER_INFO(n2n_PEER_INFO_t *pkt, - const n2n_common_t *cmn, /* info on how to interpret it */ - const uint8_t *base, - size_t *rem, - size_t *idx) { +int decode_PEER_INFO (n2n_PEER_INFO_t *pkt, + const n2n_common_t *cmn, /* info on how to interpret it */ + const uint8_t *base, + size_t *rem, + size_t *idx) { + size_t retval = 0; memset(pkt, 0, sizeof(n2n_PEER_INFO_t)); + retval += decode_uint16(&(pkt->aflags), base, rem, idx); retval += decode_mac(pkt->srcMac, base, rem, idx); retval += decode_mac(pkt->mac, base, rem, idx); @@ -605,29 +648,31 @@ int decode_PEER_INFO(n2n_PEER_INFO_t *pkt, } -int encode_QUERY_PEER( uint8_t * base, - size_t * idx, - const n2n_common_t * common, - const n2n_QUERY_PEER_t * pkt ) -{ - int retval=0; - retval += encode_common( base, idx, common ); - retval += encode_mac( base, idx, pkt->srcMac ); - retval += encode_mac( base, idx, pkt->targetMac ); +int encode_QUERY_PEER (uint8_t * base, + size_t * idx, + const n2n_common_t * common, + const n2n_QUERY_PEER_t * pkt) { + + int retval = 0; + + retval += encode_common(base, idx, common); + retval += encode_mac(base, idx, pkt->srcMac); + retval += encode_mac(base, idx, pkt->targetMac); return retval; } -int decode_QUERY_PEER( n2n_QUERY_PEER_t * pkt, - const n2n_common_t * cmn, /* info on how to interpret it */ - const uint8_t * base, - size_t * rem, - size_t * idx ) -{ - size_t retval=0; - memset( pkt, 0, sizeof(n2n_QUERY_PEER_t) ); - retval += decode_mac( pkt->srcMac, base, rem, idx ); - retval += decode_mac( pkt->targetMac, base, rem, idx ); +int decode_QUERY_PEER (n2n_QUERY_PEER_t * pkt, + const n2n_common_t * cmn, /* info on how to interpret it */ + const uint8_t * base, + size_t * rem, + size_t * idx) { + + size_t retval = 0; + memset(pkt, 0, sizeof(n2n_QUERY_PEER_t)); + + retval += decode_mac(pkt->srcMac, base, rem, idx); + retval += decode_mac(pkt->targetMac, base, rem, idx); return retval; }