Merge pull request #1 from ntop/master

sync n2n
This commit is contained in:
switch 2018-06-08 10:29:36 +08:00 committed by GitHub
commit 0f5faada07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 2503 additions and 2248 deletions

View File

@ -10,9 +10,10 @@ if(NOT DEFINED N2N_OPTION_AES)
set(N2N_OPTION_AES ON) set(N2N_OPTION_AES ON)
endif(NOT DEFINED N2N_OPTION_AES) endif(NOT DEFINED N2N_OPTION_AES)
add_definitions(-DN2N_VERSION='\"${N2N_VERSION}\"' -DN2N_OSNAME='\"${N2N_OSNAME}\"') add_definitions(-DN2N_VERSION="${N2N_VERSION}" -DN2N_OSNAME="${N2N_OSNAME}")
if(N2N_OPTION_AES) if(N2N_OPTION_AES)
find_package(OpenSSL REQUIRED)
add_definitions(-DN2N_HAVE_AES) add_definitions(-DN2N_HAVE_AES)
endif(N2N_OPTION_AES) endif(N2N_OPTION_AES)

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=

View File

@ -114,6 +114,6 @@ AES (-O3) 12532
TF (-O3) 14046 TF (-O3) 14046
NULL (-O3) 10659 NULL (-O3) 10659
(C) 2007-2010 - Luca Deri <deri@ntop.org> and Richard Andrews <andrews@ntop.org> -----------------
(C) 2016 - ntop (C) 2007-18 - ntop.org and contributors

View File

@ -1,3 +1,21 @@
/*
* (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 <http://www.gnu.org/licenses/>
*
*/
#include "n2n_wire.h" #include "n2n_wire.h"
#include "n2n_transforms.h" #include "n2n_transforms.h"
#include "n2n.h" #include "n2n.h"

8
contributors.txt Normal file
View File

@ -0,0 +1,8 @@
Code contributions courtesy of:
* Richard Andrews <andrews@ntop.org>
* Don Bindner <don.bindner@gmail.com>
* Sylwester Sosnowski <syso-n2n@no-route.org>
* Wilfried "Wonka" Klaebe
* Lukasz Taczuk
* Alaric Snell-Pym <alaric@kitten-technologies.co.uk>
* Babak Farrokhi <babak@farrokhi.net> [FreeBSD port]

19
doc/n2n_on_MacOS.txt Normal file
View File

