Flattened examples directory
This commit is contained in:
11
examples/cpp/cxproj_dll/README.md
Normal file
11
examples/cpp/cxproj_dll/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
## libzt in C++ via DLL import
|
||||
***
|
||||
|
||||
- Add [libzt.dll]() and [libzt.lib]() to solution as existing items.
|
||||
- Place [libzt.dll](libzt.dll) in the same directory as the executable.
|
||||
- Include `libzt.h` in application source.
|
||||
- Access functions grlobally like so: `zts_socket(...)`
|
||||
|
||||
***
|
||||
|
||||
The Windows `.dll` and `.lib` files are provided pre-built at the above links, but if you'd like build instructions you can check out [BUILDING.md](../../BUILDING.md)
|
||||
28
examples/cpp/cxproj_dll/libztHelloWorld.sln
Normal file
28
examples/cpp/cxproj_dll/libztHelloWorld.sln
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 14
|
||||
VisualStudioVersion = 14.0.25420.1
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libztHelloWorld", "libztHelloWorld\libztHelloWorld.vcxproj", "{31A73533-81AA-4885-AF93-796A96540C91}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{31A73533-81AA-4885-AF93-796A96540C91}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{31A73533-81AA-4885-AF93-796A96540C91}.Debug|x64.Build.0 = Debug|x64
|
||||
{31A73533-81AA-4885-AF93-796A96540C91}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{31A73533-81AA-4885-AF93-796A96540C91}.Debug|x86.Build.0 = Debug|Win32
|
||||
{31A73533-81AA-4885-AF93-796A96540C91}.Release|x64.ActiveCfg = Release|x64
|
||||
{31A73533-81AA-4885-AF93-796A96540C91}.Release|x64.Build.0 = Release|x64
|
||||
{31A73533-81AA-4885-AF93-796A96540C91}.Release|x86.ActiveCfg = Release|Win32
|
||||
{31A73533-81AA-4885-AF93-796A96540C91}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
40
examples/cpp/cxproj_dll/libztHelloWorld/ReadMe.txt
Normal file
40
examples/cpp/cxproj_dll/libztHelloWorld/ReadMe.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
========================================================================
|
||||
CONSOLE APPLICATION : libztHelloWorld Project Overview
|
||||
========================================================================
|
||||
|
||||
AppWizard has created this libztHelloWorld application for you.
|
||||
|
||||
This file contains a summary of what you will find in each of the files that
|
||||
make up your libztHelloWorld application.
|
||||
|
||||
|
||||
libztHelloWorld.vcxproj
|
||||
This is the main project file for VC++ projects generated using an Application Wizard.
|
||||
It contains information about the version of Visual C++ that generated the file, and
|
||||
information about the platforms, configurations, and project features selected with the
|
||||
Application Wizard.
|
||||
|
||||
libztHelloWorld.vcxproj.filters
|
||||
This is the filters file for VC++ projects generated using an Application Wizard.
|
||||
It contains information about the association between the files in your project
|
||||
and the filters. This association is used in the IDE to show grouping of files with
|
||||
similar extensions under a specific node (for e.g. ".cpp" files are associated with the
|
||||
"Source Files" filter).
|
||||
|
||||
libztHelloWorld.cpp
|
||||
This is the main application source file.
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
Other standard files:
|
||||
|
||||
StdAfx.h, StdAfx.cpp
|
||||
These files are used to build a precompiled header (PCH) file
|
||||
named libztHelloWorld.pch and a precompiled types file named StdAfx.obj.
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
Other notes:
|
||||
|
||||
AppWizard uses "TODO:" comments to indicate parts of the source code you
|
||||
should add to or customize.
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
22
examples/cpp/cxproj_dll/libztHelloWorld/libztHelloWorld.cpp
Normal file
22
examples/cpp/cxproj_dll/libztHelloWorld/libztHelloWorld.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// libztHelloWorld - Simple demo with libzt.lib and libzt.dll
|
||||
|
||||
#include "stdafx.h"
|
||||
#include <Windows.h>
|
||||
#include <stdio.h>
|
||||
#include <windows.h>
|
||||
#include <strsafe.h>
|
||||
|
||||
#include "..\..\..\..\include\libzt.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("waiting for libzt to come online...\n");
|
||||
zts_startjoin("config_path", "17d709436c2c5367");
|
||||
printf("started. now performing a socket call\n");
|
||||
int fd = zts_socket(AF_INET, SOCK_STREAM, 0);
|
||||
printf("fd=%d\n", fd);
|
||||
// zts_connect(), zts_bind(), etc...
|
||||
zts_stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
166
examples/cpp/cxproj_dll/libztHelloWorld/libztHelloWorld.vcxproj
Normal file
166
examples/cpp/cxproj_dll/libztHelloWorld/libztHelloWorld.vcxproj
Normal file
@@ -0,0 +1,166 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{31A73533-81AA-4885-AF93-796A96540C91}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>libztHelloWorld</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>C:\msys\home\ZeroTier\libzt;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="ReadMe.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\..\..\..\..\..\msys\home\ZeroTier\libzt\src\dllmain.h" />
|
||||
<ClInclude Include="stdafx.h" />
|
||||
<ClInclude Include="targetver.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="libztHelloWorld.cpp" />
|
||||
<ClCompile Include="stdafx.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\..\..\build\mingw32_nt-6.2\libzt.dll" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Library Include="..\..\..\..\build\mingw32_nt-6.2\libzt.lib" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="ReadMe.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="stdafx.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="targetver.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\..\..\..\..\msys\home\ZeroTier\libzt\src\dllmain.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="stdafx.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="libztHelloWorld.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\..\..\build\mingw32_nt-6.2\libzt.dll" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Library Include="..\..\..\..\build\mingw32_nt-6.2\libzt.lib" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
8
examples/cpp/cxproj_dll/libztHelloWorld/stdafx.cpp
Normal file
8
examples/cpp/cxproj_dll/libztHelloWorld/stdafx.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
// stdafx.cpp : source file that includes just the standard includes
|
||||
// libztHelloWorld.pch will be the pre-compiled header
|
||||
// stdafx.obj will contain the pre-compiled type information
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
// TODO: reference any additional headers you need in STDAFX.H
|
||||
// and not in this file
|
||||
15
examples/cpp/cxproj_dll/libztHelloWorld/stdafx.h
Normal file
15
examples/cpp/cxproj_dll/libztHelloWorld/stdafx.h
Normal file
@@ -0,0 +1,15 @@
|
||||
// stdafx.h : include file for standard system include files,
|
||||
// or project specific include files that are used frequently, but
|
||||
// are changed infrequently
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "targetver.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <tchar.h>
|
||||
|
||||
|
||||
|
||||
// TODO: reference additional headers your program requires here
|
||||
8
examples/cpp/cxproj_dll/libztHelloWorld/targetver.h
Normal file
8
examples/cpp/cxproj_dll/libztHelloWorld/targetver.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
// Including SDKDDKVer.h defines the highest available Windows platform.
|
||||
|
||||
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
|
||||
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
|
||||
|
||||
#include <SDKDDKVer.h>
|
||||
15
examples/cpp/dummy.cpp
Normal file
15
examples/cpp/dummy.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Starting ZT service");
|
||||
zts_startjoin("my_config_path",0x0000000000000000);
|
||||
|
||||
printf("Dummy. Going into infinite loop. Ping me or something\n");
|
||||
while(1) {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
96
examples/cpp/ipv4simple/ipv4client.cpp
Normal file
96
examples/cpp/ipv4simple/ipv4client.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
char *msg = (char*)"welcome to the machine";
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
std::string remote_addr = argv[3];
|
||||
int remote_port = atoi(argv[4]);
|
||||
int r=0, w=0, err=0, sockfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in in4;
|
||||
in4.sin_port = htons(remote_port);
|
||||
in4.sin_addr.s_addr = inet_addr(remote_addr.c_str());
|
||||
in4.sin_family = AF_INET;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = zts_connect(sockfd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) {
|
||||
DEBUG_ERROR("error connecting to remote host (%d)", err);
|
||||
}
|
||||
|
||||
DEBUG_TEST("sending to server...");
|
||||
w = zts_write(sockfd, msg, strlen(msg));
|
||||
|
||||
DEBUG_TEST("reading from server...");
|
||||
r = zts_read(sockfd, rbuf, strlen(msg));
|
||||
|
||||
DEBUG_TEST("Sent : %s", msg);
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
|
||||
err = zts_close(sockfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
109
examples/cpp/ipv4simple/ipv4client_udp.cpp
Normal file
109
examples/cpp/ipv4simple/ipv4client_udp.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
char *msg = (char*)"welcome to the machine";
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
std::string remote_addr = argv[3];
|
||||
int remote_port = atoi(argv[4]);
|
||||
int r=0, w=0, err=0, sockfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in in4;
|
||||
in4.sin_port = htons(remote_port);
|
||||
in4.sin_addr.s_addr = INADDR_ANY;
|
||||
in4.sin_family = AF_INET;
|
||||
|
||||
struct sockaddr_in remote4;
|
||||
remote4.sin_port = htons(remote_port);
|
||||
remote4.sin_addr.s_addr = inet_addr(remote_addr.c_str());
|
||||
remote4.sin_family = AF_INET;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) {
|
||||
DEBUG_ERROR("error binding to interface (%d)", err);
|
||||
}
|
||||
int flags = 0;
|
||||
int len = strlen(msg);
|
||||
|
||||
while(true) {
|
||||
sleep(1);
|
||||
if ((err = zts_sendto(sockfd, msg, len, flags, (const struct sockaddr *)&remote4, sizeof(remote4))) < 0) {
|
||||
DEBUG_ERROR("error sending message to remote host (%d)", err);
|
||||
}
|
||||
DEBUG_TEST("sent=%d", err);
|
||||
}
|
||||
/*
|
||||
DEBUG_TEST("reading from server...");
|
||||
r = zts_read(sockfd, rbuf, strlen(msg));
|
||||
|
||||
DEBUG_TEST("Sent : %s", msg);
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
*/
|
||||
err = zts_close(sockfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
105
examples/cpp/ipv4simple/ipv4server.cpp
Normal file
105
examples/cpp/ipv4simple/ipv4server.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include "libzt.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 4) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server [config_file_path] [nwid] [bind_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
int bind_port = atoi(argv[3]);
|
||||
int w=0, r=0, err=0, sockfd, accfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in in4, acc_in4;
|
||||
in4.sin_port = htons(bind_port);
|
||||
in4.sin_addr.s_addr = INADDR_ANY;
|
||||
in4.sin_family = AF_INET;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) {
|
||||
DEBUG_ERROR("error binding to interface (%d)", err);
|
||||
}
|
||||
|
||||
if ((err = zts_listen(sockfd, 100)) < 0) {
|
||||
DEBUG_ERROR("error placing socket in LISTENING state (%d)", err);
|
||||
}
|
||||
|
||||
socklen_t client_addrlen = sizeof(sockaddr_in);
|
||||
if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in4, &client_addrlen)) < 0) {
|
||||
DEBUG_ERROR("error accepting connection (%d)", err);
|
||||
}
|
||||
|
||||
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
|
||||
zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen);
|
||||
DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port));
|
||||
|
||||
DEBUG_TEST("reading from client...");
|
||||
r = zts_read(accfd, rbuf, sizeof rbuf);
|
||||
|
||||
DEBUG_TEST("sending to client...");
|
||||
w = zts_write(accfd, rbuf, strlen(rbuf));
|
||||
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
|
||||
err = zts_close(sockfd);
|
||||
err = zts_close(accfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
107
examples/cpp/ipv4simple/ipv4server_udp.cpp
Normal file
107
examples/cpp/ipv4simple/ipv4server_udp.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include "libzt.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 4) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server [config_file_path] [nwid] [bind_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
int bind_port = atoi(argv[3]);
|
||||
int w=0, r=0, err=0, sockfd, accfd, flags = 0;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in in4, acc_in4;
|
||||
in4.sin_port = htons(bind_port);
|
||||
in4.sin_addr.s_addr = INADDR_ANY;
|
||||
in4.sin_family = AF_INET;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) {
|
||||
DEBUG_ERROR("error binding to interface (%d)", err);
|
||||
}
|
||||
|
||||
/*
|
||||
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
|
||||
zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen);
|
||||
DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port));
|
||||
*/
|
||||
|
||||
DEBUG_TEST("reading from client...");
|
||||
socklen_t addrlen = sizeof(acc_in4);
|
||||
memset(&acc_in4, 0, sizeof acc_in4);
|
||||
|
||||
while(true) {
|
||||
memset(&rbuf, 0, sizeof rbuf);
|
||||
r = zts_recvfrom(accfd, rbuf, sizeof(rbuf), flags, (struct sockaddr *)&acc_in4, &addrlen);
|
||||
if (r >= 0) {
|
||||
char *ip = inet_ntoa(acc_in4.sin_addr);
|
||||
DEBUG_TEST("Received : r=%d, %s -- from: %s : %d", r, rbuf, ip, ntohs(acc_in4.sin_port));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
DEBUG_TEST("sending to client...");
|
||||
w = zts_write(accfd, rbuf, strlen(rbuf));
|
||||
|
||||
*/
|
||||
|
||||
err = zts_close(sockfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
114
examples/cpp/ipv6adhoc/ipv6adhocclient.cpp
Normal file
114
examples/cpp/ipv6adhoc/ipv6adhocclient.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
char *msg = (char*)"welcome to the machine";
|
||||
|
||||
uint64_t generate_adhoc_nwid_from_port(int port)
|
||||
{
|
||||
std::string padding;
|
||||
if(port < 10) {
|
||||
padding = "000";
|
||||
} else if(port < 100) {
|
||||
padding = "00";
|
||||
} else if(port < 1000) {
|
||||
padding = "0";
|
||||
}
|
||||
// We will join ad-hoc network ffSSSSEEEE000000
|
||||
// Where SSSS = start port
|
||||
// EEEE = end port
|
||||
padding = padding + std::to_string(port); // SSSS
|
||||
std::string nwidstr = "ff" + padding + padding + "000000"; // ff + SSSS + EEEE + 000000
|
||||
return strtoull(nwidstr.c_str(), NULL, 16);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
std::string remote_addr = argv[3];
|
||||
int remote_port = atoi(argv[4]);
|
||||
int r=0, w=0, err=0, sockfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in6 in6;
|
||||
in6.sin6_port = htons(remote_port);
|
||||
inet_pton(AF_INET6, remote_addr.c_str(), &(in6.sin6_addr));
|
||||
in6.sin6_family = AF_INET6;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = generate_adhoc_nwid_from_port(remote_port);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) {
|
||||
DEBUG_ERROR("error connecting to remote host (%d)", err);
|
||||
}
|
||||
|
||||
DEBUG_TEST("sending to server...");
|
||||
w = zts_write(sockfd, msg, strlen(msg));
|
||||
|
||||
DEBUG_TEST("reading from server...");
|
||||
r = zts_read(sockfd, rbuf, strlen(msg));
|
||||
|
||||
DEBUG_TEST("Sent : %s", msg);
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
|
||||
err = zts_close(sockfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
127
examples/cpp/ipv6adhoc/ipv6adhocserver.cpp
Normal file
127
examples/cpp/ipv6adhoc/ipv6adhocserver.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
uint64_t generate_adhoc_nwid_from_port(int port)
|
||||
{
|
||||
std::string padding;
|
||||
if(port < 10) {
|
||||
padding = "000";
|
||||
} else if(port < 100) {
|
||||
padding = "00";
|
||||
} else if(port < 1000) {
|
||||
padding = "0";
|
||||
}
|
||||
// We will join ad-hoc network ffSSSSEEEE000000
|
||||
// Where SSSS = start port
|
||||
// EEEE = end port
|
||||
padding = padding + std::to_string(port); // SSSS
|
||||
std::string nwidstr = "ff" + padding + padding + "000000"; // ff + SSSS + EEEE + 000000
|
||||
return strtoull(nwidstr.c_str(), NULL, 16);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 4) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server [config_file_path] [bind_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
int bind_port = atoi(argv[2]);
|
||||
uint64_t nwid = generate_adhoc_nwid_from_port(bind_port);
|
||||
int w=0, r=0, err=0, sockfd, accfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in6 in6, acc_in6;
|
||||
in6.sin6_port = htons(bind_port);
|
||||
in6.sin6_family = AF_INET6;
|
||||
in6.sin6_addr = in6addr_any;
|
||||
|
||||
fprintf(stderr, "nwid=%llx\n", (unsigned long long)nwid);
|
||||
exit(-1);
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
nwid = generate_adhoc_nwid_from_port(bind_port);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) {
|
||||
DEBUG_ERROR("error binding to interface (%d)", err);
|
||||
}
|
||||
|
||||
if ((err = zts_listen(sockfd, 100)) < 0) {
|
||||
DEBUG_ERROR("error placing socket in LISTENING state (%d)", err);
|
||||
}
|
||||
|
||||
socklen_t client_addrlen = sizeof(sockaddr_in6);
|
||||
if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) {
|
||||
DEBUG_ERROR("error accepting connection (%d)", err);
|
||||
}
|
||||
|
||||
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
|
||||
zts_getpeername(accfd, (struct sockaddr*)&acc_in6, &peer_addrlen);
|
||||
//DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in6.sin6_addr), ntohs(acc_in6.sin6_port));
|
||||
|
||||
DEBUG_TEST("reading from client...");
|
||||
r = zts_read(accfd, rbuf, sizeof rbuf);
|
||||
|
||||
DEBUG_TEST("sending to client...");
|
||||
w = zts_write(accfd, rbuf, strlen(rbuf));
|
||||
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
|
||||
err = zts_close(sockfd);
|
||||
err = zts_close(accfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
96
examples/cpp/ipv6simple/ipv6client.cpp
Normal file
96
examples/cpp/ipv6simple/ipv6client.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
char *msg = (char*)"welcome to the machine";
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
std::string remote_addr = argv[3];
|
||||
int remote_port = atoi(argv[4]);
|
||||
int r=0, w=0, err=0, sockfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in6 in6;
|
||||
in6.sin6_port = htons(remote_port);
|
||||
inet_pton(AF_INET6, remote_addr.c_str(), &(in6.sin6_addr));
|
||||
in6.sin6_family = AF_INET6;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) {
|
||||
DEBUG_ERROR("error connecting to remote host (%d)", err);
|
||||
}
|
||||
|
||||
DEBUG_TEST("sending to server...");
|
||||
w = zts_write(sockfd, msg, strlen(msg));
|
||||
|
||||
DEBUG_TEST("reading from server...");
|
||||
r = zts_read(sockfd, rbuf, strlen(msg));
|
||||
|
||||
DEBUG_TEST("Sent : %s", msg);
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
|
||||
err = zts_close(sockfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
106
examples/cpp/ipv6simple/ipv6server.cpp
Normal file
106
examples/cpp/ipv6simple/ipv6server.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 4) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server [config_file_path] [nwid] [bind_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
int bind_port = atoi(argv[3]);
|
||||
int w=0, r=0, err=0, sockfd, accfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in6 in6, acc_in6;
|
||||
in6.sin6_port = htons(bind_port);
|
||||
in6.sin6_family = AF_INET6;
|
||||
in6.sin6_addr = in6addr_any;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) {
|
||||
DEBUG_ERROR("error binding to interface (%d)", err);
|
||||
}
|
||||
|
||||
if ((err = zts_listen(sockfd, 100)) < 0) {
|
||||
DEBUG_ERROR("error placing socket in LISTENING state (%d)", err);
|
||||
}
|
||||
|
||||
socklen_t client_addrlen = sizeof(sockaddr_in6);
|
||||
if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) {
|
||||
DEBUG_ERROR("error accepting connection (%d)", err);
|
||||
}
|
||||
|
||||
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
|
||||
zts_getpeername(accfd, (struct sockaddr*)&acc_in6, &peer_addrlen);
|
||||
//DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in6.sin6_addr), ntohs(acc_in6.sin6_port));
|
||||
|
||||
DEBUG_TEST("reading from client...");
|
||||
r = zts_read(accfd, rbuf, sizeof rbuf);
|
||||
|
||||
DEBUG_TEST("sending to client...");
|
||||
w = zts_write(accfd, rbuf, strlen(rbuf));
|
||||
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
|
||||
err = zts_close(sockfd);
|
||||
err = zts_close(accfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
209
examples/cpp/sharedlib/ipv4client_shared.cpp
Normal file
209
examples/cpp/sharedlib/ipv4client_shared.cpp
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
// function pointers which will have values assigned once the dynamic library is loaded
|
||||
|
||||
int (*_zts_set_service_port)(int portno);
|
||||
int (*_zts_start)(const char *path, bool blocking);
|
||||
int (*_zts_startjoin)(const char*, uint64_t);
|
||||
void (*_zts_stop)(void);
|
||||
int (*_zts_core_running)(void);
|
||||
int (*_zts_stack_running)(void);
|
||||
int (*_zts_ready)(void);
|
||||
int (*_zts_join)(const uint64_t);
|
||||
int (*_zts_leave)(const uint64_t);
|
||||
void (*_zts_get_path)(char *homePath, const size_t len);
|
||||
uint64_t (*_zts_get_node_id)(void);
|
||||
int (*_zts_has_address)(const uint64_t);
|
||||
int (*_zts_get_num_assigned_addresses)(const uint64_t nwid);
|
||||
int (*_zts_get_address_at_index)(const uint64_t nwid, const int index, struct sockaddr_storage *addr);
|
||||
int (*_zts_get_address)(const uint64_t nwid, struct sockaddr_storage *addr, const int address_family);
|
||||
void (*_zts_get_6plane_addr)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
|
||||
void (*_zts_get_rfc4193_addr)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
|
||||
unsigned long (*_zts_get_peer_count)(void);
|
||||
int (*_zts_get_peer_address)(char *peer, const uint64_t nodeId);
|
||||
int (*_zts_socket)(int socket_family, int socket_type, int protocol);
|
||||
int (*_zts_connect)(int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int (*_zts_bind)(int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int (*_zts_listen)(int fd, int backlog);
|
||||
int (*_zts_accept)(int fd, struct sockaddr *addr, socklen_t *addrlen);
|
||||
#if defined(__linux__)
|
||||
int (*_zts_accept4)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags);
|
||||
#endif
|
||||
int (*_zts_setsockopt)(int fd, int level, int optname, const void *optval, socklen_t optlen);
|
||||
int (*_zts_getsockopt)(int fd, int level, int optname, void *optval, socklen_t *optlen);
|
||||
int (*_zts_getsockname)(int fd, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int (*_zts_getpeername)(int fd, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int (*_zts_gethostname)(char *name, size_t len);
|
||||
int (*_zts_sethostname)(const char *name, size_t len);
|
||||
struct hostent *(*_zts_gethostbyname)(const char *name);
|
||||
int (*_zts_close)(int fd);
|
||||
int(*_zts_fcntl)(int fd, int cmd, int flags);
|
||||
int (*_zts_ioctl)(int fd, unsigned long request, void *argp);
|
||||
ssize_t (*_zts_send)(int fd, const void *buf, size_t len, int flags);
|
||||
ssize_t (*_zts_sendto)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen);
|
||||
ssize_t (*_zts_sendmsg)(int fd, const struct msghdr *msg, int flags);
|
||||
ssize_t (*_zts_recv)(int fd, void *buf, size_t len, int flags);
|
||||
ssize_t (*_zts_recvfrom)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen);
|
||||
ssize_t (*_zts_recvmsg)(int fd, struct msghdr *msg,int flags);
|
||||
int (*_zts_read)(int fd, void *buf, size_t len);
|
||||
int (*_zts_write)(int fd, const void *buf, size_t len);
|
||||
int (*_zts_shutdown)(int fd, int how);
|
||||
int (*_zts_add_dns_nameserver)(struct sockaddr *addr);
|
||||
int (*_zts_del_dns_nameserver)(struct sockaddr *addr);
|
||||
|
||||
|
||||
void load_library_symbols(char *library_path)
|
||||
{
|
||||
void *libHandle = dlopen(library_path, RTLD_LAZY);
|
||||
if (libHandle == NULL) {
|
||||
DEBUG_ERROR("unable to load dynamic lib");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// Load symbols from library (call these directly)
|
||||
|
||||
_zts_set_service_port = (int(*)(int portno))dlsym(libHandle, "zts_set_service_port");
|
||||
_zts_start = (int(*)(const char *path, bool blocking))dlsym(libHandle, "zts_start");
|
||||
_zts_startjoin = (int(*)(const char*, uint64_t))dlsym(libHandle, "zts_startjoin");
|
||||
_zts_stop = (void(*)(void))dlsym(libHandle, "zts_stop");
|
||||
_zts_core_running = (int(*)(void))dlsym(libHandle, "zts_core_running");
|
||||
_zts_stack_running = (int(*)(void))dlsym(libHandle, "zts_stack_running");
|
||||
_zts_ready = (int(*)(void))dlsym(libHandle, "zts_ready");
|
||||
_zts_join = (int(*)(const uint64_t))dlsym(libHandle, "zts_join");
|
||||
_zts_leave = (int(*)(const uint64_t))dlsym(libHandle, "zts_leave");
|
||||
_zts_get_path = (void(*)(char *homePath, const size_t len))dlsym(libHandle, "zts_get_path");
|
||||
_zts_get_node_id = (uint64_t(*)(void))dlsym(libHandle, "zts_get_node_id");
|
||||
_zts_has_address = (int(*)(const uint64_t))dlsym(libHandle, "zts_has_address");
|
||||
_zts_get_num_assigned_addresses = (int(*)(const uint64_t nwid))dlsym(libHandle, "zts_get_num_assigned_addresses");
|
||||
_zts_get_address_at_index = (int(*)(const uint64_t nwid, const int index, struct sockaddr_storage *addr))dlsym(libHandle, "zts_get_address_at_index");
|
||||
_zts_get_address = (int(*)(const uint64_t nwid, struct sockaddr_storage *addr, const int address_family))dlsym(libHandle, "zts_get_address");
|
||||
_zts_get_6plane_addr = (void(*)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId))dlsym(libHandle, "zts_get_6plane_addr");
|
||||
_zts_get_rfc4193_addr = (void(*)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId))dlsym(libHandle, "zts_get_rfc4193_addr");
|
||||
_zts_get_peer_count = (unsigned long(*)(void))dlsym(libHandle, "zts_get_peer_count");
|
||||
_zts_get_peer_address = (int(*)(char *peer, const uint64_t nodeId))dlsym(libHandle, "zts_get_peer_address");
|
||||
_zts_socket = (int(*)(int socket_family, int socket_type, int protocol))dlsym(libHandle, "zts_socket");
|
||||
_zts_connect = (int(*)(int fd, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_connect");
|
||||
_zts_bind = (int(*)(int fd, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_bind");
|
||||
_zts_listen = (int(*)(int fd, int backlog))dlsym(libHandle, "zts_listen");
|
||||
_zts_accept = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_accept");
|
||||
#if defined(__linux__)
|
||||
_zts_accept4 = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags))dlsym(libHandle, "zts_accept4");
|
||||
#endif
|
||||
_zts_setsockopt = (int(*)(int fd, int level, int optname, const void *optval, socklen_t optlen))dlsym(libHandle, "zts_setsockopt");
|
||||
_zts_getsockopt = (int(*)(int fd, int level, int optname, void *optval, socklen_t *optlen))dlsym(libHandle, "zts_getsockopt");
|
||||
_zts_getsockname = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_getsockname");
|
||||
_zts_getpeername = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_getpeername");
|
||||
_zts_gethostname = (int(*)(char *name, size_t len))dlsym(libHandle, "zts_gethostname");
|
||||
_zts_sethostname = (int(*)(const char *name, size_t len))dlsym(libHandle, "zts_sethostname");
|
||||
_zts_gethostbyname = (struct hostent*(*)(const char *name))dlsym(libHandle, "zts_gethostbyname");
|
||||
_zts_close = (int(*)(int fd))dlsym(libHandle, "zts_close");
|
||||
_zts_fcntl = (int(*)(int fd, int cmd, int flags))dlsym(libHandle, "zts_fcntl");
|
||||
_zts_ioctl = (int(*)(int fd, unsigned long request, void *argp))dlsym(libHandle, "zts_ioctl");
|
||||
_zts_send = (ssize_t(*)(int fd, const void *buf, size_t len, int flags))dlsym(libHandle, "zts_send");
|
||||
_zts_sendto = (ssize_t(*)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_sendto");
|
||||
_zts_sendmsg = (ssize_t(*)(int fd, const struct msghdr *msg, int flags))dlsym(libHandle, "zts_sendmsg");
|
||||
_zts_recv = (ssize_t(*)(int fd, void *buf, size_t len, int flags))dlsym(libHandle, "zts_recv");
|
||||
_zts_recvfrom = (ssize_t(*)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_recvfrom");
|
||||
_zts_recvmsg = (ssize_t(*)(int fd, struct msghdr *msg,int flags))dlsym(libHandle, "zts_recvmsg");
|
||||
_zts_read = (int(*)(int fd, void *buf, size_t len))dlsym(libHandle, "zts_read");
|
||||
_zts_write = (int(*)(int fd, const void *buf, size_t len))dlsym(libHandle, "zts_write");
|
||||
_zts_shutdown = (int(*)(int fd, int how))dlsym(libHandle, "zts_shutdown");
|
||||
_zts_add_dns_nameserver = (int(*)(struct sockaddr *addr))dlsym(libHandle, "zts_add_dns_nameserver");
|
||||
}
|
||||
|
||||
char *msg = (char*)"welcome to the machine";
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
std::string remote_addr = argv[3];
|
||||
int remote_port = atoi(argv[4]);
|
||||
int r=0, w=0, err=0, sockfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in in4;
|
||||
in4.sin_port = htons(remote_port);
|
||||
in4.sin_addr.s_addr = inet_addr(remote_addr.c_str());
|
||||
in4.sin_family = AF_INET;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
_zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = _zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = _zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = _zts_connect(sockfd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) {
|
||||
DEBUG_ERROR("error connecting to remote host (%d)", err);
|
||||
}
|
||||
|
||||
DEBUG_TEST("sending to server...");
|
||||
w = _zts_write(sockfd, msg, strlen(msg));
|
||||
|
||||
DEBUG_TEST("reading from server...");
|
||||
r = _zts_read(sockfd, rbuf, strlen(msg));
|
||||
|
||||
DEBUG_TEST("Sent : %s", msg);
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
|
||||
err = _zts_close(sockfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
222
examples/cpp/sharedlib/ipv4server_shared.cpp
Normal file
222
examples/cpp/sharedlib/ipv4server_shared.cpp
Normal file
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* You can be released from the requirements of the license by purchasing
|
||||
* a commercial license. Buying such a license is mandatory as soon as you
|
||||
* develop commercial closed-source software that incorporates or links
|
||||
* directly against ZeroTier software without disclosing the source code
|
||||
* of your own application.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include "libzt.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
// function pointers which will have values assigned once the dynamic library is loaded
|
||||
|
||||
int (*_zts_set_service_port)(int portno);
|
||||
int (*_zts_start)(const char *path, bool blocking);
|
||||
int (*_zts_startjoin)(const char*, uint64_t);
|
||||
void (*_zts_stop)(void);
|
||||
int (*_zts_core_running)(void);
|
||||
int (*_zts_stack_running)(void);
|
||||
int (*_zts_ready)(void);
|
||||
int (*_zts_join)(const uint64_t);
|
||||
int (*_zts_leave)(const uint64_t);
|
||||
void (*_zts_get_path)(char *homePath, const size_t len);
|
||||
uint64_t (*_zts_get_node_id)(void);
|
||||
int (*_zts_has_address)(const uint64_t);
|
||||
int (*_zts_get_num_assigned_addresses)(const uint64_t nwid);
|
||||
int (*_zts_get_address_at_index)(const uint64_t nwid, const int index, struct sockaddr_storage *addr);
|
||||
int (*_zts_get_address)(const uint64_t nwid, struct sockaddr_storage *addr, const int address_family);
|
||||
void (*_zts_get_6plane_addr)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
|
||||
void (*_zts_get_rfc4193_addr)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
|
||||
unsigned long (*_zts_get_peer_count)(void);
|
||||
int (*_zts_get_peer_address)(char *peer, const uint64_t nodeId);
|
||||
int (*_zts_socket)(int socket_family, int socket_type, int protocol);
|
||||
int (*_zts_connect)(int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int (*_zts_bind)(int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int (*_zts_listen)(int fd, int backlog);
|
||||
int (*_zts_accept)(int fd, struct sockaddr *addr, socklen_t *addrlen);
|
||||
#if defined(__linux__)
|
||||
int (*_zts_accept4)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags);
|
||||
#endif
|
||||
int (*_zts_setsockopt)(int fd, int level, int optname, const void *optval, socklen_t optlen);
|
||||
int (*_zts_getsockopt)(int fd, int level, int optname, void *optval, socklen_t *optlen);
|
||||
int (*_zts_getsockname)(int fd, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int (*_zts_getpeername)(int fd, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int (*_zts_gethostname)(char *name, size_t len);
|
||||
int (*_zts_sethostname)(const char *name, size_t len);
|
||||
struct hostent *(*_zts_gethostbyname)(const char *name);
|
||||
int (*_zts_close)(int fd);
|
||||
int(*_zts_fcntl)(int fd, int cmd, int flags);
|
||||
int (*_zts_ioctl)(int fd, unsigned long request, void *argp);
|
||||
ssize_t (*_zts_send)(int fd, const void *buf, size_t len, int flags);
|
||||
ssize_t (*_zts_sendto)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen);
|
||||
ssize_t (*_zts_sendmsg)(int fd, const struct msghdr *msg, int flags);
|
||||
ssize_t (*_zts_recv)(int fd, void *buf, size_t len, int flags);
|
||||
ssize_t (*_zts_recvfrom)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen);
|
||||
ssize_t (*_zts_recvmsg)(int fd, struct msghdr *msg,int flags);
|
||||
int (*_zts_read)(int fd, void *buf, size_t len);
|
||||
int (*_zts_write)(int fd, const void *buf, size_t len);
|
||||
int (*_zts_shutdown)(int fd, int how);
|
||||
int (*_zts_add_dns_nameserver)(struct sockaddr *addr);
|
||||
int (*_zts_del_dns_nameserver)(struct sockaddr *addr);
|
||||
|
||||
|
||||
void load_library_symbols(char *library_path)
|
||||
{
|
||||
void *libHandle = dlopen(library_path, RTLD_LAZY);
|
||||
if (libHandle == NULL) {
|
||||
DEBUG_ERROR("unable to load dynamic lib");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// Load symbols from library (call these directly)
|
||||
|
||||
_zts_set_service_port = (int(*)(int portno))dlsym(libHandle, "zts_set_service_port");
|
||||
_zts_start = (int(*)(const char *path, bool blocking))dlsym(libHandle, "zts_start");
|
||||
_zts_startjoin = (int(*)(const char*, uint64_t))dlsym(libHandle, "zts_startjoin");
|
||||
_zts_stop = (void(*)(void))dlsym(libHandle, "zts_stop");
|
||||
_zts_core_running = (int(*)(void))dlsym(libHandle, "zts_core_running");
|
||||
_zts_stack_running = (int(*)(void))dlsym(libHandle, "zts_stack_running");
|
||||
_zts_ready = (int(*)(void))dlsym(libHandle, "zts_ready");
|
||||
_zts_join = (int(*)(const uint64_t))dlsym(libHandle, "zts_join");
|
||||
_zts_leave = (int(*)(const uint64_t))dlsym(libHandle, "zts_leave");
|
||||
_zts_get_path = (void(*)(char *homePath, const size_t len))dlsym(libHandle, "zts_get_path");
|
||||
_zts_get_node_id = (uint64_t(*)(void))dlsym(libHandle, "zts_get_node_id");
|
||||
_zts_has_address = (int(*)(const uint64_t))dlsym(libHandle, "zts_has_address");
|
||||
_zts_get_num_assigned_addresses = (int(*)(const uint64_t nwid))dlsym(libHandle, "zts_get_num_assigned_addresses");
|
||||
_zts_get_address_at_index = (int(*)(const uint64_t nwid, const int index, struct sockaddr_storage *addr))dlsym(libHandle, "zts_get_address_at_index");
|
||||
_zts_get_address = (int(*)(const uint64_t nwid, struct sockaddr_storage *addr, const int address_family))dlsym(libHandle, "zts_get_address");
|
||||
_zts_get_6plane_addr = (void(*)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId))dlsym(libHandle, "zts_get_6plane_addr");
|
||||
_zts_get_rfc4193_addr = (void(*)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId))dlsym(libHandle, "zts_get_rfc4193_addr");
|
||||
_zts_get_peer_count = (unsigned long(*)(void))dlsym(libHandle, "zts_get_peer_count");
|
||||
_zts_get_peer_address = (int(*)(char *peer, const uint64_t nodeId))dlsym(libHandle, "zts_get_peer_address");
|
||||
_zts_socket = (int(*)(int socket_family, int socket_type, int protocol))dlsym(libHandle, "zts_socket");
|
||||
_zts_connect = (int(*)(int fd, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_connect");
|
||||
_zts_bind = (int(*)(int fd, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_bind");
|
||||
_zts_listen = (int(*)(int fd, int backlog))dlsym(libHandle, "zts_listen");
|
||||
_zts_accept = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_accept");
|
||||
#if defined(__linux__)
|
||||
_zts_accept4 = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags))dlsym(libHandle, "zts_accept4");
|
||||
#endif
|
||||
_zts_setsockopt = (int(*)(int fd, int level, int optname, const void *optval, socklen_t optlen))dlsym(libHandle, "zts_setsockopt");
|
||||
_zts_getsockopt = (int(*)(int fd, int level, int optname, void *optval, socklen_t *optlen))dlsym(libHandle, "zts_getsockopt");
|
||||
_zts_getsockname = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_getsockname");
|
||||
_zts_getpeername = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_getpeername");
|
||||
_zts_gethostname = (int(*)(char *name, size_t len))dlsym(libHandle, "zts_gethostname");
|
||||
_zts_sethostname = (int(*)(const char *name, size_t len))dlsym(libHandle, "zts_sethostname");
|
||||
_zts_gethostbyname = (struct hostent*(*)(const char *name))dlsym(libHandle, "zts_gethostbyname");
|
||||
_zts_close = (int(*)(int fd))dlsym(libHandle, "zts_close");
|
||||
_zts_fcntl = (int(*)(int fd, int cmd, int flags))dlsym(libHandle, "zts_fcntl");
|
||||
_zts_ioctl = (int(*)(int fd, unsigned long request, void *argp))dlsym(libHandle, "zts_ioctl");
|
||||
_zts_send = (ssize_t(*)(int fd, const void *buf, size_t len, int flags))dlsym(libHandle, "zts_send");
|
||||
_zts_sendto = (ssize_t(*)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_sendto");
|
||||
_zts_sendmsg = (ssize_t(*)(int fd, const struct msghdr *msg, int flags))dlsym(libHandle, "zts_sendmsg");
|
||||
_zts_recv = (ssize_t(*)(int fd, void *buf, size_t len, int flags))dlsym(libHandle, "zts_recv");
|
||||
_zts_recvfrom = (ssize_t(*)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_recvfrom");
|
||||
_zts_recvmsg = (ssize_t(*)(int fd, struct msghdr *msg,int flags))dlsym(libHandle, "zts_recvmsg");
|
||||
_zts_read = (int(*)(int fd, void *buf, size_t len))dlsym(libHandle, "zts_read");
|
||||
_zts_write = (int(*)(int fd, const void *buf, size_t len))dlsym(libHandle, "zts_write");
|
||||
_zts_shutdown = (int(*)(int fd, int how))dlsym(libHandle, "zts_shutdown");
|
||||
_zts_add_dns_nameserver = (int(*)(struct sockaddr *addr))dlsym(libHandle, "zts_add_dns_nameserver");
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 4) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server [config_file_path] [nwid] [bind_port]\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string path = argv[1];
|
||||
std::string nwidstr = argv[2];
|
||||
int bind_port = atoi(argv[3]);
|
||||
int w=0, r=0, err=0, sockfd, accfd;
|
||||
char rbuf[32];
|
||||
memset(rbuf, 0, sizeof rbuf);
|
||||
|
||||
struct sockaddr_in in4, acc_in4;
|
||||
in4.sin_port = htons(bind_port);
|
||||
in4.sin_addr.s_addr = INADDR_ANY;
|
||||
in4.sin_family = AF_INET;
|
||||
|
||||
// --- BEGIN EXAMPLE CODE
|
||||
|
||||
char *library_path = (char*)"bin/lib/libzt.dylib";
|
||||
load_library_symbols(library_path);
|
||||
|
||||
DEBUG_TEST("Waiting for libzt to come online...\n");
|
||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||
printf("nwid=%llx\n", (unsigned long long)nwid);
|
||||
_zts_startjoin(path.c_str(), nwid);
|
||||
uint64_t nodeId = _zts_get_node_id();
|
||||
DEBUG_TEST("I am %llx", (unsigned long long)nodeId);
|
||||
|
||||
if ((sockfd = _zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("error creating ZeroTier socket");
|
||||
}
|
||||
|
||||
if ((err = _zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) {
|
||||
DEBUG_ERROR("error binding to interface (%d)", err);
|
||||
}
|
||||
|
||||
if ((err = _zts_listen(sockfd, 100)) < 0) {
|
||||
DEBUG_ERROR("error placing socket in LISTENING state (%d)", err);
|
||||
}
|
||||
|
||||
socklen_t client_addrlen = sizeof(sockaddr_in);
|
||||
if ((accfd = _zts_accept(sockfd, (struct sockaddr *)&acc_in4, &client_addrlen)) < 0) {
|
||||
DEBUG_ERROR("error accepting connection (%d)", err);
|
||||
}
|
||||
|
||||
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
|
||||
_zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen);
|
||||
DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port));
|
||||
|
||||
DEBUG_TEST("reading from client...");
|
||||
r = _zts_read(accfd, rbuf, sizeof rbuf);
|
||||
|
||||
DEBUG_TEST("sending to client...");
|
||||
w = _zts_write(accfd, rbuf, strlen(rbuf));
|
||||
|
||||
DEBUG_TEST("Received : %s", rbuf);
|
||||
|
||||
err = _zts_close(sockfd);
|
||||
err = _zts_close(accfd);
|
||||
|
||||
return err;
|
||||
}
|
||||
Reference in New Issue
Block a user