From 646da54915dded78e7f37e0285d859447b177357 Mon Sep 17 00:00:00 2001 From: Francesco Carli <62562180+fcarli3@users.noreply.github.com> Date: Sat, 19 Dec 2020 12:28:45 +0100 Subject: [PATCH] readability code clean-up (#545) --- include/n2n.h | 142 +++++++------- include/n2n_define.h | 36 ++-- include/n2n_typedefs.h | 251 +++++++++++-------------- src/n2n.c | 415 +++++++++++++++++++++++------------------ 4 files changed, 438 insertions(+), 406 deletions(-) diff --git a/include/n2n.h b/include/n2n.h index a16e6fd..f1ff9ef 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -164,94 +164,94 @@ /* ************************************** */ /* Transop Init Functions */ -int n2n_transop_null_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); -int n2n_transop_tf_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); -int n2n_transop_aes_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); -int n2n_transop_cc20_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); -int n2n_transop_speck_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); +int n2n_transop_null_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); +int n2n_transop_tf_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); +int n2n_transop_aes_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); +int n2n_transop_cc20_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); +int n2n_transop_speck_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); /* Log */ -void setTraceLevel(int level); -void setUseSyslog(int use_syslog); -void setTraceFile(FILE *f); -int getTraceLevel(); -void closeTraceFile(); -void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...); +void setTraceLevel (int level); +void setUseSyslog (int use_syslog); +void setTraceFile (FILE *f); +int getTraceLevel (); +void closeTraceFile (); +void traceEvent (int eventTraceLevel, char* file, int line, char * format, ...); /* Tuntap API */ -int tuntap_open(tuntap_dev *device, char *dev, const char *address_mode, char *device_ip, - char *device_mask, const char * device_mac, int mtu); -int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len); -int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len); -void tuntap_close(struct tuntap_dev *tuntap); -void tuntap_get_address(struct tuntap_dev *tuntap); +int tuntap_open (tuntap_dev *device, char *dev, const char *address_mode, char *device_ip, + char *device_mask, const char * device_mac, int mtu); +int tuntap_read (struct tuntap_dev *tuntap, unsigned char *buf, int len); +int tuntap_write (struct tuntap_dev *tuntap, unsigned char *buf, int len); +void tuntap_close (struct tuntap_dev *tuntap); +void tuntap_get_address (struct tuntap_dev *tuntap); /* Utils */ -char* intoa(uint32_t addr, char* buf, uint16_t buf_len); -uint32_t bitlen2mask(uint8_t bitlen); -uint8_t mask2bitlen(uint32_t mask); -char* macaddr_str(macstr_t buf, const n2n_mac_t mac); -int str2mac( uint8_t * outmac /* 6 bytes */, const char * s ); -int supernode2sock(n2n_sock_t * sn, const n2n_sn_name_t addrIn); -uint8_t is_multi_broadcast(const uint8_t * dest_mac); -char* msg_type2str(uint16_t msg_type); -void hexdump(const uint8_t * buf, size_t len); -void print_n2n_version(); -int is_empty_ip_address(const n2n_sock_t * sock); -void print_edge_stats(const n2n_edge_t *eee); +char* intoa (uint32_t addr, char* buf, uint16_t buf_len); +uint32_t bitlen2mask (uint8_t bitlen); +uint8_t mask2bitlen (uint32_t mask); +char* macaddr_str (macstr_t buf, const n2n_mac_t mac); +int str2mac (uint8_t * outmac /* 6 bytes */, const char * s); +int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn); +uint8_t is_multi_broadcast (const uint8_t * dest_mac); +char* msg_type2str (uint16_t msg_type); +void hexdump (const uint8_t * buf, size_t len); +void print_n2n_version (); +int is_empty_ip_address (const n2n_sock_t * sock); +void print_edge_stats (const n2n_edge_t *eee); /* Sockets */ -char* sock_to_cstr( n2n_sock_str_t out, - const n2n_sock_t * sock ); -char * ip_subnet_to_str(dec_ip_bit_str_t buf, const n2n_ip_subnet_t *ipaddr); -SOCKET open_socket(int local_port, int bind_any); -int sock_equal( const n2n_sock_t * a, - const n2n_sock_t * b ); +char* sock_to_cstr (n2n_sock_str_t out, + const n2n_sock_t * sock); +char * ip_subnet_to_str (dec_ip_bit_str_t buf, const n2n_ip_subnet_t *ipaddr); +SOCKET open_socket (int local_port, int bind_any); +int sock_equal (const n2n_sock_t * a, + const n2n_sock_t * b); /* Header encryption */ -uint64_t time_stamp(void); +uint64_t time_stamp (void); uint64_t initial_time_stamp (void); int time_stamp_verify_and_update (uint64_t stamp, uint64_t * previous_stamp, int allow_jitter); /* Operations on peer_info lists. */ -size_t purge_peer_list( struct peer_info ** peer_list, - time_t purge_before ); -size_t clear_peer_list( struct peer_info ** peer_list ); -size_t purge_expired_registrations( struct peer_info ** peer_list, time_t* p_last_purge, int timeout ); +size_t purge_peer_list (struct peer_info ** peer_list, + time_t purge_before); +size_t clear_peer_list (struct peer_info ** peer_list); +size_t purge_expired_registrations (struct peer_info ** peer_list, time_t* p_last_purge, int timeout); /* Edge conf */ -void edge_init_conf_defaults(n2n_edge_conf_t *conf); -int edge_verify_conf(const n2n_edge_conf_t *conf); -int edge_conf_add_supernode(n2n_edge_conf_t *conf, const char *ip_and_port); -const n2n_edge_conf_t* edge_get_conf(const n2n_edge_t *eee); -void edge_term_conf(n2n_edge_conf_t *conf); +void edge_init_conf_defaults (n2n_edge_conf_t *conf); +int edge_verify_conf (const n2n_edge_conf_t *conf); +int edge_conf_add_supernode (n2n_edge_conf_t *conf, const char *ip_and_port); +const n2n_edge_conf_t* edge_get_conf (const n2n_edge_t *eee); +void edge_term_conf (n2n_edge_conf_t *conf); /* Public functions */ -n2n_edge_t* edge_init(const n2n_edge_conf_t *conf, int *rv); -void update_supernode_reg(n2n_edge_t * eee, time_t nowTime); -void readFromIPSocket(n2n_edge_t * eee, int in_sock); -void edge_term(n2n_edge_t *eee); -void edge_set_callbacks(n2n_edge_t *eee, const n2n_edge_callbacks_t *callbacks); -void edge_set_userdata(n2n_edge_t *eee, void *user_data); -void* edge_get_userdata(n2n_edge_t *eee); -void edge_send_packet2net(n2n_edge_t *eee, uint8_t *tap_pkt, size_t len); -void edge_read_from_tap(n2n_edge_t *eee); -int edge_get_n2n_socket(n2n_edge_t *eee); -int edge_get_management_socket(n2n_edge_t *eee); -int run_edge_loop(n2n_edge_t *eee, int *keep_running); -int quick_edge_init(char *device_name, char *community_name, - char *encrypt_key, char *device_mac, - char *local_ip_address, - char *supernode_ip_address_port, - int *keep_on_running); -int comm_init(struct sn_community *comm, char *cmn); -int sn_init(n2n_sn_t *sss); -void sn_term(n2n_sn_t *sss); -int supernode2sock(n2n_sock_t * sn, const n2n_sn_name_t addrIn); -struct peer_info* add_sn_to_list_by_mac_or_sock(struct peer_info **sn_list, n2n_sock_t *sock, n2n_mac_t *mac, int *skip_add); -int run_sn_loop(n2n_sn_t *sss, int *keep_running); -int assign_one_ip_subnet(n2n_sn_t *sss, struct sn_community *comm); -const char* compression_str(uint8_t cmpr); -const char* transop_str(enum n2n_transform tr); +n2n_edge_t* edge_init (const n2n_edge_conf_t *conf, int *rv); +void update_supernode_reg (n2n_edge_t * eee, time_t nowTime); +void readFromIPSocket (n2n_edge_t * eee, int in_sock); +void edge_term (n2n_edge_t *eee); +void edge_set_callbacks (n2n_edge_t *eee, const n2n_edge_callbacks_t *callbacks); +void edge_set_userdata (n2n_edge_t *eee, void *user_data); +void* edge_get_userdata (n2n_edge_t *eee); +void edge_send_packet2net (n2n_edge_t *eee, uint8_t *tap_pkt, size_t len); +void edge_read_from_tap (n2n_edge_t *eee); +int edge_get_n2n_socket (n2n_edge_t *eee); +int edge_get_management_socket (n2n_edge_t *eee); +int run_edge_loop (n2n_edge_t *eee, int *keep_running); +int quick_edge_init (char *device_name, char *community_name, + char *encrypt_key, char *device_mac, + char *local_ip_address, + char *supernode_ip_address_port, + int *keep_on_running); +int comm_init (struct sn_community *comm, char *cmn); +int sn_init (n2n_sn_t *sss); +void sn_term (n2n_sn_t *sss); +int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn); +struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n_sock_t *sock, n2n_mac_t *mac, int *skip_add); +int run_sn_loop (n2n_sn_t *sss, int *keep_running); +int assign_one_ip_subnet (n2n_sn_t *sss, struct sn_community *comm); +const char* compression_str (uint8_t cmpr); +const char* transop_str (enum n2n_transform tr); #endif /* _N2N_H_ */ diff --git a/include/n2n_define.h b/include/n2n_define.h index 930c0ab..89310ec 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -28,15 +28,15 @@ #define MSG_TYPE_FEDERATION 9 #define MSG_TYPE_PEER_INFO 10 #define MSG_TYPE_QUERY_PEER 11 -#define MSG_TYPE_MAX_TYPE 11 +#define MSG_TYPE_MAX_TYPE 11 /* Max available space to add supernodes' informations (sockets and MACs) in REGISTER_SUPER_ACK * Field sizes of REGISTER_SUPER_ACK as used in encode/decode fucntions in src/wire.c * REVISIT: replace 255 by DEFAULT_MTU as soon as header encryption allows for longer packets to be encrypted. */ -#define REG_SUPER_ACK_PAYLOAD_SPACE (255-(sizeof(n2n_common_t)+sizeof(n2n_REGISTER_SUPER_ACK_t))) +#define REG_SUPER_ACK_PAYLOAD_SPACE (255 - (sizeof(n2n_common_t) + sizeof(n2n_REGISTER_SUPER_ACK_t))) /* Space needed to store socket and MAC address of a supernode */ -#define REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE (sizeof(n2n_REGISTER_SUPER_ACK_payload_t)) +#define REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE (sizeof(n2n_REGISTER_SUPER_ACK_payload_t)) #define PURGE_REGISTRATION_FREQUENCY 30 #define RE_REG_AND_PURGE_FREQUENCY 10 @@ -51,7 +51,7 @@ * values should be at least 3*SOCKET_TIMEOUT_INTERVAL_SECS apart. */ #define LAST_SEEN_SN_ACTIVE 20 /* sec, indicates supernodes that are proven to be active */ #define LAST_SEEN_SN_INACTIVE 90 /* sec, indicates supernodes that are proven to be inactive: they will be purged */ -#define LAST_SEEN_SN_NEW (LAST_SEEN_SN_INACTIVE - LAST_SEEN_SN_ACTIVE)/2 /* sec, indicates supernodes with unsure status, must be tested to check if they are active */ +#define LAST_SEEN_SN_NEW (LAST_SEEN_SN_INACTIVE - LAST_SEEN_SN_ACTIVE) / 2 /* sec, indicates supernodes with unsure status, must be tested to check if they are active */ #define IFACE_UPDATE_INTERVAL (30) /* sec. How long it usually takes to get an IP lease. */ @@ -66,8 +66,8 @@ #define UDP_SIZE 8 /* parameters for replay protection */ -#define TIME_STAMP_FRAME 0x0000001000000000LL /* clocks of different computers are allowed +/- 16 seconds to be off */ -#define TIME_STAMP_JITTER 0x0000000027100000LL /* we allow a packet to arrive 160 ms (== 0x27100 us) before another +#define TIME_STAMP_FRAME 0x0000001000000000LL /* clocks of different computers are allowed +/- 16 seconds to be off */ +#define TIME_STAMP_JITTER 0x0000000027100000LL /* we allow a packet to arrive 160 ms (== 0x27100 us) before another * set to 0x0000000000000000LL if increasing (or equal) time stamps allowed only */ #define TIME_STAMP_ALLOW_JITTER 1 /* constant for allowing or... */ #define TIME_STAMP_NO_JITTER 0 /* not allowing jitter to be considered */ @@ -76,19 +76,19 @@ /* Compression is disabled by default for outgoing packets if no cli * option is given. All edges are built with decompression support so * they are able to understand each other (this applies to lzo only). */ -#define N2N_COMPRESSION_ID_INVALID 0 -#define N2N_COMPRESSION_ID_NONE 1 /* default, see edge_init_conf_defaults(...) in edge_utils.c */ -#define N2N_COMPRESSION_ID_LZO 2 /* set if '-z1' or '-z' cli option is present, see setOption(...) in edge.c */ -#define N2N_COMPRESSION_ID_ZSTD 3 /* set if '-z2' cli option is present, available only if compiled with zstd lib */ -#define ZSTD_COMPRESSION_LEVEL 7 /* 1 (faster) ... 22 (more compression) */ +#define N2N_COMPRESSION_ID_INVALID 0 +#define N2N_COMPRESSION_ID_NONE 1 /* default, see edge_init_conf_defaults(...) in edge_utils.c */ +#define N2N_COMPRESSION_ID_LZO 2 /* set if '-z1' or '-z' cli option is present, see setOption(...) in edge.c */ +#define N2N_COMPRESSION_ID_ZSTD 3 /* set if '-z2' cli option is present, available only if compiled with zstd lib */ +#define ZSTD_COMPRESSION_LEVEL 7 /* 1 (faster) ... 22 (more compression) */ /* Federation name and indicators */ #define FEDERATION_NAME "*Federation" enum federation{IS_NO_FEDERATION = 0,IS_FEDERATION = 1}; /* (un)purgeable community indicator (supernode) */ -#define COMMUNITY_UNPURGEABLE 0 -#define COMMUNITY_PURGEABLE 1 +#define COMMUNITY_UNPURGEABLE 0 +#define COMMUNITY_PURGEABLE 1 /* (un)purgeable supernode indicator */ enum sn_purge{SN_PURGEABLE = 0, SN_UNPURGEABLE = 1}; @@ -100,9 +100,9 @@ enum sn_purge{SN_PURGEABLE = 0, SN_UNPURGEABLE = 1}; #define DEFAULT_MTU 1290 -#define HASH_ADD_PEER(head,add) \ +#define HASH_ADD_PEER(head,add) \ HASH_ADD(hh,head,mac_addr,sizeof(n2n_mac_t),add) -#define HASH_FIND_PEER(head,mac,out) \ +#define HASH_FIND_PEER(head,mac,out) \ HASH_FIND(hh,head,mac,sizeof(n2n_mac_t),out) #define N2N_EDGE_SN_HOST_SIZE 48 #define N2N_EDGE_NUM_SUPERNODES 2 @@ -128,8 +128,8 @@ enum skip_add{SN_ADD = 0, SN_ADD_SKIP = 1, SN_ADD_ADDED = 2}; /* The way TUNTAP allocated IP. */ #define TUNTAP_IP_MODE_SN_ASSIGN 0 -#define TUNTAP_IP_MODE_STATIC 1 -#define TUNTAP_IP_MODE_DHCP 2 +#define TUNTAP_IP_MODE_STATIC 1 +#define TUNTAP_IP_MODE_DHCP 2 /* Default network segment of the auto ip address service provided by sn. */ #define N2N_SN_MIN_AUTO_IP_NET_DEFAULT "10.128.0.0" @@ -161,7 +161,7 @@ enum skip_add{SN_ADD = 0, SN_ADD_SKIP = 1, SN_ADD_ADDED = 2}; #define N2N_IFNAMSIZ 16 /* 15 chars * NULL */ #endif -#define SN_SELECTION_CRITERION_DATA_TYPE uint32_t +#define SN_SELECTION_CRITERION_DATA_TYPE uint32_t #define SN_SELECTION_CRITERION_BUF_SIZE 14 #define N2N_TRANSFORM_ID_USER_START 64 diff --git a/include/n2n_typedefs.h b/include/n2n_typedefs.h index 23d868a..815226d 100644 --- a/include/n2n_typedefs.h +++ b/include/n2n_typedefs.h @@ -119,11 +119,10 @@ typedef unsigned long in_addr_t; #define ETH_ADDR_LEN 6 -struct ether_hdr -{ - uint8_t dhost[ETH_ADDR_LEN]; - uint8_t shost[ETH_ADDR_LEN]; - uint16_t type; /* higher layer protocol encapsulated */ +struct ether_hdr { + uint8_t dhost[ETH_ADDR_LEN]; + uint8_t shost[ETH_ADDR_LEN]; + uint16_t type; /* higher layer protocol encapsulated */ } PACK_STRUCT; typedef struct ether_hdr ether_hdr_t; @@ -148,30 +147,28 @@ struct n2n_iphdr { u_int32_t daddr; } PACK_STRUCT; -struct n2n_tcphdr -{ - u_int16_t source; - u_int16_t dest; - u_int32_t seq; - u_int32_t ack_seq; +struct n2n_tcphdr { + u_int16_t source; + u_int16_t dest; + u_int32_t seq; + u_int32_t ack_seq; #if defined(__LITTLE_ENDIAN__) - u_int16_t res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1; + u_int16_t res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1; #elif defined(__BIG_ENDIAN__) - u_int16_t doff:4, res1:4, cwr:1, ece:1, urg:1, ack:1, psh:1, rst:1, syn:1, fin:1; + u_int16_t doff:4, res1:4, cwr:1, ece:1, urg:1, ack:1, psh:1, rst:1, syn:1, fin:1; #else # error "Byte order must be defined" #endif - u_int16_t window; - u_int16_t check; - u_int16_t urg_ptr; + u_int16_t window; + u_int16_t check; + u_int16_t urg_ptr; } PACK_STRUCT; -struct n2n_udphdr -{ - u_int16_t source; - u_int16_t dest; - u_int16_t len; - u_int16_t check; +struct n2n_udphdr { + u_int16_t source; + u_int16_t dest; + u_int16_t len; + u_int16_t check; } PACK_STRUCT; #if defined(_MSC_VER) || defined(__MINGW32__) @@ -179,33 +176,31 @@ struct n2n_udphdr #endif -typedef struct port_range{ - uint16_t start_port; // range contain 'start_port' self - uint16_t end_port; // range contain 'end_port' self +typedef struct port_range { + uint16_t start_port; // range contain 'start_port' self + uint16_t end_port; // range contain 'end_port' self } port_range_t; -typedef struct filter_rule_key -{ - in_addr_t src_net_cidr; - uint8_t src_net_bit_len; - port_range_t src_port_range; - in_addr_t dst_net_cidr; - uint8_t dst_net_bit_len; - port_range_t dst_port_range; - uint8_t bool_tcp_configured; - uint8_t bool_udp_configured; - uint8_t bool_icmp_configured; +typedef struct filter_rule_key { + in_addr_t src_net_cidr; + uint8_t src_net_bit_len; + port_range_t src_port_range; + in_addr_t dst_net_cidr; + uint8_t dst_net_bit_len; + port_range_t dst_port_range; + uint8_t bool_tcp_configured; + uint8_t bool_udp_configured; + uint8_t bool_icmp_configured; } filter_rule_key_t; -typedef struct filter_rule -{ - filter_rule_key_t key; +typedef struct filter_rule { + filter_rule_key_t key; - uint8_t bool_accept_icmp; - uint8_t bool_accept_udp; - uint8_t bool_accept_tcp; + uint8_t bool_accept_icmp; + uint8_t bool_accept_udp; + uint8_t bool_accept_tcp; - UT_hash_handle hh; /* makes this structure hashable */ + UT_hash_handle hh; /* makes this structure hashable */ } filter_rule_t; @@ -239,21 +234,20 @@ typedef char dec_ip_bit_str_t[N2N_NETMASK_STR_SIZE + 4]; typedef struct speck_context_t he_context_t; typedef char n2n_sn_name_t[N2N_EDGE_SN_HOST_SIZE]; -typedef enum n2n_pc - { - n2n_ping=0, /* Not used */ - n2n_register=1, /* Register edge to edge */ - n2n_deregister=2, /* Deregister this edge */ - n2n_packet=3, /* PACKET data content */ - n2n_register_ack=4, /* ACK of a registration from edge to edge */ - n2n_register_super=5, /* Register edge to supernode */ - n2n_unregister_super=6, /* Deregister edge from supernode */ - n2n_register_super_ack=7, /* ACK from supernode to edge */ - n2n_register_super_nak=8, /* NAK from supernode to edge - registration refused */ - n2n_federation=9, /* Not used by edge */ - n2n_peer_info=10, /* Send info on a peer from sn to edge */ - n2n_query_peer=11 /* ask supernode for info on a peer */ - } n2n_pc_t; +typedef enum n2n_pc { + n2n_ping = 0, /* Not used */ + n2n_register = 1, /* Register edge to edge */ + n2n_deregister = 2, /* Deregister this edge */ + n2n_packet = 3, /* PACKET data content */ + n2n_register_ack = 4, /* ACK of a registration from edge to edge */ + n2n_register_super = 5, /* Register edge to supernode */ + n2n_unregister_super = 6, /* Deregister edge from supernode */ + n2n_register_super_ack = 7, /* ACK from supernode to edge */ + n2n_register_super_nak = 8, /* NAK from supernode to edge - registration refused */ + n2n_federation = 9, /* Not used by edge */ + n2n_peer_info = 10, /* Send info on a peer from sn to edge */ + n2n_query_peer = 11 /* ask supernode for info on a peer */ +} n2n_pc_t; #define N2N_FLAGS_OPTIONS 0x0080 #define N2N_FLAGS_SOCKET 0x0040 @@ -281,40 +275,34 @@ typedef struct n2n_ip_subnet { } n2n_ip_subnet_t; -typedef struct n2n_sock -{ +typedef struct n2n_sock { uint8_t family; /* AF_INET or AF_INET6; or 0 if invalid */ uint16_t port; /* host order */ - union - { + union { uint8_t v6[IPV6_SIZE]; /* byte sequence */ uint8_t v4[IPV4_SIZE]; /* byte sequence */ } addr; } n2n_sock_t; -typedef enum -{ - n2n_auth_none = 0, +typedef enum { + n2n_auth_none = 0, n2n_auth_simple_id = 1 } n2n_auth_scheme_t; -typedef enum -{ - update_edge_no_change = 0, +typedef enum { + update_edge_no_change = 0, update_edge_sock_change = 1, - update_edge_new_sn = 2, - update_edge_auth_fail = -1 + update_edge_new_sn = 2, + update_edge_auth_fail = -1 } update_edge_ret_value_t; -typedef struct n2n_auth -{ +typedef struct n2n_auth { uint16_t scheme; /* What kind of auth */ uint16_t toksize; /* Size of auth token */ uint8_t token[N2N_AUTH_TOKEN_SIZE]; /* Auth data interpreted based on scheme */ } n2n_auth_t; -typedef struct n2n_common -{ +typedef struct n2n_common { /* NOTE: wire representation is different! */ /* int version; */ @@ -324,8 +312,7 @@ typedef struct n2n_common n2n_community_t community; } n2n_common_t; -typedef struct n2n_REGISTER -{ +typedef struct n2n_REGISTER { n2n_cookie_t cookie; /**< Link REGISTER and REGISTER_ACK */ n2n_mac_t srcMac; /**< MAC of registering party */ n2n_mac_t dstMac; /**< MAC of target edge */ @@ -334,21 +321,19 @@ typedef struct n2n_REGISTER n2n_desc_t dev_desc; /**< Hint description correlated with the edge */ } n2n_REGISTER_t; -typedef struct n2n_REGISTER_ACK -{ +typedef struct n2n_REGISTER_ACK { n2n_cookie_t cookie; /**< Return cookie from REGISTER */ n2n_mac_t srcMac; /**< MAC of acknowledging party (supernode or edge) */ 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_REGISTER_ACK_t; -typedef struct n2n_PACKET -{ +typedef struct n2n_PACKET { n2n_mac_t srcMac; n2n_mac_t dstMac; n2n_sock_t sock; uint8_t transform; - uint8_t compression; + uint8_t compression; } n2n_PACKET_t; /* Linked with n2n_register_super in n2n_pc_t. Only from edge to supernode. */ @@ -375,14 +360,13 @@ typedef struct n2n_REGISTER_SUPER_ACK { * n2n_sock_t. */ uint8_t num_sn; /**< Number of supernodes that were send - * even if we cannot store them all. If - * non-zero then sn_bak is valid. */ + * even if we cannot store them all. If + * non-zero then sn_bak is valid. */ } n2n_REGISTER_SUPER_ACK_t; /* 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_mac_t srcMac; } n2n_REGISTER_SUPER_NAK_t; @@ -397,24 +381,22 @@ typedef struct n2n_REGISTER_SUPER_ACK_payload { /* Linked with n2n_unregister_super in n2n_pc_t. */ -typedef struct n2n_UNREGISTER_SUPER -{ +typedef struct n2n_UNREGISTER_SUPER { n2n_auth_t auth; n2n_mac_t srcMac; } n2n_UNREGISTER_SUPER_t; typedef struct n2n_PEER_INFO { - uint16_t aflags; - n2n_mac_t srcMac; - n2n_mac_t mac; - n2n_sock_t sock; + uint16_t aflags; + n2n_mac_t srcMac; + n2n_mac_t mac; + n2n_sock_t sock; SN_SELECTION_CRITERION_DATA_TYPE data; } n2n_PEER_INFO_t; -typedef struct n2n_QUERY_PEER -{ +typedef struct n2n_QUERY_PEER { n2n_mac_t srcMac; n2n_sock_t sock; n2n_mac_t targetMac; @@ -455,22 +437,22 @@ typedef struct n2n_edge n2n_edge_t; typedef enum { N2N_ACCEPT = 0, - N2N_DROP = 1 + N2N_DROP = 1 } n2n_verdict; /* *************************************************** */ typedef enum { - FPP_UNKNOWN=0, - FPP_ARP = 1, - FPP_TCP=2, - FPP_UDP=3, - FPP_ICMP=4, - FPP_IGMP=5 + FPP_UNKNOWN = 0, + FPP_ARP = 1, + FPP_TCP = 2, + FPP_UDP = 3, + FPP_ICMP = 4, + FPP_IGMP = 5 } filter_packet_proto; -typedef struct packet_address_proto_info{ +typedef struct packet_address_proto_info { in_addr_t src_ip; uint16_t src_port; in_addr_t dst_ip; @@ -478,13 +460,11 @@ typedef struct packet_address_proto_info{ filter_packet_proto proto; }packet_address_proto_info_t; -typedef struct filter_rule_pair_cache -{ +typedef struct filter_rule_pair_cache { packet_address_proto_info_t key; - uint8_t bool_allow_traffic; - - uint32_t active_count; + uint8_t bool_allow_traffic; + uint32_t active_count; UT_hash_handle hh; /* makes this structure hashable */ } filter_rule_pair_cache_t; @@ -492,10 +472,9 @@ typedef struct filter_rule_pair_cache struct network_traffic_filter; typedef struct network_traffic_filter network_traffic_filter_t; -struct network_traffic_filter -{ +struct network_traffic_filter { n2n_verdict (*filter_packet_from_peer)(network_traffic_filter_t* filter, n2n_edge_t *eee, - const n2n_sock_t *peer, uint8_t *payload, uint16_t payload_size); + const n2n_sock_t *peer, uint8_t *payload, uint16_t payload_size); n2n_verdict (*filter_packet_from_tap)(network_traffic_filter_t* filter, n2n_edge_t *eee, uint8_t *payload, uint16_t payload_size); @@ -549,24 +528,24 @@ typedef struct n2n_tuntap_priv_config { /* *************************************************** */ typedef enum n2n_transform { - N2N_TRANSFORM_ID_INVAL = 0, - N2N_TRANSFORM_ID_NULL = 1, - N2N_TRANSFORM_ID_TWOFISH = 2, - N2N_TRANSFORM_ID_AES = 3, + N2N_TRANSFORM_ID_INVAL = 0, + N2N_TRANSFORM_ID_NULL = 1, + N2N_TRANSFORM_ID_TWOFISH = 2, + N2N_TRANSFORM_ID_AES = 3, N2N_TRANSFORM_ID_CHACHA20 = 4, - N2N_TRANSFORM_ID_SPECK = 5, + N2N_TRANSFORM_ID_SPECK = 5, } n2n_transform_t; struct n2n_trans_op; /* Circular definition */ -typedef int (*n2n_transdeinit_f)( struct n2n_trans_op * arg ); -typedef void (*n2n_transtick_f)( struct n2n_trans_op * arg, time_t now ); -typedef int (*n2n_transform_f)( struct n2n_trans_op * arg, - uint8_t * outbuf, - size_t out_len, - const uint8_t * inbuf, - size_t in_len, - const n2n_mac_t peer_mac); +typedef int (*n2n_transdeinit_f)(struct n2n_trans_op * arg); +typedef void (*n2n_transtick_f)(struct n2n_trans_op * arg, time_t now); +typedef int (*n2n_transform_f)(struct n2n_trans_op * arg, + uint8_t * outbuf, + size_t out_len, + const uint8_t * inbuf, + size_t in_len, + const n2n_mac_t peer_mac); /** Holds the info associated with a data transform plugin. * * When a packet arrives the transform ID is extracted. This defines the code @@ -581,7 +560,7 @@ typedef struct n2n_trans_op { size_t rx_cnt; n2n_transdeinit_f deinit; /* destructor function */ - n2n_transtick_f tick; /* periodic maintenance */ + n2n_transtick_f tick; /* periodic maintenance */ n2n_transform_f fwd; /* encode a payload */ n2n_transform_f rev; /* decode a payload */ } n2n_trans_op_t; @@ -594,12 +573,12 @@ typedef struct n2n_edge_conf { n2n_route_t *routes; /**< Networks to route through n2n */ n2n_community_t community_name; /**< The community. 16 full octets. */ n2n_desc_t dev_desc; /**< The device description (hint) */ - uint8_t header_encryption; /**< Header encryption indicator. */ + uint8_t header_encryption; /**< Header encryption indicator. */ he_context_t *header_encryption_ctx; /**< Header encryption cipher context. */ he_context_t *header_iv_ctx; /**< Header IV ecnryption cipher context, REMOVE as soon as seperte fileds for checksum and replay protection available */ n2n_transform_t transop_id; /**< The transop to use. */ uint8_t compression; /**< Compress outgoing data packets before encryption */ - uint16_t num_routes; /**< Number of routes in routes */ + uint16_t num_routes; /**< Number of routes in routes */ uint8_t tuntap_ip_mode; /**< Interface IP address allocated mode, eg. DHCP. */ uint8_t allow_routing; /**< Accept packet no to interface address. */ uint8_t drop_multicast; /**< Multicast ethernet addresses. */ @@ -637,7 +616,7 @@ struct n2n_edge { n2n_trans_op_t transop; /**< The transop to use when encoding */ n2n_route_t *sn_route_to_clean; /**< Supernode route to clean */ n2n_edge_callbacks_t cb; /**< API callbacks */ - void *user_data; /**< Can hold user data */ + void *user_data; /**< Can hold user data */ uint64_t sn_last_valid_time_stamp;/**< last valid time stamp from supernode */ SN_SELECTION_CRITERION_DATA_TYPE sn_selection_criterion_common_data; @@ -671,8 +650,7 @@ struct n2n_edge { network_traffic_filter_t *network_traffic_filter; }; -typedef struct sn_stats -{ +typedef struct sn_stats { size_t errors; /* Number of errors encountered. */ size_t reg_super; /* Number of REGISTER_SUPER requests received. */ size_t reg_super_nak; /* Number of REGISTER_SUPER requests declined. */ @@ -682,16 +660,15 @@ typedef struct sn_stats time_t last_reg_super; /* Time when last REGISTER_SUPER was received. */ } sn_stats_t; -struct sn_community -{ - char community[N2N_COMMUNITY_SIZE]; +struct sn_community { + char community[N2N_COMMUNITY_SIZE]; uint8_t is_federation; /* if not-zero, then the current community is the federation of supernodes */ uint8_t purgeable; /* indicates purgeable community (fixed-name, predetermined (-c parameter) communties usually are unpurgeable) */ - uint8_t header_encryption; /* Header encryption indicator. */ + uint8_t header_encryption; /* Header encryption indicator. */ he_context_t *header_encryption_ctx; /* Header encryption cipher context. */ - he_context_t *header_iv_ctx; /* Header IV ecnryption cipher context, REMOVE as soon as seperate fields for checksum and replay protection available */ + he_context_t *header_iv_ctx; /* Header IV ecnryption cipher context, REMOVE as soon as seperate fields for checksum and replay protection available */ struct peer_info *edges; /* Link list of registered edges. */ - int64_t number_enc_packets; /* Number of encrypted packets handled so far, required for sorting from time to time */ + int64_t number_enc_packets; /* Number of encrypted packets handled so far, required for sorting from time to time */ n2n_ip_subnet_t auto_ip_net; /* Address range of auto ip address service. */ UT_hash_handle hh; /* makes this structure hashable */ @@ -700,15 +677,13 @@ struct sn_community /* Typedef'd pointer to get abstract datatype. */ typedef struct regex_t* re_t; -struct sn_community_regular_expression -{ +struct sn_community_regular_expression { re_t rule; // compiles regular expression UT_hash_handle hh; /* makes this structure hashable */ }; -typedef struct n2n_sn -{ +typedef struct n2n_sn { time_t start_time; /* Used to measure uptime. */ sn_stats_t stats; int daemon; /* If non-zero then daemonise. */ diff --git a/src/n2n.c b/src/n2n.c index a3509b2..3c631ed 100644 --- a/src/n2n.c +++ b/src/n2n.c @@ -32,14 +32,15 @@ static const n2n_mac_t null_mac = {0, 0, 0, 0, 0, 0}; /* ************************************** */ -SOCKET open_socket(int local_port, int bind_any) { +SOCKET open_socket (int local_port, int bind_any) { + SOCKET sock_fd; struct sockaddr_in local_address; int sockopt; - if((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { + if((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { traceEvent(TRACE_ERROR, "Unable to create socket [%s][%d]\n", - strerror(errno), sock_fd); + strerror(errno), sock_fd); return(-1); } @@ -67,28 +68,33 @@ static int traceLevel = 2 /* NORMAL */; static int useSyslog = 0, syslog_opened = 0; static FILE *traceFile = NULL; -int getTraceLevel() { +int getTraceLevel () { + return(traceLevel); } -void setTraceLevel(int level) { +void setTraceLevel (int level) { + traceLevel = level; } -void setUseSyslog(int use_syslog) { - useSyslog= use_syslog; +void setUseSyslog (int use_syslog) { + + useSyslog = use_syslog; } -void setTraceFile(FILE *f) { +void setTraceFile (FILE *f) { + traceFile = f; } -void closeTraceFile() { - if (traceFile != NULL && traceFile != stdout) { +void closeTraceFile () { + + if((traceFile != NULL) && (traceFile != stdout)) { fclose(traceFile); } #ifndef WIN32 - if (useSyslog && syslog_opened) { + if(useSyslog && syslog_opened) { closelog(); syslog_opened = 0; } @@ -96,11 +102,13 @@ void closeTraceFile() { } #define N2N_TRACE_DATESIZE 32 -void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...) { +void traceEvent (int eventTraceLevel, char* file, int line, char * format, ...) { + va_list va_ap; - if(traceFile == NULL) + if(traceFile == NULL) { traceFile = stdout; + } if(eventTraceLevel <= traceLevel) { char buf[1024]; @@ -120,15 +128,18 @@ void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...) { strftime(theDate, N2N_TRACE_DATESIZE, "%d/%b/%Y %H:%M:%S", localtime(&theTime)); va_start(va_ap, format); - vsnprintf(buf, sizeof(buf)-1, format, va_ap); + vsnprintf(buf, sizeof(buf) - 1, format, va_ap); va_end(va_ap); - if(eventTraceLevel == 0 /* TRACE_ERROR */) + if(eventTraceLevel == 0 /* TRACE_ERROR */) { extra_msg = "ERROR: "; - else if(eventTraceLevel == 1 /* TRACE_WARNING */) + } else if(eventTraceLevel == 1 /* TRACE_WARNING */) { extra_msg = "WARNING: "; + } - while(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0'; + while(buf[strlen(buf) - 1] == '\n') { + buf[strlen(buf) - 1] = '\0'; + } #ifndef WIN32 if(useSyslog) { @@ -140,14 +151,24 @@ void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...) { snprintf(out_buf, sizeof(out_buf), "%s%s", extra_msg, buf); syslog(LOG_INFO, "%s", out_buf); } else { - for(i=strlen(file)-1; i>0; i--) if(file[i] == '/') { i++; break; }; - snprintf(out_buf, sizeof(out_buf), "%s [%s:%d] %s%s", theDate, &file[i], line, extra_msg, buf); + for(i = strlen(file) - 1; i > 0; i--) { + if(file[i] == '/') { + i++; + break; + } + } + snprintf(out_buf, sizeof(out_buf), "%s [%s:%d] %s%s", theDate, &file[i], line, extra_msg, buf); fprintf(traceFile, "%s\n", out_buf); fflush(traceFile); } #else /* this is the WIN32 code */ - for(i=strlen(file)-1; i>0; i--) if(file[i] == '\\') { i++; break; }; + for(i = strlen(file) - 1; i > 0; i--) { + if(file[i] == '\\') { + i++; + break; + } + } snprintf(out_buf, sizeof(out_buf), "%s [%s:%d] %s%s", theDate, &file[i], line, extra_msg, buf); fprintf(traceFile, "%s\n", out_buf); fflush(traceFile); @@ -159,7 +180,8 @@ void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...) { /* *********************************************** */ /* addr should be in network order. Things are so much simpler that way. */ -char* intoa(uint32_t /* host order */ addr, char* buf, uint16_t buf_len) { +char* intoa (uint32_t /* host order */ addr, char* buf, uint16_t buf_len) { + char *cp, *retStr; uint8_t byteval; int n; @@ -175,50 +197,61 @@ char* intoa(uint32_t /* host order */ addr, char* buf, uint16_t buf_len) { if(byteval > 0) { *--cp = byteval % 10 + '0'; byteval /= 10; - if(byteval > 0) + if(byteval > 0) { *--cp = byteval + '0'; + } } *--cp = '.'; addr >>= 8; } while(--n > 0); /* Convert the string to lowercase */ - retStr =(char*)(cp+1); + retStr = (char*)(cp + 1); return(retStr); } /** Convert subnet prefix bit length to host order subnet mask. */ -uint32_t bitlen2mask(uint8_t bitlen) { - uint8_t i; - uint32_t mask = 0; - for (i = 1; i <= bitlen; ++i) { - mask |= 1 << (32 - i); - } - return mask; +uint32_t bitlen2mask (uint8_t bitlen) { + + uint8_t i; + uint32_t mask = 0; + + for (i = 1; i <= bitlen; ++i) { + mask |= 1 << (32 - i); + } + + return mask; } /** Convert host order subnet mask to subnet prefix bit length. */ -uint8_t mask2bitlen(uint32_t mask) { - uint8_t i, bitlen = 0; - for (i = 0; i < 32; ++i) { - if ((mask << i) & 0x80000000) ++bitlen; - else break; - } - return bitlen; +uint8_t mask2bitlen (uint32_t mask) { + + uint8_t i, bitlen = 0; + + for (i = 0; i < 32; ++i) { + if ((mask << i) & 0x80000000) { + ++bitlen; + } else { + break; + } + } + + return bitlen; } /* *********************************************** */ -char * macaddr_str(macstr_t buf, - const n2n_mac_t mac) -{ +char * macaddr_str (macstr_t buf, + 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, - mac[3] & 0xFF, mac[4] & 0xFF, mac[5] & 0xFF); + mac[0] & 0xFF, mac[1] & 0xFF, mac[2] & 0xFF, + mac[3] & 0xFF, mac[4] & 0xFF, mac[5] & 0xFF); + return(buf); } @@ -229,7 +262,8 @@ char * macaddr_str(macstr_t buf, * REVISIT: This is a really bad idea. The edge will block completely while the * hostname resolution is performed. This could take 15 seconds. */ -int supernode2sock(n2n_sock_t * sn, const n2n_sn_name_t addrIn) { +int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn) { + n2n_sn_name_t addr; const char *supernode_host; int rv = 0; @@ -245,38 +279,34 @@ int supernode2sock(n2n_sock_t * sn, const n2n_sn_name_t addrIn) { struct addrinfo * ainfo = NULL; int nameerr; - if(supernode_port){ + if(supernode_port) { sn->port = atoi(supernode_port); - } - else + } else { traceEvent(TRACE_WARNING, "Bad supernode parameter (-l ) %s %s:%s", - addr, supernode_host, supernode_port); + addr, supernode_host, supernode_port); + } nameerr = getaddrinfo(supernode_host, NULL, &aihints, &ainfo); - if(0 == nameerr) - { - struct sockaddr_in * saddr; - - /* ainfo s the head of a linked list if non-NULL. */ - if(ainfo && (PF_INET == ainfo->ai_family)) - { - /* It is definitely and IPv4 address -> sockaddr_in */ - saddr = (struct sockaddr_in *)ainfo->ai_addr; - - memcpy(sn->addr.v4, &(saddr->sin_addr.s_addr), IPV4_SIZE); - sn->family=AF_INET; - } - else - { - /* Should only return IPv4 addresses due to aihints. */ - traceEvent(TRACE_WARNING, "Failed to resolve supernode IPv4 address for %s", supernode_host); - rv = -1; - } - - freeaddrinfo(ainfo); /* free everything allocated by getaddrinfo(). */ - ainfo = NULL; + if(0 == nameerr) { + struct sockaddr_in * saddr; + + /* ainfo s the head of a linked list if non-NULL. */ + if(ainfo && (PF_INET == ainfo->ai_family)) { + /* It is definitely and IPv4 address -> sockaddr_in */ + saddr = (struct sockaddr_in *)ainfo->ai_addr; + + memcpy(sn->addr.v4, &(saddr->sin_addr.s_addr), IPV4_SIZE); + sn->family = AF_INET; } else { + /* Should only return IPv4 addresses due to aihints. */ + traceEvent(TRACE_WARNING, "Failed to resolve supernode IPv4 address for %s", supernode_host); + rv = -1; + } + + freeaddrinfo(ainfo); /* free everything allocated by getaddrinfo(). */ + ainfo = NULL; + } else { traceEvent(TRACE_WARNING, "Failed to resolve supernode host %s, %d: %s", supernode_host, nameerr, gai_strerror(nameerr)); rv = -2; } @@ -291,49 +321,49 @@ int supernode2sock(n2n_sock_t * sn, const n2n_sn_name_t addrIn) { /* ************************************** */ -struct peer_info* add_sn_to_list_by_mac_or_sock(struct peer_info **sn_list, n2n_sock_t *sock, n2n_mac_t *mac, int *skip_add){ +struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n_sock_t *sock, n2n_mac_t *mac, int *skip_add) { + struct peer_info *scan, *tmp, *peer = NULL; - if(memcmp(mac,null_mac,sizeof(n2n_mac_t)) != 0) { /* not zero MAC */ - HASH_FIND_PEER(*sn_list, mac, peer); - } + if(memcmp(mac, null_mac, sizeof(n2n_mac_t)) != 0) { /* not zero MAC */ + HASH_FIND_PEER(*sn_list, mac, peer); + } - if(peer == NULL) { /* zero MAC, search by socket */ - HASH_ITER(hh,*sn_list,scan,tmp) { - if(memcmp(&(scan->sock), sock, sizeof(n2n_sock_t)) == 0) { - HASH_DEL(*sn_list, scan); - memcpy(&(scan->mac_addr), mac, sizeof(n2n_mac_t)); - HASH_ADD_PEER(*sn_list, scan); - peer = scan; - break; - } + if(peer == NULL) { /* zero MAC, search by socket */ + HASH_ITER(hh, *sn_list, scan, tmp) { + if(memcmp(&(scan->sock), sock, sizeof(n2n_sock_t)) == 0) { + HASH_DEL(*sn_list, scan); + memcpy(&(scan->mac_addr), mac, sizeof(n2n_mac_t)); + HASH_ADD_PEER(*sn_list, scan); + peer = scan; + break; } + } - if((peer == NULL) && (*skip_add == SN_ADD)) { - peer = (struct peer_info*)calloc(1,sizeof(struct peer_info)); - if(peer) { - sn_selection_criterion_default(&(peer->selection_criterion)); - memcpy(&(peer->sock),sock,sizeof(n2n_sock_t)); - memcpy(&(peer->mac_addr),mac, sizeof(n2n_mac_t)); - HASH_ADD_PEER(*sn_list, peer); - *skip_add = SN_ADD_ADDED; - } + if((peer == NULL) && (*skip_add == SN_ADD)) { + peer = (struct peer_info*)calloc(1, sizeof(struct peer_info)); + if(peer) { + sn_selection_criterion_default(&(peer->selection_criterion)); + memcpy(&(peer->sock), sock, sizeof(n2n_sock_t)); + memcpy(&(peer->mac_addr), mac, sizeof(n2n_mac_t)); + HASH_ADD_PEER(*sn_list, peer); + *skip_add = SN_ADD_ADDED; } } + } return peer; } /* ************************************************ */ -uint8_t is_multi_broadcast(const uint8_t * dest_mac) { +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; - } /* http://www.faqs.org/rfcs/rfc908.html */ @@ -341,17 +371,18 @@ uint8_t is_multi_broadcast(const uint8_t * dest_mac) { /* *********************************************** */ -char* msg_type2str(uint16_t msg_type) { +char* msg_type2str (uint16_t msg_type) { + switch(msg_type) { - case MSG_TYPE_REGISTER: return("MSG_TYPE_REGISTER"); - case MSG_TYPE_DEREGISTER: return("MSG_TYPE_DEREGISTER"); - case MSG_TYPE_PACKET: return("MSG_TYPE_PACKET"); - case MSG_TYPE_REGISTER_ACK: return("MSG_TYPE_REGISTER_ACK"); - case MSG_TYPE_REGISTER_SUPER: return("MSG_TYPE_REGISTER_SUPER"); - case MSG_TYPE_REGISTER_SUPER_ACK: return("MSG_TYPE_REGISTER_SUPER_ACK"); - case MSG_TYPE_REGISTER_SUPER_NAK: return("MSG_TYPE_REGISTER_SUPER_NAK"); - case MSG_TYPE_FEDERATION: return("MSG_TYPE_FEDERATION"); - default: return("???"); + case MSG_TYPE_REGISTER: return("MSG_TYPE_REGISTER"); + case MSG_TYPE_DEREGISTER: return("MSG_TYPE_DEREGISTER"); + case MSG_TYPE_PACKET: return("MSG_TYPE_PACKET"); + case MSG_TYPE_REGISTER_ACK: return("MSG_TYPE_REGISTER_ACK"); + case MSG_TYPE_REGISTER_SUPER: return("MSG_TYPE_REGISTER_SUPER"); + case MSG_TYPE_REGISTER_SUPER_ACK: return("MSG_TYPE_REGISTER_SUPER_ACK"); + case MSG_TYPE_REGISTER_SUPER_NAK: return("MSG_TYPE_REGISTER_SUPER_NAK"); + case MSG_TYPE_FEDERATION: return("MSG_TYPE_FEDERATION"); + default: return("???"); } return("???"); @@ -359,41 +390,50 @@ char* msg_type2str(uint16_t msg_type) { /* *********************************************** */ -void hexdump(const uint8_t *buf, size_t len) { - size_t i; +void hexdump (const uint8_t *buf, size_t len) { - if (0 == len) { return; } + size_t i; - printf("-----------------------------------------------\n"); - for (i = 0; i < len; i++) { - if ((i > 0) && ((i % 16) == 0)) { printf("\n"); } - printf("%02X ", buf[i] & 0xFF); - } - printf("\n"); - printf("-----------------------------------------------\n"); + if(0 == len) { + return; + } + + printf("-----------------------------------------------\n"); + for (i = 0; i < len; i++) { + if ((i > 0) && ((i % 16) == 0)) { + printf("\n"); + } + printf("%02X ", buf[i] & 0xFF); + } + printf("\n"); + printf("-----------------------------------------------\n"); } /* *********************************************** */ -void print_n2n_version() { +void print_n2n_version () { + printf("Welcome to n2n v.%s for %s\n" "Built on %s\n" - "Copyright 2007-2020 - ntop.org and contributors\n\n", + "Copyright 2007-2020 - ntop.org and contributors\n\n", GIT_RELEASE, PACKAGE_OSNAME, PACKAGE_BUILDDATE); } /* *********************************************** */ -size_t purge_expired_registrations(struct peer_info ** peer_list, time_t* p_last_purge, int timeout) { +size_t purge_expired_registrations (struct peer_info ** peer_list, time_t* p_last_purge, int timeout) { + time_t now = time(NULL); size_t num_reg = 0; - if((now - (*p_last_purge)) < timeout) return 0; + if((now - (*p_last_purge)) < timeout) { + return 0; + } traceEvent(TRACE_DEBUG, "Purging old registrations"); - num_reg = purge_peer_list(peer_list, now-REGISTRATION_TIMEOUT); + num_reg = purge_peer_list(peer_list, now - REGISTRATION_TIMEOUT); (*p_last_purge) = now; traceEvent(TRACE_DEBUG, "Remove %ld registrations", num_reg); @@ -402,14 +442,14 @@ size_t purge_expired_registrations(struct peer_info ** peer_list, time_t* p_last } /** 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) -{ +size_t purge_peer_list (struct peer_info ** peer_list, + time_t purge_before) { + struct peer_info *scan, *tmp; - size_t retval=0; + size_t retval = 0; HASH_ITER(hh, *peer_list, scan, tmp) { - if(scan->purgeable == SN_PURGEABLE && scan->last_seen < purge_before) { + if((scan->purgeable == SN_PURGEABLE) && (scan->last_seen < purge_before)) { HASH_DEL(*peer_list, scan); retval++; free(scan); @@ -420,10 +460,10 @@ 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, *tmp; - size_t retval=0; + size_t retval = 0; HASH_ITER(hh, *peer_list, scan, tmp) { HASH_DEL(*peer_list, scan); @@ -434,41 +474,43 @@ 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 */ + tmp[0] = s[0]; + tmp[1] = s[1]; + tmp[2] = 0; /* NULL term */ 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; /* break it down as one case for the first "HH", the 5 x through loop for * each ":HH" where HH is a two hex nibbles in ASCII. */ - *outmac=hex2byte(s); + *outmac = hex2byte(s); ++outmac; - s+=2; /* don't skip colon yet - helps generalise loop. */ - - for(i=1; i<6; ++i) - { - s+=1; - *outmac=hex2byte(s); - ++outmac; - s+=2; - } + s += 2; /* don't skip colon yet - helps generalise loop. */ + + for(i = 1; i < 6; ++i) { + s += 1; + *outmac = hex2byte(s); + ++outmac; + s += 2; + } return 0; /* ok */ } -extern char * sock_to_cstr(n2n_sock_str_t out, - const n2n_sock_t * sock) { - if(NULL == out) { return NULL; } +extern char * sock_to_cstr (n2n_sock_str_t out, + const n2n_sock_t * sock) { + + if(NULL == out) { + return NULL; + } memset(out, 0, N2N_SOCKBUF_SIZE); if(AF_INET6 == sock->family) { @@ -479,41 +521,52 @@ extern char * sock_to_cstr(n2n_sock_str_t out, 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); + (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; } } -char *ip_subnet_to_str(dec_ip_bit_str_t buf, const n2n_ip_subnet_t *ipaddr) { - snprintf(buf, sizeof(dec_ip_bit_str_t), "%hhu.%hhu.%hhu.%hhu/%hhu", - (uint8_t) ((ipaddr->net_addr >> 24) & 0xFF), - (uint8_t) ((ipaddr->net_addr >> 16) & 0xFF), - (uint8_t) ((ipaddr->net_addr >> 8) & 0xFF), - (uint8_t) (ipaddr->net_addr & 0xFF), - ipaddr->net_bitlen); - return buf; +char *ip_subnet_to_str (dec_ip_bit_str_t buf, const n2n_ip_subnet_t *ipaddr) { + + snprintf(buf, sizeof(dec_ip_bit_str_t), "%hhu.%hhu.%hhu.%hhu/%hhu", + (uint8_t) ((ipaddr->net_addr >> 24) & 0xFF), + (uint8_t) ((ipaddr->net_addr >> 16) & 0xFF), + (uint8_t) ((ipaddr->net_addr >> 8) & 0xFF), + (uint8_t) (ipaddr->net_addr & 0xFF), + ipaddr->net_bitlen); + + return buf; } /* @return 1 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(0); } - if(a->family != b->family) { return(0); } +int sock_equal (const n2n_sock_t * a, + const n2n_sock_t * b) { + + if(a->port != b->port) { + return(0); + } + + if(a->family != b->family) { + return(0); + } switch(a->family) { - case AF_INET: - if(memcmp(a->addr.v4, b->addr.v4, IPV4_SIZE)) - return(0); - break; - default: - if(memcmp(a->addr.v6, b->addr.v6, IPV6_SIZE)) - return(0); - break; + case AF_INET: + if(memcmp(a->addr.v4, b->addr.v4, IPV4_SIZE)) { + return(0); + } + break; + + default: + if(memcmp(a->addr.v6, b->addr.v6, IPV6_SIZE)) { + return(0); + } + break; } /* equal */ @@ -523,10 +576,12 @@ int sock_equal(const n2n_sock_t * a, /* *********************************************** */ #if defined(WIN32) -int gettimeofday(struct timeval *tp, void *tzp) { +int gettimeofday (struct timeval *tp, void *tzp) { + time_t clock; struct tm tm; SYSTEMTIME wtm; + GetLocalTime(&wtm); tm.tm_year = wtm.wYear - 1900; tm.tm_mon = wtm.wMonth - 1; @@ -538,6 +593,7 @@ int gettimeofday(struct timeval *tp, void *tzp) { clock = mktime(&tm); tp->tv_sec = clock; tp->tv_usec = wtm.wMilliseconds * 1000; + return (0); } #endif @@ -567,7 +623,7 @@ uint64_t time_stamp (void) { // returns an initial time stamp for use with replay protection uint64_t initial_time_stamp (void) { - return ( time_stamp() - TIME_STAMP_FRAME ); + return (time_stamp() - TIME_STAMP_FRAME); } @@ -582,8 +638,8 @@ int time_stamp_verify_and_update (uint64_t stamp, uint64_t * previous_stamp, int // abs() diff = (diff < 0 ? -diff : diff); if(diff >= TIME_STAMP_FRAME) { - traceEvent(TRACE_DEBUG, "time_stamp_verify_and_update found a timestamp out of allowed frame."); - return (0); // failure + traceEvent(TRACE_DEBUG, "time_stamp_verify_and_update found a timestamp out of allowed frame."); + return (0); // failure } // if applicable: is it higher than previous time stamp (including allowed deviation of TIME_STAMP_JITTER)? @@ -593,8 +649,9 @@ int time_stamp_verify_and_update (uint64_t stamp, uint64_t * previous_stamp, int *previous_stamp = (*previous_stamp >> 12) << 12; diff = stamp - *previous_stamp; - if (allow_jitter) + if (allow_jitter) { diff += TIME_STAMP_JITTER; + } if(diff <= 0) { traceEvent(TRACE_DEBUG, "time_stamp_verify_and_update found a timestamp too old compared to previous.");