@ -0,0 +1,19 @@
Using n2n on MacOS
------------------
In order to use n2n on MacOS you need to first install support for the tap interface.
if you are a brew (https://brew.sh) user you can do it in a couple of steps
- brew tap homebrew/cask
- brew cask install tuntap
Note that in the latest OS versions (for instance MacOS High Sierra), you may need to
need to enable their kernel extension in
System Preferences → Security & Privacy → General
For more information refer to vendor documentation or the Apple Technical Note:
https://developer.apple.com/library/content/technotes/tn2459/_index.html

1997
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

126
n2n.c
View File

@ -1,6 +1,5 @@
/* /**
* (C) 2007-09 - Luca Deri <deri@ntop.org> * (C) 2007-18 - ntop.org and contributors
* Richard Andrews <andrews@ntop.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -13,11 +12,7 @@
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/> * along with this program; if not see see <http://www.gnu.org/licenses/>
*
* Code contributions courtesy of:
* Massimo Torquati <torquati@ntop.org>
* Matt Gilg
* *
*/ */
@ -57,13 +52,13 @@ SOCKET open_socket(int local_port, int bind_any) {
/* fcntl(sock_fd, F_SETFL, O_NONBLOCK); */ /* fcntl(sock_fd, F_SETFL, O_NONBLOCK); */
#endif #endif
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&sockopt, sizeof(sockopt)); setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,(char *)&sockopt, sizeof(sockopt));
memset(&local_address, 0, sizeof(local_address)); memset(&local_address, 0, sizeof(local_address));
local_address.sin_family = AF_INET; local_address.sin_family = AF_INET;
local_address.sin_port = htons(local_port); local_address.sin_port = htons(local_port);
local_address.sin_addr.s_addr = htonl(bind_any?INADDR_ANY:INADDR_LOOPBACK); local_address.sin_addr.s_addr = htonl(bind_any?INADDR_ANY:INADDR_LOOPBACK);
if(bind(sock_fd, (struct sockaddr*) &local_address, sizeof(local_address)) == -1) { if(bind(sock_fd,(struct sockaddr*) &local_address, sizeof(local_address)) == -1) {
traceEvent(TRACE_ERROR, "Bind error [%s]\n", strerror(errno)); traceEvent(TRACE_ERROR, "Bind error [%s]\n", strerror(errno));
return(-1); return(-1);
} }
@ -93,7 +88,7 @@ void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...) {
#endif #endif
/* We have two paths - one if we're logging, one if we aren't /* We have two paths - one if we're logging, one if we aren't
* Note that the no-log case is those systems which don't support it (WIN32), * Note that the no-log case is those systems which don't support it(WIN32),
* those without the headers !defined(USE_SYSLOG) * those without the headers !defined(USE_SYSLOG)
* those where it's parametrically off... * those where it's parametrically off...
*/ */
@ -101,7 +96,7 @@ void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...) {
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
strftime(theDate, N2N_TRACE_DATESIZE, "%d/%b/%Y %H:%M:%S", localtime(&theTime)); strftime(theDate, N2N_TRACE_DATESIZE, "%d/%b/%Y %H:%M:%S", localtime(&theTime));
va_start (va_ap, format); 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); va_end(va_ap);
@ -153,26 +148,26 @@ char* intoa(uint32_t /* host order */ addr, char* buf, uint16_t buf_len) {
byteval = addr & 0xff; byteval = addr & 0xff;
*--cp = byteval % 10 + '0'; *--cp = byteval % 10 + '0';
byteval /= 10; byteval /= 10;
if (byteval > 0) { if(byteval > 0) {
*--cp = byteval % 10 + '0'; *--cp = byteval % 10 + '0';
byteval /= 10; byteval /= 10;
if (byteval > 0) if(byteval > 0)
*--cp = byteval + '0'; *--cp = byteval + '0';
} }
*--cp = '.'; *--cp = '.';
addr >>= 8; addr >>= 8;
} while (--n > 0); } while(--n > 0);
/* Convert the string to lowercase */ /* Convert the string to lowercase */
retStr = (char*)(cp+1); retStr =(char*)(cp+1);
return(retStr); return(retStr);
} }
/* *********************************************** */ /* *********************************************** */
char * macaddr_str( macstr_t buf, char * macaddr_str(macstr_t buf,
const n2n_mac_t mac ) const n2n_mac_t mac)
{ {
snprintf(buf, N2N_MACSTR_SIZE, "%02X:%02X:%02X:%02X:%02X:%02X", snprintf(buf, N2N_MACSTR_SIZE, "%02X:%02X:%02X:%02X:%02X:%02X",
mac[0] & 0xFF, mac[1] & 0xFF, mac[2] & 0xFF, mac[0] & 0xFF, mac[1] & 0xFF, mac[2] & 0xFF,
@ -184,9 +179,9 @@ char * macaddr_str( macstr_t buf,
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_broadcast =(memcmp(broadcast_addr, dest_mac, 6) == 0);
int is_multicast = ( memcmp(multicast_addr, dest_mac, 3) == 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_ipv6_multicast =(memcmp(ipv6_multicast_addr, dest_mac, 2) == 0);
return is_broadcast || is_multicast || is_ipv6_multicast; return is_broadcast || is_multicast || is_ipv6_multicast;
@ -219,11 +214,11 @@ void hexdump(const uint8_t * buf, size_t len)
{ {
size_t i; size_t i;
if ( 0 == len ) { return; } if(0 == len) { return; }
for(i=0; i<len; i++) for(i=0; i<len; i++)
{ {
if((i > 0) && ((i % 16) == 0)) { printf("\n"); } if((i > 0) &&((i % 16) == 0)) { printf("\n"); }
printf("%02X ", buf[i] & 0xFF); printf("%02X ", buf[i] & 0xFF);
} }
@ -235,12 +230,11 @@ void hexdump(const uint8_t * buf, size_t len)
void print_n2n_version() { 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-09 - http://www.ntop.org\n\n", "Copyright 2007-18 - ntop.org and contributors\n\n",
n2n_sw_version, n2n_sw_osName, n2n_sw_buildDate); n2n_sw_version, n2n_sw_osName, n2n_sw_buildDate);
} }
/* *********************************************** */
/** Find the peer entry in list with mac_addr equal to mac. /** Find the peer entry in list with mac_addr equal to mac.
* *
@ -248,11 +242,11 @@ void print_n2n_version() {
* *
* @return NULL if not found; otherwise pointer to peer entry. * @return NULL if not found; otherwise pointer to peer entry.
*/ */
struct peer_info * find_peer_by_mac( struct peer_info * list, const n2n_mac_t mac ) struct peer_info * find_peer_by_mac(struct peer_info * list, const n2n_mac_t mac)
{ {
while(list != NULL) while(list != NULL)
{ {
if( 0 == memcmp(mac, list->mac_addr, 6) ) if(0 == memcmp(mac, list->mac_addr, 6))
{ {
return list; return list;
} }
@ -266,11 +260,11 @@ struct peer_info * find_peer_by_mac( struct peer_info * list, const n2n_mac_t ma
/** Return the number of elements in the list. /** Return the number of elements in the list.
* *
*/ */
size_t peer_list_size( const struct peer_info * list ) size_t peer_list_size(const struct peer_info * list)
{ {
size_t retval=0; size_t retval=0;
while ( list ) while(list)
{ {
++retval; ++retval;
list = list->next; list = list->next;
@ -284,8 +278,8 @@ size_t peer_list_size( const struct peer_info * list )
* The item new is added to the head of the list. New is modified during * The item new is added to the head of the list. New is modified during
* insertion. list takes ownership of new. * insertion. list takes ownership of new.
*/ */
void peer_list_add( struct peer_info * * list, void peer_list_add(struct peer_info * * list,
struct peer_info * new ) struct peer_info * new)
{ {
new->next = *list; new->next = *list;
new->last_seen = time(NULL); new->last_seen = time(NULL);
@ -293,7 +287,7 @@ void peer_list_add( struct peer_info * * list,
} }
size_t purge_expired_registrations( struct peer_info ** peer_list ) { size_t purge_expired_registrations(struct peer_info ** peer_list) {
static time_t last_purge = 0; static time_t last_purge = 0;
time_t now = time(NULL); time_t now = time(NULL);
size_t num_reg = 0; size_t num_reg = 0;
@ -302,7 +296,7 @@ size_t purge_expired_registrations( struct peer_info ** peer_list ) {
traceEvent(TRACE_INFO, "Purging old registrations"); traceEvent(TRACE_INFO, "Purging old registrations");
num_reg = purge_peer_list( peer_list, now-REGISTRATION_TIMEOUT ); num_reg = purge_peer_list(peer_list, now-REGISTRATION_TIMEOUT);
last_purge = now; last_purge = now;
traceEvent(TRACE_INFO, "Remove %ld registrations", num_reg); traceEvent(TRACE_INFO, "Remove %ld registrations", num_reg);
@ -311,8 +305,8 @@ size_t purge_expired_registrations( struct peer_info ** peer_list ) {
} }
/** Purge old items from the peer_list and return the number of items that were removed. */ /** 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, size_t purge_peer_list(struct peer_info ** peer_list,
time_t purge_before ) time_t purge_before)
{ {
struct peer_info *scan; struct peer_info *scan;
struct peer_info *prev; struct peer_info *prev;
@ -350,7 +344,7 @@ 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. */ /** 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; struct peer_info *scan;
struct peer_info *prev; struct peer_info *prev;
@ -379,17 +373,17 @@ size_t clear_peer_list( struct peer_info ** peer_list )
return retval; return retval;
} }
static uint8_t hex2byte( const char * s ) static uint8_t hex2byte(const char * s)
{ {
char tmp[3]; char tmp[3];
tmp[0]=s[0]; tmp[0]=s[0];
tmp[1]=s[1]; tmp[1]=s[1];
tmp[2]=0; /* NULL term */ tmp[2]=0; /* NULL term */
return((uint8_t)strtol( tmp, NULL, 16 )); 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; size_t i;
@ -400,7 +394,7 @@ extern int str2mac( uint8_t * outmac /* 6 bytes */, const char * s )
++outmac; ++outmac;
s+=2; /* don't skip colon yet - helps generalise loop. */ s+=2; /* don't skip colon yet - helps generalise loop. */
for (i=1; i<6; ++i ) for(i=1; i<6; ++i)
{ {
s+=1; s+=1;
*outmac=hex2byte(s); *outmac=hex2byte(s);
@ -411,42 +405,46 @@ extern int str2mac( uint8_t * outmac /* 6 bytes */, const char * s )
return 0; /* ok */ return 0; /* ok */
} }
extern char * sock_to_cstr( n2n_sock_str_t out, extern char * sock_to_cstr(n2n_sock_str_t out,
const n2n_sock_t * sock ) const n2n_sock_t * sock) {
{ if(NULL == out) { return NULL; }
int r;
if ( NULL == out ) { return NULL; }
memset(out, 0, N2N_SOCKBUF_SIZE); memset(out, 0, N2N_SOCKBUF_SIZE);
if ( AF_INET6 == sock->family ) if(AF_INET6 == sock->family) {
{
/* INET6 not written yet */ /* INET6 not written yet */
r = snprintf( out, N2N_SOCKBUF_SIZE, "XXXX:%hu", sock->port ); snprintf(out, N2N_SOCKBUF_SIZE, "XXXX:%hu", sock->port);
return out; return out;
} } else {
else
{
const uint8_t * a = sock->addr.v4; const uint8_t * a = sock->addr.v4;
r = snprintf( out, N2N_SOCKBUF_SIZE, "%hu.%hu.%hu.%hu:%hu",
(a[0] & 0xff), (a[1] & 0xff), (a[2] & 0xff), (a[3] & 0xff), sock->port ); 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);
return out; return out;
} }
} }
/* @return zero if the two sockets are equivalent. */ /* @return zero if the two sockets are equivalent. */
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) {
{ if(a->port != b->port) { return 1; }
if ( a->port != b->port ) { return 1; } if(a->family != b->family) { return 1; }
if ( a->family != b->family ) { return 1; }
switch (a->family) /* they are the same */ switch(a->family) {
{
case AF_INET: case AF_INET:
if ( 0 != memcmp( a->addr.v4, b->addr.v4, IPV4_SIZE ) ) { return 1;}; if(0 != memcmp(a->addr.v4, b->addr.v4, IPV4_SIZE)) {
return 1;
}
break; break;
default: default:
if ( 0 != memcmp( a->addr.v6, b->addr.v6, IPV6_SIZE ) ) { return 1;}; if(0 != memcmp(a->addr.v6, b->addr.v6, IPV6_SIZE)) {
return 1;
}
break; break;
} }

123
n2n.h
View File

@ -1,6 +1,5 @@
/* /**
* (C) 2007-09 - Luca Deri <deri@ntop.org> * (C) 2007-18 - ntop.org and contributors
* Richard Andrews <andrews@ntop.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -13,11 +12,7 @@
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/> * along with this program; if not see see <http://www.gnu.org/licenses/>
*
* Code contributions courtesy of:
* Babak Farrokhi <babak@farrokhi.net> [FreeBSD port]
* Lukasz Taczuk
* *
*/ */
@ -36,14 +31,7 @@
tunctl -u UID -t tunX tunctl -u UID -t tunX
*/ */
#if defined(__APPLE__) && defined(__MACH__)
#define _DARWIN_
#endif
/* Some capability defaults which can be reset for particular platforms. */
#define N2N_HAVE_DAEMON 1
#define N2N_HAVE_SETUID 1
/* #define N2N_CAN_NAME_IFACE */ /* #define N2N_CAN_NAME_IFACE */
/* Moved here to define _CRT_SECURE_NO_WARNINGS before all the including takes place */ /* Moved here to define _CRT_SECURE_NO_WARNINGS before all the including takes place */
@ -99,11 +87,6 @@ struct ether_hdr
typedef struct ether_hdr ether_hdr_t; typedef struct ether_hdr ether_hdr_t;
#ifdef __sun__
#include <sys/sysmacros.h> /* MIN() and MAX() declared here */
#undef N2N_HAVE_DAEMON
#endif /* #ifdef __sun__ */
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <netinet/udp.h> #include <netinet/udp.h>
@ -111,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 */
@ -124,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 */
@ -177,6 +164,59 @@ struct peer_info {
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;
};
/* ************************************** */ /* ************************************** */
@ -251,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

@ -1,6 +1,19 @@
/* (c) 2009 Richard Andrews <andrews@ntop.org> */ /**
/* Contributions from: * (C) 2007-18 - ntop.org and contributors
* - Jozef Kralik *
* 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 <http://www.gnu.org/licenses/>
*
*/ */
#include "n2n.h" #include "n2n.h"

View File

@ -1,4 +1,20 @@
/* (c) 2009 Richard Andrews <andrews@ntop.org> */ /**
* (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 <http://www.gnu.org/licenses/>
*
*/
/** Key files /** Key files
* *

View File

@ -1,4 +1,20 @@
/* (c) 2009 Richard Andrews <andrews@ntop.org> */ /**
* (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 <http://www.gnu.org/licenses/>
*
*/
#if !defined(N2N_TRANSFORMS_H_) #if !defined(N2N_TRANSFORMS_H_)
#define N2N_TRANSFORMS_H_ #define N2N_TRANSFORMS_H_
@ -21,8 +37,7 @@
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. */
}; };
@ -48,8 +63,7 @@ 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 */

View File

@ -1,8 +1,19 @@
/* (c) 2009 Richard Andrews <andrews@ntop.org> /**
* (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 <http://www.gnu.org/licenses/>
* *
* Contributions by:
* Luca Deri
* Lukasz Taczuk
*/ */
#if !defined( N2N_WIRE_H_ ) #if !defined( N2N_WIRE_H_ )

View File

@ -28,7 +28,7 @@ PKG_AND_VERSION="${PACKAGE}-${PKG_VERSION}"
TEMPDIR="tmp" TEMPDIR="tmp"
SOURCE_MANIFEST=" SOURCE_MANIFEST="
README README.md
edge.c edge.c
lzoconf.h lzoconf.h
lzodefs.h lzodefs.h

22
sn.c
View File

@ -1,12 +1,22 @@
/* Supernode for n2n-2.x */ /**
* (C) 2007-18 - ntop.org and contributors
/* (c) 2009 Richard Andrews <andrews@ntop.org> *
* 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 <http://www.gnu.org/licenses/>
* *
* Contributions by:
* Lukasz Taczuk
* Struan Bartlett
*/ */
/* Supernode for n2n-2.x */
#include "n2n.h" #include "n2n.h"

18
test.c
View File

@ -1,3 +1,21 @@
/**
* (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 <http://www.gnu.org/licenses/>
*
*/
#include "n2n.h" #include "n2n.h"
#include "n2n_keyfile.h" #include "n2n_keyfile.h"
#include <assert.h> #include <assert.h>

View File

@ -1,6 +1,19 @@
/* (c) 2009 Richard Andrews <andrews@ntop.org> */ /**
/* Contributions from: * (C) 2007-18 - ntop.org and contributors
* - Jozef Kralik *
* 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 <http://www.gnu.org/licenses/>
*
*/ */
#include "n2n.h" #include "n2n.h"

View File

@ -1,4 +1,20 @@
/* (c) 2009 Richard Andrews <andrews@ntop.org> */ /**
* (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 <http://www.gnu.org/licenses/>
*
*/
#include "n2n.h" #include "n2n.h"
#include "n2n_transforms.h" #include "n2n_transforms.h"

View File

@ -1,4 +1,20 @@
/* (c) 2009 Richard Andrews <andrews@ntop.org> */ /**
* (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 <http://www.gnu.org/licenses/>
*
*/
#include "n2n.h" #include "n2n.h"
#include "n2n_transforms.h" #include "n2n_transforms.h"

View File

@ -1,5 +1,5 @@
/* /**
* (C) 2007-09 - Luca Deri <deri@ntop.org> * (C) 2007-18 - ntop.org and contributors
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -13,6 +13,7 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/> * along with this program; if not see see <http://www.gnu.org/licenses/>
*
*/ */
#include "n2n.h" #include "n2n.h"

View File

@ -1,5 +1,5 @@
/* /**
* (C) 2007-09 - Luca Deri <deri@ntop.org> * (C) 2007-18 - ntop.org and contributors
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -12,13 +12,16 @@
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/> * along with this program; if not see see <http://www.gnu.org/licenses/>
*/ *
*/
#include "n2n.h" #include "n2n.h"
#ifdef __linux__ #ifdef __linux__
/* *************************************************** */
static void read_mac(char *ifname, n2n_mac_t mac_addr) { static void read_mac(char *ifname, n2n_mac_t mac_addr) {
int _sock, res; int _sock, res;
struct ifreq ifr; struct ifreq ifr;
@ -30,8 +33,10 @@ static void read_mac(char *ifname, n2n_mac_t mac_addr) {
_sock=socket(PF_INET, SOCK_DGRAM, 0); _sock=socket(PF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, ifname); strcpy(ifr.ifr_name, ifname);
res = ioctl(_sock,SIOCGIFHWADDR,&ifr); res = ioctl(_sock,SIOCGIFHWADDR,&ifr);
if (res<0) {
if(res < 0) {
perror ("Get hw addr"); perror ("Get hw addr");
traceEvent(TRACE_ERROR, "Unable to read interfce %s MAC", ifname);
} else } else
memcpy(mac_addr, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6); memcpy(mac_addr, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6);
@ -120,22 +125,29 @@ int tuntap_open(tuntap_dev *device,
return(device->fd); return(device->fd);
} }
/* *************************************************** */
int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(read(tuntap->fd, buf, len)); return(read(tuntap->fd, buf, len));
} }
/* *************************************************** */
int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(write(tuntap->fd, buf, len)); return(write(tuntap->fd, buf, len));
} }
/* *************************************************** */
void tuntap_close(struct tuntap_dev *tuntap) { void tuntap_close(struct tuntap_dev *tuntap) {
close(tuntap->fd); close(tuntap->fd);
} }
/* *************************************************** */
/* Fill out the ip_addr value from the interface. Called to pick up dynamic /* Fill out the ip_addr value from the interface. Called to pick up dynamic
* address changes. */ * address changes. */
void tuntap_get_address(struct tuntap_dev *tuntap) void tuntap_get_address(struct tuntap_dev *tuntap) {
{
FILE * fp=NULL; FILE * fp=NULL;
ssize_t nread=0; ssize_t nread=0;
char buf[N2N_LINUX_SYSTEMCMD_SIZE]; char buf[N2N_LINUX_SYSTEMCMD_SIZE];
@ -145,20 +157,24 @@ void tuntap_get_address(struct tuntap_dev *tuntap)
/* If the interface has no address (0.0.0.0) there will be no inet addr /* If the interface has no address (0.0.0.0) there will be no inet addr
* line and the returned string will be empty. */ * line and the returned string will be empty. */
snprintf( buf, sizeof(buf), "/sbin/ifconfig %s | /bin/sed -e '/inet addr:/!d' -e 's/^.*inet addr://' -e 's/ .*$//'", snprintf( buf, sizeof(buf),
tuntap->dev_name ); "/sbin/ifconfig %s | /bin/sed -e '/inet addr:/!d' -e 's/^.*inet addr://' -e 's/ .*$//'",
fp=popen(buf, "r"); tuntap->dev_name);
if (fp ) fp = popen(buf, "r");
{
memset(buf,0,N2N_LINUX_SYSTEMCMD_SIZE); /* make sure buf is NULL terminated. */ if (fp) {
nread=fread(buf, 1, 15, fp); memset(buf, 0, N2N_LINUX_SYSTEMCMD_SIZE); /* make sure buf is NULL terminated. */
nread = fread(buf, N2N_LINUX_SYSTEMCMD_SIZE-1, 1, fp);
fclose(fp); fclose(fp);
fp=NULL; fp = NULL;
traceEvent(TRACE_INFO, "ifconfig address = %s", buf); traceEvent(TRACE_INFO, "ifconfig address = %s", buf);
if(nread > 0) {
buf[nread] = '\0';
tuntap->ip_addr = inet_addr(buf); tuntap->ip_addr = inet_addr(buf);
} }
}
} }

View File

@ -1,6 +1,5 @@
/* /**
* (C) 2007-09 - Luca Deri <deri@ntop.org> * (C) 2007-18 - ntop.org and contributors
* (C) 2009 - Alaric Snell-Pym <alaric@kitten-technologies.co.uk>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -14,6 +13,7 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/> * along with this program; if not see see <http://www.gnu.org/licenses/>
*
*/ */
#include "n2n.h" #include "n2n.h"
@ -39,7 +39,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
char tap_device[N2N_NETBSD_TAPDEVICE_SIZE]; char tap_device[N2N_NETBSD_TAPDEVICE_SIZE];
struct ifreq req; struct ifreq req;
if (dev) { if(dev) {
snprintf(tap_device, sizeof(tap_device), "/dev/%s", dev); snprintf(tap_device, sizeof(tap_device), "/dev/%s", dev);
device->fd = open(tap_device, O_RDWR); device->fd = open(tap_device, O_RDWR);
snprintf(tap_device, sizeof(tap_device), "%s", dev); snprintf(tap_device, sizeof(tap_device), "%s", dev);
@ -47,7 +47,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
else { else {
device->fd = open("/dev/tap", O_RDWR); device->fd = open("/dev/tap", O_RDWR);
if(device->fd >= 0) { if(device->fd >= 0) {
if (ioctl(device->fd, TAPGIFNAME, &req) == -1) { if(ioctl(device->fd, TAPGIFNAME, &req) == -1) {
traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s)", strerror(errno)); traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s)", strerror(errno));
close(device->fd); close(device->fd);
return(-1); return(-1);
@ -62,46 +62,45 @@ int tuntap_open(tuntap_dev *device /* ignored */,
traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno)); traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno));
return(-1); return(-1);
} else { } else {
char buf[256]; char cmd[256];
FILE *fd; FILE *fd;
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device);
device->ip_addr = inet_addr(device_ip); device->ip_addr = inet_addr(device_ip);
if ( device_mac && device_mac[0] != '\0' ) if( device_mac && device_mac[0] != '\0') {
{
/* Set the hw address before bringing the if up. */ /* Set the hw address before bringing the if up. */
snprintf(buf, sizeof(buf), "ifconfig %s link %s active", snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active",
tap_device, device_mac); tap_device, device_mac);
system(buf); system(cmd);
} }
snprintf(buf, sizeof(buf), "ifconfig %s %s netmask %s mtu %d up", snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up",
tap_device, device_ip, device_mask, mtu); tap_device, device_ip, device_mask, mtu);
system(buf); system(cmd);
traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)",
tap_device, device_ip, device_mask); tap_device, device_ip, device_mask);
/* Read MAC address */ /* Read MAC address */
snprintf(cmd, sizeof(cmd), "ifconfig %s |grep address|cut -c 11-28", tap_device);
/* traceEvent(TRACE_INFO, "%s", cmd); */
snprintf(buf, sizeof(buf), "ifconfig %s |grep address|cut -c 11-28", tap_device); fd = popen(cmd, "r");
/* traceEvent(TRACE_INFO, "%s", buf); */
fd = popen(buf, "r");
if(fd < 0) { if(fd < 0) {
tun_close(device); tun_close(device);
return(-1); return(-1);
} else { } else {
int a, b, c, d, e, f; int a, b, c, d, e, f;
char buf[256];
buf[0] = 0; buf[0] = 0;
fgets(buf, sizeof(buf), fd); fgets(buf, sizeof(buf), fd);
pclose(fd); pclose(fd);
if(buf[0] == '\0') { if(buf[0] == '\0') {
traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address", tap_device); traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address [%s]", tap_device, cmd);
exit(0); exit(0);
} }

View File

@ -1,5 +1,5 @@
/* /**
* (C) 2007-09 - Luca Deri <deri@ntop.org> * (C) 2007-18 - ntop.org and contributors
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -13,11 +13,12 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/> * along with this program; if not see see <http://www.gnu.org/licenses/>
*
*/ */
#include "n2n.h" #include "n2n.h"
#ifdef _DARWIN_ #ifdef __APPLE__
void tun_close(tuntap_dev *device); void tun_close(tuntap_dev *device);
@ -77,7 +78,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
fd = popen(buf, "r"); fd = popen(buf, "r");
if(fd < 0) { if(fd < 0) {
tun_close(device); tuntap_close(device);
return(-1); return(-1);
} else { } else {
int a, b, c, d, e, f; int a, b, c, d, e, f;
@ -129,4 +130,4 @@ void tuntap_get_address(struct tuntap_dev *tuntap)
{ {
} }
#endif /* _DARWIN_ */ #endif /* __APPLE__ */

18
wire.c
View File

@ -1,4 +1,20 @@
/* (c) 2009 Richard Andrews <andrews@ntop.org> */ /**
* (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 <http://www.gnu.org/licenses/>
*
*/
/** Routines for encoding and decoding n2n packets on the wire. /** Routines for encoding and decoding n2n packets on the wire.
* *