This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
{Patch] java.net network functions
- From: Michael Koch <konqueror at gmx dot de>
- To: java-patches at gcc dot gnu dot org
- Date: Thu, 21 Nov 2002 11:26:27 +0100
- Subject: {Patch] java.net network functions
Hello list,
I have commited the attached patch to include/ and java/net/ to move
needed network functions to a central place.
I decided to move them into the existing posix.h and win32.h headers.
These functions will now be used in java.nio native code too.
In the future I plan to add some more complex functions with
prototypes in these headers. Should the implementations of these
functions be in [posic|win32].cc or somewhere else ?
Michael
--
Homepage: http://www.worldforge.org/
GPG-key: http://konqueror.dyndns.org/~mkoch/michael.gpg
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.1550
retrieving revision 1.1551
diff -b -p -u -r1.1550 -r1.1551
--- ChangeLog 20 Nov 2002 21:19:26 -0000 1.1550
+++ ChangeLog 21 Nov 2002 10:08:02 -0000 1.1551
@@ -1,3 +1,40 @@
+2002-11-21 Michael Koch <konqueror@gmx.de>
+
+ * include/posix.h
+ (_Jv_socket): New method.
+ (_Jv_connect): New method.
+ (_Jv_close): New method.
+ (_Jv_platform_close_on_exec): Prefixed system function with "::".
+ (_Jv_bind): New method.
+ (_Jv_listen): New method.
+ (_Jv_write): New method.
+ (_Jv_read): New method.
+ * include/win32.h
+ (_Jv_socket): New method.
+ (_Jv_connect): New method.
+ (_Jv_close): New method.
+ (_Jv_bind): New method.
+ (_Jv_listen): New method.
+ (_Jv_write): New method.
+ (_Jv_read): New method.
+ * java/net/natNetworkInterface.cc:
+ Include platform.h, removed inclusion of socket.h
+ (getRealNetworkInterfaces): Replaced ::socket() by _Jv_socket() and
+ ::close() by _Jv_close().
+ * java/net/natPlainDatagramSocketImpl.cc:
+ Removed include of socket.h, definition of NATIVE_CLOSE and _Jv_bind,
+ added some new lines to make code more readable.
+ (create): Replaced ::socket() by _Jv_socket().
+ (close): Replaced NATIVE_CLOSE() by _Jv_close().
+ * java/net/natPlainSocketImpl.cc:
+ Removed definition of NATIVE_CLOSE, _Jv_bind, Jv_connect and _Jv_accept,
+ removed include of socket.h, removed some windows defines
+ (now in include/win32.h).
+ (create): Replaced ::socket() by _Jv_socket().
+ (close): Replaced NATIVE_CLOSE() by _Jv_close().
+ (write): Replaced ::read by _Jv_write().
+ (read): Replaced ::read by _Jv_read().
+
2002-11-20 Michael Koch <konqueror@gmx.de>
* Makefile.am (ordinary_java_source_files):
Index: include/posix.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/posix.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -b -p -u -r1.7 -r1.8
--- include/posix.h 7 Apr 2002 11:27:00 -0000 1.7
+++ include/posix.h 21 Nov 2002 10:08:03 -0000 1.8
@@ -27,6 +27,10 @@ details. */
#include <sys/select.h>
#endif
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@@ -41,11 +45,61 @@ extern jlong _Jv_platform_gettimeofday (
extern void _Jv_platform_initialize (void);
extern void _Jv_platform_initProperties (java::util::Properties*);
+static inline int
+_Jv_socket (int domain, int type, int protocol)
+{
+ return ::socket (domain, type, protocol);
+}
+
+inline int
+_Jv_connect (jint fd, sockaddr *ptr, int len)
+{
+ return ::connect (fd, ptr, len);
+}
+
+inline int
+_Jv_close (jint fd)
+{
+ return ::close (fd);
+}
+
inline void
_Jv_platform_close_on_exec (jint fd)
{
// Ignore errors.
- fcntl (fd, F_SETFD, FD_CLOEXEC);
+ ::fcntl (fd, F_SETFD, FD_CLOEXEC);
}
+// Avoid macro definitions of bind from system headers, e.g. on
+// Solaris 7 with _XOPEN_SOURCE. FIXME
+inline int
+_Jv_bind (int fd, struct sockaddr *addr, int addrlen)
+{
+ return ::bind (fd, addr, addrlen);
+}
+
+// Same problem with accept on Tru64 UNIX with _POSIX_PII_SOCKET
+inline int
+_Jv_accept (int fd, struct sockaddr *addr, socklen_t *addrlen)
+{
+ return ::accept (fd, addr, addrlen);
+}
+
+inline int
+_Jv_listen (int fd, int backlog)
+{
+ return ::listen (fd, backlog);
+}
+
+inline int
+_Jv_write(int s, void *buf, int len)
+{
+ return ::write (s, buf, len);
+}
+
+inline int
+_Jv_read(int s, void *buf, int len)
+{
+ return ::read (s, buf, len);
+}
#endif
Index: include/win32.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/win32.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -b -p -u -r1.11 -r1.12
--- include/win32.h 14 Sep 2002 21:56:43 -0000 1.11
+++ include/win32.h 21 Nov 2002 10:08:03 -0000 1.12
@@ -22,16 +22,71 @@ details. */
#include <io.h>
+// these errors cannot occur on Win32
+#define ENOTCONN 0
+#define ECONNRESET 0
+
+#ifndef ENOPROTOOPT
+#define ENOPROTOOPT 109
+#endif
+
extern void _Jv_platform_initialize (void);
extern void _Jv_platform_initProperties (java::util::Properties*);
extern jlong _Jv_platform_gettimeofday ();
+static inline int
+_Jv_socket (int domain, int type, int protocol)
+{
+ return ::socket (domain, type, protocol);
+}
+
+inline int
+_Jv_connect (jint fd, sockaddr *ptr, int len)
+{
+ return ::connect (fd, ptr, len);
+}
+
+inline int
+_Jv_close (jint fd)
+{
+ return ::closesocket (fd);
+}
+
inline void
_Jv_platform_close_on_exec (jint)
{
// Ignore.
}
+inline int
+_Jv_bind (int fd, struct sockaddr *addr, int addrlen)
+{
+ return ::bind (fd, addr, addrlen);
+}
+
+inline int
+_Jv_accept (int fd, struct sockaddr *addr, socklen_t *addrlen)
+{
+ return ::accept (fd, addr, addrlen);
+}
+
+inline int
+_Jv_listen (int fd, int backlog)
+{
+ return ::listen (fd, backlog);
+}
+
+inline int
+_Jv_write(int s, void *buf, int len)
+{
+ return ::send (s, (char*) buf, len, 0);
+}
+
+inline int
+_Jv_read(int s, void *buf, int len)
+{
+ return ::recv (s, (char*) buf, len, 0);
+}
#define HAVE_BACKTRACE
/* Store up to SIZE return address of the current program state in
Index: java/net/natNetworkInterface.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/natNetworkInterface.cc,v
retrieving revision 1.5
retrieving revision 1.6
diff -b -p -u -r1.5 -r1.6
--- java/net/natNetworkInterface.cc 10 Oct 2002 05:19:22 -0000 1.5
+++ java/net/natNetworkInterface.cc 21 Nov 2002 10:08:03 -0000 1.6
@@ -9,6 +9,7 @@ Libgcj License. Please consult the file
details. */
#include <config.h>
+#include <platform.h>
#ifdef WIN32
@@ -27,9 +28,6 @@ details. */
#include <sys/param.h>
#include <sys/types.h>
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
@@ -83,7 +81,7 @@ java::net::NetworkInterface::getRealNetw
if_data.ifc_buf = NULL;
// Open a (random) socket to have a file descriptor for the ioctl calls.
- fd = ::socket (PF_INET, SOCK_DGRAM, htons (IPPROTO_IP));
+ fd = _Jv_socket (PF_INET, SOCK_DGRAM, htons (IPPROTO_IP));
if (fd < 0)
throw new ::java::net::SocketException;
@@ -106,6 +104,7 @@ java::net::NetworkInterface::getRealNetw
// Get addresses of all interfaces.
if_record = if_data.ifc_req;
+
for (int n = 0; n < if_data.ifc_len; n += sizeof (struct ifreq))
{
struct ifreq ifr;
@@ -136,7 +135,7 @@ java::net::NetworkInterface::getRealNetw
_Jv_Free (if_data.ifc_buf);
if (fd >= 0)
- ::close (fd);
+ _Jv_close (fd);
return ht;
#endif /* WIN32 */
Index: java/net/natPlainDatagramSocketImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/natPlainDatagramSocketImpl.cc,v
retrieving revision 1.50
diff -b -u -r1.50 natPlainDatagramSocketImpl.cc
--- java/net/natPlainDatagramSocketImpl.cc 1 Nov 2002 06:35:14 -0000 1.50
+++ java/net/natPlainDatagramSocketImpl.cc 21 Nov 2002 10:22:35 -0000
@@ -7,23 +7,15 @@
details. */
#include <config.h>
-
#include <platform.h>
#ifdef WIN32
+
#include <errno.h>
#include <string.h>
-#ifndef ENOPROTOOPT
-#define ENOPROTOOPT 109
-#endif
-
-#define NATIVE_CLOSE(s) closesocket (s)
#else /* WIN32 */
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
@@ -33,8 +25,6 @@
#include <errno.h>
#include <string.h>
-#define NATIVE_CLOSE(s) ::close (s)
-
#endif /* WIN32 */
#if HAVE_BSTRING_H
@@ -42,20 +32,6 @@
#include <bstring.h>
#endif
-#ifndef DISABLE_JAVA_NET
-// Avoid macro definitions of bind from system headers, e.g. on
-// Solaris 7 with _XOPEN_SOURCE. FIXME
-static inline int
-_Jv_bind (int fd, struct sockaddr *addr, int addrlen)
-{
- return ::bind (fd, addr, addrlen);
-}
-#endif /* DISABLE_JAVA_NET */
-
-#ifdef bind
-#undef bind
-#endif
-
#include <gcj/cni.h>
#include <java/io/IOException.h>
#include <java/io/InterruptedIOException.h>
@@ -212,7 +188,8 @@
void
java::net::PlainDatagramSocketImpl::create ()
{
- int sock = ::socket (AF_INET, SOCK_DGRAM, 0);
+ int sock = _Jv_socket (AF_INET, SOCK_DGRAM, 0);
+
if (sock < 0)
{
char* strerr = strerror (errno);
@@ -240,10 +217,12 @@
if (len == 4)
{
u.address.sin_family = AF_INET;
+
if (host != NULL)
memcpy (&u.address.sin_addr, bytes, len);
else
u.address.sin_addr.s_addr = htonl (INADDR_ANY);
+
len = sizeof (struct sockaddr_in);
u.address.sin_port = htons (lport);
}
@@ -262,19 +241,23 @@
if (_Jv_bind (fnum, ptr, len) == 0)
{
socklen_t addrlen = sizeof(u);
+
if (lport != 0)
localPort = lport;
else if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0)
localPort = ntohs (u.address.sin_port);
else
goto error;
+
/* Allow broadcast by default. */
int broadcast = 1;
if (::setsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &broadcast,
sizeof (broadcast)) != 0)
goto error;
+
return;
}
+
error:
char* strerr = strerror (errno);
throw new java::net::BindException (JvNewStringUTF (strerr));
@@ -329,8 +312,10 @@
return rport;
error:
char* strerr = strerror (errno);
+
if (errno == ECONNREFUSED)
throw new PortUnreachableException (JvNewStringUTF (strerr));
+
throw new java::io::IOException (JvNewStringUTF (strerr));
}
@@ -391,10 +376,13 @@
p->setPort (rport);
p->setLength ((jint) retlen);
return rport;
+
error:
char* strerr = strerror (errno);
+
if (errno == ECONNREFUSED)
throw new PortUnreachableException (JvNewStringUTF (strerr));
+
throw new java::io::IOException (JvNewStringUTF (strerr));
}
@@ -407,7 +395,7 @@
// The method isn't declared to throw anything, so we disregard
// the return value.
- NATIVE_CLOSE (fnum);
+ _Jv_close (fnum);
fnum = -1;
timeout = 0;
}
@@ -446,8 +434,10 @@
return;
char* strerr = strerror (errno);
+
if (errno == ECONNREFUSED)
throw new PortUnreachableException (JvNewStringUTF (strerr));
+
throw new java::io::IOException (JvNewStringUTF (strerr));
}
@@ -508,10 +498,13 @@
p->setPort (rport);
p->setLength ((jint) retlen);
return;
+
error:
char* strerr = strerror (errno);
+
if (errno == ECONNREFUSED)
throw new PortUnreachableException (JvNewStringUTF (strerr));
+
throw new java::io::IOException (JvNewStringUTF (strerr));
}
@@ -521,6 +514,7 @@
// Assumes IPPROTO_IP rather than IPPROTO_IPV6 since socket created is IPv4.
char val = (char) ttl;
socklen_t val_len = sizeof(val);
+
if (::setsockopt (fnum, IPPROTO_IP, IP_MULTICAST_TTL, &val, val_len) == 0)
return;
@@ -534,6 +528,7 @@
// Assumes IPPROTO_IP rather than IPPROTO_IPV6 since socket created is IPv4.
char val;
socklen_t val_len = sizeof(val);
+
if (::getsockopt (fnum, IPPROTO_IP, IP_MULTICAST_TTL, &val, &val_len) == 0)
return ((int) val) & 0xFF;
Index: java/net/natPlainSocketImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/natPlainSocketImpl.cc,v
retrieving revision 1.50
diff -b -u -r1.50 natPlainSocketImpl.cc
--- java/net/natPlainSocketImpl.cc 18 Nov 2002 13:22:51 -0000 1.50
+++ java/net/natPlainSocketImpl.cc 21 Nov 2002 10:22:35 -0000
@@ -10,7 +10,9 @@
#include <platform.h>
#ifndef DISABLE_JAVA_NET
+
#ifdef WIN32
+
#include <windows.h>
#include <winsock.h>
#include <errno.h>
@@ -20,8 +22,6 @@
#undef MIN_PRIORITY
#undef FIONREAD
-#define NATIVE_CLOSE(s) closesocket (s)
-
// These functions make the Win32 socket API look more POSIXy
static inline int
write(int s, void *buf, int len)
@@ -36,11 +36,6 @@
}
// these errors cannot occur on Win32
-#define ENOTCONN 0
-#define ECONNRESET 0
-#ifndef ENOPROTOOPT
-#define ENOPROTOOPT 109
-#endif
#else /* WIN32 */
#ifdef HAVE_SYS_IOCTL_H
@@ -53,14 +48,11 @@
#include <sys/filio.h>
#endif
-#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <string.h>
-#define NATIVE_CLOSE(s) ::close (s)
-
#endif /* WIN32 */
#endif /* DISABLE_JAVA_NET */
@@ -73,43 +65,6 @@
typedef int socklen_t;
#endif
-#ifndef DISABLE_JAVA_NET
-
-// Avoid macro definitions of bind, connect from system headers, e.g. on
-// Solaris 7 with _XOPEN_SOURCE. FIXME
-static inline int
-_Jv_bind (int fd, struct sockaddr *addr, int addrlen)
-{
- return ::bind (fd, addr, addrlen);
-}
-
-#ifdef bind
-#undef bind
-#endif
-
-static inline int
-_Jv_connect (int fd, struct sockaddr *addr, int addrlen)
-{
- return ::connect (fd, addr, addrlen);
-}
-
-#ifdef connect
-#undef connect
-#endif
-
-// Same problem with accept on Tru64 UNIX with _POSIX_PII_SOCKET
-static inline int
-_Jv_accept (int fd, struct sockaddr *addr, socklen_t *addrlen)
-{
- return ::accept (fd, addr, addrlen);
-}
-
-#ifdef accept
-#undef accept
-#endif
-
-#endif /* DISABLE_JAVA_NET */
-
#include <gcj/cni.h>
#include <gcj/javaprims.h>
#include <java/io/IOException.h>
@@ -258,7 +213,7 @@
void
java::net::PlainSocketImpl::create (jboolean stream)
{
- int sock = ::socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
+ int sock = _Jv_socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
if (sock < 0)
{
@@ -495,7 +450,7 @@
JvSynchronize sync (this);
// should we use shutdown here? how would that effect so_linger?
- int res = NATIVE_CLOSE (fnum);
+ int res = _Jv_close (fnum);
if (res == -1)
{
@@ -518,7 +473,7 @@
while (r != 1)
{
- r = ::write (fnum, &d, 1);
+ r = _Jv_write (fnum, &d, 1);
if (r == -1)
{
if (java::lang::Thread::interrupted())
@@ -551,7 +506,7 @@
while (len > 0)
{
- int r = ::write (fnum, bytes, len);
+ int r = _Jv_write (fnum, bytes, len);
if (r == -1)
{
@@ -614,7 +569,7 @@
}
#endif /* WIN32 */
- int r = ::read (fnum, &b, 1);
+ int r = _Jv_read (fnum, &b, 1);
if (r == 0)
return -1;