Browse Source

n2n.h cleanup

pull/100/head
emanuele-f 6 years ago
parent
commit
d689691599
  1. 5
      Makefile.in
  2. 2
      android/edge_android.c
  3. 8
      edge.c
  4. 42
      edge_utils.c
  5. 2
      example_edge_embed.c
  6. 24
      n2n.c
  7. 92
      n2n.h
  8. 4
      sn.c
  9. 9
      version.c

5
Makefile.in

@ -48,7 +48,7 @@ N2N_OBJS=n2n.o n2n_keyfile.o wire.o minilzo.o twofish.o \
edge_utils.o \ edge_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_freebsd.o tuntap_netbsd.o tuntap_linux.o \
tuntap_osx.o version.o tuntap_osx.o
LIBS_EDGE+=$(LIBS_EDGE_OPT) LIBS_EDGE+=$(LIBS_EDGE_OPT)
LIBS_SN= LIBS_SN=
@ -91,9 +91,6 @@ $(N2N_LIB): $(N2N_OBJS)
ar rcs $(N2N_LIB) $(N2N_OBJS) ar rcs $(N2N_LIB) $(N2N_OBJS)
# $(RANLIB) $@ # $(RANLIB) $@
version.o: Makefile
$(CC) $(CFLAGS) -c version.c
clean: clean:
rm -rf $(N2N_OBJS) $(N2N_LIB) $(APPS) $(DOCS) test *.dSYM *~ rm -rf $(N2N_OBJS) $(N2N_LIB) $(APPS) $(DOCS) test *.dSYM *~

2
android/edge_android.c

@ -336,8 +336,6 @@ int start_edge(const n2n_edge_cmd_t* cmd)
report_edge_status(); report_edge_status();
traceEvent(TRACE_NORMAL, "edge started"); traceEvent(TRACE_NORMAL, "edge started");
update_supernode_reg(&eee, time(NULL));
return run_edge_loop(&eee, &keep_on_running); return run_edge_loop(&eee, &keep_on_running);
} }

8
edge.c

