Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 21 Jun 2005 11:33:09 -0700
From:      Landon Fuller <landonf@threerings.net>
To:        freebsd-java@freebsd.org
Subject:   [PATCH] jdk1.4.2, getsockname(), and ECONNRESET
Message-ID:  <ac03de2a0609c35568cde420cf34043b@threerings.net>

next in thread | raw e-mail | index | archive | help

--Apple-Mail-4--662330866
Content-Type: multipart/mixed; boundary=Apple-Mail-3--662330884


--Apple-Mail-3--662330884
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=ISO-8859-1;
	format=flowed

In j2se/src/solaris/native/sun/nio/ch/Net.c, getsockname() is called to=20=

determine the local address and port of a socket.

Under both Linux and Solaris, getsockname() will only fail if invalid=20
arguments are provided. If getsockname() returns an error, Java throws=20=

a java.lang.Error.
On FreeBSD, getsockname() will also return an error with errno set to=20
ECONNRESET if the connection has been reset by the peer. Consequently,=20=

Java throws a java.lang.Error.

The only way to handle this without an API change is to check for=20
ECONNRESET; In the attached patch, if errno is set to=A0ECONNRESET we=20
fill in the sockaddr structure with a port of 0, an IP of INADDR_ANY,=20
and clear out the error.

Also, any comments on the thread-safe resolver patch?

Thanks,
-landonf

--Apple-Mail-3--662330884
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream; x-unix-mode=0644;
	name="getsockname-1.diff"
Content-Disposition: attachment;
	filename=getsockname-1.diff

Only in getsockname: cscope.out
diff -ru bsdjdk/j2se/src/solaris/native/sun/nio/ch/Net.c getsockname/j2se/src/solaris/native/sun/nio/ch/Net.c
--- bsdjdk/j2se/src/solaris/native/sun/nio/ch/Net.c	Fri May 13 18:04:33 2005
+++ getsockname/j2se/src/solaris/native/sun/nio/ch/Net.c	Sun May 29 01:32:32 2005
@@ -118,8 +118,30 @@
     SOCKADDR sa;
     int sa_len = SOCKADDR_LEN;
     if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
+#ifdef _BSD_SOURCE
+	/*
+	 * XXXBSD:
+	 * ECONNRESET is specific to the BSDs. We can not return an error,
+	 * as the calling Java code with raise a java.lang.Error given the expectation
+	 * that getsockname() will never fail. According to the Single UNIX Specification,
+	 * it shouldn't fail. As such, we just fill in generic values.
+	 */
+	if (errno == ECONNRESET) {
+	    struct sockaddr_in *sin;
+	    sin = (struct sockaddr_in *) &sa;
+	    bzero(sin, sizeof(*sin));
+	    sin->sin_len  = sizeof(struct sockaddr_in);
+	    sin->sin_family = AF_INET;
+	    sin->sin_port = htonl(0);
+	    sin->sin_addr.s_addr = INADDR_ANY;
+	} else {
+	    handleSocketError(env, errno);
+	    return -1;
+	}
+#else /* _BSD_SOURCE */
 	handleSocketError(env, errno);
 	return -1;
+#endif /* _BSD_SOURCE */
     }
     return NET_GetPortFromSockaddr((struct sockaddr *)&sa);
 }
@@ -131,8 +153,30 @@
     int sa_len = SOCKADDR_LEN;
     int port;
     if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
+#ifdef _BSD_SOURCE
+	/*
+	 * XXXBSD:
+	 * ECONNRESET is specific to the BSDs. We can not return an error,
+	 * as the calling Java code with raise a java.lang.Error with the expectation
+	 * that getsockname() will never fail. According to the Single UNIX Specification,
+	 * it shouldn't fail. As such, we just fill in generic values.
+	 */
+	if (errno == ECONNRESET) {
+	    struct sockaddr_in *sin;
+	    sin = (struct sockaddr_in *) &sa;
+	    bzero(sin, sizeof(*sin));
+	    sin->sin_len  = sizeof(struct sockaddr_in);
+	    sin->sin_family = AF_INET;
+	    sin->sin_port = htonl(0);
+	    sin->sin_addr.s_addr = INADDR_ANY;
+	} else {
+	    handleSocketError(env, errno);
+	    return NULL;
+	}
+#else /* _BSD_SOURCE */
 	handleSocketError(env, errno);
 	return NULL;
+#endif /* _BSD_SOURCE */
     }
     return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
 }

--Apple-Mail-3--662330884--

--Apple-Mail-4--662330866
content-type: application/pgp-signature; x-mac-type=70674453;
	name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (Darwin)

iD8DBQFCuF1nlplZCE/15mMRAlrDAJ9rqNEWNaX6iAmWW4AQEZUVz4lX1gCfVig7
+Kz1+xJm32a/fQVBsibMm2Y=
=tLoZ
-----END PGP SIGNATURE-----

--Apple-Mail-4--662330866--




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?ac03de2a0609c35568cde420cf34043b>