misc cleanup, adjustments to debug outputs
This commit is contained in:
@@ -93,7 +93,8 @@ int zts_get_proxy_server_address(const char * nwid, struct sockaddr_storage *add
|
||||
bool zts_proxy_is_running();
|
||||
|
||||
// ZT Service Controls
|
||||
void *zts_start_service(void *thread_id);
|
||||
void zts_start_service(const char *path);
|
||||
void *zts_start_core_service(void *thread_id);
|
||||
void zts_stop_service();
|
||||
bool zts_service_is_running();
|
||||
void zts_join_network(const char * nwid);
|
||||
|
||||
@@ -62,16 +62,16 @@ extern "C" {
|
||||
|
||||
//#if defined(SDK_DEBUG)
|
||||
#if DEBUG_LEVEL >= MSG_ERROR
|
||||
#define DEBUG_ERROR(fmt, args...) fprintf(stderr, "\nZT_ERROR: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_ERROR(fmt, args...) fprintf(stderr, "ZT_ERROR: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
|
||||
#else
|
||||
#define DEBUG_ERROR(fmt, args...)
|
||||
#endif
|
||||
#if DEBUG_LEVEL >= MSG_INFO
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "\nZT_INFO : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_INFO : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_INFO(fmt, args...) fprintf(stderr, "\nZT_INFO : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_BLANK(fmt, args...) fprintf(stderr, "\nZT_INFO : %s:%d:" fmt "\n", __FILENAME__, __LINE__, ##args)
|
||||
#define DEBUG_INFO(fmt, args...) fprintf(stderr, "ZT_INFO : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_BLANK(fmt, args...) fprintf(stderr, "ZT_INFO : %s:%d:" fmt "\n", __FILENAME__, __LINE__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_INFO(fmt, args...)
|
||||
@@ -79,18 +79,18 @@ extern "C" {
|
||||
#endif
|
||||
#if DEBUG_LEVEL >= MSG_TRANSFER
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "\nZT_TRANS : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_TRANS : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_TRANS(fmt, args...) fprintf(stderr, "\nZT_TRANS: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_TRANS(fmt, args...) fprintf(stderr, "ZT_TRANS: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_TRANS(fmt, args...)
|
||||
#endif
|
||||
#if DEBUG_LEVEL >= MSG_EXTRA
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "\nZT_EXTRA : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_EXTRA : %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_EXTRA(fmt, args...) fprintf(stderr, "\nZT_EXTRA: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_EXTRA(fmt, args...) fprintf(stderr, "ZT_EXTRA: %s:%d:%s(): " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_EXTRA(fmt, args...)
|
||||
|
||||
@@ -89,9 +89,9 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
|
||||
bufptr = buf;
|
||||
// Copy data from each pbuf, one at a time
|
||||
for(q = p; q != NULL; q = q->next) {
|
||||
memcpy(bufptr, q->payload, q->len);
|
||||
bufptr += q->len;
|
||||
totalLength += q->len;
|
||||
memcpy(bufptr, q->payload, q->len);
|
||||
bufptr += q->len;
|
||||
totalLength += q->len;
|
||||
}
|
||||
// [Send packet to network]
|
||||
// Split ethernet header and feed into handler
|
||||
@@ -142,9 +142,9 @@ NetconEthernetTap::NetconEthernetTap(
|
||||
lwipstack->__lwip_init();
|
||||
|
||||
_unixListenSocket = _phy.unixListen(sockPath,(void *)this);
|
||||
DEBUG_INFO(" NetconEthernetTap initialized on: %s\n", sockPath);
|
||||
DEBUG_INFO("tap initialized on: path=%s", sockPath);
|
||||
if (!_unixListenSocket)
|
||||
DEBUG_ERROR("unable to bind to: %s\n", sockPath);
|
||||
DEBUG_ERROR("unable to bind to: path=%s", sockPath);
|
||||
_thread = Thread::start(this);
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ bool NetconEthernetTap::enabled() const
|
||||
|
||||
bool NetconEthernetTap::addIp(const InetAddress &ip)
|
||||
{
|
||||
DEBUG_INFO("addIp(): ZT address = %s", ip.toString().c_str());
|
||||
DEBUG_INFO("local_addr=%s", ip.toString().c_str());
|
||||
Mutex::Lock _l(_ips_m);
|
||||
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
|
||||
_ips.push_back(ip);
|
||||
@@ -222,7 +222,7 @@ std::vector<InetAddress> NetconEthernetTap::ips() const
|
||||
|
||||
void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
|
||||
{
|
||||
DEBUG_EXTRA("RX packet: len = %d\n", len);
|
||||
DEBUG_EXTRA("RX packet: len=%d", len);
|
||||
struct pbuf *p,*q;
|
||||
if (!_enabled)
|
||||
return;
|
||||
@@ -240,7 +240,7 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
|
||||
// First pbuf gets ethernet header at start
|
||||
q = p;
|
||||
if (q->len < sizeof(ethhdr)) {
|
||||
DEBUG_ERROR("_put(): Dropped packet: first pbuf smaller than ethernet header\n");
|
||||
DEBUG_ERROR("dropped packet: first pbuf smaller than ethernet header");
|
||||
return;
|
||||
}
|
||||
memcpy(q->payload,ðhdr,sizeof(ethhdr));
|
||||
@@ -253,13 +253,12 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
|
||||
dataptr += q->len;
|
||||
}
|
||||
} else {
|
||||
DEBUG_ERROR("put(): Dropped packet: no pbufs available\n");
|
||||
DEBUG_ERROR("dropped packet: no pbufs available");
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
if(interface.input(p, &interface) != ERR_OK) {
|
||||
DEBUG_ERROR("put(): Error while RXing packet (netif->input)\n");
|
||||
DEBUG_ERROR("error while RX of packet (netif->input)");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -325,14 +324,14 @@ void NetconEthernetTap::threadMain()
|
||||
|
||||
ssize_t n = read(fd,&tmpbuf,BUF_SZ);
|
||||
if(_Connections[i]->TCP_pcb->state == SYN_SENT) {
|
||||
DEBUG_EXTRA(" tap_thread(): (sock=%p) state = SYN_SENT, should finish or be removed soon\n",
|
||||
DEBUG_EXTRA(" should finish or be removed soon, sock=%p, state=SYN_SENT",
|
||||
(void*)&(_Connections[i]->sock));
|
||||
}
|
||||
if((n < 0 && errno != EAGAIN) || (n == 0 && errno == EAGAIN)) {
|
||||
DEBUG_INFO(" tap_thread(): closing sock (%x)\n", (void*)_Connections[i]->sock);
|
||||
DEBUG_INFO(" closing sock (%x)", (void*)_Connections[i]->sock);
|
||||
closeConnection(_Connections[i]->sock);
|
||||
} else if (n > 0) {
|
||||
DEBUG_INFO(" tap_thread(): data read during connection check (%ld bytes)\n", n);
|
||||
DEBUG_INFO(" data read during connection check (%ld bytes)", n);
|
||||
phyOnUnixData(_Connections[i]->sock,_phy.getuptr(_Connections[i]->sock),&tmpbuf,n);
|
||||
}
|
||||
}
|
||||
@@ -374,11 +373,11 @@ Connection *NetconEthernetTap::getConnection(PhySocket *sock)
|
||||
|
||||
void NetconEthernetTap::closeConnection(PhySocket *sock)
|
||||
{
|
||||
DEBUG_EXTRA("closeConnection(sock=%x):\n", sock);
|
||||
DEBUG_EXTRA("sock=%x", sock);
|
||||
Mutex::Lock _l(_close_m);
|
||||
// Here we assume _tcpconns_m is already locked by caller
|
||||
if(!sock) {
|
||||
DEBUG_EXTRA("closeConnection(): invalid PhySocket\n");
|
||||
DEBUG_EXTRA("invalid PhySocket");
|
||||
return;
|
||||
}
|
||||
Connection *conn = getConnection(sock);
|
||||
@@ -388,14 +387,13 @@ void NetconEthernetTap::closeConnection(PhySocket *sock)
|
||||
lwipstack->__udp_remove(conn->UDP_pcb);
|
||||
}
|
||||
if(conn->TCP_pcb && conn->TCP_pcb->state != CLOSED) {
|
||||
DEBUG_EXTRA("closeConnection(conn=%p,sock=%p): PCB->state = %d\n",
|
||||
DEBUG_EXTRA("conn=%p, sock=%p, PCB->state = %d",
|
||||
(void*)&conn, (void*)&sock, conn->TCP_pcb->state);
|
||||
if(conn->TCP_pcb->state == SYN_SENT /*|| conn->TCP_pcb->state == CLOSE_WAIT*/) {
|
||||
DEBUG_EXTRA("closeConnection(sock=%p): invalid PCB state for this operation. ignoring.\n",
|
||||
(void*)&sock);
|
||||
DEBUG_EXTRA("ignoring close request. invalid PCB state for this operation. sock=%p", (void*)&sock);
|
||||
return;
|
||||
}
|
||||
DEBUG_EXTRA("__tcp_close(...)\n");
|
||||
DEBUG_BLANK("__tcp_close(...)");
|
||||
if(lwipstack->__tcp_close(conn->TCP_pcb) == ERR_OK) {
|
||||
// Unregister callbacks for this PCB
|
||||
lwipstack->__tcp_arg(conn->TCP_pcb, NULL);
|
||||
@@ -405,7 +403,7 @@ void NetconEthernetTap::closeConnection(PhySocket *sock)
|
||||
lwipstack->__tcp_poll(conn->TCP_pcb, NULL, 1);
|
||||
}
|
||||
else {
|
||||
DEBUG_EXTRA("closeConnection(sock=%p): error while calling tcp_close()\n", (void*)&sock);
|
||||
DEBUG_EXTRA("error while calling tcp_close() sock=%p", (void*)&sock);
|
||||
}
|
||||
}
|
||||
for(size_t i=0;i<_Connections.size();++i) {
|
||||
@@ -422,7 +420,7 @@ void NetconEthernetTap::closeConnection(PhySocket *sock)
|
||||
}
|
||||
|
||||
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr) {
|
||||
DEBUG_EXTRA("phyOnUnixClose(sock=%p):\n", (void*)&sock);
|
||||
DEBUG_EXTRA("sock=%p", (void*)&sock);
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
closeConnection(sock);
|
||||
}
|
||||
@@ -462,7 +460,7 @@ void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwi
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
DEBUG_TRANS("UDP RX <--- :: {TX: %.3f%%, RX: %d, sock=%x} :: payload = %d bytes (%d.%d.%d.%d:%d)\n",
|
||||
DEBUG_TRANS("UDP RX <--- :: {TX: %.3f%%, RX: %d, sock=%x} :: payload = %d bytes (src_addr=%d.%d.%d.%d:%d)",
|
||||
(float)conn->txsz / max, conn->rxsz/* / max*/, conn->sock, payload_sz, d[0],d[1],d[2],d[3], port);
|
||||
#endif
|
||||
}
|
||||
@@ -470,11 +468,11 @@ void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwi
|
||||
//DEBUG_INFO("phyOnUnixWritable(): tid = %d\n", pthread_mach_thread_np(pthread_self()));
|
||||
if(conn->type==SOCK_STREAM) { // Only acknolwedge receipt of TCP packets
|
||||
lwipstack->__tcp_recved(conn->TCP_pcb, n);
|
||||
DEBUG_TRANS("TCP RX <--- :: {TX: %.3f%%, RX: %.3f%%, sock=%x} :: %ld bytes\n",
|
||||
DEBUG_TRANS("TCP RX <--- :: {TX: %.3f%%, RX: %.3f%%, sock=%x} :: %ld bytes",
|
||||
(float)conn->txsz / max, (float)conn->rxsz / max, conn->sock, n);
|
||||
}
|
||||
} else {
|
||||
DEBUG_EXTRA(" processReceivedData(): errno = %d, rxsz = %d\n", errno, conn->rxsz);
|
||||
DEBUG_EXTRA(" errno = %d, rxsz = %d", errno, conn->rxsz);
|
||||
_phy.setNotifyWritable(conn->sock, false);
|
||||
}
|
||||
}
|
||||
@@ -496,7 +494,7 @@ void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_
|
||||
|
||||
void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len)
|
||||
{
|
||||
DEBUG_EXTRA("phyOnUnixData(%p), len = %d\n", (void*)&sock, (int)len);
|
||||
DEBUG_EXTRA("sock=%p, len=%d", (void*)&sock, (int)len);
|
||||
uint64_t CANARY_num;
|
||||
pid_t pid, tid;
|
||||
ssize_t wlen = len;
|
||||
@@ -518,11 +516,11 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data,
|
||||
if(detected_rpc) {
|
||||
unloadRPC(data, pid, tid, timestamp, CANARY, cmd, payload);
|
||||
memcpy(&CANARY_num, CANARY, CANARY_SZ);
|
||||
DEBUG_EXTRA(" <sock=%p> RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)\n",
|
||||
DEBUG_EXTRA(" <sock=%p> RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)",
|
||||
(void*)&sock, pid, tid, timestamp, cmd);
|
||||
|
||||
if(cmd == RPC_SOCKET) {
|
||||
DEBUG_INFO(" <sock=%p> RPC_SOCKET\n", (void*)&sock);
|
||||
DEBUG_INFO(" <sock=%p> RPC_SOCKET", (void*)&sock);
|
||||
// Create new lwip socket and associate it with this sock
|
||||
struct socket_st socket_rpc;
|
||||
memcpy(&socket_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct socket_st));
|
||||
@@ -550,7 +548,7 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data,
|
||||
// Find job
|
||||
sockdata = jobmap[CANARY_num];
|
||||
if(!sockdata.first) {
|
||||
DEBUG_INFO(" <sock=%p> unable to locate job entry for %llu\n", (void*)&sock, CANARY_num);
|
||||
DEBUG_ERROR(" <sock=%p> unable to locate job entry for %llu", (void*)&sock, CANARY_num);
|
||||
return;
|
||||
} else
|
||||
foundJob = true;
|
||||
@@ -612,36 +610,36 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data,
|
||||
rpcSock = sockdata.first;
|
||||
buf = (unsigned char*)sockdata.second;
|
||||
unloadRPC(buf, pid, tid, timestamp, CANARY, cmd, payload);
|
||||
DEBUG_EXTRA(" <sock=%p> RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)\n",
|
||||
DEBUG_EXTRA(" <sock=%p> RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)",
|
||||
(void*)&sock, pid, tid, timestamp, cmd);
|
||||
|
||||
switch(cmd) {
|
||||
case RPC_BIND:
|
||||
DEBUG_INFO(" <sock=%p> RPC_BIND\n", (void*)&sock);
|
||||
DEBUG_INFO(" <sock=%p> RPC_BIND", (void*)&sock);
|
||||
struct bind_st bind_rpc;
|
||||
memcpy(&bind_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct bind_st));
|
||||
handleBind(sock, rpcSock, uptr, &bind_rpc);
|
||||
break;
|
||||
case RPC_LISTEN:
|
||||
DEBUG_INFO(" <sock=%p> RPC_LISTEN\n", (void*)&sock);
|
||||
DEBUG_INFO(" <sock=%p> RPC_LISTEN", (void*)&sock);
|
||||
struct listen_st listen_rpc;
|
||||
memcpy(&listen_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct listen_st));
|
||||
handleListen(sock, rpcSock, uptr, &listen_rpc);
|
||||
break;
|
||||
case RPC_GETSOCKNAME:
|
||||
DEBUG_INFO(" <sock=%p> RPC_GETSOCKNAME\n", (void*)&sock);
|
||||
DEBUG_INFO(" <sock=%p> RPC_GETSOCKNAME", (void*)&sock);
|
||||
struct getsockname_st getsockname_rpc;
|
||||
memcpy(&getsockname_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct getsockname_st));
|
||||
handleGetsockname(sock, rpcSock, uptr, &getsockname_rpc);
|
||||
break;
|
||||
case RPC_GETPEERNAME:
|
||||
DEBUG_INFO(" <sock=%p> RPC_GETPEERNAME\n", (void*)&sock);
|
||||
DEBUG_INFO(" <sock=%p> RPC_GETPEERNAME", (void*)&sock);
|
||||
struct getsockname_st getpeername_rpc;
|
||||
memcpy(&getpeername_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct getsockname_st));
|
||||
handleGetpeername(sock, rpcSock, uptr, &getpeername_rpc);
|
||||
break;
|
||||
case RPC_CONNECT:
|
||||
DEBUG_INFO(" <sock=%p> RPC_CONNECT\n", (void*)&sock);
|
||||
DEBUG_INFO(" <sock=%p> RPC_CONNECT", (void*)&sock);
|
||||
struct connect_st connect_rpc;
|
||||
memcpy(&connect_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct connect_st));
|
||||
handleConnect(sock, rpcSock, conn, &connect_rpc);
|
||||
@@ -658,13 +656,13 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data,
|
||||
}
|
||||
|
||||
int NetconEthernetTap::sendReturnValue(PhySocket *sock, int retval, int _errno = 0){
|
||||
DEBUG_EXTRA(" sendReturnValue(sock=%p)\n", (void*)&sock);
|
||||
DEBUG_EXTRA("sock=%p", (void*)&sock);
|
||||
return sendReturnValue(_phy.getDescriptor(sock), retval, _errno);
|
||||
}
|
||||
int NetconEthernetTap::sendReturnValue(int fd, int retval, int _errno = 0)
|
||||
{
|
||||
//#if !defined(USE_SOCKS_PROXY)
|
||||
DEBUG_EXTRA(" sendReturnValue(): fd = %d, retval = %d, errno = %d\n", fd, retval, _errno);
|
||||
DEBUG_EXTRA("fd=%d, retval=%d, errno=%d", fd, retval, _errno);
|
||||
int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
|
||||
char retmsg[sz];
|
||||
memset(&retmsg, 0, sizeof(retmsg));
|
||||
@@ -694,7 +692,7 @@ void NetconEthernetTap::unloadRPC(void *data, pid_t &pid, pid_t &tid,
|
||||
|
||||
err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
|
||||
{
|
||||
DEBUG_INFO("nc_accept(pcb=%p)\n", (void*)&newPCB);
|
||||
DEBUG_INFO("pcb=%p", (void*)&newPCB);
|
||||
Larg *l = (Larg*)arg;
|
||||
Mutex::Lock _l(l->tap->_tcpconns_m);
|
||||
Connection *conn = l->conn;
|
||||
@@ -714,7 +712,7 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
|
||||
if(socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0) {
|
||||
if(errno < 0) {
|
||||
l->tap->sendReturnValue(conn, -1, errno);
|
||||
DEBUG_ERROR(" nc_accept(): unable to create socketpair\n");
|
||||
DEBUG_ERROR("unable to create socketpair");
|
||||
return ERR_MEM;
|
||||
}
|
||||
}
|
||||
@@ -737,7 +735,7 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
|
||||
tcp_accepted(conn->TCP_pcb); // Let lwIP know that it can queue additional incoming connections
|
||||
return ERR_OK;
|
||||
} else
|
||||
DEBUG_ERROR(" nc_accept(): can't locate Connection object for PCB.\n");
|
||||
DEBUG_ERROR("can't locate Connection object for PCB");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -759,7 +757,7 @@ void NetconEthernetTap::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct
|
||||
if(p) {
|
||||
// Intra-API "packetization" scheme: [addr_len|addr|payload_len|payload]
|
||||
if(l->conn->rxsz == DEFAULT_UDP_RX_BUF_SZ) { // if UDP buffer full
|
||||
DEBUG_INFO("nc_udp_recved(): UDP RX buffer full. Discarding oldest payload segment\n");
|
||||
DEBUG_INFO("UDP RX buffer full. Discarding oldest payload segment");
|
||||
memmove(l->conn->rxbuf, l->conn->rxbuf + ZT_MAX_MTU, DEFAULT_UDP_RX_BUF_SZ - ZT_MAX_MTU);
|
||||
addr_pos = l->conn->rxbuf + (DEFAULT_UDP_RX_BUF_SZ - ZT_MAX_MTU); // TODO:
|
||||
sz_pos = addr_pos + sizeof(struct sockaddr_storage);
|
||||
@@ -797,13 +795,13 @@ void NetconEthernetTap::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct
|
||||
err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err)
|
||||
{
|
||||
Larg *l = (Larg*)arg;
|
||||
DEBUG_EXTRA("nc_recved(conn=%p,pcb=%p)\n", (void*)&(l->conn), (void*)&PCB);
|
||||
DEBUG_EXTRA("conn=%p, pcb=%p", (void*)&(l->conn), (void*)&PCB);
|
||||
int tot = 0;
|
||||
struct pbuf* q = p;
|
||||
Mutex::Lock _l(l->tap->_tcpconns_m);
|
||||
|
||||
if(!l->conn) {
|
||||
DEBUG_ERROR(" nc_recved(): no connection\n");
|
||||
DEBUG_ERROR("no connection");
|
||||
return ERR_OK;
|
||||
}
|
||||
if(p == NULL) {
|
||||
@@ -822,7 +820,7 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *
|
||||
int avail = DEFAULT_TCP_RX_BUF_SZ - l->conn->rxsz;
|
||||
int len = p->len;
|
||||
if(avail < len)
|
||||
DEBUG_ERROR(" nc_recved(): not enough room (%d bytes) on RX buffer\n", avail);
|
||||
DEBUG_ERROR("not enough room (%d bytes) on RX buffer", avail);
|
||||
memcpy(l->conn->rxbuf + (l->conn->rxsz), p->payload, len);
|
||||
l->conn->rxsz += len;
|
||||
p = p->next;
|
||||
@@ -841,7 +839,7 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *
|
||||
|
||||
err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len)
|
||||
{
|
||||
DEBUG_EXTRA("nc_sent(pcb=%p)\n", (void*)&PCB);
|
||||
DEBUG_EXTRA("pcb=%p", (void*)&PCB);
|
||||
Larg *l = (Larg*)arg;
|
||||
Mutex::Lock _l(l->tap->_tcpconns_m);
|
||||
if(l->conn->probation && l->conn->txsz == 0){
|
||||
@@ -859,13 +857,13 @@ err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len)
|
||||
|
||||
err_t NetconEthernetTap::nc_connected_proxy(void *arg, struct tcp_pcb *PCB, err_t err)
|
||||
{
|
||||
DEBUG_INFO("nc_connected_proxy(pcb=%p)\n", (void*)&PCB);
|
||||
DEBUG_INFO("pcb=%p", (void*)&PCB);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *PCB, err_t err)
|
||||
{
|
||||
DEBUG_INFO("nc_connected(pcb=%p)\n", (void*)&PCB);
|
||||
DEBUG_INFO("pcb=%p", (void*)&PCB);
|
||||
Larg *l = (Larg*)arg;
|
||||
if(l && l->conn)
|
||||
l->tap->sendReturnValue(l->tap->_phy.getDescriptor(l->conn->rpcSock), ERR_OK);
|
||||
@@ -879,54 +877,54 @@ err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *PCB)
|
||||
|
||||
void NetconEthernetTap::nc_err(void *arg, err_t err)
|
||||
{
|
||||
DEBUG_INFO("nc_err() = %d\n", err);
|
||||
DEBUG_INFO("err=%d", err);
|
||||
Larg *l = (Larg*)arg;
|
||||
Mutex::Lock _l(l->tap->_tcpconns_m);
|
||||
|
||||
if(!l->conn)
|
||||
DEBUG_ERROR(" nc_err(): connection is NULL!\n");
|
||||
DEBUG_ERROR("conn==NULL");
|
||||
int fd = l->tap->_phy.getDescriptor(l->conn->sock);
|
||||
|
||||
switch(err)
|
||||
{
|
||||
case ERR_MEM:
|
||||
DEBUG_ERROR(" nc_err(): ERR_MEM->ENOMEM\n");
|
||||
DEBUG_ERROR("ERR_MEM->ENOMEM");
|
||||
l->tap->sendReturnValue(fd, -1, ENOMEM);
|
||||
break;
|
||||
case ERR_BUF:
|
||||
DEBUG_ERROR(" nc_err(): ERR_BUF->ENOBUFS\n");
|
||||
DEBUG_ERROR("ERR_BUF->ENOBUFS");
|
||||
l->tap->sendReturnValue(fd, -1, ENOBUFS);
|
||||
break;
|
||||
case ERR_TIMEOUT:
|
||||
DEBUG_ERROR(" nc_err(): ERR_TIMEOUT->ETIMEDOUT\n");
|
||||
DEBUG_ERROR("ERR_TIMEOUT->ETIMEDOUT");
|
||||
l->tap->sendReturnValue(fd, -1, ETIMEDOUT);
|
||||
break;
|
||||
case ERR_RTE:
|
||||
DEBUG_ERROR(" nc_err(): ERR_RTE->ENETUNREACH\n");
|
||||
DEBUG_ERROR("ERR_RTE->ENETUNREACH");
|
||||
l->tap->sendReturnValue(fd, -1, ENETUNREACH);
|
||||
break;
|
||||
case ERR_INPROGRESS:
|
||||
DEBUG_ERROR(" nc_err(): ERR_INPROGRESS->EINPROGRESS\n");
|
||||
DEBUG_ERROR("ERR_INPROGRESS->EINPROGRESS");
|
||||
l->tap->sendReturnValue(fd, -1, EINPROGRESS);
|
||||
break;
|
||||
case ERR_VAL:
|
||||
DEBUG_ERROR(" nc_err(): ERR_VAL->EINVAL\n");
|
||||
DEBUG_ERROR("ERR_VAL->EINVAL");
|
||||
l->tap->sendReturnValue(fd, -1, EINVAL);
|
||||
break;
|
||||
case ERR_WOULDBLOCK:
|
||||
DEBUG_ERROR(" nc_err(): ERR_WOULDBLOCK->EWOULDBLOCK\n");
|
||||
DEBUG_ERROR("ERR_WOULDBLOCK->EWOULDBLOCK");
|
||||
l->tap->sendReturnValue(fd, -1, EWOULDBLOCK);
|
||||
break;
|
||||
case ERR_USE:
|
||||
DEBUG_ERROR(" nc_err(): ERR_USE->EADDRINUSE\n");
|
||||
DEBUG_ERROR("ERR_USE->EADDRINUSE");
|
||||
l->tap->sendReturnValue(fd, -1, EADDRINUSE);
|
||||
break;
|
||||
case ERR_ISCONN:
|
||||
DEBUG_ERROR(" nc_err(): ERR_ISCONN->EISCONN\n");
|
||||
DEBUG_ERROR("ERR_ISCONN->EISCONN");
|
||||
l->tap->sendReturnValue(fd, -1, EISCONN);
|
||||
break;
|
||||
case ERR_ABRT:
|
||||
DEBUG_ERROR(" nc_err(): ERR_ABRT->ECONNREFUSED\n");
|
||||
DEBUG_ERROR("ERR_ABRT->ECONNREFUSED");
|
||||
l->tap->sendReturnValue(fd, -1, ECONNREFUSED);
|
||||
break;
|
||||
|
||||
@@ -950,7 +948,7 @@ void NetconEthernetTap::nc_err(void *arg, err_t err)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
DEBUG_ERROR(" nc_err(): closing connection\n");
|
||||
DEBUG_ERROR(" closing connection");
|
||||
l->tap->closeConnection(l->conn);
|
||||
}
|
||||
|
||||
@@ -963,7 +961,7 @@ void NetconEthernetTap::handleGetsockname(PhySocket *sock, PhySocket *rpcSock, v
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
Connection *conn = getConnection(sock);
|
||||
if(conn->local_addr == NULL){
|
||||
DEBUG_EXTRA(" handleGetsockname(): No address info available. Is it bound?");
|
||||
DEBUG_EXTRA("no address info available. is it bound?");
|
||||
struct sockaddr_storage storage;
|
||||
memset(&storage, 0, sizeof(struct sockaddr_storage));
|
||||
write(_phy.getDescriptor(rpcSock), NULL, sizeof(struct sockaddr_storage));
|
||||
@@ -977,7 +975,7 @@ void NetconEthernetTap::handleGetpeername(PhySocket *sock, PhySocket *rpcSock, v
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
Connection *conn = getConnection(sock);
|
||||
if(conn->peer_addr == NULL){
|
||||
DEBUG_EXTRA(" handleGetpeername(): No peer address info available. Is it connected?");
|
||||
DEBUG_EXTRA("no peer address info available. is it connected?");
|
||||
struct sockaddr_storage storage;
|
||||
memset(&storage, 0, sizeof(struct sockaddr_storage));
|
||||
write(_phy.getDescriptor(rpcSock), NULL, sizeof(struct sockaddr_storage));
|
||||
@@ -994,7 +992,7 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
|
||||
ip_addr_t connAddr;
|
||||
if(!_ips.size()) {
|
||||
// We haven't been given an address yet. Binding at this stage is premature
|
||||
DEBUG_ERROR(" handleBind(): ZT address hasn't been provided. Cannot bind yet.");
|
||||
DEBUG_ERROR("cannot bind yet. ZT address hasn't been provided");
|
||||
sendReturnValue(rpcSock, -1, ENOMEM);
|
||||
return;
|
||||
}
|
||||
@@ -1005,11 +1003,11 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
DEBUG_INFO(" handleBind(): %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port);
|
||||
DEBUG_INFO(" addr=%d.%d.%d.%d:%d", d[0],d[1],d[2],d[3], port);
|
||||
|
||||
connAddr.addr = *((u32_t *)_ips[0].rawIpData());
|
||||
Connection *conn = getConnection(sock);
|
||||
DEBUG_INFO(" handleBind(sock=%p,fd=%d,port=%d)\n", (void*)&sock, bind_rpc->fd, port);
|
||||
DEBUG_INFO(" sock=%p, fd=%d, port=%d", (void*)&sock, bind_rpc->fd, port);
|
||||
if(conn) {
|
||||
if(conn->type == SOCK_DGRAM) {
|
||||
#if defined(__ANDROID__)
|
||||
@@ -1033,7 +1031,7 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
|
||||
if(conn->TCP_pcb->state == CLOSED){
|
||||
err = lwipstack->__tcp_bind(conn->TCP_pcb, &connAddr, port);
|
||||
if(err != ERR_OK) {
|
||||
DEBUG_ERROR(" handleBind(): err = %d\n", err);
|
||||
DEBUG_ERROR("err=%d", err);
|
||||
if(err == ERR_USE)
|
||||
sendReturnValue(rpcSock, -1, EADDRINUSE);
|
||||
if(err == ERR_MEM)
|
||||
@@ -1045,20 +1043,20 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
|
||||
sendReturnValue(rpcSock, ERR_OK, ERR_OK); // Success
|
||||
}
|
||||
} else {
|
||||
DEBUG_ERROR(" handleBind(): PCB (conn=%p,pcb=%p) not in CLOSED state. Ignoring BIND request.\n",
|
||||
DEBUG_ERROR(" ignoring BIND request, PCB (conn=%p, pcb=%p) not in CLOSED state. ",
|
||||
(void*)&conn, (void*)&conn->TCP_pcb);
|
||||
sendReturnValue(rpcSock, -1, EINVAL);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
DEBUG_ERROR(" handleBind(): unable to locate Connection.\n");
|
||||
DEBUG_ERROR(" unable to locate Connection");
|
||||
sendReturnValue(rpcSock, -1, EBADF);
|
||||
}
|
||||
}
|
||||
|
||||
void NetconEthernetTap::handleListen(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc)
|
||||
{
|
||||
DEBUG_INFO("handleListen(sock=%p)\n", (void*)&sock);
|
||||
DEBUG_INFO("sock=%p", (void*)&sock);
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
Connection *conn = getConnection(sock);
|
||||
|
||||
@@ -1068,12 +1066,12 @@ void NetconEthernetTap::handleListen(PhySocket *sock, PhySocket *rpcSock, void *
|
||||
return;
|
||||
}
|
||||
if(!conn) {
|
||||
DEBUG_ERROR(" handleListen(): unable to locate Connection.\n");
|
||||
DEBUG_ERROR(" unable to locate Connection");
|
||||
sendReturnValue(rpcSock, -1, EBADF);
|
||||
return;
|
||||
}
|
||||
if(conn->TCP_pcb->state == LISTEN) {
|
||||
DEBUG_ERROR(" handleListen(): PCB is already in listening state.\n");
|
||||
DEBUG_ERROR(" PCB is already in listening state");
|
||||
sendReturnValue(rpcSock, ERR_OK, ERR_OK);
|
||||
return;
|
||||
}
|
||||
@@ -1100,19 +1098,19 @@ Connection * NetconEthernetTap::handleSocketProxy(PhySocket *sock, int socket_ty
|
||||
{
|
||||
Connection *conn = getConnection(sock);
|
||||
if(!conn){
|
||||
DEBUG_INFO("handleSocketProxy(sock=%p): Unable to locate Connection object for this PhySocket\n", (void*)&sock);
|
||||
DEBUG_INFO("unable to locate Connection object for this PhySocket sock=%p", (void*)&sock);
|
||||
return NULL;
|
||||
}
|
||||
DEBUG_INFO("handleSocketProxy(sock=%p)\n", (void*)&sock);
|
||||
DEBUG_INFO("sock=%p", (void*)&sock);
|
||||
struct udp_pcb *new_udp_PCB = NULL;
|
||||
struct tcp_pcb *new_tcp_PCB = NULL;
|
||||
if(socket_type == SOCK_DGRAM) {
|
||||
DEBUG_INFO(" handleSocketProxy(): SOCK_DGRAM\n");
|
||||
DEBUG_EXTRA("SOCK_DGRAM");
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
new_udp_PCB = lwipstack->__udp_new();
|
||||
}
|
||||
else if(socket_type == SOCK_STREAM) {
|
||||
DEBUG_INFO(" handleSocketProxy(): SOCK_STREAM\n");
|
||||
DEBUG_EXTRA("SOCK_STREAM");
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
new_tcp_PCB = lwipstack->__tcp_new();
|
||||
}
|
||||
@@ -1123,25 +1121,25 @@ Connection * NetconEthernetTap::handleSocketProxy(PhySocket *sock, int socket_ty
|
||||
conn->peer_addr = NULL;
|
||||
if(conn->type == SOCK_DGRAM) conn->UDP_pcb = new_udp_PCB;
|
||||
if(conn->type == SOCK_STREAM) conn->TCP_pcb = new_tcp_PCB;
|
||||
DEBUG_INFO(" handleSocketProxy(): Updated sock=%p\n", (void*)&sock);
|
||||
DEBUG_INFO(" updated sock=%p", (void*)&sock);
|
||||
return conn;
|
||||
}
|
||||
DEBUG_ERROR(" handleSocketProxy(): Memory not available for new PCB\n");
|
||||
DEBUG_ERROR(" memory not available for new PCB");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Connection * NetconEthernetTap::handleSocket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc)
|
||||
{
|
||||
DEBUG_INFO("handleSocket(sock=%p)\n", (void*)&sock);
|
||||
DEBUG_INFO("sock=%p", (void*)&sock);
|
||||
struct udp_pcb *new_udp_PCB = NULL;
|
||||
struct tcp_pcb *new_tcp_PCB = NULL;
|
||||
if(socket_rpc->socket_type == SOCK_DGRAM) {
|
||||
DEBUG_INFO("handleSocket(): SOCK_DGRAM\n");
|
||||
DEBUG_EXTRA("SOCK_DGRAM");
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
new_udp_PCB = lwipstack->__udp_new();
|
||||
}
|
||||
else if(socket_rpc->socket_type == SOCK_STREAM) {
|
||||
DEBUG_INFO("handleSocket(): SOCK_STREAM\n");
|
||||
DEBUG_EXTRA("SOCK_STREAM");
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
new_tcp_PCB = lwipstack->__tcp_new();
|
||||
}
|
||||
@@ -1157,14 +1155,14 @@ Connection * NetconEthernetTap::handleSocket(PhySocket *sock, void **uptr, struc
|
||||
_Connections.push_back(newConn);
|
||||
return newConn;
|
||||
}
|
||||
DEBUG_ERROR(" handleSocket(): Memory not available for new PCB\n");
|
||||
DEBUG_ERROR(" memory not available for new PCB");
|
||||
sendReturnValue(_phy.getDescriptor(sock), -1, ENOMEM);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *rawAddr)
|
||||
{
|
||||
DEBUG_INFO("handleConnectProxy(%p)\n", (void*)&sock);
|
||||
DEBUG_INFO("sock=%p", (void*)&sock);
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
int port = rawAddr->sin_port;
|
||||
ip_addr_t connAddr = convert_ip(rawAddr);
|
||||
@@ -1172,13 +1170,13 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r
|
||||
|
||||
Connection *conn = getConnection(sock);
|
||||
if(!conn) {
|
||||
DEBUG_INFO(" handleConnectProxy(): Unable to locate Connection object for (sock=%p)\n", (void*)&sock);
|
||||
DEBUG_INFO(" unable to locate Connection object for sock=%p", (void*)&sock);
|
||||
return -1;
|
||||
}
|
||||
if(conn->type == SOCK_DGRAM) {
|
||||
// Generates no network traffic
|
||||
if((err = lwipstack->__udp_connect(conn->UDP_pcb,&connAddr,port)) < 0)
|
||||
DEBUG_INFO("handleConnectProxy(): Error while connecting to with UDP\n");
|
||||
DEBUG_INFO("error while connecting to with UDP (sock=%p)", (void*)&sock);
|
||||
lwipstack->__udp_recv(conn->UDP_pcb, nc_udp_recved, new Larg(this, conn));
|
||||
errno = ERR_OK;
|
||||
return 0;
|
||||
@@ -1196,10 +1194,10 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
DEBUG_INFO(" handleConnectProxy(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], port);
|
||||
DEBUG_INFO(" handleConnectProxy(): pcb->state = %x\n", conn->TCP_pcb->state);
|
||||
DEBUG_INFO(" addr=%d.%d.%d.%d:%d", d[0],d[1],d[2],d[3], port);
|
||||
DEBUG_INFO(" pcb->state=%x", conn->TCP_pcb->state);
|
||||
if(conn->TCP_pcb->state != CLOSED) {
|
||||
DEBUG_INFO(" handleConnectProxy(): PCB != CLOSED, cannot connect using this PCB\n");
|
||||
DEBUG_INFO(" cannot connect using this PCB, PCB!=CLOSED");
|
||||
errno = EAGAIN;
|
||||
return -1;
|
||||
}
|
||||
@@ -1243,7 +1241,7 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r
|
||||
// that's it!
|
||||
// - Most instances of a retval for a connect() should happen
|
||||
// in the nc_connect() and nc_err() callbacks!
|
||||
DEBUG_ERROR(" handleConnectProxy(): unable to connect\n");
|
||||
DEBUG_ERROR(" unable to connect");
|
||||
errno = EAGAIN;
|
||||
return -1;
|
||||
}
|
||||
@@ -1251,7 +1249,7 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r
|
||||
conn->listening=true;
|
||||
return 0;
|
||||
} else {
|
||||
DEBUG_ERROR(" handleConnectProxy(): could not locate PCB based on their fd\n");
|
||||
DEBUG_ERROR(" could not locate PCB based on application-provided fd");
|
||||
errno = EBADF;
|
||||
return -1;
|
||||
}
|
||||
@@ -1260,7 +1258,7 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r
|
||||
|
||||
void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Connection *conn, struct connect_st* connect_rpc)
|
||||
{
|
||||
DEBUG_EXTRA("handleConnect(%p)\n", (void*)&sock);
|
||||
DEBUG_EXTRA("sock=%p", (void*)&sock);
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
struct sockaddr_in *rawAddr = (struct sockaddr_in *) &connect_rpc->addr;
|
||||
int port = lwipstack->__lwip_ntohs(rawAddr->sin_port);
|
||||
@@ -1272,12 +1270,12 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
DEBUG_INFO(" handleConnect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], port);
|
||||
DEBUG_INFO(" addr=%d.%d.%d.%d:%d", d[0],d[1],d[2],d[3], port);
|
||||
|
||||
if(conn->type == SOCK_DGRAM) {
|
||||
// Generates no network traffic
|
||||
if((err = lwipstack->__udp_connect(conn->UDP_pcb,&connAddr,port)) < 0)
|
||||
DEBUG_ERROR("handleConnect(): Error while connecting to with UDP\n");
|
||||
DEBUG_ERROR("error while connecting to with UDP");
|
||||
lwipstack->__udp_recv(conn->UDP_pcb, nc_udp_recved, new Larg(this, conn));
|
||||
sendReturnValue(rpcSock, 0, ERR_OK);
|
||||
return;
|
||||
@@ -1289,9 +1287,9 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne
|
||||
lwipstack->__tcp_poll(conn->TCP_pcb, nc_poll, APPLICATION_POLL_FREQ);
|
||||
lwipstack->__tcp_arg(conn->TCP_pcb, new Larg(this, conn));
|
||||
|
||||
DEBUG_EXTRA(" handleConnect(): pcb->state = %x\n", conn->TCP_pcb->state);
|
||||
DEBUG_EXTRA(" pcb->state=%x", conn->TCP_pcb->state);
|
||||
if(conn->TCP_pcb->state != CLOSED) {
|
||||
DEBUG_INFO(" handleConnect(): PCB != CLOSED, cannot connect using this PCB\n");
|
||||
DEBUG_INFO(" cannot connect using this PCB, PCB!=CLOSED");
|
||||
sendReturnValue(rpcSock, -1, EAGAIN);
|
||||
return;
|
||||
}
|
||||
@@ -1323,7 +1321,6 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne
|
||||
|
||||
2) Cannot allocate new pbuf
|
||||
3) Cannot allocate new TCP segment
|
||||
|
||||
*/
|
||||
sendReturnValue(rpcSock, -1, EAGAIN); // TODO: Doesn't describe the problem well, but closest match
|
||||
return;
|
||||
@@ -1336,48 +1333,48 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne
|
||||
// that's it!
|
||||
// - Most instances of a retval for a connect() should happen
|
||||
// in the nc_connect() and nc_err() callbacks!
|
||||
DEBUG_ERROR(" handleConnect(): unable to connect\n");
|
||||
DEBUG_ERROR(" unable to connect");
|
||||
sendReturnValue(rpcSock, -1, EAGAIN);
|
||||
}
|
||||
// Everything seems to be ok, but we don't have enough info to retval
|
||||
conn->listening=true;
|
||||
conn->rpcSock=rpcSock; // used for return value from lwip CB
|
||||
} else {
|
||||
DEBUG_ERROR(" handleConnect(): could not locate PCB based on their fd\n");
|
||||
DEBUG_ERROR(" could not locate PCB based on application-provided fd");
|
||||
sendReturnValue(rpcSock, -1, EBADF);
|
||||
}
|
||||
}
|
||||
|
||||
void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
{
|
||||
DEBUG_EXTRA("handleWrite(conn=%p)\n", (void*)&conn);
|
||||
DEBUG_EXTRA("conn=%p", (void*)&conn);
|
||||
if(!conn || (!conn->TCP_pcb && !conn->UDP_pcb)) {
|
||||
DEBUG_ERROR(" handleWrite(): invalid connection\n");
|
||||
DEBUG_ERROR(" invalid connection");
|
||||
return;
|
||||
}
|
||||
if(conn->type == SOCK_DGRAM) {
|
||||
if(!conn->UDP_pcb) {
|
||||
DEBUG_ERROR(" handleWrite(): type = SOCK_DGRAM, invalid UDP_pcb\n");
|
||||
DEBUG_ERROR(" invalid UDP_pcb, type=SOCK_DGRAM");
|
||||
return;
|
||||
}
|
||||
// TODO: Packet re-assembly hasn't yet been tested with lwIP so UDP packets are limited to MTU-sized chunks
|
||||
int udp_trans_len = conn->txsz < ZT_UDP_DEFAULT_PAYLOAD_MTU ? conn->txsz : ZT_UDP_DEFAULT_PAYLOAD_MTU;
|
||||
|
||||
DEBUG_EXTRA(" handleWrite(): Allocating pbuf chain of size (%d) for UDP packet, TXSZ = %d\n", udp_trans_len, conn->txsz);
|
||||
DEBUG_EXTRA(" allocating pbuf chain of size=%d for UDP packet, txsz=%d", udp_trans_len, conn->txsz);
|
||||
struct pbuf * pb = lwipstack->__pbuf_alloc(PBUF_TRANSPORT, udp_trans_len, PBUF_POOL);
|
||||
if(!pb){
|
||||
DEBUG_ERROR(" handleWrite(): unable to allocate new pbuf of size (%d)\n", conn->txsz);
|
||||
DEBUG_ERROR(" unable to allocate new pbuf of size=%d", conn->txsz);
|
||||
return;
|
||||
}
|
||||
memcpy(pb->payload, conn->txbuf, udp_trans_len);
|
||||
int err = lwipstack->__udp_send(conn->UDP_pcb, pb);
|
||||
|
||||
if(err == ERR_MEM) {
|
||||
DEBUG_ERROR(" handleWrite(): Error sending packet. Out of memory\n");
|
||||
DEBUG_ERROR(" error sending packet. out of memory");
|
||||
} else if(err == ERR_RTE) {
|
||||
DEBUG_ERROR(" handleWrite(): Could not find route to destinations address\n");
|
||||
DEBUG_ERROR(" could not find route to destinations address");
|
||||
} else if(err != ERR_OK) {
|
||||
DEBUG_ERROR(" handleWrite(): Error sending packet - %d\n", err);
|
||||
DEBUG_ERROR(" error sending packet - %d", err);
|
||||
} else {
|
||||
// Success
|
||||
int buf_remaining = (conn->txsz)-udp_trans_len;
|
||||
@@ -1394,8 +1391,7 @@ void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
|
||||
DEBUG_TRANS("UDP TX ---> :: {TX: ------, RX: ------, sock=%x} :: %d bytes (%d.%d.%d.%d:%d)\n",
|
||||
DEBUG_TRANS("UDP TX ---> :: {TX: ------, RX: ------, sock=%x} :: %d bytes (dest_addr=%d.%d.%d.%d:%d)",
|
||||
conn->sock, udp_trans_len, d[0], d[1], d[2], d[3], port);
|
||||
#endif
|
||||
}
|
||||
@@ -1404,7 +1400,7 @@ void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
}
|
||||
else if(conn->type == SOCK_STREAM) {
|
||||
if(!conn->TCP_pcb) {
|
||||
DEBUG_ERROR(" handleWrite(): type = SOCK_STREAM, invalid TCP_pcb\n");
|
||||
DEBUG_ERROR(" invalid TCP_pcb, type=SOCK_STREAM");
|
||||
return;
|
||||
}
|
||||
// How much we are currently allowed to write to the connection
|
||||
@@ -1416,7 +1412,7 @@ void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
// corresponding PhySocket until nc_sent() is called and confirms that there is
|
||||
// now space on the buffer
|
||||
if(!conn->probation) {
|
||||
DEBUG_ERROR(" handleWrite(): sndbuf == 0, LWIP stack is full\n");
|
||||
DEBUG_ERROR(" LWIP stack is full, sndbuf == 0");
|
||||
_phy.setNotifyReadable(conn->sock, false);
|
||||
conn->probation = true;
|
||||
}
|
||||
@@ -1435,9 +1431,9 @@ void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
err = lwipstack->__tcp_write(conn->TCP_pcb, &conn->txbuf, r, TCP_WRITE_FLAG_COPY);
|
||||
lwipstack->__tcp_output(conn->TCP_pcb);
|
||||
if(err != ERR_OK) {
|
||||
DEBUG_ERROR(" handleWrite(): error while writing to PCB, (err = %d)\n", err);
|
||||
DEBUG_ERROR(" error while writing to PCB, err=%d", err);
|
||||
if(err == -1)
|
||||
DEBUG_ERROR(" handleWrite(): out of memory\n");
|
||||
DEBUG_ERROR("out of memory");
|
||||
return;
|
||||
} else {
|
||||
sz = (conn->txsz)-r;
|
||||
@@ -1445,7 +1441,7 @@ void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
memmove(&conn->txbuf, (conn->txbuf+r), sz);
|
||||
conn->txsz -= r;
|
||||
int max = conn->type == SOCK_STREAM ? DEFAULT_TCP_TX_BUF_SZ : DEFAULT_UDP_TX_BUF_SZ;
|
||||
DEBUG_TRANS("TCP TX ---> :: {TX: %.3f%%, RX: %.3f%%, sock=%p} :: %d bytes\n",
|
||||
DEBUG_TRANS("TCP TX ---> :: {TX: %.3f%%, RX: %.3f%%, sock=%p} :: %d bytes",
|
||||
(float)conn->txsz / (float)max, (float)conn->rxsz / max, (void*)&conn->sock, r);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ pthread_key_t thr_id_key;
|
||||
addr_un = (struct sockaddr_un*)&addr;
|
||||
return strcmp(addr_un->sun_path, api_netpath) == 0;
|
||||
}
|
||||
DEBUG_INFO("connected_to_service(): Not connected to service\n");
|
||||
DEBUG_ERROR("not connected to service");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ pthread_key_t thr_id_key;
|
||||
#if !defined(__ANDROID__)
|
||||
ssize_t sendto(SENDTO_SIG)
|
||||
{
|
||||
DEBUG_INFO("fd=%d, len=%d\n", fd, (int)len);
|
||||
DEBUG_INFO("fd=%d, len=%d", fd, (int)len);
|
||||
//if (!check_intercept_enabled())
|
||||
return realsendto(fd, buf, len, flags, addr, addrlen);
|
||||
return zts_sendto(fd, buf, len, flags, addr, addrlen);
|
||||
@@ -184,7 +184,7 @@ pthread_key_t thr_id_key;
|
||||
#if !defined(__ANDROID__)
|
||||
ssize_t sendmsg(SENDMSG_SIG)
|
||||
{
|
||||
DEBUG_INFO("");
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
//if(!check_intercept_enabled())
|
||||
return realsendmsg(fd, msg, flags);
|
||||
zts_sendmsg(fd, msg, flags);
|
||||
@@ -283,7 +283,7 @@ pthread_key_t thr_id_key;
|
||||
#endif
|
||||
|| socket_family == AF_UNIX) {
|
||||
int err = realsocket(socket_family, socket_type, protocol);
|
||||
DEBUG_BLANK("realsocket(): err=%d\n", err);
|
||||
DEBUG_BLANK("realsocket(): err=%d", err);
|
||||
return err;
|
||||
}
|
||||
return zts_socket(socket_family, socket_type, protocol);
|
||||
@@ -303,7 +303,7 @@ pthread_key_t thr_id_key;
|
||||
struct sockaddr_storage storage;
|
||||
memcpy(&storage, addr, addrlen);
|
||||
struct sockaddr_un *s_un = (struct sockaddr_un*)&storage;
|
||||
DEBUG_INFO("addr=%s\n", s_un->sun_path);
|
||||
DEBUG_INFO("addr=%s", s_un->sun_path);
|
||||
}
|
||||
|
||||
int port = connaddr->sin_port;
|
||||
@@ -313,7 +313,7 @@ pthread_key_t thr_id_key;
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
DEBUG_INFO("addr=%d.%d.%d.%d:%d\n", d[0],d[1],d[2],d[3], ntohs(port));
|
||||
DEBUG_INFO("addr=%d.%d.%d.%d:%d", d[0],d[1],d[2],d[3], ntohs(port));
|
||||
|
||||
if(!check_intercept_enabled())
|
||||
return realconnect(fd, addr, addrlen);
|
||||
@@ -382,7 +382,7 @@ pthread_key_t thr_id_key;
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
DEBUG_INFO("addr=%d.%d.%d.%d:%d\n", d[0],d[1],d[2],d[3], ntohs(port));
|
||||
DEBUG_INFO("addr=%d.%d.%d.%d:%d", d[0],d[1],d[2],d[3], ntohs(port));
|
||||
|
||||
int sock_type;
|
||||
socklen_t sock_type_len = sizeof(sock_type);
|
||||
@@ -436,7 +436,7 @@ pthread_key_t thr_id_key;
|
||||
if(fcntl(fd, F_GETFD) < 0) {
|
||||
return -1;
|
||||
errno = EBADF;
|
||||
DEBUG_INFO("EBADF\n");
|
||||
DEBUG_ERROR("EBADF");
|
||||
return -1;
|
||||
}
|
||||
// Check that it is a socket
|
||||
@@ -444,13 +444,13 @@ pthread_key_t thr_id_key;
|
||||
socklen_t opt_len;
|
||||
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
|
||||
errno = ENOTSOCK;
|
||||
DEBUG_INFO("ENOTSOCK\n");
|
||||
DEBUG_ERROR("ENOTSOCK");
|
||||
return -1;
|
||||
}
|
||||
// Check that this socket supports accept()
|
||||
if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
|
||||
errno = EOPNOTSUPP;
|
||||
DEBUG_INFO("EOPNOTSUPP\n");
|
||||
DEBUG_ERROR("EOPNOTSUPP");
|
||||
return -1;
|
||||
}
|
||||
// Check that we haven't hit the soft-limit file descriptors allowed
|
||||
@@ -458,13 +458,13 @@ pthread_key_t thr_id_key;
|
||||
getrlimit(RLIMIT_NOFILE, &rl);
|
||||
if(fd >= rl.rlim_cur){
|
||||
errno = EMFILE;
|
||||
DEBUG_INFO("EMFILE\n");
|
||||
DEBUG_ERROR("EMFILE");
|
||||
return -1;
|
||||
}
|
||||
// Check address length
|
||||
if(addrlen < 0) {
|
||||
errno = EINVAL;
|
||||
DEBUG_INFO("EINVAL\n");
|
||||
DEBUG_ERROR("EINVAL");
|
||||
return -1;
|
||||
}
|
||||
// redirect calls for standard I/O descriptors to kernel
|
||||
@@ -483,7 +483,7 @@ pthread_key_t thr_id_key;
|
||||
|
||||
int listen(LISTEN_SIG)
|
||||
{
|
||||
DEBUG_INFO("listen(%d):\n", fd);
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
if (!check_intercept_enabled())
|
||||
return reallisten(fd, backlog);
|
||||
|
||||
@@ -520,7 +520,7 @@ pthread_key_t thr_id_key;
|
||||
// int fd
|
||||
|
||||
int close(CLOSE_SIG) {
|
||||
DEBUG_INFO(" close(%d)\n", fd);
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
if(!check_intercept_enabled()) {
|
||||
return realclose(fd);
|
||||
}
|
||||
@@ -534,14 +534,14 @@ pthread_key_t thr_id_key;
|
||||
|
||||
int getsockname(GETSOCKNAME_SIG)
|
||||
{
|
||||
DEBUG_INFO("getsockname(%d):\n", fd);
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
#if !defined(__IOS__)
|
||||
if (!check_intercept_enabled())
|
||||
return realgetsockname(fd, addr, addrlen);
|
||||
#endif
|
||||
DEBUG_INFO("getsockname(%d)\n", fd);
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
if(!connected_to_service(fd)) {
|
||||
DEBUG_INFO("getsockname(): not used by service\n");
|
||||
DEBUG_ERROR("fd=%d not used by service");
|
||||
return realgetsockname(fd, addr, addrlen);
|
||||
}
|
||||
return zts_getsockname(fd, addr, addrlen);
|
||||
@@ -569,7 +569,7 @@ pthread_key_t thr_id_key;
|
||||
|
||||
if (!check_intercept_enabled())
|
||||
return realsyscall(number,a,b,c,d,e,f);
|
||||
DEBUG_INFO("syscall(%u, ...)\n", number);
|
||||
DEBUG_INFO("number=%u", number);
|
||||
|
||||
#if defined(__i386__)
|
||||
// TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
|
||||
|
||||
@@ -50,8 +50,8 @@
|
||||
|
||||
|
||||
// externs common between SDK_Intercept and SDK_Socket from SDK.h
|
||||
//int (*realsocket)(SOCKET_SIG);
|
||||
//int (*realconnect)(CONNECT_SIG);
|
||||
int (*realsocket)(SOCKET_SIG);
|
||||
int (*realconnect)(CONNECT_SIG);
|
||||
|
||||
void dwr(int level, const char *fmt, ... );
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
std::string service_path;
|
||||
pthread_t intercept_thread;
|
||||
int * intercept_thread_id;
|
||||
extern pthread_key_t thr_id_key;
|
||||
pthread_key_t thr_id_key;
|
||||
static ZeroTier::OneService *volatile zt1Service;
|
||||
|
||||
std::string localHomeDir; // Local shortened path
|
||||
@@ -69,11 +69,15 @@ bool rpcEnabled;
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Prototypes
|
||||
void *zts_start_service(void *thread_id);
|
||||
void zts_init_rpc(const char * path, const char * nwid);
|
||||
void dwr(int level, const char *fmt, ... );
|
||||
// ------------------------------------------------------------------------------
|
||||
// --------------------------------- Base zts_* API -----------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
// Prototypes
|
||||
void *zts_start_core_service(void *thread_id);
|
||||
void zts_init_rpc(const char * path, const char * nwid);
|
||||
|
||||
//
|
||||
int zts_start_proxy_server(const char *homepath, const char * nwid, struct sockaddr_storage * addr) {
|
||||
DEBUG_INFO();
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
@@ -87,8 +91,9 @@ int zts_start_proxy_server(const char *homepath, const char * nwid, struct socka
|
||||
DEBUG_ERROR("zts_start_proxy_server(%s): Invalid tap. Possibly incorrect NWID", nwid);
|
||||
return 0;
|
||||
}
|
||||
//
|
||||
int zts_stop_proxy_server(const char *nwid) {
|
||||
DEBUG_INFO("zts_stop_proxy_server()");
|
||||
DEBUG_INFO();
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::NetconEthernetTap * tap = zt1Service->getTaps()[nwid_int];
|
||||
if(tap) {
|
||||
@@ -100,6 +105,7 @@ int zts_stop_proxy_server(const char *nwid) {
|
||||
DEBUG_ERROR("zts_stop_proxy_server(%s): Invalid tap. Possibly incorrect NWID", nwid);
|
||||
return 0;
|
||||
}
|
||||
//
|
||||
int zts_get_proxy_server_address(const char * nwid, struct sockaddr_storage * addr) {
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::NetconEthernetTap * tap = zt1Service->getTaps()[nwid_int];
|
||||
@@ -116,10 +122,10 @@ void zts_join_network(const char * nwid) {
|
||||
DEBUG_INFO();
|
||||
std::string confFile = zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
|
||||
if(!ZeroTier::OSUtils::mkdir(netDir)) {
|
||||
DEBUG_INFO("unable to create %s\n", netDir.c_str());
|
||||
DEBUG_ERROR("unable to create: %s", netDir.c_str());
|
||||
}
|
||||
if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) {
|
||||
DEBUG_INFO("unable to write network conf file: %s\n", confFile.c_str());
|
||||
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
|
||||
}
|
||||
zt1Service->join(nwid);
|
||||
// Provide the API with the RPC information
|
||||
@@ -130,9 +136,20 @@ void zts_join_network(const char * nwid) {
|
||||
zts_start_proxy_server(homeDir.c_str(), nwid, NULL); // NULL addr for default
|
||||
#endif
|
||||
}
|
||||
void zts_leave_network(const char * nwid) { zt1Service->leave(nwid); }
|
||||
bool zts_service_is_running() { return zt1Service->isRunning(); }
|
||||
void zts_stop_service() { zt1Service->terminate(); }
|
||||
//
|
||||
void zts_leave_network(const char * nwid) {
|
||||
if(zt1Service)
|
||||
zt1Service->leave(nwid);
|
||||
}
|
||||
//
|
||||
bool zts_service_is_running() {
|
||||
return !zt1Service ? false : zt1Service->isRunning();
|
||||
}
|
||||
//
|
||||
void zts_stop_service() {
|
||||
if(zt1Service)
|
||||
zt1Service->terminate();
|
||||
}
|
||||
|
||||
// FIXME: Re-implemented to make it play nicer with the C-linkage required for Xcode integrations
|
||||
// Now only returns first assigned address per network. Shouldn't normally be a problem
|
||||
@@ -161,20 +178,25 @@ void zts_get_addresses(const char * nwid, char *addrstr)
|
||||
return ip_strings;
|
||||
*/
|
||||
}
|
||||
|
||||
//
|
||||
int zts_get_device_id() { /* TODO */ return 0; }
|
||||
|
||||
//
|
||||
bool zts_is_relayed() {
|
||||
// TODO
|
||||
// zt1Service->getNode()->peers()
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
char *zts_get_homepath() {
|
||||
return (char*)givenHomeDir.c_str();
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ----------------------------- .NET Interop functions -------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#if defined(__UNITY_3D__)
|
||||
// .NET Interop-friendly debug mechanism
|
||||
typedef void (*FuncPtr)( const char * );
|
||||
@@ -197,13 +219,60 @@ char *zts_get_homepath() {
|
||||
}
|
||||
#endif
|
||||
|
||||
//
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ------------------------------ --------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
// For use when symbols are used in Swift
|
||||
|
||||
// If we're using a bridging header for an Xcode project...
|
||||
// This matters because the header/wrapper needs to define symbols
|
||||
// for usage in Swift, but in order to maintain API naming consistency
|
||||
// between build environments whilst also preventing duplicate symbols
|
||||
// we need to check for this case
|
||||
|
||||
/*
|
||||
#if defined(USING_BRIDGING_HEADER)
|
||||
void zts_start_service(const char *path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void zts_start_service(const char *path, const char *nwid)
|
||||
{
|
||||
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
|
||||
void zts_start_service(const char *path)
|
||||
{
|
||||
DEBUG_INFO("path=%s", path);
|
||||
if(path)
|
||||
homeDir = path;
|
||||
zts_start_core_service(NULL);
|
||||
}
|
||||
|
||||
#if !defined(USING_BRIDGING_HEADER)
|
||||
void zt_start_service(const char * path, const char *nwid) {
|
||||
DEBUG_INFO("path=%s", path);
|
||||
if(path)
|
||||
homeDir = path;
|
||||
zts_start_core_service(NULL);
|
||||
}
|
||||
|
||||
void zt_join_network()
|
||||
{
|
||||
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
#if (defined(__APPLE__) || defined(__linux__)) && (!defined(__IOS__) && !defined(__ANDROID__))
|
||||
void zt_start_service(const char * path, const char *nwid) {
|
||||
printf("path = %s\n", path);
|
||||
if(path)
|
||||
homeDir = path;
|
||||
zts_start_service(NULL);
|
||||
zts_start_core_service(NULL);
|
||||
}
|
||||
void zt_join_network(const char * nwid) {
|
||||
zts_join_network(nwid);
|
||||
@@ -212,10 +281,46 @@ char *zts_get_homepath() {
|
||||
zts_leave_network(nwid);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// Typically used on iOS/OSX
|
||||
#if !defined(__ANDROID__)
|
||||
// Starts a service thread and performs basic setup tasks
|
||||
void init_service(int key, const char * path) {
|
||||
givenHomeDir = path;
|
||||
pthread_key_create(&thr_id_key, NULL);
|
||||
intercept_thread_id = (int*)malloc(sizeof(int));
|
||||
*intercept_thread_id = key;
|
||||
pthread_create(&intercept_thread, NULL, zts_start_core_service, (void *)(intercept_thread_id));
|
||||
}
|
||||
void init_service_and_rpc(int key, const char * path, const char * nwid) {
|
||||
rpcEnabled = true;
|
||||
rpcNWID = nwid;
|
||||
init_service(key, path);
|
||||
}
|
||||
// Enables or disables intercept for current thread using key in thread-local storage
|
||||
void set_intercept_status(int mode) {
|
||||
#if defined(__APPLE__)
|
||||
DEBUG_INFO("mode=%d, tid=%d", mode, pthread_mach_thread_np(pthread_self()));
|
||||
#else
|
||||
// fprintf(stderr, "set_intercept_status(mode=%d): tid = %d\n", mode, gettid());
|
||||
#endif
|
||||
pthread_key_create(&thr_id_key, NULL);
|
||||
intercept_thread_id = (int*)malloc(sizeof(int));
|
||||
*intercept_thread_id = mode;
|
||||
pthread_setspecific(thr_id_key, intercept_thread_id);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ------------------------------ EXPORTED JNI METHODS --------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
// JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
|
||||
|
||||
|
||||
// Android JNI wrapper
|
||||
// JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
|
||||
#if defined(__ANDROID__)
|
||||
// Starts a new service instance
|
||||
/* NOTE: Since on Android devices the sdcard is formatted as fat32, we can't use just any
|
||||
@@ -224,7 +329,7 @@ char *zts_get_homepath() {
|
||||
JNIEXPORT int JNICALL Java_ZeroTier_ZTSDK_zt_1start_1service(JNIEnv *env, jobject thisObj, jstring path) {
|
||||
if(path)
|
||||
homeDir = env->GetStringUTFChars(path, NULL);
|
||||
zts_start_service(NULL);
|
||||
zts_start_core_service(NULL);
|
||||
}
|
||||
// Shuts down ZeroTier service and SOCKS5 Proxy server
|
||||
JNIEXPORT void JNICALL Java_ZeroTier_ZTSDK_zt_1stop_1service(JNIEnv *env, jobject thisObj) {
|
||||
@@ -333,40 +438,14 @@ char *zts_get_homepath() {
|
||||
#endif
|
||||
|
||||
|
||||
// Typically used on iOS/OSX
|
||||
#if !defined(__ANDROID__)
|
||||
// Starts a service thread and performs basic setup tasks
|
||||
void init_service(int key, const char * path) {
|
||||
givenHomeDir = path;
|
||||
pthread_key_create(&thr_id_key, NULL);
|
||||
intercept_thread_id = (int*)malloc(sizeof(int));
|
||||
*intercept_thread_id = key;
|
||||
pthread_create(&intercept_thread, NULL, zts_start_service, (void *)(intercept_thread_id));
|
||||
}
|
||||
void init_service_and_rpc(int key, const char * path, const char * nwid) {
|
||||
rpcEnabled = true;
|
||||
rpcNWID = nwid;
|
||||
init_service(key, path);
|
||||
}
|
||||
// Enables or disables intercept for current thread using key in thread-local storage
|
||||
void set_intercept_status(int mode) {
|
||||
#if defined(__APPLE__)
|
||||
fprintf(stderr, "set_intercept_status(mode=%d): tid = %d\n", mode, pthread_mach_thread_np(pthread_self()));
|
||||
#else
|
||||
// fprintf(stderr, "set_intercept_status(mode=%d): tid = %d\n", mode, gettid());
|
||||
#endif
|
||||
pthread_key_create(&thr_id_key, NULL);
|
||||
intercept_thread_id = (int*)malloc(sizeof(int));
|
||||
*intercept_thread_id = mode;
|
||||
pthread_setspecific(thr_id_key, intercept_thread_id);
|
||||
}
|
||||
#endif
|
||||
// ------------------------------------------------------------------------------
|
||||
// ------------------------------- zts_start_service ----------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// Starts a ZeroTier service in the background
|
||||
void *zts_start_service(void *thread_id) {
|
||||
void *zts_start_core_service(void *thread_id) {
|
||||
#if defined(__ANDROID__)
|
||||
DEBUG_INFO("ZTSDK_BUILD_VERSION = %d\n", ZTSDK_BUILD_VERSION);
|
||||
DEBUG_INFO("ZTSDK_BUILD_VERSION = %d", ZTSDK_BUILD_VERSION);
|
||||
#endif
|
||||
|
||||
#if defined(SDK_BUNDLED) && !defined(__ANDROID__)
|
||||
@@ -395,7 +474,7 @@ void *zts_start_service(void *thread_id) {
|
||||
localHomeDir = homeDir; // Used for RPC and *can* differ from homeDir on some platforms
|
||||
#endif
|
||||
|
||||
DEBUG_INFO("homeDir = %s\n", homeDir.c_str());
|
||||
DEBUG_INFO("homeDir=%s", homeDir.c_str());
|
||||
// Where network .conf files will be stored
|
||||
netDir = homeDir + "/networks.d";
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
@@ -412,13 +491,13 @@ void *zts_start_service(void *thread_id) {
|
||||
ptmp.append(*pi);
|
||||
if ((*pi != ".")&&(*pi != "..")) {
|
||||
if (!ZeroTier::OSUtils::mkdir(ptmp)) {
|
||||
DEBUG_ERROR("startOneService(): home path does not exist, and could not create\n");
|
||||
DEBUG_ERROR("home path does not exist, and could not create");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "start_service(): homeDir is empty, could not construct path\n");
|
||||
fprintf(stderr, "homeDir is empty, could not construct path");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -433,7 +512,7 @@ void *zts_start_service(void *thread_id) {
|
||||
|
||||
//chdir(current_dir); // Return to previous current working directory (at the request of Unity3D)
|
||||
#if defined(__UNITY_3D__)
|
||||
Debug("Starting service...\n");
|
||||
DEBUG_INFO("starting service...");
|
||||
#endif
|
||||
|
||||
// Initialize RPC
|
||||
@@ -453,7 +532,7 @@ void *zts_start_service(void *thread_id) {
|
||||
case ZeroTier::OneService::ONE_NORMAL_TERMINATION:
|
||||
break;
|
||||
case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR:
|
||||
fprintf(stderr,"start_service(): fatal error: %s",zt1Service->fatalErrorMessage().c_str());
|
||||
DEBUG_ERROR("fatal error: %s",zt1Service->fatalErrorMessage().c_str());
|
||||
break;
|
||||
case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
|
||||
delete zt1Service;
|
||||
|
||||
@@ -78,7 +78,7 @@
|
||||
char *api_netpath = (char *)0;
|
||||
void load_symbols();
|
||||
void load_symbols_rpc();
|
||||
//int (*realclose)(CLOSE_SIG);
|
||||
int (*realclose)(CLOSE_SIG);
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ---------------------------------- zt_init_rpc -------------------------------
|
||||
@@ -87,7 +87,7 @@ void load_symbols_rpc();
|
||||
// Assembles (and/or) sets the RPC path for communication with the ZeroTier service
|
||||
void zts_init_rpc(const char *path, const char *nwid)
|
||||
{
|
||||
DEBUG_EXTRA("zt_init_rpc\n");
|
||||
DEBUG_EXTRA();
|
||||
#if !defined(__IOS__)
|
||||
// Since we don't use function interposition in iOS
|
||||
if(!realconnect) {
|
||||
@@ -112,10 +112,10 @@ void load_symbols_rpc();
|
||||
// This is used when you're dynamically-linking our library into your application at runtime
|
||||
if (!api_netpath) {
|
||||
api_netpath = getenv("ZT_NC_NETWORK");
|
||||
DEBUG_INFO("$ZT_NC_NETWORK(len=%d) = %s\n", strlen(api_netpath), api_netpath);
|
||||
DEBUG_INFO("$ZT_NC_NETWORK(len=%d)=%s", strlen(api_netpath), api_netpath);
|
||||
}
|
||||
#endif
|
||||
DEBUG_EXTRA("zt_init_rpc(): api_netpath = %s\n", api_netpath);
|
||||
DEBUG_EXTRA("api_netpath=%s", api_netpath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ void load_symbols_rpc();
|
||||
ssize_t zts_sendto(SENDTO_SIG) // Used as internal implementation
|
||||
#endif
|
||||
{
|
||||
DEBUG_EXTRA("zt_sendto(%d, ...)\n", fd);
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if(len > ZT_UDP_DEFAULT_PAYLOAD_MTU) {
|
||||
errno = EMSGSIZE; // Msg is too large
|
||||
return -1;
|
||||
@@ -307,7 +307,7 @@ void load_symbols_rpc();
|
||||
ssize_t zts_recvmsg(RECVMSG_SIG)
|
||||
#endif
|
||||
{
|
||||
DEBUG_EXTRA("zt_recvmsg(%d)\n", fd);
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
ssize_t err, n, tot_len = 0;
|
||||
char *buf, *p;
|
||||
struct iovec *iov = msg->msg_iov;
|
||||
@@ -402,7 +402,7 @@ void load_symbols_rpc();
|
||||
int zts_setsockopt(SETSOCKOPT_SIG)
|
||||
#endif
|
||||
{
|
||||
DEBUG_INFO("zt_setsockopt(%d)\n", fd);
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -424,7 +424,7 @@ void load_symbols_rpc();
|
||||
int zts_getsockopt(GETSOCKOPT_SIG)
|
||||
#endif
|
||||
{
|
||||
DEBUG_INFO("zt_getsockopt(%d)\n", fd);
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
if(optname == SO_TYPE) {
|
||||
int* val = (int*)optval;
|
||||
*val = 2;
|
||||
@@ -487,9 +487,9 @@ void load_symbols_rpc();
|
||||
#endif
|
||||
#endif
|
||||
// -1 is passed since we we're generating the new socket in this call
|
||||
DEBUG_INFO("api_netpath=%s\n", api_netpath);
|
||||
DEBUG_INFO("api_netpath=%s", api_netpath);
|
||||
int err = rpc_send_command(api_netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st));
|
||||
DEBUG_INFO("err=%d\n", err);
|
||||
DEBUG_INFO("err=%d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -502,7 +502,7 @@ void load_symbols_rpc();
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) {
|
||||
struct sockaddr_in addr;
|
||||
const char *str = (*env)->GetStringUTFChars(env, addrstr, 0);
|
||||
DEBUG_INFO("zt_connect(): fd = %d\naddr = %s\nport=%d", fd, str, port);
|
||||
DEBUG_INFO("fd=%d, addr=%s, port=%d", fd, str, port);
|
||||
addr.sin_addr.s_addr = inet_addr(str);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons( port );
|
||||
@@ -518,7 +518,7 @@ void load_symbols_rpc();
|
||||
#endif
|
||||
{
|
||||
get_api_netpath();
|
||||
DEBUG_INFO("zt_connect(%d)\n", fd);
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
struct connect_st rpc_st;
|
||||
#if defined(__linux__)
|
||||
#if !defined(__ANDROID__)
|
||||
@@ -542,7 +542,7 @@ void load_symbols_rpc();
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_ZTSDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) {
|
||||
struct sockaddr_in addr;
|
||||
const char *str = (*env)->GetStringUTFChars(env, addrstr, 0);
|
||||
DEBUG_INFO("zt_bind(): fd = %d\naddr = %s\nport=%d", fd, str, port);
|
||||
DEBUG_INFO("fd=%d, addr=%s, port=%d", fd, str, port);
|
||||
addr.sin_addr.s_addr = inet_addr(str);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons( port );
|
||||
@@ -600,7 +600,7 @@ void load_symbols_rpc();
|
||||
#endif
|
||||
{
|
||||
get_api_netpath();
|
||||
DEBUG_INFO("zt_accept4(%d):\n", fd);
|
||||
DEBUG_INFO("fd=%d", fd);
|
||||
#if !defined(__ANDROID__)
|
||||
if ((flags & SOCK_CLOEXEC))
|
||||
fcntl(fd, F_SETFL, FD_CLOEXEC);
|
||||
|
||||
@@ -35,29 +35,38 @@
|
||||
// ZEROTIER CONTROLS
|
||||
// Starts a ZeroTier service at the specified path
|
||||
// This will only support SOCKS5 Proxy
|
||||
extern "C" void zt_start_service(const char * path) {
|
||||
extern "C" void zt_start_service(const char *path, const char *nwid) {
|
||||
//zts_start_service(path);
|
||||
init_service(INTERCEPT_DISABLED, path);
|
||||
}
|
||||
// Starts a ZeroTier service at the specified path and initializes the RPC mechanism
|
||||
// This will allow direct API calls
|
||||
extern "C" void zt_start_service_and_rpc(const char * path, const char * nwid) {
|
||||
extern "C" void zt_start_service_and_rpc(const char *path, const char *nwid) {
|
||||
init_service_and_rpc(INTERCEPT_DISABLED, path, nwid);
|
||||
}
|
||||
//
|
||||
extern "C" void zt_stop_service() {
|
||||
zts_stop_service();
|
||||
}
|
||||
//
|
||||
extern "C" bool zt_service_is_running() {
|
||||
return zts_service_is_running();
|
||||
}
|
||||
// Joins a ZeroTier virtual network
|
||||
extern "C" void zt_join_network(const char * nwid) {
|
||||
extern "C" void zt_join_network(const char *nwid) {
|
||||
zts_join_network(nwid);
|
||||
}
|
||||
// Leaves a ZeroTier virtual network
|
||||
extern "C" void zt_leave_network(const char * nwid) {
|
||||
extern "C" void zt_leave_network(const char *nwid) {
|
||||
zts_leave_network(nwid);
|
||||
}
|
||||
// Returns a list of addresses associated with this device on the given network
|
||||
extern "C" void zt_get_addresses(const char * nwid, char * addrstr) {
|
||||
extern "C" void zt_get_addresses(const char *nwid, char *addrstr) {
|
||||
zts_get_addresses(nwid, addrstr);
|
||||
}
|
||||
|
||||
|
||||
// PROXY SERVER CONTROLS
|
||||
//
|
||||
extern "C" void zt_start_proxy_server(const char *homepath, const char *nwid, struct sockaddr_storage *addr) {
|
||||
zts_start_proxy_server(homepath, nwid, addr);
|
||||
@@ -76,10 +85,10 @@ extern "C" void zt_get_proxy_server_address(const char *nwid, struct sockaddr_st
|
||||
}
|
||||
// Explicit ZT API wrappers
|
||||
#if !defined(__IOS__)
|
||||
// This isn't available for iOS since function interposition isn't as reliable
|
||||
extern "C" void zt_init_rpc(const char *path, const char *nwid) {
|
||||
zts_init_rpc(path, nwid);
|
||||
}
|
||||
// This isn't available for iOS since function interposition isn't as reliable
|
||||
extern "C" void zt_init_rpc(const char *path, const char *nwid) {
|
||||
zts_init_rpc(path, nwid);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user