@ -334,7 +334,7 @@ static int setOption(int optkey, char *optargument, edge_conf_t *ec, n2n_edge_t
} }
case 'v': /* verbose */ case 'v': /* verbose */
traceLevel = 4; /* DEBUG */ setTraceLevel(4); /* DEBUG */
break; break;
default: default:
@ -616,7 +616,7 @@ int main(int argc, char* argv[]) {
if((rc < 0) || (eee.sn_num == 0)) if((rc < 0) || (eee.sn_num == 0))
help(); help();
traceEvent(TRACE_NORMAL, "Starting n2n edge %s %s", n2n_sw_version, n2n_sw_buildDate); traceEvent(TRACE_NORMAL, "Starting n2n edge %s %s", PACKAGE_VERSION, PACKAGE_BUILDDATE);
for (i=0; i<eee.sn_num; ++i) for (i=0; i<eee.sn_num; ++i)
traceEvent(TRACE_NORMAL, "supernode %u => %s\n", i, (eee.sn_ip_array[i])); traceEvent(TRACE_NORMAL, "supernode %u => %s\n", i, (eee.sn_ip_array[i]));
@ -636,7 +636,7 @@ int main(int argc, char* argv[]) {
#ifndef WIN32 #ifndef WIN32
if(eee.daemon) { if(eee.daemon) {
useSyslog = 1; /* traceEvent output now goes to syslog. */ setUseSyslog(1); /* traceEvent output now goes to syslog. */
daemonize(); daemonize();
} }
#endif /* #ifndef WIN32 */ #endif /* #ifndef WIN32 */
@ -698,8 +698,6 @@ int main(int argc, char* argv[]) {
traceEvent(TRACE_NORMAL, "edge started"); traceEvent(TRACE_NORMAL, "edge started");
update_supernode_reg(&eee, time(NULL));
return run_edge_loop(&eee, &keep_on_running); return run_edge_loop(&eee, &keep_on_running);
} }

42
edge_utils.c

@ -61,6 +61,15 @@
/* ************************************** */ /* ************************************** */
static const char * supernode_ip(const n2n_edge_t * eee);
static void send_register(n2n_edge_t * eee, const n2n_sock_t * remote_peer);
static void check_peer(n2n_edge_t * eee,
uint8_t from_supernode,
const n2n_mac_t mac,
const n2n_sock_t * peer);
/* ************************************** */
/** Initialise an edge to defaults. /** Initialise an edge to defaults.
* *
* This also initialises the NULL transform operation opstruct. * This also initialises the NULL transform operation opstruct.
@ -232,7 +241,7 @@ static void try_send_register(n2n_edge_t * eee,
/* ************************************** */ /* ************************************** */
/** Update the last_seen time for this peer, or get registered. */ /** Update the last_seen time for this peer, or get registered. */
void check_peer(n2n_edge_t * eee, static void check_peer(n2n_edge_t * eee,
uint8_t from_supernode, uint8_t from_supernode,
const n2n_mac_t mac, const n2n_mac_t mac,
const n2n_sock_t * peer) { const n2n_sock_t * peer) {
@ -260,7 +269,7 @@ void check_peer(n2n_edge_t * eee,
* *
* Called by main loop when Rx a REGISTER_ACK. * Called by main loop when Rx a REGISTER_ACK.
*/ */
void set_peer_operational(n2n_edge_t * eee, static void set_peer_operational(n2n_edge_t * eee,
const n2n_mac_t mac, const n2n_mac_t mac,
const n2n_sock_t * peer) { const n2n_sock_t * peer) {
struct peer_info * prev = NULL; struct peer_info * prev = NULL;
@ -511,7 +520,7 @@ static void send_register_super(n2n_edge_t * eee,
/* ************************************** */ /* ************************************** */
/** Send a REGISTER packet to another edge. */ /** Send a REGISTER packet to another edge. */
void send_register(n2n_edge_t * eee, static void send_register(n2n_edge_t * eee,
const n2n_sock_t * remote_peer) { const n2n_sock_t * remote_peer) {
uint8_t pktbuf[N2N_PKT_BUF_SIZE]; uint8_t pktbuf[N2N_PKT_BUF_SIZE];
size_t idx; size_t idx;
@ -582,7 +591,7 @@ static void send_register_ack(n2n_edge_t * eee,
* *
* This is frequently called by the main loop. * This is frequently called by the main loop.
*/ */
void update_supernode_reg(n2n_edge_t * eee, time_t nowTime) { static void update_supernode_reg(n2n_edge_t * eee, time_t nowTime) {
u_int sn_idx; u_int sn_idx;
if(eee->sn_wait && (nowTime > (eee->last_register_req + (eee->register_lifetime/10)))) { if(eee->sn_wait && (nowTime > (eee->last_register_req + (eee->register_lifetime/10)))) {
@ -643,7 +652,7 @@ static void send_deregister(n2n_edge_t * eee,
/* ************************************** */ /* ************************************** */
/** Return the IP address of the current supernode in the ring. */ /** Return the IP address of the current supernode in the ring. */
const char * supernode_ip(const n2n_edge_t * eee) { static const char * supernode_ip(const n2n_edge_t * eee) {
return (eee->sn_ip_array)[eee->sn_idx]; return (eee->sn_ip_array)[eee->sn_idx];
} }
@ -742,7 +751,7 @@ static int n2n_tick_transop(n2n_edge_t * eee, time_t now)
* *
* @return - index into the transop array, or -1 on failure. * @return - index into the transop array, or -1 on failure.
*/ */
int transop_enum_to_index(n2n_transform_t id) { static int transop_enum_to_index(n2n_transform_t id) {
switch (id) switch (id)
{ {
case N2N_TRANSFORM_ID_TWOFISH: case N2N_TRANSFORM_ID_TWOFISH:
@ -889,7 +898,7 @@ static void readFromMgmtSocket(n2n_edge_t * eee, int * keep_running) {
if(0 == memcmp(udp_buf, "help", 4)) if(0 == memcmp(udp_buf, "help", 4))
{ {
msg_len=0; msg_len=0;
++traceLevel; setTraceLevel(getTraceLevel()+1);
msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len), msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len),
"Help for edge management console:\n" "Help for edge management console:\n"
@ -913,11 +922,11 @@ static void readFromMgmtSocket(n2n_edge_t * eee, int * keep_running) {
if(0 == memcmp(udp_buf, "+verb", 5)) if(0 == memcmp(udp_buf, "+verb", 5))
{ {
msg_len=0; msg_len=0;
++traceLevel; setTraceLevel(getTraceLevel()+1);
traceEvent(TRACE_ERROR, "+verb traceLevel=%u", (unsigned int)traceLevel); traceEvent(TRACE_ERROR, "+verb traceLevel=%u", (unsigned int)getTraceLevel());
msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len), msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len),
"> +OK traceLevel=%u\n", (unsigned int)traceLevel); "> +OK traceLevel=%u\n", (unsigned int)getTraceLevel());
sendto(eee->udp_mgmt_sock, udp_buf, msg_len, 0/*flags*/, sendto(eee->udp_mgmt_sock, udp_buf, msg_len, 0/*flags*/,
(struct sockaddr *)&sender_sock, sizeof(struct sockaddr_in)); (struct sockaddr *)&sender_sock, sizeof(struct sockaddr_in));
@ -929,19 +938,19 @@ static void readFromMgmtSocket(n2n_edge_t * eee, int * keep_running) {
{ {
msg_len=0; msg_len=0;
if(traceLevel > 0) if(getTraceLevel() > 0)
{ {
--traceLevel; setTraceLevel(getTraceLevel()-1);
msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len), msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len),
"> -OK traceLevel=%u\n", traceLevel); "> -OK traceLevel=%u\n", getTraceLevel());
} }
else else
{ {
msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len), msg_len += snprintf((char *)(udp_buf+msg_len), (N2N_PKT_BUF_SIZE-msg_len),
"> -NOK traceLevel=%u\n", traceLevel); "> -NOK traceLevel=%u\n", getTraceLevel());
} }
traceEvent(TRACE_ERROR, "-verb traceLevel=%u", (unsigned int)traceLevel); traceEvent(TRACE_ERROR, "-verb traceLevel=%u", (unsigned int)getTraceLevel());
sendto(eee->udp_mgmt_sock, udp_buf, msg_len, 0/*flags*/, sendto(eee->udp_mgmt_sock, udp_buf, msg_len, 0/*flags*/,
(struct sockaddr *)&sender_sock, sizeof(struct sockaddr_in)); (struct sockaddr *)&sender_sock, sizeof(struct sockaddr_in));
@ -1512,6 +1521,7 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) {
#endif #endif
*keep_running = 1; *keep_running = 1;
update_supernode_reg(eee, time(NULL));
/* Main loop /* Main loop
* *
@ -1809,7 +1819,5 @@ int quick_edge_init(char *device_name, char *community_name,
if(edge_init_sockets(&eee, 0 /* ANY port */, 0 /* ANY port */) < 0) if(edge_init_sockets(&eee, 0 /* ANY port */, 0 /* ANY port */) < 0)
return(-3); return(-3);
update_supernode_reg(&eee, time(NULL));
return(run_edge_loop(&eee, keep_on_running)); return(run_edge_loop(&eee, keep_on_running));
} }

2
example_edge_embed.c

@ -33,7 +33,7 @@ int main(int argc, char* argv[]) {
int keep_on_running = 1; int keep_on_running = 1;
/* Increase tracelevel to see what's happening */ /* Increase tracelevel to see what's happening */
traceLevel = 10; setTraceLevel(10);
/* /*
NOTE NOTE

24
n2n.c

@ -31,9 +31,9 @@
#endif /* #if defined(DEBUG) */ #endif /* #if defined(DEBUG) */
const uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; static const uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
const uint8_t multicast_addr[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 }; /* First 3 bytes are meaningful */ static const uint8_t multicast_addr[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 }; /* First 3 bytes are meaningful */
const uint8_t ipv6_multicast_addr[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; /* First 2 bytes are meaningful */ static const uint8_t ipv6_multicast_addr[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; /* First 2 bytes are meaningful */
/* ************************************** */ /* ************************************** */
@ -67,8 +67,20 @@ SOCKET open_socket(int local_port, int bind_any) {
return(sock_fd); return(sock_fd);
} }
int traceLevel = 2 /* NORMAL */; static int traceLevel = 2 /* NORMAL */;
int useSyslog = 0, syslog_opened = 0; static int useSyslog = 0, syslog_opened = 0;
int getTraceLevel() {
return(traceLevel);
}
void setTraceLevel(int level) {
traceLevel = level;
}
void setUseSyslog(int use_syslog) {
useSyslog= use_syslog;
}
#define N2N_TRACE_DATESIZE 32 #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, ...) {
@ -252,7 +264,7 @@ void print_n2n_version() {
printf("Welcome to n2n v.%s for %s\n" printf("Welcome to n2n v.%s for %s\n"
"Built on %s\n" "Built on %s\n"
"Copyright 2007-18 - ntop.org and contributors\n\n", "Copyright 2007-18 - ntop.org and contributors\n\n",
n2n_sw_version, n2n_sw_osName, n2n_sw_buildDate); PACKAGE_VERSION, PACKAGE_OSNAME, PACKAGE_BUILDDATE);
} }
/* *********************************************** */ /* *********************************************** */

92
n2n.h

@ -44,6 +44,7 @@
#include "config.h" #include "config.h"
#endif #endif
#define PACKAGE_BUILDDATE (__DATE__ " " __TIME__)
#include <time.h> #include <time.h>
#include <ctype.h> #include <ctype.h>
@ -262,89 +263,66 @@ struct n2n_edge {
/* ************************************** */ /* ************************************** */
/* Variables */ /* Log */
/* extern TWOFISH *tf; */ void setTraceLevel(int level);
extern int traceLevel; void setUseSyslog(int use_syslog);
extern int useSyslog; int getTraceLevel();
extern const uint8_t broadcast_addr[6]; void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...);
extern const uint8_t multicast_addr[6];
/* Functions */ /* Tuntap API */
extern void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...); int tuntap_open(tuntap_dev *device, char *dev, const char *address_mode, char *device_ip,
extern int tuntap_open(tuntap_dev *device, char *dev, const char *address_mode, char *device_ip,
char *device_mask, const char * device_mac, int mtu); char *device_mask, const char * device_mac, int mtu);
extern int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len); int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len);
extern int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len); int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len);
extern void tuntap_close(struct tuntap_dev *tuntap); void tuntap_close(struct tuntap_dev *tuntap);
extern void tuntap_get_address(struct tuntap_dev *tuntap); void tuntap_get_address(struct tuntap_dev *tuntap);
extern SOCKET open_socket(int local_port, int bind_any); /* Utils */
char* intoa(uint32_t addr, char* buf, uint16_t buf_len);
char* macaddr_str(macstr_t buf, const n2n_mac_t mac);
int str2mac( uint8_t * outmac /* 6 bytes */, const char * s );
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();
void supernode2addr(n2n_sock_t * sn, const n2n_sn_name_t addrIn);
int is_empty_ip_address(const n2n_sock_t * sock);
const char *random_device_mac(void);
extern char* intoa(uint32_t addr, char* buf, uint16_t buf_len); /* Sockets */
extern char* macaddr_str(macstr_t buf, const n2n_mac_t mac); char* sock_to_cstr( n2n_sock_str_t out,
extern int str2mac( uint8_t * outmac /* 6 bytes */, const char * s );
extern char * sock_to_cstr( n2n_sock_str_t out,
const n2n_sock_t * sock ); const n2n_sock_t * sock );
SOCKET open_socket(int local_port, int bind_any);
extern int sock_equal( const n2n_sock_t * a, int sock_equal( const n2n_sock_t * a,
const n2n_sock_t * b ); const n2n_sock_t * b );
extern uint8_t is_multi_broadcast(const uint8_t * dest_mac);
extern char* msg_type2str(uint16_t msg_type);
extern void hexdump(const uint8_t * buf, size_t len);
void print_n2n_version();
/* Operations on peer_info lists. */ /* Operations on peer_info lists. */
struct peer_info * find_peer_by_mac( struct peer_info * list, struct peer_info * find_peer_by_mac( struct peer_info * list,
const n2n_mac_t mac ); const n2n_mac_t mac );
void peer_list_add( struct peer_info * * list, void peer_list_add( struct peer_info * * list,
struct peer_info * newp ); struct peer_info * newp );
size_t peer_list_size( const struct peer_info * list ); size_t peer_list_size( const struct peer_info * list );
size_t purge_peer_list( struct peer_info ** peer_list, size_t purge_peer_list( struct peer_info ** peer_list,
time_t purge_before ); time_t purge_before );
size_t clear_peer_list( struct peer_info ** peer_list ); size_t clear_peer_list( struct peer_info ** peer_list );
size_t purge_expired_registrations( struct peer_info ** peer_list ); size_t purge_expired_registrations( struct peer_info ** peer_list );
/* version.c */
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, void update_peer_address(n2n_edge_t * eee,
uint8_t from_supernode, uint8_t from_supernode,
const n2n_mac_t mac, const n2n_mac_t mac,
const n2n_sock_t * peer, const n2n_sock_t * peer,
time_t when); time_t when);
int transop_enum_to_index(n2n_transform_t id);
/* Public functions */
int edge_init_keyschedule(n2n_edge_t * eee); 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, void send_packet2net(n2n_edge_t * eee,
uint8_t *tap_pkt, size_t len); 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 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_encryption(n2n_edge_t * eee, uint8_t *encrypt_pwd, uint32_t encrypt_pwd_len); int edge_init_encryption(n2n_edge_t * eee, uint8_t *encrypt_pwd, uint32_t encrypt_pwd_len);
int edge_init_sockets(n2n_edge_t *eee, int udp_local_port, int mgmt_port); int edge_init_sockets(n2n_edge_t *eee, int udp_local_port, int mgmt_port);
int run_edge_loop(n2n_edge_t * eee, int *keep_running);
int edge_init(n2n_edge_t * eee);
void edge_term(n2n_edge_t * eee); void edge_term(n2n_edge_t * eee);
const char *random_device_mac(void); int run_edge_loop(n2n_edge_t * eee, int *keep_running);
int quick_edge_init(char *device_name, char *community_name, int quick_edge_init(char *device_name, char *community_name,
char *encrypt_key, char *device_mac, char *encrypt_key, char *device_mac,
char *local_ip_address, char *local_ip_address,

4
sn.c

@ -721,7 +721,7 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
break; break;
case 'v': /* verbose */ case 'v': /* verbose */
traceLevel = 4; /* DEBUG */ setTraceLevel(4); /* DEBUG */
break; break;
default: default:
@ -905,7 +905,7 @@ int main(int argc, char * const argv[]) {
} }
#endif /* #if defined(N2N_HAVE_DAEMON) */ #endif /* #if defined(N2N_HAVE_DAEMON) */
traceEvent(TRACE_DEBUG, "traceLevel is %d", traceLevel); traceEvent(TRACE_DEBUG, "traceLevel is %d", getTraceLevel());
sss_node.sock = open_socket(sss_node.lport, 1 /*bind ANY*/); sss_node.sock = open_socket(sss_node.lport, 1 /*bind ANY*/);
if(-1 == sss_node.sock) { if(-1 == sss_node.sock) {

9
version.c

@ -1,9 +0,0 @@
#ifndef WIN32
#include "config.h"
#else
#include "win32/winconfig.h"
#endif
const char * n2n_sw_version = PACKAGE_VERSION;
const char * n2n_sw_osName = PACKAGE_OSNAME;
const char * n2n_sw_buildDate = __DATE__ " " __TIME__;
Loading…
Cancel
Save