New upstream version 3.1.1

This commit is contained in:
billchenchina 2022-11-15 08:50:35 +08:00
parent 4e9934e5ec
commit e7b41df57b
229 changed files with 57000 additions and 12055 deletions

5
win32/CMakeLists.txt Normal file
View file

@ -0,0 +1,5 @@
add_library(n2n_win32
getopt1.c
getopt.c
wintap.c)
target_link_libraries(n2n_win32 PUBLIC ws2_32.lib)

26
win32/DotNet/n2n.sln Normal file
View file

@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual C++ Express 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "edge", "n2n.vcproj", "{4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "supernode", "supernode.vcproj", "{BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Debug|Win32.ActiveCfg = Debug|Win32
{4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Debug|Win32.Build.0 = Debug|Win32
{4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Release|Win32.ActiveCfg = Release|Win32
{4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}.Release|Win32.Build.0 = Release|Win32
{BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}.Debug|Win32.ActiveCfg = Debug|Win32
{BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}.Debug|Win32.Build.0 = Debug|Win32
{BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}.Release|Win32.ActiveCfg = Release|Win32
{BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

BIN
win32/DotNet/n2n.suo Normal file

Binary file not shown.

300
win32/DotNet/n2n.vcproj Normal file
View file

@ -0,0 +1,300 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9,00"
Name="edge"
ProjectGUID="{4911ADD4-08A3-4C9F-B9C9-9492DA10D01D}"
RootNamespace="n2n"
Keyword="Win32Proj"
TargetFrameworkVersion="131072"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
UseUnicodeResponseFiles="false"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="false"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="Ws2_32.lib Advapi32.lib libeay32.lib"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
RandomizedBaseAddress="1"
DataExecutionPrevention="0"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="0"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="false"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="Ws2_32.lib Advapi32.lib libeay32.lib"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
RandomizedBaseAddress="1"
DataExecutionPrevention="0"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\..\edge.c"
>
</File>
<File
RelativePath="..\getopt.c"
>
</File>
<File
RelativePath="..\getopt1.c"
>
</File>
<File
RelativePath="..\..\minilzo.c"
>
</File>
<File
RelativePath="..\..\n2n.c"
>
</File>
<File
RelativePath="..\..\n2n_keyfile.c"
>
</File>
<File
RelativePath="..\..\transform_aes.c"
>
</File>
<File
RelativePath="..\..\transform_null.c"
>
</File>
<File
RelativePath="..\..\transform_tf.c"
>
</File>
<File
RelativePath="..\..\twofish.c"
>
</File>
<File
RelativePath="..\version-msvc.c"
>
</File>
<File
RelativePath="..\wintap.c"
>
</File>
<File
RelativePath="..\..\wire.c"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\getopt.h"
>
</File>
<File
RelativePath="..\..\lzoconf.h"
>
</File>
<File
RelativePath="..\..\lzodefs.h"
>
</File>
<File
RelativePath="..\..\minilzo.h"
>
</File>
<File
RelativePath="..\..\n2n.h"
>
</File>
<File
RelativePath="..\..\n2n_keyfile.h"
>
</File>
<File
RelativePath="..\..\n2n_transforms.h"
>
</File>
<File
RelativePath="..\n2n_win32.h"
>
</File>
<File
RelativePath="..\..\n2n_wire.h"
>
</File>
<File
RelativePath="..\..\twofish.h"
>
</File>
<File
RelativePath="..\wintap.h"
>
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
<File
RelativePath="..\..\HACKING"
>
</File>
<File
RelativePath="..\..\README"
>
</File>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View file

@ -0,0 +1,226 @@
<?xml version="1.0" encoding="windows-1250"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9,00"
Name="supernode"
ProjectGUID="{BDB93CAB-BE22-4ED6-9A05-2E4D6F1D76E1}"
RootNamespace="supernode"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
GenerateDebugInformation="true"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="false"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="0"
EnableFunctionLevelLinking="false"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="Ws2_32.lib"
GenerateDebugInformation="true"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\getopt.c"
>
</File>
<File
RelativePath="..\getopt1.c"
>
</File>
<File
RelativePath="..\..\n2n.c"
>
</File>
<File
RelativePath="..\..\sn.c"
>
</File>
<File
RelativePath="..\version-msvc.c"
>
</File>
<File
RelativePath="..\wintap.c"
>
</File>
<File
RelativePath="..\..\wire.c"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\getopt.h"
>
</File>
<File
RelativePath="..\..\n2n.h"
>
</File>
<File
RelativePath="..\n2n_win32.h"
>
</File>
<File
RelativePath="..\..\n2n_wire.h"
>
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

19
win32/Makefile Normal file
View file

@ -0,0 +1,19 @@
#
# This is not a standalone makefile, it must be called from the toplevel
# makefile to inherit the correct environment
CFLAGS+=-I../include
LDFLAGS+=-L..
.PHONY: all clean install
all: n2n_win32.a
n2n_win32.a: getopt1.o getopt.o wintap.o
$(AR) rcs $@ $+
clean:
rm -rf n2n_win32.a *.o *.gcno *.gcda
install:
true

1092
win32/getopt.c Normal file

File diff suppressed because it is too large Load diff

187
win32/getopt.h Normal file
View file

@ -0,0 +1,187 @@
/**
* (C) 2007-22 - ntop.org and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/>
*
*/
/* Declarations for getopt.
Copyright (C) 1989,90,91,92,93,94,96,97,98 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifndef _GETOPT_H
#ifndef __need_getopt
# define _GETOPT_H 1
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* For communication from `getopt' to the caller.
When `getopt' finds an option that takes an argument,
the argument value is returned here.
Also, when `ordering' is RETURN_IN_ORDER,
each non-option ARGV-element is returned here. */
extern char *optarg;
/* Index in ARGV of the next element to be scanned.
This is used for communication to and from the caller
and for communication between successive calls to `getopt'.
On entry to `getopt', zero means this is the first call; initialize.
When `getopt' returns -1, this is the index of the first of the
non-option elements that the caller should itself scan.
Otherwise, `optind' communicates from one call to the next
how much of ARGV has been scanned so far. */
extern int optind;
/* Callers store zero here to inhibit the error message `getopt' prints
for unrecognized options. */
extern int opterr;
/* Set to an option character which was unrecognized. */
extern int optopt;
#ifndef __need_getopt
/* Describe the long-named options requested by the application.
The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
of `struct option' terminated by an element containing a name which is
zero.
The field `has_arg' is:
no_argument (or 0) if the option does not take an argument,
required_argument (or 1) if the option requires an argument,
optional_argument (or 2) if the option takes an optional argument.
If the field `flag' is not NULL, it points to a variable that is set
to the value given in the field `val' when the option is found, but
left unchanged if the option is not found.
To have a long-named option do something other than set an `int' to
a compiled-in constant, such as set a value from `optarg', set the
option's `flag' field to zero and its `val' field to a nonzero
value (the equivalent single-letter option character, if there is
one). For long options that have a zero `flag' field, `getopt'
returns the contents of the `val' field. */
struct option
{
# if defined __STDC__ && __STDC__
const char *name;
# else
char *name;
# endif
/* has_arg can't be an enum because some compilers complain about
type mismatches in all the code that assumes it is an int. */
int has_arg;
int *flag;
int val;
};
/* Names for the values of the `has_arg' field of `struct option'. */
# define no_argument 0
# define required_argument 1
# define optional_argument 2
#endif /* need getopt */
/* Get definitions and prototypes for functions to process the
arguments in ARGV (ARGC of them, minus the program name) for
options given in OPTS.
Return the option character from OPTS just read. Return -1 when
there are no more options. For unrecognized options, or options
missing arguments, `optopt' is set to the option letter, and '?' is
returned.
The OPTS string is a list of characters which are recognized option
letters, optionally followed by colons, specifying that that letter
takes an argument, to be placed in `optarg'.
If a letter in OPTS is followed by two colons, its argument is
optional. This behavior is specific to the GNU `getopt'.
The argument `--' causes premature termination of argument
scanning, explicitly telling `getopt' that there are no more
options.
If OPTS begins with `--', then non-option arguments are treated as
arguments to the option '\0'. This behavior is specific to the GNU
`getopt'. */
#if defined __STDC__ && __STDC__
# ifdef __GNU_LIBRARY__
/* Many other libraries have conflicting prototypes for getopt, with
differences in the consts, in stdlib.h. To avoid compilation
errors, only prototype getopt for the GNU C library. */
extern int getopt (int __argc, char *const *__argv, const char *__shortopts);
# else /* not __GNU_LIBRARY__ */
extern int getopt ();
# endif /* __GNU_LIBRARY__ */
# ifndef __need_getopt
extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts,
const struct option *__longopts, int *__longind);
extern int getopt_long_only (int __argc, char *const *__argv,
const char *__shortopts,
const struct option *__longopts, int *__longind);
/* Internal only. Users should not call this directly. */
extern int _getopt_internal (int __argc, char *const *__argv,
const char *__shortopts,
const struct option *__longopts, int *__longind,
int __long_only);
# endif
#else /* not __STDC__ */
extern int getopt ();
# ifndef __need_getopt
extern int getopt_long ();
extern int getopt_long_only ();
extern int _getopt_internal ();
# endif
#endif /* __STDC__ */
#ifdef __cplusplus
}
#endif
/* Make sure we later can get all the definitions and declarations. */
#undef __need_getopt
#endif /* getopt.h */

207
win32/getopt1.c Normal file
View file

@ -0,0 +1,207 @@
/**
* (C) 2007-22 - ntop.org and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/>
*
*/
/* getopt_long and getopt_long_only entry points for GNU getopt.
Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "getopt.h"
#if !defined __STDC__ || !__STDC__
/* This is a separate conditional since some stdc systems
reject `defined (const)'. */
#ifndef const
#define const
#endif
#endif
#include <stdio.h>
/* Comment out all this code if we are using the GNU C Library, and are not
actually compiling the library itself. This code is part of the GNU C
Library, but also included in many other GNU distributions. Compiling
and linking in this code is a waste when using the GNU C library
(especially if it is a shared library). Rather than having every GNU
program understand `configure --with-gnu-libc' and omit the object files,
it is simpler to just do this in the source for each such file. */
#define GETOPT_INTERFACE_VERSION 2
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
#include <gnu-versions.h>
#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
#define ELIDE_CODE
#endif
#endif
#ifndef ELIDE_CODE
/* This needs to come after some library #include
to get __GNU_LIBRARY__ defined. */
#ifdef __GNU_LIBRARY__
#include <stdlib.h>
#endif
#ifndef NULL
#define NULL 0
#endif
int
getopt_long (argc, argv, options, long_options, opt_index)
int argc;
char *const *argv;
const char *options;
const struct option *long_options;
int *opt_index;
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
}
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
If an option that starts with '-' (not '--') doesn't match a long option,
but does match a short option, it is parsed as a short option
instead. */
int
getopt_long_only (argc, argv, options, long_options, opt_index)
int argc;
char *const *argv;
const char *options;
const struct option *long_options;
int *opt_index;
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
}
#endif /* Not ELIDE_CODE. */
#ifdef TEST
#include <stdio.h>
int
main (argc, argv)
int argc;
char **argv;
{
int c;
int digit_optind = 0;
while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] =
{
{"add", 1, 0, 0},
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 0, 0, 0},
{"file", 1, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "abc:d:0123456789",
long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf ("option %c\n", c);
break;
case 'a':
printf ("option a\n");
break;
case 'b':
printf ("option b\n");
break;
case 'c':
printf ("option c with value `%s'\n", optarg);
break;
case 'd':
printf ("option d with value `%s'\n", optarg);
break;
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
exit (0);
}
#endif /* TEST */

