Browse Source

Code reorganization

pull/271/head
Luca Deri 4 years ago
parent
commit
3eb82b2a32
  1. 26
      Makefile.in
  2. 4
      configure.seed
  3. 34
      include/edge_utils_win32.h
  4. 8
      include/header_encryption.h
  5. 0
      include/lzoconf.h
  6. 0
      include/lzodefs.h
  7. 0
      include/minilzo.h
  8. 109
      include/n2n.h
  9. 94
      include/n2n_define.h
  10. 0
      include/n2n_transforms.h
  11. 0
      include/n2n_wire.h
  12. 0
      include/pearson.h
  13. 0
      include/portable_endian.h
  14. 38
      include/random_numbers.h
  15. 0
      include/speck.h
  16. 0
      include/twofish.h
  17. 0
      include/uthash.h
  18. 34
      random_numbers.h
  19. 6
      src/edge.c
  20. 89
      src/edge_utils.c
  21. 49
      src/edge_utils_win32.c
  22. 0
      src/example_edge_embed.c
  23. 1
      src/example_edge_embed_quick_edge_init.c
  24. 0
      src/example_sn_embed.c
  25. 12
      src/header_encryption.c
  26. 0
      src/minilzo.c
  27. 0
      src/n2n.c
  28. 0
      src/pearson.c
  29. 23
      src/random_numbers.c
  30. 0
      src/sn.c
  31. 0
      src/sn_utils.c
  32. 0
      src/speck.c
  33. 2
      src/transform_aes.c
  34. 2
      src/transform_cc20.c
  35. 0
      src/transform_null.c
  36. 4
      src/transform_speck.c
  37. 7
      src/transform_tf.c
  38. 0
      src/tuntap_freebsd.c
  39. 9
      src/tuntap_linux.c
  40. 0
      src/tuntap_netbsd.c
  41. 0
      src/tuntap_osx.c
  42. 8
      src/twofish.c
  43. 0
      src/wire.c
  44. 4
      tools/Makefile.in

26
Makefile.in

