From b00f329a31198700827bd074946a07d02a8f8a6d Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Fri, 8 Jun 2018 08:19:06 +0200 Subject: [PATCH 1/6] Code cleanup --- Makefile | 6 +++- edge.c | 65 ------------------------------------ egde_utils.c => edge_utils.c | 51 ++++++++++++++++++++++++++++ n2n.h | 4 --- 4 files changed, 56 insertions(+), 70 deletions(-) rename egde_utils.c => edge_utils.c (96%) diff --git a/Makefile b/Makefile index 2e8266d..2eca42d 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ MAN8DIR=$(MANDIR)/man8 N2N_LIB=n2n.a N2N_OBJS=n2n.o n2n_keyfile.o wire.o minilzo.o twofish.o \ - egde_utils.o \ + edge_utils.o \ transform_null.o transform_tf.o transform_aes.o \ tuntap_freebsd.o tuntap_netbsd.o tuntap_linux.o \ tuntap_osx.o version.o @@ -60,6 +60,7 @@ endif APPS=edge APPS+=supernode +APPS+=example_edge_embed DOCS=edge.8.gz supernode.1.gz n2n_v2.7.gz @@ -77,6 +78,9 @@ supernode: sn.c $(N2N_LIB) n2n.h Makefile benchmark: benchmark.c $(N2N_LIB) n2n_wire.h n2n.h Makefile $(CC) $(CFLAGS) benchmark.c $(N2N_LIB) $(LIBS_SN) -o benchmark +example_edge_embed: example_edge_embed.c $(N2N_LIB) n2n.h + $(CC) $(CFLAGS) example_edge_embed.c $(N2N_LIB) $(LIBS_EDGE) -o example_edge_embed + .c.o: n2n.h n2n_keyfile.h n2n_transforms.h n2n_wire.h twofish.h Makefile $(CC) $(CFLAGS) -c $< diff --git a/edge.c b/edge.c index 02c15c0..12da835 100644 --- a/edge.c +++ b/edge.c @@ -222,57 +222,6 @@ static void help() { /* ************************************** */ -/** Start the registration process. - * - * If the peer is already in pending_peers, ignore the request. - * If not in pending_peers, add it and send a REGISTER. - * - * If hdr is for a direct peer-to-peer packet, try to register back to sender - * even if the MAC is in pending_peers. This is because an incident direct - * packet indicates that peer-to-peer exchange should work so more aggressive - * registration can be permitted (once per incoming packet) as this should only - * last for a small number of packets.. - * - * Called from the main loop when Rx a packet for our device mac. - */ -void try_send_register(n2n_edge_t * eee, - uint8_t from_supernode, - const n2n_mac_t mac, - const n2n_sock_t * peer) -{ - /* REVISIT: purge of pending_peers not yet done. */ - struct peer_info * scan = find_peer_by_mac(eee->pending_peers, mac); - macstr_t mac_buf; - n2n_sock_str_t sockbuf; - - if(NULL == scan) - { - scan = calloc(1, sizeof(struct peer_info)); - - memcpy(scan->mac_addr, mac, N2N_MAC_SIZE); - scan->sock = *peer; - scan->last_seen = time(NULL); /* Don't change this it marks the pending peer for removal. */ - - peer_list_add(&(eee->pending_peers), scan); - - traceEvent(TRACE_DEBUG, "=== new pending %s -> %s", - macaddr_str(mac_buf, scan->mac_addr), - sock_to_cstr(sockbuf, &(scan->sock))); - - traceEvent(TRACE_INFO, "Pending peers list size=%u", - (unsigned int)peer_list_size(eee->pending_peers)); - - /* trace Sending REGISTER */ - - send_register(eee, &(scan->sock)); - - /* pending_peers now owns scan. */ - } else { - } -} - -/* ************************************** */ - #if defined(DUMMY_ID_00001) /* Disabled waiting for config option to enable it */ static char gratuitous_arp[] = { @@ -799,17 +748,3 @@ int main(int argc, char* argv[]) { } /* ************************************** */ - -#ifdef QUICK_INIT - -int main(int argc, char* argv[]) { - traceLevel = 10; - return(quick_edge_init("n2n0", - "mynetwork", - "ntop2018", - "DE:AD:BE:EF:01:10", - "192.168.254.10", - "192.12.193.11:7654")); -} - -#endif diff --git a/egde_utils.c b/edge_utils.c similarity index 96% rename from egde_utils.c rename to edge_utils.c index 32a3caf..c9ff1a0 100644 --- a/egde_utils.c +++ b/edge_utils.c @@ -151,6 +151,57 @@ void supernode2addr(n2n_sock_t * sn, const n2n_sn_name_t addrIn) { /* ************************************** */ +/** Start the registration process. + * + * If the peer is already in pending_peers, ignore the request. + * If not in pending_peers, add it and send a REGISTER. + * + * If hdr is for a direct peer-to-peer packet, try to register back to sender + * even if the MAC is in pending_peers. This is because an incident direct + * packet indicates that peer-to-peer exchange should work so more aggressive + * registration can be permitted (once per incoming packet) as this should only + * last for a small number of packets.. + * + * Called from the main loop when Rx a packet for our device mac. + */ +static void try_send_register(n2n_edge_t * eee, + uint8_t from_supernode, + const n2n_mac_t mac, + const n2n_sock_t * peer) +{ + /* REVISIT: purge of pending_peers not yet done. */ + struct peer_info * scan = find_peer_by_mac(eee->pending_peers, mac); + macstr_t mac_buf; + n2n_sock_str_t sockbuf; + + if(NULL == scan) + { + scan = calloc(1, sizeof(struct peer_info)); + + memcpy(scan->mac_addr, mac, N2N_MAC_SIZE); + scan->sock = *peer; + scan->last_seen = time(NULL); /* Don't change this it marks the pending peer for removal. */ + + peer_list_add(&(eee->pending_peers), scan); + + traceEvent(TRACE_DEBUG, "=== new pending %s -> %s", + macaddr_str(mac_buf, scan->mac_addr), + sock_to_cstr(sockbuf, &(scan->sock))); + + traceEvent(TRACE_INFO, "Pending peers list size=%u", + (unsigned int)peer_list_size(eee->pending_peers)); + + /* trace Sending REGISTER */ + + send_register(eee, &(scan->sock)); + + /* pending_peers now owns scan. */ + } else { + } +} + +/* ************************************** */ + /** Update the last_seen time for this peer, or get registered. */ void check_peer(n2n_edge_t * eee, uint8_t from_supernode, diff --git a/n2n.h b/n2n.h index cf11c48..10f1513 100644 --- a/n2n.h +++ b/n2n.h @@ -317,10 +317,6 @@ 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); From 1893ad549b9248474030eabfec52af28f8d27413 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Fri, 8 Jun 2018 08:19:46 +0200 Subject: [PATCH 2/6] Simple example application that shows how to easily embed the edge onto a use app --- example_edge_embed.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 example_edge_embed.c diff --git a/example_edge_embed.c b/example_edge_embed.c new file mode 100644 index 0000000..fbb4c1f --- /dev/null +++ b/example_edge_embed.c @@ -0,0 +1,49 @@ +/** + * (C) 2007-18 - ntop.org and contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not see see + * + */ + +#include "n2n.h" + +/* + This tool demonstrates how to easily embed + n2n on an existing application + */ + +int main(int argc, char* argv[]) { + char *device_name = (char*)"n2n0"; + char *network_name = (char*)"mynetwork"; + char *secret_key = (char*)"mysecret"; + char *my_mac_address = (char*)"DE:AD:BE:EF:01:10"; + char *my_ipv4_addr = (char*)"1.2.3.4"; + char *supernode = (char*)"7.8.9.10:1234"; + + /* Increase tracelevel to see what's happening */ + traceLevel = 10; + + /* + NOTE + + As the function below won't end, you should + call it inside a separate thread + */ + return(quick_edge_init(device_name, + network_name, + secret_key, + my_mac_address, + my_ipv4_addr, + supernode)); +} From 73cd2992754614fee07f66caa6f0f4694bb1ff54 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Fri, 8 Jun 2018 08:21:27 +0200 Subject: [PATCH 3/6] Moved to 2.3.x release --- CMakeLists.txt | 2 +- Makefile | 2 +- n2n.spec | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 24e9361..af87762 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ project(n2n) cmake_minimum_required(VERSION 2.6) # N2n information -set(N2N_VERSION 2.1.0) +set(N2N_VERSION 2.3.0) set(N2N_OSNAME ${CMAKE_SYSTEM}) # N2n specific params diff --git a/Makefile b/Makefile index 2eca42d..2b0d9b0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -N2N_VERSION=2.1.0 +N2N_VERSION=2.3.0 N2N_OSNAME=$(shell uname -p) ######## diff --git a/n2n.spec b/n2n.spec index f5ee7d8..c783d2c 100644 --- a/n2n.spec +++ b/n2n.spec @@ -1,11 +1,11 @@ Summary: N2N peer-to-peer virtual private network system. Name: n2n -Version: 2.1.0 +Version: 2.3.0 Release: 1 License: GPLv3 Vendor: ntop.org Group: None -URL: http://www.ntop.org/n2n +URL: http://www.ntop.org/ Source0: %{name}-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root From 828ae8316ce7e9d64f99e39a358d18b393333acb Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Fri, 8 Jun 2018 11:26:25 +0200 Subject: [PATCH 4/6] Renamed n2n.a -> libn2n.a --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 2b0d9b0..a494447 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ N2N_OPTION_AES?="yes" #N2N_OPTION_AES=no ifeq ($(N2N_OPTION_AES), "yes") - N2N_DEFINES+="-DN2N_HAVE_AES" + N2N_DEFINES+=-DN2N_HAVE_AES LIBS_EDGE_OPT+=-lcrypto endif @@ -43,7 +43,7 @@ MAN1DIR=$(MANDIR)/man1 MAN7DIR=$(MANDIR)/man7 MAN8DIR=$(MANDIR)/man8 -N2N_LIB=n2n.a +N2N_LIB=libn2n.a N2N_OBJS=n2n.o n2n_keyfile.o wire.o minilzo.o twofish.o \ edge_utils.o \ transform_null.o transform_tf.o transform_aes.o \ From b6fdae38b950a2a6ca323ba0219874c673fb6885 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Fri, 8 Jun 2018 12:01:03 +0200 Subject: [PATCH 5/6] Added termination variable to edge loop --- edge.c | 3 ++- edge_utils.c | 18 +++++++++--------- example_edge_embed.c | 6 ++++-- n2n.h | 5 +++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/edge.c b/edge.c index 12da835..2c09b6e 100644 --- a/edge.c +++ b/edge.c @@ -394,6 +394,7 @@ static void daemonize() { /** Entry point to program from kernel. */ int main(int argc, char* argv[]) { int opt; + int keep_on_running = 1; int local_port = 0 /* any port */; int mgmt_port = N2N_EDGE_MGMT_PORT; /* 5644 by default */ char tuntap_dev_name[N2N_IFNAMSIZ] = "edge0"; @@ -744,7 +745,7 @@ int main(int argc, char* argv[]) { update_supernode_reg(&eee, time(NULL)); - return run_edge_loop(&eee); + return run_edge_loop(&eee, &keep_on_running); } /* ************************************** */ diff --git a/edge_utils.c b/edge_utils.c index c9ff1a0..249daab 100644 --- a/edge_utils.c +++ b/edge_utils.c @@ -1394,8 +1394,7 @@ static void readFromIPSocket(n2n_edge_t * eee) { /* ************************************** */ -int run_edge_loop(n2n_edge_t * eee) { - int keep_running=1; +int run_edge_loop(n2n_edge_t * eee, int *keep_running) { size_t numPurged; time_t lastIfaceCheck=0; time_t lastTransop=0; @@ -1404,6 +1403,8 @@ int run_edge_loop(n2n_edge_t * eee) { startTunReadThread(eee); #endif + *keep_running = 1; + /* Main loop * * select() is used to wait for input on either the TAP fd or the UDP/TCP @@ -1450,16 +1451,14 @@ int run_edge_loop(n2n_edge_t * eee) { readFromIPSocket(eee); } - if(FD_ISSET(eee->udp_mgmt_sock, &socket_mask)) - { + if(FD_ISSET(eee->udp_mgmt_sock, &socket_mask)) { /* Read a cooked socket from the internet socket. Writes on the TAP * socket. */ - readFromMgmtSocket(eee, &keep_running); + readFromMgmtSocket(eee, keep_running); } #ifndef WIN32 - if(FD_ISSET(eee->device.fd, &socket_mask)) - { + if(FD_ISSET(eee->device.fd, &socket_mask)) { /* Read an ethernet frame from the TAP socket. Write on the IP * socket. */ readFromTAPSocket(eee); @@ -1583,7 +1582,8 @@ 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) { + char *supernode_ip_address_port, + int *keep_on_running) { n2n_edge_t eee; edge_init(&eee); @@ -1610,5 +1610,5 @@ int quick_edge_init(char *device_name, char *community_name, update_supernode_reg(&eee, time(NULL)); - return(run_edge_loop(&eee)); + return(run_edge_loop(&eee, keep_on_running)); } diff --git a/example_edge_embed.c b/example_edge_embed.c index fbb4c1f..4bd1811 100644 --- a/example_edge_embed.c +++ b/example_edge_embed.c @@ -30,7 +30,8 @@ int main(int argc, char* argv[]) { char *my_mac_address = (char*)"DE:AD:BE:EF:01:10"; char *my_ipv4_addr = (char*)"1.2.3.4"; char *supernode = (char*)"7.8.9.10:1234"; - + int keep_on_running = 1; + /* Increase tracelevel to see what's happening */ traceLevel = 10; @@ -45,5 +46,6 @@ int main(int argc, char* argv[]) { secret_key, my_mac_address, my_ipv4_addr, - supernode)); + supernode, + &keep_on_running)); } diff --git a/n2n.h b/n2n.h index 10f1513..f0a56ca 100644 --- a/n2n.h +++ b/n2n.h @@ -323,11 +323,12 @@ void set_peer_operational(n2n_edge_t * eee, 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); +int run_edge_loop(n2n_edge_t * eee, int *keep_running); 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); + char *supernode_ip_address_port, + int *keep_on_running); #endif /* _N2N_H_ */ From ae815c22e2975ccaff627661d5f242764e0f8129 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Fri, 8 Jun 2018 14:01:33 +0200 Subject: [PATCH 6/6] C++ fixes --- n2n.c | 8 ++++---- n2n.h | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/n2n.c b/n2n.c index d5c293c..2d1878f 100644 --- a/n2n.c +++ b/n2n.c @@ -279,11 +279,11 @@ size_t peer_list_size(const struct peer_info * list) * insertion. list takes ownership of new. */ void peer_list_add(struct peer_info * * list, - struct peer_info * new) + struct peer_info * newp) { - new->next = *list; - new->last_seen = time(NULL); - *list = new; + newp->next = *list; + newp->last_seen = time(NULL); + *list = newp; } diff --git a/n2n.h b/n2n.h index f0a56ca..2ad827e 100644 --- a/n2n.h +++ b/n2n.h @@ -220,11 +220,13 @@ struct n2n_edge { /* ************************************** */ +#ifndef TRACE_ERROR #define TRACE_ERROR 0, __FILE__, __LINE__ #define TRACE_WARNING 1, __FILE__, __LINE__ #define TRACE_NORMAL 2, __FILE__, __LINE__ #define TRACE_INFO 3, __FILE__, __LINE__ #define TRACE_DEBUG 4, __FILE__, __LINE__ +#endif /* ************************************** */ @@ -281,7 +283,7 @@ void print_n2n_version(); struct peer_info * find_peer_by_mac( struct peer_info * list, const n2n_mac_t mac ); void peer_list_add( struct peer_info * * list, - struct peer_info * new ); + struct peer_info * newp ); size_t peer_list_size( const struct peer_info * list ); size_t purge_peer_list( struct peer_info ** peer_list, time_t purge_before );