132
win32/n2n_win32.h Normal file
View file

@ -0,0 +1,132 @@
/*
(C) 2007-22 - Luca Deri <deri@ntop.org>
*/
#ifndef _N2N_WIN32_H_
#define _N2N_WIN32_H_
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#define WIN32_LEAN_AND_MEAN
#if defined(__MINGW32__)
/* should be defined here and before winsock gets included */
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x501 //Otherwise the linker doesnt find getaddrinfo
#endif /* #ifndef _WIN32_WINNT */
#include <inttypes.h>
#endif /* #if defined(__MINGW32__) */
#include <winsock2.h>
#include <windows.h>
#include <ws2def.h>
#include <ws2ipdef.h>
#if defined(_MSC_VER)
#include <Iphlpapi.h>
#pragma comment(lib,"Iphlpapi.lib")
#endif
#include <netioapi.h>
#include <winioctl.h>
#include <iptypes.h>
#include "wintap.h"
#undef EAFNOSUPPORT
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define snprintf _snprintf
#define strdup _strdup
#define socklen_t int
/* ************************************* */
struct ip {
#if BYTE_ORDER == LITTLE_ENDIAN
u_char ip_hl:4, /* header length */
ip_v:4; /* version */
#else
u_char ip_v:4, /* version */
ip_hl:4; /* header length */
#endif
u_char ip_tos; /* type of service */
short ip_len; /* total length */
u_short ip_id; /* identification */
short ip_off; /* fragment offset field */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_char ip_ttl; /* time to live */
u_char ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src,ip_dst; /* source and dest address */
};
/* ************************************* */
typedef struct tuntap_dev {
HANDLE device_handle;
char *device_name;
char *ifName;
int if_idx;
OVERLAPPED overlap_read, overlap_write;
n2n_mac_t mac_addr;
uint32_t ip_addr;
uint32_t device_mask;
unsigned int mtu;
unsigned int metric;
unsigned int metric_original;
} tuntap_dev;
/* ************************************* */
#define index(a, b) strchr(a, b)
#define sleep(x) Sleep(x * 1000)
/* ************************************* */
#define HAVE_PTHREAD
#define pthread_t HANDLE
#define pthread_mutex_t HANDLE
#define pthread_create(p_thread_handle, attr, thread_func, p_param) \
(*p_thread_handle = CreateThread(0 /* default security flags */, 0 /*default stack*/, \
thread_func, p_param, 0 /* default creation flags */, \
NULL) == 0)
#define pthread_cancel(p_thread_handle) \
TerminateThread(p_thread_handle, 0)
#define pthread_mutex_init(p_mutex_handle, attr) \
*p_mutex_handle = CreateMutex(NULL /*default security flags */, \
FALSE /* initially not owned */, NULL /* unnamed */)
#define pthread_mutex_lock(mutex) \
WaitForSingleObject(*mutex, INFINITE)
#define pthread_mutex_trylock(mutex) \
WaitForSingleObject(*mutex, NULL)
#define pthread_mutex_unlock(mutex) \
ReleaseMutex(*mutex)
/* ************************************* */
#endif

