abandoning traditional socket API parameter naming convention for something that actually makes sense

This commit is contained in:
Joseph Henry
2016-09-01 12:26:42 -07:00
parent 530d704557
commit 10963eab81
12 changed files with 218 additions and 236 deletions

View File

@@ -104,7 +104,7 @@ char *zts_get_homepath();
// ZT Intercept/RPC Controls // ZT Intercept/RPC Controls
// TODO: Remove any? // TODO: Remove any?
void set_intercept_status(int mode); /* TODO: Rethink this */ void set_intercept_status(int mode); // TODO: Rethink this
void init_service(int key, const char * path); void init_service(int key, const char * path);
void init_service_and_rpc(int key, const char * path, const char * nwid); void init_service_and_rpc(int key, const char * path, const char * nwid);
void init_intercept(int key); void init_intercept(int key);
@@ -132,7 +132,7 @@ ssize_t zts_recvmsg(RECVMSG_SIG);
#if defined(__UNITY_3D__) #if defined(__UNITY_3D__)
ssize_t zts_recv(int fd, void *buf, int len); ssize_t zts_recv(int fd, void *buf, int len);
ssize_t zts_send(int fd, void *buf, int len); ssize_t zts_send(int fd, void *buf, int len);
int zts_set_nonblock(int fd); /* TODO combine with fcntl() */ int zts_set_nonblock(int fd); // TODO combine with fcntl()
#endif #endif
#if !defined(__IOS__) #if !defined(__IOS__)
@@ -143,9 +143,6 @@ ssize_t zts_recvmsg(RECVMSG_SIG);
// Android JNI Direct-call API // Android JNI Direct-call API
// JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME // JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
/* If you define anything else in this file it that you wish to expose to your Android
Java application you *must* follow that convention and any corresponding Java package/classes
in your Android project must match this as well */
#if defined(__ANDROID__) #if defined(__ANDROID__)
// Exported JNI : ZT SERVICE CONTROLS // Exported JNI : ZT SERVICE CONTROLS
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1start_1service(JNIEnv *env, jobject thisObj, jstring path); JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1start_1service(JNIEnv *env, jobject thisObj, jstring path);

View File

