Code rework

This commit is contained in:
Luca Deri 2018-06-08 00:17:42 +02:00
parent 41401a131f
commit 85ac185781
5 changed files with 1931 additions and 1892 deletions

View File

@ -45,8 +45,10 @@ MAN8DIR=$(MANDIR)/man8
N2N_LIB=n2n.a N2N_LIB=n2n.a
N2N_OBJS=n2n.o n2n_keyfile.o wire.o minilzo.o twofish.o \ N2N_OBJS=n2n.o n2n_keyfile.o wire.o minilzo.o twofish.o \
egde_utils.o \
transform_null.o transform_tf.o transform_aes.o \ transform_null.o transform_tf.o transform_aes.o \
tuntap_freebsd.o tuntap_netbsd.o tuntap_linux.o tuntap_osx.o version.o tuntap_freebsd.o tuntap_netbsd.o tuntap_linux.o \
tuntap_osx.o version.o
LIBS_EDGE+=$(LIBS_EDGE_OPT) LIBS_EDGE+=$(LIBS_EDGE_OPT)
LIBS_SN= LIBS_SN=

2098
edge.c

File diff suppressed because it is too large Load Diff

1563
egde_utils.c Normal file

File diff suppressed because it is too large Load Diff

130
n2n.h
View File

@ -20,15 +20,15 @@
#define _N2N_H_ #define _N2N_H_
/* /*
tunctl -t tun0 tunctl -t tun0
tunctl -t tun1 tunctl -t tun1
ifconfig tun0 1.2.3.4 up ifconfig tun0 1.2.3.4 up
ifconfig tun1 1.2.3.5 up ifconfig tun1 1.2.3.5 up
./edge -d tun0 -l 2000 -r 127.0.0.1:3000 -c hello ./edge -d tun0 -l 2000 -r 127.0.0.1:3000 -c hello
./edge -d tun1 -l 3000 -r 127.0.0.1:2000 -c hello ./edge -d tun1 -l 3000 -r 127.0.0.1:2000 -c hello
tunctl -u UID -t tunX tunctl -u UID -t tunX
*/ */
@ -80,9 +80,9 @@
#define ETH_ADDR_LEN 6 #define ETH_ADDR_LEN 6
struct ether_hdr struct ether_hdr
{ {
uint8_t dhost[ETH_ADDR_LEN]; uint8_t dhost[ETH_ADDR_LEN];
uint8_t shost[ETH_ADDR_LEN]; uint8_t shost[ETH_ADDR_LEN];
uint16_t type; /* higher layer protocol encapsulated */ uint16_t type; /* higher layer protocol encapsulated */
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
typedef struct ether_hdr ether_hdr_t; typedef struct ether_hdr ether_hdr_t;
@ -94,6 +94,9 @@ typedef struct ether_hdr ether_hdr_t;
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h>
#include <sys/stat.h>
#include "minilzo.h"
#define closesocket(a) close(a) #define closesocket(a) close(a)
#endif /* #ifndef WIN32 */ #endif /* #ifndef WIN32 */
@ -107,6 +110,7 @@ typedef struct ether_hdr ether_hdr_t;
#endif /* #ifdef WIN32 */ #endif /* #ifdef WIN32 */
#include "n2n_wire.h" #include "n2n_wire.h"
#include "n2n_transforms.h"
/* N2N_IFNAMSIZ is needed on win32 even if dev_name is not used after declaration */ /* N2N_IFNAMSIZ is needed on win32 even if dev_name is not used after declaration */
#define N2N_IFNAMSIZ 16 /* 15 chars * NULL */ #define N2N_IFNAMSIZ 16 /* 15 chars * NULL */
@ -150,16 +154,69 @@ typedef char ipstr_t[32];
typedef char macstr_t[N2N_MACSTR_SIZE]; typedef char macstr_t[N2N_MACSTR_SIZE];
struct peer_info { struct peer_info {
struct peer_info * next; struct peer_info * next;
n2n_community_t community_name; n2n_community_t community_name;
n2n_mac_t mac_addr; n2n_mac_t mac_addr;
n2n_sock_t sock; n2n_sock_t sock;
time_t last_seen; time_t last_seen;
}; };
struct n2n_edge; /* defined in edge.c */ struct n2n_edge; /* defined in edge.c */
typedef struct n2n_edge n2n_edge_t; typedef struct n2n_edge n2n_edge_t;
#define N2N_EDGE_SN_HOST_SIZE 48
#define N2N_EDGE_NUM_SUPERNODES 2
#define N2N_EDGE_SUP_ATTEMPTS 3 /* Number of failed attmpts before moving on to next supernode. */
#define N2N_PATHNAME_MAXLEN 256
#define N2N_MAX_TRANSFORMS 16
#define N2N_EDGE_MGMT_PORT 5644
typedef char n2n_sn_name_t[N2N_EDGE_SN_HOST_SIZE];
struct n2n_edge {
int daemon; /**< Non-zero if edge should detach and run in the background. */
uint8_t re_resolve_supernode_ip;
n2n_sock_t supernode;
size_t sn_idx; /**< Currently active supernode. */
size_t sn_num; /**< Number of supernode addresses defined. */
n2n_sn_name_t sn_ip_array[N2N_EDGE_NUM_SUPERNODES];
int sn_wait; /**< Whether we are waiting for a supernode response. */
n2n_community_t community_name; /**< The community. 16 full octets. */
char keyschedule[N2N_PATHNAME_MAXLEN];
int null_transop; /**< Only allowed if no key sources defined. */
int udp_sock;
int udp_mgmt_sock; /**< socket for status info. */
tuntap_dev device; /**< All about the TUNTAP device */
int dyn_ip_mode; /**< Interface IP address is dynamically allocated, eg. DHCP. */
int allow_routing; /**< Accept packet no to interface address. */
int drop_multicast; /**< Multicast ethernet addresses. */
n2n_trans_op_t transop[N2N_MAX_TRANSFORMS]; /* one for each transform at fixed positions */
size_t tx_transop_idx; /**< The transop to use when encoding. */
struct peer_info * known_peers; /**< Edges we are connected to. */
struct peer_info * pending_peers; /**< Edges we have tried to register with. */
time_t last_register_req; /**< Check if time to re-register with super*/
size_t register_lifetime; /**< Time distance after last_register_req at which to re-register. */
time_t last_p2p; /**< Last time p2p traffic was received. */
time_t last_sup; /**< Last time a packet arrived from supernode. */
size_t sup_attempts; /**< Number of remaining attempts to this supernode. */
n2n_cookie_t last_cookie; /**< Cookie sent in last REGISTER_SUPER. */
time_t start_time; /**< For calculating uptime */
/* Statistics */
size_t tx_p2p;
size_t rx_p2p;
size_t tx_sup;
size_t rx_sup;
};
/* ************************************** */ /* ************************************** */
@ -234,4 +291,47 @@ size_t purge_expired_registrations( struct peer_info ** peer_list );
/* version.c */ /* version.c */
extern char *n2n_sw_version, *n2n_sw_osName, *n2n_sw_buildDate; extern char *n2n_sw_version, *n2n_sw_osName, *n2n_sw_buildDate;
/* egde_utils.c */
int edge_init(n2n_edge_t * eee);
void supernode2addr(n2n_sock_t * sn, const n2n_sn_name_t addrIn);
void update_supernode_reg(n2n_edge_t * eee, time_t nowTime);
int is_empty_ip_address(const n2n_sock_t * sock);
void update_peer_address(n2n_edge_t * eee,
uint8_t from_supernode,
const n2n_mac_t mac,
const n2n_sock_t * peer,
time_t when);
int transop_enum_to_index(n2n_transform_t id);
int edge_init_keyschedule(n2n_edge_t * eee);
void update_peer_address(n2n_edge_t * eee,
uint8_t from_supernode,
const n2n_mac_t mac,
const n2n_sock_t * peer,
time_t when);
int is_empty_ip_address(const n2n_sock_t * sock);
void send_register(n2n_edge_t * eee,
const n2n_sock_t * remote_peer);
void send_packet2net(n2n_edge_t * eee,
uint8_t *tap_pkt, size_t len);
void check_peer(n2n_edge_t * eee,
uint8_t from_supernode,
const n2n_mac_t mac,
const n2n_sock_t * peer);
void try_send_register(n2n_edge_t * eee,
uint8_t from_supernode,
const n2n_mac_t mac,
const n2n_sock_t * peer);
void set_peer_operational(n2n_edge_t * eee,
const n2n_mac_t mac,
const n2n_sock_t * peer);
const char * supernode_ip(const n2n_edge_t * eee);
int edge_init_twofish(n2n_edge_t * eee, uint8_t *encrypt_pwd,
uint32_t encrypt_pwd_len);
int run_edge_loop(n2n_edge_t * eee);
void edge_term(n2n_edge_t * eee);
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);
#endif /* _N2N_H_ */ #endif /* _N2N_H_ */

View File

@ -37,10 +37,9 @@
struct n2n_trans_op; struct n2n_trans_op;
typedef struct n2n_trans_op n2n_trans_op_t; typedef struct n2n_trans_op n2n_trans_op_t;
struct n2n_tostat struct n2n_tostat {
{ uint8_t can_tx; /* Does this transop have a valid SA for encoding. */
uint8_t can_tx; /* Does this transop have a valid SA for encoding. */ n2n_cipherspec_t tx_spec; /* If can_tx, the spec used to encode. */
n2n_cipherspec_t tx_spec; /* If can_tx, the spec used to encode. */
}; };
typedef struct n2n_tostat n2n_tostat_t; typedef struct n2n_tostat n2n_tostat_t;
@ -64,19 +63,18 @@ typedef int (*n2n_transform_f)( n2n_trans_op_t * arg,
* to use to decode the packet content. The transform code then decodes the * to use to decode the packet content. The transform code then decodes the
* packet and consults its internal key lookup. * packet and consults its internal key lookup.
*/ */
struct n2n_trans_op struct n2n_trans_op {
{ void * priv; /* opaque data. Key schedule goes here. */
void * priv; /* opaque data. Key schedule goes here. */
n2n_transform_t transform_id; /* link header enum to a transform */ n2n_transform_t transform_id; /* link header enum to a transform */
size_t tx_cnt; size_t tx_cnt;
size_t rx_cnt; size_t rx_cnt;
n2n_transdeinit_f deinit; /* destructor function */ n2n_transdeinit_f deinit; /* destructor function */
n2n_transaddspec_f addspec; /* parse opaque data from a key schedule file. */ n2n_transaddspec_f addspec; /* parse opaque data from a key schedule file. */
n2n_transtick_f tick; /* periodic maintenance */ n2n_transtick_f tick; /* periodic maintenance */
n2n_transform_f fwd; /* encode a payload */ n2n_transform_f fwd; /* encode a payload */
n2n_transform_f rev; /* decode a payload */ n2n_transform_f rev; /* decode a payload */
}; };
/* Setup a single twofish SA for single-key operation. */ /* Setup a single twofish SA for single-key operation. */