3
win32/version-msvc.c Normal file
View file

@ -0,0 +1,3 @@
const char * n2n_sw_version = "2.0.0";
const char * n2n_sw_osName = "Win32";
const char * n2n_sw_buildDate = __DATE__ " " __TIME__;

11
win32/winconfig.h Normal file
View file

@ -0,0 +1,11 @@
/* winconfig.h. Win32 replacement for file generated from config.h.in by configure. */
/* OS name */
#ifndef PACKAGE_OSNAME
#define PACKAGE_OSNAME "windows"
#endif
/* Define to the version of this package. */
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION N2N_VERSION
#endif

517
win32/wintap.c Normal file
View file

@ -0,0 +1,517 @@
/*
(C) 2007-22 - Luca Deri <deri@ntop.org>
*/
#include "n2n.h"
#include "n2n_win32.h"
/* ***************************************************** */
void initWin32() {
WSADATA wsaData;
int err;
err = WSAStartup(MAKEWORD(2, 2), &wsaData );
if( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("FATAL ERROR: unable to initialise Winsock 2.x.");
exit(EXIT_FAILURE);
}
}
void destroyWin32() {
WSACleanup();
}
struct win_adapter_info {
HANDLE handle;
char adapterid[1024];
char adaptername[1024];
};
/* ***************************************************** */
static HANDLE open_tap_device(const char *adapterid) {
char tapname[1024];
_snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
return(CreateFile(tapname, GENERIC_WRITE | GENERIC_READ,
0, /* Don't let other processes share or open
the resource until the handle's been closed */
0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0));
}
/* ***************************************************** */
static void iterate_win_network_adapters(
int (*callback)(struct win_adapter_info*, struct tuntap_dev *),
void *userdata) {
HKEY key, key2;
char regpath[1024];
long len, rc;
int found = 0;
int err, i;
struct win_adapter_info adapter;
/* Open registry and look for network adapters */
if((rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &key))) {
printf("Unable to read registry: [rc=%d]\n", rc);
exit(EXIT_FAILURE);
/* MSVC Note: If you keep getting rc=2 errors, make sure you set:
Project -> Properties -> Configuration Properties -> General -> Character set
to: "Use Multi-Byte Character Set"
*/
}
for (i = 0; ; i++) {
len = sizeof(adapter.adapterid);
if(RegEnumKeyEx(key, i, (LPTSTR)adapter.adapterid, &len, 0, 0, 0, NULL))
break;
/* Find out more about this adapter */
_snprintf(regpath, sizeof(regpath), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, adapter.adapterid);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCSTR)regpath, 0, KEY_READ, &key2))
continue;
len = sizeof(adapter.adaptername);
err = RegQueryValueEx(key2, "Name", 0, 0, adapter.adaptername, &len);
RegCloseKey(key2);
if(err)
continue;
adapter.handle = open_tap_device(adapter.adapterid);
if(adapter.handle != INVALID_HANDLE_VALUE) {
/* Valid device, use the callback */
if(!callback(&adapter, userdata))
break;
else
CloseHandle(adapter.handle);
/* continue */
}
}
RegCloseKey(key);
}
/* ***************************************************** */
static int print_adapter_callback(struct win_adapter_info *adapter, struct tuntap_dev *device) {
printf(" %s - %s\n", adapter->adapterid, adapter->adaptername);
/* continue */
return(1);
}
void win_print_available_adapters() {
iterate_win_network_adapters(print_adapter_callback, NULL);
}
/* ***************************************************** */
static int lookup_adapter_info_reg(const char *target_adapter, char *regpath, size_t regpath_size) {
HKEY key, key2;
long len, rc;
char index[16];
int err, i;
char adapter_name[N2N_IFNAMSIZ];
int rv = 0;
if((rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ADAPTER_INFO_KEY, 0, KEY_READ, &key))) {
printf("Unable to read registry: %s, [rc=%d]\n", ADAPTER_INFO_KEY, rc);
exit(EXIT_FAILURE);
}
for(i = 0; ; i++) {
len = sizeof(index);
if(RegEnumKeyEx(key, i, (LPTSTR)index, &len, 0, 0, 0, NULL))
break;
_snprintf(regpath, regpath_size, "%s\\%s", ADAPTER_INFO_KEY, index);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCSTR)regpath, 0, KEY_READ, &key2))
continue;
len = sizeof(adapter_name);
err = RegQueryValueEx(key2, "NetCfgInstanceId", 0, 0, adapter_name, &len);
RegCloseKey(key2);
if(err)
continue;
if(!strcmp(adapter_name, target_adapter)) {
rv = 1;
break;
}
}
RegCloseKey(key);
return(rv);
}
/* ***************************************************** */
static void set_interface_mac(struct tuntap_dev *device, const char *mac_str) {
char cmd[256];
char mac_buf[18];
char adapter_info_reg[1024];
uint64_t mac = 0;
uint8_t *ptr = (uint8_t*)&mac;
if(strlen(mac_str) != 17) {
printf("Invalid MAC: %s\n", mac_str);
exit(EXIT_FAILURE);
}
/* Remove the colons */
for(int i=0; i<6; i++) {
mac_buf[i*2] = mac_str[2*i + i];
mac_buf[i*2+1] = mac_str[2*i + i + 1];
}
mac_buf[12] = '\0';
if(!lookup_adapter_info_reg(device->device_name, adapter_info_reg, sizeof(adapter_info_reg))) {
printf("Could not determine adapter MAC registry key\n");
exit(EXIT_FAILURE);
}
_snprintf(cmd, sizeof(cmd),
"reg add HKEY_LOCAL_MACHINE\\%s /v MAC /d %s /f > nul", adapter_info_reg, mac_buf);
system(cmd);
/* Put down then up again to apply */
CloseHandle(device->device_handle);
_snprintf(cmd, sizeof(cmd), "netsh interface set interface \"%s\" disabled > nul", device->ifName);
system(cmd);
_snprintf(cmd, sizeof(cmd), "netsh interface set interface \"%s\" enabled > nul", device->ifName);
system(cmd);
device->device_handle = open_tap_device(device->device_name);
if(device->device_handle == INVALID_HANDLE_VALUE) {
printf("Reopening TAP device \"%s\" failed\n", device->device_name);
exit(EXIT_FAILURE);
}
}
/* ***************************************************** */
static int choose_adapter_callback(struct win_adapter_info *adapter, struct tuntap_dev *device) {
if(device->device_name) {
/* A device name filter was set, name must match */
if(strcmp(device->device_name, adapter->adapterid) &&
strcmp(device->device_name, adapter->adaptername)) {
/* Not found, continue */
return(1);
}
} /* otherwise just pick the first available adapter */
/* Adapter found, break */
device->device_handle = adapter->handle;
if(device->device_name) free(device->device_name);
device->device_name = _strdup(adapter->adapterid);
device->ifName = _strdup(adapter->adaptername);
return(0);
}
/* ***************************************************** */
int open_wintap(struct tuntap_dev *device,
const char * devname,
const char * address_mode, /* "static" or "dhcp" */
char *device_ip,
char *device_mask,
const char *device_mac,
int mtu,
int metric) {
char cmd[256];
DWORD len;
ULONG status = TRUE;
memset(device, 0, sizeof(struct tuntap_dev));
device->device_handle = INVALID_HANDLE_VALUE;
device->device_name = devname[0] ? _strdup(devname) : NULL;
device->ifName = NULL;
device->ip_addr = inet_addr(device_ip);
iterate_win_network_adapters(choose_adapter_callback, device);
if(device->device_handle == INVALID_HANDLE_VALUE) {
if(!devname[0])
printf("No Windows tap devices found, did you run tapinstall.exe?\n");
else
printf("Cannot find tap device \"%s\"\n", devname);
return -1;
}
/* ************************************** */
/* interface index, required for routing */
ULONG buffer_len = 0;
IP_ADAPTER_INFO *buffer;
// get required buffer size and allocate buffer
GetAdaptersInfo(NULL, &buffer_len);
buffer = malloc(buffer_len);
// find device by name and get its index
if(buffer && !GetAdaptersInfo(buffer, &buffer_len)) {
IP_ADAPTER_INFO *i;
for(i = buffer; i != NULL; i = i->Next) {
if(!strcmp(device->device_name, i->AdapterName)) {
device->if_idx = i->Index;
break;
}
}
}
free(buffer);
/* ************************************** */
if(device_mac[0])
set_interface_mac(device, device_mac);
/* Get MAC address from tap device->device_name */
if(!DeviceIoControl(device->device_handle, TAP_IOCTL_GET_MAC,
device->mac_addr, sizeof(device->mac_addr),
device->mac_addr, sizeof(device->mac_addr), &len, 0)) {
printf("Could not get MAC address from Windows tap %s (%s)\n",
device->device_name, device->ifName);
return -1;
}
device->mtu = mtu;
printf("Open device [name=%s][ip=%s][ifName=%s][MTU=%d][mac=%02X:%02X:%02X:%02X:%02X:%02X]\n",
device->device_name, device_ip, device->ifName, device->mtu,
device->mac_addr[0] & 0xFF,
device->mac_addr[1] & 0xFF,
device->mac_addr[2] & 0xFF,
device->mac_addr[3] & 0xFF,
device->mac_addr[4] & 0xFF,
device->mac_addr[5] & 0xFF);
/* ****************** */
if ( 0 == strcmp("dhcp", address_mode) )
{
_snprintf(cmd, sizeof(cmd),
"netsh interface ip set address \"%s\" dhcp > nul",
device->ifName);
}
else
{
_snprintf(cmd, sizeof(cmd),
"netsh interface ip set address \"%s\" static %s %s > nul",
device->ifName, device_ip, device_mask);
}
if(system(cmd) == 0) {
device->ip_addr = inet_addr(device_ip);
device->device_mask = inet_addr(device_mask);
} else
printf("WARNING: Unable to set device %s IP address [%s]\n",
device->ifName, cmd);
/* ****************** */
/* MTU */
_snprintf(cmd, sizeof(cmd),
"netsh interface ipv4 set subinterface \"%s\" mtu=%d store=persistent > nul",
device->ifName, mtu);
if(system(cmd) != 0)
printf("WARNING: Unable to set device %s parameters MTU=%d store=persistent [%s]\n",
device->ifName, mtu, cmd);
/* ****************** */
/* metric */
PMIB_IPINTERFACE_ROW Row;
if(metric) { /* try to change only if a value has been given, otherwise leave with default or as set before */
// find & store original metric
Row = calloc(1, sizeof(MIB_IPINTERFACE_ROW));
InitializeIpInterfaceEntry(Row);
Row->InterfaceIndex = device->if_idx;
Row->Family = AF_INET;
GetIpInterfaceEntry(Row);
device->metric_original = Row->Metric;
device->metric = metric;
// set new value
Row->Metric = metric;
// store
Row->SitePrefixLength = 0; /* if not set to zero, following function call fails... */
SetIpInterfaceEntry(Row);
free(Row);
}
/* ****************** */
/* set driver media status to 'connected' (i.e. set the interface up) */
if (!DeviceIoControl (device->device_handle, TAP_IOCTL_SET_MEDIA_STATUS,
&status, sizeof (status),
&status, sizeof (status), &len, NULL))
printf("WARNING: Unable to enable TAP adapter\n");
/*
* Initialize overlapped structures
*/
device->overlap_read.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
device->overlap_write.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!device->overlap_read.hEvent || !device->overlap_write.hEvent) {
return -1;
}
return(0);
}
/* ************************************************ */
int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len)
{
DWORD read_size, last_err;
ResetEvent(tuntap->overlap_read.hEvent);
if (ReadFile(tuntap->device_handle, buf, len, &read_size, &tuntap->overlap_read)) {
//printf("tun_read(len=%d)\n", read_size);
return read_size;
}
switch (last_err = GetLastError()) {
case ERROR_IO_PENDING:
WaitForSingleObject(tuntap->overlap_read.hEvent, INFINITE);
GetOverlappedResult(tuntap->device_handle, &tuntap->overlap_read, &read_size, FALSE);
return read_size;
break;
default:
printf("GetLastError() returned %d\n", last_err);
break;
}
return -1;
}
/* ************************************************ */
int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len)
{
DWORD write_size;
//printf("tun_write(len=%d)\n", len);
ResetEvent(tuntap->overlap_write.hEvent);
if (WriteFile(tuntap->device_handle,
buf,
len,
&write_size,
&tuntap->overlap_write)) {
//printf("DONE tun_write(len=%d)\n", write_size);
return write_size;
}
switch (GetLastError()) {
case ERROR_IO_PENDING:
WaitForSingleObject(tuntap->overlap_write.hEvent, INFINITE);
GetOverlappedResult(tuntap->device_handle, &tuntap->overlap_write,
&write_size, FALSE);
return write_size;
break;
default:
break;
}
return -1;
}
/* ************************************************ */
int tuntap_open(struct tuntap_dev *device,
char *dev,
const char *address_mode, /* static or dhcp */
char *device_ip,
char *device_mask,
const char * device_mac,
int mtu,
int metric) {
return(open_wintap(device, dev, address_mode, device_ip, device_mask, device_mac, mtu, metric));
}
/* ************************************************ */
void tuntap_close(struct tuntap_dev *tuntap) {
PMIB_IPINTERFACE_ROW Row;
if(tuntap->metric) { /* only required if a value has been given (and thus stored) */
// find device entry
Row = calloc(1, sizeof(MIB_IPINTERFACE_ROW));
InitializeIpInterfaceEntry(Row);
Row->InterfaceIndex = tuntap->if_idx;
Row->Family = AF_INET;
GetIpInterfaceEntry(Row);
// restore original value
Row->Metric = tuntap->metric_original;
// store
Row->SitePrefixLength = 0; /* if not set to zero, following function call fails... */
SetIpInterfaceEntry(Row);
free(Row);
}
CloseHandle(tuntap->device_handle);
}
/* Fill out the ip_addr value from the interface. Called to pick up dynamic
* address changes. */
void tuntap_get_address(struct tuntap_dev *tuntap)
{
}
/* ************************************************ */
#if 0
int main(int argc, char* argv[]) {
struct tuntap_dev tuntap;
int i;
int mtu = 1400;
printf("Welcome to n2n\n");
initWin32();
open_wintap(&tuntap, "static", "1.2.3.20", "255.255.255.0", mtu, 0);
for(i=0; i<10; i++) {
u_char buf[MTU];
int rc;
rc = tun_read(&tuntap, buf, sizeof(buf));
buf[0]=2;
buf[1]=3;
buf[2]=4;
printf("tun_read returned %d\n", rc);
rc = tun_write(&tuntap, buf, rc);
printf("tun_write returned %d\n", rc);
}
// rc = tun_open (device->device_name, IF_MODE_TUN);
WSACleanup ();
return(0);
}
#endif