@@ -965,14 +965,14 @@ void NetconEthernetTap::handleGetsockname(PhySocket *sock, PhySocket *rpcSock, v
{ {
Mutex::Lock _l(_tcpconns_m); Mutex::Lock _l(_tcpconns_m);
Connection *conn = getConnection(sock); Connection *conn = getConnection(sock);
if(conn->addr == NULL){ if(conn->local_addr == NULL){
dwr(MSG_DEBUG_EXTRA," handleGetsockname(): No address info available. Is it bound?"); dwr(MSG_DEBUG_EXTRA," handleGetsockname(): No address info available. Is it bound?");
struct sockaddr_storage storage; struct sockaddr_storage storage;
memset(&storage, 0, sizeof(struct sockaddr_storage)); memset(&storage, 0, sizeof(struct sockaddr_storage));
write(_phy.getDescriptor(rpcSock), NULL, sizeof(struct sockaddr_storage)); write(_phy.getDescriptor(rpcSock), NULL, sizeof(struct sockaddr_storage));
return; return;
} }
write(_phy.getDescriptor(rpcSock), conn->addr, sizeof(struct sockaddr_storage)); write(_phy.getDescriptor(rpcSock), conn->local_addr, sizeof(struct sockaddr_storage));
} }
void NetconEthernetTap::handleGetpeername(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct getsockname_st *getsockname_rpc) void NetconEthernetTap::handleGetpeername(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct getsockname_st *getsockname_rpc)
@@ -1012,7 +1012,7 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
connAddr.addr = *((u32_t *)_ips[0].rawIpData()); connAddr.addr = *((u32_t *)_ips[0].rawIpData());
Connection *conn = getConnection(sock); Connection *conn = getConnection(sock);
dwr(MSG_DEBUG," handleBind(sock=%p,fd=%d,port=%d)\n", (void*)&sock, bind_rpc->sockfd, port); dwr(MSG_DEBUG," handleBind(sock=%p,fd=%d,port=%d)\n", (void*)&sock, bind_rpc->fd, port);
if(conn) { if(conn) {
if(conn->type == SOCK_DGRAM) { if(conn->type == SOCK_DGRAM) {
#if defined(__ANDROID__) #if defined(__ANDROID__)
@@ -1027,7 +1027,7 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
struct sockaddr_in addr_in; struct sockaddr_in addr_in;
memcpy(&addr_in, &bind_rpc->addr, sizeof(addr_in)); memcpy(&addr_in, &bind_rpc->addr, sizeof(addr_in));
addr_in.sin_port = Utils::ntoh(conn->UDP_pcb->local_port); // Newly assigned port addr_in.sin_port = Utils::ntoh(conn->UDP_pcb->local_port); // Newly assigned port
memcpy(&conn->addr, &addr_in, sizeof(addr_in)); memcpy(&conn->local_addr, &addr_in, sizeof(addr_in));
sendReturnValue(rpcSock, ERR_OK, ERR_OK); // Success sendReturnValue(rpcSock, ERR_OK, ERR_OK); // Success
} }
return; return;
@@ -1044,7 +1044,7 @@ void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **u
if(err == ERR_BUF) if(err == ERR_BUF)
sendReturnValue(rpcSock, -1, ENOMEM); sendReturnValue(rpcSock, -1, ENOMEM);
} else { } else {
conn->addr = (struct sockaddr_storage *) &bind_rpc->addr; conn->local_addr = (struct sockaddr_storage *) &bind_rpc->addr;
sendReturnValue(rpcSock, ERR_OK, ERR_OK); // Success sendReturnValue(rpcSock, ERR_OK, ERR_OK); // Success
} }
} else { } else {
@@ -1122,7 +1122,7 @@ Connection * NetconEthernetTap::handleSocketProxy(PhySocket *sock, int socket_ty
if(new_udp_PCB || new_tcp_PCB) { if(new_udp_PCB || new_tcp_PCB) {
conn->sock = sock; conn->sock = sock;
conn->type = socket_type; conn->type = socket_type;
conn->addr = NULL; conn->local_addr = NULL;
conn->peer_addr = NULL; conn->peer_addr = NULL;
if(conn->type == SOCK_DGRAM) conn->UDP_pcb = new_udp_PCB; if(conn->type == SOCK_DGRAM) conn->UDP_pcb = new_udp_PCB;
if(conn->type == SOCK_STREAM) conn->TCP_pcb = new_tcp_PCB; if(conn->type == SOCK_STREAM) conn->TCP_pcb = new_tcp_PCB;
@@ -1153,7 +1153,7 @@ Connection * NetconEthernetTap::handleSocket(PhySocket *sock, void **uptr, struc
*uptr = newConn; *uptr = newConn;
newConn->type = socket_rpc->socket_type; newConn->type = socket_rpc->socket_type;
newConn->sock = sock; newConn->sock = sock;
newConn->addr = NULL; newConn->local_addr = NULL;
newConn->peer_addr = NULL; newConn->peer_addr = NULL;
if(newConn->type == SOCK_DGRAM) newConn->UDP_pcb = new_udp_PCB; if(newConn->type == SOCK_DGRAM) newConn->UDP_pcb = new_udp_PCB;
if(newConn->type == SOCK_STREAM) newConn->TCP_pcb = new_tcp_PCB; if(newConn->type == SOCK_STREAM) newConn->TCP_pcb = new_tcp_PCB;
@@ -1265,7 +1265,7 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne
{ {
dwr(MSG_DEBUG_EXTRA, "handleConnect(%p)\n", (void*)&sock); dwr(MSG_DEBUG_EXTRA, "handleConnect(%p)\n", (void*)&sock);
Mutex::Lock _l(_tcpconns_m); Mutex::Lock _l(_tcpconns_m);
struct sockaddr_in *rawAddr = (struct sockaddr_in *) &connect_rpc->__addr; struct sockaddr_in *rawAddr = (struct sockaddr_in *) &connect_rpc->addr;
int port = lwipstack->__lwip_ntohs(rawAddr->sin_port); int port = lwipstack->__lwip_ntohs(rawAddr->sin_port);
ip_addr_t connAddr = convert_ip(rawAddr); ip_addr_t connAddr = convert_ip(rawAddr);
int err = 0, ip = rawAddr->sin_addr.s_addr; int err = 0, ip = rawAddr->sin_addr.s_addr;

View File

@@ -94,12 +94,11 @@ namespace ZeroTier {
PhySocket *rpcSock, *sock; PhySocket *rpcSock, *sock;
struct tcp_pcb *TCP_pcb; struct tcp_pcb *TCP_pcb;
struct udp_pcb *UDP_pcb; struct udp_pcb *UDP_pcb;
struct sockaddr_storage *addr; // TODO: Rename struct sockaddr_storage *local_addr; // Address we've bound to locally
struct sockaddr_storage *peer_addr; // Only set by connection procedure struct sockaddr_storage *peer_addr; // Address of connection call to remote host
unsigned short port; unsigned short port;
unsigned char txbuf[DEFAULT_TCP_TX_BUF_SZ]; unsigned char txbuf[DEFAULT_TCP_TX_BUF_SZ];
unsigned char rxbuf[DEFAULT_TCP_RX_BUF_SZ]; unsigned char rxbuf[DEFAULT_TCP_RX_BUF_SZ];
// TODO: necessary still? // TODO: necessary still?
int proxy_conn_state; int proxy_conn_state;
}; };

View File

@@ -127,10 +127,10 @@ char *api_netpath;
load_symbols(); load_symbols();
} }
#if defined(SDK_BUNDLED) #if defined(SDK_BUNDLED)
/* The reasoning for this check is that if you've built the SDK with SDK_BUNDLE=1, then // The reasoning for this check is that if you've built the SDK with SDK_BUNDLE=1, then
you've included a full ZeroTier service in the same binary as your intercept, and we // you've included a full ZeroTier service in the same binary as your intercept, and we
don't want to run ZeroTier network API calls through the intercept, so we must specify // don't want to run ZeroTier network API calls through the intercept, so we must specify
which threads should be intercepted manually */ // which threads should be intercepted manually
void *spec = pthread_getspecific(thr_id_key); void *spec = pthread_getspecific(thr_id_key);
int thr_id = spec != NULL ? *((int*)spec) : -1; int thr_id = spec != NULL ? *((int*)spec) : -1;
return thr_id == INTERCEPT_ENABLED; return thr_id == INTERCEPT_ENABLED;
@@ -164,76 +164,75 @@ char *api_netpath;
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ------------------------------------ sendto() -------------------------------- // ------------------------------------ sendto() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, const void *buf, size_t len, int flags, // int fd, const void *buf, size_t len, int flags,
// const struct sockaddr *addr, socklen_t addr_len // const struct sockaddr *addr, socklen_t addrlen
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
ssize_t sendto(SENDTO_SIG) ssize_t sendto(SENDTO_SIG)
{ {
dwr(MSG_DEBUG, "sendto(%d, %d)\n", sockfd, len); dwr(MSG_DEBUG, "sendto(%d, ..., %d)\n", fd, len);
//if (!check_intercept_enabled()) //if (!check_intercept_enabled())
return realsendto(sockfd, buf, len, flags, addr, addr_len); return realsendto(fd, buf, len, flags, addr, addrlen);
return zts_sendto(sockfd, buf, len, flags, addr, addr_len); return zts_sendto(fd, buf, len, flags, addr, addrlen);
} }
#endif #endif
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ----------------------------------- sendmsg() -------------------------------- // ----------------------------------- sendmsg() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int socket, const struct msghdr *message, int flags // int fd, const struct msghdr *msg, int flags
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
ssize_t sendmsg(SENDMSG_SIG) ssize_t sendmsg(SENDMSG_SIG)
{ {
dwr(MSG_DEBUG, "sendmsg()\n"); dwr(MSG_DEBUG, "sendmsg()\n");
//if(!check_intercept_enabled()) //if(!check_intercept_enabled())
return realsendmsg(socket, message, flags); return realsendmsg(fd, msg, flags);
zts_sendmsg(socket, message, flags); zts_sendmsg(fd, msg, flags);
} }
#endif #endif
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ---------------------------------- recvfrom() -------------------------------- // ---------------------------------- recvfrom() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int socket, void *restrict buffer, size_t length, int flags, struct sockaddr // int fd, void *restrict buf, size_t len, int flags, struct sockaddr
// *restrict address, socklen_t *restrict address_len // *restrict addr, socklen_t *restrict addrlen
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
ssize_t recvfrom(RECVFROM_SIG) ssize_t recvfrom(RECVFROM_SIG)
{ {
dwr(MSG_DEBUG, "recvfrom(%d)\n", socket); dwr(MSG_DEBUG, "recvfrom(%d)\n", socket);
if(!check_intercept_enabled()) if(!check_intercept_enabled())
return realrecvfrom(socket, buffer, length, flags, address, address_len); return realrecvfrom(fd, buf, len, flags, addr, addrlen);
return zts_recvfrom(socket, buffer, length, flags, address, address_len); return zts_recvfrom(fd, buf, len, flags, addr, addrlen);
} }
#endif #endif
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ----------------------------------- recvmsg() -------------------------------- // ----------------------------------- recvmsg() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int socket, struct msghdr *message, int flags // int fd, struct msghdr *msg, int flags
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
ssize_t recvmsg(RECVMSG_SIG) ssize_t recvmsg(RECVMSG_SIG)
{ {
dwr(MSG_DEBUG, "recvmsg(%d)\n", socket); dwr(MSG_DEBUG, "recvmsg(%d)\n", fd);
//if(!check_intercept_enabled()) //if(!check_intercept_enabled())
return realrecvmsg(socket, message, flags); return realrecvmsg(fd, msg, flags);
return zts_recvmsg(socket, message, flags); return zts_recvmsg(fd, msg, flags);
} }
#endif #endif
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// --------------------------------- setsockopt() ------------------------------- // --------------------------------- setsockopt() -------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int socket, int level, int optname, const void *optval, // int fd, int level, int optname, const void *optval, socklen_t optlen
// socklen_t optlen
int setsockopt(SETSOCKOPT_SIG) int setsockopt(SETSOCKOPT_SIG)
{ {
dwr(MSG_DEBUG, "setsockopt(%d)\n", socket); dwr(MSG_DEBUG, "setsockopt(%d)\n", fd);
if (!check_intercept_enabled()) if (!check_intercept_enabled())
return realsetsockopt(socket, level, optname, optval, optlen); return realsetsockopt(fd, level, optname, optval, optlen);
#if defined(__linux__) #if defined(__linux__)
if(level == SOL_IPV6 && optname == IPV6_V6ONLY) if(level == SOL_IPV6 && optname == IPV6_V6ONLY)
return 0; return 0;
@@ -242,23 +241,22 @@ char *api_netpath;
#endif #endif
if(level == IPPROTO_TCP || (level == SOL_SOCKET && optname == SO_KEEPALIVE)) if(level == IPPROTO_TCP || (level == SOL_SOCKET && optname == SO_KEEPALIVE))
return 0; return 0;
if(realsetsockopt(socket, level, optname, optval, optlen) < 0) if(realsetsockopt(fd, level, optname, optval, optlen) < 0)
perror("setsockopt():\n"); perror("setsockopt():\n");
return zts_setsockopt(socket, level, optname, optval, optlen); return zts_setsockopt(fd, level, optname, optval, optlen);
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// --------------------------------- getsockopt() ------------------------------- // --------------------------------- getsockopt() -------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, int level, int optname, void *optval, // int fd, int level, int optname, void *optval, socklen_t *optlen
// socklen_t *optlen
int getsockopt(GETSOCKOPT_SIG) int getsockopt(GETSOCKOPT_SIG)
{ {
dwr(MSG_DEBUG, "getsockopt(%d)\n", sockfd); dwr(MSG_DEBUG, "getsockopt(%d)\n", fd);
if (!check_intercept_enabled() || !connected_to_service(sockfd)) if (!check_intercept_enabled() || !connected_to_service(fd))
return realgetsockopt(sockfd, level, optname, optval, optlen); return realgetsockopt(fd, level, optname, optval, optlen);
return zts_getsockopt(sockfd, level, optname, optval, optlen); return zts_getsockopt(fd, level, optname, optval, optlen);
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@@ -295,16 +293,16 @@ char *api_netpath;
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ---------------------------------- connect() --------------------------------- // ---------------------------------- connect() ---------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int __fd, const struct sockaddr * __addr, socklen_t __len // int fd, const struct sockaddr *addr, socklen_t addrlen
int connect(CONNECT_SIG) int connect(CONNECT_SIG)
{ {
dwr(MSG_DEBUG, "connect(%d)\n", __fd); dwr(MSG_DEBUG, "connect(%d)\n", fd);
struct sockaddr_in *connaddr; struct sockaddr_in *connaddr;
connaddr = (struct sockaddr_in *)__addr; connaddr = (struct sockaddr_in *)addr;
if(__addr->sa_family == AF_LOCAL || __addr->sa_family == AF_UNIX) { if(addr->sa_family == AF_LOCAL || addr->sa_family == AF_UNIX) {
struct sockaddr_storage storage; struct sockaddr_storage storage;
memcpy(&storage, __addr, __len); memcpy(&storage, addr, addrlen);
struct sockaddr_un *s_un = (struct sockaddr_un*)&storage; struct sockaddr_un *s_un = (struct sockaddr_un*)&storage;
dwr(MSG_DEBUG, "connect(): address = %s\n", s_un->sun_path); dwr(MSG_DEBUG, "connect(): address = %s\n", s_un->sun_path);
} }
@@ -319,53 +317,53 @@ char *api_netpath;
dwr(MSG_DEBUG,"connect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port)); dwr(MSG_DEBUG,"connect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port));
if(!check_intercept_enabled()) if(!check_intercept_enabled())
return realconnect(__fd, __addr, __len); return realconnect(fd, addr, addrlen);
/* Check that this is a valid fd */ // Check that this is a valid fd
if(fcntl(__fd, F_GETFD) < 0) { if(fcntl(fd, F_GETFD) < 0) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
/* Check that it is a socket */ // Check that it is a socket
int sock_type; int sock_type;
socklen_t sock_type_len = sizeof(sock_type); socklen_t sock_type_len = sizeof(sock_type);
if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) { if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
#if defined(__linux__) #if defined(__linux__)
/* Check family */ // Check family
if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){ if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
errno = EAFNOSUPPORT; errno = EAFNOSUPPORT;
return -1; return -1;
} }
#endif #endif
/* make sure we don't touch any standard outputs */ // make sure we don't touch any standard outputs
if(__fd == 0 || __fd == 1 || __fd == 2) if(fd == 0 || fd == 1 || fd == 2)
return(realconnect(__fd, __addr, __len)); return(realconnect(fd, addr, addrlen));
if(__addr != NULL && (connaddr->sin_family == AF_LOCAL if(addr != NULL && (connaddr->sin_family == AF_LOCAL
#if defined(__linux__) #if defined(__linux__)
|| connaddr->sin_family == PF_NETLINK || connaddr->sin_family == PF_NETLINK
|| connaddr->sin_family == AF_NETLINK || connaddr->sin_family == AF_NETLINK
#endif #endif
|| connaddr->sin_family == AF_UNIX)) { || connaddr->sin_family == AF_UNIX)) {
return realconnect(__fd, __addr, __len); return realconnect(fd, addr, addrlen);
} }
return zts_connect(__fd, __addr, __len); return zts_connect(fd, addr, addrlen);
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ------------------------------------ bind() ---------------------------------- // ------------------------------------ bind() ----------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, const struct sockaddr *addr, socklen_t addrlen // int fd, const struct sockaddr *addr, socklen_t addrlen
int bind(BIND_SIG) int bind(BIND_SIG)
{ {
dwr(MSG_DEBUG,"bind(%d)\n", sockfd); dwr(MSG_DEBUG,"bind(%d)\n", fd);
// make sure we don't touch any standard outputs // make sure we don't touch any standard outputs
if(sockfd == 0 || sockfd == 1 || sockfd == 2) if(fd == 0 || fd == 1 || fd == 2)
return(realbind(sockfd, addr, addrlen)); return(realbind(fd, addr, addrlen));
struct sockaddr_in *connaddr; struct sockaddr_in *connaddr;
connaddr = (struct sockaddr_in *)addr; connaddr = (struct sockaddr_in *)addr;
@@ -374,7 +372,7 @@ char *api_netpath;
|| connaddr->sin_family == AF_NETLINK || connaddr->sin_family == AF_NETLINK
#endif #endif
|| connaddr->sin_family == AF_UNIX) { || connaddr->sin_family == AF_UNIX) {
int err = realbind(sockfd, addr, addrlen); int err = realbind(fd, addr, addrlen);
dwr(MSG_DEBUG,"realbind, err = %d\n", err); dwr(MSG_DEBUG,"realbind, err = %d\n", err);
return err; return err;
} }
@@ -389,132 +387,132 @@ char *api_netpath;
int sock_type; int sock_type;
socklen_t sock_type_len = sizeof(sock_type); socklen_t sock_type_len = sizeof(sock_type);
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) { if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
// Otherwise, perform usual intercept logic // Otherwise, perform usual intercept logic
if (!check_intercept_enabled()) if (!check_intercept_enabled())
return realbind(sockfd, addr, addrlen); return realbind(fd, addr, addrlen);
// Check that this is a valid fd // Check that this is a valid fd
if(fcntl(sockfd, F_GETFD) < 0) { if(fcntl(fd, F_GETFD) < 0) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
// Check that it is a socket // Check that it is a socket
int opt = -1; int opt = -1;
socklen_t opt_len; socklen_t opt_len;
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) { if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
return zts_bind(sockfd, addr, addrlen); return zts_bind(fd, addr, addrlen);
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ----------------------------------- accept4() -------------------------------- // ----------------------------------- accept4() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags // int fd, struct sockaddr *addr, socklen_t *addrlen, int flags
#if defined(__linux__) #if defined(__linux__)
int accept4(ACCEPT4_SIG) { int accept4(ACCEPT4_SIG) {
dwr(MSG_DEBUG,"accept4(%d):\n", sockfd); dwr(MSG_DEBUG,"accept4(%d):\n", fd);
return zts_accept4(sockfd, addr, addrlen, flags); return zts_accept4(fd, addr, addrlen, flags);
} }
#endif #endif
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ----------------------------------- accept() --------------------------------- // ----------------------------------- accept() ---------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd struct sockaddr *addr, socklen_t *addrlen // int fd struct sockaddr *addr, socklen_t *addrlen
int accept(ACCEPT_SIG) { int accept(ACCEPT_SIG) {
dwr(MSG_DEBUG,"accept(%d):\n", sockfd); dwr(MSG_DEBUG,"accept(%d):\n", fd);
if (!check_intercept_enabled()) if (!check_intercept_enabled())
return realaccept(sockfd, addr, addrlen); return realaccept(fd, addr, addrlen);
/* Check that this is a valid fd */ // Check that this is a valid fd
if(fcntl(sockfd, F_GETFD) < 0) { if(fcntl(fd, F_GETFD) < 0) {
return -1; return -1;
errno = EBADF; errno = EBADF;
dwr(MSG_DEBUG,"EBADF\n"); dwr(MSG_DEBUG,"EBADF\n");
return -1; return -1;
} }
/* Check that it is a socket */ // Check that it is a socket
int opt; int opt;
socklen_t opt_len; socklen_t opt_len;
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) { if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
errno = ENOTSOCK; errno = ENOTSOCK;
dwr(MSG_DEBUG,"ENOTSOCK\n"); dwr(MSG_DEBUG,"ENOTSOCK\n");
return -1; return -1;
} }
/* Check that this socket supports accept() */ // Check that this socket supports accept()
if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) { if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
errno = EOPNOTSUPP; errno = EOPNOTSUPP;
dwr(MSG_DEBUG,"EOPNOTSUPP\n"); dwr(MSG_DEBUG,"EOPNOTSUPP\n");
return -1; return -1;
} }
/* Check that we haven't hit the soft-limit file descriptors allowed */ // Check that we haven't hit the soft-limit file descriptors allowed
struct rlimit rl; struct rlimit rl;
getrlimit(RLIMIT_NOFILE, &rl); getrlimit(RLIMIT_NOFILE, &rl);
if(sockfd >= rl.rlim_cur){ if(fd >= rl.rlim_cur){
errno = EMFILE; errno = EMFILE;
dwr(MSG_DEBUG,"EMFILE\n"); dwr(MSG_DEBUG,"EMFILE\n");
return -1; return -1;
} }
/* Check address length */ // Check address length
if(addrlen < 0) { if(addrlen < 0) {
errno = EINVAL; errno = EINVAL;
dwr(MSG_DEBUG,"EINVAL\n"); dwr(MSG_DEBUG,"EINVAL\n");
return -1; return -1;
} }
/* redirect calls for standard I/O descriptors to kernel */ // redirect calls for standard I/O descriptors to kernel
if(sockfd == 0 || sockfd == 1 || sockfd == 2){ if(fd == 0 || fd == 1 || fd == 2){
dwr(MSG_DEBUG,"realaccept():\n"); dwr(MSG_DEBUG,"realaccept():\n");
return(realaccept(sockfd, addr, addrlen)); return(realaccept(fd, addr, addrlen));
} }
return zts_accept(sockfd, addr, addrlen); return zts_accept(fd, addr, addrlen);
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ------------------------------------- listen()-------------------------------- // ------------------------------------- listen()--------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, int backlog // int fd, int backlog
int listen(LISTEN_SIG) int listen(LISTEN_SIG)
{ {
dwr(MSG_DEBUG,"listen(%d):\n", sockfd); dwr(MSG_DEBUG,"listen(%d):\n", fd);
if (!check_intercept_enabled()) if (!check_intercept_enabled())
return reallisten(sockfd, backlog); return reallisten(fd, backlog);
int sock_type; int sock_type;
socklen_t sock_type_len = sizeof(sock_type); socklen_t sock_type_len = sizeof(sock_type);
/* Check that this is a valid fd */ // Check that this is a valid fd
if(fcntl(sockfd, F_GETFD) < 0) { if(fcntl(fd, F_GETFD) < 0) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
/* Check that it is a socket */ // Check that it is a socket
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) { if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
/* Check that this socket supports accept() */ // Check that this socket supports accept()
if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) { if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
errno = EOPNOTSUPP; errno = EOPNOTSUPP;
return -1; return -1;
} }
/* make sure we don't touch any standard outputs */ // make sure we don't touch any standard outputs
if(sockfd == 0 || sockfd == 1 || sockfd == 2) if(fd == 0 || fd == 1 || fd == 2)
return reallisten(sockfd, backlog); return reallisten(fd, backlog);
if(!connected_to_service(sockfd)) { if(!connected_to_service(fd)) {
return reallisten(sockfd, backlog); return reallisten(fd, backlog);
} }
return zts_listen(sockfd, backlog); return zts_listen(fd, backlog);
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@@ -533,26 +531,27 @@ char *api_netpath;
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// -------------------------------- getsockname() ------------------------------- // -------------------------------- getsockname() -------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, struct sockaddr *addr, socklen_t *addrlen // int fd, struct sockaddr *addr, socklen_t *addrlen
int getsockname(GETSOCKNAME_SIG) int getsockname(GETSOCKNAME_SIG)
{ {
dwr(MSG_DEBUG,"getsockname(%d):\n", sockfd); dwr(MSG_DEBUG,"getsockname(%d):\n", fd);
#if !defined(__IOS__) #if !defined(__IOS__)
if (!check_intercept_enabled()) if (!check_intercept_enabled())
return realgetsockname(sockfd, addr, addrlen); return realgetsockname(fd, addr, addrlen);
#endif #endif
dwr(MSG_DEBUG,"getsockname(%d)\n", sockfd); dwr(MSG_DEBUG,"getsockname(%d)\n", fd);
if(!connected_to_service(sockfd)) { if(!connected_to_service(fd)) {
dwr(MSG_DEBUG,"getsockname(): not used by service\n"); dwr(MSG_DEBUG,"getsockname(): not used by service\n");
return realgetsockname(sockfd, addr, addrlen); return realgetsockname(fd, addr, addrlen);
} }
return zts_getsockname(sockfd, addr, addrlen); return zts_getsockname(fd, addr, addrlen);
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ------------------------------------ syscall() ------------------------------- // ------------------------------------ syscall() -------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// long number, ...
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
#if defined(__linux__) #if defined(__linux__)

View File

@@ -175,7 +175,6 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
memcpy(CANARY+CANARY_SZ, padding, sizeof(padding)); memcpy(CANARY+CANARY_SZ, padding, sizeof(padding));
uint64_t canary_num; uint64_t canary_num;
// ephemeral RPC socket used only for this command // ephemeral RPC socket used only for this command
// TODO: Re-engineer RPC socket model for more efficiency
int rpc_sock = rpc_join(path); int rpc_sock = rpc_join(path);
// Generate token // Generate token
int fdrand = open("/dev/urandom", O_RDONLY); int fdrand = open("/dev/urandom", O_RDONLY);

View File

@@ -89,17 +89,17 @@ void rpc_mutex_init();
/* Structures used for sending commands via RPC mechanism */ /* Structures used for sending commands via RPC mechanism */
struct bind_st { struct bind_st {
int sockfd; int fd;
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t addrlen; socklen_t addrlen;
int __tid; int tid;
}; };
struct connect_st { struct connect_st {
int __fd; int fd;
struct sockaddr_storage __addr; struct sockaddr_storage addr;
socklen_t __len; socklen_t addrlen;
int __tid; int tid;
}; };
struct close_st { struct close_st {
@@ -107,23 +107,23 @@ struct close_st {
}; };
struct listen_st { struct listen_st {
int sockfd; int fd;
int backlog; int backlog;
int __tid; int tid;
}; };
struct socket_st { struct socket_st {
int socket_family; int socket_family;
int socket_type; int socket_type;
int protocol; int protocol;
int __tid; int tid;
}; };
struct accept_st { struct accept_st {
int sockfd; int fd;
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t addrlen; socklen_t addrlen;
int __tid; int tid;
}; };
struct shutdown_st { struct shutdown_st {
@@ -132,7 +132,7 @@ struct shutdown_st {
}; };
struct getsockname_st { struct getsockname_st {
int sockfd; int fd;
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t addrlen; socklen_t addrlen;
}; };

View File

@@ -331,9 +331,7 @@ char *zts_get_homepath() {
// Typically used on iOS/OSX // Typically used on iOS/OSX
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
/* // Starts a service thread and performs basic setup tasks
* Starts a service thread and performs basic setup tasks
*/
void init_service(int key, const char * path) { void init_service(int key, const char * path) {
givenHomeDir = path; givenHomeDir = path;
pthread_key_create(&thr_id_key, NULL); pthread_key_create(&thr_id_key, NULL);
@@ -346,9 +344,7 @@ char *zts_get_homepath() {
rpcNWID = nwid; rpcNWID = nwid;
init_service(key, path); init_service(key, path);
} }
/* // Enables or disables intercept for current thread using key in thread-local storage
* Enables or disables intercept for current thread using key in thread-local storage
*/
void set_intercept_status(int mode) { void set_intercept_status(int mode) {
#if defined(__APPLE__) #if defined(__APPLE__)
fprintf(stderr, "set_intercept_status(mode=%d): tid = %d\n", mode, pthread_mach_thread_np(pthread_self())); fprintf(stderr, "set_intercept_status(mode=%d): tid = %d\n", mode, pthread_mach_thread_np(pthread_self()));

View File

@@ -30,29 +30,28 @@
#include <sys/socket.h> #include <sys/socket.h>
#define SETSOCKOPT_SIG int socket, int level, int optname, const void *optval, socklen_t optlen #define SETSOCKOPT_SIG int fd, int level, int optname, const void *optval, socklen_t optlen
#define GETSOCKOPT_SIG int sockfd, int level, int optname, void *optval, socklen_t *optlen #define GETSOCKOPT_SIG int fd, int level, int optname, void *optval, socklen_t *optlen
#define SENDMSG_SIG int socket, const struct msghdr *message, int flags #define SENDMSG_SIG int fd, const struct msghdr *msg, int flags
#define SENDTO_SIG int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addr_len #define SENDTO_SIG int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen
#define RECV_SIG int socket, void *buffer, size_t length, int flags #define RECV_SIG int fd, void *buf, size_t len, int flags
#define RECVFROM_SIG int socket, void * buffer, size_t length, int flags, struct sockaddr * address, socklen_t * address_len #define RECVFROM_SIG int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen
#define RECVMSG_SIG int socket, struct msghdr *message,int flags #define RECVMSG_SIG int fd, struct msghdr *msg,int flags
#define SEND_SIG int socket, const void *buffer, size_t length, int flags #define SEND_SIG int fd, const void *buf, size_t len, int flags
#define WRITE_SIG int __fd, const void *__buf, size_t __n #define WRITE_SIG int fd, const void *buf, size_t len
#define RECV_SIG int socket, void *buffer, size_t length, int flags #define READ_SIG int fd, void *buf, size_t len
#define READ_SIG int __fd, void *__buf, size_t __nbytes
#define SOCKET_SIG int socket_family, int socket_type, int protocol #define SOCKET_SIG int socket_family, int socket_type, int protocol
#define CONNECT_SIG int __fd, const struct sockaddr * __addr, socklen_t __len #define CONNECT_SIG int fd, const struct sockaddr *addr, socklen_t addrlen
#define BIND_SIG int sockfd, const struct sockaddr *addr, socklen_t addrlen #define BIND_SIG int fd, const struct sockaddr *addr, socklen_t addrlen
#define LISTEN_SIG int sockfd, int backlog #define LISTEN_SIG int fd, int backlog
#define ACCEPT4_SIG int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags #define ACCEPT4_SIG int fd, struct sockaddr *addr, socklen_t *addrlen, int flags
#define ACCEPT_SIG int sockfd, struct sockaddr *addr, socklen_t *addrlen #define ACCEPT_SIG int fd, struct sockaddr *addr, socklen_t *addrlen
#define CLOSE_SIG int fd #define CLOSE_SIG int fd
#define GETSOCKNAME_SIG int sockfd, struct sockaddr *addr, socklen_t *addrlen #define GETSOCKNAME_SIG int fd, struct sockaddr *addr, socklen_t *addrlen
#define GETPEERNAME_SIG int sockfd, struct sockaddr *addr, socklen_t *addrlen #define GETPEERNAME_SIG int fd, struct sockaddr *addr, socklen_t *addrlen
#define FCNTL_SIG int fd, int cmd, int flags #define FCNTL_SIG int fd, int cmd, int flags
#define SYSCALL_SIG long number, ... #define SYSCALL_SIG long number, ...

View File

@@ -125,7 +125,7 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ------------------------------------ send() ---------------------------------- // ------------------------------------ send() ----------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, const void *buf, size_t len // int fd, const void *buf, size_t len
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags) JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags)
@@ -142,8 +142,8 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ------------------------------------ sendto() -------------------------------- // ------------------------------------ sendto() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, const void *buf, size_t len, int flags, // int fd, const void *buf, size_t len, int flags,
// const struct sockaddr *addr, socklen_t addr_len // const struct sockaddr *addr, socklen_t addrlen
#if defined(__ANDROID__) #if defined(__ANDROID__)
// TODO: Check result of each JNI call // TODO: Check result of each JNI call
@@ -177,14 +177,14 @@ int (*realclose)(CLOSE_SIG);
ssize_t zts_sendto(SENDTO_SIG) // Used as internal implementation ssize_t zts_sendto(SENDTO_SIG) // Used as internal implementation
#endif #endif
{ {
dwr(MSG_DEBUG_EXTRA, "zt_sendto(%d, ...)\n", sockfd); dwr(MSG_DEBUG_EXTRA, "zt_sendto(%d, ...)\n", fd);
if(len > ZT_UDP_DEFAULT_PAYLOAD_MTU) { if(len > ZT_UDP_DEFAULT_PAYLOAD_MTU) {
errno = EMSGSIZE; // Msg is too large errno = EMSGSIZE; // Msg is too large
return -1; return -1;
} }
int socktype = 0; int socktype = 0;
socklen_t socktype_len; socklen_t socktype_len;
getsockopt(sockfd,SOL_SOCKET, SO_TYPE, (void*)&socktype, &socktype_len); getsockopt(fd,SOL_SOCKET, SO_TYPE, (void*)&socktype, &socktype_len);
if((socktype & SOCK_STREAM) || (socktype & SOCK_SEQPACKET)) { if((socktype & SOCK_STREAM) || (socktype & SOCK_SEQPACKET)) {
if(addr == NULL || flags != 0) { if(addr == NULL || flags != 0) {
@@ -197,19 +197,19 @@ int (*realclose)(CLOSE_SIG);
// TODO: More efficient solution // TODO: More efficient solution
// This connect call is used to get the address info to the stack for sending the packet // This connect call is used to get the address info to the stack for sending the packet
int err; int err;
if((err = zts_connect(sockfd, addr, addr_len)) < 0) { if((err = zts_connect(fd, addr, addrlen)) < 0) {
LOGV("sendto(): unknown problem passing address info to stack\n"); LOGV("sendto(): unknown problem passing address info to stack\n");
errno = EISCONN; // double-check this is correct errno = EISCONN; // double-check this is correct
return -1; return -1;
} }
return write(sockfd, buf, len); return write(fd, buf, len);
} }
//#endif //#endif
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ----------------------------------- sendmsg() -------------------------------- // ----------------------------------- sendmsg() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int socket, const struct msghdr *message, int flags // int fd, const struct msghdr *msg, int flags
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
#ifdef DYNAMIC_LIB #ifdef DYNAMIC_LIB
@@ -218,12 +218,12 @@ int (*realclose)(CLOSE_SIG);
ssize_t zts_sendmsg(SENDMSG_SIG) ssize_t zts_sendmsg(SENDMSG_SIG)
#endif #endif
{ {
dwr(MSG_DEBUG_EXTRA, "zt_sendmsg()\n"); dwr(MSG_DEBUG_EXTRA, "zt_sendmsg(%d)\n", fd);
char * p, * buf; char * p, * buf;
size_t tot_len = 0; size_t tot_len = 0;
size_t err; size_t err;
struct iovec * iov = message->msg_iov; struct iovec * iov = msg->msg_iov;
for(int i=0; i<message->msg_iovlen; ++i) for(int i=0; i<msg->msg_iovlen; ++i)
tot_len += iov[i].iov_len; tot_len += iov[i].iov_len;
if(tot_len > ZT_UDP_DEFAULT_PAYLOAD_MTU) { if(tot_len > ZT_UDP_DEFAULT_PAYLOAD_MTU) {
errno = EMSGSIZE; // Message too large to send atomically via underlying protocol, don't send errno = EMSGSIZE; // Message too large to send atomically via underlying protocol, don't send
@@ -235,11 +235,11 @@ int (*realclose)(CLOSE_SIG);
return -1; return -1;
} }
p = buf; p = buf;
for(int i=0; i < message->msg_iovlen; ++i) { for(int i=0; i < msg->msg_iovlen; ++i) {
memcpy(p, iov[i].iov_base, iov[i].iov_len); memcpy(p, iov[i].iov_base, iov[i].iov_len);
p += iov[i].iov_len; p += iov[i].iov_len;
} }
err = sendto(socket, buf, tot_len, flags, message->msg_name, message->msg_namelen); err = sendto(fd, buf, tot_len, flags, msg->msg_name, msg->msg_namelen);
free(buf); free(buf);
return err; return err;
} }
@@ -248,8 +248,8 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ---------------------------------- recvfrom() -------------------------------- // ---------------------------------- recvfrom() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int socket, void *restrict buffer, size_t length, int flags, struct sockaddr // int fd, void *restrict buf, size_t len, int flags, struct sockaddr
// *restrict address, socklen_t *restrict address_len // *restrict addr, socklen_t *restrict addrlen
#if defined(__ANDROID__) #if defined(__ANDROID__)
// UDP RX // UDP RX
@@ -283,11 +283,11 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
{ {
int tmpsz = 0; // payload size int tmpsz = 0; // payload size
// dwr(MSG_DEBUG_EXTRA,"zt_recvfrom(%d, ...)\n", socket); // dwr(MSG_DEBUG_EXTRA,"zt_recvfrom(%d, ...)\n", fd);
if(read(socket, buffer, ZT_MAX_MTU) > 0) { if(read(fd, buf, ZT_MAX_MTU) > 0) {
// TODO: case for address size mismatch? // TODO: case for address size mismatch?
memcpy(address, buffer, address_len); memcpy(addr, buf, addrlen);
memcpy(&tmpsz, buffer + sizeof(struct sockaddr_storage), sizeof(tmpsz)); memcpy(&tmpsz, buf + sizeof(struct sockaddr_storage), sizeof(tmpsz));
} }
else { else {
perror("read:\n"); perror("read:\n");
@@ -299,7 +299,7 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ----------------------------------- recvmsg() -------------------------------- // ----------------------------------- recvmsg() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int socket, struct msghdr *message, int flags // int fd, struct msghdr *msg, int flags
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
#ifdef DYNAMIC_LIB #ifdef DYNAMIC_LIB
@@ -308,25 +308,25 @@ int (*realclose)(CLOSE_SIG);
ssize_t zts_recvmsg(RECVMSG_SIG) ssize_t zts_recvmsg(RECVMSG_SIG)
#endif #endif
{ {
dwr(MSG_DEBUG_EXTRA, "zt_recvmsg(%d)\n", socket); dwr(MSG_DEBUG_EXTRA, "zt_recvmsg(%d)\n", fd);
ssize_t err, n, tot_len = 0; ssize_t err, n, tot_len = 0;
char *buf, *p; char *buf, *p;
struct iovec *iov = message->msg_iov; struct iovec *iov = msg->msg_iov;
for(int i = 0; i < message->msg_iovlen; ++i) for(int i = 0; i < msg->msg_iovlen; ++i)
tot_len += iov[i].iov_len; tot_len += iov[i].iov_len;
buf = malloc(tot_len); buf = malloc(tot_len);
if(tot_len != 0 && buf == NULL) { if(tot_len != 0 && buf == NULL) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
n = err = recvfrom(socket, buf, tot_len, flags, message->msg_name, &message->msg_namelen); n = err = recvfrom(fd, buf, tot_len, flags, msg->msg_name, &msg->msg_namelen);
p = buf; p = buf;
// According to: http://pubs.opengroup.org/onlinepubs/009695399/functions/recvmsg.html // According to: http://pubs.opengroup.org/onlinepubs/009695399/functions/recvmsg.html
if(err > message->msg_controllen && !( message->msg_flags & MSG_PEEK)) { if(err > msg->msg_controllen && !( msg->msg_flags & MSG_PEEK)) {
// excess data should be disgarded // excess data should be disgarded
message->msg_flags |= MSG_TRUNC; // Indicate that the buffer has been truncated msg->msg_flags |= MSG_TRUNC; // Indicate that the buffer has been truncated
} }
while (n > 0) { while (n > 0) {
@@ -388,8 +388,7 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// --------------------------------- setsockopt() ------------------------------- // --------------------------------- setsockopt() -------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int socket, int level, int option_name, const void *option_value, // int fd, int level, int optname, const void *optval, socklen_t optlen
// socklen_t option_len
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1setsockopt( JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1setsockopt(
@@ -404,15 +403,14 @@ int (*realclose)(CLOSE_SIG);
int zts_setsockopt(SETSOCKOPT_SIG) int zts_setsockopt(SETSOCKOPT_SIG)
#endif #endif
{ {
dwr(MSG_DEBUG, "zt_setsockopt()\n"); dwr(MSG_DEBUG, "zt_setsockopt(%d)\n", fd);
return 0; return 0;
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// --------------------------------- getsockopt() ------------------------------- // --------------------------------- getsockopt() -------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, int level, int optname, void *optval, // int fd, int level, int optname, void *optval, socklen_t *optlen
// socklen_t *optlen
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getsockopt( JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getsockopt(
@@ -427,7 +425,7 @@ int (*realclose)(CLOSE_SIG);
int zts_getsockopt(GETSOCKOPT_SIG) int zts_getsockopt(GETSOCKOPT_SIG)
#endif #endif
{ {
dwr(MSG_DEBUG,"zt_getsockopt(%d)\n", sockfd); dwr(MSG_DEBUG,"zt_getsockopt(%d)\n", fd);
if(optname == SO_TYPE) { if(optname == SO_TYPE) {
int* val = (int*)optval; int* val = (int*)optval;
*val = 2; *val = 2;
@@ -455,7 +453,7 @@ int (*realclose)(CLOSE_SIG);
{ {
get_api_netpath(); get_api_netpath();
dwr(MSG_DEBUG, "zt_socket()\n"); dwr(MSG_DEBUG, "zt_socket()\n");
/* Check that type makes sense */ // Check that type makes sense
#if defined(__linux__) #if defined(__linux__)
int flags = socket_type & ~SOCK_TYPE_MASK; int flags = socket_type & ~SOCK_TYPE_MASK;
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
@@ -466,7 +464,7 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
#endif #endif
socket_type &= SOCK_TYPE_MASK; socket_type &= SOCK_TYPE_MASK;
/* Check protocol is in range */ // Check protocol is in range
#if defined(__linux__) #if defined(__linux__)
if (socket_family < 0 || socket_family >= NPROTO){ if (socket_family < 0 || socket_family >= NPROTO){
errno = EAFNOSUPPORT; errno = EAFNOSUPPORT;
@@ -477,19 +475,19 @@ int (*realclose)(CLOSE_SIG);
return -1; return -1;
} }
#endif #endif
/* Assemble and send RPC */ // Assemble and send RPC
struct socket_st rpc_st; struct socket_st rpc_st;
rpc_st.socket_family = socket_family; rpc_st.socket_family = socket_family;
rpc_st.socket_type = socket_type; rpc_st.socket_type = socket_type;
rpc_st.protocol = protocol; rpc_st.protocol = protocol;
#if defined(__linux__) #if defined(__linux__)
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
rpc_st.__tid = 5; //syscall(SYS_gettid); rpc_st.tid = 5; //syscall(SYS_gettid);
#else #else
rpc_st.__tid = gettid(); // dummy value rpc_st.tid = gettid(); // dummy value
#endif #endif
#endif #endif
/* -1 is passed since we we're generating the new socket in this call */ // -1 is passed since we we're generating the new socket in this call
printf("api_netpath = %s\n", api_netpath); printf("api_netpath = %s\n", api_netpath);
int err = rpc_send_command(api_netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st)); int err = rpc_send_command(api_netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st));
dwr(MSG_DEBUG," socket() = %d\n", err); dwr(MSG_DEBUG," socket() = %d\n", err);
@@ -499,7 +497,7 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ---------------------------------- connect() --------------------------------- // ---------------------------------- connect() ---------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int __fd, const struct sockaddr * __addr, socklen_t __len // int fd, const struct sockaddr *addr, socklen_t addrlen
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) {
@@ -521,25 +519,25 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
{ {
get_api_netpath(); get_api_netpath();
dwr(MSG_DEBUG,"zt_connect(%d)\n", __fd); dwr(MSG_DEBUG,"zt_connect(%d)\n", fd);
struct connect_st rpc_st; struct connect_st rpc_st;
#if defined(__linux__) #if defined(__linux__)
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
//rpc_st.__tid = syscall(SYS_gettid); //rpc_st.tid = syscall(SYS_gettid);
#else #else
//rpc_st.__tid = gettid(); // dummy value //rpc_st.tid = gettid(); // dummy value
#endif #endif
#endif #endif
rpc_st.__fd = __fd; rpc_st.fd = fd;
memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr_storage)); memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr_storage));
memcpy(&rpc_st.__len, &__len, sizeof(socklen_t)); memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
return rpc_send_command(api_netpath, RPC_CONNECT, __fd, &rpc_st, sizeof(struct connect_st)); return rpc_send_command(api_netpath, RPC_CONNECT, fd, &rpc_st, sizeof(struct connect_st));
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ------------------------------------ bind() ---------------------------------- // ------------------------------------ bind() ----------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, const struct sockaddr *addr, socklen_t addrlen // int fd, const struct sockaddr *addr, socklen_t addrlen
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) {
@@ -561,26 +559,26 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
{ {
get_api_netpath(); get_api_netpath();
dwr(MSG_DEBUG,"zt_bind(%d)\n", sockfd); dwr(MSG_DEBUG,"zt_bind(%d)\n", fd);
struct bind_st rpc_st; struct bind_st rpc_st;
rpc_st.sockfd = sockfd; rpc_st.fd = fd;
#if defined(__linux__) #if defined(__linux__)
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
// TODO: Candidate for removal // TODO: Candidate for removal
rpc_st.__tid = 5;//syscall(SYS_gettid); rpc_st.tid = 5;//syscall(SYS_gettid);
#else #else
rpc_st.__tid = gettid(); // dummy value rpc_st.tid = gettid(); // dummy value
#endif #endif
#endif #endif
memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr_storage)); memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr_storage));
memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t)); memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
return rpc_send_command(api_netpath, RPC_BIND, sockfd, &rpc_st, sizeof(struct bind_st)); return rpc_send_command(api_netpath, RPC_BIND, fd, &rpc_st, sizeof(struct bind_st));
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ----------------------------------- accept4() -------------------------------- // ----------------------------------- accept4() --------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags // int fd, struct sockaddr *addr, socklen_t *addrlen, int flags
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept4(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port, jint flags) { JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept4(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port, jint flags) {
@@ -603,22 +601,22 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
{ {
get_api_netpath(); get_api_netpath();
dwr(MSG_DEBUG,"zt_accept4(%d):\n", sockfd); dwr(MSG_DEBUG,"zt_accept4(%d):\n", fd);
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
if ((flags & SOCK_CLOEXEC)) if ((flags & SOCK_CLOEXEC))
fcntl(sockfd, F_SETFL, FD_CLOEXEC); fcntl(fd, F_SETFL, FD_CLOEXEC);
if ((flags & SOCK_NONBLOCK)) if ((flags & SOCK_NONBLOCK))
fcntl(sockfd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFL, O_NONBLOCK);
#endif #endif
int len = !addr ? 0 : addrlen; int len = !addr ? 0 : addrlen;
return accept(sockfd, addr, len); return accept(fd, addr, len);
} }
#endif #endif
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ----------------------------------- accept() --------------------------------- // ----------------------------------- accept() ---------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd struct sockaddr *addr, socklen_t *addrlen // int fd struct sockaddr *addr, socklen_t *addrlen
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) { JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) {
@@ -638,13 +636,13 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
{ {
get_api_netpath(); get_api_netpath();
dwr(MSG_DEBUG,"zt_accept(%d):\n", sockfd); dwr(MSG_DEBUG,"zt_accept(%d):\n", fd);
// FIXME: Find a better solution for this before production // FIXME: Find a better solution for this before production
#if !defined(__UNITY_3D__) #if !defined(__UNITY_3D__)
if(addr) if(addr)
addr->sa_family = AF_INET; addr->sa_family = AF_INET;
#endif #endif
int new_fd = get_new_fd(sockfd); int new_fd = get_new_fd(fd);
dwr(MSG_DEBUG,"newfd = %d\n", new_fd); dwr(MSG_DEBUG,"newfd = %d\n", new_fd);
if(new_fd > 0) { if(new_fd > 0) {
@@ -658,7 +656,7 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// ------------------------------------- listen()-------------------------------- // ------------------------------------- listen()--------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, int backlog // int fd, int backlog
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1listen(JNIEnv *env, jobject thisObj, jint fd, int backlog) { JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1listen(JNIEnv *env, jobject thisObj, jint fd, int backlog) {
@@ -673,18 +671,18 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
{ {
get_api_netpath(); get_api_netpath();
dwr(MSG_DEBUG,"zt_listen(%d):\n", sockfd); dwr(MSG_DEBUG,"zt_listen(%d):\n", fd);
struct listen_st rpc_st; struct listen_st rpc_st;
rpc_st.sockfd = sockfd; rpc_st.fd = fd;
rpc_st.backlog = backlog; rpc_st.backlog = backlog;
#if defined(__linux__) #if defined(__linux__)
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
rpc_st.__tid = syscall(SYS_gettid); rpc_st.tid = syscall(SYS_gettid);
#else #else
rpc_st.__tid = gettid(); // dummy value rpc_st.tid = gettid(); // dummy value
#endif #endif
#endif #endif
return rpc_send_command(api_netpath, RPC_LISTEN, sockfd, &rpc_st, sizeof(struct listen_st)); return rpc_send_command(api_netpath, RPC_LISTEN, fd, &rpc_st, sizeof(struct listen_st));
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@@ -712,7 +710,7 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// -------------------------------- getsockname() ------------------------------- // -------------------------------- getsockname() -------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, struct sockaddr *addr, socklen_t *addrlen // int fd, struct sockaddr *addr, socklen_t *addrlen
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getsockname(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr) { JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getsockname(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr) {
@@ -735,11 +733,11 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
{ {
get_api_netpath(); get_api_netpath();
dwr(MSG_DEBUG_EXTRA,"zt_getsockname(%d):\n", sockfd); dwr(MSG_DEBUG_EXTRA,"zt_getsockname(%d):\n", fd);
struct getsockname_st rpc_st; struct getsockname_st rpc_st;
rpc_st.sockfd = sockfd; rpc_st.fd = fd;
memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t)); memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
int rpcfd = rpc_send_command(api_netpath, RPC_GETSOCKNAME, sockfd, &rpc_st, sizeof(struct getsockname_st)); int rpcfd = rpc_send_command(api_netpath, RPC_GETSOCKNAME, fd, &rpc_st, sizeof(struct getsockname_st));
// read address info from service // read address info from service
char addrbuf[sizeof(struct sockaddr_storage)]; char addrbuf[sizeof(struct sockaddr_storage)];
memset(&addrbuf, 0, sizeof(struct sockaddr_storage)); memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
@@ -774,7 +772,7 @@ int (*realclose)(CLOSE_SIG);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// -------------------------------- getpeername() ------------------------------- // -------------------------------- getpeername() -------------------------------
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int sockfd, struct sockaddr *addr, socklen_t *addrlen // int fd, struct sockaddr *addr, socklen_t *addrlen
#if defined(__ANDROID__) #if defined(__ANDROID__)
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getpeername(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr) { JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1getpeername(JNIEnv *env, jobject thisObj, jint fd, jobject ztaddr) {
@@ -797,11 +795,11 @@ int (*realclose)(CLOSE_SIG);
#endif #endif
{ {
get_api_netpath(); get_api_netpath();
dwr(MSG_DEBUG_EXTRA,"zt_getpeername(%d):\n", sockfd); dwr(MSG_DEBUG_EXTRA,"zt_getpeername(%d):\n", fd);
struct getsockname_st rpc_st; struct getsockname_st rpc_st;
rpc_st.sockfd = sockfd; rpc_st.fd = fd;
memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t)); memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
int rpcfd = rpc_send_command(api_netpath, RPC_GETPEERNAME, sockfd, &rpc_st, sizeof(struct getsockname_st)); int rpcfd = rpc_send_command(api_netpath, RPC_GETPEERNAME, fd, &rpc_st, sizeof(struct getsockname_st));
// read address info from service // read address info from service
char addrbuf[sizeof(struct sockaddr_storage)]; char addrbuf[sizeof(struct sockaddr_storage)];
memset(&addrbuf, 0, sizeof(struct sockaddr_storage)); memset(&addrbuf, 0, sizeof(struct sockaddr_storage));

View File

@@ -28,4 +28,4 @@
#ifndef SDK_XCODE_WRAPPER_HPP #ifndef SDK_XCODE_WRAPPER_HPP
#define SDK_XCODE_WRAPPER_HPP #define SDK_XCODE_WRAPPER_HPP
#endif /* SDK_XCODE_WRAPPER_HPP */ #endif // SDK_XCODE_WRAPPER_HPP

View File

@@ -6,12 +6,7 @@ import java.net.InetSocketAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/* // A convenience object for moving address information across the JNI memory border.
The ZTAddress object is merely a convenience object for moving address information
across the JNI memory border.
*/
public class ZTAddress public class ZTAddress
{ {

View File

@@ -68,7 +68,7 @@ else
endif endif
# Debug output for Network Containers # Debug output for Network Containers
# Specific levels can be controlled in netcon/common.inc.c # Specific levels can be controlled in src/SDK_Debug.h
ifeq ($(SDK_DEBUG),1) ifeq ($(SDK_DEBUG),1)
DEFS+=-DSDK_DEBUG DEFS+=-DSDK_DEBUG
endif endif