diff options
-rw-r--r-- | libjava/ChangeLog | 8 | ||||
-rw-r--r-- | libjava/gnu/java/net/natPlainDatagramSocketImplPosix.cc | 88 |
2 files changed, 47 insertions, 49 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 18fe970..e3d2b03 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,11 @@ +2005-12-18 Anthony Green <green@redhat.com> + + * gnu/java/net/natPlainDatagramSocketImplPosix.cc (getLocalAddress): + New helper function. + (setOption): Use getLocalAddress. Don't downcast value to + InetAddress. + (getOption): Use getLocalAddress. + 2005-12-16 Tom Tromey <tromey@redhat.com> * java/lang/Class.java (getPackage): Get package from diff --git a/libjava/gnu/java/net/natPlainDatagramSocketImplPosix.cc b/libjava/gnu/java/net/natPlainDatagramSocketImplPosix.cc index 708f542..ada5fd1 100644 --- a/libjava/gnu/java/net/natPlainDatagramSocketImplPosix.cc +++ b/libjava/gnu/java/net/natPlainDatagramSocketImplPosix.cc @@ -1,4 +1,4 @@ -/* Copyright (C) 2003 Free Software Foundation +/* Copyright (C) 2003, 2005 Free Software Foundation This file is part of libgcj. @@ -485,6 +485,38 @@ gnu::java::net::PlainDatagramSocketImpl::mcastGrp (::java::net::InetAddress *ine throw new ::java::io::IOException (JvNewStringUTF (strerr)); } +// Helper function to get the InetAddress for a given socket (file +// descriptor). +static ::java::net::InetAddress * +getLocalAddress (int native_fd) +{ + jbyteArray laddr; + union SockAddr u; + socklen_t addrlen = sizeof(u); + + if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) != 0) + { + char* strerr = strerror (errno); + throw new ::java::net::SocketException (JvNewStringUTF (strerr)); + } + if (u.address.sin_family == AF_INET) + { + laddr = JvNewByteArray (4); + memcpy (elements (laddr), &u.address.sin_addr, 4); + } +#ifdef HAVE_INET6 + else if (u.address.sin_family == AF_INET6) + { + laddr = JvNewByteArray (16); + memcpy (elements (laddr), &u.address6.sin6_addr, 16); + } +#endif + else + throw new ::java::net::SocketException (JvNewStringUTF ("invalid family")); + + return new ::java::net::InetAddress (laddr, NULL); +} + void gnu::java::net::PlainDatagramSocketImpl::setOption (jint optID, ::java::lang::Object *value) @@ -605,8 +637,10 @@ gnu::java::net::PlainDatagramSocketImpl::setOption (jint optID, return; case _Jv_IP_MULTICAST_LOOP_ : - haddress = ((::java::net::InetAddress *) value)->addr; - len = haddress->length; + // cache the local address + if (localAddress == NULL) + localAddress = getLocalAddress (native_fd); + len = localAddress->addr->length; if (len == 4) { level = IPPROTO_IP; @@ -650,8 +684,6 @@ gnu::java::net::PlainDatagramSocketImpl::getOption (jint optID) { int val; socklen_t val_len = sizeof(val); - union SockAddr u; - socklen_t addrlen = sizeof(u); int level, opname; switch (optID) @@ -697,27 +729,7 @@ gnu::java::net::PlainDatagramSocketImpl::getOption (jint optID) case _Jv_SO_BINDADDR_: // cache the local address if (localAddress == NULL) - { - jbyteArray laddr; - if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) != 0) - goto error; - if (u.address.sin_family == AF_INET) - { - laddr = JvNewByteArray (4); - memcpy (elements (laddr), &u.address.sin_addr, 4); - } -#ifdef HAVE_INET6 - else if (u.address.sin_family == AF_INET6) - { - laddr = JvNewByteArray (16); - memcpy (elements (laddr), &u.address6.sin6_addr, 16); - } -#endif - else - throw new ::java::net::SocketException ( - JvNewStringUTF ("invalid family")); - localAddress = new ::java::net::InetAddress (laddr, NULL); - } + localAddress = getLocalAddress (native_fd); return localAddress; break; case _Jv_SO_REUSEADDR_ : @@ -761,29 +773,7 @@ gnu::java::net::PlainDatagramSocketImpl::getOption (jint optID) case _Jv_IP_MULTICAST_LOOP_ : // cache the local address - if (localAddress == NULL) - { - jbyteArray laddr; - if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) != 0) - goto error; - if (u.address.sin_family == AF_INET) - { - laddr = JvNewByteArray (4); - memcpy (elements (laddr), &u.address.sin_addr, 4); - } -#ifdef HAVE_INET6 - else if (u.address.sin_family == AF_INET6) - { - laddr = JvNewByteArray (16); - memcpy (elements (laddr), &u.address6.sin6_addr, 16); - } -#endif - else - throw new ::java::net::SocketException ( - JvNewStringUTF ("invalid family")); - localAddress = new ::java::net::InetAddress (laddr, NULL); - - } + localAddress = getLocalAddress (native_fd); if (localAddress->addr->length == 4) { level = IPPROTO_IP; |