72
win32/wintap.h Normal file
View file

@ -0,0 +1,72 @@
/*
(C) 2007-22 - Luca Deri <deri@ntop.org>
*/
#ifndef _WINTAP_H_
#define _WINTAP_H_
#undef UNICODE
#undef _UNICODE
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <ws2tcpip.h>
#include <stdio.h>
#include <string.h>
//===============================================
// This file is included both by OpenVPN and
// the TAP-Win32 driver and contains definitions
// common to both.
//===============================================
//=============
// TAP IOCTLs
//=============
#define TAP_CONTROL_CODE(request,method) \
CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)
#define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED)
#define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED)
#define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED)
#define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED)
#define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED)
#define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED)
//=================
// Registry keys
//=================
#define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
#define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
#define ADAPTER_INFO_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
//======================
// Filesystem prefixes
//======================
#define USERMODEDEVICEDIR "\\\\.\\Global\\"
#define SYSDEVICEDIR "\\Device\\"
#define USERDEVICEDIR "\\DosDevices\\Global\\"
#define TAPSUFFIX ".tap"
//=========================================================
// TAP_COMPONENT_ID -- This string defines the TAP driver
// type -- different component IDs can reside in the system
// simultaneously.
//=========================================================
#define TAP_COMPONENT_ID "tap0801"
extern void initWin32();
extern void destroyWin32();
extern void win_print_available_adapters();
#endif