Android API updates, address-related bugfixes, file leak fix
This commit is contained in:
@@ -46,6 +46,17 @@ public class SDK {
|
||||
public static int SOCK_STREAM = 1;
|
||||
public static int SOCK_DGRAM = 2;
|
||||
|
||||
// fcntl flags
|
||||
public static int O_APPEND = 1024;
|
||||
public static int O_NONBLOCK = 2048;
|
||||
public static int O_ASYNC = 8192;
|
||||
public static int O_DIRECT = 65536;
|
||||
public static int O_NOATIME = 262144;
|
||||
|
||||
// fcntl cmds
|
||||
public static int F_GETFL = 3;
|
||||
public static int F_SETFL = 4;
|
||||
|
||||
// Loads JNI code
|
||||
static { System.loadLibrary("ZeroTierOneJNI"); }
|
||||
|
||||
@@ -65,9 +76,25 @@ public class SDK {
|
||||
zt_leave_network(nwid);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ------------------------------- get_addresses() ------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
public native ArrayList<String> zt_get_addresses(String nwid);
|
||||
public ArrayList<String> get_addresses(String nwid) {
|
||||
return zt_get_addresses(nwid);
|
||||
int err = -1;
|
||||
ArrayList<String> addresses;
|
||||
while (err < 0) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
addresses = zt_get_addresses(nwid);
|
||||
if (addresses.size() > 0) {
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public native int zt_get_proxy_port(String nwid);
|
||||
@@ -104,7 +131,7 @@ public class SDK {
|
||||
public int connect(int sock, String addr, int port, String nwid)
|
||||
{
|
||||
int err = -1;
|
||||
ArrayList<String> addresses = new ArrayList<String>();
|
||||
ArrayList<String> addresses;
|
||||
while (err < 0) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
@@ -131,7 +158,7 @@ public class SDK {
|
||||
}
|
||||
public int bind(int sock, String addr, int port, String nwid) {
|
||||
int err = -1;
|
||||
ArrayList<String> addresses = new ArrayList<String>();
|
||||
ArrayList<String> addresses;
|
||||
while (err < 0) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
@@ -213,6 +240,15 @@ public class SDK {
|
||||
return zt_sendto(fd,buf,len,flags,addr);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ----------------------------------- send() -----------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
public native int zt_send(int fd, byte[] buf, int len, int flags);
|
||||
public int send(int fd, byte[] buf, int len, int flags) {
|
||||
return zt_send(fd, buf, len, flags);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ---------------------------------- recvfrom() --------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
@@ -222,6 +258,17 @@ public class SDK {
|
||||
return zt_recvfrom(fd,buf,len,flags,addr);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ---------------------------------- recvfrom() --------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
public native int zt_fcntl(int sock, int cmd, int flag);
|
||||
public int fcntl(int sock, int cmd, int flag) {
|
||||
return zt_fcntl(sock, F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//public static native int zt_getsockopt(int fd, int type, int protocol);
|
||||
//public static native int zt_setsockopt(int fd, int type, int protocol);
|
||||
//public static native int zt_getsockname(int fd, int type, int protocol);
|
||||
|
||||
@@ -4,20 +4,23 @@ import java.math.BigInteger;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ZTAddress
|
||||
{
|
||||
// int -> byte array
|
||||
static public byte[] toIPByteArray(long addr){
|
||||
return new byte[]{(byte)addr,(byte)(addr>>>8),(byte)(addr>>>16),(byte)(addr>>>24)};
|
||||
}
|
||||
|
||||
int pack(byte[] bytes) {
|
||||
int val = 0;
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
val <<= 8;
|
||||
val |= bytes[i] & 0xff;
|
||||
// byte array -> int
|
||||
long toIPInt(String _addr) {
|
||||
long result = 0;
|
||||
for(String part: _addr.split(Pattern.quote("."))) {
|
||||
result = result << 8;
|
||||
result |= Integer.parseInt(part);
|
||||
}
|
||||
return val;
|
||||
return result;
|
||||
}
|
||||
|
||||
public int port;
|
||||
@@ -42,18 +45,20 @@ public class ZTAddress
|
||||
|
||||
public ZTAddress()
|
||||
{
|
||||
|
||||
port = -1;
|
||||
_rawAddr = -1;
|
||||
}
|
||||
|
||||
public ZTAddress(String _addr, int _port)
|
||||
{
|
||||
_rawAddr = toIPInt(_addr);
|
||||
port = _port;
|
||||
_rawAddr = pack(_addr.getBytes());
|
||||
}
|
||||
|
||||
public void ZTAddress(InetSocketAddress ins)
|
||||
{
|
||||
|
||||
port = ins.getPort();
|
||||
_rawAddr = toIPInt(ins.getAddress().getHostAddress());
|
||||
}
|
||||
|
||||
public InetSocketAddress ToInetSocketAddress() throws IllegalArgumentException {
|
||||
@@ -65,4 +70,8 @@ public class ZTAddress
|
||||
}
|
||||
return sock_addr;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return port != -1 && !Address().startsWith("-1.-1.-1.-1/-1");
|
||||
}
|
||||
}
|
||||
@@ -175,36 +175,42 @@ public class MainActivity extends AppCompatActivity {
|
||||
int sock = zt.socket(SDK.AF_INET, SDK.SOCK_DGRAM, 0);
|
||||
|
||||
Log.d("TEST", "binding...");
|
||||
err = zt.bind(sock, bindAddr, nwid);
|
||||
if (err < 0)
|
||||
if((err = zt.bind(sock, bindAddr, nwid)) < 0)
|
||||
Log.d("TEST", "bind_err = " + err + "\n");
|
||||
|
||||
err = zt.listen(sock, 0);
|
||||
if(err < 0)
|
||||
if((err = zt.listen(sock, 0)) < 0)
|
||||
Log.d("TEST", "listen_err = " + err);
|
||||
ArrayList<String> addresses = zt.get_addresses(nwid);
|
||||
if(addresses.size() < 0) {
|
||||
Log.d("TEST", "unable to obtain ZT address");
|
||||
return;
|
||||
}
|
||||
else {
|
||||
Log.d("TEST", "IPV4 = " + addresses.get(0));
|
||||
}
|
||||
|
||||
String bufStr = "";
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
zt.fcntl(sock, zt.F_SETFL, zt.O_NONBLOCK);
|
||||
|
||||
// ECHO
|
||||
while(true) {
|
||||
bufStr = "";
|
||||
|
||||
while(err >= 0) {
|
||||
// RX
|
||||
byte[] buffer = new byte[32];
|
||||
String addr_string = "-1.-1.-1.-1";
|
||||
int port_no = -1;
|
||||
|
||||
err = zt.recvfrom(sock, buffer, 32, 0, remoteServer);
|
||||
String bufStr = new String(buffer).substring(0, err);
|
||||
if((err = zt.recvfrom(sock, buffer, 32, 0, remoteServer)) > 0) {
|
||||
bufStr = new String(buffer).substring(0, err);
|
||||
Log.d("TEST", "read (" + err + ") bytes from " + remoteServer.Address() + " : " + remoteServer.Port() + ", msg = " + bufStr);
|
||||
|
||||
// TX
|
||||
|
||||
if(err > 0) {
|
||||
String msg = "Welcome response from android";
|
||||
String msg = "Welcome response from android\n";
|
||||
err = zt.sendto(sock, msg.getBytes(), msg.length(), 0, remoteServer);
|
||||
if (err < 0)
|
||||
Log.d("TEST", "sendto_err = " + err);
|
||||
}
|
||||
|
||||
}
|
||||
Log.d("TEST", "leaving network");
|
||||
zt.leave_network(nwid);
|
||||
//Log.d("TEST", "leaving network");
|
||||
//zt.leave_network(nwid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,7 @@ int zts_listen(LISTEN_SIG);
|
||||
int zts_setsockopt(SETSOCKOPT_SIG);
|
||||
int zts_getsockopt(GETSOCKOPT_SIG);
|
||||
int zts_getsockname(GETSOCKNAME_SIG);
|
||||
int zts_getpeername(GETPEERNAME_SIG);
|
||||
int zts_close(CLOSE_SIG);
|
||||
|
||||
ssize_t zts_sendto(SENDTO_SIG);
|
||||
@@ -123,9 +124,15 @@ ssize_t zts_recvmsg(RECVMSG_SIG);
|
||||
// TCP
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1write(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len);
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1read(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len);
|
||||
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags);
|
||||
|
||||
// UDP
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1sendto(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr);
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1recvfrom(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr);
|
||||
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1fcntl(JNIEnv *env, jobject thisObj, jint socket, jint cmd, jint flags);
|
||||
|
||||
// Takes a given numerical file descriptor and manufactures a java FileDescriptor object for use in javaland
|
||||
JNIEXPORT jobject JNICALL Java_ZeroTier_SDK_zt_1getFileDescriptor(JNIEnv *env, jobject thisObj, jint fd);
|
||||
#endif
|
||||
|
||||
@@ -46,10 +46,10 @@
|
||||
#define SDK_DEBUG_H
|
||||
|
||||
// Set during make (e.g. make SDK_DEBUG=2)
|
||||
#define DEBUG_LEVEL 4
|
||||
#define DEBUG_LEVEL 2
|
||||
|
||||
#define MSG_TRANSFER 1 // RX/TX specific statements
|
||||
#define MSG_ERROR 2 // Errors
|
||||
#define MSG_ERROR 1 // Errors
|
||||
#define MSG_TRANSFER 2 // RX/TX specific statements
|
||||
#define MSG_INFO 3 // Information which is generally useful to any user
|
||||
#define MSG_DEBUG 4 // Information which is only useful to someone debugging
|
||||
#define MSG_DEBUG_EXTRA 5 // If nothing in your world makes sense
|
||||
|
||||
@@ -26,10 +26,10 @@
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#define DEBUG_LEVEL 4 // Set this to adjust what you'd like to see in the debug traces
|
||||
#define DEBUG_LEVEL 2 // Set this to adjust what you'd like to see in the debug traces
|
||||
|
||||
#define MSG_TRANSFER 1 // RX/TX specific statements
|
||||
#define MSG_ERROR 2 // Errors
|
||||
#define MSG_ERROR 1 // Errors
|
||||
#define MSG_TRANSFER 2 // RX/TX specific statements
|
||||
#define MSG_INFO 3 // Information which is generally useful to any user
|
||||
#define MSG_DEBUG 4 // Information which is only useful to someone debugging
|
||||
#define MSG_DEBUG_EXTRA 5 // If nothing in your world makes sense
|
||||
|
||||
@@ -306,7 +306,6 @@ void NetconEthernetTap::threadMain()
|
||||
throw()
|
||||
{
|
||||
uint64_t prev_tcp_time = 0, prev_status_time = 0, prev_etharp_time = 0;
|
||||
|
||||
// Main timer loop
|
||||
while (_run) {
|
||||
|
||||
@@ -438,7 +437,7 @@ void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr) {
|
||||
|
||||
void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwip_invoked)
|
||||
{
|
||||
dwr(MSG_DEBUG,"processReceivedData(sock=%p): lwip_invoked = %d\n",
|
||||
dwr(MSG_DEBUG_EXTRA,"processReceivedData(sock=%p): lwip_invoked = %d\n",
|
||||
(void*)&sock, lwip_invoked);
|
||||
if(!lwip_invoked) {
|
||||
_tcpconns_m.lock();
|
||||
@@ -452,9 +451,22 @@ void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwi
|
||||
memcpy(conn->rxbuf, conn->rxbuf+n, conn->rxsz-n);
|
||||
conn->rxsz -= n;
|
||||
if(conn->type==SOCK_DGRAM){
|
||||
conn->unread_udp_packet = false;
|
||||
_phy.setNotifyWritable(conn->sock, false);
|
||||
dwr(MSG_TRANSFER,"UDP RX <--- :: {TX: ------, RX: ------, sock=%x} :: %d bytes\n", conn->sock, n);
|
||||
|
||||
#if DEBUG_LEVEL >= MSG_TRANSFER
|
||||
struct sockaddr_in * addr_in2 = (struct sockaddr_in *)conn->peer_addr;
|
||||
int port = lwipstack->__lwip_ntohs(addr_in2->sin_port);
|
||||
int ip = addr_in2->sin_addr.s_addr;
|
||||
unsigned char d[4];
|
||||
d[0] = ip & 0xFF;
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
|
||||
dwr(MSG_TRANSFER,"UDP RX <--- :: {TX: ------, RX: ------, sock=%x} :: %d bytes (%d.%d.%d.%d:%d)\n",
|
||||
conn->sock, n, d[0],d[1],d[2],d[3], port);
|
||||
#endif
|
||||
conn->unread_udp_packet = false;
|
||||
}
|
||||
//dwr(MSG_DEBUG, "phyOnUnixWritable(): tid = %d\n", pthread_mach_thread_np(pthread_self()));
|
||||
if(conn->type==SOCK_STREAM) { // Only acknolwedge receipt of TCP packets
|
||||
@@ -464,7 +476,7 @@ void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwi
|
||||
(float)conn->txsz / max, (float)conn->rxsz / max, conn->sock, n);
|
||||
}
|
||||
} else {
|
||||
dwr(MSG_DEBUG," processReceivedData(): errno = %d, rxsz = %d\n", errno, conn->rxsz);
|
||||
dwr(MSG_DEBUG_EXTRA," processReceivedData(): errno = %d, rxsz = %d\n", errno, conn->rxsz);
|
||||
_phy.setNotifyWritable(conn->sock, false);
|
||||
}
|
||||
}
|
||||
@@ -481,7 +493,7 @@ void NetconEthernetTap::processReceivedData(PhySocket *sock,void **uptr,bool lwi
|
||||
|
||||
void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_invoked)
|
||||
{
|
||||
dwr(MSG_DEBUG," phyOnUnixWritable(sock=%p): lwip_invoked = %d\n", (void*)&sock, lwip_invoked);
|
||||
dwr(MSG_DEBUG_EXTRA," phyOnUnixWritable(sock=%p): lwip_invoked = %d\n", (void*)&sock, lwip_invoked);
|
||||
processReceivedData(sock,uptr,lwip_invoked);
|
||||
}
|
||||
|
||||
@@ -509,7 +521,7 @@ 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);
|
||||
dwr(MSG_DEBUG," <sock=%p> RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)\n",
|
||||
dwr(MSG_DEBUG_EXTRA," <sock=%p> RPC: (pid=%d, tid=%d, timestamp=%s, cmd=%d)\n",
|
||||
(void*)&sock, pid, tid, timestamp, cmd);
|
||||
|
||||
if(cmd == RPC_SOCKET) {
|
||||
@@ -625,6 +637,12 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data,
|
||||
memcpy(&getsockname_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct getsockname_st));
|
||||
handleGetsockname(sock, rpcSock, uptr, &getsockname_rpc);
|
||||
break;
|
||||
case RPC_GETPEERNAME:
|
||||
dwr(MSG_DEBUG," <sock=%p> RPC_GETPEERNAME\n", (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:
|
||||
dwr(MSG_DEBUG," <sock=%p> RPC_CONNECT\n", (void*)&sock);
|
||||
struct connect_st connect_rpc;
|
||||
@@ -732,21 +750,26 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
|
||||
void NetconEthernetTap::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, struct ip_addr * addr, u16_t port)
|
||||
{
|
||||
Larg *l = (Larg*)arg;
|
||||
dwr(MSG_DEBUG, "nc_udp_recved(conn=%p,pcb=%p,port=%d)\n", (void*)&(l->conn), (void*)&upcb, port);
|
||||
dwr(MSG_DEBUG_EXTRA, "nc_udp_recved(conn=%p,pcb=%p,port=%d)\n", (void*)&(l->conn), (void*)&upcb, port);
|
||||
int tot = 0;
|
||||
struct pbuf* q = p;
|
||||
Mutex::Lock _l2(l->tap->_rx_buf_m);
|
||||
// Cycle through pbufs and write them to the RX buffer
|
||||
// The RX "buffer" will be emptied via phyOnUnixWritable()
|
||||
if(l->conn->unread_udp_packet) {
|
||||
dwr(MSG_DEBUG, "nc_udp_recved(): dropping packet\n");
|
||||
l->tap->lwipstack->__pbuf_free(q);
|
||||
return;
|
||||
}
|
||||
if(p) {
|
||||
// assign provided address info to "connection"
|
||||
struct sockaddr_in addr_in;
|
||||
addr_in.sin_addr.s_addr = addr->addr;
|
||||
addr_in.sin_port = port;
|
||||
l->conn->peer_addr = (struct sockaddr_storage*)&addr_in;
|
||||
// reset buffer contents
|
||||
l->conn->rxsz = 0;
|
||||
memset(l->conn->rxbuf, 0, DEFAULT_UDP_RX_BUF_SZ);
|
||||
// Add address info beginning of data buffer
|
||||
u32_t raw_addr = ip4_addr_get_u32(addr);
|
||||
memcpy(l->conn->rxbuf + (l->conn->rxsz), &raw_addr, sizeof(u32_t)); // addr
|
||||
l->conn->rxsz += sizeof(u32_t);
|
||||
memcpy(l->conn->rxbuf + (l->conn->rxsz), &port, sizeof(u16_t)); // port
|
||||
l->conn->rxsz += sizeof(u16_t);
|
||||
}
|
||||
while(p != NULL) {
|
||||
if(p->len <= 0)
|
||||
@@ -758,7 +781,7 @@ void NetconEthernetTap::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct
|
||||
tot += len;
|
||||
}
|
||||
if(tot) {
|
||||
dwr(MSG_DEBUG, " nc_udp_recved(): data_len = %d, rxsz = %d, addr_info_len = %d\n",
|
||||
dwr(MSG_DEBUG_EXTRA, " nc_udp_recved(): data_len = %d, rxsz = %d, addr_info_len = %d\n",
|
||||
tot, l->conn->rxsz, sizeof(u32_t) + sizeof(u16_t));
|
||||
l->conn->unread_udp_packet = true;
|
||||
l->tap->phyOnUnixWritable(l->conn->sock, NULL, true);
|
||||
@@ -771,7 +794,7 @@ 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;
|
||||
dwr(MSG_DEBUG, "nc_recved(conn=%p,pcb=%p)\n", (void*)&(l->conn), (void*)&PCB);
|
||||
dwr(MSG_DEBUG_EXTRA, "nc_recved(conn=%p,pcb=%p)\n", (void*)&(l->conn), (void*)&PCB);
|
||||
int tot = 0;
|
||||
struct pbuf* q = p;
|
||||
Mutex::Lock _l(l->tap->_tcpconns_m);
|
||||
@@ -815,7 +838,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)
|
||||
{
|
||||
dwr(MSG_DEBUG, "nc_sent(pcb=%p)\n", (void*)&PCB);
|
||||
dwr(MSG_DEBUG_EXTRA, "nc_sent(pcb=%p)\n", (void*)&PCB);
|
||||
Larg *l = (Larg*)arg;
|
||||
Mutex::Lock _l(l->tap->_tcpconns_m);
|
||||
if(l->conn->probation && l->conn->txsz == 0){
|
||||
@@ -936,7 +959,30 @@ void NetconEthernetTap::handleGetsockname(PhySocket *sock, PhySocket *rpcSock, v
|
||||
{
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
Connection *conn = getConnection(sock);
|
||||
write(_phy.getDescriptor(rpcSock), &conn->addr, sizeof(struct sockaddr_storage));
|
||||
|
||||
if(conn->addr == NULL){
|
||||
dwr(MSG_DEBUG_EXTRA," handleGetsockname(): 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));
|
||||
return;
|
||||
}
|
||||
write(_phy.getDescriptor(rpcSock), conn->addr, sizeof(struct sockaddr_storage));
|
||||
}
|
||||
|
||||
void NetconEthernetTap::handleGetpeername(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct getsockname_st *getsockname_rpc)
|
||||
{
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
Connection *conn = getConnection(sock);
|
||||
|
||||
if(conn->peer_addr == NULL){
|
||||
dwr(MSG_DEBUG_EXTRA," handleGetpeername(): 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));
|
||||
return;
|
||||
}
|
||||
write(_phy.getDescriptor(rpcSock), conn->peer_addr, sizeof(struct sockaddr_storage));
|
||||
}
|
||||
|
||||
void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct bind_st *bind_rpc)
|
||||
@@ -951,6 +997,15 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
|
||||
sendReturnValue(rpcSock, -1, ENOMEM);
|
||||
return;
|
||||
}
|
||||
|
||||
int ip = rawAddr->sin_addr.s_addr;
|
||||
unsigned char d[4];
|
||||
d[0] = ip & 0xFF;
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
dwr(MSG_DEBUG," handleBind(): %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port);
|
||||
|
||||
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);
|
||||
@@ -976,14 +1031,6 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
|
||||
else if (conn->type == SOCK_STREAM) {
|
||||
if(conn->TCP_pcb->state == CLOSED){
|
||||
err = lwipstack->__tcp_bind(conn->TCP_pcb, &connAddr, port);
|
||||
int ip = rawAddr->sin_addr.s_addr;
|
||||
unsigned char d[4];
|
||||
d[0] = ip & 0xFF;
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
dwr(MSG_DEBUG," handleBind(): %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port);
|
||||
|
||||
if(err != ERR_OK) {
|
||||
dwr(MSG_ERROR," handleBind(): err = %d\n", err);
|
||||
if(err == ERR_USE)
|
||||
@@ -1071,6 +1118,8 @@ Connection * NetconEthernetTap::handleSocketProxy(PhySocket *sock, int socket_ty
|
||||
if(new_udp_PCB || new_tcp_PCB) {
|
||||
conn->sock = sock;
|
||||
conn->type = socket_type;
|
||||
conn->addr = NULL;
|
||||
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;
|
||||
dwr(MSG_DEBUG, " handleSocketProxy(): Updated sock=%p\n", (void*)&sock);
|
||||
@@ -1100,6 +1149,8 @@ Connection * NetconEthernetTap::handleSocket(PhySocket *sock, void **uptr, struc
|
||||
*uptr = newConn;
|
||||
newConn->type = socket_rpc->socket_type;
|
||||
newConn->sock = sock;
|
||||
newConn->addr = NULL;
|
||||
newConn->peer_addr = NULL;
|
||||
if(newConn->type == SOCK_DGRAM) newConn->UDP_pcb = new_udp_PCB;
|
||||
if(newConn->type == SOCK_STREAM) newConn->TCP_pcb = new_tcp_PCB;
|
||||
_Connections.push_back(newConn);
|
||||
@@ -1208,7 +1259,7 @@ int NetconEthernetTap::handleConnectProxy(PhySocket *sock, struct sockaddr_in *r
|
||||
|
||||
void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Connection *conn, struct connect_st* connect_rpc)
|
||||
{
|
||||
dwr(MSG_DEBUG, "handleConnect(%p)\n", (void*)&sock);
|
||||
dwr(MSG_DEBUG_EXTRA, "handleConnect(%p)\n", (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);
|
||||
@@ -1225,7 +1276,7 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne
|
||||
if(conn->type == SOCK_DGRAM) {
|
||||
// Generates no network traffic
|
||||
if((err = lwipstack->__udp_connect(conn->UDP_pcb,&connAddr,port)) < 0)
|
||||
dwr(MSG_DEBUG, "handleConnect(): Error while connecting to with UDP\n");
|
||||
dwr(MSG_ERROR, "handleConnect(): Error while connecting to with UDP\n");
|
||||
lwipstack->__udp_recv(conn->UDP_pcb, nc_udp_recved, new Larg(this, conn));
|
||||
sendReturnValue(rpcSock, 0, ERR_OK);
|
||||
return;
|
||||
@@ -1237,7 +1288,7 @@ 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));
|
||||
|
||||
dwr(MSG_DEBUG," handleConnect(): pcb->state = %x\n", conn->TCP_pcb->state);
|
||||
dwr(MSG_DEBUG_EXTRA," handleConnect(): pcb->state = %x\n", conn->TCP_pcb->state);
|
||||
if(conn->TCP_pcb->state != CLOSED) {
|
||||
dwr(MSG_DEBUG," handleConnect(): PCB != CLOSED, cannot connect using this PCB\n");
|
||||
sendReturnValue(rpcSock, -1, EAGAIN);
|
||||
@@ -1298,14 +1349,14 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne
|
||||
|
||||
void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
{
|
||||
dwr(MSG_DEBUG, "handleWrite(conn=%p)\n", (void*)&conn);
|
||||
dwr(MSG_DEBUG_EXTRA, "handleWrite(conn=%p)\n", (void*)&conn);
|
||||
if(!conn || (!conn->TCP_pcb && !conn->UDP_pcb)) {
|
||||
dwr(MSG_ERROR," handleWrite(): invalid connection\n");
|
||||
return;
|
||||
}
|
||||
if(conn->type == SOCK_DGRAM) {
|
||||
if(!conn->UDP_pcb) {
|
||||
dwr(MSG_DEBUG, " handleWrite(): type = SOCK_DGRAM, invalid UDP_pcb\n");
|
||||
dwr(MSG_ERROR, " handleWrite(): type = SOCK_DGRAM, invalid UDP_pcb\n");
|
||||
return;
|
||||
}
|
||||
// TODO: Packet re-assembly hasn't yet been tested with lwIP so UDP packets are limited to MTU-sized chunks
|
||||
@@ -1314,18 +1365,18 @@ void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
dwr(MSG_DEBUG_EXTRA, " handleWrite(): Allocating pbuf chain of size (%d) for UDP packet, TXSZ = %d\n", udp_trans_len, conn->txsz);
|
||||
struct pbuf * pb = lwipstack->__pbuf_alloc(PBUF_TRANSPORT, udp_trans_len, PBUF_POOL);
|
||||
if(!pb){
|
||||
dwr(MSG_DEBUG, " handleWrite(): unable to allocate new pbuf of size (%d)\n", conn->txsz);
|
||||
dwr(MSG_ERROR, " handleWrite(): unable to allocate new pbuf of size (%d)\n", conn->txsz);
|
||||
return;
|
||||
}
|
||||
memcpy(pb->payload, conn->txbuf, udp_trans_len);
|
||||
int err = lwipstack->__udp_send(conn->UDP_pcb, pb);
|
||||
|
||||
if(err == ERR_MEM) {
|
||||
dwr(MSG_DEBUG, " handleWrite(): Error sending packet. Out of memory\n");
|
||||
dwr(MSG_ERROR, " handleWrite(): Error sending packet. Out of memory\n");
|
||||
} else if(err == ERR_RTE) {
|
||||
dwr(MSG_DEBUG, " handleWrite(): Could not find route to destinations address\n");
|
||||
dwr(MSG_ERROR, " handleWrite(): Could not find route to destinations address\n");
|
||||
} else if(err != ERR_OK) {
|
||||
dwr(MSG_DEBUG, " handleWrite(): Error sending packet - %d\n", err);
|
||||
dwr(MSG_ERROR, " handleWrite(): Error sending packet - %d\n", err);
|
||||
} else {
|
||||
// Success
|
||||
int buf_remaining = (conn->txsz)-udp_trans_len;
|
||||
@@ -1333,14 +1384,27 @@ void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
memmove(&conn->txbuf, (conn->txbuf+udp_trans_len), buf_remaining);
|
||||
conn->txsz -= udp_trans_len;
|
||||
int max = conn->type == SOCK_STREAM ? DEFAULT_TCP_TX_BUF_SZ : DEFAULT_UDP_TX_BUF_SZ;
|
||||
dwr(MSG_TRANSFER,"UDP TX ---> :: {TX: ------, RX: ------, sock=%x} :: %d bytes\n", conn->sock, udp_trans_len);
|
||||
|
||||
#if DEBUG_LEVEL >= MSG_TRANSFER
|
||||
struct sockaddr_in * addr_in2 = (struct sockaddr_in *)conn->peer_addr;
|
||||
int port = lwipstack->__lwip_ntohs(addr_in2->sin_port);
|
||||
int ip = addr_in2->sin_addr.s_addr;
|
||||
unsigned char d[4];
|
||||
d[0] = ip & 0xFF;
|
||||
d[1] = (ip >> 8) & 0xFF;
|
||||
d[2] = (ip >> 16) & 0xFF;
|
||||
d[3] = (ip >> 24) & 0xFF;
|
||||
|
||||
dwr(MSG_TRANSFER,"UDP TX ---> :: {TX: ------, RX: ------, sock=%x} :: %d bytes (%d.%d.%d.%d:%d)\n",
|
||||
conn->sock, udp_trans_len, d[0], d[1], d[2], d[3], port);
|
||||
#endif
|
||||
}
|
||||
lwipstack->__pbuf_free(pb);
|
||||
return;
|
||||
}
|
||||
else if(conn->type == SOCK_STREAM) {
|
||||
if(!conn->TCP_pcb) {
|
||||
dwr(MSG_DEBUG, " handleWrite(): type = SOCK_STREAM, invalid TCP_pcb\n");
|
||||
dwr(MSG_ERROR, " handleWrite(): type = SOCK_STREAM, invalid TCP_pcb\n");
|
||||
return;
|
||||
}
|
||||
// How much we are currently allowed to write to the connection
|
||||
@@ -1352,7 +1416,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) {
|
||||
dwr(MSG_DEBUG," handleWrite(): sndbuf == 0, LWIP stack is full\n");
|
||||
dwr(MSG_ERROR," handleWrite(): sndbuf == 0, LWIP stack is full\n");
|
||||
_phy.setNotifyReadable(conn->sock, false);
|
||||
conn->probation = true;
|
||||
}
|
||||
@@ -1373,7 +1437,7 @@ void NetconEthernetTap::handleWrite(Connection *conn)
|
||||
if(err != ERR_OK) {
|
||||
dwr(MSG_ERROR," handleWrite(): error while writing to PCB, (err = %d)\n", err);
|
||||
if(err == -1)
|
||||
dwr(MSG_DEBUG," handleWrite(): out of memory\n");
|
||||
dwr(MSG_ERROR," handleWrite(): out of memory\n");
|
||||
return;
|
||||
} else {
|
||||
sz = (conn->txsz)-r;
|
||||
|
||||
@@ -94,7 +94,8 @@ namespace ZeroTier {
|
||||
PhySocket *rpcSock, *sock;
|
||||
struct tcp_pcb *TCP_pcb;
|
||||
struct udp_pcb *UDP_pcb;
|
||||
struct sockaddr_storage *addr;
|
||||
struct sockaddr_storage *addr; // TODO: Rename
|
||||
struct sockaddr_storage *peer_addr;
|
||||
unsigned short port;
|
||||
unsigned char txbuf[DEFAULT_TCP_TX_BUF_SZ];
|
||||
unsigned char rxbuf[DEFAULT_TCP_RX_BUF_SZ];
|
||||
@@ -403,6 +404,11 @@ namespace ZeroTier {
|
||||
*/
|
||||
void handleGetsockname(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc);
|
||||
|
||||
/*
|
||||
* Return the address of the peer connected to this socket
|
||||
*/
|
||||
void handleGetpeername(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc);
|
||||
|
||||
/*
|
||||
* Writes data from the application's socket to the LWIP connection
|
||||
*/
|
||||
|
||||
@@ -128,7 +128,7 @@ int load_symbols_rpc()
|
||||
int rpc_join(char * sockname)
|
||||
{
|
||||
if(sockname == NULL) {
|
||||
// dwr(MSG_ERROR, "Warning, rpc netpath is NULL\n");
|
||||
fprintf(stderr,"Warning, rpc netpath is NULL\n");
|
||||
}
|
||||
if(!load_symbols_rpc())
|
||||
return -1;
|
||||
@@ -144,17 +144,17 @@ int rpc_join(char * sockname)
|
||||
#else
|
||||
if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
|
||||
#endif
|
||||
// dwr(MSG_ERROR, "Error while creating RPC socket\n");
|
||||
fprintf(stderr,"Error while creating RPC socket\n");
|
||||
return -1;
|
||||
}
|
||||
while((conn_err != 0) && (attempts < SERVICE_CONNECT_ATTEMPTS)){
|
||||
while((conn_err != 0) /* && (attempts < SERVICE_CONNECT_ATTEMPTS) */){
|
||||
#if defined(SDK_INTERCEPT)
|
||||
if((conn_err = realconnect(sock, (struct sockaddr*)&addr, sizeof(addr))) != 0) {
|
||||
#else
|
||||
if((conn_err = connect(sock, (struct sockaddr*)&addr, sizeof(addr))) != 0) {
|
||||
#endif
|
||||
// dwr(MSG_ERROR, "Error while connecting to RPC socket. Re-attempting...\n");
|
||||
sleep(1);
|
||||
fprintf(stderr,"Error while connecting to RPC socket. Re-attempting...\n");
|
||||
usleep(100000);
|
||||
}
|
||||
else
|
||||
return sock;
|
||||
@@ -175,13 +175,15 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
|
||||
memcpy(CANARY+CANARY_SZ, padding, sizeof(padding));
|
||||
uint64_t canary_num;
|
||||
// ephemeral RPC socket used only for this command
|
||||
// TODO: Re-engineer RPC socket model for more efficiency
|
||||
int rpc_sock = rpc_join(path);
|
||||
// Generate token
|
||||
int fdrand = open("/dev/urandom", O_RDONLY);
|
||||
if(read(fdrand, &CANARY, CANARY_SZ) < 0) {
|
||||
// dwr(MSG_ERROR, "unable to read from /dev/urandom for RPC canary data\n");
|
||||
fprintf(stderr,"unable to read from /dev/urandom for RPC canary data\n");
|
||||
return -1;
|
||||
}
|
||||
close(fdrand);
|
||||
memcpy(&canary_num, CANARY, CANARY_SZ);
|
||||
cmdbuf[CMD_ID_IDX] = cmd;
|
||||
memcpy(&cmdbuf[CANARY_IDX], &canary_num, CANARY_SZ);
|
||||
@@ -215,18 +217,20 @@ 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) {
|
||||
// dwr(MSG_ERROR, "Error writing command to service (CMD = %d)\n", cmdbuf[CMD_ID_IDX]);
|
||||
fprintf(stderr,"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) {
|
||||
// dwr(MSG_ERROR, "unable to read RPC ACK byte from service.\n");
|
||||
fprintf(stderr,"unable to read RPC ACK byte from service.\n");
|
||||
close(rpc_sock);
|
||||
return -1;
|
||||
}
|
||||
if(c == 'z' && n_write > 0 && forfd > -1){
|
||||
if(send(forfd, &CANARY, CANARY_SZ+PADDING_SZ, 0) < 0) {
|
||||
perror("send: \n");
|
||||
// dwr(MSG_ERROR, "unable to write canary to stream (fd=%d)\n", forfd);
|
||||
fprintf(stderr,"unable to write canary to stream (fd=%d)\n", forfd);
|
||||
close(rpc_sock);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -242,7 +246,7 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
|
||||
|| cmdbuf[CMD_ID_IDX]==RPC_LISTEN) {
|
||||
ret = get_retval(rpc_sock);
|
||||
}
|
||||
if(cmdbuf[CMD_ID_IDX]==RPC_GETSOCKNAME) {
|
||||
if(cmdbuf[CMD_ID_IDX]==RPC_GETSOCKNAME || cmdbuf[CMD_ID_IDX]==RPC_GETPEERNAME) {
|
||||
pthread_mutex_unlock(&lock);
|
||||
return rpc_sock; // Don't close rpc here, we'll use it to read getsockopt_st
|
||||
}
|
||||
@@ -323,11 +327,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) {
|
||||
// dwr(MSG_ERROR, "invalid cmsg_level %d\n",cmsg->cmsg_level);
|
||||
fprintf(stderr,"invalid cmsg_level %d\n",cmsg->cmsg_level);
|
||||
return -1;
|
||||
}
|
||||
if (cmsg->cmsg_type != SCM_RIGHTS) {
|
||||
// dwr(MSG_ERROR, "invalid cmsg_type %d\n",cmsg->cmsg_type);
|
||||
fprintf(stderr,"invalid cmsg_type %d\n",cmsg->cmsg_type);
|
||||
return -1;
|
||||
}
|
||||
*fd = *((int *) CMSG_DATA(cmsg));
|
||||
@@ -336,7 +340,7 @@ ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
|
||||
} else {
|
||||
size = read (sock, buf, bufsize);
|
||||
if (size < 0) {
|
||||
// dwr(MSG_ERROR, "sock_fd_read(): read: Error\n");
|
||||
fprintf(stderr,"sock_fd_read(): read: Error\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +65,10 @@
|
||||
#define RPC_SOCKET 9
|
||||
#define RPC_SHUTDOWN 10
|
||||
#define RPC_GETSOCKNAME 11
|
||||
#define RPC_RETVAL 12
|
||||
#define RPC_IS_CONNECTED 13
|
||||
#define RPC_GETPEERNAME 12
|
||||
#define RPC_RETVAL 13
|
||||
#define RPC_IS_CONNECTED 14
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
#define SENDMSG_SIG int socket, const struct msghdr *message, int flags
|
||||
#define SENDTO_SIG int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addr_len
|
||||
#define RECV_SIG int socket, void *buffer, size_t length, int flags
|
||||
#define RECVFROM_SIG int socket, void * buffer, size_t length, int flags, struct sockaddr_in * __restrict address, socklen_t * __restrict address_len
|
||||
#define RECVFROM_SIG int socket, void * buffer, size_t length, int flags, struct sockaddr_in * address, socklen_t * address_len
|
||||
#define RECVMSG_SIG int socket, struct msghdr *message,int flags
|
||||
|
||||
#define SEND_SIG int socket, const void *buffer, size_t length, int flags
|
||||
@@ -52,6 +52,8 @@
|
||||
#define ACCEPT_SIG int sockfd, struct sockaddr *addr, socklen_t *addrlen
|
||||
#define CLOSE_SIG int fd
|
||||
#define GETSOCKNAME_SIG int sockfd, struct sockaddr *addr, socklen_t *addrlen
|
||||
#define GETPEERNAME_SIG int sockfd, struct sockaddr *addr, socklen_t *addrlen
|
||||
#define FCNTL_SIG int fd, int cmd, int flags
|
||||
#define SYSCALL_SIG long number, ...
|
||||
|
||||
#endif // _SDK_SIGNATURES_H
|
||||
@@ -88,7 +88,7 @@ int (*realclose)(CLOSE_SIG);
|
||||
// Assembles (and/or) sets the RPC path for communication with the ZeroTier service
|
||||
void zt_init_rpc(const char *path, const char *nwid)
|
||||
{
|
||||
dwr(MSG_DEBUG, "zt_init_rpc\n");
|
||||
// dwr(MSG_DEBUG_EXTRA, "zt_init_rpc\n");
|
||||
#if !defined(__IOS__)
|
||||
// Since we don't use function interposition in iOS
|
||||
if(!realconnect) {
|
||||
@@ -97,6 +97,7 @@ int (*realclose)(CLOSE_SIG);
|
||||
#endif
|
||||
// If no path, construct one or get it fron system env vars
|
||||
if(!api_netpath) {
|
||||
rpc_mutex_init();
|
||||
#if defined(SDK_BUNDLED)
|
||||
// Get the path/nwid from the user application
|
||||
// netpath = [path + "/nc_" + nwid]
|
||||
@@ -115,12 +116,29 @@ int (*realclose)(CLOSE_SIG);
|
||||
dwr(MSG_DEBUG, "$ZT_NC_NETWORK = %s\n", api_netpath);
|
||||
}
|
||||
#endif
|
||||
dwr(MSG_DEBUG_EXTRA, "zt_init_rpc(): api_netpath = %s\n", api_netpath);
|
||||
}
|
||||
dwr(MSG_DEBUG, "zt_init_rpc(): api_netpath = %s\n", api_netpath);
|
||||
}
|
||||
|
||||
void get_api_netpath() { zt_init_rpc("",""); }
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ------------------------------------ send() ----------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
// int sockfd, const void *buf, size_t len
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags)
|
||||
{
|
||||
jbyte *body = (*env)->GetByteArrayElements(env, buf, 0);
|
||||
char * bufp = (char *)malloc(sizeof(char)*len);
|
||||
memcpy(bufp, body, len);
|
||||
(*env)->ReleaseByteArrayElements(env, buf, body, 0);
|
||||
int written_bytes = write(fd, body, len);
|
||||
return written_bytes;
|
||||
}
|
||||
#endif
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ------------------------------------ sendto() --------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
@@ -140,14 +158,12 @@ int (*realclose)(CLOSE_SIG);
|
||||
f = (*env)->GetFieldID(env, cls, "_rawAddr", "J");
|
||||
addr.sin_addr.s_addr = (*env)->GetLongField(env, ztaddr, f);
|
||||
addr.sin_family = AF_INET;
|
||||
LOGV("zt_sendto(): fd = %d\naddr = %s\nport=%d", fd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
||||
|
||||
//LOGV("zt_sendto(): fd = %d\naddr = %s\nport=%d", fd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
||||
// TODO: Optimize this
|
||||
jbyte *body = (*env)->GetByteArrayElements(env, buf, 0);
|
||||
char * bufp = (char *)malloc(sizeof(char)*len);
|
||||
memcpy(bufp, body, len);
|
||||
(*env)->ReleaseByteArrayElements(env, buf, body, 0);
|
||||
|
||||
// "connect" and send buffer contents
|
||||
int sent_bytes = zts_sendto(fd, body, len, flags, (struct sockaddr *)&addr, sizeof(addr));
|
||||
return sent_bytes;
|
||||
@@ -161,7 +177,7 @@ int (*realclose)(CLOSE_SIG);
|
||||
ssize_t zts_sendto(SENDTO_SIG) // Used as internal implementation
|
||||
#endif
|
||||
{
|
||||
LOGV("zt_sendto()\n");
|
||||
dwr(MSG_DEBUG_EXTRA, "zt_sendto(%d, ...)\n", sockfd);
|
||||
if(len > ZT_UDP_DEFAULT_PAYLOAD_MTU) {
|
||||
errno = EMSGSIZE; // Msg is too large
|
||||
return -1;
|
||||
@@ -176,14 +192,6 @@ int (*realclose)(CLOSE_SIG);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// the socket isn't connected
|
||||
//int err = rpc_send_command(api_netpath, RPC_IS_CONNECTED, -1, &fd, sizeof(struct fd));
|
||||
//if(err == -1) {
|
||||
// errno = ENOTCONN;
|
||||
// return -1;
|
||||
//}
|
||||
|
||||
// EMSGSIZE should be returned if the message is too long to be passed atomically through
|
||||
// the underlying protocol, in our case MTU?
|
||||
// TODO: More efficient solution
|
||||
@@ -210,7 +218,7 @@ int (*realclose)(CLOSE_SIG);
|
||||
ssize_t zts_sendmsg(SENDMSG_SIG)
|
||||
#endif
|
||||
{
|
||||
dwr(MSG_DEBUG, "zt_sendmsg()\n");
|
||||
dwr(MSG_DEBUG_EXTRA, "zt_sendmsg()\n");
|
||||
char * p, * buf;
|
||||
size_t tot_len = 0;
|
||||
size_t err;
|
||||
@@ -249,9 +257,8 @@ int (*realclose)(CLOSE_SIG);
|
||||
JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
dwr(MSG_DEBUG, "zt_recvfrom(): fd = %d\n", fd);
|
||||
jbyte *body = (*env)->GetByteArrayElements(env, buf, 0);
|
||||
int recvd_bytes = zts_recvfrom(fd, body, len, flags, &addr, sizeof(addr));
|
||||
int recvd_bytes = zts_recvfrom(fd, body, len, flags, &addr, sizeof(struct sockaddr));
|
||||
(*env)->ReleaseByteArrayElements(env, buf, body, 0);
|
||||
// Update fields of Java ZTAddress object
|
||||
jfieldID fid;
|
||||
@@ -271,32 +278,15 @@ int (*realclose)(CLOSE_SIG);
|
||||
ssize_t zts_recvfrom(RECVFROM_SIG)
|
||||
#endif
|
||||
{
|
||||
dwr(MSG_DEBUG,"zt_recvfrom(%d)\n", socket);
|
||||
ssize_t err;
|
||||
// Since this can be called for connection-oriented sockets,
|
||||
// we need to check the type before we try to read the address info
|
||||
/*
|
||||
int sock_type;
|
||||
socklen_t type_len;
|
||||
realgetsockopt(socket, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &type_len);
|
||||
*/
|
||||
//if(sock_type == SOCK_DGRAM && address != NULL && address_len != NULL) {
|
||||
//zts_getsockname(socket, address, address_len);
|
||||
//}
|
||||
// TODO: Explore more efficient means of adjusting buffer
|
||||
unsigned short port;
|
||||
unsigned int addr;
|
||||
err = read(socket, buffer, length);
|
||||
int addr_info_len = sizeof(addr) + sizeof(port);
|
||||
memcpy(&addr, buffer, sizeof(addr));
|
||||
memcpy(&port, buffer + sizeof(addr), sizeof(port));
|
||||
memmove(buffer, (buffer + addr_info_len), err - addr_info_len);
|
||||
address->sin_addr.s_addr = addr;
|
||||
address->sin_port = port;
|
||||
// zts_getsockname(socket, address, address_len);
|
||||
if(err < 0)
|
||||
dwr(MSG_DEBUG_EXTRA,"zt_recvfrom(%d, ...)\n", socket);
|
||||
ssize_t err = read(socket, buffer, length);
|
||||
if(err < 0) {
|
||||
perror("read:\n");
|
||||
return err < 0 ? err : err - addr_info_len; // Adjust reported buffer size to exclude address info
|
||||
}
|
||||
else {
|
||||
zts_getpeername(socket, address, address_len);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
//#endif
|
||||
|
||||
@@ -312,7 +302,7 @@ int (*realclose)(CLOSE_SIG);
|
||||
ssize_t zts_recvmsg(RECVMSG_SIG)
|
||||
#endif
|
||||
{
|
||||
dwr(MSG_DEBUG, "zt_recvmsg(%d)\n", socket);
|
||||
dwr(MSG_DEBUG_EXTRA, "zt_recvmsg(%d)\n", socket);
|
||||
ssize_t err, n, tot_len = 0;
|
||||
char *buf, *p;
|
||||
struct iovec *iov = message->msg_iov;
|
||||
@@ -373,8 +363,10 @@ int (*realclose)(CLOSE_SIG);
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1write(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len)
|
||||
{
|
||||
jbyte *body = (*env)->GetByteArrayElements(env, buf, 0);
|
||||
int written_bytes = write(fd, body, len);
|
||||
char * bufp = (char *)malloc(sizeof(char)*len);
|
||||
memcpy(bufp, body, len);
|
||||
(*env)->ReleaseByteArrayElements(env, buf, body, 0);
|
||||
int written_bytes = write(fd, body, len);
|
||||
return written_bytes;
|
||||
}
|
||||
// TCP RX
|
||||
@@ -709,27 +701,112 @@ int (*realclose)(CLOSE_SIG);
|
||||
#endif
|
||||
{
|
||||
get_api_netpath();
|
||||
dwr(MSG_DEBUG,"zt_getsockname(%d):\n", sockfd);
|
||||
/* TODO: This is kind of a hack as it stands -- assumes sockaddr is sockaddr_in
|
||||
* and is an IPv4 address. */
|
||||
dwr(MSG_DEBUG_EXTRA,"zt_getsockname(%d):\n", sockfd);
|
||||
struct getsockname_st rpc_st;
|
||||
rpc_st.sockfd = sockfd;
|
||||
memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
|
||||
int rpcfd = rpc_send_command(api_netpath, RPC_GETSOCKNAME, sockfd, &rpc_st, sizeof(struct getsockname_st));
|
||||
/* read address info from service */
|
||||
// read address info from service
|
||||
char addrbuf[sizeof(struct sockaddr_storage)];
|
||||
memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
|
||||
if(rpcfd > -1)
|
||||
if(read(rpcfd, &addrbuf, sizeof(struct sockaddr_storage)) > 0)
|
||||
if(rpcfd > -1) {
|
||||
int err = read(rpcfd, &addrbuf, sizeof(struct sockaddr));
|
||||
close(rpcfd);
|
||||
if(err > 0) {
|
||||
int sum=0;
|
||||
for(int i=0;i<sizeof(struct sockaddr_storage);i++) {
|
||||
sum|=addrbuf[i];
|
||||
}
|
||||
if(!sum) { // RXed a zero-ed address buffer, currently the only way to signal a problem
|
||||
errno = ENOTSOCK; // TODO: general error, needs to be more specific
|
||||
dwr(MSG_ERROR, "zt_getpeername(): no address info given by service.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
errno = ENOTSOCK; // TODO: general error, needs to be more specific
|
||||
dwr(MSG_ERROR, "zt_getpeername(): unable to read address info from service.\n", err);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
struct sockaddr_storage sock_storage;
|
||||
memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr_storage));
|
||||
*addrlen = sizeof(struct sockaddr_in);
|
||||
memcpy(addr, &sock_storage, (*addrlen > sizeof(sock_storage)) ? sizeof(sock_storage) : *addrlen);
|
||||
memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr));
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
memcpy(addr, &sock_storage, sizeof(struct sockaddr));
|
||||
addr->sa_family = AF_INET;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// -------------------------------- getpeername() -------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
// int sockfd, struct sockaddr *addr, socklen_t *addrlen
|
||||
|
||||
#ifdef DYNAMIC_LIB
|
||||
int zt_getpeername(GETSOCKNAME_SIG)
|
||||
#else
|
||||
int zts_getpeername(GETSOCKNAME_SIG)
|
||||
#endif
|
||||
{
|
||||
get_api_netpath();
|
||||
dwr(MSG_DEBUG_EXTRA,"zt_getpeername(%d):\n", sockfd);
|
||||
struct getsockname_st rpc_st;
|
||||
rpc_st.sockfd = sockfd;
|
||||
memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
|
||||
int rpcfd = rpc_send_command(api_netpath, RPC_GETPEERNAME, sockfd, &rpc_st, sizeof(struct getsockname_st));
|
||||
// read address info from service
|
||||
char addrbuf[sizeof(struct sockaddr_storage)];
|
||||
memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
|
||||
|
||||
if(rpcfd > -1) {
|
||||
int err = read(rpcfd, &addrbuf, sizeof(struct sockaddr));
|
||||
close(rpcfd);
|
||||
if(err > 0) {
|
||||
int sum=0;
|
||||
for(int i=0;i<sizeof(struct sockaddr_storage);i++) {
|
||||
sum|=addrbuf[i];
|
||||
}
|
||||
if(!sum) { // RXed a zero-ed address buffer, currently the only way to signal a problem
|
||||
errno = ENOTSOCK; // TODO: general error, needs to be more specific
|
||||
dwr(MSG_ERROR, "zt_getpeername(): no address info given by service.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
errno = ENOTSOCK; // TODO: general error, needs to be more specific
|
||||
dwr(MSG_ERROR, "zt_getpeername(): unable to read address info from service.\n", err);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
struct sockaddr_storage sock_storage;
|
||||
memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr));
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
memcpy(addr, &sock_storage, sizeof(struct sockaddr));
|
||||
addr->sa_family = AF_INET;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ------------------------------------ fcntl() ---------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
// int fd, int cmd, int flags
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1fcntl(JNIEnv *env, jobject thisObj, jint fd, jint cmd, jint flags) {
|
||||
return zts_fcntl(fd,cmd,flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef DYNAMIC_LIB
|
||||
int zt_fcntl(FCNTL_SIG)
|
||||
#else
|
||||
int zts_fcntl(FCNTL_SIG)
|
||||
#endif
|
||||
{
|
||||
dwr(MSG_DEBUG_EXTRA,"zt_fcntl(%d, %d, %d)\n", fd, cmd, flags);
|
||||
return fcntl(fd,cmd,flags);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -65,7 +65,7 @@ int main(int argc, char **argv) {
|
||||
{
|
||||
count++;
|
||||
printf("\nTX(%lu)...\n", count);
|
||||
usleep(100000);
|
||||
usleep(10000);
|
||||
//bzero(buf, BUFSIZE);
|
||||
//printf("\nPlease enter msg: ");
|
||||
//fgets(buf, BUFSIZE, stdin);
|
||||
@@ -73,7 +73,7 @@ int main(int argc, char **argv) {
|
||||
/* send the message to the server */
|
||||
serverlen = sizeof(serveraddr);
|
||||
printf("A\n");
|
||||
n = sendto(sockfd, msg, strlen(msg), 0, &serveraddr, serverlen);
|
||||
n = sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr *)&serveraddr, serverlen);
|
||||
printf("B\n");
|
||||
//if (n < 0)
|
||||
// error("ERROR in sendto");
|
||||
@@ -82,7 +82,7 @@ int main(int argc, char **argv) {
|
||||
printf("C\n");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
printf("D\n");
|
||||
n = recvfrom(sockfd, buf, BUFSIZE, 0, &serveraddr, &serverlen);
|
||||
n = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *)&serveraddr, (socklen_t *)&serverlen);
|
||||
printf("E\n");
|
||||
//if (n < 0)
|
||||
// printf("ERROR in recvfrom: %d", n);
|
||||
|
||||
Reference in New Issue
Block a user