commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1100563 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ native/include/acr/ native/shared/ test/org/apache/commons/runtime/
Date Sat, 07 May 2011 16:11:02 GMT
Author: mturk
Date: Sat May  7 16:11:02 2011
New Revision: 1100563

URL: http://svn.apache.org/viewvc?rev=1100563&view=rev
Log:
Axe APR pointer addresses. They don't work with relocatable java byte[]

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h
    commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java?rev=1100563&r1=1100562&r2=1100563&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Inet4SocketAddress.java
Sat May  7 16:11:02 2011
@@ -40,6 +40,7 @@ public final class Inet4SocketAddress ex
     public Inet4SocketAddress(String hostname, int port)
         throws OutOfMemoryError, NetworkException
     {
+        super();
         if (hostname == null || hostname.length() == 0)
             throw new IllegalArgumentException("invalid hostname");
         byte[] sa = SocketAddressImpl.getnameinfo(AddressFamily.INET, hostname, null, port);

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java?rev=1100563&r1=1100562&r2=1100563&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/InetSocketAddress.java
Sat May  7 16:11:02 2011
@@ -44,12 +44,12 @@ public class InetSocketAddress extends S
     
     protected InetSocketAddress()
     {
-        // No instance
     }
 
     public InetSocketAddress(String hostname, int port)
         throws OutOfMemoryError, NetworkException
     {
+        super();
         byte[] sa = SocketAddressImpl.getnameinfo(AddressFamily.UNSPEC, hostname, null, port);
         SocketAddressImpl.sockaddr(this, sa);
     }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java?rev=1100563&r1=1100562&r2=1100563&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
Sat May  7 16:11:02 2011
@@ -70,6 +70,7 @@ public abstract class SocketAddress exte
      * @param servname a service name or a port number represented as string.
      * @param family indicates a protocl family accepted.
      */
+    /*
     protected SocketAddress(AddressFamily family, String hostname, String servname, int port)
         throws InvalidArgumentException,
                NetworkException
@@ -96,7 +97,7 @@ public abstract class SocketAddress exte
     {
         this(family, hostname, null, 0);
     }
-
+    */
     /**
      * Gets the hostname of this socket.
      * <p>
@@ -144,4 +145,9 @@ public abstract class SocketAddress exte
         return port0(super.sa);
     }
 
+    public final String getIpAddress()
+    {
+        return ipaddr0(super.sa);
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h?rev=1100563&r1=1100562&r2=1100563&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/netapi.h Sat May  7 16:11:02
2011
@@ -40,9 +40,9 @@ struct acr_sockaddr_t {
 #endif
     } sa;
     /** The hostname */
-    char       hostname[NI_MAXHOST];
+    char        hostname[NI_MAXHOST];
     /** Either a string of the port number or the service name for the port */
-    char       servname[NI_MAXSERV];
+    char        servname[NI_MAXSERV];
     /** The numeric port */
     int         port;
     /** The family */
@@ -59,19 +59,12 @@ struct acr_sockaddr_t {
      * this points to a number of the address.
      */
     int         addrcnt;
-    /** This points to the IP address structure within the appropriate
-     *  sockaddr structure.  */
-    void       *ipaddr;
 };
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-acr_sockaddr_t *AcrGetSockaddr(JNI_STDARGS);
-int     AcrSetSockaddr(JNI_STDARGS, acr_sockaddr_t *sa);
-
-
 #ifdef __cplusplus
 }
 #endif

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c?rev=1100563&r1=1100562&r2=1100563&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Sat May  7 16:11:02 2011
@@ -29,9 +29,9 @@
 #define V4MAPPED_EQUAL(a,b)                                             \
     ((a)->sa.sin.sin_family == AF_INET &&                               \
      (b)->sa.sin.sin_family == AF_INET6 &&                              \
-     IN6_IS_ADDR_V4MAPPED((struct in6_addr *)(b)->ipaddr) &&            \
-     memcmp((a)->ipaddr,                                                \
-            &((struct in6_addr *)(b)->ipaddr)->s6_addr[12],             \
+     IN6_IS_ADDR_V4MAPPED(&(b)->sa.sin6.sin6_addr) &&                   \
+     memcmp(&(a)->sa.sin.sin_addr,                                      \
+            &(b)->sa.sin6.sin6_addr.s6_addr[12],                        \
             (a)->iplen) == 0)
 
 #if !defined(WINDOWS)
@@ -358,13 +358,13 @@ inet_pton6(const char *src, unsigned cha
  *      Paul Vixie, 1996.
  */
 const char *
-AcrInetNtop(int af, const void *src, char *dst, int size)
+AcrInetNtop(acr_sockaddr_t *sa, char *dst, int size)
 {
-    switch (af) {
+    switch (sa->family) {
         case AF_INET:
-            return inet_ntop4(src, dst, size);
+            return inet_ntop4((const unsigned char *)&sa->sa.sin.sin_addr,   dst,
size);
         case AF_INET6:
-            return inet_ntop6(src, dst, size);
+            return inet_ntop6((const unsigned char *)&sa->sa.sin6.sin6_addr, dst,
size);
         default:
             ACR_SET_OS_ERROR(EAFNOSUPPORT);
             return 0;
@@ -373,13 +373,13 @@ AcrInetNtop(int af, const void *src, cha
 }
 
 int
-AcrInetPton(int af, const char *src, void *dst)
+AcrInetPton(const char *src, acr_sockaddr_t *sa)
 {
-    switch (af) {
+    switch (sa->family) {
         case AF_INET:
-            return inet_pton4(src, dst);
+            return inet_pton4(src, (unsigned char *)&sa->sa.sin.sin_addr);
         case AF_INET6:
-            return inet_pton6(src, dst);
+            return inet_pton6(src, (unsigned char *)&sa->sa.sin6.sin6_addr);
         default:
             ACR_SET_OS_ERROR(EAFNOSUPPORT);
             return -1;
@@ -390,10 +390,10 @@ AcrInetPton(int af, const char *src, voi
 static int
 sockaddr_to_ipstr(char *buf, int buflen, acr_sockaddr_t *sockaddr)
 {
-    if (AcrInetNtop(sockaddr->family, sockaddr->ipaddr, buf, buflen) == 0)
+    if (AcrInetNtop(sockaddr, buf, buflen) == 0)
         return ACR_GET_OS_ERROR();
     if (sockaddr->family == AF_INET6 &&
-        IN6_IS_ADDR_V4MAPPED((struct in6_addr *)sockaddr->ipaddr) &&
+        IN6_IS_ADDR_V4MAPPED(&sockaddr->sa.sin6.sin6_addr) &&
         buflen > 7) {
         /* Number 7 here is strlen("::ffff:") */
         /* This is an IPv4-mapped IPv6 address; drop the leading
@@ -450,20 +450,17 @@ sockaddr_vars_set(acr_sockaddr_t *addr, 
     if (family == AF_INET) {
         addr->salen   = ISIZEOF(struct sockaddr_in);
         addr->addrlen = 16;
-        addr->ipaddr  = &(addr->sa.sin.sin_addr);
         addr->iplen   = sizeof(struct in_addr);
     }
     else if (family == AF_INET6) {
         addr->salen   = ISIZEOF(struct sockaddr_in6);
         addr->addrlen = 46;
-        addr->ipaddr  = &(addr->sa.sin6.sin6_addr);
         addr->iplen   = sizeof(struct in6_addr);
     }
 #if HAVE_SYS_UN_H
     else if (family == AF_LOCAL) {
         addr->salen   = ISIZEOF(struct sockaddr_un);
         addr->addrlen = ISIZEOF(addr->sa.unx.sun_path);;
-        addr->ipaddr  = &(addr->sa.unx.sun_path);
         addr->iplen   = addr->addrlen;
     }
 #endif
@@ -616,7 +613,7 @@ get_hostname(const char **hostname, acr_
         struct sockaddr_in tmpsa;
         tmpsa.sin_family = AF_INET;
         tmpsa.sin_port   = 0;
-        tmpsa.sin_addr.s_addr = ((acr_u32_t *)sockaddr->ipaddr)[3];
+        tmpsa.sin_addr.s_addr = ((acr_u32_t *)&sockaddr->sa.sin6.sin6_addr)[3];
 #ifdef SIN6_LEN
         tmpsa.sin_len    = sizeof(tmpsa);
 #endif
@@ -678,7 +675,7 @@ get_servname(const char **servname, acr_
         struct sockaddr_in tmpsa;
         tmpsa.sin_family = AF_INET;
         tmpsa.sin_port   = 0;
-        tmpsa.sin_addr.s_addr = ((acr_u32_t *)sockaddr->ipaddr)[3];
+        tmpsa.sin_addr.s_addr = ((acr_u32_t *)&sockaddr->sa.sin6.sin6_addr)[3];
 #ifdef SIN6_LEN
         tmpsa.sin_len    = sizeof(tmpsa);
 #endif
@@ -754,10 +751,13 @@ ACR_NET_EXPORT(jint, EndpointAddress, fa
         switch (sa->family) {
             case AF_INET:
                 rv = 1;
+            break;
             case AF_INET6:
                 rv = 2;
+            break;
             case AF_LOCAL:
                 rv = 3;
+            break;
             default:
                 rv = 0;
             break;
@@ -786,7 +786,6 @@ ACR_NET_EXPORT(jstring, EndpointAddress,
     SOCKADDR_RELEASE(a, sa);
     return AcrNewJavaStringA(env, name);
 }
-
 ACR_NET_EXPORT(jboolean, EndpointAddress, equals0)(JNI_STDARGS,
                                                    jbyteArray sa1,
                                                    jbyteArray sa2)
@@ -796,9 +795,14 @@ ACR_NET_EXPORT(jboolean, EndpointAddress
     acr_sockaddr_t *addr2 = SOCKADDR_CAST(sa2);
 
     if (addr1 != 0 && addr2 != 0) {
-        if (addr1->iplen == addr2->iplen &&
-            memcmp(addr1->ipaddr, addr2->ipaddr, addr1->iplen) == 0)
-            rv = JNI_TRUE;
+        if (addr1->iplen == addr2->iplen) {
+            if (addr1->family == AF_INET && addr2->family == AF_INET &&
+                memcmp(&addr1->sa.sin.sin_addr, &addr2->sa.sin.sin_addr, addr1->iplen)
== 0)
+                rv = JNI_TRUE;
+            else if (addr1->family == AF_INET6 && addr2->family == AF_INET6
&&
+                memcmp(&addr1->sa.sin6.sin6_addr, &addr2->sa.sin6.sin6_addr,
addr1->iplen) == 0)
+                rv = JNI_TRUE;
+        }
         else if (V4MAPPED_EQUAL(addr1, addr2))
             rv = JNI_TRUE;
         else if (V4MAPPED_EQUAL(addr2, addr1))
@@ -836,7 +840,6 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
         sa.family  = AF_LOCAL;
         sa.salen   = ISIZEOF(struct sockaddr_un);
         sa.addrlen = ISIZEOF(sa.sa.unx.sun_path);
-        sa.ipaddr  = &(sa.sa.unx.sun_path);
         sa.iplen   = sa.addrlen;
         rc = 0;
 #elif defined(WINDOWS)
@@ -850,7 +853,6 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
         }
         sa.family  = AF_LOCAL;
         sa.addrlen = ISIZEOF(sa.hostname);
-        sa.ipaddr  = &(sa.hostname);
         sa.iplen   = sa.addrlen;
         rc = 0;
 #endif
@@ -878,7 +880,7 @@ ACR_NET_EXPORT(jstring, SocketAddress, h
     acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0) {
-        if (sa->hostname != '\0')
+        if (sa->hostname[0] != '\0')
             rv = AcrNewJavaStringA(env, sa->hostname);
         else {
             char buf[256];
@@ -919,7 +921,7 @@ ACR_NET_EXPORT(jstring, SocketAddress, s
     acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0) {
-        if (sa->servname != '\0')
+        if (sa->servname[0] != '\0')
             rv = AcrNewJavaStringA(env, sa->servname);
         else {
             rc = get_servname(&serv, sa, 0);

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java?rev=1100563&r1=1100562&r2=1100563&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
Sat May  7 16:11:02 2011
@@ -62,4 +62,35 @@ public class TestAddress extends Assert
         Service s2 = new Service(21);
         assertEquals(s2.getName(), "ftp");
     }
+
+    @Test(groups = { "core" })
+    public void simpleLocalhost()
+        throws IOException
+    {
+        InetSocketAddress a = new InetSocketAddress("localhost");
+        System.out.println("localhost: " + a.getIpAddress());
+        System.out.println("localhost: " + a.getCanonicalHostName());
+        System.out.println("localhost: " + a.getFamily());
+    }
+
+    @Test(groups = { "core" })
+    public void ipv4Localhost()
+        throws IOException
+    {
+        InetSocketAddress a = new InetSocketAddress("127.0.0.1");
+        System.out.println("localhost: " + a.getFamily());
+        assertEquals(a.getFamily(), AddressFamily.INET);
+    }
+
+    @Test(groups = { "core" })
+    public void getAllForLocalhost()
+        throws IOException
+    {
+        SocketAddress aa[] = InetSocketAddress.getAll(null, 0);
+        for (int i = 0; i < aa.length; i++) {
+            System.out.println("addr[" + i + "] " + aa[i].getIpAddress());
+            System.out.println("addr[" + i + "] " + aa[i].getFamily());
+        }
+    }
+
 }



Mime
View raw message