zt_bind()-related address bugfix, also dwr/LOGV refactor
This commit is contained in:
@@ -29,46 +29,63 @@ public class MainActivity extends AppCompatActivity {
|
||||
// Create ZeroTier socket
|
||||
int sock = zt.zt_socket(SDK.AF_INET, SDK.SOCK_STREAM, 0);
|
||||
|
||||
/*
|
||||
try {
|
||||
Thread.sleep(25000);
|
||||
}
|
||||
catch(java.lang.InterruptedException e) { }
|
||||
*/
|
||||
|
||||
int mode = 0; // client/server mode toggle
|
||||
// client/server mode toggle
|
||||
int mode = 1, err = -1;
|
||||
|
||||
// Establish outgoing connection
|
||||
if(mode==0)
|
||||
{
|
||||
int err = -1;
|
||||
while(err < 0) {
|
||||
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
catch(java.lang.InterruptedException e) { }
|
||||
err = zt.zt_connect(sock, "10.9.9.100", 7003);
|
||||
err = zt.zt_connect(sock, "10.9.9.100", 7004);
|
||||
Log.d("TEST", "err = " + err + "\n");
|
||||
}
|
||||
|
||||
// TX
|
||||
zt.zt_write(sock, "Welcome to the machine".getBytes(), 16);
|
||||
|
||||
// RX
|
||||
byte[] buffer = new byte[12];
|
||||
zt.zt_read(sock, buffer, 12);
|
||||
String bufStr = new String(buffer);
|
||||
Log.d("TEST", "response = " + bufStr);
|
||||
// Test section
|
||||
for(int i=0; i<1000; i++)
|
||||
{
|
||||
try {
|
||||
Thread.sleep(20);
|
||||
}
|
||||
catch(java.lang.InterruptedException e) { }
|
||||
|
||||
String msg = "Welcome to the machine!";
|
||||
int written = zt.zt_write(sock, msg.getBytes(), msg.length());
|
||||
Log.d("TEST", "TX[" + i + "] = " + written);
|
||||
|
||||
// RX
|
||||
byte[] buffer = new byte[1024];
|
||||
zt.zt_read(sock, buffer, buffer.length);
|
||||
String bufStr = new String(buffer);
|
||||
Log.d("TEST", "RX[" + i + "] = " + bufStr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Listen to incoming connections
|
||||
if(mode==1)
|
||||
{
|
||||
int err;
|
||||
zt.zt_bind(sock, "0.0.0.0", 8081);
|
||||
zt.zt_listen(sock,1);
|
||||
err = zt.zt_accept(sock,null); // Pass a ZTAddress to get remote host's address (if you want)
|
||||
while(err < 0) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
catch(java.lang.InterruptedException e) { }
|
||||
err = zt.zt_bind(sock, "0.0.0.0", 8080);
|
||||
Log.d("TEST", "err = " + err + "\n");
|
||||
}
|
||||
|
||||
|
||||
//zt.zt_listen(sock,1);
|
||||
//err = zt.zt_accept(sock,null); // Pass a ZTAddress to get remote host's address (if you want)
|
||||
|
||||
//Log.d("TEST", "accept_err = " + err);
|
||||
|
||||
// Example ZTAddress usage
|
||||
/*
|
||||
|
||||
56
integrations/nanomsg/pipeline.c
Normal file
56
integrations/nanomsg/pipeline.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <assert.h>
|
||||
#include <libc.h>
|
||||
#include <stdio.h>
|
||||
#include <nanomsg/nn.h>
|
||||
#include <nanomsg/pipeline.h>
|
||||
|
||||
// zerotier
|
||||
#include <SDK.h>
|
||||
#include <SDK_ServiceSetup.hpp>
|
||||
|
||||
#define NODE0 "node0"
|
||||
#define NODE1 "node1"
|
||||
|
||||
int node0 (const char *url)
|
||||
{
|
||||
int sock = nn_socket (AF_SP, NN_PULL);
|
||||
assert (sock >= 0);
|
||||
assert (nn_bind (sock, url) >= 0);
|
||||
while (1)
|
||||
{
|
||||
char *buf = NULL;
|
||||
int bytes = nn_recv (sock, &buf, NN_MSG, 0);
|
||||
assert (bytes >= 0);
|
||||
printf ("NODE0: RECEIVED \"%s\"\n", buf);
|
||||
nn_freemsg (buf);
|
||||
}
|
||||
}
|
||||
|
||||
int node1 (const char *url, const char *msg)
|
||||
{
|
||||
int sz_msg = strlen (msg) + 1; // '\0' too
|
||||
int sock = nn_socket (AF_SP, NN_PUSH);
|
||||
assert (sock >= 0);
|
||||
assert (nn_connect (sock, url) >= 0);
|
||||
printf ("NODE1: SENDING \"%s\"\n", msg);
|
||||
int bytes = nn_send (sock, msg, sz_msg, 0);
|
||||
assert (bytes == sz_msg);
|
||||
return nn_shutdown (sock, 0);
|
||||
}
|
||||
|
||||
int main (const int argc, const char **argv)
|
||||
{
|
||||
// start zerotier
|
||||
init_service(INTERCEPT_ENABLED, "/Users/Joseph/utest3");
|
||||
|
||||
if (strncmp (NODE0, argv[1], strlen (NODE0)) == 0 && argc > 1)
|
||||
return node0 (argv[2]);
|
||||
else if (strncmp (NODE1, argv[1], strlen (NODE1)) == 0 && argc > 2)
|
||||
return node1 (argv[2], argv[3]);
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "Usage: pipeline %s|%s <URL> <ARG> ...'\n",
|
||||
NODE0, NODE1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,16 @@ all: remove_only_intermediates linux_shared_lib check
|
||||
remove_only_intermediates:
|
||||
-find . -type f \( -name '*.o' -o -name '*.so' \) -delete
|
||||
|
||||
# TODO: CHECK if ANDROID/GRADLE TOOLS are installed
|
||||
# Build library for Android Unity integrations
|
||||
# Build JNI library for Android app integration
|
||||
android_jni_lib:
|
||||
cd $(INT)/android/android_jni_lib/proj; ./gradlew assembleDebug
|
||||
mkdir -p $(BUILD)/android_jni_lib
|
||||
cp docs/android_zt_sdk.md $(BUILD)/android_jni_lib/README.md
|
||||
mv -f $(INT)/android/android_jni_lib/java/libs/* $(BUILD)/android_jni_lib
|
||||
cp -R $(BUILD)/android_jni_lib/* $(INT)/android/example_app/app/src/main/jniLibs
|
||||
|
||||
# Build a dynamically-loadable library
|
||||
linux_shared_lib: $(OBJS)
|
||||
mkdir -p $(BUILD)/linux_shared_lib
|
||||
@@ -124,13 +134,13 @@ docker_check_test:
|
||||
check:
|
||||
./check.sh $(BUILD)/lwip/liblwip.so
|
||||
./check.sh $(BUILD)/linux_shared_lib/libztintercept.so
|
||||
./check.sh $(BUILD)/android_jni_lib/arm64-v8a/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/armeabi/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/armeabi-v7a/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/mips/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/mips64/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/x86/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/x86_64/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/arm64-v8a/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/armeabi/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/armeabi-v7a/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/mips/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/mips64/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/x86/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/x86_64/libZeroTierOneJNI.so
|
||||
|
||||
# Tests
|
||||
TEST_OBJDIR := $(BUILD)/tests
|
||||
|
||||
14
make-mac.mk
14
make-mac.mk
@@ -124,13 +124,13 @@ check:
|
||||
./check.sh $(BUILD)/osx_app_framework/Debug/ZeroTierSDK_OSX.framework
|
||||
./check.sh $(BUILD)/ios_app_framework/Debug-iphoneos/ZeroTierSDK_iOS.framework
|
||||
./check.sh $(BUILD)/ios_unity3d_bundle/Debug-iphoneos/ZeroTierSDK_Unity3D_iOS.bundle
|
||||
./check.sh $(BUILD)/android_jni_lib/arm64-v8a/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/armeabi/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/armeabi-v7a/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/mips/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/mips64/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/x86/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/x86_64/libZeroTierJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/arm64-v8a/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/armeabi/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/armeabi-v7a/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/mips/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/mips64/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/x86/libZeroTierOneJNI.so
|
||||
./check.sh $(BUILD)/android_jni_lib/x86_64/libZeroTierOneJNI.so
|
||||
|
||||
# Tests
|
||||
TEST_OBJDIR := $(BUILD)/tests
|
||||
|
||||
@@ -78,6 +78,7 @@ extern "C" {
|
||||
//char *debug_logfile = (char*)0;
|
||||
void dwr(int level, const char *fmt, ... );
|
||||
|
||||
//#if !defined(__ANDROID__)
|
||||
void dwr(int level, const char *fmt, ... )
|
||||
{
|
||||
#if defined(SDK_DEBUG)
|
||||
@@ -102,7 +103,6 @@ void dwr(int level, const char *fmt, ... )
|
||||
if(!debug_logfile) { // Try to get logfile from env
|
||||
debug_logfile = getenv("ZT_SDK_LOGFILE");
|
||||
}
|
||||
|
||||
if(debug_logfile) {
|
||||
FILE *file = fopen(debug_logfile,"a");
|
||||
fprintf(file, "%s [tid=%7d] ", timestring, tid);
|
||||
@@ -110,7 +110,6 @@ void dwr(int level, const char *fmt, ... )
|
||||
fclose(file);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
#endif
|
||||
va_start(ap, fmt);
|
||||
fprintf(stderr, "%s [tid=%7d] ", timestring, tid);
|
||||
@@ -118,7 +117,7 @@ void dwr(int level, const char *fmt, ... )
|
||||
|
||||
// Outputs to Android debug console
|
||||
#if defined(__ANDROID__)
|
||||
LOGV(fmt, ap);
|
||||
__android_log_vprint(ANDROID_LOG_VERBOSE, "ZT-JNI", fmt, ap);
|
||||
#endif
|
||||
|
||||
fflush(stderr);
|
||||
@@ -126,6 +125,7 @@ void dwr(int level, const char *fmt, ... )
|
||||
va_end(ap);
|
||||
#endif // _SDK_DEBUG
|
||||
}
|
||||
//#endif
|
||||
|
||||
#endif //
|
||||
#endif //
|
||||
@@ -148,9 +148,8 @@ NetconEthernetTap::NetconEthernetTap(
|
||||
|
||||
_unixListenSocket = _phy.unixListen(sockPath,(void *)this);
|
||||
dwr(MSG_DEBUG, " NetconEthernetTap initialized on: %s\n", sockPath);
|
||||
LOGV(" NetconEthernetTap initialized on: %s\n", sockPath);
|
||||
if (!_unixListenSocket)
|
||||
LOGV("unable to bind to: %s\n", sockPath);
|
||||
dwr(MSG_ERROR, "unable to bind to: %s\n", sockPath);
|
||||
_thread = Thread::start(this);
|
||||
}
|
||||
|
||||
@@ -176,6 +175,7 @@ bool NetconEthernetTap::enabled() const
|
||||
|
||||
bool NetconEthernetTap::addIp(const InetAddress &ip)
|
||||
{
|
||||
dwr(MSG_DEBUG, "addIp(): ZT address = %s", ip.toString().c_str());
|
||||
Mutex::Lock _l(_ips_m);
|
||||
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
|
||||
_ips.push_back(ip);
|
||||
@@ -331,7 +331,7 @@ void NetconEthernetTap::threadMain()
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
unsigned char tmpbuf[BUF_SZ];
|
||||
|
||||
int n = read(fd,&tmpbuf,BUF_SZ);
|
||||
ssize_t n = read(fd,&tmpbuf,BUF_SZ);
|
||||
if(_Connections[i]->TCP_pcb->state == SYN_SENT) {
|
||||
dwr(MSG_DEBUG_EXTRA," tap_thread(): (sock=%p) state = SYN_SENT, should finish or be removed soon\n",
|
||||
(void*)&(_Connections[i]->sock));
|
||||
@@ -484,12 +484,12 @@ void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_
|
||||
processReceivedData(sock,uptr,lwip_invoked);
|
||||
}
|
||||
|
||||
void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
|
||||
void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len)
|
||||
{
|
||||
dwr(MSG_DEBUG, "phyOnUnixData(%p), len = %d\n", (void*)&sock, len);
|
||||
uint64_t CANARY_num;
|
||||
pid_t pid, tid;
|
||||
int wlen = len;
|
||||
ssize_t wlen = len;
|
||||
char cmd, timestamp[20], CANARY[CANARY_SZ], padding[] = {PADDING};
|
||||
void *payload;
|
||||
unsigned char *buf = (unsigned char*)data;
|
||||
@@ -936,9 +936,14 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
|
||||
struct sockaddr_in *rawAddr = (struct sockaddr_in *) &bind_rpc->addr;
|
||||
int err, port = lwipstack->__lwip_ntohs(rawAddr->sin_port);
|
||||
ip_addr_t connAddr;
|
||||
if(!_ips.size()) {
|
||||
// We haven't been given an address yet. Binding at this stage is premature
|
||||
dwr(MSG_ERROR, " handleBind(): ZT address hasn't been provided. Cannot bind yet.");
|
||||
sendReturnValue(rpcSock, -1, ENOMEM);
|
||||
return;
|
||||
}
|
||||
connAddr.addr = *((u32_t *)_ips[0].rawIpData());
|
||||
Connection *conn = getConnection(sock);
|
||||
|
||||
dwr(MSG_DEBUG," handleBind(sock=%p,fd=%d,port=%d)\n", (void*)&sock, bind_rpc->sockfd, port);
|
||||
if(conn) {
|
||||
if(conn->type == SOCK_DGRAM) {
|
||||
|
||||
@@ -434,7 +434,7 @@ namespace ZeroTier {
|
||||
/*
|
||||
* Notifies us that there is data to be read from an application's socket
|
||||
*/
|
||||
void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
|
||||
void phyOnUnixData(PhySocket *sock,void **uptr,void *data,ssize_t len);
|
||||
|
||||
/*
|
||||
* Notifies us that we can write to an application's socket
|
||||
|
||||
@@ -60,16 +60,13 @@
|
||||
#define MAX_ADDR_LEN 32
|
||||
#define PORT_LEN 2
|
||||
|
||||
void dwr(int level, const char *fmt, ... );
|
||||
|
||||
namespace ZeroTier
|
||||
{
|
||||
void NetconEthernetTap::StartProxy(const char *sockpath, const char *homepath, uint64_t nwid)
|
||||
{
|
||||
#if defined (__ANDROID__)
|
||||
LOGV("StartProxy()\n");
|
||||
#else
|
||||
printf("StartProxy()\n");
|
||||
#endif
|
||||
|
||||
dwr(MSG_ERROR, "StartProxy()\n");
|
||||
// Look for a port file for this network's proxy server instance
|
||||
char portFile[4096];
|
||||
Utils::snprintf(portFile,sizeof(portFile),"%s/networks.d/%.16llx.port",homepath,nwid);
|
||||
@@ -86,12 +83,12 @@ namespace ZeroTier
|
||||
unsigned int randp = 0;
|
||||
Utils::getSecureRandom(&randp,sizeof(randp));
|
||||
proxyListenPort = 1000 + (randp % 1000);
|
||||
printf("Proxy(): No port specified in networks.d/%.16llx.port, randomly picking port\n", nwid);
|
||||
dwr(MSG_DEBUG, "Proxy(): No port specified in networks.d/%.16llx.port, randomly picking port\n", nwid);
|
||||
std::stringstream ss;
|
||||
ss << proxyListenPort;
|
||||
portStr = ss.str();
|
||||
if(!ZeroTier::OSUtils::writeFile(portFile, portStr)) {
|
||||
LOGV("unable to write proxy port file: %s\n", portFile);
|
||||
dwr(MSG_ERROR, "unable to write proxy port file: %s\n", portFile);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +99,7 @@ namespace ZeroTier
|
||||
in4.sin_port = Utils::hton((uint16_t)proxyListenPort);
|
||||
proxyListenPhySocket = _phy.tcpListen((const struct sockaddr*)&in4,(void *)this);
|
||||
sockstate = SOCKS_OPEN;
|
||||
printf("SOCKS5 proxy server address for <%.16llx> is: <0.0.0.0:%d> (sock=%p)\n", nwid, proxyListenPort, (void*)&proxyListenPhySocket);
|
||||
dwr(MSG_DEBUG, "SOCKS5 proxy server address for <%.16llx> is: <0.0.0.0:%d> (sock=%p)\n", nwid, proxyListenPort, (void*)&proxyListenPhySocket);
|
||||
}
|
||||
|
||||
void ExtractAddress(int addr_type, unsigned char *buf, struct sockaddr_in * addr)
|
||||
@@ -128,14 +125,14 @@ namespace ZeroTier
|
||||
|
||||
void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
|
||||
{
|
||||
printf("phyOnTcpData(): sock=%p, len=%lu\n", (void*)&sock, len);
|
||||
dwr(MSG_DEBUG, "phyOnTcpData(): sock=%p, len=%lu\n", (void*)&sock, len);
|
||||
unsigned char *buf;
|
||||
buf = (unsigned char *)data;
|
||||
|
||||
// Get connection for this PhySocket
|
||||
Connection *conn = getConnection(sock);
|
||||
if(!conn) {
|
||||
printf("phyOnTcpData(): Unable to locate Connection for sock=%p\n", (void*)&sock);
|
||||
dwr(MSG_DEBUG, "phyOnTcpData(): Unable to locate Connection for sock=%p\n", (void*)&sock);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -143,7 +140,7 @@ namespace ZeroTier
|
||||
if(conn->proxy_conn_state == SOCKS_COMPLETE)
|
||||
{
|
||||
if(len) {
|
||||
printf("len=%lu\n", len);
|
||||
dwr(MSG_DEBUG, "len=%lu\n", len);
|
||||
memcpy((&conn->txbuf)+(conn->txsz), buf, len);
|
||||
conn->txsz += len;
|
||||
handleWrite(conn);
|
||||
@@ -152,7 +149,7 @@ namespace ZeroTier
|
||||
|
||||
if(conn->proxy_conn_state==SOCKS_UDP)
|
||||
{
|
||||
printf("SOCKS_UDP from client\n");
|
||||
dwr(MSG_DEBUG, "SOCKS_UDP from client\n");
|
||||
// +----+------+------+----------+----------+----------+
|
||||
// |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
|
||||
// +----+------+------+----------+----------+----------+
|
||||
@@ -182,7 +179,7 @@ namespace ZeroTier
|
||||
if(firstSupportedMethod == 2) {
|
||||
supportedMethod = firstSupportedMethod;
|
||||
}
|
||||
printf(" INFO <ver=%d, meth_len=%d, supp_meth=%d>\n", version, methodsLength, supportedMethod);
|
||||
dwr(MSG_DEBUG, " INFO <ver=%d, meth_len=%d, supp_meth=%d>\n", version, methodsLength, supportedMethod);
|
||||
|
||||
// Send METHOD selection msg
|
||||
// +----+--------+
|
||||
@@ -215,7 +212,7 @@ namespace ZeroTier
|
||||
int cmd = buf[IDX_COMMAND];
|
||||
int addr_type = buf[IDX_ATYP];
|
||||
|
||||
printf("SOCKS REQUEST = <ver=%d, cmd=%d, typ=%d>\n", version, cmd, addr_type);
|
||||
dwr(MSG_DEBUG, "SOCKS REQUEST = <ver=%d, cmd=%d, typ=%d>\n", version, cmd, addr_type);
|
||||
|
||||
// CONNECT request
|
||||
if(cmd == 1) {
|
||||
@@ -227,12 +224,12 @@ namespace ZeroTier
|
||||
memcpy(&raw_addr, &buf[4], 4);
|
||||
char newaddr[16];
|
||||
inet_ntop(AF_INET, &raw_addr, (char*)newaddr, INET_ADDRSTRLEN);
|
||||
printf("new addr = %s\n", newaddr);
|
||||
dwr(MSG_DEBUG, "new addr = %s\n", newaddr);
|
||||
|
||||
int rawport, port;
|
||||
memcpy(&rawport, &buf[5], 2);
|
||||
port = Utils::ntoh(rawport);
|
||||
printf("new port = %d\n", port);
|
||||
dwr(MSG_DEBUG, "new port = %d\n", port);
|
||||
|
||||
// Assemble new address
|
||||
struct sockaddr_in addr;
|
||||
@@ -258,7 +255,7 @@ namespace ZeroTier
|
||||
ExtractAddress(addr_type,buf,&addr);
|
||||
PhySocket * new_sock = handleSocketProxy(sock, SOCK_STREAM);
|
||||
if(!new_sock)
|
||||
printf("Error while creating proxied-socket\n");
|
||||
dwr(MSG_ERROR, "Error while creating proxied-socket\n");
|
||||
handleConnectProxy(sock, &addr);
|
||||
|
||||
// Convert connection err code into SOCKS-err-code
|
||||
@@ -280,7 +277,7 @@ namespace ZeroTier
|
||||
// | 1 | 1 | X'00' | 1 | Variable | 2 |
|
||||
// +----+-----+-------+------+----------+----------+
|
||||
|
||||
printf("REPLY = %d\n", addr.sin_port);
|
||||
dwr(MSG_DEBUG, "REPLY = %d\n", addr.sin_port);
|
||||
char reply[len]; // TODO: determine proper length
|
||||
int addr_len = domain_len;
|
||||
memset(reply, 0, len); // Create reply buffer at least as big as incoming SOCKS request data
|
||||
@@ -302,7 +299,7 @@ namespace ZeroTier
|
||||
// BIND Request
|
||||
if(cmd == 2)
|
||||
{
|
||||
printf("BIND request\n");
|
||||
dwr(MSG_DEBUG, "BIND request\n");
|
||||
//char raw_addr[15];
|
||||
//int bind_port;
|
||||
}
|
||||
@@ -311,7 +308,7 @@ namespace ZeroTier
|
||||
if(cmd == 3)
|
||||
{
|
||||
// PORT supplied should be port assigned by server in previous msg
|
||||
printf("UDP association request\n");
|
||||
dwr(MSG_DEBUG, "UDP association request\n");
|
||||
|
||||
// SOCKS_CONNECT (Cont.)
|
||||
// +----+-----+-------+------+----------+----------+
|
||||
@@ -330,17 +327,17 @@ namespace ZeroTier
|
||||
memcpy(raw_addr, &buf[5], domain_len);
|
||||
|
||||
std::string ip, port, addrstr(raw_addr);
|
||||
int del = addrstr.find(":");
|
||||
ssize_t del = addrstr.find(":");
|
||||
ip = addrstr.substr(0, del);
|
||||
port = addrstr.substr(del+1, domain_len);
|
||||
|
||||
// Create new lwIP PCB
|
||||
PhySocket * new_sock = handleSocketProxy(sock, SOCK_DGRAM);
|
||||
|
||||
printf("new_sock = %p\n", (void*)&sock);
|
||||
printf("new_sock = %p\n", (void*)&new_sock);
|
||||
dwr(MSG_DEBUG, "new_sock = %p\n", (void*)&sock);
|
||||
dwr(MSG_DEBUG, "new_sock = %p\n", (void*)&new_sock);
|
||||
if(!new_sock)
|
||||
printf("Error while creating proxied-socket\n");
|
||||
dwr(MSG_ERROR, "Error while creating proxied-socket\n");
|
||||
|
||||
// Form address
|
||||
struct sockaddr_in addr;
|
||||
@@ -363,7 +360,7 @@ namespace ZeroTier
|
||||
|
||||
void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from)
|
||||
{
|
||||
printf("phyOnTcpAccept(): sock=%p\n", (void*)&sockN);
|
||||
dwr(MSG_DEBUG, "phyOnTcpAccept(): sock=%p\n", (void*)&sockN);
|
||||
Connection *newConn = new Connection();
|
||||
newConn->sock = sockN;
|
||||
_phy.setNotifyWritable(sockN, false);
|
||||
@@ -372,17 +369,17 @@ namespace ZeroTier
|
||||
|
||||
void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
|
||||
{
|
||||
printf("phyOnTcpConnect(): sock=%p\n", (void*)&sock);
|
||||
dwr(MSG_DEBUG, "phyOnTcpConnect(): sock=%p\n", (void*)&sock);
|
||||
}
|
||||
|
||||
// Unused -- no UDP or TCP from this thread/Phy<>
|
||||
void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *local_address, const struct sockaddr *from,void *data,unsigned long len)
|
||||
{
|
||||
printf("phyOnDatagram(): len = %lu\n", len);
|
||||
dwr(MSG_DEBUG, "phyOnDatagram(): len = %lu\n", len);
|
||||
if(len) {
|
||||
Connection *conn = getConnection(sock);
|
||||
if(!conn){
|
||||
printf("unable to locate Connection: sock=%p\n", (void*)sock);
|
||||
dwr(MSG_ERROR, "unable to locate Connection: sock=%p\n", (void*)sock);
|
||||
return;
|
||||
}
|
||||
unsigned char *buf = (unsigned char*)data;
|
||||
@@ -394,20 +391,20 @@ namespace ZeroTier
|
||||
|
||||
void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr)
|
||||
{
|
||||
printf("phyOnTcpClose(): sock=%p\n", (void*)&sock);
|
||||
dwr(MSG_DEBUG, "phyOnTcpClose(): sock=%p\n", (void*)&sock);
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
closeConnection(sock);
|
||||
}
|
||||
|
||||
void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr, bool lwip_invoked)
|
||||
{
|
||||
printf(" phyOnTcpWritable(): sock=%p\n", (void*)&sock);
|
||||
dwr(MSG_DEBUG, " phyOnTcpWritable(): sock=%p\n", (void*)&sock);
|
||||
processReceivedData(sock,uptr,lwip_invoked);
|
||||
}
|
||||
|
||||
// RX data on stream socks and send back over client sock's underlying fd
|
||||
void NetconEthernetTap::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable)
|
||||
{
|
||||
printf("phyOnFileDescriptorActivity(): sock=%p\n", (void*&)sock);
|
||||
dwr(MSG_DEBUG, "phyOnFileDescriptorActivity(): sock=%p\n", (void*&)sock);
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,8 @@
|
||||
int (*realsocket)(SOCKET_SIG);
|
||||
int (*realconnect)(CONNECT_SIG);
|
||||
|
||||
void dwr(int level, const char *fmt, ... );
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -126,7 +128,7 @@ int load_symbols_rpc()
|
||||
int rpc_join(char * sockname)
|
||||
{
|
||||
if(sockname == NULL) {
|
||||
printf("Warning, rpc netpath is NULL\n");
|
||||
dwr(MSG_ERROR, "Warning, rpc netpath is NULL\n");
|
||||
}
|
||||
if(!load_symbols_rpc())
|
||||
return -1;
|
||||
@@ -142,7 +144,7 @@ int rpc_join(char * sockname)
|
||||
#else
|
||||
if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
|
||||
#endif
|
||||
LOGV(stderr, "Error while creating RPC socket\n");
|
||||
dwr(MSG_ERROR, "Error while creating RPC socket\n");
|
||||
return -1;
|
||||
}
|
||||
while((conn_err != 0) && (attempts < SERVICE_CONNECT_ATTEMPTS)){
|
||||
@@ -151,7 +153,7 @@ int rpc_join(char * sockname)
|
||||
#else
|
||||
if((conn_err = connect(sock, (struct sockaddr*)&addr, sizeof(addr))) != 0) {
|
||||
#endif
|
||||
LOGV("Error while connecting to RPC socket. Re-attempting...\n");
|
||||
dwr(MSG_ERROR, "Error while connecting to RPC socket. Re-attempting...\n");
|
||||
sleep(1);
|
||||
}
|
||||
else
|
||||
@@ -177,7 +179,7 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
|
||||
// Generate token
|
||||
int fdrand = open("/dev/urandom", O_RDONLY);
|
||||
if(read(fdrand, &CANARY, CANARY_SZ) < 0) {
|
||||
fprintf(stderr,"unable to read from /dev/urandom for RPC canary data\n");
|
||||
dwr(MSG_ERROR, "unable to read from /dev/urandom for RPC canary data\n");
|
||||
return -1;
|
||||
}
|
||||
memcpy(&canary_num, CANARY, CANARY_SZ);
|
||||
@@ -213,18 +215,18 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
|
||||
// Write RPC
|
||||
long n_write = write(rpc_sock, &metabuf, BUF_SZ);
|
||||
if(n_write < 0) {
|
||||
fprintf(stderr, "Error writing command to service (CMD = %d)\n", cmdbuf[CMD_ID_IDX]);
|
||||
dwr(MSG_ERROR, "Error writing command to service (CMD = %d)\n", cmdbuf[CMD_ID_IDX]);
|
||||
errno = 0;
|
||||
}
|
||||
// Write token to corresponding data stream
|
||||
if(read(rpc_sock, &c, 1) < 0) {
|
||||
fprintf(stderr, "unable to read RPC ACK byte from service.\n");
|
||||
dwr(MSG_ERROR, "unable to read RPC ACK byte from service.\n");
|
||||
return -1;
|
||||
}
|
||||
if(c == 'z' && n_write > 0 && forfd > -1){
|
||||
if(send(forfd, &CANARY, CANARY_SZ+PADDING_SZ, 0) < 0) {
|
||||
perror("send: \n");
|
||||
fprintf(stderr,"unable to write canary to stream (fd=%d)\n", forfd);
|
||||
dwr(MSG_ERROR, "unable to write canary to stream (fd=%d)\n", forfd);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -321,11 +323,11 @@ ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
|
||||
cmsg = CMSG_FIRSTHDR(&msg);
|
||||
if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
|
||||
if (cmsg->cmsg_level != SOL_SOCKET) {
|
||||
fprintf (stderr, "invalid cmsg_level %d\n",cmsg->cmsg_level);
|
||||
dwr(MSG_ERROR, "invalid cmsg_level %d\n",cmsg->cmsg_level);
|
||||
return -1;
|
||||
}
|
||||
if (cmsg->cmsg_type != SCM_RIGHTS) {
|
||||
fprintf (stderr, "invalid cmsg_type %d\n",cmsg->cmsg_type);
|
||||
dwr(MSG_ERROR, "invalid cmsg_type %d\n",cmsg->cmsg_type);
|
||||
return -1;
|
||||
}
|
||||
*fd = *((int *) CMSG_DATA(cmsg));
|
||||
@@ -334,7 +336,7 @@ ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
|
||||
} else {
|
||||
size = read (sock, buf, bufsize);
|
||||
if (size < 0) {
|
||||
fprintf(stderr, "sock_fd_read(): read: Error\n");
|
||||
dwr(MSG_ERROR, "sock_fd_read(): read: Error\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
void zt_init_rpc(const char * path, const char * nwid);
|
||||
void dwr(int level, const char *fmt, ... );
|
||||
|
||||
#if defined(__UNITY_3D__)
|
||||
// .NET Interop-friendly debug mechanism
|
||||
@@ -96,12 +97,12 @@ void zt_init_rpc(const char * path, const char * nwid);
|
||||
// Basic ZT service controls
|
||||
void zts_join_network(const char * nwid) {
|
||||
std::string confFile = zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
|
||||
LOGV("writing conf file = %s\n", confFile.c_str());
|
||||
dwr(MSG_ERROR, "writing conf file = %s\n", confFile.c_str());
|
||||
if(!ZeroTier::OSUtils::mkdir(netDir)) {
|
||||
LOGV("unable to create %s\n", netDir.c_str());
|
||||
dwr(MSG_ERROR, "unable to create %s\n", netDir.c_str());
|
||||
}
|
||||
if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) {
|
||||
LOGV("unable to write network conf file: %s\n", confFile.c_str());
|
||||
dwr(MSG_ERROR, "unable to write network conf file: %s\n", confFile.c_str());
|
||||
}
|
||||
// Provide the API with the RPC information
|
||||
zt_init_rpc(homeDir.c_str(), nwid);
|
||||
@@ -216,14 +217,14 @@ void zts_terminate() { zt1Service->terminate(); }
|
||||
localHomeDir = givenHomeDir; // Used for RPC and *can* differ from homeDir on some platforms
|
||||
#endif
|
||||
|
||||
LOGV("homeDir = %s", givenHomeDir.c_str());
|
||||
dwr(MSG_DEBUG, "homeDir = %s", givenHomeDir.c_str());
|
||||
// Where network .conf files will be stored
|
||||
netDir = homeDir + "/networks.d";
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
|
||||
// Construct path for network config and supporting service files
|
||||
if (homeDir.length()) {
|
||||
LOGV("start_service(): constructing path...\n");
|
||||
dwr(MSG_DEBUG, "start_service(): constructing path...\n");
|
||||
std::vector<std::string> hpsp(ZeroTier::Utils::split(homeDir.c_str(),ZT_PATH_SEPARATOR_S,"",""));
|
||||
std::string ptmp;
|
||||
if (homeDir[0] == ZT_PATH_SEPARATOR)
|
||||
@@ -234,7 +235,7 @@ void zts_terminate() { zt1Service->terminate(); }
|
||||
ptmp.append(*pi);
|
||||
if ((*pi != ".")&&(*pi != "..")) {
|
||||
if (!ZeroTier::OSUtils::mkdir(ptmp)) {
|
||||
LOGV("startOneService(): home path does not exist, and could not create\n");
|
||||
dwr(MSG_ERROR, "startOneService(): home path does not exist, and could not create\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -423,7 +423,6 @@ int (*realclose)(CLOSE_SIG);
|
||||
/* -1 is passed since we we're generating the new socket in this call */
|
||||
printf("api_netpath = %s\n", api_netpath);
|
||||
int err = rpc_send_command(api_netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st));
|
||||
//LOGV("socket() = %d\n", err);
|
||||
dwr(MSG_DEBUG," socket() = %d\n", err);
|
||||
return err;
|
||||
}
|
||||
@@ -437,7 +436,7 @@ int (*realclose)(CLOSE_SIG);
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) {
|
||||
struct sockaddr_in addr;
|
||||
const char *str = (*env)->GetStringUTFChars(env, addrstr, 0);
|
||||
LOGV("zt_connect(): fd = %d\naddr = %s\nport=%d", fd, str, port);
|
||||
dwr(MSG_DEBUG, "zt_connect(): fd = %d\naddr = %s\nport=%d", fd, str, port);
|
||||
addr.sin_addr.s_addr = inet_addr(str);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons( port );
|
||||
@@ -477,7 +476,7 @@ int (*realclose)(CLOSE_SIG);
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) {
|
||||
struct sockaddr_in addr;
|
||||
const char *str = (*env)->GetStringUTFChars(env, addrstr, 0);
|
||||
LOGV("zt_bind(): fd = %d\naddr = %s\nport=%d", fd, str, port);
|
||||
dwr(MSG_DEBUG, "zt_bind(): fd = %d\naddr = %s\nport=%d", fd, str, port);
|
||||
addr.sin_addr.s_addr = inet_addr(str);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons( port );
|
||||
@@ -541,7 +540,8 @@ int (*realclose)(CLOSE_SIG);
|
||||
if ((flags & SOCK_NONBLOCK))
|
||||
fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||
#endif
|
||||
return accept(sockfd, addr, addrlen);
|
||||
int len = !addr ? 0 : addrlen;
|
||||
return accept(sockfd, addr, len);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -47,14 +47,19 @@ int main(int argc , char *argv[])
|
||||
printf("connection accepted\n reading...\n");
|
||||
|
||||
// RX
|
||||
int bytes_read = recv(client_sock , client_message , 2000 , 0);
|
||||
printf("read (%d) bytes\n", bytes_read);
|
||||
for(int i=0; i<bytes_read; i++) {
|
||||
printf("%c", client_message[i]);
|
||||
}
|
||||
|
||||
// TX
|
||||
int bytes_written = write(client_sock, "Server here!", 12);
|
||||
printf("bytes_written = %d\n", bytes_written);
|
||||
int msglen = 1024;
|
||||
while(1)
|
||||
{
|
||||
int bytes_read = read(client_sock, client_message, msglen);
|
||||
printf("RX = (%d): ", bytes_read);
|
||||
for(int i=0; i<bytes_read; i++) {
|
||||
printf("%c", client_message[i]);
|
||||
}
|
||||
|
||||
// TX
|
||||
int bytes_written = write(client_sock, "Server here!", msglen);
|
||||
printf("\nTX = %d\n", bytes_written);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user