summaryrefslogtreecommitdiff
path: root/native/jni/native-lib/cpnet.c
diff options
context:
space:
mode:
Diffstat (limited to 'native/jni/native-lib/cpnet.c')
-rw-r--r--native/jni/native-lib/cpnet.c56
1 files changed, 53 insertions, 3 deletions
diff --git a/native/jni/native-lib/cpnet.c b/native/jni/native-lib/cpnet.c
index c112ac2a7..85c4640e1 100644
--- a/native/jni/native-lib/cpnet.c
+++ b/native/jni/native-lib/cpnet.c
@@ -49,6 +49,7 @@ exception statement from your version. */
#include <sys/ioctl.h>
#include <sys/time.h>
#include <unistd.h>
+#include <arpa/inet.h>
#include "cpnet.h"
@@ -597,8 +598,8 @@ jint cpnet_getHostByName (JNIEnv *env, const char *hostname, cpnet_address ***ad
struct hostent hret;
struct hostent *result;
jint buflen = 1024;
- int herr;
- int ret;
+ int herr = 0;
+ int ret = 0;
int counter = 0;
cpnet_address **addr_arr;
int i;
@@ -610,7 +611,13 @@ jint cpnet_getHostByName (JNIEnv *env, const char *hostname, cpnet_address ***ad
#ifdef HAVE_GETHOSTBYNAME_R
ret = gethostbyname_r (hostname, &hret, buf, buflen, &result, &herr);
#else
- ret = gethostbyname (hostname);
+ hret.h_addr_list = NULL;
+ hret.h_addrtype = 0;
+
+ result = gethostbyname (hostname);
+ if (result == NULL)
+ return -errno;
+ memcpy (&hret, result, sizeof (struct hostent));
#endif
if (ret != 0 || result == NULL)
{
@@ -709,6 +716,49 @@ jint cpnet_getHostByAddr (JNIEnv *env UNUSED, cpnet_address *addr, char *hostnam
return 0;
}
+jint cpnet_aton (JNIEnv *env, const char *hostname, cpnet_address **addr)
+{
+ jbyte *bytes = NULL;
+#ifdef HAVE_INET_PTON
+ jbyte inet6_addr[16];
+#endif
+
+#ifdef HAVE_INET_ATON
+ struct in_addr laddr;
+ if (inet_aton (hostname, &laddr))
+ {
+ bytes = (jbyte *) &laddr;
+ }
+#elif defined(HAVE_INET_ADDR)
+#if ! HAVE_IN_ADDR_T
+ typedef jint in_addr_t;
+#endif
+ in_addr_t laddr = inet_addr (hostname);
+ if (laddr != (in_addr_t)(-1))
+ {
+ bytes = (jbyte *) &laddr;
+ }
+#endif
+ if (bytes)
+ {
+ *addr = cpnet_newIPV4Address(env);
+ cpnet_bytesToIPV4Address(*addr, bytes);
+ return 0;
+ }
+
+#ifdef HAVE_INET_PTON
+ if (inet_pton (AF_INET6, hostname, inet6_addr) > 0)
+ {
+ *addr = cpnet_newIPV6Address(env);
+ cpnet_bytesToIPV6Address(*addr, inet6_addr);
+ return 0;
+ }
+#endif
+
+ *addr = NULL;
+ return 0;
+}
+
void cpnet_freeAddresses(JNIEnv * env, cpnet_address **addr, jint addresses_count)
{
jint i;