newInetAddress and newInetSocketAddress work

allow newInetSocketAddress to return NULL if given empty address
This commit is contained in:
Brenton Bostick 2023-02-01 16:01:16 -05:00
parent e1c16a8e68
commit 132bff25df
2 changed files with 27 additions and 53 deletions

View File

@ -132,6 +132,10 @@ jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr)
InetAddress_class, InetAddress_getByAddress_method, buff);
}
break;
default:
{
assert(false && "addr.ss_family is neither AF_INET6 nor AF_INET");
}
}
if(env->ExceptionCheck() || inetAddressObj == NULL) {
LOGE("Error creating InetAddress object");
@ -171,11 +175,16 @@ int addressPort(const sockaddr_storage addr) {
return port;
}
//
// addr may be empty
//
// may return NULL
//
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
{
LOGV("newInetSocketAddress Called");
if(addr.ss_family == 0)
if(isSocketAddressEmpty(addr))
{
return NULL;
}
@ -184,39 +193,15 @@ jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
if(env->ExceptionCheck() || inetAddressObject == NULL)
{
LOGE("Error creating new inet address");
return NULL;
}
int port = 0;
switch(addr.ss_family)
{
case AF_INET6:
{
LOGV("IPV6 Address");
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
port = ntohs(ipv6->sin6_port);
LOGV("Port %d", port);
}
break;
case AF_INET:
{
LOGV("IPV4 Address");
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
port = ntohs(ipv4->sin_port);
LOGV("Port: %d", port);
}
break;
default:
{
break;
}
}
int port = addressPort(addr);
jobject inetSocketAddressObject = env->NewObject(InetSocketAddress_class, InetSocketAddress_ctor, inetAddressObject, port);
if(env->ExceptionCheck() || inetSocketAddressObject == NULL) {
LOGE("Error creating InetSocketAddress object");
return NULL;
}
return inetSocketAddressObject;
}
@ -225,9 +210,11 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
{
LOGV("newPeerPhysicalPath Called");
//
// may be NULL
//
jobject addressObject = newInetSocketAddress(env, ppp.address);
if(env->ExceptionCheck() || addressObject == NULL) {
LOGE("Error creating InetSocketAddress object");
if(env->ExceptionCheck()) {
return NULL;
}

View File

@ -414,12 +414,18 @@ namespace {
return -100;
}
//
// may be NULL
//
jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
if (env->ExceptionCheck()) {
return -101;
}
const unsigned char *bytes = static_cast<const unsigned char *>(buffer);
jbyteArray bufferObj = newByteArray(env, bytes, bufferSize);
if (env->ExceptionCheck() || bufferObj == NULL)
{
return -101;
return -102;
}
int retval = env->CallIntMethod(ref->packetSender, PacketSender_onSendPacketRequested_method, localSocket, remoteAddressObj, bufferObj);
@ -446,30 +452,11 @@ namespace {
ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
//
// was:
// struct sockaddr_storage nullAddress = {0};
// may be NULL
//
// but was getting this warning:
// warning: suggest braces around initialization of subobject
//
// when building ZeroTierOne
//
struct sockaddr_storage nullAddress;
//
// It is possible to assume knowledge about internals of sockaddr_storage and construct
// correct 0-initializer, but it is simpler to just treat sockaddr_storage as opaque and
// use memset here to fill with 0
//
// This is also done in InetAddress.hpp for InetAddress
//
memset(&nullAddress, 0, sizeof(sockaddr_storage));
jobject remoteAddressObj = NULL;
if(memcmp(remoteAddress, &nullAddress, sizeof(sockaddr_storage)) != 0)
{
remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
if (env->ExceptionCheck()) {
return true;
}
return env->CallBooleanMethod(ref->pathChecker, PathChecker_onPathCheck_method, address, localSocket, remoteAddressObj);