├── n2n_v1 ├── COPYING ├── HACKING ├── INSTALL ├── Makefile ├── README ├── debian │ ├── README.Debian │ ├── changelog │ ├── compat │ ├── control │ ├── copyright │ ├── n2n.dirs │ ├── n2n.docs │ ├── n2n.install │ ├── n2n.manpages │ └── rules ├── edge.8 ├── edge.c ├── lzoconf.h ├── lzodefs.h ├── minilzo.c ├── minilzo.h ├── n2n.c ├── n2n.h ├── n2n.spec ├── openwrt │ └── kamikaze │ │ └── Makefile ├── scripts │ ├── mk_SRPM.sh │ ├── mk_deb.sh │ └── mk_tar.sh ├── supernode.1 ├── supernode.c ├── tuntap_freebsd.c ├── tuntap_linux.c ├── tuntap_osx.c ├── twofish.c ├── twofish.h └── win32 │ ├── DotNet │ ├── n2n.sln │ ├── n2n.suo │ ├── n2n.vcproj │ └── supernode │ │ └── supernode.vcproj │ ├── getopt.c │ ├── getopt.h │ ├── getopt1.c │ ├── n2n_win32.h │ ├── wintap.c │ └── wintap.h └── n2n_v2 ├── CMakeLists.txt ├── COPYING ├── HACKING ├── INSTALL ├── Makefile ├── NEW_FEATURES.txt ├── README ├── benchmark.c ├── debian ├── README.Debian ├── changelog ├── compat ├── control ├── copyright ├── n2n-edge.default ├── n2n-edge.docs ├── n2n-edge.init ├── n2n-edge.install ├── n2n-edge.manpages ├── n2n-supernode.init ├── n2n-supernode.install ├── n2n-supernode.manpages └── rules ├── edge.8 ├── edge.c ├── gen_keyfile.py ├── lzoconf.h ├── lzodefs.h ├── minilzo.c ├── minilzo.h ├── n2n.c ├── n2n.h ├── n2n.spec ├── n2n_keyfile.c ├── n2n_keyfile.h ├── n2n_transforms.h ├── n2n_v2.7 ├── n2n_wire.h ├── openwrt └── kamikaze │ └── Makefile ├── scripts ├── mk_SRPM.sh ├── mk_deb.sh └── mk_tar.sh ├── sn.c ├── supernode.1 ├── test.c ├── transform_aes.c ├── transform_null.c ├── transform_tf.c ├── tuntap_freebsd.c ├── tuntap_linux.c ├── tuntap_netbsd.c ├── tuntap_osx.c ├── twofish.c ├── twofish.h ├── version.c ├── win32 ├── CMakeLists.txt ├── DotNet │ ├── n2n.sln │ ├── n2n.suo │ ├── n2n.vcproj │ └── supernode.vcproj ├── getopt.c ├── getopt.h ├── getopt1.c ├── n2n_win32.h ├── version-msvc.c ├── wintap.c └── wintap.h └── wire.c /n2n_v1/INSTALL: -------------------------------------------------------------------------------- 1 | INSTALL 2 | 3 | To build the programs: 4 | 5 | $ make 6 | 7 | To install the programs and man pages: 8 | 9 | $ make install 10 | 11 | or 12 | 13 | $ make PREFIX=/usr/local install 14 | 15 | 16 | RPM Package 17 | ----------- 18 | 19 | These steps should work with RPM based Linux distributions since rpmbuild was 20 | split from the rpm utility (c RedHat 9). 21 | 22 | 23 | To build an RPM the easy way follow these steps. 24 | 25 | 1. Build SRPM 26 | 27 | $ cd n2n 28 | $ scripts/mk_SRPM.sh 29 | 30 | Look for where the src.rpm file was put ( "Wrote:" ). 31 | 32 | 2. Build binary RPM from SRPM 33 | 34 | $ rpm -i path/to/n2n-.src.rpm 35 | $ rpmbuild -bb n2n.spec 36 | 37 | 38 | All this can be done as non-root user if you have a ~/.rpmmacros file with this 39 | line in it: 40 | 41 | %_topdir /home/username/rpmtopdir 42 | 43 | 44 | To build an RPM the hard way follow these steps. 45 | 46 | $ cp -a n2ndir n2n-1.3 47 | $ tar czf n2n-1.3.tar.gz n2n-1.3 48 | $ mv n2n-1.3.tar.gz /usr/src/redhat/SOURCES 49 | $ cp n2ndir/n2n.spec /usr/src/redhat/SPECS 50 | $ rpmbuild -bb n2n.spec 51 | -------------------------------------------------------------------------------- /n2n_v1/Makefile: -------------------------------------------------------------------------------- 1 | 2 | N2N_VERSION="1.3.2" 3 | 4 | ######## 5 | 6 | CC=gcc 7 | DEBUG?=-g 8 | WARN?=-Wall -Wshadow -Wpointer-arith -Wmissing-declarations -Wnested-externs 9 | 10 | #Ultrasparc64 users experiencing SIGBUS should try the following gcc options 11 | #(thanks to Robert Gibbon) 12 | PLATOPTS_SPARC64=-mcpu=ultrasparc -pipe -fomit-frame-pointer -ffast-math -finline-functions -fweb -frename-registers -mapp-regs 13 | 14 | 15 | CFLAGS+=$(DEBUG) $(WARN) $(OPTIONS) $(PLATOPTS) 16 | 17 | INSTALL=install 18 | MKDIR=mkdir -p 19 | 20 | INSTALL_PROG=$(INSTALL) -m755 21 | INSTALL_DOC=$(INSTALL) -m644 22 | 23 | 24 | # DESTDIR set in debian make system 25 | PREFIX?=$(DESTDIR)/usr 26 | BINDIR=$(PREFIX)/bin 27 | SBINDIR=$(PREFIX)/sbin 28 | MANDIR?=$(PREFIX)/share/man 29 | MAN1DIR=$(MANDIR)/man1 30 | MAN8DIR=$(MANDIR)/man8 31 | 32 | N2N_LIB=n2n.a 33 | N2N_OBJS=n2n.o minilzo.o twofish.o tuntap_freebsd.o tuntap_linux.o tuntap_osx.o version.o 34 | LIBS=-lpthread 35 | 36 | APPS=edge supernode 37 | DOCS=edge.8.gz supernode.1.gz 38 | 39 | all: $(APPS) #$(DOCS) 40 | 41 | edge: edge.c $(N2N_LIB) n2n.h Makefile 42 | $(CC) $(CFLAGS) edge.c $(N2N_LIB) $(LIBS) -o edge 43 | 44 | supernode: supernode.c $(N2N_LIB) n2n.h Makefile 45 | $(CC) $(CFLAGS) supernode.c $(N2N_LIB) $(LIBS) -o supernode 46 | 47 | .c.o: n2n.h Makefile 48 | $(CC) $(CFLAGS) -c $< 49 | 50 | %.gz : % 51 | gzip -c $< > $@ 52 | 53 | $(N2N_LIB): $(N2N_OBJS) 54 | ar rcs $(N2N_LIB) $(N2N_OBJS) 55 | # $(RANLIB) $@ 56 | 57 | version.c: 58 | @echo $(N2N_VERSION) | sed -e 's/.*/const char * version = "&";/' > version.c 59 | @uname -p | sed -e 's/.*/const char * osName = "&";/' >> version.c 60 | @date +"%D %r" | sed -e 's/.*/const char * buildDate = "&";/' >> version.c 61 | 62 | clean: 63 | rm -rf $(N2N_OBJS) $(N2N_LIB) $(APPS) $(DOCS) *.dSYM *~ version.c 64 | 65 | install: edge supernode edge.8.gz supernode.1.gz 66 | echo "MANDIR=$(MANDIR)" 67 | $(MKDIR) $(BINDIR) $(SBINDIR) $(MAN1DIR) $(MAN8DIR) 68 | $(INSTALL_PROG) supernode $(BINDIR)/ 69 | $(INSTALL_PROG) edge $(SBINDIR)/ 70 | $(INSTALL_DOC) edge.8.gz $(MAN8DIR)/ 71 | $(INSTALL_DOC) supernode.1.gz $(MAN1DIR)/ 72 | 73 | # Courtesy of Ole Tange 74 | 75 | deb: 76 | dpkg-buildpackage -------------------------------------------------------------------------------- /n2n_v1/README: -------------------------------------------------------------------------------- 1 | 2 | 3 | Edge node 4 | --------- 5 | 6 | You need to start an egde node on each host you want to connect with the *same* 7 | community. 8 | 9 | 0. become root 10 | 11 | 1. create tun device 12 | # tunctl -t tun0 13 | 14 | 3. enable the edge process 15 | # ./edge -d n2n0 -c mynetwork -k encryptme -u 99 -g 99 -m 3C:A0:12:34:56:78 -a 1.2.3.4 -l a.b.c.d:xyw 16 | or 17 | # N2N_KEY=encryptme ./edge -d n2n0 -c mynetwork -u 99 -g 99 -m 3C:A0:12:34:56:78 -a 1.2.3.4 -l a.b.c.d:xyw 18 | 19 | Once you have this worked out, you can add the "-f" option to make edge detach 20 | and run as a daemon. 21 | 22 | Note that -u, -g and -f options are not available for Windows. 23 | 24 | Supernode 25 | -------- 26 | 27 | You need to start the supernode once 28 | 29 | 1. ./supernode -l 1234 -v 30 | 31 | 32 | Dropping Root Privileges and SUID-Root Executables (UNIX) 33 | -------------------------------------------------- 34 | 35 | The edge node uses superuser privileges to create a TAP network interface 36 | device. Once this is created root privileges are not required and can constitute 37 | a security hazard if there is some way for an attacker to take control of an 38 | edge process while it is running. Edge will drop to a non-privileged user if you 39 | specify the -u and -g options. These are numeric IDs. Consult 40 | /etc/passwd. 41 | 42 | You may choose to install edge SUID-root to do this: 43 | 44 | 1. Become root 45 | 2. chown root:root edge 46 | 3. chmod +s edge 47 | done 48 | 49 | Any user can now run edge. You may not want this, but it may be convenient and 50 | safe if your host has only one login user. 51 | 52 | 53 | Running As a Daemon (UNIX) 54 | ------------------- 55 | 56 | When given "-f" as a command line option, edge will call daemon(3) after 57 | successful setup. This causes the process to fork a child which closes stdin, 58 | stdout and stderr then sets itself as process group leader. When this is done, 59 | the edge command returns immediately and you will only see the edge process in 60 | the process listings, eg. from ps or top. 61 | 62 | If the edge command returns 0 then the daemon started successfully. If it 63 | returns non-zero then edge failed to start up for some reason. When edge starts 64 | running as a daemon, all logging goes to syslog daemon.info facility. 65 | 66 | 67 | IPv6 Support (added r3650) 68 | ------------ 69 | 70 | n2n supports the carriage of IPv6 packets within the n2n tunnel. N2n does not 71 | yet use IPv6 for transport between edges and supernodes. 72 | 73 | To make IPv6 carriage work you need to manually add IPv6 addresses to the TAP 74 | interfaces at each end. There is currently no way to specify an IPv6 address on 75 | the edge command line. 76 | 77 | eg. under linux: 78 | 79 | on hostA: 80 | [hostA] # /sbin/ip -6 addr add fc00:abcd:1234::7/48 dev n2n0 81 | 82 | on hostB: 83 | [hostB] # /sbin/ip -6 addr add fc00:abcd:1234::6/48 dev n2n0 84 | 85 | You may find it useful to make use of tunctl from the uml-utilities 86 | package. Tunctl allow you to bring up a TAP interface and configure addressing 87 | prior to starting edge. It also allows edge to be restarted without the 88 | interface closing (which would normally affect routing tables). 89 | 90 | Once the IPv6 addresses are configured and edge started, IPv6 neighbor discovery 91 | packets flow (get broadcast) and IPv6 entities self arrange. Test your IPv6 92 | setup with ping6 - the IPv6 ping command. 93 | 94 | 95 | (C) 2007,2008 - Luca Deri , Richard Andrews 96 | -------------------------------------------------------------------------------- /n2n_v1/debian/README.Debian: -------------------------------------------------------------------------------- 1 | n2n for Debian 2 | -------------- 3 | 4 | This package depends on the kernel having the TUN/TAP driver configured in using 5 | CONFIG_TUN=yes. 6 | 7 | -- Richard Andrews Thu, 10 Jul 2008 22:38:02 +1000 8 | -------------------------------------------------------------------------------- /n2n_v1/debian/changelog: -------------------------------------------------------------------------------- 1 | n2n (1.3-1) hardy; urgency=low 2 | 3 | * New upstream release 4 | 5 | -- Richard Andrews Fri, 30 Jan 2009 23:49:56 +1100 6 | 7 | n2n (1.2-1) unstable; urgency=low 8 | 9 | * Initial release 10 | 11 | -- Richard Andrews Thu, 10 Jul 2008 22:38:02 +1000 12 | 13 | -------------------------------------------------------------------------------- /n2n_v1/debian/compat: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /n2n_v1/debian/control: -------------------------------------------------------------------------------- 1 | Source: n2n 2 | Section: net 3 | Priority: extra 4 | Maintainer: Jean-Baptiste Denis 5 | Build-Depends: cdbs, debhelper (>= 5), libc6-dev (>= 2.0), dpatch, gcc 6 | Standards-Version: 3.7.2 7 | 8 | Package: n2n 9 | Architecture: any 10 | Suggests: uml-utilities 11 | Depends: ${shlibs:Depends}, ${misc:Depends} 12 | Description: a layer-two peer-to-peer virtual private network (VPN) 13 | n2n is a layer-two peer-to-peer virtual private network (VPN) which allows 14 | users to exploit features typical of P2P applications at network instead of 15 | application level. This means that users can gain native IP visibility (e.g. 16 | two PCs belonging to the same n2n network can ping each other) and be 17 | reachable with the same network IP address regardless of the network where 18 | they currently belong. In a nutshell, as OpenVPN moved SSL from application 19 | (e.g. used to implement the https protocol) to network protocol, n2n moves 20 | P2P from application to network level. 21 | 22 | 23 | -------------------------------------------------------------------------------- /n2n_v1/debian/copyright: -------------------------------------------------------------------------------- 1 | This package was debianized by Jean-Baptiste Denis on 2 | Thu, 20 Nov 2008 23:53:02 +1000. 3 | 4 | It was downloaded from http://www.ntop.org/n2n/ 5 | 6 | Upstream Author(s): 7 | 8 | Luca Deri 9 | Richard Andrews 10 | 11 | Copyright: 12 | 13 | Copyright (C) 2008 Luca Deri 14 | Copyright (C) 2008 Richard Andrews 15 | 16 | License: 17 | 18 | GPLv3 19 | 20 | The Debian packaging is (C) 2008, Richard Andrews , 21 | Luca Deri and is licensed under the GPLv3, see 22 | `/usr/share/common-licenses/GPL-3'. 23 | 24 | -------------------------------------------------------------------------------- /n2n_v1/debian/n2n.dirs: -------------------------------------------------------------------------------- 1 | usr/bin 2 | usr/sbin 3 | usr/share/doc/n2n 4 | usr/share/man/man1 5 | -------------------------------------------------------------------------------- /n2n_v1/debian/n2n.docs: -------------------------------------------------------------------------------- 1 | README 2 | supernode.1 3 | edge.8 4 | -------------------------------------------------------------------------------- /n2n_v1/debian/n2n.install: -------------------------------------------------------------------------------- 1 | edge /usr/bin 2 | supernode /usr/sbin 3 | -------------------------------------------------------------------------------- /n2n_v1/debian/n2n.manpages: -------------------------------------------------------------------------------- 1 | edge.8 2 | supernode.1 3 | -------------------------------------------------------------------------------- /n2n_v1/debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | #DEB_MAKE_INSTALL_TARGET = install prefix=$(CURDIR)/debian/n2n/usr 4 | 5 | include /usr/share/cdbs/1/rules/debhelper.mk 6 | include /usr/share/cdbs/1/rules/dpatch.mk 7 | include /usr/share/cdbs/1/class/makefile.mk 8 | 9 | # Add here any variable or target overrides you need. 10 | -------------------------------------------------------------------------------- /n2n_v1/edge.8: -------------------------------------------------------------------------------- 1 | .TH edge 1 "Jan 3, 2009" "revision 3679" "SUPERUSER COMMANDS" 2 | .SH NAME 3 | edge \- n2n edge node daemon 4 | .SH SYNOPSIS 5 | .B edge 6 | [\-d ] \-a \-c \-k \-l 7 | [\-p ] [\-u ] [\-g ] [-f] [\-m ] [\-t] [\-r] [\-v] 8 | .SH DESCRIPTION 9 | N2N is a peer-to-peer VPN system. Edge is the edge node daemon for n2n which 10 | creates a TAP interface to expose the n2n virtual LAN. On startup n2n creates 11 | the TAP interface and configures it then registers with the supernode so it can 12 | begin to find other nodes in the community. 13 | .PP 14 | .SH OPTIONS 15 | .TP 16 | \-d 17 | sets the TAP device name as seen in ifconfig. 18 | .TP 19 | \-a 20 | sets the n2n virtual LAN IP address being claimed. This is a private IP 21 | address. All IP addresses in an n2n community should belong to the same /24 22 | network (ie. only the last segment of the IP addresses varies). 23 | .TP 24 | \-b 25 | cause edge to perform hostname resolution for the supernode address each time 26 | the supernode is periodically contacted. 27 | .TP 28 | \-c 29 | sets the n2n community name. All edges within the same community look to be on 30 | the same LAN (layer 2 network segment). All edges communicating must use the 31 | same key and community name. 32 | .TP 33 | \-h 34 | write usage to tty then exit. 35 | .TP 36 | \-k 37 | sets the twofish encryption key from ASCII text (see also N2N_KEY in 38 | ENVIRONMENT). All edges communicating must use the same key and community name. 39 | .TP 40 | \-l : 41 | sets the n2n supernode IP address and port to register to. 42 | .TP 43 | \-p 44 | binds edge to the given UDP port. Useful for keeping the same external socket 45 | across restarts of edge. 46 | .TP 47 | \-u 48 | causes the edge process to drop to the given user ID when privileges are no 49 | longer required. 50 | .TP 51 | \-g 52 | causes the edge process to drop to the given group ID when privileges are no 53 | longer required. 54 | .TP 55 | \-f 56 | causes the edge process to fork and run as a daemon, closing stdin, stdout, 57 | stderr and becoming a process group leader. 58 | .TP 59 | \-m 60 | start the TAP interface with the given MAC address. This is highly recommended 61 | as it means the same address will be used if edge stops and restarts. If this is 62 | not done, the ARP caches of all peers will be wrong and packets will not flow to 63 | this edge until the next ARP refresh. 64 | .TP 65 | \-M 66 | set the MTU of the edge interface in bytes. MTU is the largest packet fragment 67 | size allowed to be moved throught the interface. The default is 1400. 68 | .TP 69 | \-s 70 | set the netmask of edge interface in IPv4 dotted decimal notation. The default 71 | is 255.255.255.0 (ie. /24). 72 | .TP 73 | \-t 74 | use HTTP tunneling instead of the normal UDP mechanism (experimental). 75 | .TP 76 | \-r 77 | enable packet forwarding/routing through the n2n virtual LAN. Without this 78 | option, packets arriving over n2n which are not for the -a IP address are 79 | dropped. 80 | .TP 81 | \-v 82 | use verbose logging. 83 | .SH ENVIRONMENT 84 | .TP 85 | .B N2N_KEY 86 | set the encryption key so it is not visible on the command line 87 | .SH EXAMPLES 88 | .TP 89 | .B edge \-d n2n0 \-c mynetwork \-k encryptme \-u 99 \-g 99 \-m DE:AD:BE:EF:01:23 \-a 192.168.254.7 \-p 50001 \-l 123.121.120.119:7654 90 | 91 | Start edge with TAP device n2n0 on community "mynetwork" with community 92 | supernode at 123.121.120.119 UDP port 7654 and bind the locally used UDP port to 93 | 50001. Use "encryptme" as the shared encryption key. Assign MAC address 94 | DE:AD:BE:EF:01:23 to the n2n interface and drop to user=99 and group=99 after 95 | the TAP device is successfull configured. 96 | .PP 97 | Add the -f option to make edge run as a daemon. 98 | .PP 99 | Somewhere else setup another edge with similar parameters, eg. 100 | 101 | .B edge \-d n2n0 \-c mynetwork \-k encryptme \-u 99 \-g 99 \-m DE:AD:BE:EF:01:21 \-a 192.168.254.5 \-p 50001 \-l 123.121.120.119:7654 102 | .PP 103 | Now you can ping from 192.168.254.5 to 192.168.254.7. 104 | .PP 105 | The MAC address (-m ) and virtual IP address (-a ) must be different on all edges in the same community. 106 | 107 | .SH CONFIGURATION 108 | All configuration for edge is from the command line and environment 109 | variables. If you wish to reconfigure edge you should kill the process and 110 | restart with the desired options. 111 | .SH EXIT STATUS 112 | edge is a daemon and any exit is an error. 113 | .SH AUTHOR 114 | Luca Deri ( deri (at) ntop.org ), Richard Andrews ( andrews (at) ntop.org ), Don Bindner 115 | .SH SEE ALSO 116 | ifconfig(8) supernode(1) tunctl(8) 117 | -------------------------------------------------------------------------------- /n2n_v1/minilzo.h: -------------------------------------------------------------------------------- 1 | /* minilzo.h -- mini subset of the LZO real-time data compression library 2 | 3 | This file is part of the LZO real-time data compression library. 4 | 5 | Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer 6 | Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer 7 | Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer 8 | Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer 9 | Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer 10 | Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer 11 | Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer 12 | Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer 13 | Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer 14 | Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer 15 | Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer 16 | Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer 17 | Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer 18 | All Rights Reserved. 19 | 20 | The LZO library is free software; you can redistribute it and/or 21 | modify it under the terms of the GNU General Public License as 22 | published by the Free Software Foundation; either version 2 of 23 | the License, or (at your option) any later version. 24 | 25 | The LZO library is distributed in the hope that it will be useful, 26 | but WITHOUT ANY WARRANTY; without even the implied warranty of 27 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 | GNU General Public License for more details. 29 | 30 | You should have received a copy of the GNU General Public License 31 | along with the LZO library; see the file COPYING. 32 | If not, write to the Free Software Foundation, Inc., 33 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 34 | 35 | Markus F.X.J. Oberhumer 36 | 37 | http://www.oberhumer.com/opensource/lzo/ 38 | */ 39 | 40 | /* 41 | * NOTE: 42 | * the full LZO package can be found at 43 | * http://www.oberhumer.com/opensource/lzo/ 44 | */ 45 | 46 | 47 | #ifndef __MINILZO_H 48 | #define __MINILZO_H 49 | 50 | #define MINILZO_VERSION 0x2030 51 | 52 | #ifdef __LZOCONF_H 53 | # error "you cannot use both LZO and miniLZO" 54 | #endif 55 | 56 | #undef LZO_HAVE_CONFIG_H 57 | #include "lzoconf.h" 58 | 59 | #if !defined(LZO_VERSION) || (LZO_VERSION != MINILZO_VERSION) 60 | # error "version mismatch in header files" 61 | #endif 62 | 63 | 64 | #ifdef __cplusplus 65 | extern "C" { 66 | #endif 67 | 68 | 69 | /*********************************************************************** 70 | // 71 | ************************************************************************/ 72 | 73 | /* Memory required for the wrkmem parameter. 74 | * When the required size is 0, you can also pass a NULL pointer. 75 | */ 76 | 77 | #define LZO1X_MEM_COMPRESS LZO1X_1_MEM_COMPRESS 78 | #define LZO1X_1_MEM_COMPRESS ((lzo_uint32) (16384L * lzo_sizeof_dict_t)) 79 | #define LZO1X_MEM_DECOMPRESS (0) 80 | 81 | 82 | /* compression */ 83 | LZO_EXTERN(int) 84 | lzo1x_1_compress ( const lzo_bytep src, lzo_uint src_len, 85 | lzo_bytep dst, lzo_uintp dst_len, 86 | lzo_voidp wrkmem ); 87 | 88 | /* decompression */ 89 | LZO_EXTERN(int) 90 | lzo1x_decompress ( const lzo_bytep src, lzo_uint src_len, 91 | lzo_bytep dst, lzo_uintp dst_len, 92 | lzo_voidp wrkmem /* NOT USED */ ); 93 | 94 | /* safe decompression with overrun testing */ 95 | LZO_EXTERN(int) 96 | lzo1x_decompress_safe ( const lzo_bytep src, lzo_uint src_len, 97 | lzo_bytep dst, lzo_uintp dst_len, 98 | lzo_voidp wrkmem /* NOT USED */ ); 99 | 100 | 101 | #ifdef __cplusplus 102 | } /* extern "C" */ 103 | #endif 104 | 105 | #endif /* already included */ 106 | 107 | -------------------------------------------------------------------------------- /n2n_v1/n2n.spec: -------------------------------------------------------------------------------- 1 | Summary: N2N peer-to-peer virtual private network system. 2 | Name: n2n 3 | Version: 1.3 4 | Release: 1 5 | License: GPLv3 6 | Vendor: ntop.org 7 | Group: None 8 | URL: http://www.ntop.org/n2n 9 | Source0: %{name}-%{version}.tar.gz 10 | BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root 11 | 12 | %description 13 | N2N is a peer-to-peer virtual private network system. N2N uses the universal 14 | TUNTAP interface to create TAP network interfaces to an encrypted virtual 15 | LAN. Members of a community share a common encryption key which allows echange 16 | of data. The supernode is used for peer discovery and initial packet relay 17 | before direct peer-to-peer exchange is established. 18 | Once direct packet exchange is established, the supernode is not required. 19 | 20 | %prep 21 | 22 | %setup -q 23 | 24 | echo -e "\n *** Building ${RPM_PACKAGE_NAME}-${RPM_PACKAGE_VERSION}-${RPM_PACKAGE_RELEASE} ***\n" 25 | 26 | %build 27 | make 28 | 29 | %install 30 | make PREFIX=${RPM_BUILD_ROOT}/usr install 31 | 32 | %clean 33 | rm -rf $RPM_BUILD_ROOT 34 | 35 | 36 | %files 37 | %defattr(-,root,root,-) 38 | /usr/bin/supernode 39 | /usr/sbin/edge 40 | %doc /usr/share/man/man1/supernode.1.gz 41 | %doc /usr/share/man/man8/edge.8.gz 42 | 43 | 44 | %changelog 45 | * Sat May 3 2008 Richard Andrews - 46 | - Initial build. 47 | 48 | -------------------------------------------------------------------------------- /n2n_v1/openwrt/kamikaze/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2008 OpenWrt.org 3 | # 4 | # This is free software, licensed under the GNU General Public License v2. 5 | 6 | 7 | include $(TOPDIR)/rules.mk 8 | 9 | PKG_BRANCH:=trunk 10 | PKG_SOURCE_URL:=https://svn.ntop.org/svn/ntop/trunk/n2n 11 | PKG_REV:=$(shell LC_ALL=C svn info ${PKG_SOURCE_URL} | sed -ne's/^Last Changed Rev: //p') 12 | 13 | PKG_NAME:=n2n 14 | PKG_VERSION:=svn$(PKG_REV) 15 | PKG_RELEASE:=1 16 | 17 | PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION) 18 | PKG_SOURCE:=$(PKG_SOURCE_SUBDIR).tar.gz 19 | PKG_SOURCE_PROTO:=svn 20 | PKG_SOURCE_VERSION:=$(PKG_REV) 21 | 22 | PKG_BUILD_DEPENDS:= 23 | 24 | PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) 25 | PKG_INSTALL_DIR:=$(PKG_BUILD_DIR) 26 | 27 | 28 | 29 | include $(INCLUDE_DIR)/package.mk 30 | 31 | define Package/n2n 32 | SECTION:=net 33 | CATEGORY:=Network 34 | TITLE:=VPN tunneling daemon 35 | URL:=http://www.ntop.org/n2n/ 36 | SUBMENU:=VPN 37 | DEPENDS:=libpthread 38 | endef 39 | 40 | 41 | define Build/Configure 42 | endef 43 | 44 | define Build/Compile 45 | $(MAKE) CC="$(TARGET_CC)" -C $(PKG_BUILD_DIR) 46 | endef 47 | 48 | 49 | define Package/n2n/install 50 | $(INSTALL_DIR) $(1)/usr/sbin 51 | $(INSTALL_BIN) $(PKG_INSTALL_DIR)/edge $(1)/usr/sbin/ 52 | $(INSTALL_BIN) $(PKG_INSTALL_DIR)/supernode $(1)/usr/sbin/ 53 | endef 54 | 55 | $(eval $(call BuildPackage,n2n)) 56 | -------------------------------------------------------------------------------- /n2n_v1/scripts/mk_SRPM.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # This script makes a SRPM - a source RPM file which can be built into the 4 | # appropriate distro specific RPM for any platform. 5 | # 6 | # To build the binary package: 7 | # rpm -i n2n-.src.rpm 8 | # rpmbuild -bb n2n.spec 9 | # 10 | # Look for the "Wrote:" line to see where the final RPM is. 11 | # 12 | # To run this script cd to the n2n directory and run it as follows 13 | # scripts/mk_SRPMS.sh 14 | # 15 | 16 | set -e 17 | 18 | set -x 19 | 20 | BASE=`pwd` 21 | 22 | TARFILE=`${BASE}/scripts/mk_tar.sh` 23 | 24 | test -f ${TARFILE} 25 | 26 | echo "Building SRPM" 27 | # -ts means build source RPM from tarfile 28 | rpmbuild -ts ${TARFILE} 29 | 30 | echo "Done" 31 | -------------------------------------------------------------------------------- /n2n_v1/scripts/mk_deb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script makes a SRPM - a source RPM file which can be built into the 4 | # appropriate distro specific RPM for any platform. 5 | # 6 | # To build the binary package: 7 | # rpm -i n2n-.src.rpm 8 | # rpmbuild -bb n2n.spec 9 | # 10 | # Look for the "Wrote:" line to see where the final RPM is. 11 | # 12 | # To run this script cd to the n2n directory and run it as follows 13 | # scripts/mk_SRPMS.sh 14 | # 15 | 16 | set -e 17 | 18 | set -x 19 | 20 | BASE=`pwd` 21 | 22 | TARFILE=`${BASE}/scripts/mk_tar.sh` 23 | TEMPDIR="build_deb" 24 | 25 | test -f ${TARFILE} 26 | 27 | echo "Building .deb" 28 | 29 | if [ -d ${TEMPDIR} ]; then 30 | echo "Removing ${TEMPDIR} directory" 31 | rm -rf ${TEMPDIR} >&2 32 | fi 33 | 34 | mkdir ${TEMPDIR} 35 | 36 | pushd ${TEMPDIR} 37 | 38 | tar xzf ${TARFILE} #At original location 39 | 40 | cd n2n* 41 | 42 | dpkg-buildpackage -rfakeroot 43 | 44 | popd 45 | 46 | echo "Done" 47 | -------------------------------------------------------------------------------- /n2n_v1/scripts/mk_tar.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script makes a SRPM - a source RPM file which can be built into the 4 | # appropriate distro specific RPM for any platform. 5 | # 6 | # To build the binary package: 7 | # rpm -i n2n-.src.rpm 8 | # rpmbuild -bb n2n.spec 9 | # 10 | # Look for the "Wrote:" line to see where the final RPM is. 11 | # 12 | # To run this script cd to the n2n directory and run it as follows 13 | # scripts/mk_SRPMS.sh 14 | # 15 | 16 | set -e 17 | 18 | function exit_fail() 19 | { 20 | echo "$1" 21 | exit 1 22 | } 23 | 24 | PACKAGE="n2n" 25 | PKG_VERSION="1.3" 26 | PKG_AND_VERSION="${PACKAGE}-${PKG_VERSION}" 27 | 28 | TEMPDIR="tmp" 29 | 30 | SOURCE_MANIFEST=" 31 | README 32 | edge.c 33 | lzoconf.h 34 | lzodefs.h 35 | Makefile 36 | minilzo.c 37 | minilzo.h 38 | n2n.c 39 | n2n.h 40 | n2n.spec 41 | supernode.c 42 | tuntap_linux.c 43 | tuntap_freebsd.c 44 | tuntap_osx.c 45 | twofish.c 46 | twofish.h 47 | edge.8 48 | supernode.1 49 | debian/changelog 50 | debian/compat 51 | debian/control 52 | debian/copyright 53 | debian/n2n.dirs 54 | debian/n2n.docs 55 | debian/n2n.install 56 | debian/n2n.manpages 57 | debian/README.Debian 58 | debian/rules 59 | " 60 | 61 | BASE=`pwd` 62 | 63 | for F in ${SOURCE_MANIFEST}; do 64 | test -e $F || exit_fail "Cannot find $F. Maybe you're in the wrong directory. Please execute from n2n directory."; >&2 65 | done 66 | 67 | echo "Found critical files. Proceeding." >&2 68 | 69 | if [ -d ${TEMPDIR} ]; then 70 | echo "Removing ${TEMPDIR} directory" 71 | rm -rf ${TEMPDIR} >&2 72 | fi 73 | 74 | mkdir ${TEMPDIR} >&2 75 | 76 | pushd ${TEMPDIR} >&2 77 | 78 | echo "Creating staging directory ${PWD}/${PKG_AND_VERSION}" >&2 79 | 80 | if [ -d ${PKG_AND_VERSION} ] ; then 81 | echo "Removing ${PKG_AND_VERSION} directory" 82 | rm -rf ${PKG_AND_VERSION} >&2 83 | fi 84 | 85 | mkdir ${PKG_AND_VERSION} 86 | 87 | pushd ${BASE} >&2 88 | 89 | echo "Copying in files" >&2 90 | for F in ${SOURCE_MANIFEST}; do 91 | cp --parents -a $F ${TEMPDIR}/${PKG_AND_VERSION}/ 92 | done 93 | 94 | popd >&2 95 | 96 | TARFILE="${PKG_AND_VERSION}.tar.gz" 97 | echo "Creating ${TARFILE}" >&2 98 | tar czf ${BASE}/${TARFILE} ${PKG_AND_VERSION} 99 | 100 | popd >&2 101 | 102 | rm -rf ${TEMPDIR} >&2 103 | 104 | echo ${BASE}/${TARFILE} 105 | -------------------------------------------------------------------------------- /n2n_v1/supernode.1: -------------------------------------------------------------------------------- 1 | .TH supernode 1 "Jan 3, 2009" "revision 3679" "USER COMMANDS" 2 | .SH NAME 3 | supernode \- n2n supernode daemon 4 | .SH SYNOPSIS 5 | .B supernode \-l [\-v] 6 | .SH DESCRIPTION 7 | N2N is a peer-to-peer VPN system. Supernode is a node introduction registry, 8 | broadcast conduit and packet relay node for the n2n system. On startup supernode 9 | begins listening on the specified UDP port for node registrations, and other 10 | packets to route. The supernode can service any number of communities and routes 11 | packets only between members of the same community. The supernode does not hold 12 | the community encryption key and so cannot snoop or inject packets into the 13 | community. 14 | .PP 15 | Supernode can service a number of n2n communities concurrently. Traffic does not 16 | cross between communities. 17 | .PP 18 | All logging goes to stdout. 19 | .SH OPTIONS 20 | .TP 21 | \-l 22 | listen on the given UDP port 23 | .TP 24 | \-v 25 | use verbose logging 26 | .SH EXAMPLES 27 | .TP 28 | .B supernode -l 7654 -v 29 | Start supernode listening on UDP port 7654 with verbose output. 30 | .PP 31 | .SH RESTART 32 | When suprenode restarts it loses all registration information from associated 33 | edge nodes. It can take up to five minutes for the edge nodes to re-register and 34 | normal traffic flow to resume. 35 | .SH EXIT STATUS 36 | supernode is a daemon and any exit is an error 37 | .SH AUTHOR 38 | Luca Deri ( deri (at) ntop.org ), Richard Andrews ( andrews (at) ntop.org ), Don Bindner 39 | .SH SEE ALSO 40 | ifconfig(8) edge(8) 41 | -------------------------------------------------------------------------------- /n2n_v1/tuntap_freebsd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2007-09 - Luca Deri 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not see see 16 | */ 17 | 18 | #include "n2n.h" 19 | 20 | #ifdef __FreeBSD__ 21 | 22 | void tun_close(tuntap_dev *device); 23 | 24 | /* ********************************** */ 25 | 26 | #define N2N_FREEBSD_TAPDEVICE_SIZE 32 27 | int tuntap_open(tuntap_dev *device /* ignored */, 28 | char *dev, 29 | char *device_ip, 30 | char *device_mask, 31 | const char * device_mac, 32 | int mtu) { 33 | int i; 34 | char tap_device[N2N_FREEBSD_TAPDEVICE_SIZE]; 35 | 36 | for (i = 0; i < 255; i++) { 37 | snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i); 38 | 39 | device->fd = open(tap_device, O_RDWR); 40 | if(device->fd > 0) { 41 | traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); 42 | break; 43 | } 44 | } 45 | 46 | if(device->fd < 0) { 47 | traceEvent(TRACE_ERROR, "Unable to open tap device"); 48 | return(-1); 49 | } else { 50 | char buf[256]; 51 | FILE *fd; 52 | 53 | device->ip_addr = inet_addr(device_ip); 54 | 55 | if ( device_mac ) 56 | { 57 | /* FIXME - This is not tested. Might be wrong syntax for OS X */ 58 | 59 | /* Set the hw address before bringing the if up. */ 60 | snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s", 61 | i, device_mac); 62 | system(buf); 63 | } 64 | 65 | snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", 66 | i, device_ip, device_mask, mtu); 67 | system(buf); 68 | 69 | traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", 70 | i, device_ip, device_mask); 71 | 72 | /* Read MAC address */ 73 | 74 | snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i); 75 | /* traceEvent(TRACE_INFO, "%s", buf); */ 76 | 77 | fd = popen(buf, "r"); 78 | if(fd < 0) { 79 | tun_close(device); 80 | return(-1); 81 | } else { 82 | int a, b, c, d, e, f; 83 | 84 | buf[0] = 0; 85 | fgets(buf, sizeof(buf), fd); 86 | pclose(fd); 87 | 88 | if(buf[0] == '\0') { 89 | traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address"); 90 | exit(0); 91 | } 92 | 93 | traceEvent(TRACE_NORMAL, "Interface tap%d mac %s", i, buf); 94 | if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { 95 | device->mac_addr[0] = a, device->mac_addr[1] = b; 96 | device->mac_addr[2] = c, device->mac_addr[3] = d; 97 | device->mac_addr[4] = e, device->mac_addr[5] = f; 98 | } 99 | } 100 | } 101 | 102 | 103 | /* read_mac(dev, device->mac_addr); */ 104 | return(device->fd); 105 | } 106 | 107 | /* ********************************** */ 108 | 109 | int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 110 | return(read(tuntap->fd, buf, len)); 111 | } 112 | 113 | /* ********************************** */ 114 | 115 | int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 116 | return(write(tuntap->fd, buf, len)); 117 | } 118 | 119 | /* ********************************** */ 120 | 121 | void tuntap_close(struct tuntap_dev *tuntap) { 122 | close(tuntap->fd); 123 | } 124 | 125 | #endif 126 | -------------------------------------------------------------------------------- /n2n_v1/tuntap_linux.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2007-09 - Luca Deri 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, see 16 | */ 17 | 18 | #include "n2n.h" 19 | 20 | #ifdef __linux__ 21 | 22 | static void read_mac(char *ifname, char *mac_addr) { 23 | int _sock, res; 24 | struct ifreq ifr; 25 | macstr_t mac_addr_buf; 26 | 27 | memset (&ifr,0,sizeof(struct ifreq)); 28 | 29 | /* Dummy socket, just to make ioctls with */ 30 | _sock=socket(PF_INET, SOCK_DGRAM, 0); 31 | strcpy(ifr.ifr_name, ifname); 32 | res = ioctl(_sock,SIOCGIFHWADDR,&ifr); 33 | if (res<0) { 34 | perror ("Get hw addr"); 35 | } else 36 | memcpy(mac_addr, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6); 37 | 38 | traceEvent(TRACE_NORMAL, "Interface %s has MAC %s", 39 | ifname, 40 | macaddr_str(mac_addr, mac_addr_buf, sizeof(mac_addr_buf))); 41 | close(_sock); 42 | } 43 | 44 | /* ********************************** */ 45 | 46 | /** @brief Open and configure the TAP device for packet read/write. 47 | * 48 | * This routine creates the interface via the tuntap driver then uses ifconfig 49 | * to configure address/mask and MTU. 50 | * 51 | * @param device - [inout] a device info holder object 52 | * @param dev - user-defined name for the new iface, 53 | * if NULL system will assign a name 54 | * @param device_ip - address of iface 55 | * @param device_mask - netmask for device_ip 56 | * @param mtu - MTU for device_ip 57 | * 58 | * @return - negative value on error 59 | * - non-negative file-descriptor on success 60 | */ 61 | int tuntap_open(tuntap_dev *device, 62 | char *dev, /* user-definable interface name, eg. edge0 */ 63 | char *device_ip, 64 | char *device_mask, 65 | const char * device_mac, 66 | int mtu) { 67 | char *tuntap_device = "/dev/net/tun"; 68 | #define N2N_LINUX_SYSTEMCMD_SIZE 128 69 | char buf[N2N_LINUX_SYSTEMCMD_SIZE]; 70 | struct ifreq ifr; 71 | int rc; 72 | 73 | device->fd = open(tuntap_device, O_RDWR); 74 | if(device->fd < 0) { 75 | printf("ERROR: ioctl() [%s][%d]\n", strerror(errno), errno); 76 | return -1; 77 | } 78 | 79 | memset(&ifr, 0, sizeof(ifr)); 80 | ifr.ifr_flags = IFF_TAP|IFF_NO_PI; /* Want a TAP device for layer 2 frames. */ 81 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); 82 | rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr); 83 | 84 | if(rc < 0) { 85 | traceEvent(TRACE_ERROR, "ioctl() [%s][%d]\n", strerror(errno), rc); 86 | close(device->fd); 87 | return -1; 88 | } 89 | 90 | if ( device_mac ) 91 | { 92 | /* Set the hw address before bringing the if up. */ 93 | snprintf(buf, sizeof(buf), "/sbin/ifconfig %s hw ether %s", 94 | ifr.ifr_name, device_mac ); 95 | system(buf); 96 | traceEvent(TRACE_INFO, "Setting MAC: %s", buf); 97 | } 98 | 99 | snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s netmask %s mtu %d up", 100 | ifr.ifr_name, device_ip, device_mask, mtu); 101 | system(buf); 102 | traceEvent(TRACE_INFO, "Bringing up: %s", buf); 103 | 104 | device->ip_addr = inet_addr(device_ip); 105 | device->device_mask = inet_addr(device_mask); 106 | read_mac(dev, (char*)device->mac_addr); 107 | return(device->fd); 108 | } 109 | 110 | int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 111 | return(read(tuntap->fd, buf, len)); 112 | } 113 | 114 | int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 115 | return(write(tuntap->fd, buf, len)); 116 | } 117 | 118 | void tuntap_close(struct tuntap_dev *tuntap) { 119 | close(tuntap->fd); 120 | } 121 | 122 | #endif /* #ifdef __linux__ */ 123 | -------------------------------------------------------------------------------- /n2n_v1/tuntap_osx.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2007-09 - Luca Deri 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not see see 16 | */ 17 | 18 | #include "n2n.h" 19 | 20 | #ifdef _DARWIN_ 21 | 22 | void tun_close(tuntap_dev *device); 23 | 24 | /* ********************************** */ 25 | 26 | #define N2N_OSX_TAPDEVICE_SIZE 32 27 | int tuntap_open(tuntap_dev *device /* ignored */, 28 | char *dev, 29 | char *device_ip, 30 | char *device_mask, 31 | const char * device_mac, 32 | int mtu) { 33 | int i; 34 | char tap_device[N2N_OSX_TAPDEVICE_SIZE]; 35 | 36 | for (i = 0; i < 255; i++) { 37 | snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i); 38 | 39 | device->fd = open(tap_device, O_RDWR); 40 | if(device->fd > 0) { 41 | traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); 42 | break; 43 | } 44 | } 45 | 46 | if(device->fd < 0) { 47 | traceEvent(TRACE_ERROR, "Unable to open tap device"); 48 | return(-1); 49 | } else { 50 | char buf[256]; 51 | FILE *fd; 52 | 53 | device->ip_addr = inet_addr(device_ip); 54 | 55 | if ( device_mac ) 56 | { 57 | /* FIXME - This is not tested. Might be wrong syntax for OS X */ 58 | 59 | /* Set the hw address before bringing the if up. */ 60 | snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s", 61 | i, device_mac); 62 | system(buf); 63 | } 64 | 65 | snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", 66 | i, device_ip, device_mask, mtu); 67 | system(buf); 68 | 69 | traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", 70 | i, device_ip, device_mask); 71 | 72 | /* Read MAC address */ 73 | 74 | snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i); 75 | /* traceEvent(TRACE_INFO, "%s", buf); */ 76 | 77 | fd = popen(buf, "r"); 78 | if(fd < 0) { 79 | tun_close(device); 80 | return(-1); 81 | } else { 82 | int a, b, c, d, e, f; 83 | 84 | buf[0] = 0; 85 | fgets(buf, sizeof(buf), fd); 86 | pclose(fd); 87 | 88 | if(buf[0] == '\0') { 89 | traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address"); 90 | exit(0); 91 | } 92 | 93 | traceEvent(TRACE_NORMAL, "Interface tap%d [MTU %d] mac %s", i, mtu, buf); 94 | if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { 95 | device->mac_addr[0] = a, device->mac_addr[1] = b; 96 | device->mac_addr[2] = c, device->mac_addr[3] = d; 97 | device->mac_addr[4] = e, device->mac_addr[5] = f; 98 | } 99 | } 100 | } 101 | 102 | 103 | /* read_mac(dev, device->mac_addr); */ 104 | return(device->fd); 105 | } 106 | 107 | /* ********************************** */ 108 | 109 | int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 110 | return(read(tuntap->fd, buf, len)); 111 | } 112 | 113 | /* ********************************** */ 114 | 115 | int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 116 | return(write(tuntap->fd, buf, len)); 117 | } 118 | 119 | /* ********************************** */ 120 | 121 | void tuntap_close(struct tuntap_dev *tuntap) { 122 | close(tuntap->fd); 123 | } 124 | 125 | #endif 126 | -------------------------------------------------------------------------------- /n2n_v1/win32/DotNet/n2n.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual C++ Express 2008 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "edge", "n2n.vcproj", "{4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "supernode", "supernode\supernode.vcproj", "{1F7F0E45-7DE9-4CE2-845F-38D59C2E4F51}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Debug|Win32.Build.0 = Debug|Win32 16 | {4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Release|Win32.ActiveCfg = Release|Win32 17 | {4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Release|Win32.Build.0 = Release|Win32 18 | {1F7F0E45-7DE9-4CE2-845F-38D59C2E4F51}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {1F7F0E45-7DE9-4CE2-845F-38D59C2E4F51}.Debug|Win32.Build.0 = Debug|Win32 20 | {1F7F0E45-7DE9-4CE2-845F-38D59C2E4F51}.Release|Win32.ActiveCfg = Release|Win32 21 | {1F7F0E45-7DE9-4CE2-845F-38D59C2E4F51}.Release|Win32.Build.0 = Release|Win32 22 | EndGlobalSection 23 | GlobalSection(SolutionProperties) = preSolution 24 | HideSolutionNode = FALSE 25 | EndGlobalSection 26 | EndGlobal 27 | -------------------------------------------------------------------------------- /n2n_v1/win32/DotNet/n2n.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ForgotFun/n2n/205c363dd106190dc4c59550b8b137b680e2291e/n2n_v1/win32/DotNet/n2n.suo -------------------------------------------------------------------------------- /n2n_v1/win32/DotNet/n2n.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 26 | 29 | 32 | 35 | 38 | 41 | 54 | 57 | 60 | 63 | 73 | 76 | 79 | 82 | 85 | 88 | 91 | 94 | 95 | 103 | 106 | 109 | 112 | 115 | 118 | 127 | 130 | 133 | 136 | 148 | 151 | 154 | 157 | 160 | 163 | 166 | 169 | 170 | 171 | 172 | 173 | 174 | 179 | 182 | 183 | 186 | 187 | 190 | 191 | 194 | 195 | 198 | 199 | 202 | 203 | 206 | 207 | 210 | 211 | 212 | 217 | 220 | 221 | 224 | 225 | 228 | 229 | 232 | 233 | 236 | 237 | 240 | 241 | 244 | 245 | 248 | 249 | 250 | 255 | 256 | 259 | 260 | 261 | 262 | 263 | 264 | -------------------------------------------------------------------------------- /n2n_v1/win32/DotNet/supernode/supernode.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 26 | 29 | 32 | 35 | 38 | 41 | 52 | 55 | 58 | 61 | 69 | 72 | 75 | 78 | 81 | 84 | 87 | 90 | 91 | 99 | 102 | 105 | 108 | 111 | 114 | 125 | 128 | 131 | 134 | 144 | 147 | 150 | 153 | 156 | 159 | 162 | 165 | 166 | 167 | 168 | 169 | 170 | 175 | 178 | 179 | 182 | 183 | 186 | 187 | 190 | 191 | 194 | 195 | 198 | 199 | 202 | 203 | 206 | 207 | 208 | 213 | 216 | 217 | 220 | 221 | 224 | 225 | 228 | 229 | 232 | 233 | 236 | 237 | 240 | 241 | 244 | 245 | 246 | 251 | 252 | 255 | 256 | 257 | 258 | 259 | 260 | -------------------------------------------------------------------------------- /n2n_v1/win32/getopt.h: -------------------------------------------------------------------------------- 1 | /* Declarations for getopt. 2 | Copyright (C) 1989,90,91,92,93,94,96,97,98 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | 5 | The GNU C Library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public License as 7 | published by the Free Software Foundation; either version 2 of the 8 | License, or (at your option) any later version. 9 | 10 | The GNU C Library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with the GNU C Library; see the file COPYING.LIB. If not, 17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 | Boston, MA 02111-1307, USA. */ 19 | 20 | #ifndef _GETOPT_H 21 | 22 | #ifndef __need_getopt 23 | # define _GETOPT_H 1 24 | #endif 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | /* For communication from `getopt' to the caller. 31 | When `getopt' finds an option that takes an argument, 32 | the argument value is returned here. 33 | Also, when `ordering' is RETURN_IN_ORDER, 34 | each non-option ARGV-element is returned here. */ 35 | 36 | extern char *optarg; 37 | 38 | /* Index in ARGV of the next element to be scanned. 39 | This is used for communication to and from the caller 40 | and for communication between successive calls to `getopt'. 41 | 42 | On entry to `getopt', zero means this is the first call; initialize. 43 | 44 | When `getopt' returns -1, this is the index of the first of the 45 | non-option elements that the caller should itself scan. 46 | 47 | Otherwise, `optind' communicates from one call to the next 48 | how much of ARGV has been scanned so far. */ 49 | 50 | extern int optind; 51 | 52 | /* Callers store zero here to inhibit the error message `getopt' prints 53 | for unrecognized options. */ 54 | 55 | extern int opterr; 56 | 57 | /* Set to an option character which was unrecognized. */ 58 | 59 | extern int optopt; 60 | 61 | #ifndef __need_getopt 62 | /* Describe the long-named options requested by the application. 63 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector 64 | of `struct option' terminated by an element containing a name which is 65 | zero. 66 | 67 | The field `has_arg' is: 68 | no_argument (or 0) if the option does not take an argument, 69 | required_argument (or 1) if the option requires an argument, 70 | optional_argument (or 2) if the option takes an optional argument. 71 | 72 | If the field `flag' is not NULL, it points to a variable that is set 73 | to the value given in the field `val' when the option is found, but 74 | left unchanged if the option is not found. 75 | 76 | To have a long-named option do something other than set an `int' to 77 | a compiled-in constant, such as set a value from `optarg', set the 78 | option's `flag' field to zero and its `val' field to a nonzero 79 | value (the equivalent single-letter option character, if there is 80 | one). For long options that have a zero `flag' field, `getopt' 81 | returns the contents of the `val' field. */ 82 | 83 | struct option 84 | { 85 | # if defined __STDC__ && __STDC__ 86 | const char *name; 87 | # else 88 | char *name; 89 | # endif 90 | /* has_arg can't be an enum because some compilers complain about 91 | type mismatches in all the code that assumes it is an int. */ 92 | int has_arg; 93 | int *flag; 94 | int val; 95 | }; 96 | 97 | /* Names for the values of the `has_arg' field of `struct option'. */ 98 | 99 | # define no_argument 0 100 | # define required_argument 1 101 | # define optional_argument 2 102 | #endif /* need getopt */ 103 | 104 | 105 | /* Get definitions and prototypes for functions to process the 106 | arguments in ARGV (ARGC of them, minus the program name) for 107 | options given in OPTS. 108 | 109 | Return the option character from OPTS just read. Return -1 when 110 | there are no more options. For unrecognized options, or options 111 | missing arguments, `optopt' is set to the option letter, and '?' is 112 | returned. 113 | 114 | The OPTS string is a list of characters which are recognized option 115 | letters, optionally followed by colons, specifying that that letter 116 | takes an argument, to be placed in `optarg'. 117 | 118 | If a letter in OPTS is followed by two colons, its argument is 119 | optional. This behavior is specific to the GNU `getopt'. 120 | 121 | The argument `--' causes premature termination of argument 122 | scanning, explicitly telling `getopt' that there are no more 123 | options. 124 | 125 | If OPTS begins with `--', then non-option arguments are treated as 126 | arguments to the option '\0'. This behavior is specific to the GNU 127 | `getopt'. */ 128 | 129 | #if defined __STDC__ && __STDC__ 130 | # ifdef __GNU_LIBRARY__ 131 | /* Many other libraries have conflicting prototypes for getopt, with 132 | differences in the consts, in stdlib.h. To avoid compilation 133 | errors, only prototype getopt for the GNU C library. */ 134 | extern int getopt (int __argc, char *const *__argv, const char *__shortopts); 135 | # else /* not __GNU_LIBRARY__ */ 136 | extern int getopt (); 137 | # endif /* __GNU_LIBRARY__ */ 138 | 139 | # ifndef __need_getopt 140 | extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts, 141 | const struct option *__longopts, int *__longind); 142 | extern int getopt_long_only (int __argc, char *const *__argv, 143 | const char *__shortopts, 144 | const struct option *__longopts, int *__longind); 145 | 146 | /* Internal only. Users should not call this directly. */ 147 | extern int _getopt_internal (int __argc, char *const *__argv, 148 | const char *__shortopts, 149 | const struct option *__longopts, int *__longind, 150 | int __long_only); 151 | # endif 152 | #else /* not __STDC__ */ 153 | extern int getopt (); 154 | # ifndef __need_getopt 155 | extern int getopt_long (); 156 | extern int getopt_long_only (); 157 | 158 | extern int _getopt_internal (); 159 | # endif 160 | #endif /* __STDC__ */ 161 | 162 | #ifdef __cplusplus 163 | } 164 | #endif 165 | 166 | /* Make sure we later can get all the definitions and declarations. */ 167 | #undef __need_getopt 168 | 169 | #endif /* getopt.h */ 170 | -------------------------------------------------------------------------------- /n2n_v1/win32/getopt1.c: -------------------------------------------------------------------------------- 1 | /* getopt_long and getopt_long_only entry points for GNU getopt. 2 | Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 3 | Free Software Foundation, Inc. 4 | This file is part of the GNU C Library. 5 | 6 | The GNU C Library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Library General Public License as 8 | published by the Free Software Foundation; either version 2 of the 9 | License, or (at your option) any later version. 10 | 11 | The GNU C Library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Library General Public License for more details. 15 | 16 | You should have received a copy of the GNU Library General Public 17 | License along with the GNU C Library; see the file COPYING.LIB. If not, 18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 19 | Boston, MA 02111-1307, USA. */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include 23 | #endif 24 | 25 | #include "getopt.h" 26 | 27 | #if !defined __STDC__ || !__STDC__ 28 | /* This is a separate conditional since some stdc systems 29 | reject `defined (const)'. */ 30 | #ifndef const 31 | #define const 32 | #endif 33 | #endif 34 | 35 | #include 36 | 37 | /* Comment out all this code if we are using the GNU C Library, and are not 38 | actually compiling the library itself. This code is part of the GNU C 39 | Library, but also included in many other GNU distributions. Compiling 40 | and linking in this code is a waste when using the GNU C library 41 | (especially if it is a shared library). Rather than having every GNU 42 | program understand `configure --with-gnu-libc' and omit the object files, 43 | it is simpler to just do this in the source for each such file. */ 44 | 45 | #define GETOPT_INTERFACE_VERSION 2 46 | #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 47 | #include 48 | #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 49 | #define ELIDE_CODE 50 | #endif 51 | #endif 52 | 53 | #ifndef ELIDE_CODE 54 | 55 | 56 | /* This needs to come after some library #include 57 | to get __GNU_LIBRARY__ defined. */ 58 | #ifdef __GNU_LIBRARY__ 59 | #include 60 | #endif 61 | 62 | #ifndef NULL 63 | #define NULL 0 64 | #endif 65 | 66 | int 67 | getopt_long (argc, argv, options, long_options, opt_index) 68 | int argc; 69 | char *const *argv; 70 | const char *options; 71 | const struct option *long_options; 72 | int *opt_index; 73 | { 74 | return _getopt_internal (argc, argv, options, long_options, opt_index, 0); 75 | } 76 | 77 | /* Like getopt_long, but '-' as well as '--' can indicate a long option. 78 | If an option that starts with '-' (not '--') doesn't match a long option, 79 | but does match a short option, it is parsed as a short option 80 | instead. */ 81 | 82 | int 83 | getopt_long_only (argc, argv, options, long_options, opt_index) 84 | int argc; 85 | char *const *argv; 86 | const char *options; 87 | const struct option *long_options; 88 | int *opt_index; 89 | { 90 | return _getopt_internal (argc, argv, options, long_options, opt_index, 1); 91 | } 92 | 93 | 94 | #endif /* Not ELIDE_CODE. */ 95 | 96 | #ifdef TEST 97 | 98 | #include 99 | 100 | int 101 | main (argc, argv) 102 | int argc; 103 | char **argv; 104 | { 105 | int c; 106 | int digit_optind = 0; 107 | 108 | while (1) 109 | { 110 | int this_option_optind = optind ? optind : 1; 111 | int option_index = 0; 112 | static struct option long_options[] = 113 | { 114 | {"add", 1, 0, 0}, 115 | {"append", 0, 0, 0}, 116 | {"delete", 1, 0, 0}, 117 | {"verbose", 0, 0, 0}, 118 | {"create", 0, 0, 0}, 119 | {"file", 1, 0, 0}, 120 | {0, 0, 0, 0} 121 | }; 122 | 123 | c = getopt_long (argc, argv, "abc:d:0123456789", 124 | long_options, &option_index); 125 | if (c == -1) 126 | break; 127 | 128 | switch (c) 129 | { 130 | case 0: 131 | printf ("option %s", long_options[option_index].name); 132 | if (optarg) 133 | printf (" with arg %s", optarg); 134 | printf ("\n"); 135 | break; 136 | 137 | case '0': 138 | case '1': 139 | case '2': 140 | case '3': 141 | case '4': 142 | case '5': 143 | case '6': 144 | case '7': 145 | case '8': 146 | case '9': 147 | if (digit_optind != 0 && digit_optind != this_option_optind) 148 | printf ("digits occur in two different argv-elements.\n"); 149 | digit_optind = this_option_optind; 150 | printf ("option %c\n", c); 151 | break; 152 | 153 | case 'a': 154 | printf ("option a\n"); 155 | break; 156 | 157 | case 'b': 158 | printf ("option b\n"); 159 | break; 160 | 161 | case 'c': 162 | printf ("option c with value `%s'\n", optarg); 163 | break; 164 | 165 | case 'd': 166 | printf ("option d with value `%s'\n", optarg); 167 | break; 168 | 169 | case '?': 170 | break; 171 | 172 | default: 173 | printf ("?? getopt returned character code 0%o ??\n", c); 174 | } 175 | } 176 | 177 | if (optind < argc) 178 | { 179 | printf ("non-option ARGV-elements: "); 180 | while (optind < argc) 181 | printf ("%s ", argv[optind++]); 182 | printf ("\n"); 183 | } 184 | 185 | exit (0); 186 | } 187 | 188 | #endif /* TEST */ 189 | -------------------------------------------------------------------------------- /n2n_v1/win32/n2n_win32.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | (C) 2007 - Luca Deri 4 | 5 | */ 6 | 7 | #ifndef _N2N_WIN32_H_ 8 | #define _N2N_WIN32_H_ 9 | 10 | #ifndef _CRT_SECURE_NO_WARNINGS 11 | #define _CRT_SECURE_NO_WARNINGS 12 | #endif 13 | 14 | #include "wintap.h" 15 | 16 | typedef unsigned int u_int32_t; 17 | typedef unsigned short u_int16_t; 18 | typedef unsigned char u_int8_t; 19 | typedef int int32_t; 20 | typedef short int16_t; 21 | typedef char int8_t; 22 | 23 | #define snprintf _snprintf 24 | #define strdup _strdup 25 | 26 | #define socklen_t int 27 | 28 | #define ETHER_ADDR_LEN 6 29 | /* 30 | * Structure of a 10Mb/s Ethernet header. 31 | */ 32 | struct ether_header { 33 | u_char ether_dhost[ETHER_ADDR_LEN]; 34 | u_char ether_shost[ETHER_ADDR_LEN]; 35 | u_short ether_type; 36 | }; 37 | 38 | /* ************************************* */ 39 | 40 | struct ip { 41 | #if BYTE_ORDER == LITTLE_ENDIAN 42 | u_char ip_hl:4, /* header length */ 43 | ip_v:4; /* version */ 44 | #else 45 | u_char ip_v:4, /* version */ 46 | ip_hl:4; /* header length */ 47 | #endif 48 | u_char ip_tos; /* type of service */ 49 | short ip_len; /* total length */ 50 | u_short ip_id; /* identification */ 51 | short ip_off; /* fragment offset field */ 52 | #define IP_DF 0x4000 /* dont fragment flag */ 53 | #define IP_MF 0x2000 /* more fragments flag */ 54 | #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ 55 | u_char ip_ttl; /* time to live */ 56 | u_char ip_p; /* protocol */ 57 | u_short ip_sum; /* checksum */ 58 | struct in_addr ip_src,ip_dst; /* source and dest address */ 59 | }; 60 | 61 | 62 | /* ************************************* */ 63 | 64 | typedef struct tuntap_dev { 65 | HANDLE device_handle; 66 | char *device_name; 67 | char *ifName; 68 | OVERLAPPED overlap_read, overlap_write; 69 | u_int8_t mac_addr[6]; 70 | u_int32_t ip_addr, device_mask; 71 | u_int mtu; 72 | } tuntap_dev; 73 | 74 | #endif -------------------------------------------------------------------------------- /n2n_v1/win32/wintap.c: -------------------------------------------------------------------------------- 1 | /* 2 | (C) 2007-08 - Luca Deri 3 | */ 4 | 5 | #include "../n2n.h" 6 | #include "n2n_win32.h" 7 | 8 | /* 1500 bytes payload + 14 bytes ethernet header + 4 bytes VLAN tag */ 9 | #define MTU 1518 10 | 11 | void initWin32() { 12 | WSADATA wsaData; 13 | int err; 14 | 15 | err = WSAStartup(MAKEWORD(2, 2), &wsaData ); 16 | if( err != 0 ) { 17 | /* Tell the user that we could not find a usable */ 18 | /* WinSock DLL. */ 19 | printf("FATAL ERROR: unable to initialise Winsock 2.x."); 20 | exit(-1); 21 | } 22 | } 23 | 24 | int open_wintap(struct tuntap_dev *device, 25 | char *device_ip, char *device_mask, 26 | char *device_mac, int mtu) { 27 | HKEY key, key2; 28 | LONG rc; 29 | char regpath[1024], cmd[256]; 30 | char adapterid[1024]; 31 | char adaptername[1024]; 32 | char tapname[1024]; 33 | long len; 34 | int found = 0; 35 | int err, i; 36 | ULONG status = TRUE; 37 | 38 | memset(device, 0, sizeof(struct tuntap_dev)); 39 | device->device_handle = INVALID_HANDLE_VALUE; 40 | device->device_name = NULL; 41 | device->ifName = NULL; 42 | device->ip_addr = inet_addr(device_ip); 43 | 44 | /* Open registry and look for network adapters */ 45 | if((rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key))) { 46 | printf("Unable to read registry: [rc=%d]\n", rc); 47 | exit(-1); 48 | } 49 | 50 | for (i = 0; ; i++) { 51 | len = sizeof(adapterid); 52 | if(RegEnumKeyEx(key, i, (LPCWSTR)adapterid, &len, 0, 0, 0, NULL)) 53 | break; 54 | 55 | /* Find out more about this adapter */ 56 | 57 | _snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapterid); 58 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCSTR)regpath, 0, KEY_READ, &key2)) 59 | continue; 60 | 61 | len = sizeof(adaptername); 62 | err = RegQueryValueEx(key2, "Name", 0, 0, adaptername, &len); 63 | 64 | RegCloseKey(key2); 65 | 66 | if(err) 67 | continue; 68 | 69 | if(device->device_name) { 70 | if(!strcmp(device->device_name, adapterid)) { 71 | found = 1; 72 | break; 73 | } else 74 | continue; 75 | } 76 | 77 | if(device->ifName) { 78 | if(!strcmp(device->ifName, adaptername)) { 79 | found = 1; 80 | break; 81 | } else 82 | continue; 83 | } 84 | 85 | _snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid); 86 | device->device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 87 | 0, /* Don't let other processes share or open 88 | the resource until the handle's been closed */ 89 | 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); 90 | if(device->device_handle != INVALID_HANDLE_VALUE) { 91 | found = 1; 92 | break; 93 | } 94 | } 95 | 96 | RegCloseKey(key); 97 | 98 | if(!found) { 99 | printf("No Windows tap device found!\n"); 100 | exit(0); 101 | } 102 | 103 | /* ************************************** */ 104 | 105 | if(!device->device_name) 106 | device->device_name = _strdup(adapterid); 107 | 108 | if(!device->ifName) 109 | device->ifName = _strdup(adaptername); 110 | 111 | /* Try to open the corresponding tap device->device_name */ 112 | 113 | if(device->device_handle == INVALID_HANDLE_VALUE) { 114 | _snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device->device_name); 115 | device->device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, 116 | OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); 117 | } 118 | 119 | if(device->device_handle == INVALID_HANDLE_VALUE) { 120 | printf("%s (%s) is not a usable Windows tap device\n", device->device_name, device->ifName); 121 | exit(-1); 122 | } 123 | 124 | /* Get MAC address from tap device->device_name */ 125 | 126 | if(!DeviceIoControl(device->device_handle, TAP_IOCTL_GET_MAC, 127 | device->mac_addr, sizeof(device->mac_addr), 128 | device->mac_addr, sizeof(device->mac_addr), &len, 0)) { 129 | printf("Could not get MAC address from Windows tap %s (%s)\n", 130 | device->device_name, device->ifName); 131 | return -1; 132 | } 133 | 134 | device->mtu = mtu; 135 | 136 | printf("Open device [name=%s][ip=%s][ifName=%s][MTU=%d][mac=%02X:%02X:%02X:%02X:%02X:%02X]\n", 137 | device->device_name, device_ip, device->ifName, device->mtu, 138 | device->mac_addr[0] & 0xFF, 139 | device->mac_addr[1] & 0xFF, 140 | device->mac_addr[2] & 0xFF, 141 | device->mac_addr[3] & 0xFF, 142 | device->mac_addr[4] & 0xFF, 143 | device->mac_addr[5] & 0xFF); 144 | 145 | /* ****************** */ 146 | 147 | printf("Setting %s device address...\n", device->ifName); 148 | 149 | _snprintf(cmd, sizeof(cmd), 150 | "netsh interface ip set address \"%s\" static %s %s", 151 | device->ifName, device_ip, device_mask); 152 | 153 | if(system(cmd) == 0) { 154 | device->ip_addr = inet_addr(device_ip); 155 | device->device_mask = inet_addr(device_mask); 156 | printf("Device %s set to %s/%s\n", 157 | device->ifName, device_ip, device_mask); 158 | } else 159 | printf("WARNING: Unable to set device %s IP address [%s]\n", 160 | device->ifName, cmd); 161 | 162 | /* ****************** */ 163 | 164 | if(device->mtu != DEFAULT_MTU) 165 | printf("WARNING: MTU set is not supported on Windows\n"); 166 | 167 | /* set driver media status to 'connected' (i.e. set the interface up) */ 168 | if (!DeviceIoControl (device->device_handle, TAP_IOCTL_SET_MEDIA_STATUS, 169 | &status, sizeof (status), 170 | &status, sizeof (status), &len, NULL)) 171 | printf("WARNING: Unable to enable TAP adapter\n"); 172 | 173 | /* 174 | * Initialize overlapped structures 175 | */ 176 | device->overlap_read.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 177 | device->overlap_write.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 178 | if (!device->overlap_read.hEvent || !device->overlap_write.hEvent) { 179 | return -1; 180 | } 181 | 182 | return(0); 183 | } 184 | 185 | /* ************************************************ */ 186 | 187 | int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) 188 | { 189 | DWORD read_size, last_err; 190 | 191 | ResetEvent(tuntap->overlap_read.hEvent); 192 | if (ReadFile(tuntap->device_handle, buf, len, &read_size, &tuntap->overlap_read)) { 193 | //printf("tun_read(len=%d)\n", read_size); 194 | return read_size; 195 | } 196 | switch (last_err = GetLastError()) { 197 | case ERROR_IO_PENDING: 198 | WaitForSingleObject(tuntap->overlap_read.hEvent, INFINITE); 199 | GetOverlappedResult(tuntap->device_handle, &tuntap->overlap_read, &read_size, FALSE); 200 | return read_size; 201 | break; 202 | default: 203 | printf("GetLastError() returned %d\n", last_err); 204 | break; 205 | } 206 | 207 | return -1; 208 | } 209 | /* ************************************************ */ 210 | 211 | int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) 212 | { 213 | DWORD write_size; 214 | 215 | //printf("tun_write(len=%d)\n", len); 216 | 217 | ResetEvent(tuntap->overlap_write.hEvent); 218 | if (WriteFile(tuntap->device_handle, 219 | buf, 220 | len, 221 | &write_size, 222 | &tuntap->overlap_write)) { 223 | //printf("DONE tun_write(len=%d)\n", write_size); 224 | return write_size; 225 | } 226 | switch (GetLastError()) { 227 | case ERROR_IO_PENDING: 228 | WaitForSingleObject(tuntap->overlap_write.hEvent, INFINITE); 229 | GetOverlappedResult(tuntap->device_handle, &tuntap->overlap_write, 230 | &write_size, FALSE); 231 | return write_size; 232 | break; 233 | default: 234 | break; 235 | } 236 | 237 | return -1; 238 | } 239 | 240 | /* ************************************************ */ 241 | 242 | int tuntap_open(tuntap_dev *device, char *dev, char *device_ip, 243 | char *device_mask, const char * device_mac, int mtu) { 244 | return(open_wintap(device, device_ip, device_mask, device_mac, mtu)); 245 | } 246 | 247 | /* ************************************************ */ 248 | 249 | void tuntap_close(struct tuntap_dev *tuntap) { 250 | CloseHandle(tuntap->device_handle); 251 | } 252 | 253 | /* ************************************************ */ 254 | 255 | #if 0 256 | int main(int argc, char* argv[]) { 257 | struct tuntap_dev tuntap; 258 | int i; 259 | int mtu = 1400; 260 | 261 | printf("Welcome to n2n\n"); 262 | initWin32(); 263 | open_wintap(&tuntap, "1.2.3.20", "255.255.255.0", mtu); 264 | 265 | for(i=0; i<10; i++) { 266 | u_char buf[MTU]; 267 | int rc; 268 | 269 | rc = tun_read(&tuntap, buf, sizeof(buf)); 270 | buf[0]=2; 271 | buf[1]=3; 272 | buf[2]=4; 273 | 274 | printf("tun_read returned %d\n", rc); 275 | rc = tun_write(&tuntap, buf, rc); 276 | printf("tun_write returned %d\n", rc); 277 | } 278 | // rc = tun_open (device->device_name, IF_MODE_TUN); 279 | WSACleanup (); 280 | return(0); 281 | } 282 | 283 | #endif 284 | -------------------------------------------------------------------------------- /n2n_v1/win32/wintap.h: -------------------------------------------------------------------------------- 1 | /* 2 | (C) 2007 - Luca Deri 3 | */ 4 | 5 | #ifndef _WINTAP_H_ 6 | #define _WINTAP_H_ 7 | 8 | #undef UNICODE 9 | #undef _UNICODE 10 | #define _CRT_SECURE_NO_WARNINGS 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | 17 | 18 | //=============================================== 19 | // This file is included both by OpenVPN and 20 | // the TAP-Win32 driver and contains definitions 21 | // common to both. 22 | //=============================================== 23 | 24 | //============= 25 | // TAP IOCTLs 26 | //============= 27 | 28 | #define TAP_CONTROL_CODE(request,method) \ 29 | CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS) 30 | 31 | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED) 32 | #define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED) 33 | #define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED) 34 | #define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED) 35 | #define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED) 36 | #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED) 37 | #define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED) 38 | #define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED) 39 | #define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED) 40 | 41 | //================= 42 | // Registry keys 43 | //================= 44 | 45 | #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}" 46 | #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" 47 | 48 | //====================== 49 | // Filesystem prefixes 50 | //====================== 51 | 52 | #define USERMODEDEVICEDIR "\\\\.\\Global\\" 53 | #define SYSDEVICEDIR "\\Device\\" 54 | #define USERDEVICEDIR "\\DosDevices\\Global\\" 55 | #define TAPSUFFIX ".tap" 56 | 57 | //========================================================= 58 | // TAP_COMPONENT_ID -- This string defines the TAP driver 59 | // type -- different component IDs can reside in the system 60 | // simultaneously. 61 | //========================================================= 62 | 63 | #define TAP_COMPONENT_ID "tap0801" 64 | 65 | extern void initWin32(); 66 | 67 | #endif -------------------------------------------------------------------------------- /n2n_v2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(n2n) 2 | cmake_minimum_required(VERSION 2.6) 3 | 4 | # N2n information 5 | set(N2N_VERSION 2.1.0) 6 | set(N2N_OSNAME ${CMAKE_SYSTEM}) 7 | 8 | # N2n specific params 9 | if(NOT DEFINED N2N_OPTION_AES) 10 | set(N2N_OPTION_AES ON) 11 | endif(NOT DEFINED N2N_OPTION_AES) 12 | 13 | add_definitions(-DN2N_VERSION='\"${N2N_VERSION}\"' -DN2N_OSNAME='\"${N2N_OSNAME}\"') 14 | 15 | if(N2N_OPTION_AES) 16 | add_definitions(-DN2N_HAVE_AES) 17 | endif(N2N_OPTION_AES) 18 | 19 | # Build information 20 | if(NOT DEFINED BUILD_SHARED_LIBS) 21 | set(BUILD_SHARED_LIBS OFF) 22 | endif(NOT DEFINED BUILD_SHARED_LIBS) 23 | 24 | if(NOT DEFINED CMAKE_BUILD_TYPE) 25 | set(CMAKE_BUILD_TYPE None) 26 | endif(NOT DEFINED CMAKE_BUILD_TYPE) 27 | #set(CMAKE_BUILD_TYPE Debug) 28 | #set(CMAKE_BUILD_TYPE Release) 29 | 30 | #Ultrasparc64 users experiencing SIGBUS should try the following gcc options 31 | #(thanks to Robert Gibbon) 32 | #PLATOPTS_SPARC64=-mcpu=ultrasparc -pipe -fomit-frame-pointer -ffast-math -finline-functions -fweb -frename-registers -mapp-regs 33 | 34 | # None 35 | set(CMAKE_C_FLAGS "-Wall -Wshadow -Wpointer-arith -Wmissing-declarations -Wnested-externs") 36 | set(CMAKE_CXX_FLAGS "-Wall -Wshadow -Wpointer-arith -Wmissing-declarations -Wnested-externs") 37 | # Debug 38 | set(CMAKE_C_FLAGS_DEBUG "-g") 39 | set(CMAKE_CXX_FLAGS_DEBUG "-g") 40 | # Release 41 | set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG") 42 | set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") 43 | 44 | ## DEBUG FOR CMAKE 45 | #message(${N2N_VERSION}) 46 | #message(${N2N_OSNAME}) 47 | ##message(${CMAKE_BUILD_TYPE}) 48 | #message(${N2N_OPTION_AES}) 49 | ## DEBUG FOR CMAKE 50 | 51 | add_library(n2n n2n.c 52 | n2n_keyfile.c 53 | wire.c 54 | minilzo.c 55 | twofish.c 56 | transform_null.c 57 | transform_tf.c 58 | transform_aes.c 59 | tuntap_freebsd.c 60 | tuntap_netbsd.c 61 | tuntap_linux.c 62 | tuntap_osx.c 63 | version.c 64 | ) 65 | 66 | if(DEFINED WIN32) 67 | add_subdirectory(win32) 68 | target_link_libraries(n2n n2n_win32) 69 | endif(DEFINED WIN32) 70 | 71 | if(N2N_OPTION_AES) 72 | target_link_libraries(n2n crypto) 73 | endif(N2N_OPTION_AES) 74 | 75 | # For Solaris (or OpenSolaris?) 76 | #target_link_libraries(n2n socket nsl) 77 | 78 | add_executable(edge edge.c) 79 | target_link_libraries(edge n2n) 80 | 81 | add_executable(supernode sn.c) 82 | target_link_libraries(supernode n2n) 83 | 84 | add_executable(test test.c) 85 | target_link_libraries(test n2n) 86 | 87 | add_executable(benchmark benchmark.c) 88 | target_link_libraries(benchmark n2n) 89 | 90 | install(TARGETS edge supernode 91 | RUNTIME DESTINATION sbin 92 | LIBRARY DESTINATION lib 93 | ARCHIVE DESTINATION lib 94 | ) 95 | 96 | # Documentation 97 | if(DEFINED UNIX) 98 | add_dependencies(n2n doc) 99 | file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/doc) 100 | add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/doc/edge.8.gz 101 | COMMAND gzip -c ${PROJECT_SOURCE_DIR}/edge.8 > ${PROJECT_BINARY_DIR}/doc/edge.8.gz 102 | DEPENDS ${PROJECT_SOURCE_DIR}/edge.8 103 | ) 104 | 105 | add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/doc/supernode.1.gz 106 | COMMAND gzip -c ${PROJECT_SOURCE_DIR}/supernode.1 > ${PROJECT_BINARY_DIR}/doc/supernode.1.gz 107 | DEPENDS ${PROJECT_SOURCE_DIR}/supernode.1 108 | ) 109 | 110 | add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/doc/n2n_v2.7.gz 111 | COMMAND gzip -c ${PROJECT_SOURCE_DIR}/n2n_v2.7 > ${PROJECT_BINARY_DIR}/doc/n2n_v2.7.gz 112 | DEPENDS ${PROJECT_SOURCE_DIR}/n2n_v2.7 113 | ) 114 | 115 | add_custom_target(doc DEPENDS ${PROJECT_BINARY_DIR}/doc/edge.8.gz 116 | ${PROJECT_BINARY_DIR}/doc/supernode.1.gz 117 | ${PROJECT_BINARY_DIR}/doc/n2n_v2.7.gz 118 | ) 119 | 120 | set_source_files_properties(${PROJECT_BINARY_DIR}/doc/edge.8.gz 121 | ${PROJECT_BINARY_DIR}/doc/supernode.1.gz 122 | ${PROJECT_BINARY_DIR}/doc/n2n_v2.7.gz 123 | PROPERTIES GENERATED 1) 124 | 125 | install(FILES ${PROJECT_BINARY_DIR}/doc/edge.8.gz 126 | DESTINATION /usr/share/man8) 127 | install(FILES ${PROJECT_BINARY_DIR}/doc/supernode.1.gz 128 | DESTINATION /usr/share/man1) 129 | install(FILES ${PROJECT_BINARY_DIR}/doc/n2n_v2.7.gz 130 | DESTINATION /usr/share/man7) 131 | endif(DEFINED UNIX) 132 | -------------------------------------------------------------------------------- /n2n_v2/INSTALL: -------------------------------------------------------------------------------- 1 | INSTALL 2 | 3 | To build the programs: 4 | 5 | $ make 6 | 7 | To install the programs and man pages: 8 | 9 | $ make install 10 | 11 | or 12 | 13 | $ make PREFIX=/usr/local install 14 | 15 | 16 | RPM Package 17 | ----------- 18 | 19 | These steps should work with RPM based Linux distributions since rpmbuild was 20 | split from the rpm utility (c RedHat 9). 21 | 22 | 23 | To build an RPM the easy way follow these steps. 24 | 25 | 1. Build SRPM 26 | 27 | $ cd n2n 28 | $ scripts/mk_SRPM.sh 29 | 30 | Look for where the src.rpm file was put ( "Wrote:" ). 31 | 32 | 2. Build binary RPM from SRPM 33 | 34 | $ rpm -i path/to/n2n-.src.rpm 35 | $ rpmbuild -bb n2n.spec 36 | 37 | 38 | All this can be done as non-root user if you have a ~/.rpmmacros file with this 39 | line in it: 40 | 41 | %_topdir /home/username/rpmtopdir 42 | 43 | 44 | To build an RPM the hard way follow these steps. 45 | 46 | $ cp -a n2ndir n2n-2.0 47 | $ tar czf n2n-2.0.tar.gz n2n-2.0 48 | $ mv n2n-2.0.tar.gz /usr/src/redhat/SOURCES 49 | $ cp n2ndir/n2n.spec /usr/src/redhat/SPECS 50 | $ rpmbuild -bb n2n.spec 51 | -------------------------------------------------------------------------------- /n2n_v2/Makefile: -------------------------------------------------------------------------------- 1 | 2 | N2N_VERSION=2.1.0 3 | N2N_OSNAME=$(shell uname -p) 4 | 5 | ######## 6 | 7 | CC=gcc 8 | DEBUG?=-g3 9 | #OPTIMIZATION?=-O2 10 | WARN?=-Wall -Wshadow -Wpointer-arith -Wmissing-declarations -Wnested-externs 11 | 12 | #Ultrasparc64 users experiencing SIGBUS should try the following gcc options 13 | #(thanks to Robert Gibbon) 14 | PLATOPTS_SPARC64=-mcpu=ultrasparc -pipe -fomit-frame-pointer -ffast-math -finline-functions -fweb -frename-registers -mapp-regs 15 | 16 | N2N_DEFINES= 17 | N2N_OBJS_OPT= 18 | LIBS_EDGE_OPT= 19 | 20 | N2N_OPTION_AES?="yes" 21 | #N2N_OPTION_AES=no 22 | 23 | ifeq ($(N2N_OPTION_AES), "yes") 24 | N2N_DEFINES+="-DN2N_HAVE_AES" 25 | LIBS_EDGE_OPT+=-lcrypto 26 | endif 27 | 28 | CFLAGS+=$(DEBUG) $(OPTIMIZATION) $(WARN) $(OPTIONS) $(PLATOPTS) $(N2N_DEFINES) 29 | 30 | INSTALL=install 31 | MKDIR=mkdir -p 32 | 33 | INSTALL_PROG=$(INSTALL) -m755 34 | INSTALL_DOC=$(INSTALL) -m644 35 | 36 | 37 | # DESTDIR set in debian make system 38 | PREFIX?=$(DESTDIR)/usr 39 | #BINDIR=$(PREFIX)/bin 40 | SBINDIR=$(PREFIX)/sbin 41 | MANDIR?=$(PREFIX)/share/man 42 | MAN1DIR=$(MANDIR)/man1 43 | MAN7DIR=$(MANDIR)/man7 44 | MAN8DIR=$(MANDIR)/man8 45 | 46 | N2N_LIB=n2n.a 47 | N2N_OBJS=n2n.o n2n_keyfile.o wire.o minilzo.o twofish.o \ 48 | transform_null.o transform_tf.o transform_aes.o \ 49 | tuntap_freebsd.o tuntap_netbsd.o tuntap_linux.o tuntap_osx.o version.o 50 | LIBS_EDGE+=$(LIBS_EDGE_OPT) 51 | LIBS_SN= 52 | 53 | #For OpenSolaris (Solaris too?) 54 | ifeq ($(shell uname), SunOS) 55 | LIBS_EDGE+=-lsocket -lnsl 56 | LIBS_SN+=-lsocket -lnsl 57 | endif 58 | 59 | APPS=edge 60 | APPS+=supernode 61 | 62 | DOCS=edge.8.gz supernode.1.gz n2n_v2.7.gz 63 | 64 | all: $(APPS) $(DOCS) 65 | 66 | edge: edge.c $(N2N_LIB) n2n_wire.h n2n.h Makefile 67 | $(CC) $(CFLAGS) edge.c $(N2N_LIB) $(LIBS_EDGE) -o edge 68 | 69 | test: test.c $(N2N_LIB) n2n_wire.h n2n.h Makefile 70 | $(CC) $(CFLAGS) test.c $(N2N_LIB) $(LIBS_EDGE) -o test 71 | 72 | supernode: sn.c $(N2N_LIB) n2n.h Makefile 73 | $(CC) $(CFLAGS) sn.c $(N2N_LIB) $(LIBS_SN) -o supernode 74 | 75 | benchmark: benchmark.c $(N2N_LIB) n2n_wire.h n2n.h Makefile 76 | $(CC) $(CFLAGS) benchmark.c $(N2N_LIB) $(LIBS_SN) -o benchmark 77 | 78 | .c.o: n2n.h n2n_keyfile.h n2n_transforms.h n2n_wire.h twofish.h Makefile 79 | $(CC) $(CFLAGS) -c $< 80 | 81 | %.gz : % 82 | gzip -c $< > $@ 83 | 84 | $(N2N_LIB): $(N2N_OBJS) 85 | ar rcs $(N2N_LIB) $(N2N_OBJS) 86 | # $(RANLIB) $@ 87 | 88 | version.o: Makefile 89 | $(CC) $(CFLAGS) -DN2N_VERSION='"$(N2N_VERSION)"' -DN2N_OSNAME='"$(N2N_OSNAME)"' -c version.c 90 | 91 | clean: 92 | rm -rf $(N2N_OBJS) $(N2N_LIB) $(APPS) $(DOCS) test *.dSYM *~ 93 | 94 | install: edge supernode edge.8.gz supernode.1.gz n2n_v2.7.gz 95 | echo "MANDIR=$(MANDIR)" 96 | $(MKDIR) $(SBINDIR) $(MAN1DIR) $(MAN7DIR) $(MAN8DIR) 97 | $(INSTALL_PROG) supernode $(SBINDIR)/ 98 | $(INSTALL_PROG) edge $(SBINDIR)/ 99 | $(INSTALL_DOC) edge.8.gz $(MAN8DIR)/ 100 | $(INSTALL_DOC) supernode.1.gz $(MAN1DIR)/ 101 | $(INSTALL_DOC) n2n_v2.7.gz $(MAN7DIR)/ 102 | -------------------------------------------------------------------------------- /n2n_v2/NEW_FEATURES.txt: -------------------------------------------------------------------------------- 1 | 2 | Between 2.0.x and 2.1.x 3 | 4 | * Better ming Windows build support. 5 | * Added -E flag to allow multicast ethernet traffic. 6 | 7 | -------------------------------------------------------------------------------- /n2n_v2/README: -------------------------------------------------------------------------------- 1 | 2 | 3 | Edge node 4 | --------- 5 | 6 | You need to start an edge node on each host you want to connect with the *same* 7 | community. 8 | 9 | 0. become root 10 | 11 | 1. create tun device 12 | # tunctl -t tun0 13 | 14 | 3. enable the edge process 15 | # ./edge -d n2n0 -c mynetwork -k encryptme -u 99 -g 99 -m 3C:A0:12:34:56:78 -a 1.2.3.4 -l a.b.c.d:xyw 16 | or 17 | # N2N_KEY=encryptme ./edge -d n2n0 -c mynetwork -u 99 -g 99 -m 3C:A0:12:34:56:78 -a 1.2.3.4 -l a.b.c.d:xyw 18 | 19 | Once you have this worked out, you can add the "-f" option to make edge detach 20 | and run as a daemon. 21 | 22 | Note that -u, -g and -f options are not available for Windows. 23 | 24 | Supernode 25 | -------- 26 | 27 | You need to start the supernode once 28 | 29 | 1. ./supernode -l 1234 -v 30 | 31 | 32 | Dropping Root Privileges and SUID-Root Executables (UNIX) 33 | -------------------------------------------------- 34 | 35 | The edge node uses superuser privileges to create a TAP network interface 36 | device. Once this is created root privileges are not required and can constitute 37 | a security hazard if there is some way for an attacker to take control of an 38 | edge process while it is running. Edge will drop to a non-privileged user if you 39 | specify the -u and -g options. These are numeric IDs. Consult 40 | /etc/passwd. 41 | 42 | You may choose to install edge SUID-root to do this: 43 | 44 | 1. Become root 45 | 2. chown root:root edge 46 | 3. chmod +s edge 47 | done 48 | 49 | Any user can now run edge. You may not want this, but it may be convenient and 50 | safe if your host has only one login user. 51 | 52 | 53 | Running As a Daemon (UNIX) 54 | ------------------- 55 | 56 | Unless given "-f" as a command line option, edge will call daemon(3) after 57 | successful setup. This causes the process to fork a child which closes stdin, 58 | stdout and stderr then sets itself as process group leader. When this is done, 59 | the edge command returns immediately and you will only see the edge process in 60 | the process listings, eg. from ps or top. 61 | 62 | If the edge command returns 0 then the daemon started successfully. If it 63 | returns non-zero then edge failed to start up for some reason. When edge starts 64 | running as a daemon, all logging goes to syslog daemon.info facility. 65 | 66 | 67 | IPv6 Support 68 | ------------ 69 | 70 | n2n supports the carriage of IPv6 packets within the n2n tunnel. N2n does not 71 | yet use IPv6 for transport between edges and supernodes. 72 | 73 | To make IPv6 carriage work you need to manually add IPv6 addresses to the TAP 74 | interfaces at each end. There is currently no way to specify an IPv6 address on 75 | the edge command line. 76 | 77 | eg. under linux: 78 | 79 | on hostA: 80 | [hostA] # /sbin/ip -6 addr add fc00:abcd:1234::7/48 dev n2n0 81 | 82 | on hostB: 83 | [hostB] # /sbin/ip -6 addr add fc00:abcd:1234::6/48 dev n2n0 84 | 85 | You may find it useful to make use of tunctl from the uml-utilities 86 | package. Tunctl allow you to bring up a TAP interface and configure addressing 87 | prior to starting edge. It also allows edge to be restarted without the 88 | interface closing (which would normally affect routing tables). 89 | 90 | Once the IPv6 addresses are configured and edge started, IPv6 neighbor discovery 91 | packets flow (get broadcast) and IPv6 entities self arrange. Test your IPv6 92 | setup with ping6 - the IPv6 ping command. 93 | 94 | 95 | Performance Notes 96 | ----------------- 97 | 98 | The time taken to perform a ping test for various ciphers is given below: 99 | 100 | Test: ping -f -l 8 -s 800 -c 10000 101 | 102 | AES (-O0) 11820 103 | TF (-O0) 25761 104 | 105 | TF (-O2) 20554 106 | 107 | AES (-O3) 12532 108 | TF (-O3) 14046 109 | NULL (-O3) 10659 110 | 111 | (C) 2007-2010 - Luca Deri , Richard Andrews 112 | -------------------------------------------------------------------------------- /n2n_v2/benchmark.c: -------------------------------------------------------------------------------- 1 | #include "n2n_wire.h" 2 | #include "n2n_transforms.h" 3 | #include "n2n.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | uint8_t PKT_CONTENT[]={ 11 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 12 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 13 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 14 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 15 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 16 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 17 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 18 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 19 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 20 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 21 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 22 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 23 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 24 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 25 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 26 | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; 27 | 28 | /* Prototypes */ 29 | static ssize_t do_encode_packet( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c ); 30 | 31 | int main( int argc, char * argv[] ) 32 | { 33 | uint8_t pktbuf[N2N_PKT_BUF_SIZE]; 34 | n2n_trans_op_t transop_null; 35 | 36 | n2n_common_t cmn; 37 | n2n_PACKET_t pkt; 38 | n2n_community_t c; 39 | 40 | struct timeval t1; 41 | struct timeval t2; 42 | 43 | size_t i; 44 | size_t n; 45 | size_t idx; 46 | size_t rem; 47 | ssize_t nw; 48 | ssize_t tdiff; 49 | 50 | transop_null_init( &transop_null ); 51 | memset(c,0,sizeof(N2N_COMMUNITY_SIZE)); 52 | 53 | n=10000; 54 | memcpy( c, "abc123def456", 12 ); 55 | 56 | gettimeofday( &t1, NULL ); 57 | for(i=0; i %u nsec each) %u.%06u -> %u.%06u.\n", i, tdiff, (tdiff *1000)/i, (uint32_t)t1.tv_sec, (uint32_t)t1.tv_usec, (uint32_t)t2.tv_sec, (uint32_t)t2.tv_usec ); 81 | 82 | return 0; 83 | } 84 | 85 | static ssize_t do_encode_packet( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c ) 86 | { 87 | n2n_mac_t destMac={0,1,2,3,4,5}; 88 | n2n_common_t cmn; 89 | n2n_PACKET_t pkt; 90 | size_t idx; 91 | 92 | 93 | memset( &cmn, 0, sizeof(cmn) ); 94 | cmn.ttl = N2N_DEFAULT_TTL; 95 | cmn.pc = n2n_packet; 96 | cmn.flags=0; /* no options, not from supernode, no socket */ 97 | memcpy( cmn.community, c, N2N_COMMUNITY_SIZE ); 98 | 99 | memset( &pkt, 0, sizeof(pkt) ); 100 | memcpy( pkt.srcMac, destMac, N2N_MAC_SIZE); 101 | memcpy( pkt.dstMac, destMac, N2N_MAC_SIZE); 102 | 103 | pkt.sock.family=0; /* do not encode sock */ 104 | 105 | idx=0; 106 | encode_PACKET( pktbuf, &idx, &cmn, &pkt ); 107 | traceEvent( TRACE_DEBUG, "encoded PACKET header of size=%u", (unsigned int)idx ); 108 | 109 | return idx; 110 | } 111 | -------------------------------------------------------------------------------- /n2n_v2/debian/README.Debian: -------------------------------------------------------------------------------- 1 | n2n for Debian 2 | -------------- 3 | 4 | This package depends on the kernel having the TUN/TAP driver configured in using 5 | CONFIG_TUN=yes. 6 | 7 | -- Richard Andrews Thu, 10 Jul 2008 22:38:02 +1000 8 | -------------------------------------------------------------------------------- /n2n_v2/debian/changelog: -------------------------------------------------------------------------------- 1 | n2n (2.1.0-1) unstable; urgency=low 2 | 3 | * Split package in two. 4 | * Move manpage for edge to section 8. 5 | * Install manpage for n2n_v2 to section 7. 6 | * Create init.d files for the daemons. 7 | 8 | -- Kim Hansen Sun, 04 Apr 2010 21:40:46 +0200 9 | 10 | n2n (2.0-1) hardy; urgency=low 11 | 12 | * New upstream release 13 | 14 | -- Richard Andrews Tue, 30 Oct 2009 22:26:04 +1100 15 | 16 | n2n (1.3-1) hardy; urgency=low 17 | 18 | * New upstream release 19 | 20 | -- Richard Andrews Fri, 30 Jan 2009 23:49:56 +1100 21 | 22 | n2n (1.2-1) unstable; urgency=low 23 | 24 | * Initial release 25 | 26 | -- Richard Andrews Thu, 10 Jul 2008 22:38:02 +1000 27 | 28 | -------------------------------------------------------------------------------- /n2n_v2/debian/compat: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /n2n_v2/debian/control: -------------------------------------------------------------------------------- 1 | Source: n2n 2 | Section: net 3 | Priority: extra 4 | Maintainer: Jean-Baptiste Denis 5 | Build-Depends: cdbs, debhelper (>= 5), libc6-dev (>= 2.0), dpatch, gcc, libssl-dev 6 | Standards-Version: 3.7.2 7 | 8 | Package: n2n 9 | Architecture: any 10 | Depends: n2n-edge, n2n-supernode 11 | Description: dummy package for transition purposes 12 | A dummy package for transition purposes that depends on n2n-edge and 13 | n2n-supernode 14 | 15 | Package: n2n-edge 16 | Architecture: any 17 | Suggests: uml-utilities 18 | Depends: ${shlibs:Depends}, ${misc:Depends} 19 | Conflicts: n2n (<< 2.1.0-1) 20 | Replaces: n2n (<< 2.1.0-1) 21 | Description: a layer-two peer-to-peer virtual private network (VPN) 22 | n2n is a layer-two peer-to-peer virtual private network (VPN) which allows 23 | users to exploit features typical of P2P applications at network instead of 24 | application level. This means that users can gain native IP visibility (e.g. 25 | two PCs belonging to the same n2n network can ping each other) and be 26 | reachable with the same network IP address regardless of the network where 27 | they currently belong. In a nutshell, as OpenVPN moved SSL from application 28 | (e.g. used to implement the https protocol) to network protocol, n2n moves 29 | P2P from application to network level. 30 | . 31 | Edge is the edge node daemon for n2n which creates a TAP interface to expose 32 | the n2n virtual LAN. 33 | 34 | Package: n2n-supernode 35 | Architecture: any 36 | Suggests: n2n-edge 37 | Depends: ${shlibs:Depends}, ${misc:Depends} 38 | Conflicts: n2n (<< 2.1.0-1) 39 | Replaces: n2n (<< 2.1.0-1) 40 | Description: a layer-two peer-to-peer virtual private network (VPN) 41 | n2n is a layer-two peer-to-peer virtual private network (VPN) which allows 42 | users to exploit features typical of P2P applications at network instead of 43 | application level. This means that users can gain native IP visibility (e.g. 44 | two PCs belonging to the same n2n network can ping each other) and be 45 | reachable with the same network IP address regardless of the network where 46 | they currently belong. In a nutshell, as OpenVPN moved SSL from application 47 | (e.g. used to implement the https protocol) to network protocol, n2n moves 48 | P2P from application to network level. 49 | . 50 | Supernode is a node introduction registry, broadcast conduit and packet relay 51 | node for the n2n system. 52 | -------------------------------------------------------------------------------- /n2n_v2/debian/copyright: -------------------------------------------------------------------------------- 1 | This package was debianized by Jean-Baptiste Denis on 2 | Thu, 20 Nov 2008 23:53:02 +1000. 3 | 4 | It was downloaded from http://www.ntop.org/n2n/ 5 | 6 | Upstream Author(s): 7 | 8 | Luca Deri 9 | Richard Andrews 10 | 11 | Copyright: 12 | 13 | Copyright (C) 2008 Luca Deri 14 | Copyright (C) 2008 Richard Andrews 15 | 16 | License: 17 | 18 | GPLv3 19 | 20 | The Debian packaging is (C) 2008, Richard Andrews , 21 | Luca Deri and is licensed under the GPLv3, see 22 | `/usr/share/common-licenses/GPL-3'. 23 | 24 | -------------------------------------------------------------------------------- /n2n_v2/debian/n2n-edge.default: -------------------------------------------------------------------------------- 1 | # Config file for the n2n edge node daemon. 2 | 3 | # Sets the n2n community name. All edges within the same community appear on 4 | # the same LAN (layer 2 network segment). Community name is 16 bytes in length. 5 | N2N_COMMUNITY="MyCommunityName" 6 | 7 | # Sets the twofish encryption key from ASCII text. All edges communicating must 8 | # use the same key and community name. 9 | N2N_KEY="MySecretCode" 10 | 11 | # Sets the n2n supernode IP address to register to. 12 | N2N_SUPERNODE="gw1.example.com" 13 | 14 | # Sets the n2n virtual LAN IP address being claimed. This is a private IP 15 | # address. All IP addresses in an n2n community typical belong to the same /24 16 | # net‐ work (ie. only the last octet of the IP addresses varies). 17 | N2N_IP="10.10.10.11" 18 | 19 | # Uncomment this to get edge node started. 20 | #N2N_EDGE_CONFIG_DONE="yes" 21 | 22 | -------------------------------------------------------------------------------- /n2n_v2/debian/n2n-edge.docs: -------------------------------------------------------------------------------- 1 | README 2 | -------------------------------------------------------------------------------- /n2n_v2/debian/n2n-edge.init: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | ### BEGIN INIT INFO 3 | # Provides: n2n-edge 4 | # Required-Start: $remote_fs $syslog 5 | # Required-Stop: $remote_fs $syslog 6 | # Default-Start: 2 3 4 5 7 | # Default-Stop: 0 1 6 8 | # Short-Description: n2n-edge 9 | # Description: Start n2n edge node daemon 10 | ### END INIT INFO 11 | 12 | # Init script for n2n edge node 13 | # Copyright (C) 2010 Kim Hansen 14 | # 15 | # This program is free software: you can redistribute it and/or modify 16 | # it under the terms of the GNU General Public License as published by 17 | # the Free Software Foundation, either version 3 of the License, or 18 | # (at your option) any later version. 19 | # 20 | # This program is distributed in the hope that it will be useful, 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 | # GNU General Public License for more details. 24 | # 25 | # You should have received a copy of the GNU General Public License 26 | # along with this program. If not, see . 27 | 28 | # Do NOT "set -e" 29 | 30 | # PATH should only include /usr/* if it runs after the mountnfs.sh script 31 | PATH=/sbin:/usr/sbin:/bin:/usr/bin 32 | DESC="n2n edge" 33 | NAME=n2n-edge 34 | DAEMON=/usr/sbin/edge 35 | SCRIPTNAME=/etc/init.d/$NAME 36 | 37 | # Exit if the package is not installed 38 | [ -x "$DAEMON" ] || exit 0 39 | 40 | # Read configuration variable file if it is present 41 | [ -r /etc/default/$NAME ] && . /etc/default/$NAME 42 | 43 | # Check config 44 | if [ -z "$N2N_EDGE_CONFIG_DONE" ] 45 | then 46 | echo "Warning: n2n-edge not configured, edit config file in /etc/default/$NAME." 1>&2 47 | exit 0 48 | fi 49 | 50 | # Load the VERBOSE setting and other rcS variables 51 | . /lib/init/vars.sh 52 | 53 | # Define LSB log_* functions. 54 | # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. 55 | . /lib/lsb/init-functions 56 | 57 | # 58 | # Function that starts the daemon/service 59 | # 60 | do_start() 61 | { 62 | # Return 63 | # 0 if daemon has been started 64 | # 1 if daemon was already running 65 | # 2 if daemon could not be started 66 | start-stop-daemon --start --quiet --user nobody --exec $DAEMON --test \ 67 | || return 1 68 | export N2N_KEY 69 | start-stop-daemon --start --quiet --user nobody --exec $DAEMON -- \ 70 | -a $N2N_IP -c $N2N_COMMUNITY -l $N2N_SUPERNODE:7654 -u $(id -u nobody) -g $(id -g nobody) \ 71 | $DAEMON_ARGS \ 72 | || return 2 73 | } 74 | 75 | # 76 | # Function that stops the daemon/service 77 | # 78 | do_stop() 79 | { 80 | # Return 81 | # 0 if daemon has been stopped 82 | # 1 if daemon was already stopped 83 | # 2 if daemon could not be stopped 84 | # other if a failure occurred 85 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user nobody --exec $DAEMON 86 | } 87 | 88 | case "$1" in 89 | start) 90 | [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" 91 | do_start 92 | case "$?" in 93 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 94 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; 95 | esac 96 | ;; 97 | stop) 98 | [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" 99 | do_stop 100 | case "$?" in 101 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 102 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; 103 | esac 104 | ;; 105 | restart|force-reload) 106 | log_daemon_msg "Restarting $DESC" "$NAME" 107 | do_stop 108 | case "$?" in 109 | 0|1) 110 | do_start 111 | case "$?" in 112 | 0) log_end_msg 0 ;; 113 | 1) log_end_msg 1 ;; # Old process is still running 114 | *) log_end_msg 1 ;; # Failed to start 115 | esac 116 | ;; 117 | *) 118 | # Failed to stop 119 | log_end_msg 1 120 | ;; 121 | esac 122 | ;; 123 | *) 124 | echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 125 | exit 3 126 | ;; 127 | esac 128 | 129 | true # Set exit status to 0 (succes) 130 | -------------------------------------------------------------------------------- /n2n_v2/debian/n2n-edge.install: -------------------------------------------------------------------------------- 1 | edge /usr/sbin 2 | -------------------------------------------------------------------------------- /n2n_v2/debian/n2n-edge.manpages: -------------------------------------------------------------------------------- 1 | edge.8 2 | n2n_v2.7 3 | -------------------------------------------------------------------------------- /n2n_v2/debian/n2n-supernode.init: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: n2n-supernode 4 | # Required-Start: $remote_fs $syslog 5 | # Required-Stop: $remote_fs $syslog 6 | # Default-Start: 2 3 4 5 7 | # Default-Stop: 0 1 6 8 | # Short-Description: n2n-supernode 9 | # Description: Start n2n supernode 10 | ### END INIT INFO 11 | 12 | # Init script for n2n supernode 13 | # Copyright (C) 2010 Kim Hansen 14 | # 15 | # This program is free software: you can redistribute it and/or modify 16 | # it under the terms of the GNU General Public License as published by 17 | # the Free Software Foundation, either version 3 of the License, or 18 | # (at your option) any later version. 19 | # 20 | # This program is distributed in the hope that it will be useful, 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 | # GNU General Public License for more details. 24 | # 25 | # You should have received a copy of the GNU General Public License 26 | # along with this program. If not, see . 27 | 28 | # Do NOT "set -e" 29 | 30 | # PATH should only include /usr/* if it runs after the mountnfs.sh script 31 | PATH=/sbin:/usr/sbin:/bin:/usr/bin 32 | DESC="n2n supernode" 33 | NAME=n2n-supernode 34 | DAEMON=/usr/sbin/supernode 35 | DAEMON_ARGS="" 36 | SCRIPTNAME=/etc/init.d/$NAME 37 | 38 | # Exit if the package is not installed 39 | [ -x "$DAEMON" ] || exit 0 40 | 41 | # Read configuration variable file if it is present 42 | [ -r /etc/default/$NAME ] && . /etc/default/$NAME 43 | 44 | # Load the VERBOSE setting and other rcS variables 45 | . /lib/init/vars.sh 46 | 47 | # Define LSB log_* functions. 48 | # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. 49 | . /lib/lsb/init-functions 50 | 51 | # 52 | # Function that starts the daemon/service 53 | # 54 | do_start() 55 | { 56 | # Return 57 | # 0 if daemon has been started 58 | # 1 if daemon was already running 59 | # 2 if daemon could not be started 60 | start-stop-daemon --start --quiet --user nobody --exec $DAEMON --test \ 61 | || return 1 62 | start-stop-daemon --start --quiet --user nobody --chuid nobody --exec $DAEMON -- \ 63 | $DAEMON_ARGS \ 64 | || return 2 65 | } 66 | 67 | # 68 | # Function that stops the daemon/service 69 | # 70 | do_stop() 71 | { 72 | # Return 73 | # 0 if daemon has been stopped 74 | # 1 if daemon was already stopped 75 | # 2 if daemon could not be stopped 76 | # other if a failure occurred 77 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user nobody --exec $DAEMON 78 | } 79 | 80 | case "$1" in 81 | start) 82 | [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" 83 | do_start 84 | case "$?" in 85 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 86 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; 87 | esac 88 | ;; 89 | stop) 90 | [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" 91 | do_stop 92 | case "$?" in 93 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 94 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; 95 | esac 96 | ;; 97 | restart|force-reload) 98 | log_daemon_msg "Restarting $DESC" "$NAME" 99 | do_stop 100 | case "$?" in 101 | 0|1) 102 | do_start 103 | case "$?" in 104 | 0) log_end_msg 0 ;; 105 | 1) log_end_msg 1 ;; # Old process is still running 106 | *) log_end_msg 1 ;; # Failed to start 107 | esac 108 | ;; 109 | *) 110 | # Failed to stop 111 | log_end_msg 1 112 | ;; 113 | esac 114 | ;; 115 | *) 116 | echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 117 | exit 3 118 | ;; 119 | esac 120 | 121 | true # Set exit status to 0 (succes) 122 | -------------------------------------------------------------------------------- /n2n_v2/debian/n2n-supernode.install: -------------------------------------------------------------------------------- 1 | supernode /usr/sbin 2 | -------------------------------------------------------------------------------- /n2n_v2/debian/n2n-supernode.manpages: -------------------------------------------------------------------------------- 1 | supernode.1 2 | -------------------------------------------------------------------------------- /n2n_v2/debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | include /usr/share/cdbs/1/rules/debhelper.mk 4 | include /usr/share/cdbs/1/class/makefile.mk 5 | 6 | -------------------------------------------------------------------------------- /n2n_v2/edge.8: -------------------------------------------------------------------------------- 1 | .TH edge 8 "17 Mar 2010" "n2n-2.1" "SUPERUSER COMMANDS" 2 | .SH NAME 3 | edge \- n2n edge node daemon 4 | .SH SYNOPSIS 5 | .B edge 6 | [\-d ] \-a \-c {\-k |\-K } 7 | [\-s ] \-l 8 | [\-p ] [\-u ] [\-g ] [-f] [\-m ] [\-r] [\-v] 9 | .SH DESCRIPTION 10 | N2N is a peer-to-peer VPN system. Edge is the edge node daemon for n2n which 11 | creates a TAP interface to expose the n2n virtual LAN. On startup n2n creates 12 | the TAP interface and configures it then registers with the supernode so it can 13 | begin to find other nodes in the community. 14 | .PP 15 | .SH OPTIONS 16 | .TP 17 | \-d 18 | sets the TAP device name as seen in ifconfig. Only available on Linux. 19 | .TP 20 | \-a {|static:|dhcp:0.0.0.0} 21 | sets the n2n virtual LAN IP address being claimed. This is a private IP 22 | address. All IP addresses in an n2n community typical belong to the same /24 23 | network (ie. only the last octet of the IP addresses varies). If DHCP is used to 24 | assign interface addresses then specify the address as 25 | .B -a dhcp:0.0.0.0 26 | .TP 27 | \-b 28 | cause edge to perform hostname resolution for the supernode address each time 29 | the supernode is periodically contacted. This can cause reliability problems 30 | because all packet processing stops while the supernode address is resolved 31 | which might take 15 seconds. 32 | .TP 33 | \-c 34 | sets the n2n community name. All edges within the same community appear on the 35 | same LAN (layer 2 network segment). Community name is 16 bytes in length. A name 36 | smaller than this is padded with 0x00 bytes and a name longer than this is 37 | truncated to take the first 16 bytes. 38 | .TP 39 | \-h 40 | write usage then exit. 41 | .TP 42 | \-k 43 | sets the twofish encryption key from ASCII text (see also N2N_KEY in 44 | ENVIRONMENT). All edges communicating must use the same key and community 45 | name. If neither -k nor -K is used to specify a key source then edge uses 46 | cleartext mode (no encryption). The -k and -K options are mutually exclusive. 47 | .TP 48 | \-K 49 | Reads a key-schedule file and populates the internal transform 50 | operations with the data found there. This mechanism allows keys to roll at 51 | pre-determined times for a group of hosts. Accurate time synchronisation is not 52 | required as older keys can be decoded for some time after expiry. If neither -k 53 | nor -K is used to specify a key source then edge uses cleartext mode (no 54 | encryption). The -k and -K options are mutually exclusive. 55 | .TP 56 | \-l : 57 | sets the n2n supernode IP address and port to register to. Up to 2 supernodes 58 | can be specified by two invocations of -l :. eg. 59 | .B edge -l 12.34.56.78:7654 -l 98.76.54.32:7654 60 | . 61 | .TP 62 | \-p 63 | binds edge to the given UDP port. Useful for keeping the same external socket 64 | across restarts of edge. This allows peer edges which know the edge socket to 65 | continue p2p operation without going back to the supernode. 66 | .TP 67 | \-t 68 | binds the edge management system to the given UDP port. Default 5644. Use this 69 | if you need to run multiple instance of edge; or something is bound to that 70 | port. 71 | .TP 72 | \-u 73 | causes the edge process to drop to the given user ID when privileges are no 74 | longer required (UNIX). 75 | .TP 76 | \-g 77 | causes the edge process to drop to the given group ID when privileges are no 78 | longer required (UNIX). 79 | .TP 80 | \-f 81 | disables daemon mode (UNIX) and causes edge to run in the foreground. 82 | .TP 83 | \-m 84 | start the TAP interface with the given MAC address. This is highly recommended 85 | as it means the same address will be used if edge stops and restarts. If this is 86 | not done, the ARP caches of all peers will be wrong and packets will not flow to 87 | this edge until the next ARP refresh. 88 | .TP 89 | \-M 90 | set the MTU of the edge interface in bytes. MTU is the largest packet fragment 91 | size allowed to be moved throught the interface. The default is 1400. 92 | .TP 93 | \-s 94 | set the netmask of edge interface in IPv4 dotted decimal notation. The default 95 | is 255.255.255.0 (ie. /24). 96 | .TP 97 | \-r 98 | enable IP packet forwarding/routing through the n2n virtual LAN. Without this 99 | option, IP packets arriving over n2n are dropped if not for the -a (or 100 | DHCP assigned) IP address of the edge interface. 101 | .TP 102 | \-E 103 | accept packets destined for multicast ethernet MAC addresses. These addresses 104 | are used in multicast ethernet and IPv6 neighbour discovery. If this option is 105 | not present these multicast packets are discarded as most users do not need or 106 | understand them. 107 | .TP 108 | \-v 109 | more verbose logging (may be specified several times for more verbosity). 110 | .SH ENVIRONMENT 111 | .TP 112 | .B N2N_KEY 113 | set the encryption key so it is not visible on the command line 114 | .SH EXAMPLES 115 | .TP 116 | .B edge \-d n2n0 \-c mynetwork \-k encryptme \-u 99 \-g 99 \-m DE:AD:BE:EF:01:23 \-a 192.168.254.7 \-p 50001 \-l 123.121.120.119:7654 117 | 118 | Start edge with TAP device n2n0 on community "mynetwork" with community 119 | supernode at 123.121.120.119 UDP port 7654 and bind the locally used UDP port to 120 | 50001. Use "encryptme" as the single permanent shared encryption key. Assign MAC 121 | address DE:AD:BE:EF:01:23 to the n2n interface and drop to user=99 and group=99 122 | after the TAP device is successfull configured. 123 | .PP 124 | Add the -f option to stop edge running as a daemon. 125 | .PP 126 | Somewhere else setup another edge with similar parameters, eg. 127 | 128 | .B edge \-d n2n0 \-c mynetwork \-k encryptme \-u 99 \-g 99 \-m DE:AD:BE:EF:01:21 \-a 192.168.254.5 \-p 50001 \-l 123.121.120.119:7654 129 | .PP 130 | Now you can ping from 192.168.254.5 to 192.168.254.7. 131 | .PP 132 | The MAC address (-m ) and virtual IP address (-a ) must be different 133 | on all edges in the same community. 134 | 135 | .SH KEY SCHEDULE FILES 136 | (See 137 | .B n2n_v2(7) 138 | for more details). 139 | 140 | The -K option reads a key schedule file. 141 | 142 | .B edge \-d n2n0 \-c mynetwork \-K /path/to/file \-u 99 \-g 99 \-m DE:AD:BE:EF:01:21 \-a 192.168.254.5 \-p 50001 \-l 123.121.120.119:7654 143 | .PP 144 | 145 | The key schedule file consists of line, one per key in the schedule. The purpose 146 | of key schedules is to encourage regular changing of the encryption keys used by 147 | a community. The file structure also allows for full binary keys to be specified 148 | as compared to the ASCII keys allowed by the single key injection. Each key line 149 | consists of the following: 150 | 151 | .B 152 | 153 | and are ASCII decimal values of the UNIX times during which the 154 | key is valid. is the index of the transform that applies 155 | to. is some text which is parsed by the transform module to derive the 156 | key for that line. 157 | 158 | Supported values are: 159 | .TP 160 | 2 = TwoFish 161 | has the form _. eg. 162 | 163 | .B 1252327945 1252328305 2 602_3d7c7769b34b2a4812f8c0e9d87ce9 164 | 165 | This specifies security association number 602 and a 16-octet key of numeric 166 | value 0x3d7c7769b34b2a4812f8c0e9d87ce9. is a 32-bit unsigned integer which 167 | is used to identify the encryption key to the receiver. The SA number is sent 168 | unencrypted so the receiver may find the correct key from the key 169 | schedule. is up to 16 octets although shorter keys are allowed. 170 | 171 | .TP 172 | 3 = AES-CBC 173 | has the form _. Same rules as TwoFish. 174 | 175 | .SH CLEARTEXT MODE 176 | If neither 177 | .B -k 178 | nor 179 | .B -K 180 | is specified then edge uses cleartext mode. In cleartext mode there is no 181 | transform of the packet data it is simply encrypted. This is useful for 182 | debugging n2n as packet contents can be seen clearly. 183 | 184 | To prevent accidental exposure of data, edge only enters cleartext mode when no 185 | keying parameters are specified. In the case where keying parameters are 186 | specified but no valid keys can be determined, edge exits with an error at 187 | startup. If all keys become invalid while running, edge continues to encode 188 | using the last key that was valid. 189 | 190 | .SH MANAGEMENT INTERFACE 191 | Edge provides a very simple management system on UDP port 5644. Send a newline 192 | to receive a status output. Send 'reload' to cause re-read of the 193 | keyfile. Send 'stop' to cause edge to exit cleanly. 194 | 195 | .SH EXIT STATUS 196 | edge is a daemon and any exit is an error. 197 | .SH AUTHORS 198 | .TP 199 | Richard Andrews 200 | andrews (at) ntop.org - n2n-1 maintainer and main author of n2n-2 201 | .TP 202 | Luca Deri 203 | deri (at) ntop.org - original author of n2n 204 | .TP 205 | Don Bindner 206 | (--) - significant contributions to n2n-1 207 | .SH SEE ALSO 208 | ifconfig(8) supernode(1) tunctl(8) n2n_v2(7) 209 | -------------------------------------------------------------------------------- /n2n_v2/gen_keyfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # (c) 2009 Richard Andrews 4 | 5 | # Program to generate a n2n_edge key schedule file for twofish keys 6 | # Each key line consists of the following element 7 | # 8 | # 9 | # where , are UNIX time_t values of key valid period 10 | # is the transform ID (=2 for twofish) 11 | # is twofish-specific data as follows 12 | # _ 13 | 14 | import os 15 | import sys 16 | import time 17 | import random 18 | 19 | NUM_KEYS=30 20 | KEY_LIFE=300 21 | KEY_LEN=16 22 | 23 | now=time.time() 24 | start_sa=random.randint( 0, 0xffffffff ) 25 | 26 | random.seed(now) # note now is a floating point time value 27 | 28 | def rand_key(): 29 | key=str() 30 | for i in range(0,KEY_LEN): 31 | key += "%02x"%( random.randint( 0, 255) ) 32 | 33 | return key 34 | 35 | for i in range(0,NUM_KEYS): 36 | from_time = now + (KEY_LIFE * (i-1) ) 37 | until_time = now + (KEY_LIFE * (i+1) ) 38 | key = rand_key() 39 | sa_idx = start_sa + i 40 | transform_id = random.randint( 2, 3 ) 41 | 42 | sys.stdout.write("%d %d %d %d_%s\n"%(from_time, until_time, transform_id,sa_idx, key) ) 43 | 44 | 45 | -------------------------------------------------------------------------------- /n2n_v2/minilzo.h: -------------------------------------------------------------------------------- 1 | /* minilzo.h -- mini subset of the LZO real-time data compression library 2 | 3 | This file is part of the LZO real-time data compression library. 4 | 5 | Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer 6 | Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer 7 | Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer 8 | Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer 9 | Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer 10 | Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer 11 | Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer 12 | Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer 13 | Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer 14 | Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer 15 | Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer 16 | Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer 17 | Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer 18 | All Rights Reserved. 19 | 20 | The LZO library is free software; you can redistribute it and/or 21 | modify it under the terms of the GNU General Public License as 22 | published by the Free Software Foundation; either version 2 of 23 | the License, or (at your option) any later version. 24 | 25 | The LZO library is distributed in the hope that it will be useful, 26 | but WITHOUT ANY WARRANTY; without even the implied warranty of 27 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 | GNU General Public License for more details. 29 | 30 | You should have received a copy of the GNU General Public License 31 | along with the LZO library; see the file COPYING. 32 | If not, write to the Free Software Foundation, Inc., 33 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 34 | 35 | Markus F.X.J. Oberhumer 36 | 37 | http://www.oberhumer.com/opensource/lzo/ 38 | */ 39 | 40 | /* 41 | * NOTE: 42 | * the full LZO package can be found at 43 | * http://www.oberhumer.com/opensource/lzo/ 44 | */ 45 | 46 | 47 | #ifndef __MINILZO_H 48 | #define __MINILZO_H 49 | 50 | #define MINILZO_VERSION 0x2030 51 | 52 | #ifdef __LZOCONF_H 53 | # error "you cannot use both LZO and miniLZO" 54 | #endif 55 | 56 | #undef LZO_HAVE_CONFIG_H 57 | #include "lzoconf.h" 58 | 59 | #if !defined(LZO_VERSION) || (LZO_VERSION != MINILZO_VERSION) 60 | # error "version mismatch in header files" 61 | #endif 62 | 63 | 64 | #ifdef __cplusplus 65 | extern "C" { 66 | #endif 67 | 68 | 69 | /*********************************************************************** 70 | // 71 | ************************************************************************/ 72 | 73 | /* Memory required for the wrkmem parameter. 74 | * When the required size is 0, you can also pass a NULL pointer. 75 | */ 76 | 77 | #define LZO1X_MEM_COMPRESS LZO1X_1_MEM_COMPRESS 78 | #define LZO1X_1_MEM_COMPRESS ((lzo_uint32) (16384L * lzo_sizeof_dict_t)) 79 | #define LZO1X_MEM_DECOMPRESS (0) 80 | 81 | 82 | /* compression */ 83 | LZO_EXTERN(int) 84 | lzo1x_1_compress ( const lzo_bytep src, lzo_uint src_len, 85 | lzo_bytep dst, lzo_uintp dst_len, 86 | lzo_voidp wrkmem ); 87 | 88 | /* decompression */ 89 | LZO_EXTERN(int) 90 | lzo1x_decompress ( const lzo_bytep src, lzo_uint src_len, 91 | lzo_bytep dst, lzo_uintp dst_len, 92 | lzo_voidp wrkmem /* NOT USED */ ); 93 | 94 | /* safe decompression with overrun testing */ 95 | LZO_EXTERN(int) 96 | lzo1x_decompress_safe ( const lzo_bytep src, lzo_uint src_len, 97 | lzo_bytep dst, lzo_uintp dst_len, 98 | lzo_voidp wrkmem /* NOT USED */ ); 99 | 100 | 101 | #ifdef __cplusplus 102 | } /* extern "C" */ 103 | #endif 104 | 105 | #endif /* already included */ 106 | 107 | -------------------------------------------------------------------------------- /n2n_v2/n2n.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2007-09 - Luca Deri 3 | * Richard Andrews 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, see 17 | * 18 | * Code contributions courtesy of: 19 | * Babak Farrokhi [FreeBSD port] 20 | * Lukasz Taczuk 21 | * 22 | */ 23 | 24 | #ifndef _N2N_H_ 25 | #define _N2N_H_ 26 | 27 | /* 28 | tunctl -t tun0 29 | tunctl -t tun1 30 | ifconfig tun0 1.2.3.4 up 31 | ifconfig tun1 1.2.3.5 up 32 | ./edge -d tun0 -l 2000 -r 127.0.0.1:3000 -c hello 33 | ./edge -d tun1 -l 3000 -r 127.0.0.1:2000 -c hello 34 | 35 | 36 | tunctl -u UID -t tunX 37 | */ 38 | 39 | #if defined(__APPLE__) && defined(__MACH__) 40 | #define _DARWIN_ 41 | #endif 42 | 43 | 44 | /* Some capability defaults which can be reset for particular platforms. */ 45 | #define N2N_HAVE_DAEMON 1 46 | #define N2N_HAVE_SETUID 1 47 | /* #define N2N_CAN_NAME_IFACE */ 48 | 49 | /* Moved here to define _CRT_SECURE_NO_WARNINGS before all the including takes place */ 50 | #ifdef WIN32 51 | #include "win32/n2n_win32.h" 52 | #undef N2N_HAVE_DAEMON 53 | #undef N2N_HAVE_SETUID 54 | #endif 55 | 56 | #include 57 | #include 58 | #include 59 | 60 | #ifndef WIN32 61 | #include 62 | #endif 63 | 64 | #ifndef _MSC_VER 65 | #include 66 | #endif /* #ifndef _MSC_VER */ 67 | 68 | #include 69 | #include 70 | #include 71 | 72 | #ifndef WIN32 73 | #include 74 | #include 75 | #include 76 | #include 77 | #include 78 | 79 | #ifdef __linux__ 80 | #include 81 | #include 82 | #define N2N_CAN_NAME_IFACE 1 83 | #endif /* #ifdef __linux__ */ 84 | 85 | #ifdef __FreeBSD__ 86 | #include 87 | #endif /* #ifdef __FreeBSD__ */ 88 | 89 | #include 90 | #include 91 | 92 | #define ETH_ADDR_LEN 6 93 | struct ether_hdr 94 | { 95 | uint8_t dhost[ETH_ADDR_LEN]; 96 | uint8_t shost[ETH_ADDR_LEN]; 97 | uint16_t type; /* higher layer protocol encapsulated */ 98 | } __attribute__ ((__packed__)); 99 | 100 | typedef struct ether_hdr ether_hdr_t; 101 | 102 | #ifdef __sun__ 103 | #include /* MIN() and MAX() declared here */ 104 | #undef N2N_HAVE_DAEMON 105 | #endif /* #ifdef __sun__ */ 106 | 107 | #include 108 | #include 109 | #include 110 | #include 111 | #include 112 | #include 113 | #include 114 | 115 | #define closesocket(a) close(a) 116 | #endif /* #ifndef WIN32 */ 117 | 118 | #include 119 | 120 | #include 121 | 122 | #ifdef WIN32 123 | #include "win32/wintap.h" 124 | #endif /* #ifdef WIN32 */ 125 | 126 | #include "n2n_wire.h" 127 | 128 | /* N2N_IFNAMSIZ is needed on win32 even if dev_name is not used after declaration */ 129 | #define N2N_IFNAMSIZ 16 /* 15 chars * NULL */ 130 | #ifndef WIN32 131 | typedef struct tuntap_dev { 132 | int fd; 133 | uint8_t mac_addr[6]; 134 | uint32_t ip_addr, device_mask; 135 | uint16_t mtu; 136 | char dev_name[N2N_IFNAMSIZ]; 137 | } tuntap_dev; 138 | 139 | #define SOCKET int 140 | #endif /* #ifndef WIN32 */ 141 | 142 | #define QUICKLZ 1 143 | 144 | /* N2N packet header indicators. */ 145 | #define MSG_TYPE_REGISTER 1 146 | #define MSG_TYPE_DEREGISTER 2 147 | #define MSG_TYPE_PACKET 3 148 | #define MSG_TYPE_REGISTER_ACK 4 149 | #define MSG_TYPE_REGISTER_SUPER 5 150 | #define MSG_TYPE_REGISTER_SUPER_ACK 6 151 | #define MSG_TYPE_REGISTER_SUPER_NAK 7 152 | #define MSG_TYPE_FEDERATION 8 153 | 154 | /* Set N2N_COMPRESSION_ENABLED to 0 to disable lzo1x compression of ethernet 155 | * frames. Doing this will break compatibility with the standard n2n packet 156 | * format so do it only for experimentation. All edges must be built with the 157 | * same value if they are to understand each other. */ 158 | #define N2N_COMPRESSION_ENABLED 1 159 | 160 | #define DEFAULT_MTU 1400 161 | 162 | /** Common type used to hold stringified IP addresses. */ 163 | typedef char ipstr_t[32]; 164 | 165 | /** Common type used to hold stringified MAC addresses. */ 166 | #define N2N_MACSTR_SIZE 32 167 | typedef char macstr_t[N2N_MACSTR_SIZE]; 168 | 169 | struct peer_info { 170 | struct peer_info * next; 171 | n2n_community_t community_name; 172 | n2n_mac_t mac_addr; 173 | n2n_sock_t sock; 174 | time_t last_seen; 175 | }; 176 | 177 | struct n2n_edge; /* defined in edge.c */ 178 | typedef struct n2n_edge n2n_edge_t; 179 | 180 | 181 | /* ************************************** */ 182 | 183 | #define TRACE_ERROR 0, __FILE__, __LINE__ 184 | #define TRACE_WARNING 1, __FILE__, __LINE__ 185 | #define TRACE_NORMAL 2, __FILE__, __LINE__ 186 | #define TRACE_INFO 3, __FILE__, __LINE__ 187 | #define TRACE_DEBUG 4, __FILE__, __LINE__ 188 | 189 | /* ************************************** */ 190 | 191 | #define SUPERNODE_IP "127.0.0.1" 192 | #define SUPERNODE_PORT 1234 193 | 194 | /* ************************************** */ 195 | 196 | #ifndef max 197 | #define max(a, b) ((a < b) ? b : a) 198 | #endif 199 | 200 | #ifndef min 201 | #define min(a, b) ((a > b) ? b : a) 202 | #endif 203 | 204 | /* ************************************** */ 205 | 206 | /* Variables */ 207 | /* extern TWOFISH *tf; */ 208 | extern int traceLevel; 209 | extern int useSyslog; 210 | extern const uint8_t broadcast_addr[6]; 211 | extern const uint8_t multicast_addr[6]; 212 | 213 | /* Functions */ 214 | extern void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...); 215 | extern int tuntap_open(tuntap_dev *device, char *dev, const char *address_mode, char *device_ip, 216 | char *device_mask, const char * device_mac, int mtu); 217 | extern int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len); 218 | extern int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len); 219 | extern void tuntap_close(struct tuntap_dev *tuntap); 220 | extern void tuntap_get_address(struct tuntap_dev *tuntap); 221 | 222 | extern SOCKET open_socket(int local_port, int bind_any); 223 | 224 | extern char* intoa(uint32_t addr, char* buf, uint16_t buf_len); 225 | extern char* macaddr_str(macstr_t buf, const n2n_mac_t mac); 226 | extern int str2mac( uint8_t * outmac /* 6 bytes */, const char * s ); 227 | extern char * sock_to_cstr( n2n_sock_str_t out, 228 | const n2n_sock_t * sock ); 229 | 230 | extern int sock_equal( const n2n_sock_t * a, 231 | const n2n_sock_t * b ); 232 | 233 | extern uint8_t is_multi_broadcast(const uint8_t * dest_mac); 234 | extern char* msg_type2str(uint16_t msg_type); 235 | extern void hexdump(const uint8_t * buf, size_t len); 236 | 237 | void print_n2n_version(); 238 | 239 | 240 | /* Operations on peer_info lists. */ 241 | struct peer_info * find_peer_by_mac( struct peer_info * list, 242 | const n2n_mac_t mac ); 243 | void peer_list_add( struct peer_info * * list, 244 | struct peer_info * new ); 245 | size_t peer_list_size( const struct peer_info * list ); 246 | size_t purge_peer_list( struct peer_info ** peer_list, 247 | time_t purge_before ); 248 | size_t clear_peer_list( struct peer_info ** peer_list ); 249 | size_t purge_expired_registrations( struct peer_info ** peer_list ); 250 | 251 | /* version.c */ 252 | extern char *n2n_sw_version, *n2n_sw_osName, *n2n_sw_buildDate; 253 | 254 | #endif /* _N2N_H_ */ 255 | -------------------------------------------------------------------------------- /n2n_v2/n2n.spec: -------------------------------------------------------------------------------- 1 | Summary: N2N peer-to-peer virtual private network system. 2 | Name: n2n 3 | Version: 2.1.0 4 | Release: 1 5 | License: GPLv3 6 | Vendor: ntop.org 7 | Group: None 8 | URL: http://www.ntop.org/n2n 9 | Source0: %{name}-%{version}.tar.gz 10 | BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root 11 | 12 | %description 13 | N2N is a peer-to-peer virtual private network system. N2N uses the universal 14 | TUNTAP interface to create TAP network interfaces to an encrypted virtual 15 | LAN. Members of a community share encryption keys which allow exchange of 16 | data. The supernode is used for peer discovery and initial packet relay before 17 | direct peer-to-peer exchange is established. Once direct packet exchange is 18 | established, the supernode is not required. 19 | 20 | N2N-2 introduces additional security features and multiple supernodes. 21 | 22 | %prep 23 | 24 | %setup -q 25 | 26 | echo -e "\n *** Building ${RPM_PACKAGE_NAME}-${RPM_PACKAGE_VERSION}-${RPM_PACKAGE_RELEASE} ***\n" 27 | 28 | %build 29 | make 30 | 31 | %install 32 | make PREFIX=${RPM_BUILD_ROOT}/usr install 33 | 34 | %clean 35 | rm -rf $RPM_BUILD_ROOT 36 | 37 | 38 | %files 39 | %defattr(-,root,root,-) 40 | /usr/sbin/supernode 41 | /usr/sbin/edge 42 | %doc /usr/share/man/man1/supernode.1.gz 43 | %doc /usr/share/man/man8/edge.8.gz 44 | %doc /usr/share/man/man7/n2n_v2.7.gz 45 | 46 | 47 | %changelog 48 | * Fri Oct 30 2009 Richard Andrews - 49 | - First beta for n2n-2 50 | * Sat May 3 2008 Richard Andrews - 51 | - Initial build. 52 | 53 | -------------------------------------------------------------------------------- /n2n_v2/n2n_keyfile.c: -------------------------------------------------------------------------------- 1 | /* (c) 2009 Richard Andrews */ 2 | /* Contributions from: 3 | * - Jozef Kralik 4 | */ 5 | 6 | #include "n2n.h" 7 | #include "n2n_keyfile.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | #ifdef WIN32 15 | char *strsep( char **ppsz_string, const char *psz_delimiters ) 16 | { 17 | char *p; 18 | char *psz_string = *ppsz_string; 19 | if( !psz_string ) 20 | return NULL; 21 | 22 | p = strpbrk( psz_string, psz_delimiters ); 23 | if( !p ) 24 | { 25 | *ppsz_string = NULL; 26 | return psz_string; 27 | } 28 | *p++ = '\0'; 29 | 30 | *ppsz_string = p; 31 | return psz_string; 32 | } 33 | #endif 34 | 35 | 36 | /* Parse hex nibbles in ascii until a non-nibble character is found. Nibble 37 | * characters are 0-9, a-f and A-F. 38 | * 39 | * Return number of bytes parsed into keyBuf or a negative error code. 40 | */ 41 | ssize_t n2n_parse_hex( uint8_t * keyBuf, 42 | size_t keyLen, 43 | const char * textKey, 44 | size_t textLen) 45 | { 46 | ssize_t retval=0; 47 | uint8_t * pout=keyBuf; 48 | size_t octet=0; 49 | const char * textEnd; 50 | const char * pbeg; 51 | 52 | textEnd = textKey+textLen; 53 | pbeg=textKey; 54 | 55 | while ( ( pbeg + 1 < textEnd ) && ( retval < (ssize_t)keyLen ) ) 56 | { 57 | if ( 1 != sscanf( pbeg, "%02x", (unsigned int*)&octet ) ) 58 | { 59 | retval=-1; 60 | break; 61 | } 62 | 63 | *pout = (octet & 0xff); 64 | ++pout; 65 | ++retval; 66 | pbeg += 2; 67 | } 68 | 69 | return retval; 70 | } 71 | 72 | 73 | static int parseKeyLine( n2n_cipherspec_t * spec, 74 | const char * linein ) 75 | { 76 | /* parameters are separated by whitespace */ 77 | char line[N2N_KEYFILE_LINESIZE]; 78 | char * lp=line; 79 | const char * token; 80 | strncpy( line, linein, N2N_KEYFILE_LINESIZE ); 81 | 82 | memset( spec, 0, sizeof( n2n_cipherspec_t ) ); 83 | 84 | /* decode valid_from time */ 85 | token = strsep( &lp, DELIMITERS ); 86 | if ( !token ) { goto error; } 87 | spec->valid_from = atol(token); 88 | 89 | /* decode valid_until time */ 90 | token = strsep( &lp, DELIMITERS ); 91 | if ( !token ) { goto error; } 92 | spec->valid_until = atol(token); 93 | 94 | /* decode the transform number */ 95 | token = strsep( &lp, DELIMITERS ); 96 | if ( !token ) { goto error; } 97 | spec->t = atoi(token); 98 | 99 | /* The reset if opaque key data */ 100 | token = strsep( &lp, DELIMITERS ); 101 | if ( !token ) { goto error; } 102 | strncpy( (char *)spec->opaque, token, N2N_MAX_KEYSIZE ); 103 | spec->opaque_size=strlen( (char *)spec->opaque); 104 | 105 | return 0; 106 | 107 | error: 108 | return -1; 109 | } 110 | 111 | 112 | #define SEP "/" 113 | 114 | 115 | int validCipherSpec( const n2n_cipherspec_t * k, 116 | time_t now ) 117 | { 118 | if ( k->valid_until < k->valid_from ) { goto bad; } 119 | if ( k->valid_from > now ) { goto bad; } 120 | if ( k->valid_until < now ) { goto bad; } 121 | 122 | return 0; 123 | 124 | bad: 125 | return -1; 126 | } 127 | 128 | /* Read key control file and return the number of specs stored or a negative 129 | * error code. 130 | * 131 | * As the specs are read in the from and until time values are compared to 132 | * present time. Only those keys which are valid are stored. 133 | */ 134 | int n2n_read_keyfile( n2n_cipherspec_t * specs, /* fill out this array of cipherspecs */ 135 | size_t numspecs, /* number of slots in the array. */ 136 | const char * ctrlfile_path ) /* path to control file */ 137 | { 138 | /* Each line contains one cipherspec. */ 139 | 140 | int retval=0; 141 | FILE * fp=NULL; 142 | size_t idx=0; 143 | time_t now = time(NULL); 144 | 145 | traceEvent( TRACE_DEBUG, "Reading '%s'\n", ctrlfile_path ); 146 | 147 | fp = fopen( ctrlfile_path, "r" ); 148 | if ( fp ) 149 | { 150 | /* Read the file a line a time with fgets. */ 151 | char line[N2N_KEYFILE_LINESIZE]; 152 | size_t lineNum=0; 153 | 154 | while ( idx < numspecs ) 155 | { 156 | n2n_cipherspec_t * k = &(specs[idx]); 157 | fgets( line, N2N_KEYFILE_LINESIZE, fp ); 158 | ++lineNum; 159 | 160 | if ( strlen(line) > 1 ) 161 | { 162 | if ( 0 == parseKeyLine( k, line ) ) 163 | { 164 | if ( k->valid_until > now ) 165 | { 166 | traceEvent( TRACE_INFO, " --> [%u] from %lu, until %lu, transform=%hu, data=%s\n", 167 | idx, k->valid_from, k->valid_until, k->t, k->opaque ); 168 | 169 | ++retval; 170 | ++idx; 171 | } 172 | else 173 | { 174 | traceEvent( TRACE_INFO, " --X [%u] from %lu, until %lu, transform=%hu, data=%s\n", 175 | idx, k->valid_from, k->valid_until, k->t, k->opaque ); 176 | 177 | } 178 | } 179 | else 180 | { 181 | traceEvent( TRACE_WARNING, "Failed to decode line %u\n", lineNum ); 182 | } 183 | } 184 | 185 | if ( feof(fp) ) 186 | { 187 | break; 188 | } 189 | 190 | line[0]=0; /* this line has been consumed */ 191 | } 192 | 193 | fclose( fp); 194 | fp=NULL; 195 | } 196 | else 197 | { 198 | traceEvent( TRACE_ERROR, "Failed to open '%s'\n", ctrlfile_path ); 199 | retval = -1; 200 | } 201 | 202 | return retval; 203 | } 204 | -------------------------------------------------------------------------------- /n2n_v2/n2n_keyfile.h: -------------------------------------------------------------------------------- 1 | /* (c) 2009 Richard Andrews */ 2 | 3 | /** Key files 4 | * 5 | * Edge implements a very simple interface for getting instructions about 6 | * rolling keys. 7 | * 8 | * Key definitions are written as individual files in /.key. The 9 | * format of each key is a single line of hex nibbles as follows: 10 | * 11 | * 0102030405060708090a0b0c0d0e0f 12 | * 13 | * Any external key exchange mechanism can receive the key data write it into 14 | * the keyfiles. 15 | * 16 | * To control which keys are active at what times the key control file is 17 | * used. This is a single file which is periodically reread. It contains key 18 | * definitions in chronological order with one line per key definition as 19 | * follows: 20 | * 21 | * 22 | * 23 | * edge reads the key control file periodically to get updates in policy. edge 24 | * holds a number of keys in memory. Data can be decoded if it was encoded by 25 | * any of the keys still in memory. By having at least 2 keys in memory it 26 | * allows for clock skew and transmission delay when encoder and decoder roll 27 | * keys at slightly different times. The amount of overlap in the valid time 28 | * ranges provides the tolerance to timing skews in the system. 29 | * 30 | * The keys have the same level of secrecy as any other user file. Existing 31 | * UNIX permission systems can be used to provide access controls. 32 | * 33 | */ 34 | 35 | /** How Edge Uses The Key Schedule 36 | * 37 | * Edge provides state space for a number of transform algorithms. Each 38 | * transform uses its state space to store the SA information for its keys as 39 | * found in the key file. When a packet is received the transform ID is in 40 | * plain text. The packets is then sent to that transform for decoding. Each 41 | * transform can store its SA numbers differently (or not at all). The 42 | * transform code then finds the SA number, then finds the cipher (with key) in 43 | * the state space and uses this to decode the packet. 44 | * 45 | * To support this, as edge reads each key line, it passes it to the 46 | * appropriate transform to parse the line and store the SA information in its 47 | * state space. 48 | * 49 | * When encoding a packet, edge has several transforms and potentially valid 50 | * SAs to choose from. To keep track of which one to use for encoding edge does 51 | * its own book-keeping as each key line is passed to the transform code: it 52 | * stores a lookup of valid_from -> transform. When encoding a packet it then 53 | * just calls the transform with the best valid_from in the table. The 54 | * transform's own state space has all the SAs for its keys and the best of 55 | * those is chosen. 56 | */ 57 | 58 | #if !defined( N2N_KEYFILE_H_ ) 59 | #define N2N_KEYFILE_H_ 60 | 61 | 62 | #include "n2n_wire.h" 63 | #include 64 | 65 | #define N2N_MAX_KEYSIZE 256 /* bytes */ 66 | #define N2N_MAX_NUM_CIPHERSPECS 8 67 | #define N2N_KEYPATH_SIZE 256 68 | #define N2N_KEYFILE_LINESIZE 256 69 | 70 | /** This structure stores an encryption cipher spec. */ 71 | struct n2n_cipherspec 72 | { 73 | n2n_transform_t t; /* N2N_TRANSFORM_ID_xxx for this spec. */ 74 | time_t valid_from; /* Start using the key at this time. */ 75 | time_t valid_until; /* Key is valid if time < valid_until. */ 76 | uint16_t opaque_size; /* Size in bytes of key. */ 77 | uint8_t opaque[N2N_MAX_KEYSIZE];/* Key matter. */ 78 | }; 79 | 80 | typedef struct n2n_cipherspec n2n_cipherspec_t; 81 | 82 | 83 | static const char * const DELIMITERS=" \t\n\r"; 84 | 85 | 86 | /** @return number of cipherspec items filled. */ 87 | int n2n_read_keyfile( n2n_cipherspec_t * specs, /* fill out this array of cipherspecs */ 88 | size_t numspecs, /* number of slots in the array. */ 89 | const char * ctrlfile_path ); /* path to control file */ 90 | 91 | int validCipherSpec( const n2n_cipherspec_t * k, 92 | time_t now ); 93 | 94 | ssize_t n2n_parse_hex( uint8_t * keyBuf, 95 | size_t keyMax, 96 | const char * textKey, 97 | size_t textLen ); 98 | 99 | /*----------------------------------------------------------------------------*/ 100 | 101 | #endif /* #if !defined( N2N_KEYFILE_H_ ) */ 102 | -------------------------------------------------------------------------------- /n2n_v2/n2n_transforms.h: -------------------------------------------------------------------------------- 1 | /* (c) 2009 Richard Andrews */ 2 | 3 | #if !defined(N2N_TRANSFORMS_H_) 4 | #define N2N_TRANSFORMS_H_ 5 | 6 | #include "n2n_keyfile.h" 7 | #include "n2n_wire.h" 8 | 9 | 10 | #define N2N_TRANSFORM_ID_INVAL 0 /* marks uninitialised data */ 11 | #define N2N_TRANSFORM_ID_NULL 1 12 | #define N2N_TRANSFORM_ID_TWOFISH 2 13 | #define N2N_TRANSFORM_ID_AESCBC 3 14 | #define N2N_TRANSFORM_ID_LZO 4 15 | #define N2N_TRANSFORM_ID_TWOFISH_LZO 5 16 | #define N2N_TRANSFORM_ID_AESCBC_LZO 6 17 | #define N2N_TRANSFORM_ID_USER_START 64 18 | #define N2N_TRANSFORM_ID_MAX 65535 19 | 20 | 21 | struct n2n_trans_op; 22 | typedef struct n2n_trans_op n2n_trans_op_t; 23 | 24 | struct n2n_tostat 25 | { 26 | uint8_t can_tx; /* Does this transop have a valid SA for encoding. */ 27 | n2n_cipherspec_t tx_spec; /* If can_tx, the spec used to encode. */ 28 | }; 29 | 30 | typedef struct n2n_tostat n2n_tostat_t; 31 | 32 | 33 | typedef int (*n2n_transdeinit_f)( n2n_trans_op_t * arg ); 34 | typedef int (*n2n_transaddspec_f)( n2n_trans_op_t * arg, 35 | const n2n_cipherspec_t * cspec ); 36 | typedef n2n_tostat_t (*n2n_transtick_f)( n2n_trans_op_t * arg, 37 | time_t now ); 38 | 39 | typedef int (*n2n_transform_f)( n2n_trans_op_t * arg, 40 | uint8_t * outbuf, 41 | size_t out_len, 42 | const uint8_t * inbuf, 43 | size_t in_len ); 44 | 45 | /** Holds the info associated with a data transform plugin. 46 | * 47 | * When a packet arrives the transform ID is extracted. This defines the code 48 | * to use to decode the packet content. The transform code then decodes the 49 | * packet and consults its internal key lookup. 50 | */ 51 | struct n2n_trans_op 52 | { 53 | void * priv; /* opaque data. Key schedule goes here. */ 54 | 55 | n2n_transform_t transform_id; /* link header enum to a transform */ 56 | size_t tx_cnt; 57 | size_t rx_cnt; 58 | 59 | n2n_transdeinit_f deinit; /* destructor function */ 60 | n2n_transaddspec_f addspec; /* parse opaque data from a key schedule file. */ 61 | n2n_transtick_f tick; /* periodic maintenance */ 62 | n2n_transform_f fwd; /* encode a payload */ 63 | n2n_transform_f rev; /* decode a payload */ 64 | }; 65 | 66 | /* Setup a single twofish SA for single-key operation. */ 67 | int transop_twofish_setup( n2n_trans_op_t * ttt, 68 | n2n_sa_t sa_num, 69 | uint8_t * encrypt_pwd, 70 | uint32_t encrypt_pwd_len ); 71 | 72 | /* Initialise an empty transop ready to receive cipherspec elements. */ 73 | int transop_twofish_init( n2n_trans_op_t * ttt ); 74 | int transop_aes_init( n2n_trans_op_t * ttt ); 75 | void transop_null_init( n2n_trans_op_t * ttt ); 76 | 77 | #endif /* #if !defined(N2N_TRANSFORMS_H_) */ 78 | 79 | -------------------------------------------------------------------------------- /n2n_v2/n2n_v2.7: -------------------------------------------------------------------------------- 1 | .TH "n2n_v2" 7 "Sep 21, 2009" "revision 3909" "Background" 2 | .SH NAME 3 | N2n Version 2 \- version 2 of the n2n decentralised peer-to-peer network overlay 4 | VPN. 5 | .SH DESCRIPTION 6 | N2n is a peer-to-peer network overlay or VPN system that provides layer 2 over 7 | layer 3 encapsulation with data transform capabilities such as encryption and 8 | compression. This guide discusses the differences of version 2 or n2n from 9 | version 1. 10 | .SH PROTOCOLS 11 | N2n-2 uses a different set of messages to communicate with edges and 12 | supernodes. The n2n-2 messages are not compatible with n2n-1. There is no 13 | backward compatibility for n2n-1. 14 | .SH ENCRYPTION 15 | N2n-2 offers a new way of handling encryption compared to n2n-1. N2n-1 provided 16 | facility for a single community password with no expiration. In n2n-2 this 17 | method is preserved but a new mechanism has been added using a key schedule 18 | file. 19 | .TP 20 | Key Schedule 21 | A key schedule file lists a number of keys with the period for which each is 22 | valid along with the encryption type identifier and the actual key value. This 23 | allows the user to define up to 32 keys in advance with a pre-set time at which 24 | they keys will change. The key schedule file can be reloaded while the edge is 25 | running to allow new keys to be loaded and unused keys expunged. 26 | .TP 27 | Timing Requirements When a key rolls over to the next in the schedule, the new 28 | key is used for all transmitted packets; however any packets received using an 29 | older key can still be decoded as the keys from the key schedule are still 30 | known. As a result edges do not need to have accurate time synchronisation. The 31 | accuracy of required synchronisation depends to a large degree on the key 32 | schedule. Rapid key roll-overs requires more accurate time synchronisation. 33 | .P 34 | N2n-2 provides the following encryption ciphers; more can be added as required: 35 | .TP 36 | .B (1) NULL 37 | Data is encapsulated unchanged. Useful for testing and high-performance, low 38 | sensitivity applications. 39 | .TP 40 | .B (2) TF 41 | Twofish AES candidate. 42 | .P 43 | The following additional ciphers are specified but not yet implemented: 44 | .TP 45 | .B (3) AES-CBC 46 | AES in CBC mode with 256-bit key. 47 | .TP 48 | .B (4) LZO 49 | LZO compression of data (no encryption). 50 | .TP 51 | .B (5) TF-LZO 52 | TF cipher with LZO compression of data prior to encryption. 53 | .TP 54 | .B (6) AES-CBC-LZO 55 | AES-CBC ciper with LZO compression of data prior to encryption. 56 | 57 | .SH EXTENSIBILITY 58 | N2n-2 decouples the data transform system from the core of the edge 59 | operation. This allows for easier addition of new data transform 60 | operations. N2n-2 reserves 64 standard transform identifiers (such as TwoFish 61 | encryption) but allocates transform identifiers 64 - 65536 for user-defined 62 | transforms. This allows anyone to add to n2n new private transforms without 63 | breaking compatibility with the standard offering. 64 | 65 | .SH MULTIPLE SUPERNODES 66 | N2n-2 introduces the capability of multiple supernodes to be used by an 67 | edge. N2n-2 offers supernode in several flavours: 68 | .TP 69 | Stand-alone supernode 70 | 71 | This is the same concept as from n2n-1. Supernode is a small efficient C program 72 | which operates in isolation. 73 | .TP 74 | Federated supernodes 75 | 76 | This is a cluster of supernodes which share information. Edges registered to any 77 | of the cooperating supernodes can relay packets through the supernode federation 78 | and switch supernodes if required. Supernodes can send PACKET or REGISTER 79 | messages to other supernodes to try and find the destination edge. 80 | 81 | .P 82 | The n2n-2 edge implementation allows multiple supernodes to be specified on the 83 | command line. Edges monitor the current supernode for responses to 84 | REGISTER_SUPER messages. If 3 responses are missed then the edge starts looking 85 | for a new supernode. It cycles through the list of supernodes specified until it 86 | finds a working one. 87 | 88 | .SH EFFICIENCY 89 | The n2n-2 message formats have been made more efficient. The amount of data 90 | overhead has been reduced by ensuring the messages contain only the data fields 91 | required. Some optional fields do not consume data if they are not present. 92 | 93 | .SH DAEMON OPERATION 94 | The supernode and edge use daemon mode of operation by default. This sense is 95 | inverted from n2n-1 where they ran in the foreground by default. They can be 96 | made to run in the foreground so tools such a DJB's daemontools can work with 97 | them. See the 98 | .B -f 99 | option 100 | 101 | .SH MANAGEMENT CONSOLE 102 | Edge and supernode in n2n-2 provide a UDP-based management console. Both listen 103 | on the localhost address 127.0.0.1. Commands can be sent to the programs by 104 | sending to the UDP socket. Responses are returned to the socket from which 105 | commands were issued. This only works from the computer on which the programs 106 | are running. Statistics can be retrieved and commands issued. The netcat utility 107 | is all that is required; but more sophisticated tools could be built on the 108 | interface. 109 | 110 | .SH SUPERNODE AUTHENTICATION 111 | .B (To be implemented) 112 | Space has been reserved in the supernode registration messages for an 113 | authentication mechanism. 114 | 115 | .SH MESSAGE SUMMARY 116 | The following message types work within n2n-2. 117 | .TP 118 | REGISTER_SUPER 119 | Sent from an edge to its local supernode to register its MAC with the community. 120 | .TP 121 | REGISTER_SUPER_ACK 122 | Sent from a supernode to an edge to confirm registration. This also carries the 123 | definition of the edge socket as seen at the supernode so NAT can be detected 124 | and described. 125 | .TP 126 | REGISTER_SUPER_NAK 127 | Supernode refusing to register an edge. 128 | .TP 129 | PACKET 130 | Encapsulated ethernet packets sent between edges. Supernodes forward or 131 | broadcast these and edges send them direct in peer-to-peer mode. 132 | .TP 133 | REGISTER 134 | A peer-to-peer mode registration request from one edge to another. Supernodes 135 | forward these to facilitate NAT crossing introductions. 136 | .TP 137 | REGISTER_ACK 138 | Complete peer-to-peer mode setup between two edges. These messages need to 139 | travel direct between edges. 140 | .TP 141 | FEDERATION 142 | Federated supernodes exchanging community information. 143 | 144 | .SH OTHER DIFFERENCES 145 | .TP 146 | HTTP Tunneling 147 | This experimental feature (-t option in n2n_v1) of n2n_v1 has been removed 148 | entirely from n2n_v2. 149 | .SH AUTHORS 150 | .TP 151 | Richard Andrews andrews (at) ntop.org - main author of n2n-2 152 | .TP 153 | Luca Deri 154 | deri (at) ntop.org - code inherited from n2n-1 155 | .SH SEE ALSO 156 | ifconfig(8) edge(8) supernode(1) 157 | -------------------------------------------------------------------------------- /n2n_v2/openwrt/kamikaze/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2008 OpenWrt.org 3 | # 4 | # This is free software, licensed under the GNU General Public License v2. 5 | 6 | 7 | include $(TOPDIR)/rules.mk 8 | 9 | PKG_BRANCH:=trunk 10 | PKG_SOURCE_URL:=https://svn.ntop.org/svn/ntop/trunk/n2n 11 | PKG_REV:=$(shell LC_ALL=C svn info ${PKG_SOURCE_URL} | sed -ne's/^Last Changed Rev: //p') 12 | 13 | PKG_NAME:=n2n 14 | PKG_VERSION:=svn$(PKG_REV) 15 | PKG_RELEASE:=1 16 | 17 | PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION) 18 | PKG_SOURCE:=$(PKG_SOURCE_SUBDIR).tar.gz 19 | PKG_SOURCE_PROTO:=svn 20 | PKG_SOURCE_VERSION:=$(PKG_REV) 21 | 22 | PKG_BUILD_DEPENDS:= 23 | 24 | PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) 25 | PKG_INSTALL_DIR:=$(PKG_BUILD_DIR) 26 | 27 | 28 | 29 | include $(INCLUDE_DIR)/package.mk 30 | 31 | define Package/n2n 32 | SECTION:=net 33 | CATEGORY:=Network 34 | TITLE:=VPN tunneling daemon 35 | URL:=http://www.ntop.org/n2n/ 36 | SUBMENU:=VPN 37 | DEPENDS:=libpthread 38 | endef 39 | 40 | 41 | define Build/Configure 42 | endef 43 | 44 | define Build/Compile 45 | $(MAKE) CC="$(TARGET_CC)" -C $(PKG_BUILD_DIR) 46 | endef 47 | 48 | 49 | define Package/n2n/install 50 | $(INSTALL_DIR) $(1)/usr/sbin 51 | $(INSTALL_BIN) $(PKG_INSTALL_DIR)/edge $(1)/usr/sbin/ 52 | $(INSTALL_BIN) $(PKG_INSTALL_DIR)/supernode $(1)/usr/sbin/ 53 | endef 54 | 55 | $(eval $(call BuildPackage,n2n)) 56 | -------------------------------------------------------------------------------- /n2n_v2/scripts/mk_SRPM.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # This script makes a SRPM - a source RPM file which can be built into the 4 | # appropriate distro specific RPM for any platform. 5 | # 6 | # To build the binary package: 7 | # rpm -i n2n-.src.rpm 8 | # rpmbuild -bb n2n.spec 9 | # 10 | # Look for the "Wrote:" line to see where the final RPM is. 11 | # 12 | # To run this script cd to the n2n directory and run it as follows 13 | # scripts/mk_SRPMS.sh 14 | # 15 | 16 | set -e 17 | 18 | set -x 19 | 20 | BASE=`pwd` 21 | 22 | TARFILE=`${BASE}/scripts/mk_tar.sh` 23 | 24 | test -f ${TARFILE} 25 | 26 | echo "Building SRPM" 27 | # -ts means build source RPM from tarfile 28 | rpmbuild -ts ${TARFILE} 29 | 30 | echo "Done" 31 | -------------------------------------------------------------------------------- /n2n_v2/scripts/mk_deb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script makes a SRPM - a source RPM file which can be built into the 4 | # appropriate distro specific RPM for any platform. 5 | # 6 | # To build the binary package: 7 | # rpm -i n2n-.src.rpm 8 | # rpmbuild -bb n2n.spec 9 | # 10 | # Look for the "Wrote:" line to see where the final RPM is. 11 | # 12 | # To run this script cd to the n2n directory and run it as follows 13 | # scripts/mk_SRPMS.sh 14 | # 15 | 16 | set -e 17 | 18 | set -x 19 | 20 | BASE=`pwd` 21 | 22 | TARFILE=`${BASE}/scripts/mk_tar.sh` 23 | TEMPDIR="build_deb" 24 | 25 | test -f ${TARFILE} 26 | 27 | echo "Building .deb" 28 | 29 | if [ -d ${TEMPDIR} ]; then 30 | echo "Removing ${TEMPDIR} directory" 31 | rm -rf ${TEMPDIR} >&2 32 | fi 33 | 34 | mkdir ${TEMPDIR} 35 | 36 | pushd ${TEMPDIR} 37 | 38 | tar xzf ${TARFILE} #At original location 39 | 40 | cd n2n* 41 | 42 | dpkg-buildpackage -rfakeroot 43 | 44 | popd 45 | 46 | echo "Done" 47 | -------------------------------------------------------------------------------- /n2n_v2/scripts/mk_tar.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script makes a SRPM - a source RPM file which can be built into the 4 | # appropriate distro specific RPM for any platform. 5 | # 6 | # To build the binary package: 7 | # rpm -i n2n-.src.rpm 8 | # rpmbuild -bb n2n.spec 9 | # 10 | # Look for the "Wrote:" line to see where the final RPM is. 11 | # 12 | # To run this script cd to the n2n directory and run it as follows 13 | # scripts/mk_SRPMS.sh 14 | # 15 | 16 | set -e 17 | 18 | function exit_fail() 19 | { 20 | echo "$1" 21 | exit 1 22 | } 23 | 24 | PACKAGE="n2n" 25 | PKG_VERSION="2.1.0" 26 | PKG_AND_VERSION="${PACKAGE}-${PKG_VERSION}" 27 | 28 | TEMPDIR="tmp" 29 | 30 | SOURCE_MANIFEST=" 31 | README 32 | edge.c 33 | lzoconf.h 34 | lzodefs.h 35 | Makefile 36 | minilzo.c 37 | minilzo.h 38 | n2n.c 39 | n2n.h 40 | n2n_keyfile.c 41 | n2n_keyfile.h 42 | n2n.spec 43 | n2n_transforms.h 44 | n2n_wire.h 45 | sn.c 46 | transform_aes.c 47 | transform_null.c 48 | transform_tf.c 49 | tuntap_linux.c 50 | tuntap_freebsd.c 51 | tuntap_netbsd.c 52 | tuntap_osx.c 53 | twofish.c 54 | twofish.h 55 | version.c 56 | wire.c 57 | edge.8 58 | supernode.1 59 | n2n_v2.7 60 | debian/changelog 61 | debian/compat 62 | debian/control 63 | debian/copyright 64 | debian/n2n-edge.docs 65 | debian/n2n-edge.install 66 | debian/n2n-supernode.install 67 | debian/n2n-edge.manpages 68 | debian/n2n-supernode.manpages 69 | debian/README.Debian 70 | debian/rules 71 | " 72 | 73 | BASE=`pwd` 74 | 75 | for F in ${SOURCE_MANIFEST}; do 76 | test -e $F || exit_fail "Cannot find $F. Maybe you're in the wrong directory. Please execute from n2n directory."; >&2 77 | done 78 | 79 | echo "Found critical files. Proceeding." >&2 80 | 81 | if [ -d ${TEMPDIR} ]; then 82 | echo "Removing ${TEMPDIR} directory" 83 | rm -rf ${TEMPDIR} >&2 84 | fi 85 | 86 | mkdir ${TEMPDIR} >&2 87 | 88 | pushd ${TEMPDIR} >&2 89 | 90 | echo "Creating staging directory ${PWD}/${PKG_AND_VERSION}" >&2 91 | 92 | if [ -d ${PKG_AND_VERSION} ] ; then 93 | echo "Removing ${PKG_AND_VERSION} directory" 94 | rm -rf ${PKG_AND_VERSION} >&2 95 | fi 96 | 97 | mkdir ${PKG_AND_VERSION} 98 | 99 | pushd ${BASE} >&2 100 | 101 | echo "Copying in files" >&2 102 | for F in ${SOURCE_MANIFEST}; do 103 | cp --parents -a $F ${TEMPDIR}/${PKG_AND_VERSION}/ 104 | done 105 | 106 | popd >&2 107 | 108 | TARFILE="${PKG_AND_VERSION}.tar.gz" 109 | echo "Creating ${TARFILE}" >&2 110 | tar czf ${BASE}/${TARFILE} ${PKG_AND_VERSION} 111 | 112 | popd >&2 113 | 114 | rm -rf ${TEMPDIR} >&2 115 | 116 | echo ${BASE}/${TARFILE} 117 | -------------------------------------------------------------------------------- /n2n_v2/supernode.1: -------------------------------------------------------------------------------- 1 | .TH supernode 1 "Jan 3, 2009" "revision 3679" "USER COMMANDS" 2 | .SH NAME 3 | supernode \- n2n supernode daemon 4 | .SH SYNOPSIS 5 | .B supernode \-l [\-v] 6 | .SH DESCRIPTION 7 | N2N is a peer-to-peer VPN system. Supernode is a node introduction registry, 8 | broadcast conduit and packet relay node for the n2n system. On startup supernode 9 | begins listening on the specified UDP port for node registrations, and other 10 | packets to route. The supernode can service any number of communities and routes 11 | packets only between members of the same community. The supernode does not hold 12 | the community encryption key and so cannot snoop or inject packets into the 13 | community. 14 | .PP 15 | Supernode can service a number of n2n communities concurrently. Traffic does not 16 | cross between communities. 17 | .PP 18 | All logging goes to stdout. 19 | .SH OPTIONS 20 | .TP 21 | \-l 22 | listen on the given UDP port 23 | .TP 24 | \-v 25 | use verbose logging 26 | .TP 27 | \-f 28 | disable daemon mode (UNIX) and run in foreground. 29 | .SH EXAMPLES 30 | .TP 31 | .B supernode -l 7654 -v 32 | Start supernode listening on UDP port 7654 with verbose output. 33 | .PP 34 | .SH RESTART 35 | When suprenode restarts it loses all registration information from associated 36 | edge nodes. It can take up to five minutes for the edge nodes to re-register and 37 | normal traffic flow to resume. 38 | .SH EXIT STATUS 39 | supernode is a daemon and any exit is an error 40 | .SH AUTHOR 41 | Luca Deri ( deri (at) ntop.org ), Richard Andrews ( andrews (at) ntop.org ), Don Bindner 42 | .SH SEE ALSO 43 | ifconfig(8) edge(8) 44 | -------------------------------------------------------------------------------- /n2n_v2/test.c: -------------------------------------------------------------------------------- 1 | #include "n2n.h" 2 | #include "n2n_keyfile.h" 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int arc, const char * argv[] ) 8 | { 9 | int e; 10 | n2n_cipherspec_t specs[N2N_MAX_NUM_CIPHERSPECS]; 11 | 12 | e = n2n_read_keyfile( specs, N2N_MAX_NUM_CIPHERSPECS, "keyctrl.conf" ); 13 | 14 | if ( e < 0 ) 15 | { 16 | perror( "Failed to read keyfile" ); 17 | } 18 | else 19 | { 20 | fprintf( stderr, "Stored %d keys.\n", e ); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /n2n_v2/transform_null.c: -------------------------------------------------------------------------------- 1 | /* (c) 2009 Richard Andrews */ 2 | 3 | #include "n2n.h" 4 | #include "n2n_transforms.h" 5 | 6 | static int transop_deinit_null( n2n_trans_op_t * arg ) 7 | { 8 | /* nothing to deallocate, nothing to release. */ 9 | return 0; 10 | } 11 | 12 | static int transop_encode_null( n2n_trans_op_t * arg, 13 | uint8_t * outbuf, 14 | size_t out_len, 15 | const uint8_t * inbuf, 16 | size_t in_len ) 17 | { 18 | int retval = -1; 19 | 20 | traceEvent( TRACE_DEBUG, "encode_null %lu", in_len ); 21 | if ( out_len >= in_len ) 22 | { 23 | memcpy( outbuf, inbuf, in_len ); 24 | retval = in_len; 25 | } 26 | else 27 | { 28 | traceEvent( TRACE_DEBUG, "encode_null %lu too big for packet buffer", in_len ); 29 | } 30 | 31 | return retval; 32 | } 33 | 34 | static int transop_decode_null( n2n_trans_op_t * arg, 35 | uint8_t * outbuf, 36 | size_t out_len, 37 | const uint8_t * inbuf, 38 | size_t in_len ) 39 | { 40 | int retval = -1; 41 | 42 | traceEvent( TRACE_DEBUG, "decode_null %lu", in_len ); 43 | if ( out_len >= in_len ) 44 | { 45 | memcpy( outbuf, inbuf, in_len ); 46 | retval = in_len; 47 | } 48 | else 49 | { 50 | traceEvent( TRACE_DEBUG, "decode_null %lu too big for packet buffer", in_len ); 51 | } 52 | 53 | return retval; 54 | } 55 | 56 | static int transop_addspec_null( n2n_trans_op_t * arg, const n2n_cipherspec_t * cspec ) 57 | { 58 | return 0; 59 | } 60 | 61 | static n2n_tostat_t transop_tick_null( n2n_trans_op_t * arg, time_t now ) 62 | { 63 | n2n_tostat_t r; 64 | 65 | r.can_tx=1; 66 | r.tx_spec.t = N2N_TRANSFORM_ID_NULL; 67 | r.tx_spec.valid_from = 0; 68 | r.tx_spec.valid_until = (time_t)(-1); 69 | r.tx_spec.opaque_size=0; 70 | 71 | return r; 72 | } 73 | 74 | void transop_null_init( n2n_trans_op_t * ttt ) 75 | { 76 | memset(ttt, 0, sizeof(n2n_trans_op_t) ); 77 | 78 | ttt->transform_id = N2N_TRANSFORM_ID_NULL; 79 | ttt->deinit = transop_deinit_null; 80 | ttt->addspec = transop_addspec_null; 81 | ttt->tick = transop_tick_null; 82 | ttt->fwd = transop_encode_null; 83 | ttt->rev = transop_decode_null; 84 | } 85 | -------------------------------------------------------------------------------- /n2n_v2/tuntap_freebsd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2007-09 - Luca Deri 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not see see 16 | */ 17 | 18 | #include "n2n.h" 19 | 20 | #ifdef __FreeBSD__ 21 | 22 | void tun_close(tuntap_dev *device); 23 | 24 | /* ********************************** */ 25 | 26 | #define N2N_FREEBSD_TAPDEVICE_SIZE 32 27 | int tuntap_open(tuntap_dev *device /* ignored */, 28 | char *dev, 29 | const char *address_mode, /* static or dhcp */ 30 | char *device_ip, 31 | char *device_mask, 32 | const char * device_mac, 33 | int mtu) { 34 | int i; 35 | char tap_device[N2N_FREEBSD_TAPDEVICE_SIZE]; 36 | 37 | for (i = 0; i < 255; i++) { 38 | snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i); 39 | 40 | device->fd = open(tap_device, O_RDWR); 41 | if(device->fd > 0) { 42 | traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); 43 | break; 44 | } 45 | } 46 | 47 | if(device->fd < 0) { 48 | traceEvent(TRACE_ERROR, "Unable to open tap device"); 49 | return(-1); 50 | } else { 51 | char buf[256]; 52 | FILE *fd; 53 | 54 | device->ip_addr = inet_addr(device_ip); 55 | 56 | if ( device_mac && device_mac[0] != '\0' ) 57 | { 58 | /* FIXME - This is not tested. Might be wrong syntax for OS X */ 59 | 60 | /* Set the hw address before bringing the if up. */ 61 | snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s", 62 | i, device_mac); 63 | system(buf); 64 | } 65 | 66 | snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", 67 | i, device_ip, device_mask, mtu); 68 | system(buf); 69 | 70 | traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", 71 | i, device_ip, device_mask); 72 | 73 | /* Read MAC address */ 74 | 75 | snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i); 76 | /* traceEvent(TRACE_INFO, "%s", buf); */ 77 | 78 | fd = popen(buf, "r"); 79 | if(fd < 0) { 80 | tun_close(device); 81 | return(-1); 82 | } else { 83 | int a, b, c, d, e, f; 84 | 85 | buf[0] = 0; 86 | fgets(buf, sizeof(buf), fd); 87 | pclose(fd); 88 | 89 | if(buf[0] == '\0') { 90 | traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address"); 91 | exit(0); 92 | } 93 | 94 | traceEvent(TRACE_NORMAL, "Interface tap%d mac %s", i, buf); 95 | if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { 96 | device->mac_addr[0] = a, device->mac_addr[1] = b; 97 | device->mac_addr[2] = c, device->mac_addr[3] = d; 98 | device->mac_addr[4] = e, device->mac_addr[5] = f; 99 | } 100 | } 101 | } 102 | 103 | 104 | /* read_mac(dev, device->mac_addr); */ 105 | return(device->fd); 106 | } 107 | 108 | /* ********************************** */ 109 | 110 | int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 111 | return(read(tuntap->fd, buf, len)); 112 | } 113 | 114 | /* ********************************** */ 115 | 116 | int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 117 | return(write(tuntap->fd, buf, len)); 118 | } 119 | 120 | /* ********************************** */ 121 | 122 | void tuntap_close(struct tuntap_dev *tuntap) { 123 | close(tuntap->fd); 124 | } 125 | 126 | /* Fill out the ip_addr value from the interface. Called to pick up dynamic 127 | * address changes. */ 128 | void tuntap_get_address(struct tuntap_dev *tuntap) 129 | { 130 | } 131 | 132 | #endif /* #ifdef __FreeBSD__ */ 133 | -------------------------------------------------------------------------------- /n2n_v2/tuntap_linux.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2007-09 - Luca Deri 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, see 16 | */ 17 | 18 | #include "n2n.h" 19 | 20 | #ifdef __linux__ 21 | 22 | static void read_mac(char *ifname, n2n_mac_t mac_addr) { 23 | int _sock, res; 24 | struct ifreq ifr; 25 | macstr_t mac_addr_buf; 26 | 27 | memset (&ifr,0,sizeof(struct ifreq)); 28 | 29 | /* Dummy socket, just to make ioctls with */ 30 | _sock=socket(PF_INET, SOCK_DGRAM, 0); 31 | strcpy(ifr.ifr_name, ifname); 32 | res = ioctl(_sock,SIOCGIFHWADDR,&ifr); 33 | if (res<0) { 34 | perror ("Get hw addr"); 35 | } else 36 | memcpy(mac_addr, ifr.ifr_ifru.ifru_hwaddr.sa_data, 6); 37 | 38 | traceEvent(TRACE_NORMAL, "Interface %s has MAC %s", 39 | ifname, 40 | macaddr_str(mac_addr_buf, mac_addr )); 41 | close(_sock); 42 | } 43 | 44 | /* ********************************** */ 45 | 46 | /** @brief Open and configure the TAP device for packet read/write. 47 | * 48 | * This routine creates the interface via the tuntap driver then uses ifconfig 49 | * to configure address/mask and MTU. 50 | * 51 | * @param device - [inout] a device info holder object 52 | * @param dev - user-defined name for the new iface, 53 | * if NULL system will assign a name 54 | * @param device_ip - address of iface 55 | * @param device_mask - netmask for device_ip 56 | * @param mtu - MTU for device_ip 57 | * 58 | * @return - negative value on error 59 | * - non-negative file-descriptor on success 60 | */ 61 | int tuntap_open(tuntap_dev *device, 62 | char *dev, /* user-definable interface name, eg. edge0 */ 63 | const char *address_mode, /* static or dhcp */ 64 | char *device_ip, 65 | char *device_mask, 66 | const char * device_mac, 67 | int mtu) { 68 | char *tuntap_device = "/dev/net/tun"; 69 | #define N2N_LINUX_SYSTEMCMD_SIZE 128 70 | char buf[N2N_LINUX_SYSTEMCMD_SIZE]; 71 | struct ifreq ifr; 72 | int rc; 73 | 74 | device->fd = open(tuntap_device, O_RDWR); 75 | if(device->fd < 0) { 76 | printf("ERROR: ioctl() [%s][%d]\n", strerror(errno), errno); 77 | return -1; 78 | } 79 | 80 | memset(&ifr, 0, sizeof(ifr)); 81 | ifr.ifr_flags = IFF_TAP|IFF_NO_PI; /* Want a TAP device for layer 2 frames. */ 82 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); 83 | rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr); 84 | 85 | if(rc < 0) { 86 | traceEvent(TRACE_ERROR, "ioctl() [%s][%d]\n", strerror(errno), rc); 87 | close(device->fd); 88 | return -1; 89 | } 90 | 91 | /* Store the device name for later reuse */ 92 | strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ) ); 93 | 94 | if ( device_mac && device_mac[0] != '\0' ) 95 | { 96 | /* Set the hw address before bringing the if up. */ 97 | snprintf(buf, sizeof(buf), "/sbin/ifconfig %s hw ether %s", 98 | ifr.ifr_name, device_mac ); 99 | system(buf); 100 | traceEvent(TRACE_INFO, "Setting MAC: %s", buf); 101 | } 102 | 103 | if ( 0 == strncmp( "dhcp", address_mode, 5 ) ) 104 | { 105 | snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s mtu %d up", 106 | ifr.ifr_name, device_ip, mtu); 107 | } 108 | else 109 | { 110 | snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s netmask %s mtu %d up", 111 | ifr.ifr_name, device_ip, device_mask, mtu); 112 | } 113 | 114 | system(buf); 115 | traceEvent(TRACE_INFO, "Bringing up: %s", buf); 116 | 117 | device->ip_addr = inet_addr(device_ip); 118 | device->device_mask = inet_addr(device_mask); 119 | read_mac(dev, device->mac_addr); 120 | return(device->fd); 121 | } 122 | 123 | int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 124 | return(read(tuntap->fd, buf, len)); 125 | } 126 | 127 | int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 128 | return(write(tuntap->fd, buf, len)); 129 | } 130 | 131 | void tuntap_close(struct tuntap_dev *tuntap) { 132 | close(tuntap->fd); 133 | } 134 | 135 | /* Fill out the ip_addr value from the interface. Called to pick up dynamic 136 | * address changes. */ 137 | void tuntap_get_address(struct tuntap_dev *tuntap) 138 | { 139 | FILE * fp=NULL; 140 | ssize_t nread=0; 141 | char buf[N2N_LINUX_SYSTEMCMD_SIZE]; 142 | 143 | /* Would rather have a more direct way to get the inet address but a netlink 144 | * socket is overkill and probably less portable than ifconfig and sed. */ 145 | 146 | /* If the interface has no address (0.0.0.0) there will be no inet addr 147 | * line and the returned string will be empty. */ 148 | snprintf( buf, sizeof(buf), "/sbin/ifconfig %s | /bin/sed -e '/inet addr:/!d' -e 's/^.*inet addr://' -e 's/ .*$//'", 149 | tuntap->dev_name ); 150 | fp=popen(buf, "r"); 151 | if (fp ) 152 | { 153 | memset(buf,0,N2N_LINUX_SYSTEMCMD_SIZE); /* make sure buf is NULL terminated. */ 154 | nread=fread(buf, 1, 15, fp); 155 | fclose(fp); 156 | fp=NULL; 157 | 158 | traceEvent(TRACE_INFO, "ifconfig address = %s", buf); 159 | 160 | tuntap->ip_addr = inet_addr(buf); 161 | } 162 | } 163 | 164 | 165 | #endif /* #ifdef __linux__ */ 166 | -------------------------------------------------------------------------------- /n2n_v2/tuntap_netbsd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2007-09 - Luca Deri 3 | * (C) 2009 - Alaric Snell-Pym 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not see see 17 | */ 18 | 19 | #include "n2n.h" 20 | 21 | #ifdef __NetBSD__ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | void tun_close(tuntap_dev *device); 28 | 29 | /* ********************************** */ 30 | 31 | #define N2N_NETBSD_TAPDEVICE_SIZE 32 32 | int tuntap_open(tuntap_dev *device /* ignored */, 33 | char *dev, 34 | const char *address_mode, /* static or dhcp */ 35 | char *device_ip, 36 | char *device_mask, 37 | const char * device_mac, 38 | int mtu) { 39 | char tap_device[N2N_NETBSD_TAPDEVICE_SIZE]; 40 | struct ifreq req; 41 | 42 | if (dev) { 43 | snprintf(tap_device, sizeof(tap_device), "/dev/%s", dev); 44 | device->fd = open(tap_device, O_RDWR); 45 | snprintf(tap_device, sizeof(tap_device), "%s", dev); 46 | } 47 | else { 48 | device->fd = open("/dev/tap", O_RDWR); 49 | if(device->fd >= 0) { 50 | if (ioctl(device->fd, TAPGIFNAME, &req) == -1) { 51 | traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s)", strerror(errno)); 52 | close(device->fd); 53 | return(-1); 54 | } 55 | else { 56 | snprintf(tap_device, sizeof(tap_device), req.ifr_name); 57 | } 58 | } 59 | } 60 | 61 | if(device->fd < 0) { 62 | traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno)); 63 | return(-1); 64 | } else { 65 | char buf[256]; 66 | FILE *fd; 67 | 68 | traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); 69 | 70 | device->ip_addr = inet_addr(device_ip); 71 | 72 | if ( device_mac && device_mac[0] != '\0' ) 73 | { 74 | /* Set the hw address before bringing the if up. */ 75 | snprintf(buf, sizeof(buf), "ifconfig %s link %s active", 76 | tap_device, device_mac); 77 | system(buf); 78 | } 79 | 80 | snprintf(buf, sizeof(buf), "ifconfig %s %s netmask %s mtu %d up", 81 | tap_device, device_ip, device_mask, mtu); 82 | system(buf); 83 | 84 | traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", 85 | tap_device, device_ip, device_mask); 86 | 87 | /* Read MAC address */ 88 | 89 | snprintf(buf, sizeof(buf), "ifconfig %s |grep address|cut -c 11-28", tap_device); 90 | /* traceEvent(TRACE_INFO, "%s", buf); */ 91 | 92 | fd = popen(buf, "r"); 93 | if(fd < 0) { 94 | tun_close(device); 95 | return(-1); 96 | } else { 97 | int a, b, c, d, e, f; 98 | 99 | buf[0] = 0; 100 | fgets(buf, sizeof(buf), fd); 101 | pclose(fd); 102 | 103 | if(buf[0] == '\0') { 104 | traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address", tap_device); 105 | exit(0); 106 | } 107 | 108 | traceEvent(TRACE_NORMAL, "Interface %s mac %s", tap_device, buf); 109 | if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { 110 | device->mac_addr[0] = a, device->mac_addr[1] = b; 111 | device->mac_addr[2] = c, device->mac_addr[3] = d; 112 | device->mac_addr[4] = e, device->mac_addr[5] = f; 113 | } 114 | } 115 | } 116 | 117 | 118 | /* read_mac(dev, device->mac_addr); */ 119 | return(device->fd); 120 | } 121 | 122 | /* ********************************** */ 123 | 124 | int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 125 | return(read(tuntap->fd, buf, len)); 126 | } 127 | 128 | /* ********************************** */ 129 | 130 | int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 131 | return(write(tuntap->fd, buf, len)); 132 | } 133 | 134 | /* ********************************** */ 135 | 136 | void tuntap_close(struct tuntap_dev *tuntap) { 137 | close(tuntap->fd); 138 | } 139 | 140 | /* Fill out the ip_addr value from the interface. Called to pick up dynamic 141 | * address changes. */ 142 | void tuntap_get_address(struct tuntap_dev *tuntap) 143 | { 144 | } 145 | 146 | #endif /* #ifdef __NetBSD__ */ 147 | -------------------------------------------------------------------------------- /n2n_v2/tuntap_osx.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2007-09 - Luca Deri 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not see see 16 | */ 17 | 18 | #include "n2n.h" 19 | 20 | #ifdef _DARWIN_ 21 | 22 | void tun_close(tuntap_dev *device); 23 | 24 | /* ********************************** */ 25 | 26 | #define N2N_OSX_TAPDEVICE_SIZE 32 27 | int tuntap_open(tuntap_dev *device /* ignored */, 28 | char *dev, 29 | const char *address_mode, /* static or dhcp */ 30 | char *device_ip, 31 | char *device_mask, 32 | const char * device_mac, 33 | int mtu) { 34 | int i; 35 | char tap_device[N2N_OSX_TAPDEVICE_SIZE]; 36 | 37 | for (i = 0; i < 255; i++) { 38 | snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i); 39 | 40 | device->fd = open(tap_device, O_RDWR); 41 | if(device->fd > 0) { 42 | traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); 43 | break; 44 | } 45 | } 46 | 47 | if(device->fd < 0) { 48 | traceEvent(TRACE_ERROR, "Unable to open tap device"); 49 | return(-1); 50 | } else { 51 | char buf[256]; 52 | FILE *fd; 53 | 54 | device->ip_addr = inet_addr(device_ip); 55 | 56 | if ( device_mac && device_mac[0] != '\0' ) 57 | { 58 | /* FIXME - This is not tested. Might be wrong syntax for OS X */ 59 | 60 | /* Set the hw address before bringing the if up. */ 61 | snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s", 62 | i, device_mac); 63 | system(buf); 64 | } 65 | 66 | snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", 67 | i, device_ip, device_mask, mtu); 68 | system(buf); 69 | 70 | traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", 71 | i, device_ip, device_mask); 72 | 73 | /* Read MAC address */ 74 | 75 | snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i); 76 | /* traceEvent(TRACE_INFO, "%s", buf); */ 77 | 78 | fd = popen(buf, "r"); 79 | if(fd < 0) { 80 | tun_close(device); 81 | return(-1); 82 | } else { 83 | int a, b, c, d, e, f; 84 | 85 | buf[0] = 0; 86 | fgets(buf, sizeof(buf), fd); 87 | pclose(fd); 88 | 89 | if(buf[0] == '\0') { 90 | traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address"); 91 | exit(0); 92 | } 93 | 94 | traceEvent(TRACE_NORMAL, "Interface tap%d [MTU %d] mac %s", i, mtu, buf); 95 | if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { 96 | device->mac_addr[0] = a, device->mac_addr[1] = b; 97 | device->mac_addr[2] = c, device->mac_addr[3] = d; 98 | device->mac_addr[4] = e, device->mac_addr[5] = f; 99 | } 100 | } 101 | } 102 | 103 | 104 | /* read_mac(dev, device->mac_addr); */ 105 | return(device->fd); 106 | } 107 | 108 | /* ********************************** */ 109 | 110 | int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 111 | return(read(tuntap->fd, buf, len)); 112 | } 113 | 114 | /* ********************************** */ 115 | 116 | int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { 117 | return(write(tuntap->fd, buf, len)); 118 | } 119 | 120 | /* ********************************** */ 121 | 122 | void tuntap_close(struct tuntap_dev *tuntap) { 123 | close(tuntap->fd); 124 | } 125 | 126 | /* Fill out the ip_addr value from the interface. Called to pick up dynamic 127 | * address changes. */ 128 | void tuntap_get_address(struct tuntap_dev *tuntap) 129 | { 130 | } 131 | 132 | #endif /* _DARWIN_ */ 133 | -------------------------------------------------------------------------------- /n2n_v2/version.c: -------------------------------------------------------------------------------- 1 | const char * n2n_sw_version = N2N_VERSION; 2 | const char * n2n_sw_osName = N2N_OSNAME; 3 | const char * n2n_sw_buildDate = __DATE__ " " __TIME__; 4 | -------------------------------------------------------------------------------- /n2n_v2/win32/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(n2n_win32 2 | win32/getopt1.c 3 | win32/getopt.c 4 | win32/wintap.c) 5 | -------------------------------------------------------------------------------- /n2n_v2/win32/DotNet/n2n.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual C++ Express 2008 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "edge", "n2n.vcproj", "{4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "supernode", "supernode.vcproj", "{BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Debug|Win32.Build.0 = Debug|Win32 16 | {4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Release|Win32.ActiveCfg = Release|Win32 17 | {4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Release|Win32.Build.0 = Release|Win32 18 | {BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}.Debug|Win32.Build.0 = Debug|Win32 20 | {BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}.Release|Win32.ActiveCfg = Release|Win32 21 | {BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}.Release|Win32.Build.0 = Release|Win32 22 | EndGlobalSection 23 | GlobalSection(SolutionProperties) = preSolution 24 | HideSolutionNode = FALSE 25 | EndGlobalSection 26 | EndGlobal 27 | -------------------------------------------------------------------------------- /n2n_v2/win32/DotNet/n2n.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ForgotFun/n2n/205c363dd106190dc4c59550b8b137b680e2291e/n2n_v2/win32/DotNet/n2n.suo -------------------------------------------------------------------------------- /n2n_v2/win32/DotNet/n2n.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 26 | 29 | 32 | 35 | 38 | 41 | 54 | 57 | 60 | 63 | 73 | 76 | 79 | 82 | 85 | 88 | 91 | 94 | 95 | 103 | 106 | 109 | 112 | 115 | 118 | 128 | 131 | 134 | 137 | 149 | 152 | 155 | 158 | 161 | 164 | 167 | 170 | 171 | 172 | 173 | 174 | 175 | 180 | 183 | 184 | 187 | 188 | 191 | 192 | 195 | 196 | 199 | 200 | 203 | 204 | 207 | 208 | 211 | 212 | 215 | 216 | 219 | 220 | 223 | 224 | 227 | 228 | 231 | 232 | 233 | 238 | 241 | 242 | 245 | 246 | 249 | 250 | 253 | 254 | 257 | 258 | 261 | 262 | 265 | 266 | 269 | 270 | 273 | 274 | 277 | 278 | 281 | 282 | 283 | 288 | 289 | 292 | 293 | 296 | 297 | 298 | 299 | 300 | 301 | -------------------------------------------------------------------------------- /n2n_v2/win32/DotNet/supernode.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 25 | 28 | 31 | 34 | 37 | 40 | 49 | 52 | 55 | 58 | 63 | 66 | 69 | 72 | 75 | 78 | 81 | 84 | 85 | 93 | 96 | 99 | 102 | 105 | 108 | 118 | 121 | 124 | 127 | 135 | 138 | 141 | 144 | 147 | 150 | 153 | 156 | 157 | 158 | 159 | 160 | 161 | 166 | 169 | 170 | 173 | 174 | 177 | 178 | 181 | 182 | 185 | 186 | 189 | 190 | 193 | 194 | 195 | 200 | 203 | 204 | 207 | 208 | 211 | 212 | 215 | 216 | 217 | 222 | 223 | 224 | 225 | 226 | 227 | -------------------------------------------------------------------------------- /n2n_v2/win32/getopt.h: -------------------------------------------------------------------------------- 1 | /* Declarations for getopt. 2 | Copyright (C) 1989,90,91,92,93,94,96,97,98 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | 5 | The GNU C Library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public License as 7 | published by the Free Software Foundation; either version 2 of the 8 | License, or (at your option) any later version. 9 | 10 | The GNU C Library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with the GNU C Library; see the file COPYING.LIB. If not, 17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 | Boston, MA 02111-1307, USA. */ 19 | 20 | #ifndef _GETOPT_H 21 | 22 | #ifndef __need_getopt 23 | # define _GETOPT_H 1 24 | #endif 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | /* For communication from `getopt' to the caller. 31 | When `getopt' finds an option that takes an argument, 32 | the argument value is returned here. 33 | Also, when `ordering' is RETURN_IN_ORDER, 34 | each non-option ARGV-element is returned here. */ 35 | 36 | extern char *optarg; 37 | 38 | /* Index in ARGV of the next element to be scanned. 39 | This is used for communication to and from the caller 40 | and for communication between successive calls to `getopt'. 41 | 42 | On entry to `getopt', zero means this is the first call; initialize. 43 | 44 | When `getopt' returns -1, this is the index of the first of the 45 | non-option elements that the caller should itself scan. 46 | 47 | Otherwise, `optind' communicates from one call to the next 48 | how much of ARGV has been scanned so far. */ 49 | 50 | extern int optind; 51 | 52 | /* Callers store zero here to inhibit the error message `getopt' prints 53 | for unrecognized options. */ 54 | 55 | extern int opterr; 56 | 57 | /* Set to an option character which was unrecognized. */ 58 | 59 | extern int optopt; 60 | 61 | #ifndef __need_getopt 62 | /* Describe the long-named options requested by the application. 63 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector 64 | of `struct option' terminated by an element containing a name which is 65 | zero. 66 | 67 | The field `has_arg' is: 68 | no_argument (or 0) if the option does not take an argument, 69 | required_argument (or 1) if the option requires an argument, 70 | optional_argument (or 2) if the option takes an optional argument. 71 | 72 | If the field `flag' is not NULL, it points to a variable that is set 73 | to the value given in the field `val' when the option is found, but 74 | left unchanged if the option is not found. 75 | 76 | To have a long-named option do something other than set an `int' to 77 | a compiled-in constant, such as set a value from `optarg', set the 78 | option's `flag' field to zero and its `val' field to a nonzero 79 | value (the equivalent single-letter option character, if there is 80 | one). For long options that have a zero `flag' field, `getopt' 81 | returns the contents of the `val' field. */ 82 | 83 | struct option 84 | { 85 | # if defined __STDC__ && __STDC__ 86 | const char *name; 87 | # else 88 | char *name; 89 | # endif 90 | /* has_arg can't be an enum because some compilers complain about 91 | type mismatches in all the code that assumes it is an int. */ 92 | int has_arg; 93 | int *flag; 94 | int val; 95 | }; 96 | 97 | /* Names for the values of the `has_arg' field of `struct option'. */ 98 | 99 | # define no_argument 0 100 | # define required_argument 1 101 | # define optional_argument 2 102 | #endif /* need getopt */ 103 | 104 | 105 | /* Get definitions and prototypes for functions to process the 106 | arguments in ARGV (ARGC of them, minus the program name) for 107 | options given in OPTS. 108 | 109 | Return the option character from OPTS just read. Return -1 when 110 | there are no more options. For unrecognized options, or options 111 | missing arguments, `optopt' is set to the option letter, and '?' is 112 | returned. 113 | 114 | The OPTS string is a list of characters which are recognized option 115 | letters, optionally followed by colons, specifying that that letter 116 | takes an argument, to be placed in `optarg'. 117 | 118 | If a letter in OPTS is followed by two colons, its argument is 119 | optional. This behavior is specific to the GNU `getopt'. 120 | 121 | The argument `--' causes premature termination of argument 122 | scanning, explicitly telling `getopt' that there are no more 123 | options. 124 | 125 | If OPTS begins with `--', then non-option arguments are treated as 126 | arguments to the option '\0'. This behavior is specific to the GNU 127 | `getopt'. */ 128 | 129 | #if defined __STDC__ && __STDC__ 130 | # ifdef __GNU_LIBRARY__ 131 | /* Many other libraries have conflicting prototypes for getopt, with 132 | differences in the consts, in stdlib.h. To avoid compilation 133 | errors, only prototype getopt for the GNU C library. */ 134 | extern int getopt (int __argc, char *const *__argv, const char *__shortopts); 135 | # else /* not __GNU_LIBRARY__ */ 136 | extern int getopt (); 137 | # endif /* __GNU_LIBRARY__ */ 138 | 139 | # ifndef __need_getopt 140 | extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts, 141 | const struct option *__longopts, int *__longind); 142 | extern int getopt_long_only (int __argc, char *const *__argv, 143 | const char *__shortopts, 144 | const struct option *__longopts, int *__longind); 145 | 146 | /* Internal only. Users should not call this directly. */ 147 | extern int _getopt_internal (int __argc, char *const *__argv, 148 | const char *__shortopts, 149 | const struct option *__longopts, int *__longind, 150 | int __long_only); 151 | # endif 152 | #else /* not __STDC__ */ 153 | extern int getopt (); 154 | # ifndef __need_getopt 155 | extern int getopt_long (); 156 | extern int getopt_long_only (); 157 | 158 | extern int _getopt_internal (); 159 | # endif 160 | #endif /* __STDC__ */ 161 | 162 | #ifdef __cplusplus 163 | } 164 | #endif 165 | 166 | /* Make sure we later can get all the definitions and declarations. */ 167 | #undef __need_getopt 168 | 169 | #endif /* getopt.h */ 170 | -------------------------------------------------------------------------------- /n2n_v2/win32/getopt1.c: -------------------------------------------------------------------------------- 1 | /* getopt_long and getopt_long_only entry points for GNU getopt. 2 | Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 3 | Free Software Foundation, Inc. 4 | This file is part of the GNU C Library. 5 | 6 | The GNU C Library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Library General Public License as 8 | published by the Free Software Foundation; either version 2 of the 9 | License, or (at your option) any later version. 10 | 11 | The GNU C Library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Library General Public License for more details. 15 | 16 | You should have received a copy of the GNU Library General Public 17 | License along with the GNU C Library; see the file COPYING.LIB. If not, 18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 19 | Boston, MA 02111-1307, USA. */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include 23 | #endif 24 | 25 | #include "getopt.h" 26 | 27 | #if !defined __STDC__ || !__STDC__ 28 | /* This is a separate conditional since some stdc systems 29 | reject `defined (const)'. */ 30 | #ifndef const 31 | #define const 32 | #endif 33 | #endif 34 | 35 | #include 36 | 37 | /* Comment out all this code if we are using the GNU C Library, and are not 38 | actually compiling the library itself. This code is part of the GNU C 39 | Library, but also included in many other GNU distributions. Compiling 40 | and linking in this code is a waste when using the GNU C library 41 | (especially if it is a shared library). Rather than having every GNU 42 | program understand `configure --with-gnu-libc' and omit the object files, 43 | it is simpler to just do this in the source for each such file. */ 44 | 45 | #define GETOPT_INTERFACE_VERSION 2 46 | #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 47 | #include 48 | #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 49 | #define ELIDE_CODE 50 | #endif 51 | #endif 52 | 53 | #ifndef ELIDE_CODE 54 | 55 | 56 | /* This needs to come after some library #include 57 | to get __GNU_LIBRARY__ defined. */ 58 | #ifdef __GNU_LIBRARY__ 59 | #include 60 | #endif 61 | 62 | #ifndef NULL 63 | #define NULL 0 64 | #endif 65 | 66 | int 67 | getopt_long (argc, argv, options, long_options, opt_index) 68 | int argc; 69 | char *const *argv; 70 | const char *options; 71 | const struct option *long_options; 72 | int *opt_index; 73 | { 74 | return _getopt_internal (argc, argv, options, long_options, opt_index, 0); 75 | } 76 | 77 | /* Like getopt_long, but '-' as well as '--' can indicate a long option. 78 | If an option that starts with '-' (not '--') doesn't match a long option, 79 | but does match a short option, it is parsed as a short option 80 | instead. */ 81 | 82 | int 83 | getopt_long_only (argc, argv, options, long_options, opt_index) 84 | int argc; 85 | char *const *argv; 86 | const char *options; 87 | const struct option *long_options; 88 | int *opt_index; 89 | { 90 | return _getopt_internal (argc, argv, options, long_options, opt_index, 1); 91 | } 92 | 93 | 94 | #endif /* Not ELIDE_CODE. */ 95 | 96 | #ifdef TEST 97 | 98 | #include 99 | 100 | int 101 | main (argc, argv) 102 | int argc; 103 | char **argv; 104 | { 105 | int c; 106 | int digit_optind = 0; 107 | 108 | while (1) 109 | { 110 | int this_option_optind = optind ? optind : 1; 111 | int option_index = 0; 112 | static struct option long_options[] = 113 | { 114 | {"add", 1, 0, 0}, 115 | {"append", 0, 0, 0}, 116 | {"delete", 1, 0, 0}, 117 | {"verbose", 0, 0, 0}, 118 | {"create", 0, 0, 0}, 119 | {"file", 1, 0, 0}, 120 | {0, 0, 0, 0} 121 | }; 122 | 123 | c = getopt_long (argc, argv, "abc:d:0123456789", 124 | long_options, &option_index); 125 | if (c == -1) 126 | break; 127 | 128 | switch (c) 129 | { 130 | case 0: 131 | printf ("option %s", long_options[option_index].name); 132 | if (optarg) 133 | printf (" with arg %s", optarg); 134 | printf ("\n"); 135 | break; 136 | 137 | case '0': 138 | case '1': 139 | case '2': 140 | case '3': 141 | case '4': 142 | case '5': 143 | case '6': 144 | case '7': 145 | case '8': 146 | case '9': 147 | if (digit_optind != 0 && digit_optind != this_option_optind) 148 | printf ("digits occur in two different argv-elements.\n"); 149 | digit_optind = this_option_optind; 150 | printf ("option %c\n", c); 151 | break; 152 | 153 | case 'a': 154 | printf ("option a\n"); 155 | break; 156 | 157 | case 'b': 158 | printf ("option b\n"); 159 | break; 160 | 161 | case 'c': 162 | printf ("option c with value `%s'\n", optarg); 163 | break; 164 | 165 | case 'd': 166 | printf ("option d with value `%s'\n", optarg); 167 | break; 168 | 169 | case '?': 170 | break; 171 | 172 | default: 173 | printf ("?? getopt returned character code 0%o ??\n", c); 174 | } 175 | } 176 | 177 | if (optind < argc) 178 | { 179 | printf ("non-option ARGV-elements: "); 180 | while (optind < argc) 181 | printf ("%s ", argv[optind++]); 182 | printf ("\n"); 183 | } 184 | 185 | exit (0); 186 | } 187 | 188 | #endif /* TEST */ 189 | -------------------------------------------------------------------------------- /n2n_v2/win32/n2n_win32.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | (C) 2007-09 - Luca Deri 4 | 5 | */ 6 | 7 | #ifndef _N2N_WIN32_H_ 8 | #define _N2N_WIN32_H_ 9 | 10 | #ifndef _CRT_SECURE_NO_WARNINGS 11 | #define _CRT_SECURE_NO_WARNINGS 12 | #endif 13 | 14 | #if defined(__MINGW32__) 15 | /* should be defined here and before winsock gets included */ 16 | #define _WIN32_WINNT 0x501 //Otherwise the linker doesnt find getaddrinfo 17 | #include 18 | #endif /* #if defined(__MINGW32__) */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | 25 | #include "wintap.h" 26 | 27 | #ifdef _MSC_VER 28 | #include "getopt.h" 29 | 30 | /* Other Win environments are expected to support stdint.h */ 31 | 32 | /* stdint.h typedefs (C99) (not present in Visual Studio) */ 33 | typedef unsigned int uint32_t; 34 | typedef unsigned short uint16_t; 35 | typedef unsigned char uint8_t; 36 | 37 | /* sys/types.h typedefs (not present in Visual Studio) */ 38 | typedef unsigned int u_int32_t; 39 | typedef unsigned short u_int16_t; 40 | typedef unsigned char u_int8_t; 41 | 42 | typedef int ssize_t; 43 | #endif /* #ifdef _MSC_VER */ 44 | 45 | typedef unsigned long in_addr_t; 46 | 47 | 48 | #define EAFNOSUPPORT WSAEAFNOSUPPORT 49 | #define MAX(a,b) (a > b ? a : b) 50 | #define MIN(a,b) (a < b ? a : b) 51 | 52 | #define snprintf _snprintf 53 | #define strdup _strdup 54 | 55 | #define socklen_t int 56 | 57 | #define ETH_ADDR_LEN 6 58 | /* 59 | * Structure of a 10Mb/s Ethernet header. 60 | */ 61 | struct ether_hdr 62 | { 63 | uint8_t dhost[ETH_ADDR_LEN]; 64 | uint8_t shost[ETH_ADDR_LEN]; 65 | uint16_t type; /* higher layer protocol encapsulated */ 66 | }; 67 | 68 | typedef struct ether_hdr ether_hdr_t; 69 | 70 | /* ************************************* */ 71 | 72 | struct ip { 73 | #if BYTE_ORDER == LITTLE_ENDIAN 74 | u_char ip_hl:4, /* header length */ 75 | ip_v:4; /* version */ 76 | #else 77 | u_char ip_v:4, /* version */ 78 | ip_hl:4; /* header length */ 79 | #endif 80 | u_char ip_tos; /* type of service */ 81 | short ip_len; /* total length */ 82 | u_short ip_id; /* identification */ 83 | short ip_off; /* fragment offset field */ 84 | #define IP_DF 0x4000 /* dont fragment flag */ 85 | #define IP_MF 0x2000 /* more fragments flag */ 86 | #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ 87 | u_char ip_ttl; /* time to live */ 88 | u_char ip_p; /* protocol */ 89 | u_short ip_sum; /* checksum */ 90 | struct in_addr ip_src,ip_dst; /* source and dest address */ 91 | }; 92 | 93 | 94 | /* ************************************* */ 95 | 96 | typedef struct tuntap_dev { 97 | HANDLE device_handle; 98 | char *device_name; 99 | char *ifName; 100 | OVERLAPPED overlap_read, overlap_write; 101 | uint8_t mac_addr[6]; 102 | uint32_t ip_addr, device_mask; 103 | unsigned int mtu; 104 | } tuntap_dev; 105 | 106 | #define index(a, b) strchr(a, b) 107 | 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /n2n_v2/win32/version-msvc.c: -------------------------------------------------------------------------------- 1 | const char * n2n_sw_version = "2.0.0"; 2 | const char * n2n_sw_osName = "Win32"; 3 | const char * n2n_sw_buildDate = __DATE__ " " __TIME__; 4 | -------------------------------------------------------------------------------- /n2n_v2/win32/wintap.h: -------------------------------------------------------------------------------- 1 | /* 2 | (C) 2007 - Luca Deri 3 | */ 4 | 5 | #ifndef _WINTAP_H_ 6 | #define _WINTAP_H_ 7 | 8 | #undef UNICODE 9 | #undef _UNICODE 10 | #define _CRT_SECURE_NO_WARNINGS 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | 17 | 18 | //=============================================== 19 | // This file is included both by OpenVPN and 20 | // the TAP-Win32 driver and contains definitions 21 | // common to both. 22 | //=============================================== 23 | 24 | //============= 25 | // TAP IOCTLs 26 | //============= 27 | 28 | #define TAP_CONTROL_CODE(request,method) \ 29 | CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS) 30 | 31 | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED) 32 | #define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED) 33 | #define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED) 34 | #define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED) 35 | #define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED) 36 | #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED) 37 | #define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED) 38 | #define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED) 39 | #define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED) 40 | 41 | //================= 42 | // Registry keys 43 | //================= 44 | 45 | #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}" 46 | #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" 47 | 48 | //====================== 49 | // Filesystem prefixes 50 | //====================== 51 | 52 | #define USERMODEDEVICEDIR "\\\\.\\Global\\" 53 | #define SYSDEVICEDIR "\\Device\\" 54 | #define USERDEVICEDIR "\\DosDevices\\Global\\" 55 | #define TAPSUFFIX ".tap" 56 | 57 | //========================================================= 58 | // TAP_COMPONENT_ID -- This string defines the TAP driver 59 | // type -- different component IDs can reside in the system 60 | // simultaneously. 61 | //========================================================= 62 | 63 | #define TAP_COMPONENT_ID "tap0801" 64 | 65 | extern void initWin32(); 66 | 67 | #endif --------------------------------------------------------------------------------