Browse Source

Compilation fixes

pull/392/head
Luca Deri 4 years ago
parent
commit
25620fcbc7
  1. 158
      include/n2n_wire.h
  2. 4
      src/aes.c
  3. 2
      src/sn_utils.c
  4. 520
      src/wire.c

158
include/n2n_wire.h

@ -50,19 +50,19 @@ typedef char n2n_sock_str_t[N2N_SOCKBUF_SIZE]; /* tracing string buffer
#endif /* #if defined(WIN32) */ #endif /* #if defined(WIN32) */
typedef enum n2n_pc typedef enum n2n_pc
{ {
n2n_ping=0, /* Not used */ n2n_ping=0, /* Not used */
n2n_register=1, /* Register edge to edge */ n2n_register=1, /* Register edge to edge */
n2n_deregister=2, /* Deregister this edge */ n2n_deregister=2, /* Deregister this edge */
n2n_packet=3, /* PACKET data content */ n2n_packet=3, /* PACKET data content */
n2n_register_ack=4, /* ACK of a registration from edge to edge */ n2n_register_ack=4, /* ACK of a registration from edge to edge */
n2n_register_super=5, /* Register edge to supernode */ n2n_register_super=5, /* Register edge to supernode */
n2n_register_super_ack=6, /* ACK from supernode to edge */ n2n_register_super_ack=6, /* ACK from supernode to edge */
n2n_register_super_nak=7, /* NAK from supernode to edge - registration refused */ n2n_register_super_nak=7, /* NAK from supernode to edge - registration refused */
n2n_federation=8, /* Not used by edge */ n2n_federation=8, /* Not used by edge */
n2n_peer_info=9, /* Send info on a peer from sn to edge */ n2n_peer_info=9, /* Send info on a peer from sn to edge */
n2n_query_peer=10 /* ask supernode for info on a peer */ n2n_query_peer=10 /* ask supernode for info on a peer */
} n2n_pc_t; } n2n_pc_t;
#define N2N_FLAGS_OPTIONS 0x0080 #define N2N_FLAGS_OPTIONS 0x0080
#define N2N_FLAGS_SOCKET 0x0040 #define N2N_FLAGS_SOCKET 0x0040
@ -85,105 +85,105 @@ typedef enum n2n_pc
#define N2N_ENOSPACE -4 #define N2N_ENOSPACE -4
typedef struct n2n_ip_subnet { typedef struct n2n_ip_subnet {
uint32_t net_addr; /* Host order IP address. */ uint32_t net_addr; /* Host order IP address. */
uint8_t net_bitlen; /* Subnet prefix. */ uint8_t net_bitlen; /* Subnet prefix. */
} n2n_ip_subnet_t; } n2n_ip_subnet_t;
typedef struct n2n_sock typedef struct n2n_sock
{ {
uint8_t family; /* AF_INET or AF_INET6; or 0 if invalid */ uint8_t family; /* AF_INET or AF_INET6; or 0 if invalid */
uint16_t port; /* host order */ uint16_t port; /* host order */
union union
{ {
uint8_t v6[IPV6_SIZE]; /* byte sequence */ uint8_t v6[IPV6_SIZE]; /* byte sequence */
uint8_t v4[IPV4_SIZE]; /* byte sequence */ uint8_t v4[IPV4_SIZE]; /* byte sequence */
} addr; } addr;
} n2n_sock_t; } n2n_sock_t;
typedef struct n2n_auth typedef struct n2n_auth
{ {
uint16_t scheme; /* What kind of auth */ uint16_t scheme; /* What kind of auth */
uint16_t toksize; /* Size of auth token */ uint16_t toksize; /* Size of auth token */
uint8_t token[N2N_AUTH_TOKEN_SIZE]; /* Auth data interpreted based on scheme */ uint8_t token[N2N_AUTH_TOKEN_SIZE]; /* Auth data interpreted based on scheme */
} n2n_auth_t; } n2n_auth_t;
typedef struct n2n_common typedef struct n2n_common
{ {
/* NOTE: wire representation is different! */ /* NOTE: wire representation is different! */
/* int version; */ /* int version; */
uint8_t ttl; uint8_t ttl;
uint8_t pc; uint8_t pc;
uint16_t flags; uint16_t flags;
n2n_community_t community; n2n_community_t community;
} n2n_common_t; } n2n_common_t;
typedef struct n2n_REGISTER typedef struct n2n_REGISTER
{ {
n2n_cookie_t cookie; /**< Link REGISTER and REGISTER_ACK */ n2n_cookie_t cookie; /**< Link REGISTER and REGISTER_ACK */
n2n_mac_t srcMac; /**< MAC of registering party */ n2n_mac_t srcMac; /**< MAC of registering party */
n2n_mac_t dstMac; /**< MAC of target edge */ n2n_mac_t dstMac; /**< MAC of target edge */
n2n_sock_t sock; /**< REVISIT: unused? */ n2n_sock_t sock; /**< REVISIT: unused? */
n2n_ip_subnet_t dev_addr; /**< IP address of the tuntap adapter. */ n2n_ip_subnet_t dev_addr; /**< IP address of the tuntap adapter. */
} n2n_REGISTER_t; } n2n_REGISTER_t;
typedef struct n2n_REGISTER_ACK typedef struct n2n_REGISTER_ACK
{ {
n2n_cookie_t cookie; /**< Return cookie from REGISTER */ n2n_cookie_t cookie; /**< Return cookie from REGISTER */
n2n_mac_t srcMac; /**< MAC of acknowledging party (supernode or edge) */ n2n_mac_t srcMac; /**< MAC of acknowledging party (supernode or edge) */
n2n_mac_t dstMac; /**< Reflected MAC of registering edge from REGISTER */ n2n_mac_t dstMac; /**< Reflected MAC of registering edge from REGISTER */
n2n_sock_t sock; /**< Supernode's view of edge socket (IP Addr, port) */ n2n_sock_t sock; /**< Supernode's view of edge socket (IP Addr, port) */
} n2n_REGISTER_ACK_t; } n2n_REGISTER_ACK_t;
typedef struct n2n_PACKET typedef struct n2n_PACKET
{ {
n2n_mac_t srcMac; n2n_mac_t srcMac;
n2n_mac_t dstMac; n2n_mac_t dstMac;
n2n_sock_t sock; n2n_sock_t sock;
uint8_t transform; uint8_t transform;
uint8_t compression; uint8_t compression;
} n2n_PACKET_t; } n2n_PACKET_t;
/* Linked with n2n_register_super in n2n_pc_t. Only from edge to supernode. */ /* Linked with n2n_register_super in n2n_pc_t. Only from edge to supernode. */
typedef struct n2n_REGISTER_SUPER { typedef struct n2n_REGISTER_SUPER {
n2n_cookie_t cookie; /**< Link REGISTER_SUPER and REGISTER_SUPER_ACK */ n2n_cookie_t cookie; /**< Link REGISTER_SUPER and REGISTER_SUPER_ACK */
n2n_mac_t edgeMac; /**< MAC to register with edge sending socket */ n2n_mac_t edgeMac; /**< MAC to register with edge sending socket */
n2n_ip_subnet_t dev_addr; /**< IP address of the tuntap adapter. */ n2n_ip_subnet_t dev_addr; /**< IP address of the tuntap adapter. */
n2n_auth_t auth; /**< Authentication scheme and tokens */ n2n_auth_t auth; /**< Authentication scheme and tokens */
} n2n_REGISTER_SUPER_t; } n2n_REGISTER_SUPER_t;
/* Linked with n2n_register_super_ack in n2n_pc_t. Only from supernode to edge. */ /* Linked with n2n_register_super_ack in n2n_pc_t. Only from supernode to edge. */
typedef struct n2n_REGISTER_SUPER_ACK { typedef struct n2n_REGISTER_SUPER_ACK {
n2n_cookie_t cookie; /**< Return cookie from REGISTER_SUPER */ n2n_cookie_t cookie; /**< Return cookie from REGISTER_SUPER */
n2n_mac_t edgeMac; /**< MAC registered to edge sending socket */ n2n_mac_t edgeMac; /**< MAC registered to edge sending socket */
n2n_ip_subnet_t dev_addr; /**< Assign an IP address to the tuntap adapter of edge. */ n2n_ip_subnet_t dev_addr; /**< Assign an IP address to the tuntap adapter of edge. */
uint16_t lifetime; /**< How long the registration will live */ uint16_t lifetime; /**< How long the registration will live */
n2n_sock_t sock; /**< Sending sockets associated with edgeMac */ n2n_sock_t sock; /**< Sending sockets associated with edgeMac */
/** The packet format provides additional supernode definitions here. /** The packet format provides additional supernode definitions here.
* uint8_t count, then for each count there is one * uint8_t count, then for each count there is one
* n2n_sock_t. * n2n_sock_t.
*/ */
uint8_t num_sn; /**< Number of supernodes that were send uint8_t num_sn; /**< Number of supernodes that were send
* even if we cannot store them all. If * even if we cannot store them all. If
* non-zero then sn_bak is valid. */ * non-zero then sn_bak is valid. */
n2n_sock_t sn_bak; /**< Socket of the first backup supernode */ n2n_sock_t sn_bak; /**< Socket of the first backup supernode */
} n2n_REGISTER_SUPER_ACK_t; } n2n_REGISTER_SUPER_ACK_t;
/* Linked with n2n_register_super_ack in n2n_pc_t. Only from supernode to edge. */ /* Linked with n2n_register_super_ack in n2n_pc_t. Only from supernode to edge. */
typedef struct n2n_REGISTER_SUPER_NAK typedef struct n2n_REGISTER_SUPER_NAK
{ {
n2n_cookie_t cookie; /* Return cookie from REGISTER_SUPER */ n2n_cookie_t cookie; /* Return cookie from REGISTER_SUPER */
} n2n_REGISTER_SUPER_NAK_t; } n2n_REGISTER_SUPER_NAK_t;
typedef struct n2n_PEER_INFO { typedef struct n2n_PEER_INFO {
uint16_t aflags; uint16_t aflags;
n2n_mac_t mac; n2n_mac_t mac;
n2n_sock_t sock; n2n_sock_t sock;
} n2n_PEER_INFO_t; } n2n_PEER_INFO_t;
@ -329,25 +329,25 @@ int decode_PACKET( n2n_PACKET_t * pkt,
size_t * idx ); size_t * idx );
int encode_PEER_INFO( uint8_t * base, int encode_PEER_INFO( uint8_t * base,
size_t * idx, size_t * idx,
const n2n_common_t * common, const n2n_common_t * common,
const n2n_PEER_INFO_t * pkt ); const n2n_PEER_INFO_t * pkt );
int decode_PEER_INFO( 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 n2n_common_t * cmn, /* info on how to interpret it */
const uint8_t * base, const uint8_t * base,
size_t * rem, size_t * rem,
size_t * idx ); size_t * idx );
int encode_QUERY_PEER( uint8_t * base, int encode_QUERY_PEER( uint8_t * base,
size_t * idx, size_t * idx,
const n2n_common_t * common, const n2n_common_t * common,
const n2n_QUERY_PEER_t * pkt ); const n2n_QUERY_PEER_t * pkt );
int decode_QUERY_PEER( 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 n2n_common_t * cmn, /* info on how to interpret it */
const uint8_t * base, const uint8_t * base,
size_t * rem, size_t * rem,
size_t * idx ); size_t * idx );
#endif /* #if !defined( N2N_WIRE_H_ ) */ #endif /* #if !defined( N2N_WIRE_H_ ) */

