From: green Date: Mon, 19 Dec 2005 02:54:19 +0000 (+0000) Subject: 2005-12-18 Anthony Green X-Git-Tag: upstream/4.9.2~56899 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0dd4b6f29477b6d18bc8a479e6a05aa1cbbaf02a;p=platform%2Fupstream%2Flinaro-gcc.git 2005-12-18 Anthony Green * gnu/java/net/natPlainDatagramSocketImplPosix.cc (getLocalAddress): New helper function. (setOption): Use getLocalAddress. Don't downcast value to InetAddress. (getOption): Use getLocalAddress. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@108779 138bc75d-0d04-0410-961f-82ee72b054a4 --- 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 + + * 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 * 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;