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 2e8266d..a494447 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -N2N_VERSION=2.1.0 +N2N_VERSION=2.3.0 N2N_OSNAME=$(shell uname -p) ######## @@ -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,9 +43,9 @@ 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 \ - 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..2c09b6e 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[] = { @@ -445,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"; @@ -795,21 +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); } /* ************************************** */ - -#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 95% rename from egde_utils.c rename to edge_utils.c index 32a3caf..249daab 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, @@ -1343,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; @@ -1353,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 @@ -1399,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); @@ -1532,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); @@ -1559,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 new file mode 100644 index 0000000..4bd1811 --- /dev/null +++ b/example_edge_embed.c @@ -0,0 +1,51 @@ +/** + * (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"; + int keep_on_running = 1; + + /* 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, + &keep_on_running)); +} 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 cf11c48..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 ); @@ -317,21 +319,18 @@ 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); +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_ */ 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