4
src/aes.c

@ -84,6 +84,8 @@ int aes_cbc_encrypt (unsigned char *out, const unsigned char *in, size_t in_len,
tmp_iv, tmp_iv,
AES_ENCRYPT); AES_ENCRYPT);
#endif #endif
return(0);
} }
/* ****************************************************** */ /* ****************************************************** */
@ -141,6 +143,8 @@ int aes_ecb_decrypt (unsigned char *out, const unsigned char *in, aes_context_t
#else #else
AES_ecb_encrypt(in, out, &(ctx->dec_key), AES_DECRYPT); AES_ecb_encrypt(in, out, &(ctx->dec_key), AES_DECRYPT);
#endif #endif
return(0);
} }
/* ****************************************************** */ /* ****************************************************** */

2
src/sn_utils.c

@ -413,7 +413,7 @@ int assign_one_ip_subnet(n2n_sn_t *sss,
no_subnets += 1; no_subnets += 1;
// proposal for sub-network to choose // proposal for sub-network to choose
net_id = pearson_hash_32(comm->community, N2N_COMMUNITY_SIZE) % no_subnets; net_id = pearson_hash_32((const uint8_t *)comm->community, N2N_COMMUNITY_SIZE) % no_subnets;
net_id = sss->min_auto_ip_net.net_addr + (net_id << (32 - sss->min_auto_ip_net.net_bitlen)); net_id = sss->min_auto_ip_net.net_addr + (net_id << (32 - sss->min_auto_ip_net.net_bitlen));
// check for availability starting from net_id, then downwards, ... // check for availability starting from net_id, then downwards, ...

520
src/wire.c

@ -26,16 +26,16 @@
* example. * example.
*/ */
#include "n2n_wire.h" #include "n2n.h"
#include <string.h>
int encode_uint8( uint8_t * base, int encode_uint8( uint8_t * base,
size_t * idx, size_t * idx,
const uint8_t v ) const uint8_t v )
{ {
*(base + (*idx)) = (v & 0xff); *(base + (*idx)) = (v & 0xff);
++(*idx); ++(*idx);
return 1; return 1;
} }
int decode_uint8( uint8_t * out, int decode_uint8( uint8_t * out,
@ -43,22 +43,22 @@ int decode_uint8( uint8_t * out,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
if (*rem < 1 ) { return 0; } if (*rem < 1 ) { return 0; }
*out = ( base[*idx] & 0xff ); *out = ( base[*idx] & 0xff );
++(*idx); ++(*idx);
--(*rem); --(*rem);
return 1; return 1;
} }
int encode_uint16( uint8_t * base, int encode_uint16( uint8_t * base,
size_t * idx, size_t * idx,
const uint16_t v ) const uint16_t v )
{ {
*(base + (*idx)) = ( v >> 8) & 0xff; *(base + (*idx)) = ( v >> 8) & 0xff;
*(base + (1 + *idx)) = ( v & 0xff ); *(base + (1 + *idx)) = ( v & 0xff );
*idx += 2; *idx += 2;
return 2; return 2;
} }
int decode_uint16( uint16_t * out, int decode_uint16( uint16_t * out,
@ -66,25 +66,25 @@ int decode_uint16( uint16_t * out,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
if (*rem < 2 ) { return 0; } if (*rem < 2 ) { return 0; }
*out = ( base[*idx] & 0xff ) << 8; *out = ( base[*idx] & 0xff ) << 8;
*out |= ( base[1 + *idx] & 0xff ); *out |= ( base[1 + *idx] & 0xff );
*idx += 2; *idx += 2;
*rem -= 2; *rem -= 2;
return 2; return 2;
} }
int encode_uint32( uint8_t * base, int encode_uint32( uint8_t * base,
size_t * idx, size_t * idx,
const uint32_t v ) const uint32_t v )
{ {
*(base + (0 + *idx)) = ( v >> 24) & 0xff; *(base + (0 + *idx)) = ( v >> 24) & 0xff;
*(base + (1 + *idx)) = ( v >> 16) & 0xff; *(base + (1 + *idx)) = ( v >> 16) & 0xff;
*(base + (2 + *idx)) = ( v >> 8) & 0xff; *(base + (2 + *idx)) = ( v >> 8) & 0xff;
*(base + (3 + *idx)) = ( v & 0xff ); *(base + (3 + *idx)) = ( v & 0xff );
*idx += 4; *idx += 4;
return 4; return 4;
} }
int decode_uint32( uint32_t * out, int decode_uint32( uint32_t * out,
@ -92,24 +92,24 @@ int decode_uint32( uint32_t * out,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
if (*rem < 4 ) { return 0; } if (*rem < 4 ) { return 0; }
*out = ( base[0 + *idx] & 0xff ) << 24; *out = ( base[0 + *idx] & 0xff ) << 24;
*out |= ( base[1 + *idx] & 0xff ) << 16; *out |= ( base[1 + *idx] & 0xff ) << 16;
*out |= ( base[2 + *idx] & 0xff ) << 8; *out |= ( base[2 + *idx] & 0xff ) << 8;
*out |= ( base[3 + *idx] & 0xff ); *out |= ( base[3 + *idx] & 0xff );
*idx += 4; *idx += 4;
*rem -= 4; *rem -= 4;
return 4; return 4;
} }
int encode_uint64( uint8_t * base, int encode_uint64( uint8_t * base,
size_t * idx, size_t * idx,
const uint64_t v ) const uint64_t v )
{ {
*(uint64_t*)(base + *idx) = htobe64(v); *(uint64_t*)(base + *idx) = htobe64(v);
*idx += 8; *idx += 8;
return 8; return 8;
} }
int decode_uint64( uint64_t * out, int decode_uint64( uint64_t * out,
@ -117,12 +117,12 @@ int decode_uint64( uint64_t * out,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
if (*rem < 8 ) { return 0; } if (*rem < 8 ) { return 0; }
*out = be64toh(*(uint64_t*)base + *idx); *out = be64toh(*(uint64_t*)base + *idx);
*idx += 8; *idx += 8;
*rem -= 8; *rem -= 8;
return 8; return 8;
} }
int encode_buf( uint8_t * base, int encode_buf( uint8_t * base,
@ -130,9 +130,9 @@ int encode_buf( uint8_t * base,
const void * p, const void * p,
size_t s) size_t s)
{ {
memcpy( (base + (*idx)), p, s ); memcpy( (base + (*idx)), p, s );
*idx += s; *idx += s;
return s; return s;
} }
/* Copy from base to out of size bufsize */ /* Copy from base to out of size bufsize */
@ -142,12 +142,12 @@ int decode_buf( uint8_t * out,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
if (*rem < bufsize ) { return 0; } if (*rem < bufsize ) { return 0; }
memcpy( out, (base + *idx), bufsize ); memcpy( out, (base + *idx), bufsize );
*idx += bufsize; *idx += bufsize;
*rem -= bufsize; *rem -= bufsize;
return bufsize; return bufsize;
} }
@ -156,7 +156,7 @@ int encode_mac( uint8_t * base,
size_t * idx, size_t * idx,
const n2n_mac_t m ) const n2n_mac_t m )
{ {
return encode_buf( base, idx, m, N2N_MAC_SIZE ); 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 */
@ -164,7 +164,7 @@ int decode_mac( uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passin
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
return decode_buf( out, N2N_MAC_SIZE, base, rem, idx ); return decode_buf( out, N2N_MAC_SIZE, base, rem, idx );
} }
@ -173,17 +173,17 @@ int encode_common( uint8_t * base,
size_t * idx, size_t * idx,
const n2n_common_t * common ) const n2n_common_t * common )
{ {
uint16_t flags=0; uint16_t flags=0;
encode_uint8( base, idx, N2N_PKT_VERSION ); encode_uint8( base, idx, N2N_PKT_VERSION );
encode_uint8( base, idx, common->ttl ); encode_uint8( base, idx, common->ttl );
flags = common->pc & N2N_FLAGS_TYPE_MASK; flags = common->pc & N2N_FLAGS_TYPE_MASK;
flags |= common->flags & N2N_FLAGS_BITS_MASK; flags |= common->flags & N2N_FLAGS_BITS_MASK;
encode_uint16( base, idx, flags ); encode_uint16( base, idx, flags );
encode_buf( base, idx, common->community, N2N_COMMUNITY_SIZE ); encode_buf( base, idx, common->community, N2N_COMMUNITY_SIZE );
return -1; return -1;
} }
int decode_common( n2n_common_t * out, int decode_common( n2n_common_t * out,
@ -191,23 +191,23 @@ int decode_common( n2n_common_t * out,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
size_t idx0=*idx; size_t idx0=*idx;
uint8_t dummy=0; uint8_t dummy=0;
decode_uint8( &dummy, base, rem, idx ); decode_uint8( &dummy, base, rem, idx );
if ( N2N_PKT_VERSION != dummy ) if ( N2N_PKT_VERSION != dummy )
{ {
return -1; return -1;
} }
decode_uint8( &(out->ttl), base, rem, idx ); decode_uint8( &(out->ttl), base, rem, idx );
decode_uint16( &(out->flags), base, rem, idx ); decode_uint16( &(out->flags), base, rem, idx );
out->pc = ( out->flags & N2N_FLAGS_TYPE_MASK ); out->pc = ( out->flags & N2N_FLAGS_TYPE_MASK );
out->flags &= N2N_FLAGS_BITS_MASK; out->flags &= N2N_FLAGS_BITS_MASK;
decode_buf( out->community, N2N_COMMUNITY_SIZE, base, rem, idx ); decode_buf( out->community, N2N_COMMUNITY_SIZE, base, rem, idx );
return (*idx - idx0); return (*idx - idx0);
} }
@ -215,32 +215,32 @@ int encode_sock( uint8_t * base,
size_t * idx, size_t * idx,
const n2n_sock_t * sock ) const n2n_sock_t * sock )
{ {
int retval=0; int retval=0;
uint16_t f; uint16_t f;
switch (sock->family) switch (sock->family)
{ {
case AF_INET: case AF_INET:
{ {
f = 0; f = 0;
retval += encode_uint16(base,idx,f); retval += encode_uint16(base,idx,f);
retval += encode_uint16(base,idx,sock->port); retval += encode_uint16(base,idx,sock->port);
retval += encode_buf(base,idx,sock->addr.v4,IPV4_SIZE); retval += encode_buf(base,idx,sock->addr.v4,IPV4_SIZE);
break; break;
} }
case AF_INET6: case AF_INET6:
{ {
f = 0x8000; f = 0x8000;
retval += encode_uint16(base,idx,f); retval += encode_uint16(base,idx,f);
retval += encode_uint16(base,idx,sock->port); retval += encode_uint16(base,idx,sock->port);
retval += encode_buf(base,idx,sock->addr.v6,IPV6_SIZE); retval += encode_buf(base,idx,sock->addr.v6,IPV6_SIZE);
break; break;
} }
default: default:
retval=-1; retval=-1;
} }
return retval; return retval;
} }
@ -249,28 +249,28 @@ int decode_sock( n2n_sock_t * sock,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
size_t * idx0=idx; size_t * idx0=idx;
uint16_t f=0; uint16_t f=0;
decode_uint16( &f, base, rem, idx ); decode_uint16( &f, base, rem, idx );
if( f & 0x8000 ) if( f & 0x8000 )
{ {
/* IPv6 */ /* IPv6 */
sock->family = AF_INET6; sock->family = AF_INET6;
decode_uint16( &(sock->port), base, rem, idx ); decode_uint16( &(sock->port), base, rem, idx );
decode_buf( sock->addr.v6, IPV6_SIZE, base, rem, idx ); decode_buf( sock->addr.v6, IPV6_SIZE, base, rem, idx );
} }
else else
{ {
/* IPv4 */ /* IPv4 */
sock->family = AF_INET; sock->family = AF_INET;
decode_uint16( &(sock->port), base, rem, idx ); decode_uint16( &(sock->port), base, rem, idx );
memset( sock->addr.v6, 0, IPV6_SIZE ); /* so memcmp() works for equality. */ memset( sock->addr.v6, 0, IPV6_SIZE ); /* so memcmp() works for equality. */
decode_buf( sock->addr.v4, IPV4_SIZE, base, rem, idx ); decode_buf( sock->addr.v4, IPV4_SIZE, base, rem, idx );
} }
return (idx-idx0); return (idx-idx0);
} }
@ -278,18 +278,18 @@ int encode_REGISTER(uint8_t *base,
size_t *idx, size_t *idx,
const n2n_common_t *common, const n2n_common_t *common,
const n2n_REGISTER_t *reg) { const n2n_REGISTER_t *reg) {
int retval = 0; int retval = 0;
retval += encode_common(base, idx, common); retval += encode_common(base, idx, common);
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE); retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
retval += encode_mac(base, idx, reg->srcMac); retval += encode_mac(base, idx, reg->srcMac);
retval += encode_mac(base, idx, reg->dstMac); 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_sock(base, idx, &(reg->sock));
} }
retval += encode_uint32(base, idx, reg->dev_addr.net_addr); retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen); retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
return retval; return retval;
} }
@ -298,19 +298,19 @@ int decode_REGISTER(n2n_REGISTER_t *reg,
const uint8_t *base, const uint8_t *base,
size_t *rem, size_t *rem,
size_t *idx) { size_t *idx) {
size_t retval = 0; size_t retval = 0;
memset(reg, 0, sizeof(n2n_REGISTER_t)); memset(reg, 0, sizeof(n2n_REGISTER_t));
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
retval += decode_mac(reg->srcMac, base, rem, idx); retval += decode_mac(reg->srcMac, base, rem, idx);
retval += decode_mac(reg->dstMac, 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_sock(&(reg->sock), base, rem, idx);
} }
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx); retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx); retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
return retval; return retval;
} }
@ -318,16 +318,16 @@ int encode_REGISTER_SUPER(uint8_t *base,
size_t *idx, size_t *idx,
const n2n_common_t *common, const n2n_common_t *common,
const n2n_REGISTER_SUPER_t *reg) { const n2n_REGISTER_SUPER_t *reg) {
int retval = 0; int retval = 0;
retval += encode_common(base, idx, common); retval += encode_common(base, idx, common);
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE); retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
retval += encode_mac(base, idx, reg->edgeMac); retval += encode_mac(base, idx, reg->edgeMac);
retval += encode_uint32(base, idx, reg->dev_addr.net_addr); retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen); retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
retval += encode_uint16(base, idx, 0); /* NULL auth scheme */ retval += encode_uint16(base, idx, 0); /* NULL auth scheme */
retval += encode_uint16(base, idx, 0); /* No auth data */ retval += encode_uint16(base, idx, 0); /* No auth data */
return retval; return retval;
} }
@ -336,16 +336,16 @@ int decode_REGISTER_SUPER(n2n_REGISTER_SUPER_t *reg,
const uint8_t *base, const uint8_t *base,
size_t *rem, size_t *rem,
size_t *idx) { size_t *idx) {
size_t retval = 0; size_t retval = 0;
memset(reg, 0, sizeof(n2n_REGISTER_SUPER_t)); memset(reg, 0, sizeof(n2n_REGISTER_SUPER_t));
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
retval += decode_mac(reg->edgeMac, base, rem, idx); retval += decode_mac(reg->edgeMac, base, rem, idx);
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx); retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx); retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
retval += decode_uint16(&(reg->auth.scheme), base, rem, idx); retval += decode_uint16(&(reg->auth.scheme), base, rem, idx);
retval += decode_uint16(&(reg->auth.toksize), base, rem, idx); retval += decode_uint16(&(reg->auth.toksize), base, rem, idx);
retval += decode_buf(reg->auth.token, reg->auth.toksize, base, rem, idx); retval += decode_buf(reg->auth.token, reg->auth.toksize, base, rem, idx);
return retval; return retval;
} }
@ -353,20 +353,20 @@ int encode_REGISTER_ACK(uint8_t *base,
size_t *idx, size_t *idx,
const n2n_common_t *common, const n2n_common_t *common,
const n2n_REGISTER_ACK_t *reg) { const n2n_REGISTER_ACK_t *reg) {
int retval = 0; int retval = 0;
retval += encode_common(base, idx, common); retval += encode_common(base, idx, common);
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE); retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
retval += encode_mac(base, idx, reg->dstMac); retval += encode_mac(base, idx, reg->dstMac);
retval += encode_mac(base, idx, reg->srcMac); retval += encode_mac(base, idx, reg->srcMac);
/* The socket in REGISTER_ACK is the socket from which the REGISTER /* 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 * arrived. This is sent back to the sender so it knows what its public
* socket is. */ * socket is. */
if (0 != reg->sock.family) { if (0 != reg->sock.family) {
retval += encode_sock(base, idx, &(reg->sock)); retval += encode_sock(base, idx, &(reg->sock));
} }
return retval; return retval;
} }
@ -375,20 +375,20 @@ int decode_REGISTER_ACK(n2n_REGISTER_ACK_t *reg,
const uint8_t *base, const uint8_t *base,
size_t *rem, size_t *rem,
size_t *idx) { size_t *idx) {
size_t retval = 0; size_t retval = 0;
memset(reg, 0, sizeof(n2n_REGISTER_ACK_t)); memset(reg, 0, sizeof(n2n_REGISTER_ACK_t));
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
retval += decode_mac(reg->dstMac, base, rem, idx); retval += decode_mac(reg->dstMac, base, rem, idx);
retval += decode_mac(reg->srcMac, base, rem, idx); retval += decode_mac(reg->srcMac, base, rem, idx);
/* The socket in REGISTER_ACK is the socket from which the REGISTER /* 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 * arrived. This is sent back to the sender so it knows what its public
* socket is. */ * socket is. */
if (cmn->flags & N2N_FLAGS_SOCKET) { if (cmn->flags & N2N_FLAGS_SOCKET) {
retval += decode_sock(&(reg->sock), base, rem, idx); retval += decode_sock(&(reg->sock), base, rem, idx);
} }
return retval; return retval;
} }
@ -396,21 +396,21 @@ int encode_REGISTER_SUPER_ACK(uint8_t *base,
size_t *idx, size_t *idx,
const n2n_common_t *common, const n2n_common_t *common,
const n2n_REGISTER_SUPER_ACK_t *reg) { const n2n_REGISTER_SUPER_ACK_t *reg) {
int retval = 0; int retval = 0;
retval += encode_common(base, idx, common); retval += encode_common(base, idx, common);
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE); retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
retval += encode_mac(base, idx, reg->edgeMac); retval += encode_mac(base, idx, reg->edgeMac);
retval += encode_uint32(base, idx, reg->dev_addr.net_addr); retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen); retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
retval += encode_uint16(base, idx, reg->lifetime); retval += encode_uint16(base, idx, reg->lifetime);
retval += encode_sock(base, idx, &(reg->sock)); retval += encode_sock(base, idx, &(reg->sock));
retval += encode_uint8(base, idx, reg->num_sn); retval += encode_uint8(base, idx, reg->num_sn);
if (reg->num_sn > 0) { if (reg->num_sn > 0) {
/* We only support 0 or 1 at this stage */ /* We only support 0 or 1 at this stage */
retval += encode_sock(base, idx, &(reg->sn_bak)); retval += encode_sock(base, idx, &(reg->sn_bak));
} }
return retval; return retval;
} }
@ -419,26 +419,26 @@ int decode_REGISTER_SUPER_ACK(n2n_REGISTER_SUPER_ACK_t *reg,
const uint8_t *base, const uint8_t *base,
size_t *rem, size_t *rem,
size_t *idx) { size_t *idx) {
size_t retval = 0; size_t retval = 0;
memset(reg, 0, sizeof(n2n_REGISTER_SUPER_ACK_t)); memset(reg, 0, sizeof(n2n_REGISTER_SUPER_ACK_t));
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx); retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
retval += decode_mac(reg->edgeMac, base, rem, idx); retval += decode_mac(reg->edgeMac, base, rem, idx);
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx); retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx); retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
retval += decode_uint16(&(reg->lifetime), base, rem, idx); retval += decode_uint16(&(reg->lifetime), base, rem, idx);
/* Socket is mandatory in this message type */ /* Socket is mandatory in this message type */
retval += decode_sock(&(reg->sock), base, rem, idx); retval += decode_sock(&(reg->sock), base, rem, idx);
/* Following the edge socket are an array of backup supernodes. */ /* Following the edge socket are an array of backup supernodes. */
retval += decode_uint8(&(reg->num_sn), base, rem, idx); retval += decode_uint8(&(reg->num_sn), base, rem, idx);
if (reg->num_sn > 0) { if (reg->num_sn > 0) {
/* We only support 0 or 1 at this stage */ /* We only support 0 or 1 at this stage */
retval += decode_sock(&(reg->sn_bak), base, rem, idx); retval += decode_sock(&(reg->sn_bak), base, rem, idx);
} }
return retval; return retval;
} }
@ -446,21 +446,21 @@ int fill_sockaddr( struct sockaddr * addr,
size_t addrlen, size_t addrlen,
const n2n_sock_t * sock ) const n2n_sock_t * sock )
{ {
int retval=-1; int retval=-1;
if ( AF_INET == sock->family ) if ( AF_INET == sock->family )
{ {
if ( addrlen >= sizeof(struct sockaddr_in) ) if ( addrlen >= sizeof(struct sockaddr_in) )
{ {
struct sockaddr_in * si = (struct sockaddr_in *)addr; struct sockaddr_in * si = (struct sockaddr_in *)addr;
si->sin_family = sock->family; si->sin_family = sock->family;
si->sin_port = htons( sock->port ); si->sin_port = htons( sock->port );
memcpy( &(si->sin_addr.s_addr), sock->addr.v4, IPV4_SIZE ); memcpy( &(si->sin_addr.s_addr), sock->addr.v4, IPV4_SIZE );
retval=0; retval=0;
} }
} }
return retval; return retval;
} }
@ -469,18 +469,18 @@ int encode_PACKET( uint8_t * base,
const n2n_common_t * common, const n2n_common_t * common,
const n2n_PACKET_t * pkt ) const n2n_PACKET_t * pkt )
{ {
int retval=0; int retval=0;
retval += encode_common( base, idx, common ); retval += encode_common( base, idx, common );
retval += encode_mac( base, idx, pkt->srcMac ); retval += encode_mac( base, idx, pkt->srcMac );
retval += encode_mac( base, idx, pkt->dstMac ); retval += encode_mac( base, idx, pkt->dstMac );
if ( 0 != pkt->sock.family ) if ( 0 != pkt->sock.family )
{ {
retval += encode_sock( base, idx, &(pkt->sock) ); retval += encode_sock( base, idx, &(pkt->sock) );
} }
retval += encode_uint8( base, idx, pkt->compression ); retval += encode_uint8( base, idx, pkt->compression );
retval += encode_uint8( base, idx, pkt->transform ); retval += encode_uint8( base, idx, pkt->transform );
return retval; return retval;
} }
@ -490,20 +490,20 @@ int decode_PACKET( n2n_PACKET_t * pkt,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
size_t retval=0; size_t retval=0;
memset( pkt, 0, sizeof(n2n_PACKET_t) ); memset( pkt, 0, sizeof(n2n_PACKET_t) );
retval += decode_mac( pkt->srcMac, base, rem, idx ); retval += decode_mac( pkt->srcMac, base, rem, idx );
retval += decode_mac( pkt->dstMac, base, rem, idx ); retval += decode_mac( pkt->dstMac, base, rem, idx );
if ( cmn->flags & N2N_FLAGS_SOCKET ) if ( cmn->flags & N2N_FLAGS_SOCKET )
{ {
retval += decode_sock( &(pkt->sock), base, rem, idx ); retval += decode_sock( &(pkt->sock), base, rem, idx );
} }
retval += decode_uint8( &(pkt->compression), base, rem, idx ); retval += decode_uint8( &(pkt->compression), base, rem, idx );
retval += decode_uint8( &(pkt->transform), base, rem, idx ); retval += decode_uint8( &(pkt->transform), base, rem, idx );
return retval; return retval;
} }
@ -511,13 +511,13 @@ int encode_PEER_INFO(uint8_t *base,
size_t *idx, size_t *idx,
const n2n_common_t *common, const n2n_common_t *common,
const n2n_PEER_INFO_t *pkt) { const n2n_PEER_INFO_t *pkt) {
int retval = 0; int retval = 0;
retval += encode_common(base, idx, common); retval += encode_common(base, idx, common);
retval += encode_uint16(base, idx, pkt->aflags); retval += encode_uint16(base, idx, pkt->aflags);
retval += encode_mac(base, idx, pkt->mac); retval += encode_mac(base, idx, pkt->mac);
retval += encode_sock(base, idx, &pkt->sock); retval += encode_sock(base, idx, &pkt->sock);
return retval; return retval;
} }
@ -526,39 +526,39 @@ int decode_PEER_INFO(n2n_PEER_INFO_t *pkt,
const uint8_t *base, const uint8_t *base,
size_t *rem, size_t *rem,
size_t *idx) { size_t *idx) {
size_t retval = 0; size_t retval = 0;
memset(pkt, 0, sizeof(n2n_PEER_INFO_t)); memset(pkt, 0, sizeof(n2n_PEER_INFO_t));
retval += decode_uint16(&(pkt->aflags), base, rem, idx); retval += decode_uint16(&(pkt->aflags), base, rem, idx);
retval += decode_mac(pkt->mac, base, rem, idx); retval += decode_mac(pkt->mac, base, rem, idx);
retval += decode_sock(&pkt->sock, base, rem, idx); retval += decode_sock(&pkt->sock, base, rem, idx);
return retval; return retval;
} }
int encode_QUERY_PEER( uint8_t * base, int encode_QUERY_PEER( uint8_t * base,
size_t * idx, size_t * idx,
const n2n_common_t * common, const n2n_common_t * common,
const n2n_QUERY_PEER_t * pkt ) const n2n_QUERY_PEER_t * pkt )
{ {
int retval=0; int retval=0;
retval += encode_common( base, idx, common ); retval += encode_common( base, idx, common );
retval += encode_mac( base, idx, pkt->srcMac ); retval += encode_mac( base, idx, pkt->srcMac );
retval += encode_mac( base, idx, pkt->targetMac ); retval += encode_mac( base, idx, pkt->targetMac );
return retval; return retval;
} }
int decode_QUERY_PEER( 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 n2n_common_t * cmn, /* info on how to interpret it */
const uint8_t * base, const uint8_t * base,
size_t * rem, size_t * rem,
size_t * idx ) size_t * idx )
{ {
size_t retval=0; size_t retval=0;
memset( pkt, 0, sizeof(n2n_QUERY_PEER_t) ); memset( pkt, 0, sizeof(n2n_QUERY_PEER_t) );
retval += decode_mac( pkt->srcMac, base, rem, idx ); retval += decode_mac( pkt->srcMac, base, rem, idx );
retval += decode_mac( pkt->targetMac, base, rem, idx ); retval += decode_mac( pkt->targetMac, base, rem, idx );
return retval; return retval;
} }

Loading…
Cancel
Save