Preparation for C--, removed classes, namespaces, advanced build options are now controlled via include/libztDefs.h

This commit is contained in:
Joseph Henry
2017-11-06 13:50:20 -08:00
parent 920afa079e
commit e0d4b84dd4
65 changed files with 12208 additions and 2609 deletions

View File

@@ -24,27 +24,25 @@
* of your own application.
*/
#include <unistd.h>
#include <string.h>
#if defined(__linux__) || defined(__APPLE__)
#include <netdb.h>
#endif
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fcntl.h>
#include <vector>
#include <algorithm>
#include <map>
#include "RingBuffer.hpp"
#include "ztproxy.hpp"
#include "Utilities.h"
#include "libzt.h"
#include "RingBuffer.h"
#include "ztproxy.hpp"
namespace ZeroTier {
typedef void PhySocket;
@@ -292,7 +290,7 @@ namespace ZeroTier {
}
// Write data coming from client TCP connection to its TX buffer, later emptied into libzt by threadMain I/O loop
conn->tx_m.lock();
if ((wr = conn->TXbuf->write((const unsigned char *)data, len)) < 0) {
if ((wr = conn->TXbuf->write((const char *)data, len)) < 0) {
DEBUG_ERROR("there was an error while writing data from client to tx buffer, err=%d", wr);
}
else {
@@ -368,7 +366,7 @@ int main(int argc, char **argv)
}
std::string path = argv[1];
int proxy_listen_port = atoi(argv[2]);
std::string nwid = argv[3];
std::string nwidstr = argv[3];
std::string internal_addr = argv[4];
int internal_port = atoi(argv[5]);
std::string dns_nameserver= "";//argv[6];
@@ -376,13 +374,14 @@ int main(int argc, char **argv)
// Start ZeroTier Node
// Join Network which contains resources we need to proxy
DEBUG_INFO("waiting for libzt to come online");
zts_startjoin(path.c_str(), nwid.c_str());
uint64_t nwid = strtoll(nwidstr.c_str(),NULL,16);
zts_startjoin(path.c_str(), nwid);
ZeroTier::ZTProxy *proxy = new ZeroTier::ZTProxy(proxy_listen_port, nwid, path, internal_addr, internal_port, dns_nameserver);
ZeroTier::ZTProxy *proxy = new ZeroTier::ZTProxy(proxy_listen_port, nwidstr, path, internal_addr, internal_port, dns_nameserver);
if (proxy) {
printf("\nZTProxy started. Listening on %d\n", proxy_listen_port);
printf("Traffic will be proxied to and from %s:%d on network %s\n", internal_addr.c_str(), internal_port, nwid.c_str());
printf("Traffic will be proxied to and from %s:%d on network %s\n", internal_addr.c_str(), internal_port, nwidstr.c_str());
printf("Proxy Node config files and key stored in: %s/\n\n", path.c_str());
while(1) {
sleep(1);

View File

@@ -54,14 +54,13 @@ namespace ZeroTier {
public:
int zfd;
PhySocket *client_sock;
RingBuffer<unsigned char> *TXbuf;
RingBuffer<unsigned char> *RXbuf;
RingBuffer *TXbuf, *RXbuf;
Mutex tx_m, rx_m;
TcpConnection() {
zfd = -1;
TXbuf = new RingBuffer<unsigned char>(BUF_SZ);
RXbuf = new RingBuffer<unsigned char>(BUF_SZ);
TXbuf = new RingBuffer(BUF_SZ);
RXbuf = new RingBuffer(BUF_SZ);
}
~TcpConnection() {

View File

@@ -20,7 +20,7 @@ int main(int argc, char **argv)
exit(0);
}
std::string path = argv[1];
std::string nwid = argv[2];
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;
@@ -36,10 +36,10 @@ int main(int argc, char **argv)
// --- BEGIN
DEBUG_TEST("Waiting for libzt to come online...\n");
zts_startjoin(path.c_str(), nwid.c_str());
char device_id[11];
zts_get_id(device_id);
DEBUG_TEST("I am %s", device_id);
uint64_t nwid = strtoll(nwidstr.c_str(),NULL,16);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
DEBUG_TEST("I am %x", nodeId);
sleep(2);
// socket()

View File

@@ -18,7 +18,7 @@ int main(int argc, char **argv)
exit(0);
}
std::string path = argv[1];
std::string nwid = argv[2];
std::string nwidstr = argv[2];
int bind_port = atoi(argv[3]);
int w=0, r=0, err=0, sockfd, accfd;
char rbuf[32];
@@ -33,10 +33,10 @@ int main(int argc, char **argv)
DEBUG_TEST("Waiting for libzt to come online...\n");
zts_startjoin(path.c_str(), nwid.c_str());
char device_id[11];
zts_get_id(device_id);
DEBUG_TEST("I am %s", device_id);
uint64_t nwid = strtoll(nwidstr.c_str(),NULL,16);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
DEBUG_TEST("I am %x", nodeId);
sleep(2);
// socket()

View File

@@ -18,58 +18,46 @@ namespace ZeroTier
public static extern int zts_start(string path);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int zts_startjoin(string path, string nwid);
public static extern int zts_startjoin(string path, ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_stop();
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_join(string nwid);
public static extern void zts_join(ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_join_soft(string filepath, string nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_leave(string nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_leave_soft(string filepath, string nwid);
public static extern void zts_leave(ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_get_homepath(string homePath, int len);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int zts_get_id(string devID);
public static extern int zts_get_id(string nodeId);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int zts_running();
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int zts_has_ipv4_address(string nwid);
public static extern int zts_has_address(ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int zts_has_ipv6_address(string nwid);
public static extern int zts_has_address(ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int zts_has_address(string nwid);
public static extern void zts_get_address(ulong nwid, System.IntPtr addr, int addrlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_get_ipv4_address(string nwid, string addrstr, int addrlen);
public static extern void zts_get_6plane_addr(System.IntPtr addr addr, string nwid, string nodeId);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_get_ipv6_address(string nwid, string addrstr, int addrlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_get_6plane_addr(string addr, string nwid, string devID);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_get_rfc4193_addr(string addr, string nwid, string devID);
public static extern void zts_get_rfc4193_addr(System.IntPtr addr addr, string nwid, string nodeId);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern long zts_get_peer_count();
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int zts_get_peer_address(string peer, string devID);
public static extern int zts_get_peer_address(string peer, ulong nodeId);
[DllImport("libzt.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void zts_enable_http_control_plane();

View File

@@ -27,6 +27,7 @@
// Simple Java example for libzt using JNI
import zerotier.ZeroTier;
import java.net.*;
public class ExampleApp {
@@ -39,25 +40,28 @@ public class ExampleApp {
}
public static void main(String[] args) {
final ZeroTier libzt = new ZeroTier();
new Thread(new Runnable() {
public void run() {
System.out.println("starting libzt");
libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "1212121212121212");
//libzt.startjoin("config_path", "123456789abcdeff");
libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "17d709436c2c5367");
System.out.println("started.");
// start(path) will not block
// startjoin(path, nwid) will block
int fd = 0, err = 0;
if ((fd = libzt.socket(libzt.AF_INET, libzt.SOCK_STREAM, 0)) < 0) {
System.out.println("error creating socket");
return;
}
if ((err = libzt.bind(fd, "0.0.0.0", 3000)) < 0) {
InetSocketAddress addr = new InetSocketAddress("0.0.0.0", 3434);
if ((err = libzt.bind(fd, addr)) < 0) {
System.out.println("error binding socket to virtual interface");
return;
}
}
}
}).start();

View File

@@ -11,4 +11,11 @@ To get this example project to work, do the following:
Notes:
Upon execution, it will load the libzt dynamic library via the `loadLibrary` method and begin generating an identity.
Upon execution, it will load the libzt dynamic library via the `loadLibrary` method and begin generating an identity.
***
More resources on JNI usage:
http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html

View File

@@ -1,106 +0,0 @@
/*
* 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.
*/
package zerotier;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.regex.Pattern;
/*
The ZTAddress object is merely a convenience object for moving address information
across the JNI memory border.
*/
public class Address
{
// int -> byte array
static public byte[] toIPByteArray(long addr){
return new byte[]{(byte)addr,(byte)(addr>>>8),(byte)(addr>>>16),(byte)(addr>>>24)};
}
// byte array -> int
long toIPInt(String _addr) {
long result = 0;
for(String part: _addr.split(Pattern.quote("."))) {
result = result << 8;
result |= Integer.parseInt(part);
}
return result;
}
public int port;
public int Port() {
return port;
}
public long _rawAddr;
public String Address()
{
try {
return InetAddress.getByAddress(toIPByteArray(_rawAddr)).getHostAddress();
} catch (UnknownHostException e) {
// should never happen
return null;
}
}
public String toString() {
return Address() + ":" + Port();
}
public Address()
{
port = -1;
_rawAddr = -1;
}
public Address(String _addr, int _port)
{
_rawAddr = toIPInt(_addr);
port = _port;
}
public void ZTAddress(InetSocketAddress ins)
{
port = ins.getPort();
_rawAddr = toIPInt(ins.getAddress().getHostAddress());
}
public InetSocketAddress ToInetSocketAddress() throws IllegalArgumentException {
InetSocketAddress sock_addr = null;
try {
sock_addr = new InetSocketAddress(Address(), port);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return sock_addr;
}
}

View File

@@ -26,6 +26,8 @@
package zerotier;
import java.net.*;
public class ZeroTier {
// socket families
@@ -58,7 +60,7 @@ public class ZeroTier {
// socket API
public native int socket(int family, int type, int protocol);
public native int connect(int fd, String addr, int port);
public native int bind(int fd, String addr, int port);
public native int bind(int fd, InetSocketAddress addr);
public native int listen(int fd, int backlog);
public native int accept(int fd, Address addr);
public native int accept4(int fd, String addr, int port);