@ -5,10 +5,7 @@ GIT_COMMITS=@GIT_COMMITS@
######## ########
CC?=gcc CC=@CC@
DEBUG?=-g3
OPTIMIZATION?=-O3 #-march=native
WARN?=-Wall
#Ultrasparc64 users experiencing SIGBUS should try the following gcc options #Ultrasparc64 users experiencing SIGBUS should try the following gcc options
#(thanks to Robert Gibbon) #(thanks to Robert Gibbon)
@ -16,7 +13,7 @@ PLATOPTS_SPARC64=-mcpu=ultrasparc -pipe -fomit-frame-pointer -ffast-math -finlin
N2N_OBJS_OPT= N2N_OBJS_OPT=
LIBS_EDGE_OPT=@N2N_LIBS@ LIBS_EDGE_OPT=@N2N_LIBS@
CFLAGS=@CFLAGS@ CFLAGS=@CFLAGS@ -I ./include
LDFLAGS=@LDFLAGS@ LDFLAGS=@LDFLAGS@
OPENSSL_CFLAGS=$(shell pkg-config openssl; echo $$?) OPENSSL_CFLAGS=$(shell pkg-config openssl; echo $$?)
@ -48,12 +45,9 @@ MAN7DIR=$(MANDIR)/man7
MAN8DIR=$(MANDIR)/man8 MAN8DIR=$(MANDIR)/man8
N2N_LIB=libn2n.a N2N_LIB=libn2n.a
N2N_OBJS=n2n.o wire.o minilzo.o twofish.o speck.o \ N2N_OBJS=$(patsubst src/%.c, src/%.o, $(wildcard src/*.c))
edge_utils.o sn_utils.o \ N2N_DEPS=$(wildcard include/*.h) $(wildcard src/*.c) Makefile
transform_null.o transform_tf.o transform_aes.o transform_cc20.o transform_speck.o \
header_encryption.o pearson.o \
tuntap_freebsd.o tuntap_netbsd.o tuntap_linux.o random_numbers.o \
tuntap_osx.o
LIBS_EDGE+=$(LIBS_EDGE_OPT) LIBS_EDGE+=$(LIBS_EDGE_OPT)
LIBS_SN= LIBS_SN=
@ -77,19 +71,19 @@ all: $(APPS) $(DOCS) tools
tools: $(N2N_LIB) tools: $(N2N_LIB)
$(MAKE) -C $@ $(MAKE) -C $@
edge: edge.c $(N2N_LIB) n2n_wire.h n2n.h Makefile edge: src/edge.c $(N2N_LIB) $(N2N_DEPS)
$(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_EDGE) -o $@ $(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_EDGE) -o $@
supernode: sn.c $(N2N_LIB) n2n.h Makefile supernode: src/sn.c $(N2N_LIB) $(N2N_DEPS)
$(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_SN) -o $@ $(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_SN) -o $@
example_edge_embed_quick_edge_init: example_edge_embed_quick_edge_init.c $(N2N_LIB) n2n.h example_edge_embed_quick_edge_init: src/example_edge_embed_quick_edge_init.c $(N2N_DEPS)
$(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_EDGE) -o $@ $(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_EDGE) -o $@
example_sn_embed: example_sn_embed.c $(N2N_LIB) n2n.h example_sn_embed: src/example_sn_embed.c $(N2N_DEPS)
$(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_EDGE) -o $@ $(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_EDGE) -o $@
example_edge_embed: example_edge_embed.c $(N2N_LIB) n2n.h example_edge_embed: src/example_edge_embed.c $(N2N_DEPS)
$(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_EDGE) -o $@ $(CC) $(CFLAGS) $< $(LDFLAGS) $(N2N_LIB) $(LIBS_EDGE) -o $@
%.gz : % %.gz : %

4
configure.seed

@ -15,6 +15,7 @@ fi
N2N_LIBS= N2N_LIBS=
AC_PROG_CC([clang])
AC_CHECK_LIB([zstd], [ZSTD_compress]) AC_CHECK_LIB([zstd], [ZSTD_compress])
if test "x$ac_cv_lib_zstd_ZSTD_compress" != xyes; then if test "x$ac_cv_lib_zstd_ZSTD_compress" != xyes; then
@ -92,6 +93,7 @@ fi
DATE=`date +"%Y-%m-%d"` DATE=`date +"%Y-%m-%d"`
AC_SUBST(CC)
AC_SUBST(CFLAGS) AC_SUBST(CFLAGS)
AC_SUBST(LDFLAGS) AC_SUBST(LDFLAGS)
AC_SUBST(N2N_MAJOR) AC_SUBST(N2N_MAJOR)
@ -104,7 +106,7 @@ AC_SUBST(GIT_RELEASE)
AC_SUBST(N2N_DEFINES) AC_SUBST(N2N_DEFINES)
AC_SUBST(N2N_LIBS) AC_SUBST(N2N_LIBS)
AC_SUBST(ADDITIONAL_TOOLS) AC_SUBST(ADDITIONAL_TOOLS)
AC_CONFIG_HEADERS(config.h) AC_CONFIG_HEADERS(include/config.h)
AC_CONFIG_FILES(Makefile) AC_CONFIG_FILES(Makefile)
AC_CONFIG_FILES(tools/Makefile) AC_CONFIG_FILES(tools/Makefile)

34
include/edge_utils_win32.h

@ -0,0 +1,34 @@
/**
* (C) 2007-20 - 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/>
*
*/
#ifdef WIN32
#include <process.h>
/* Multicast peers discovery disabled due to https://github.com/ntop/n2n/issues/65 */
#define SKIP_MULTICAST_PEERS_DISCOVERY
struct tunread_arg {
n2n_edge_t *eee;
int *keep_running;
};
extern HANDLE startTunReadThread(struct tunread_arg *arg);
#endif

8
header_encryption.h → include/header_encryption.h

@ -16,11 +16,6 @@
* *
*/ */
#include <stdint.h>
#include "n2n.h"
#include "speck.h"
/* Header encryption indicators */ /* Header encryption indicators */
@ -28,15 +23,12 @@
#define HEADER_ENCRYPTION_NONE 1 #define HEADER_ENCRYPTION_NONE 1
#define HEADER_ENCRYPTION_ENABLED 2 #define HEADER_ENCRYPTION_ENABLED 2
uint32_t packet_header_decrypt (uint8_t packet[], uint8_t packet_len, uint32_t packet_header_decrypt (uint8_t packet[], uint8_t packet_len,
char * community_name, he_context_t * ctx); char * community_name, he_context_t * ctx);
int8_t packet_header_decrypt_if_required (uint8_t packet[], uint16_t packet_len, int8_t packet_header_decrypt_if_required (uint8_t packet[], uint16_t packet_len,
struct sn_community * communities); struct sn_community * communities);
int32_t packet_header_encrypt (uint8_t packet[], uint8_t header_len, he_context_t * ctx); int32_t packet_header_encrypt (uint8_t packet[], uint8_t header_len, he_context_t * ctx);

0
lzoconf.h → include/lzoconf.h

0
lzodefs.h → include/lzodefs.h

0
minilzo.h → include/minilzo.h

109
n2n.h → include/n2n.h

@ -64,6 +64,8 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdint.h>
#include <time.h>
#ifndef WIN32 #ifndef WIN32
#include <unistd.h> #include <unistd.h>
@ -74,6 +76,16 @@
#ifdef __linux__ #ifdef __linux__
#define N2N_CAN_NAME_IFACE 1 #define N2N_CAN_NAME_IFACE 1
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#define GRND_NONBLOCK 1
#endif /* #ifdef __linux__ */ #endif /* #ifdef __linux__ */
#ifdef __FreeBSD__ #ifdef __FreeBSD__
@ -83,7 +95,12 @@
#include <syslog.h> #include <syslog.h>
#include <sys/wait.h> #include <sys/wait.h>
#if defined (__RDRND__) || defined (__RDSEED__)
#include <immintrin.h>
#endif
#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];
@ -93,10 +110,17 @@ struct ether_hdr
typedef struct ether_hdr ether_hdr_t; typedef struct ether_hdr ether_hdr_t;
#ifdef HAVE_LIBZSTD
#include <zstd.h>
#endif
#ifdef __ANDROID_NDK__ #ifdef __ANDROID_NDK__
#undef N2N_HAVE_DAEMON #undef N2N_HAVE_DAEMON
#undef N2N_HAVE_SETUID #undef N2N_HAVE_SETUID
#undef N2N_CAN_NAME_IFACE #undef N2N_CAN_NAME_IFACE
#include "android/edge_android.h"
#include <tun2tap/tun2tap.h>
#define ARP_PERIOD_INTERVAL (10) /* sec */
#endif /* #ifdef __ANDROID_NDK__ */ #endif /* #ifdef __ANDROID_NDK__ */
#include <netinet/in.h> #include <netinet/in.h>
@ -106,27 +130,37 @@ 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 <string.h>
#include <assert.h> #include <assert.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdint.h>
#include <openssl/opensslv.h> #include <openssl/opensslv.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include "minilzo.h" #include "minilzo.h"
#include "n2n_define.h"
#define closesocket(a) close(a) #define closesocket(a) close(a)
#endif /* #ifndef WIN32 */ #endif /* #ifndef WIN32 */
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include "uthash.h" #include "uthash.h"
#include "lzoconf.h"
#ifdef WIN32 #ifdef WIN32
#include "win32/wintap.h" #include "win32/wintap.h"
#include <sys/stat.h>
#else
#include <pwd.h>
#endif /* #ifdef WIN32 */ #endif /* #ifdef WIN32 */
#include "n2n_wire.h" #include "n2n_wire.h"
#include "n2n_transforms.h" #include "n2n_transforms.h"
#include "random_numbers.h"
#include "pearson.h"
#include "portable_endian.h"
#include "speck.h"
#ifdef WIN32 #ifdef WIN32
#define N2N_IFNAMSIZ 64 #define N2N_IFNAMSIZ 64
@ -147,43 +181,6 @@ typedef struct tuntap_dev {
#define SOCKET int #define SOCKET int
#endif /* #ifndef WIN32 */ #endif /* #ifndef WIN32 */
#define QUICKLZ 1
/* N2N packet header indicators. */
#define MSG_TYPE_REGISTER 1
#define MSG_TYPE_DEREGISTER 2
#define MSG_TYPE_PACKET 3
#define MSG_TYPE_REGISTER_ACK 4
#define MSG_TYPE_REGISTER_SUPER 5
#define MSG_TYPE_REGISTER_SUPER_ACK 6
#define MSG_TYPE_REGISTER_SUPER_NAK 7
#define MSG_TYPE_FEDERATION 8
#define MSG_TYPE_PEER_INFO 9
#define MSG_TYPE_QUERY_PEER 10
#define MSG_TYPE_MAX_TYPE 10
/* N2N compression indicators. */
/* Compression is disabled by default for outgoing packets if no cli
* option is given. All edges are built with decompression support so
* they are able to understand each other (this applies to lzo only). */
#define N2N_COMPRESSION_ID_NONE 0 /* default, see edge_init_conf_defaults(...) in edge_utils.c */
#define N2N_COMPRESSION_ID_LZO 1 /* set if '-z1' or '-z' cli option is present, see setOption(...) in edge.c */
#ifdef N2N_HAVE_ZSTD
#define N2N_COMPRESSION_ID_ZSTD 2 /* set if '-z2' cli option is present, available only if compiled with zstd lib */
#define ZSTD_COMPRESSION_LEVEL 7 /* 1 (faster) ... 22 (more compression) */
#endif
// with the next major packet structure update, make '0' = invalid, and '1' = no compression
// '2' = LZO, '3' = ZSTD, ... REVISIT then (also: change all occurences in source).
#define N2N_COMPRESSION_ID_BITLEN 3 /* number of bits used for encoding compression id in the uppermost
bits of transform_id; will be obsolete as soon as compression gets
its own field in the packet. REVISIT then. */
/* forward delcaration of header encryption context, see 'header_encryption.h' */
typedef struct speck_context_t he_context_t;
#define DEFAULT_MTU 1290
/** Uncomment this to enable the MTU check, then try to ssh to generate a fragmented packet. */ /** Uncomment this to enable the MTU check, then try to ssh to generate a fragmented packet. */
/** NOTE: see doc/MTU.md for an explanation on the 1400 value */ /** NOTE: see doc/MTU.md for an explanation on the 1400 value */
//#define MTU_ASSERT_VALUE 1400 //#define MTU_ASSERT_VALUE 1400
@ -206,17 +203,7 @@ struct peer_info {
UT_hash_handle hh; /* makes this structure hashable */ UT_hash_handle hh; /* makes this structure hashable */
}; };
#define HASH_ADD_PEER(head,add) \ typedef struct speck_context_t he_context_t;
HASH_ADD(hh,head,mac_addr,sizeof(n2n_mac_t),add)
#define HASH_FIND_PEER(head,mac,out) \
HASH_FIND(hh,head,mac,sizeof(n2n_mac_t),out)
#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_EDGE_MGMT_PORT 5644
typedef char n2n_sn_name_t[N2N_EDGE_SN_HOST_SIZE]; typedef char n2n_sn_name_t[N2N_EDGE_SN_HOST_SIZE];
typedef struct n2n_route { typedef struct n2n_route {
@ -261,7 +248,7 @@ typedef struct sn_stats
time_t last_reg_super; /* Time when last REGISTER_SUPER was received. */ time_t last_reg_super; /* Time when last REGISTER_SUPER was received. */
} sn_stats_t; } sn_stats_t;
struct sn_community struct sn_community
{ {
char community[N2N_COMMUNITY_SIZE]; char community[N2N_COMMUNITY_SIZE];
uint8_t header_encryption; /* Header encryption indicator. */ uint8_t header_encryption; /* Header encryption indicator. */
@ -271,7 +258,7 @@ typedef struct sn_stats
UT_hash_handle hh; /* makes this structure hashable */ UT_hash_handle hh; /* makes this structure hashable */
}; };
typedef struct n2n_sn typedef struct n2n_sn
{ {
time_t start_time; /* Used to measure uptime. */ time_t start_time; /* Used to measure uptime. */
sn_stats_t stats; sn_stats_t stats;
@ -285,6 +272,9 @@ typedef struct sn_stats
/* ************************************** */ /* ************************************** */
#include "header_encryption.h"
#include "twofish.h"
#ifdef __ANDROID_NDK__ #ifdef __ANDROID_NDK__
#include <android/log.h> #include <android/log.h>
#endif /* #ifdef __ANDROID_NDK__ */ #endif /* #ifdef __ANDROID_NDK__ */
@ -298,21 +288,6 @@ typedef struct sn_stats
/* ************************************** */ /* ************************************** */
#define SUPERNODE_IP "127.0.0.1"
#define SUPERNODE_PORT 1234
/* ************************************** */
#ifndef max
#define max(a, b) ((a < b) ? b : a)
#endif
#ifndef min
#define min(a, b) ((a > b) ? b : a)
#endif
/* ************************************** */
/* Transop Init Functions */ /* Transop Init Functions */
int n2n_transop_null_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); int n2n_transop_null_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);
int n2n_transop_twofish_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt); int n2n_transop_twofish_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);

