This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: fix datagram socket multicast loops
- From: Anthony Green <green at redhat dot com>
- To: java-patches at gcc dot gnu dot org
- Date: Mon, 06 Jun 2005 22:26:18 -0700
- Subject: Patch: fix datagram socket multicast loops
Found with Azureus...
This patch fixes a bad downcasting problem in the _Jv_IP_MULTICAST_LOOP
case of setOption. It also factors out some common code from three
places into a static helper function.
Ok for HEAD and the 4.0 branch?
AG
2005-06-06 Anthony Green <green@localhost.localdomain>
* gnu/java/net/natPlainDatagramSocketImplPosix.cc (getLocalAddress):
New helper function.
(setOption): Use getLocalAddress. Don't downcast value to
InetAddress.
(getOption): Use getLocalAddress.
Index: gnu/java/net/natPlainDatagramSocketImplPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/natPlainDatagramSocketImplPosix.cc,v
retrieving revision 1.8
diff -u -p -r1.8 natPlainDatagramSocketImplPosix.cc
--- gnu/java/net/natPlainDatagramSocketImplPosix.cc 1 Feb 2005 19:22:47 -0000 1.8
+++ gnu/java/net/natPlainDatagramSocketImplPosix.cc 7 Jun 2005 05:21:09 -0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2003 Free Software Foundation
+/* Copyright (C) 2003, 2005 Free Software Foundation
This file is part of libgcj.
@@ -482,6 +482,38 @@ gnu::java::net::PlainDatagramSocketImpl:
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)
@@ -602,8 +634,10 @@ gnu::java::net::PlainDatagramSocketImpl:
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;
@@ -647,8 +681,6 @@ gnu::java::net::PlainDatagramSocketImpl:
{
int val;
socklen_t val_len = sizeof(val);
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
int level, opname;
switch (optID)
@@ -694,27 +726,7 @@ gnu::java::net::PlainDatagramSocketImpl:
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_ :
@@ -759,28 +771,7 @@ gnu::java::net::PlainDatagramSocketImpl:
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;