Browse Source

added sock type field to data structure (#1031)

pull/1034/head
Logan oos Even 2 years ago
committed by GitHub
parent
commit
cf23457d3b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      include/n2n_typedefs.h
  2. 22
      src/wire.c

4
include/n2n_typedefs.h

@ -302,7 +302,9 @@ typedef struct n2n_ip_subnet {
} 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, AF_INET6 or AF_INVALID (-1, a custom #define);
mind that AF_UNSPEC (0) means auto IPv4 or IPv6 */
uint8_t type; /* for later use, usually SOCK_STREAM (1) or SOCK_DGRAM (2) */
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 */

22
src/wire.c

@ -255,6 +255,9 @@ int encode_sock (uint8_t * base,
switch(sock->family) { switch(sock->family) {
case AF_INET: { case AF_INET: {
f = 0; f = 0;
if(sock->type == SOCK_STREAM) {
f |= 0x4000;
}
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);
@ -263,6 +266,9 @@ int encode_sock (uint8_t * base,
case AF_INET6: { case AF_INET6: {
f = 0x8000; f = 0x8000;
if(sock->type == SOCK_STREAM) {
f |= 0x4000;
}
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);
@ -286,21 +292,27 @@ int decode_sock (n2n_sock_t * sock,
uint16_t f = 0; uint16_t f = 0;
decode_uint16(&f, base, rem, idx); decode_uint16(&f, base, rem, idx);
decode_uint16(&(sock->port), 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_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);
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);
} }
if(f & 0x4000) {
// TCP
sock->type = SOCK_STREAM;
} else {
// UDP
sock->type = SOCK_DGRAM;
}
return (idx - idx0); return (idx - idx0);
} }

Loading…
Cancel
Save