94
include/n2n_define.h

@ -0,0 +1,94 @@
/**
* (C) 2007-20 - 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/>
*
*/
#define QUICKLZ 1
/* N2N packet header indicators. */
#define MSG_TYPE_REGISTER 1
#define MSG_TYPE_DEREGISTER 2
#define MSG_TYPE_PACKET 3
#define MSG_TYPE_REGISTER_ACK 4
#define MSG_TYPE_REGISTER_SUPER 5
#define MSG_TYPE_REGISTER_SUPER_ACK 6
#define MSG_TYPE_REGISTER_SUPER_NAK 7
#define MSG_TYPE_FEDERATION 8
#define MSG_TYPE_PEER_INFO 9
#define MSG_TYPE_QUERY_PEER 10
#define MSG_TYPE_MAX_TYPE 10
#define SOCKET_TIMEOUT_INTERVAL_SECS 10
#define REGISTER_SUPER_INTERVAL_DFL 20 /* sec, usually UDP NAT entries in a firewall expire after 30 seconds */
#define IFACE_UPDATE_INTERVAL (30) /* sec. How long it usually takes to get an IP lease. */
#define TRANSOP_TICK_INTERVAL (10) /* sec */
#define ETH_FRAMESIZE 14
#define IP4_SRCOFFSET 12
#define IP4_DSTOFFSET 16
#define IP4_MIN_SIZE 20
#define UDP_SIZE 8
/* heap allocation for compression as per lzo example doc */
#define HEAP_ALLOC(var,size) lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
/* N2N compression indicators. */
/* Compression is disabled by default for outgoing packets if no cli
* option is given. All edges are built with decompression support so
* they are able to understand each other (this applies to lzo only). */
#define N2N_COMPRESSION_ID_NONE 0 /* default, see edge_init_conf_defaults(...) in edge_utils.c */
#define N2N_COMPRESSION_ID_LZO 1 /* set if '-z1' or '-z' cli option is present, see setOption(...) in edge.c */
#ifdef N2N_HAVE_ZSTD
#define N2N_COMPRESSION_ID_ZSTD 2 /* set if '-z2' cli option is present, available only if compiled with zstd lib */
#define ZSTD_COMPRESSION_LEVEL 7 /* 1 (faster) ... 22 (more compression) */
#endif
// with the next major packet structure update, make '0' = invalid, and '1' = no compression
// '2' = LZO, '3' = ZSTD, ... REVISIT then (also: change all occurences in source).
#define N2N_COMPRESSION_ID_BITLEN 3 /* number of bits used for encoding compression id in the uppermost
bits of transform_id; will be obsolete as soon as compression gets
its own field in the packet. REVISIT then. */
#define DEFAULT_MTU 1290
#define HASH_ADD_PEER(head,add) \
HASH_ADD(hh,head,mac_addr,sizeof(n2n_mac_t),add)
#define HASH_FIND_PEER(head,mac,out) \
HASH_FIND(hh,head,mac,sizeof(n2n_mac_t),out)
#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_EDGE_MGMT_PORT 5644
/* ************************************** */
#define SUPERNODE_IP "127.0.0.1"
#define SUPERNODE_PORT 1234
/* ************************************** */
#ifndef max
#define max(a, b) ((a < b) ? b : a)
#endif
#ifndef min
#define min(a, b) ((a > b) ? b : a)
#endif

