2017-04-06 19:16:01 -07:00
|
|
|
/*
|
2017-05-04 15:53:38 -07:00
|
|
|
* ZeroTier SDK - Network Virtualization Everywhere
|
2019-01-14 12:01:29 -08:00
|
|
|
* Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
|
2017-04-06 19:16:01 -07:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-01-14 12:01:29 -08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-04 15:35:50 -07:00
|
|
|
*
|
|
|
|
|
* --
|
|
|
|
|
*
|
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
|
* of your own application.
|
2017-04-06 19:16:01 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-09-27 02:29:04 -07:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
2019-01-14 12:01:29 -08:00
|
|
|
* ZeroTier Socket API
|
2017-09-27 02:29:04 -07:00
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
#include <string.h>
|
2017-05-05 16:46:07 -07:00
|
|
|
|
2017-09-27 02:29:04 -07:00
|
|
|
#include "lwip/sockets.h"
|
2019-02-06 22:00:39 -08:00
|
|
|
#include "lwip/def.h"
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
#include "libzt.h"
|
2019-02-06 22:00:39 -08:00
|
|
|
#include "Options.h"
|
2019-01-14 12:01:29 -08:00
|
|
|
#include "Debug.hpp"
|
2019-02-06 22:00:39 -08:00
|
|
|
#include "Controls.hpp"
|
2019-01-14 12:01:29 -08:00
|
|
|
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
|
//#include <sys/socket.h>
|
|
|
|
|
//#include <sys/types.h>
|
|
|
|
|
//#include <sys/select.h>
|
|
|
|
|
//#include <sys/ioctl.h>
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-02-06 22:00:39 -08:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Socket API //
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2017-10-18 17:57:51 -07:00
|
|
|
|
2019-02-06 22:00:39 -08:00
|
|
|
extern bool _run_service;
|
|
|
|
|
extern bool _run_lwip_tcpip;
|
|
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
2017-10-18 17:57:51 -07:00
|
|
|
#endif
|
|
|
|
|
|
2019-02-06 22:00:39 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
void ss2zta(JNIEnv *env, struct sockaddr_storage *ss, jobject addr);
|
|
|
|
|
void zta2ss(JNIEnv *env, struct sockaddr_storage *ss, jobject addr);
|
|
|
|
|
void ztfdset2fdset(JNIEnv *env, int nfds, jobject src_ztfd_set, fd_set *dest_fd_set);
|
|
|
|
|
void fdset2ztfdset(JNIEnv *env, int nfds, fd_set *src_fd_set, jobject dest_ztfd_set);
|
|
|
|
|
#endif
|
2019-01-14 12:01:29 -08:00
|
|
|
|
|
|
|
|
// Copied from lwip/src/include/sockets.h and renamed to prevent a name collision
|
|
|
|
|
// with system definitions
|
|
|
|
|
struct lwip_sockaddr {
|
|
|
|
|
u8_t sa_len;
|
|
|
|
|
sa_family_t sa_family;
|
|
|
|
|
char sa_data[14];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// ZeroTier Socket API //
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2019-02-06 22:00:39 -08:00
|
|
|
extern int zts_errno;
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_socket(int socket_family, int socket_type, int protocol)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_socket(socket_family, socket_type, protocol);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_socket(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol)
|
|
|
|
|
{
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_socket(family, type, protocol);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno); // Encode lwip errno in return value for JNI functions only
|
2017-04-07 17:56:05 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
|
2019-01-14 12:01:29 -08:00
|
|
|
{
|
|
|
|
|
if (!addr) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (addrlen > (int)sizeof(struct sockaddr_storage) || addrlen < (int)sizeof(struct sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_connect(fd, addr, addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_connect(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jobject addr)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2017-09-27 02:29:04 -07:00
|
|
|
struct sockaddr_storage ss;
|
2019-01-14 12:01:29 -08:00
|
|
|
zta2ss(env, &ss, addr);
|
|
|
|
|
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_connect(fd, (struct sockaddr *)&ss, addrlen);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
|
2019-01-14 12:01:29 -08:00
|
|
|
{
|
|
|
|
|
if (!addr) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (addrlen > (int)sizeof(struct sockaddr_storage) || addrlen < (int)sizeof(struct sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_bind(fd, addr, addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_bind(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jobject addr)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2017-09-27 02:29:04 -07:00
|
|
|
struct sockaddr_storage ss;
|
2019-01-14 12:01:29 -08:00
|
|
|
zta2ss(env, &ss, addr);
|
|
|
|
|
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_bind(fd, (struct sockaddr*)&ss, addrlen);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_listen(int fd, int backlog)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_listen(fd, backlog);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_listen(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, int backlog)
|
|
|
|
|
{
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_listen(fd, backlog);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-09-22 14:14:14 -07:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
2017-09-27 02:29:04 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_accept(fd, addr, addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_accept(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
|
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval =zts_accept(fd, (struct sockaddr *)&ss, &addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
ss2zta(env, &ss, addr);
|
2019-01-25 12:42:53 -08:00
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-04-07 17:56:05 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2017-04-14 17:23:28 -07:00
|
|
|
#if defined(__linux__)
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags)
|
2017-09-14 13:17:37 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_INVALID_OP : ZTS_ERR_INVALID_OP;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_accept4(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port, jint flags)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
|
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_accept4(fd, (struct sockaddr *)&ss, &addrlen, flags);
|
2019-01-14 12:01:29 -08:00
|
|
|
ss2zta(env, &ss, addr);
|
2019-01-25 12:42:53 -08:00
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-09-14 13:17:37 -07:00
|
|
|
}
|
2017-04-14 17:23:28 -07:00
|
|
|
#endif
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_setsockopt(fd, level, optname, optval, optlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_setsockopt(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jobject optval)
|
|
|
|
|
{
|
|
|
|
|
jclass c = (*env).GetObjectClass(optval);
|
|
|
|
|
if (!c) {
|
|
|
|
|
return ZTS_ERR_INVALID_OP;
|
|
|
|
|
}
|
|
|
|
|
int optval_int = -1;
|
|
|
|
|
|
|
|
|
|
if (optname == ZTS_SO_BROADCAST
|
|
|
|
|
|| optname == ZTS_SO_KEEPALIVE
|
|
|
|
|
|| optname == ZTS_SO_REUSEADDR
|
|
|
|
|
|| optname == ZTS_SO_REUSEPORT
|
|
|
|
|
|| optname == ZTS_TCP_NODELAY)
|
|
|
|
|
{
|
2019-01-25 12:42:53 -08:00
|
|
|
jfieldID fid = (*env).GetFieldID(c, "booleanValue", "Z");
|
2019-01-14 12:01:29 -08:00
|
|
|
optval_int = (int)(*env).GetBooleanField(optval, fid);
|
|
|
|
|
}
|
2019-01-25 12:42:53 -08:00
|
|
|
if (optname == ZTS_IP_TTL
|
|
|
|
|
|| optname == ZTS_SO_RCVTIMEO
|
2019-01-14 12:01:29 -08:00
|
|
|
|| optname == ZTS_IP_TOS
|
|
|
|
|
|| optname == ZTS_SO_LINGER
|
|
|
|
|
|| optname == ZTS_SO_RCVBUF
|
|
|
|
|
|| optname == ZTS_SO_SNDBUF)
|
|
|
|
|
{
|
|
|
|
|
jfieldID fid = (*env).GetFieldID(c, "integerValue", "I");
|
|
|
|
|
optval_int = (*env).GetIntField(optval, fid);
|
|
|
|
|
}
|
2019-01-25 12:42:53 -08:00
|
|
|
|
|
|
|
|
zts_err_t retval = ZTS_ERR_OK;
|
|
|
|
|
|
|
|
|
|
if (optname == ZTS_SO_RCVTIMEO) {
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
// Convert milliseconds from setSoTimeout() call to seconds and microseconds
|
|
|
|
|
tv.tv_usec = optval_int * 1000;
|
|
|
|
|
tv.tv_sec = optval_int / 1000000;
|
|
|
|
|
retval = zts_setsockopt(fd, level, optname, &tv, sizeof(tv));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
retval = zts_setsockopt(fd, level, optname, &optval_int, sizeof(optval_int));
|
|
|
|
|
}
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_getsockopt(fd, level, optname, optval, optlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_getsockopt(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jobject optval)
|
|
|
|
|
{
|
|
|
|
|
jclass c = (*env).GetObjectClass(optval);
|
|
|
|
|
if (!c) {
|
|
|
|
|
return ZTS_ERR_INVALID_OP;
|
|
|
|
|
}
|
2019-01-25 12:42:53 -08:00
|
|
|
int optval_int = 0;
|
2019-01-14 12:01:29 -08:00
|
|
|
int optlen; // Intentionally not used
|
2019-01-25 12:42:53 -08:00
|
|
|
|
|
|
|
|
zts_err_t retval;
|
|
|
|
|
|
|
|
|
|
if (optname == ZTS_SO_RCVTIMEO) {
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
optlen = sizeof(tv);
|
|
|
|
|
retval = zts_getsockopt(fd, level, optname, &tv, &optlen);
|
|
|
|
|
// Convert seconds and microseconds back to milliseconds
|
|
|
|
|
optval_int = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
retval = zts_getsockopt(fd, level, optname, &optval_int, &optlen);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
if (optname == ZTS_SO_BROADCAST
|
|
|
|
|
|| optname == ZTS_SO_KEEPALIVE
|
|
|
|
|
|| optname == ZTS_SO_REUSEADDR
|
|
|
|
|
|| optname == ZTS_SO_REUSEPORT
|
|
|
|
|
|| optname == ZTS_TCP_NODELAY)
|
|
|
|
|
{
|
2019-01-25 12:42:53 -08:00
|
|
|
jfieldID fid = (*env).GetFieldID(c, "isBoolean", "Z");
|
2019-01-14 12:01:29 -08:00
|
|
|
(*env).SetBooleanField(optval, fid, true);
|
2019-01-25 12:42:53 -08:00
|
|
|
fid = (*env).GetFieldID(c, "booleanValue", "Z");
|
2019-01-14 12:01:29 -08:00
|
|
|
(*env).SetBooleanField(optval, fid, (bool)optval_int);
|
|
|
|
|
}
|
2019-01-25 12:42:53 -08:00
|
|
|
if (optname == ZTS_IP_TTL
|
|
|
|
|
|| optname == ZTS_SO_RCVTIMEO
|
2019-01-14 12:01:29 -08:00
|
|
|
|| optname == ZTS_IP_TOS
|
|
|
|
|
|| optname == ZTS_SO_LINGER
|
|
|
|
|
|| optname == ZTS_SO_RCVBUF
|
|
|
|
|
|| optname == ZTS_SO_SNDBUF)
|
|
|
|
|
{
|
2019-01-25 12:42:53 -08:00
|
|
|
jfieldID fid = (*env).GetFieldID(c, "isInteger", "Z");
|
2019-01-14 12:01:29 -08:00
|
|
|
(*env).SetBooleanField(optval, fid, true);
|
|
|
|
|
fid = (*env).GetFieldID(c, "integerValue", "I");
|
|
|
|
|
(*env).SetIntField(optval, fid, optval_int);
|
|
|
|
|
}
|
2019-01-25 12:42:53 -08:00
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!addr) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (*addrlen > (int)sizeof(struct sockaddr_storage) || *addrlen < (int)sizeof(struct sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_getsockname(fd, addr, addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_getsockname(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jobject addr)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
|
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval =zts_getsockname(fd, (struct sockaddr *)&ss, &addrlen);
|
|
|
|
|
ss2zta(env, &ss, addr);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!addr) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (*addrlen > (int)sizeof(struct sockaddr_storage) || *addrlen < (int)sizeof(struct sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_getpeername(fd, addr, addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_getpeername(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jobject addr)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_storage ss;
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_getpeername(fd, (struct sockaddr *)&ss, (socklen_t *)sizeof(struct sockaddr_storage));
|
|
|
|
|
ss2zta(env, &ss, addr);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-09-22 14:14:14 -07:00
|
|
|
|
|
|
|
|
int zts_gethostname(char *name, size_t len)
|
2017-08-18 07:43:29 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_INVALID_OP : ZTS_ERR_INVALID_OP; // TODO
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#endif
|
2017-08-18 07:43:29 -07:00
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_sethostname(const char *name, size_t len)
|
2017-08-18 07:43:29 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_INVALID_OP : ZTS_ERR_INVALID_OP; // TODO
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#endif
|
2017-08-18 07:43:29 -07:00
|
|
|
|
2019-02-06 22:00:39 -08:00
|
|
|
/*
|
2017-10-09 00:07:31 -07:00
|
|
|
struct hostent *zts_gethostbyname(const char *name)
|
|
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (struct hostent *)((!_run_service || !_run_lwip_tcpip) ? NULL : NULL);
|
2017-10-10 14:59:55 -07:00
|
|
|
// TODO: Test thread safety
|
2017-10-09 00:07:31 -07:00
|
|
|
char buf[256];
|
|
|
|
|
int buflen = 256;
|
|
|
|
|
int h_err = 0;
|
|
|
|
|
struct hostent hret;
|
|
|
|
|
struct hostent **result = NULL;
|
|
|
|
|
int err = 0;
|
|
|
|
|
if ((err = lwip_gethostbyname_r(name, &hret, buf, buflen, result, &h_err)) != 0) {
|
|
|
|
|
DEBUG_ERROR("err = %d", err);
|
|
|
|
|
DEBUG_ERROR("h_err = %d", h_err);
|
|
|
|
|
errno = h_err;
|
|
|
|
|
return NULL; // failure
|
|
|
|
|
}
|
|
|
|
|
return *result;
|
2017-12-07 16:45:02 -08:00
|
|
|
|
2017-10-09 00:07:31 -07:00
|
|
|
return lwip_gethostbyname(name);
|
|
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#endif
|
2019-02-06 22:00:39 -08:00
|
|
|
*/
|
2017-10-09 00:07:31 -07:00
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_close(int fd)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_close(fd);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_close(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd)
|
|
|
|
|
{
|
|
|
|
|
return zts_close(fd);
|
2017-07-12 11:44:31 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-07-12 11:44:31 -07:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
2017-09-29 15:37:50 -07:00
|
|
|
struct timeval *timeout)
|
2017-07-12 11:44:31 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_select(nfds, readfds, writefds, exceptfds, timeout);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_select(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint nfds, jobject readfds, jobject writefds, jobject exceptfds, jint timeout_sec, jint timeout_usec)
|
|
|
|
|
{
|
|
|
|
|
struct timeval _timeout;
|
|
|
|
|
_timeout.tv_sec = timeout_sec;
|
2019-01-25 12:42:53 -08:00
|
|
|
_timeout.tv_usec = timeout_usec;
|
|
|
|
|
fd_set _readfds, _writefds, _exceptfds;
|
|
|
|
|
fd_set *r = NULL;
|
|
|
|
|
fd_set *w = NULL;
|
|
|
|
|
fd_set *e = NULL;
|
|
|
|
|
if (readfds) {
|
|
|
|
|
r = &_readfds;
|
|
|
|
|
ztfdset2fdset(env, nfds, readfds, &_readfds);
|
|
|
|
|
}
|
|
|
|
|
if (writefds) {
|
|
|
|
|
w = &_writefds;
|
|
|
|
|
ztfdset2fdset(env, nfds, writefds, &_writefds);
|
|
|
|
|
}
|
|
|
|
|
if (exceptfds) {
|
|
|
|
|
e = &_exceptfds;
|
|
|
|
|
ztfdset2fdset(env, nfds, exceptfds, &_exceptfds);
|
|
|
|
|
}
|
|
|
|
|
zts_err_t retval = zts_select(nfds, r, w, e, &_timeout);
|
2019-01-14 12:01:29 -08:00
|
|
|
if (readfds) {
|
|
|
|
|
fdset2ztfdset(env, nfds, &_readfds, readfds);
|
|
|
|
|
}
|
|
|
|
|
if (writefds) {
|
2019-01-25 12:42:53 -08:00
|
|
|
fdset2ztfdset(env, nfds, &_writefds, writefds);
|
|
|
|
|
}
|
|
|
|
|
if (exceptfds) {
|
|
|
|
|
fdset2ztfdset(env, nfds, &_exceptfds, exceptfds);
|
|
|
|
|
}
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-07-12 11:44:31 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-07-12 11:44:31 -07:00
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_fcntl(int fd, int cmd, int flags)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-10-18 17:57:51 -07:00
|
|
|
// translation from platform flag values to stack flag values
|
2017-09-27 02:29:04 -07:00
|
|
|
int translated_flags = 0;
|
2017-10-18 17:57:51 -07:00
|
|
|
#if defined(__linux__)
|
2017-09-27 02:29:04 -07:00
|
|
|
if (flags == 2048) {
|
|
|
|
|
translated_flags = 1;
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2017-10-18 17:57:51 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
if (flags == 4) {
|
|
|
|
|
translated_flags = 1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_fcntl(fd, cmd, translated_flags);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_fcntl(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jint cmd, jint flags)
|
|
|
|
|
{
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_fcntl(fd, cmd, flags);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-09-22 14:14:14 -07:00
|
|
|
|
|
|
|
|
int zts_ioctl(int fd, unsigned long request, void *argp)
|
2017-08-02 14:39:21 -07:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!argp) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_ioctl(fd, request, argp);
|
2017-08-02 14:39:21 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT int JNICALL Java_com_zerotier_libzt_ZeroTier_ioctl(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jlong request, jobject argp)
|
|
|
|
|
{
|
|
|
|
|
zts_err_t retval = ZTS_ERR_OK;
|
|
|
|
|
if (request == FIONREAD) {
|
|
|
|
|
DEBUG_ERROR("FIONREAD");
|
|
|
|
|
int bytesRemaining = 0;
|
|
|
|
|
retval = zts_ioctl(fd, request, &bytesRemaining);
|
|
|
|
|
// set value in general object
|
|
|
|
|
jclass c = (*env).GetObjectClass(argp);
|
|
|
|
|
if (!c) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
|
|
|
|
jfieldID fid = (*env).GetFieldID(c, "integer", "I");
|
|
|
|
|
(*env).SetIntField(argp, fid, bytesRemaining);
|
|
|
|
|
}
|
|
|
|
|
if (request == FIONBIO) {
|
|
|
|
|
// TODO: double check
|
|
|
|
|
int meaninglessVariable = 0;
|
|
|
|
|
DEBUG_ERROR("FIONBIO");
|
|
|
|
|
retval = zts_ioctl(fd, request, &meaninglessVariable);
|
|
|
|
|
}
|
2019-01-25 12:42:53 -08:00
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
ssize_t zts_send(int fd, const void *buf, size_t len, int flags)
|
|
|
|
|
{
|
|
|
|
|
if (!buf || len <= 0) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_send(fd, buf, len, flags);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_send(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, int flags)
|
|
|
|
|
{
|
|
|
|
|
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_send(fd, data, env->GetArrayLength(buf), flags);
|
|
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#endif
|
2017-08-02 14:39:21 -07:00
|
|
|
|
2018-07-27 15:52:15 -07:00
|
|
|
ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags,
|
2017-09-29 15:37:50 -07:00
|
|
|
const struct sockaddr *addr, socklen_t addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!addr || !buf || len <= 0) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (addrlen > (int)sizeof(struct sockaddr_storage) || addrlen < (int)sizeof(struct sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_sendto(fd, buf, len, flags, addr, addrlen);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_sendto(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint flags, jobject addr)
|
2017-08-18 07:43:29 -07:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
|
zta2ss(env, &ss, addr);
|
|
|
|
|
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_sendto(fd, data, env->GetArrayLength(buf), flags, (struct sockaddr *)&ss, addrlen);
|
|
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-08-18 07:43:29 -07:00
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
ssize_t zts_sendmsg(int fd, const struct msghdr *msg, int flags)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_sendmsg(fd, msg, flags);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#endif
|
2017-09-22 14:14:14 -07:00
|
|
|
|
|
|
|
|
ssize_t zts_recv(int fd, void *buf, size_t len, int flags)
|
2017-08-18 07:43:29 -07:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_recv(fd, buf, len, flags);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_recv(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jbyteArray buf, jint flags)
|
|
|
|
|
{
|
|
|
|
|
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_recv(fd, data, env->GetArrayLength(buf), flags);
|
2019-01-14 12:01:29 -08:00
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
2019-01-25 12:42:53 -08:00
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-08-18 07:43:29 -07:00
|
|
|
|
2018-07-27 15:52:15 -07:00
|
|
|
ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags,
|
2017-09-29 15:37:50 -07:00
|
|
|
struct sockaddr *addr, socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_recvfrom(fd, buf, len, flags, addr, addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_recvfrom(
|
|
|
|
|
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint flags, jobject addr)
|
|
|
|
|
{
|
|
|
|
|
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
|
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_recvfrom(fd, data, env->GetArrayLength(buf), flags, (struct sockaddr *)&ss, &addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
2019-01-25 12:42:53 -08:00
|
|
|
ss2zta(env, &ss, addr);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2018-02-21 11:42:07 -08:00
|
|
|
ssize_t zts_recvmsg(int fd, struct msghdr *msg, int flags)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : -1; // Not currently implemented by stack
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#endif
|
2017-09-22 14:14:14 -07:00
|
|
|
|
2017-09-29 15:37:50 -07:00
|
|
|
int zts_read(int fd, void *buf, size_t len)
|
|
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_read(fd, buf, len);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
int zts_read_offset(int fd, void *buf, size_t offset, size_t len)
|
|
|
|
|
{
|
|
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
|
|
|
|
char *cbuf = (char*)buf;
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_read(fd, &(cbuf[offset]), len);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_read(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jbyteArray buf)
|
|
|
|
|
{
|
|
|
|
|
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_read(fd, data, env->GetArrayLength(buf));
|
|
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_read_1offset(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jbyteArray buf, jint offset, jint len)
|
|
|
|
|
{
|
|
|
|
|
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_read_offset(fd, data, offset, len);
|
|
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_read_1length(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jbyteArray buf, jint len)
|
|
|
|
|
{
|
|
|
|
|
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_read(fd, data, len);
|
|
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#endif
|
2017-09-22 14:14:14 -07:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
int zts_write(int fd, const void *buf, size_t len)
|
2017-09-29 15:37:50 -07:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!buf || len <= 0) {
|
|
|
|
|
return ZTS_ERR_INVALID_ARG;
|
|
|
|
|
}
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_write(fd, buf, len);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_write__IB(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jbyteArray buf)
|
|
|
|
|
{
|
|
|
|
|
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_write(fd, data, env->GetArrayLength(buf));
|
|
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_write_1offset(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jbyteArray buf, jint offset, jint len)
|
|
|
|
|
{
|
|
|
|
|
void *data = env->GetPrimitiveArrayCritical(&(buf[offset]), NULL); // PENDING: check?
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_write(fd, data, len);
|
|
|
|
|
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_write_1byte(JNIEnv *env, jobject thisObj,
|
|
|
|
|
jint fd, jbyte buf)
|
|
|
|
|
{
|
2019-01-25 12:42:53 -08:00
|
|
|
zts_err_t retval = zts_write(fd, &buf, 1);
|
|
|
|
|
return retval > -1 ? retval : -(zts_errno);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-09-22 14:14:14 -07:00
|
|
|
|
|
|
|
|
int zts_shutdown(int fd, int how)
|
2017-07-12 11:44:31 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_shutdown(fd, how);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
|
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_shutdown(
|
|
|
|
|
JNIEnv *env, jobject thisObj, int fd, int how)
|
|
|
|
|
{
|
|
|
|
|
return zts_shutdown(fd, how);
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif
|
2017-08-24 11:45:39 -07:00
|
|
|
|
|
|
|
|
int zts_add_dns_nameserver(struct sockaddr *addr)
|
|
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : -1; // TODO
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#endif
|
2017-08-24 11:45:39 -07:00
|
|
|
|
2017-08-30 14:13:13 -07:00
|
|
|
int zts_del_dns_nameserver(struct sockaddr *addr)
|
2017-08-24 11:45:39 -07:00
|
|
|
{
|
2019-02-06 22:00:39 -08:00
|
|
|
return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : -1; // TODO
|
2017-11-06 13:50:20 -08:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
#endif
|
2017-11-06 13:50:20 -08:00
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifdef SDK_JNI
|
|
|
|
|
void ztfdset2fdset(JNIEnv *env, int nfds, jobject src_ztfd_set, fd_set *dest_fd_set)
|
|
|
|
|
{
|
|
|
|
|
jclass c = (*env).GetObjectClass(src_ztfd_set);
|
|
|
|
|
if (!c) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
FD_ZERO(dest_fd_set);
|
|
|
|
|
jfieldID fid = env->GetFieldID(c, "fds_bits", "[B");
|
|
|
|
|
jobject fdData = (*env).GetObjectField (src_ztfd_set, fid);
|
|
|
|
|
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&fdData);
|
|
|
|
|
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
|
|
|
|
for (int i=0; i<nfds; i++) {
|
|
|
|
|
if (data[i] == 0x01) {
|
|
|
|
|
FD_SET(i, dest_fd_set);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void fdset2ztfdset(JNIEnv *env, int nfds, fd_set *src_fd_set, jobject dest_ztfd_set)
|
|
|
|
|
{
|
|
|
|
|
jclass c = (*env).GetObjectClass(dest_ztfd_set);
|
|
|
|
|
if (!c) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
jfieldID fid = env->GetFieldID(c, "fds_bits", "[B");
|
|
|
|
|
jobject fdData = (*env).GetObjectField (dest_ztfd_set, fid);
|
|
|
|
|
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&fdData);
|
|
|
|
|
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
|
|
|
|
for (int i=0; i<nfds; i++) {
|
|
|
|
|
if (FD_ISSET(i, src_fd_set)) {
|
|
|
|
|
data[i] = 0x01;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-11-06 13:50:20 -08:00
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Helpers (for moving data across the JNI barrier) //
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2017-11-06 13:50:20 -08:00
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
void ss2zta(JNIEnv *env, struct sockaddr_storage *ss, jobject addr)
|
2017-11-06 13:50:20 -08:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
jclass c = (*env).GetObjectClass(addr);
|
|
|
|
|
if (!c) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(ss->ss_family == AF_INET)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_in *in4 = (struct sockaddr_in*)ss;
|
|
|
|
|
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
2019-02-06 22:00:39 -08:00
|
|
|
(*env).SetIntField(addr, fid, lwip_ntohs(in4->sin_port));
|
2019-01-14 12:01:29 -08:00
|
|
|
fid = (*env).GetFieldID(c,"_family", "I");
|
|
|
|
|
(*env).SetIntField(addr, fid, (in4->sin_family));
|
|
|
|
|
fid = env->GetFieldID(c, "_ip4", "[B");
|
|
|
|
|
jobject ipData = (*env).GetObjectField (addr, fid);
|
|
|
|
|
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
|
|
|
|
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
|
|
|
|
memcpy(data, &(in4->sin_addr.s_addr), 4);
|
|
|
|
|
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(ss->ss_family == AF_INET6)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)ss;
|
|
|
|
|
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
2019-02-06 22:00:39 -08:00
|
|
|
(*env).SetIntField(addr, fid, lwip_ntohs(in6->sin6_port));
|
2019-01-14 12:01:29 -08:00
|
|
|
fid = (*env).GetFieldID(c,"_family", "I");
|
|
|
|
|
(*env).SetIntField(addr, fid, (in6->sin6_family));
|
|
|
|
|
fid = env->GetFieldID(c, "_ip6", "[B");
|
|
|
|
|
jobject ipData = (*env).GetObjectField (addr, fid);
|
|
|
|
|
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
|
|
|
|
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
|
|
|
|
memcpy(data, &(in6->sin6_addr.s6_addr), 16);
|
|
|
|
|
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-11-06 13:50:20 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
void zta2ss(JNIEnv *env, struct sockaddr_storage *ss, jobject addr)
|
2017-11-06 13:50:20 -08:00
|
|
|
{
|
2019-01-14 12:01:29 -08:00
|
|
|
jclass c = (*env).GetObjectClass(addr);
|
|
|
|
|
if (!c) {
|
|
|
|
|
return;
|
2017-11-06 13:50:20 -08:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
jfieldID fid = (*env).GetFieldID(c, "_family", "I");
|
|
|
|
|
int family = (*env).GetIntField(addr, fid);
|
|
|
|
|
if (family == AF_INET)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_in *in4 = (struct sockaddr_in*)ss;
|
|
|
|
|
fid = (*env).GetFieldID(c, "_port", "I");
|
2019-02-06 22:00:39 -08:00
|
|
|
in4->sin_port = lwip_htons((*env).GetIntField(addr, fid));
|
2019-01-14 12:01:29 -08:00
|
|
|
in4->sin_family = AF_INET;
|
|
|
|
|
fid = env->GetFieldID(c, "_ip4", "[B");
|
|
|
|
|
jobject ipData = (*env).GetObjectField (addr, fid);
|
|
|
|
|
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
|
|
|
|
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
|
|
|
|
memcpy(&(in4->sin_addr.s_addr), data, 4);
|
|
|
|
|
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (family == AF_INET6)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)ss;
|
|
|
|
|
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
2019-02-06 22:00:39 -08:00
|
|
|
in6->sin6_port = lwip_htons((*env).GetIntField(addr, fid));
|
2019-01-14 12:01:29 -08:00
|
|
|
fid = (*env).GetFieldID(c,"_family", "I");
|
|
|
|
|
in6->sin6_family = AF_INET6;
|
|
|
|
|
fid = env->GetFieldID(c, "_ip6", "[B");
|
|
|
|
|
jobject ipData = (*env).GetObjectField (addr, fid);
|
|
|
|
|
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
|
|
|
|
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
|
|
|
|
memcpy(&(in6->sin6_addr.s6_addr), data, 16);
|
|
|
|
|
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
|
|
|
|
return;
|
2017-11-06 13:50:20 -08:00
|
|
|
}
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
#endif // JNI
|
2017-08-02 14:39:21 -07:00
|
|
|
|
2017-04-06 19:16:01 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
2017-09-27 13:42:27 -07:00
|
|
|
#endif
|
2019-02-06 22:00:39 -08:00
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|