#include <stdio.h>
#include <string.h>
-#include <assert.h>
#include <netdb.h>
#include <errno.h>
return NULL;
}
- size_t addrlen = env->GetArrayLength(javaAddress);
- jbyte* rawAddress = env->GetByteArrayElements(javaAddress, NULL);
- if (rawAddress == NULL) {
- throwNullPointerException(env);
- return NULL;
- }
-
// Convert the raw address bytes into a socket address structure.
- int ret = 0;
struct sockaddr_storage ss;
- struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
- struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
- size_t socklen;
memset(&ss, 0, sizeof(ss));
- switch (addrlen) {
- case 4:
- socklen = sizeof(struct sockaddr_in);
- sin->sin_family = AF_INET;
- memcpy(&sin->sin_addr.s_addr, rawAddress, addrlen);
- env->ReleaseByteArrayElements(javaAddress, rawAddress, JNI_ABORT);
- break;
- case 16:
- socklen = sizeof(struct sockaddr_in6);
- sin6->sin6_family = AF_INET6;
- memcpy(&sin6->sin6_addr.s6_addr, rawAddress, addrlen);
- env->ReleaseByteArrayElements(javaAddress, rawAddress, JNI_ABORT);
- break;
- default:
- // The caller already throws an exception in this case. Don't worry
- // about it here.
- env->ReleaseByteArrayElements(javaAddress, rawAddress, JNI_ABORT);
- return NULL;
+
+ size_t socklen;
+ const size_t addressLength = env->GetArrayLength(javaAddress);
+ if (addressLength == 4) {
+ struct sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&ss);
+ sin->sin_family = AF_INET;
+ socklen = sizeof(struct sockaddr_in);
+ jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr);
+ env->GetByteArrayRegion(javaAddress, 0, 4, dst);
+ } else if (addressLength == 16) {
+ struct sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6*>(&ss);
+ sin6->sin6_family = AF_INET6;
+ socklen = sizeof(struct sockaddr_in6);
+ jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr);
+ env->GetByteArrayRegion(javaAddress, 0, 16, dst);
+ } else {
+ // The caller already throws an exception in this case. Don't worry
+ // about it here.
+ return NULL;
}
// Look up the host name from the IP address.
char name[NI_MAXHOST];
- if (ret == 0) {
- ret = getnameinfo((struct sockaddr *) &ss, socklen, name, sizeof(name),
- NULL, 0, NI_NAMEREQD);
- }
-
- if (ret == 0) {
- return env->NewStringUTF(name);
+ int ret = getnameinfo(reinterpret_cast<sockaddr*>(&ss), socklen,
+ name, sizeof(name), NULL, 0, NI_NAMEREQD);
+ if (ret != 0) {
+ jniThrowException(env, "java/net/UnknownHostException", gai_strerror(ret));
+ return NULL;
}
- jniThrowException(env, "java/net/UnknownHostException", gai_strerror(ret));
- return NULL;
+ return env->NewStringUTF(name);
}
/*
static int structInToJavaAddress(
JNIEnv *env, struct in_addr *address, jbyteArray java_address) {
- if(java_address == NULL) {
+ if (java_address == NULL) {
throwNullPointerException(env);
return -1;
}
- if((*env)->GetArrayLength(env, java_address) != sizeof(address->s_addr)) {
+ if ((*env)->GetArrayLength(env, java_address) != sizeof(address->s_addr)) {
jniThrowIOException(env, errno);
return -1;
}
- jbyte *java_address_bytes;
-
- java_address_bytes = (*env)->GetByteArrayElements(env, java_address, NULL);
-
- memcpy(java_address_bytes, &(address->s_addr), sizeof(address->s_addr));
-
- (*env)->ReleaseByteArrayElements(env, java_address, java_address_bytes, 0);
-
+ jbyte* src = (jbyte*)(&(address->s_addr));
+ (*env)->SetByteArrayRegion(env, java_address, 0, sizeof(address->s_addr), src);
return 0;
}
*/
static int javaAddressToStructIn(
JNIEnv *env, jbyteArray java_address, struct in_addr *address) {
-
- memset(address, 0, sizeof(address));
-
if (java_address == NULL) {
return -1;
}
return -1;
}
- jbyte * java_address_bytes
- = env->GetByteArrayElements(java_address, NULL);
-
- memcpy(&(address->s_addr),
- java_address_bytes,
- sizeof(address->s_addr));
-
- env->ReleaseByteArrayElements(java_address, java_address_bytes, JNI_ABORT);
+ memset(address, 0, sizeof(address));
+ jbyte* dst = reinterpret_cast<jbyte*>(&(address->s_addr));
+ env->GetByteArrayRegion(java_address, 0, sizeof(address->s_addr), dst);
return 0;
}
*
* @exception SocketError if the address family is unknown
*/
-static int inetAddressToSocketAddress(JNIEnv *env,
- jobject inetaddress, int port, struct sockaddr_storage *sockaddress) {
-
+static int inetAddressToSocketAddress(JNIEnv *env, jobject inetaddress,
+ int port, struct sockaddr_storage *sockaddress) {
// Get the byte array that stores the IP address bytes in the InetAddress.
- jbyteArray addressByteArray;
- addressByteArray = (jbyteArray)env->GetObjectField(inetaddress,
- gCachedFields.iaddr_ipaddress);
- if (addressByteArray == NULL) {
- throwNullPointerException(env);
- return -1;
+ if (inetaddress == NULL) {
+ throwNullPointerException(env);
+ return -1;
}
-
- // Get the raw IP address bytes.
- jbyte *addressBytes = env->GetByteArrayElements(addressByteArray, NULL);
+ jbyteArray addressBytes =
+ reinterpret_cast<jbyteArray>(env->GetObjectField(inetaddress,
+ gCachedFields.iaddr_ipaddress));
if (addressBytes == NULL) {
- throwNullPointerException(env);
- return -1;
+ throwNullPointerException(env);
+ return -1;
}
// Convert the IP address bytes to the proper IP address type.
- size_t addressLength = env->GetArrayLength(addressByteArray);
- int result = 0;
+ size_t addressLength = env->GetArrayLength(addressBytes);
if (addressLength == 4) {
// IPv4 address.
- struct sockaddr_in *sin = (struct sockaddr_in *) sockaddress;
+ struct sockaddr_in *sin = reinterpret_cast<sockaddr_in*>(sockaddress);
memset(sin, 0, sizeof(struct sockaddr_in));
sin->sin_family = AF_INET;
sin->sin_port = htons(port);
- memcpy(&sin->sin_addr.s_addr, addressBytes, 4);
+ jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr);
+ env->GetByteArrayRegion(addressBytes, 0, 4, dst);
} else if (addressLength == 16) {
// IPv6 address.
- struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sockaddress;
+ struct sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6*>(sockaddress);
memset(sin6, 0, sizeof(struct sockaddr_in6));
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(port);
- memcpy(&sin6->sin6_addr.s6_addr, addressBytes, 16);
+ jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr);
+ env->GetByteArrayRegion(addressBytes, 0, 16, dst);
} else {
// Unknown address family.
throwSocketException(env, SOCKERR_BADAF);
- result = -1;
+ return -1;
}
- env->ReleaseByteArrayElements(addressByteArray, addressBytes, 0);
- return result;
+ return 0;
}
/**
*/
static jbyteArray org_apache_harmony_xnet_provider_jsse_OpenSSLSessionImpl_getid(JNIEnv* env, jobject object)
{
- SSL_SESSION * ssl_session;
- jbyteArray bytes;
- jbyte *tmp;
-
- ssl_session = getSslSessionPointer(env, object);
+ SSL_SESSION* ssl_session = getSslSessionPointer(env, object);
- bytes = env->NewByteArray(ssl_session->session_id_length);
- if (bytes != NULL) {
- tmp = env->GetByteArrayElements(bytes, NULL);
- memcpy(tmp, ssl_session->session_id, ssl_session->session_id_length);
- env->ReleaseByteArrayElements(bytes, tmp, 0);
+ jbyteArray result = env->NewByteArray(ssl_session->session_id_length);
+ if (result != NULL) {
+ jbyte* src = reinterpret_cast<jbyte*>(ssl_session->session_id);
+ env->SetByteArrayRegion(result, 0, ssl_session->session_id_length, src);
}
- return bytes;
+ return result;
}
/**
joa = NULL;
break;
} else {
- jbyte *tmp = env->GetByteArrayElements(bytes, NULL);
- memcpy(tmp, bptr->data, bptr->length);
- env->ReleaseByteArrayElements(bytes, tmp, 0);
+ jbyte* src = reinterpret_cast<jbyte*>(bptr->data);
+ env->SetByteArrayRegion(bytes, 0, bptr->length, src);
env->SetObjectArrayElement(joa, i, bytes);
}
}