0
n2n_transforms.h → include/n2n_transforms.h

0
n2n_wire.h → include/n2n_wire.h

0
pearson.h → include/pearson.h

0
portable_endian.h → include/portable_endian.h

38
include/random_numbers.h

@ -0,0 +1,38 @@
/**
* (C) 2007-20 - 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/>
*
*/
/* The WIN32 code is still untested and thus commented
#if defined (WIN32)
#include <Wincrypt.h>
#endif
*/
struct rn_generator_state_t {
uint64_t a, b;
};
struct splitmix64_state_t {
uint64_t s;
};
int n2n_srand (uint64_t seed);
uint64_t n2n_rand ();
uint64_t n2n_seed ();

0
speck.h → include/speck.h

0
twofish.h → include/twofish.h

0
uthash.h → include/uthash.h

34
random_numbers.h

@ -1,34 +0,0 @@
#include <stdint.h>
#include <time.h>
#if defined (__linux__)
#include <sys/syscall.h>
#include <unistd.h>
#define GRND_NONBLOCK 1
#endif
#if defined (__RDRND__) || defined (__RDSEED__)
#include <immintrin.h>
#endif
/* The WIN32 code is still untested and thus commented
#if defined (WIN32)
#include <Wincrypt.h>
#endif */
struct rn_generator_state_t {
uint64_t a, b;
};
struct splitmix64_state_t {
uint64_t s;
};
int n2n_srand (uint64_t seed);
uint64_t n2n_rand ();
uint64_t n2n_seed ();

