commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1100469 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/SocketAddress.java native/shared/netaddr.c
Date Sat, 07 May 2011 07:07:55 GMT
Author: mturk
Date: Sat May  7 07:07:55 2011
New Revision: 1100469

URL: http://svn.apache.org/viewvc?rev=1100469&view=rev
Log:
Allow defining service name

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
    commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c

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=1100469&r1=1100468&r2=1100469&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 07:07:55 2011
@@ -55,7 +55,7 @@ public abstract class SocketAddress exte
     private static native int     port0(byte[] sa);
 
     private static native String  ipaddr0(byte[] sa);
-    private static native byte[]  sockaddr0(String hostname, int family, int port, int flags)
+    private static native byte[]  sockaddr0(String hostname, String servname, int family,
int port, int flags)
         throws OutOfMemoryError, NetworkException;
     private static native int     next0(byte[] sa);
 
@@ -75,27 +75,42 @@ public abstract class SocketAddress exte
         super(family);
     }
 
-    protected SocketAddress(String host, AddressFamily family, int port, int flags)
+    /**
+     * Create a new socket address object.
+     *
+     * @param hostname a host(node) name or a numeric host address string.
+     * @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,
int flags)
         throws InvalidArgumentException,
                NetworkException
     {
         super(family);
         if (family == AddressFamily.LOCAL)
             throw new InvalidArgumentException();
-        int masked = flags & (IPV4_ADDR_OK | IPV6_ADDR_OK);
-        if (host == null || family != AddressFamily.UNSPEC || masked == (IPV4_ADDR_OK | IPV6_ADDR_OK))
-            throw new InvalidArgumentException();
-        super.sa = sockaddr0(host, family.valueOf(), port, flags);
+        if (hostname == null) {
+            int masked = flags & (IPV4_ADDR_OK | IPV6_ADDR_OK);
+            if (family != AddressFamily.UNSPEC || masked == (IPV4_ADDR_OK | IPV6_ADDR_OK))
+                throw new InvalidArgumentException();
+            if (servname == null)
+                servname = Integer.toString(port);
+        }
+        super.sa = sockaddr0(hostname, servname, family.valueOf(), port, flags);
     }
 
-    protected SocketAddress(String host, AddressFamily family)
+    protected SocketAddress(AddressFamily family, String hostname, String servname)
         throws InvalidArgumentException,
                NetworkException
     {
-        super(family);
-        if (family == AddressFamily.LOCAL)
-            throw new InvalidArgumentException();
-        super.sa = sockaddr0(host, family.valueOf(), 0, 0);
+        this(family, hostname, servname, 0, 0);
+    }
+
+    protected SocketAddress(AddressFamily family, String hostname)
+        throws InvalidArgumentException,
+               NetworkException
+    {
+        this(family, hostname, null, 0, 0);
     }
 
     /**
@@ -104,7 +119,7 @@ public abstract class SocketAddress exte
      * The methods does not resolve the host name. If the address was
      * not resloved already the method returns IP notion of the address.
      * </p>
-     * @return the socket endpoint hostname.
+     * @return the socket endpoint host name.
      */
     public final String getHostName()
     {
@@ -116,7 +131,7 @@ public abstract class SocketAddress exte
      * <p>
      * The methods resolves the host name.
      * </p>
-     * @return the socket endpoint hostname.
+     * @return the socket endpoint host name.
      */
     public final String getCanonicalHostName()
     {

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=1100469&r1=1100468&r2=1100469&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 07:07:55 2011
@@ -471,6 +471,7 @@ sockaddr_vars_set(acr_sockaddr_t *addr, 
 
 static int
 call_resolver(acr_sockaddr_t **sa, const char *hostname,
+              const char *servname,
               int family, int port, int flags)
 {
     struct addrinfo hints, *ai, *ai_list;
@@ -478,8 +479,6 @@ call_resolver(acr_sockaddr_t **sa, const
     int error;
     int ai_len = ISIZEOF(acr_sockaddr_t);
     int ai_cnt = 0;
-    char *servname = 0;
-    char  buf[16];
 
     memset(&hints, 0, sizeof(hints));
     hints.ai_family   = family;
@@ -495,13 +494,10 @@ call_resolver(acr_sockaddr_t **sa, const
     if(hostname == 0) {
 #if defined(AI_PASSIVE)
         /* If hostname is NULL, assume we are trying to bind to all
-         * interfaces. */
+         * interfaces.
+         */
         hints.ai_flags |= AI_PASSIVE;
 #endif
-        /* getaddrinfo according to RFC 2553 must have either hostname
-         * or servname non-NULL.
-         */
-        servname = AcrItoa(buf, port);
     }
     error = getaddrinfo(hostname, servname, &hints, &ai_list);
 #if defined(AI_ADDRCONFIG)
@@ -569,7 +565,8 @@ call_resolver(acr_sockaddr_t **sa, const
         }
         if (hostname != 0)
             strlcpy(new_sa->hostname, hostname, NI_MAXHOST);
-
+        if (servname != 0)
+            strlcpy(new_sa->servname, servname, NI_MAXSERV);
         new_sa++;
         ai = ai->ai_next;
     }
@@ -579,23 +576,23 @@ call_resolver(acr_sockaddr_t **sa, const
 
 int
 AcrFindAddresses(acr_sockaddr_t **sa, const char *hostname,
-                 int family, int port, int flags)
+                 const char *servname, int family, int port, int flags)
 {
     if (flags & ACR_IPV4_ADDR_OK) {
-        int rc = call_resolver(sa, hostname, AF_INET, port, flags);
+        int rc = call_resolver(sa, hostname, servname, AF_INET, port, flags);
         if (rc != 0)
             family = AF_INET6; /* try again */
         else
             return 0;
     }
     else if (flags & ACR_IPV6_ADDR_OK) {
-        int rc = call_resolver(sa, hostname, AF_INET6, port, flags);
+        int rc = call_resolver(sa, hostname, servname, AF_INET6, port, flags);
         if (rc != 0)
             family = AF_INET;  /* try again */
         else
             return 0;
     }
-    return call_resolver(sa, hostname, family, port, flags);
+    return call_resolver(sa, hostname, servname, family, port, flags);
 }
 
 #define GETHOSTBYNAME_BUFLEN 512
@@ -962,6 +959,7 @@ ACR_NET_EXPORT(jint, SocketAddress, port
 }
 
 ACR_NET_EXPORT(jbyteArray, SocketAddress, sockaddr0)(JNI_STDARGS, jstring hostname,
+                                                     jstring servname,
                                                      jint family, jint port, jint flags)
 {
     acr_sockaddr_t *sa = 0;
@@ -980,7 +978,9 @@ ACR_NET_EXPORT(jbyteArray, SocketAddress
         break;
     }
     WITH_CSTR(hostname) {
-        rc = AcrFindAddresses(&sa, J2S(hostname), ffamily, port, flags);
+    WITH_CSTR(servname) {
+        rc = AcrFindAddresses(&sa, J2S(hostname), J2S(servname), ffamily, port, flags);
+    } DONE_WITH_STR(servname);
     } DONE_WITH_STR(hostname);
 
     if (rc != 0) {



Mime
View raw message