6
edge.c → src/edge.c

@ -17,12 +17,6 @@
*/ */
#include "n2n.h" #include "n2n.h"
#include "random_numbers.h"
#ifdef WIN32
#include <sys/stat.h>
#else
#include <pwd.h>
#endif
#define N2N_NETMASK_STR_SIZE 16 /* dotted decimal 12 numbers + 3 dots */ #define N2N_NETMASK_STR_SIZE 16 /* dotted decimal 12 numbers + 3 dots */
#define N2N_MACNAMSIZ 18 /* AA:BB:CC:DD:EE:FF + NULL*/ #define N2N_MACNAMSIZ 18 /* AA:BB:CC:DD:EE:FF + NULL*/

89
edge_utils.c → src/edge_utils.c

@ -17,49 +17,6 @@
*/ */
#include "n2n.h" #include "n2n.h"
#include "lzoconf.h"
#include "random_numbers.h"
#ifdef HAVE_LIBZSTD
#include <zstd.h>
#endif
#ifdef WIN32
#include <process.h>
/* Multicast peers discovery disabled due to https://github.com/ntop/n2n/issues/65 */
#define SKIP_MULTICAST_PEERS_DISCOVERY
#endif
#ifdef __ANDROID_NDK__
#include "android/edge_android.h"
#include <tun2tap/tun2tap.h>
#endif /* __ANDROID_NDK__ */
#define SOCKET_TIMEOUT_INTERVAL_SECS 10
#define REGISTER_SUPER_INTERVAL_DFL 20 /* sec, usually UDP NAT entries in a firewall expire after 30 seconds */
#define IFACE_UPDATE_INTERVAL (30) /* sec. How long it usually takes to get an IP lease. */
#define TRANSOP_TICK_INTERVAL (10) /* sec */
#ifdef __ANDROID_NDK__
#define ARP_PERIOD_INTERVAL (10) /* sec */
#endif
#ifdef __linux__
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#endif
#define ETH_FRAMESIZE 14
#define IP4_SRCOFFSET 12
#define IP4_DSTOFFSET 16
#define IP4_MIN_SIZE 20
#define UDP_SIZE 8
/* heap allocation for compression as per lzo example doc */
#define HEAP_ALLOC(var,size) lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
/* ************************************** */ /* ************************************** */
@ -306,11 +263,11 @@ n2n_edge_t* edge_init(const tuntap_dev *dev, const n2n_edge_conf_t *conf, int *r
goto edge_init_error; goto edge_init_error;
} }
//edge_init_success: //edge_init_success:
*rv = 0; *rv = 0;
return(eee); return(eee);
edge_init_error: edge_init_error:
if(eee) if(eee)
free(eee); free(eee);
*rv = rc; *rv = rc;
@ -1517,9 +1474,7 @@ static void readFromTAPSocket(n2n_edge_t * eee) {
len = uip_arp_len; len = uip_arp_len;
memcpy(eth_pkt, uip_arp_buf, MIN(uip_arp_len, N2N_PKT_BUF_SIZE)); memcpy(eth_pkt, uip_arp_buf, MIN(uip_arp_len, N2N_PKT_BUF_SIZE));
traceEvent(TRACE_DEBUG, "ARP reply packet to send"); traceEvent(TRACE_DEBUG, "ARP reply packet to send");
} } else {
else
{
#endif /* #ifdef __ANDROID_NDK__ */ #endif /* #ifdef __ANDROID_NDK__ */
len = tuntap_read( &(eee->device), eth_pkt, N2N_PKT_BUF_SIZE ); len = tuntap_read( &(eee->device), eth_pkt, N2N_PKT_BUF_SIZE );
#ifdef __ANDROID_NDK__ #ifdef __ANDROID_NDK__
@ -1554,40 +1509,6 @@ static void readFromTAPSocket(n2n_edge_t * eee) {
/* ************************************** */ /* ************************************** */
#ifdef WIN32
struct tunread_arg {
n2n_edge_t *eee;
int *keep_running;
};
static DWORD* tunReadThread(LPVOID lpArg) {
struct tunread_arg *arg = (struct tunread_arg*)lpArg;
while(*arg->keep_running)
readFromTAPSocket(arg->eee);
return((DWORD*)NULL);
}
/* ************************************** */
/** Start a second thread in Windows because TUNTAP interfaces do not expose
* file descriptors. */
static HANDLE startTunReadThread(struct tunread_arg *arg) {
DWORD dwThreadId;
return(CreateThread(NULL, /* security attributes */
0, /* use default stack size */
(LPTHREAD_START_ROUTINE)tunReadThread, /* thread function */
(void*)arg, /* argument to thread function */
0, /* thread creation flags */
&dwThreadId)); /* thread id out */
}
#endif
/* ************************************** */
/** Read a datagram from the main UDP socket to the internet. */ /** Read a datagram from the main UDP socket to the internet. */
static void readFromIPSocket(n2n_edge_t * eee, int in_sock) { static void readFromIPSocket(n2n_edge_t * eee, int in_sock) {
n2n_common_t cmn; /* common fields in the packet header */ n2n_common_t cmn; /* common fields in the packet header */
@ -2301,7 +2222,7 @@ static int routectl(int cmd, int flags, n2n_route_t *route, int if_idx) {
traceEvent(TRACE_DEBUG, route_cmd_to_str(cmd, route, route_buf, sizeof(route_buf))); traceEvent(TRACE_DEBUG, route_cmd_to_str(cmd, route, route_buf, sizeof(route_buf)));
rv = 0; rv = 0;
out: out:
close(nl_sock); close(nl_sock);
return(rv); return(rv);
@ -2495,7 +2416,7 @@ int quick_edge_init(char *device_name, char *community_name,
edge_term(eee); edge_term(eee);
edge_term_conf(&conf); edge_term_conf(&conf);
quick_edge_init_end: quick_edge_init_end:
tuntap_close(&tuntap); tuntap_close(&tuntap);
return(rv); return(rv);
} }

49
src/edge_utils_win32.c

@ -0,0 +1,49 @@
/**
* (C) 2007-20 - 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/>
*
*/
#ifdef WIN32
#include "edge_utils_win32.h"
/* ************************************** */
static DWORD* tunReadThread(LPVOID lpArg) {
struct tunread_arg *arg = (struct tunread_arg*)lpArg;
while(*arg->keep_running)
readFromTAPSocket(arg->eee);
return((DWORD*)NULL);
}
/* ************************************** */
/** Start a second thread in Windows because TUNTAP interfaces do not expose
* file descriptors. */
HANDLE startTunReadThread(struct tunread_arg *arg) {
DWORD dwThreadId;
return(CreateThread(NULL, /* security attributes */
0, /* use default stack size */
(LPTHREAD_START_ROUTINE)tunReadThread, /* thread function */
(void*)arg, /* argument to thread function */
0, /* thread creation flags */
&dwThreadId)); /* thread id out */
}
#endif

0
example_edge_embed.c → src/example_edge_embed.c

1
example_edge_embed_quick_edge_init.c → src/example_edge_embed_quick_edge_init.c

@ -17,7 +17,6 @@
*/ */
#include "n2n.h" #include "n2n.h"
#include "random_numbers.h"
/* /*
This tool demonstrates how to easily embed This tool demonstrates how to easily embed

0
example_sn_embed.c → src/example_sn_embed.c

12
header_encryption.c → src/header_encryption.c

@ -16,17 +16,12 @@
* *
*/ */
#include "header_encryption.h"
#include <string.h>
#include "random_numbers.h"
#include "pearson.h"
#include "portable_endian.h"
#include "n2n.h"
#define HASH_FIND_COMMUNITY(head, name, out) HASH_FIND_STR(head, name, out) #define HASH_FIND_COMMUNITY(head, name, out) HASH_FIND_STR(head, name, out)
/* ********************************************************************** */
uint32_t packet_header_decrypt (uint8_t packet[], uint8_t packet_len, uint32_t packet_header_decrypt (uint8_t packet[], uint8_t packet_len,
char * community_name, he_context_t * ctx) { char * community_name, he_context_t * ctx) {
@ -58,6 +53,7 @@ uint32_t packet_header_decrypt (uint8_t packet[], uint8_t packet_len,
return (0); // unsuccessful return (0); // unsuccessful
} }
/* ********************************************************************** */
int8_t packet_header_decrypt_if_required (uint8_t packet[], uint16_t packet_len, int8_t packet_header_decrypt_if_required (uint8_t packet[], uint16_t packet_len,
struct sn_community *communities) { struct sn_community *communities) {
@ -113,6 +109,7 @@ int8_t packet_header_decrypt_if_required (uint8_t packet[], uint16_t packet_len,
} }
} }
/* ********************************************************************** */
int32_t packet_header_encrypt (uint8_t packet[], uint8_t header_len, he_context_t * ctx) { int32_t packet_header_encrypt (uint8_t packet[], uint8_t header_len, he_context_t * ctx) {
uint8_t iv[16]; uint8_t iv[16];
@ -134,6 +131,7 @@ int32_t packet_header_encrypt (uint8_t packet[], uint8_t header_len, he_context_
return (0); return (0);
} }
/* ********************************************************************** */
void packet_header_setup_key (char * community_name, he_context_t * ctx) { void packet_header_setup_key (char * community_name, he_context_t * ctx) {

0
minilzo.c → src/minilzo.c

0
n2n.c → src/n2n.c

0
pearson.c → src/pearson.c

23
random_numbers.c → src/random_numbers.c

@ -1,12 +1,29 @@
/**
* (C) 2007-20 - 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"
/* The following code offers an alterate pseudo random number generator /* The following code offers an alterate pseudo random number generator
namely XORSHIFT128+ to use instead of C's rand(). Its performance is namely XORSHIFT128+ to use instead of C's rand(). Its performance is
on par with C's rand(). on par with C's rand().
*/ */
#include "random_numbers.h"
/* The state must be seeded in a way that it is not all zero, choose some /* The state must be seeded in a way that it is not all zero, choose some
arbitrary defaults (in this case: taken from splitmix64) */ arbitrary defaults (in this case: taken from splitmix64) */
static struct rn_generator_state_t rn_current_state = { static struct rn_generator_state_t rn_current_state = {

0
sn.c → src/sn.c

0
sn_utils.c → src/sn_utils.c

0
speck.c → src/speck.c

2
transform_aes.c → src/transform_aes.c

@ -17,8 +17,6 @@
*/ */
#include "n2n.h" #include "n2n.h"
#include "n2n_transforms.h"
#include "random_numbers.h"
#ifdef N2N_HAVE_AES #ifdef N2N_HAVE_AES

2
transform_cc20.c → src/transform_cc20.c

@ -17,8 +17,6 @@
*/ */
#include "n2n.h" #include "n2n.h"
#include "n2n_transforms.h"
#include "random_numbers.h"
#ifdef HAVE_OPENSSL_1_1 #ifdef HAVE_OPENSSL_1_1

0
transform_null.c → src/transform_null.c

4
transform_speck.c → src/transform_speck.c

@ -17,10 +17,6 @@
*/ */
#include "n2n.h" #include "n2n.h"
#include "n2n_transforms.h"
#include "speck.h"
#include "random_numbers.h"
#include "pearson.h"
#define N2N_SPECK_TRANSFORM_VERSION 1 /* version of the transform encoding */ #define N2N_SPECK_TRANSFORM_VERSION 1 /* version of the transform encoding */
#define N2N_SPECK_IVEC_SIZE 16 #define N2N_SPECK_IVEC_SIZE 16

7
transform_tf.c → src/transform_tf.c

@ -17,13 +17,6 @@
*/ */
#include "n2n.h" #include "n2n.h"
#include "n2n_transforms.h"
#include "twofish.h"
#include "random_numbers.h"
#ifndef _MSC_VER
/* Not included in Visual Studio 2008 */
#include <strings.h> /* index() */
#endif
#define N2N_TWOFISH_NUM_SA 32 /* space for SAa */ #define N2N_TWOFISH_NUM_SA 32 /* space for SAa */

0
tuntap_freebsd.c → src/tuntap_freebsd.c

9
tuntap_linux.c → src/tuntap_linux.c

@ -16,16 +16,9 @@
* *
*/ */
#include "n2n.h"
#ifdef __linux__ #ifdef __linux__
#include <net/if_arp.h> #include "n2n.h"
#include <net/if.h>
#include <linux/if_tun.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include "random_numbers.h"
/* ********************************** */ /* ********************************** */

0
tuntap_netbsd.c → src/tuntap_netbsd.c

0
tuntap_osx.c → src/tuntap_osx.c

8
twofish.c → src/twofish.c

@ -35,13 +35,7 @@
#ifndef __TWOFISH_LIBRARY_SOURCE__ #ifndef __TWOFISH_LIBRARY_SOURCE__
#define __TWOFISH_LIBRARY_SOURCE__ #define __TWOFISH_LIBRARY_SOURCE__
#include <string.h> #include "n2n.h"
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <sys/types.h>
#include "twofish.h"
#include "random_numbers.h"
/* Fixed 8x8 permutation S-boxes */ /* Fixed 8x8 permutation S-boxes */
static const uint8_t TwoFish_P[2][256] = static const uint8_t TwoFish_P[2][256] =

0
wire.c → src/wire.c

4
tools/Makefile.in

@ -16,8 +16,8 @@ endif
LIBS_EDGE_OPT=@N2N_LIBS@ LIBS_EDGE_OPT=@N2N_LIBS@
LIBS_EDGE+=$(LIBS_EDGE_OPT) LIBS_EDGE+=$(LIBS_EDGE_OPT)
HEADERS=../n2n_wire.h ../n2n.h ../twofish.h ../n2n_transforms.h HEADERS=$(wildcard include/*.h)
CFLAGS+=-I.. @CFLAGS@ CFLAGS+=-I../include @CFLAGS@
LDFLAGS+=-L.. LDFLAGS+=-L..
CFLAGS+=$(DEBUG) $(OPTIMIZATION) $(WARN) CFLAGS+=$(DEBUG) $(OPTIMIZATION) $(WARN)
LDFLAGS=@LDFLAGS@ LDFLAGS=@LDFLAGS@

Loading…
Cancel
Save