From 7ec40f47534cde573348b6c1f8168413eea6f4b1 Mon Sep 17 00:00:00 2001 From: warrenl Date: Tue, 18 May 1999 18:02:01 +0000 Subject: [PATCH] * Makefile.am (ordinary_java_source_files): Added DatagramPacket.java, DatagramSocket.java, DatagramSocketImpl.java, MulticastSocket.java, PlainDatagramSocketImpl.java, and SocketOptions.java. (nat_source_files): Added natPlainDatagramSocketImpl.cc. * Makefile.in: Rebuilt. * java/net/DatagramPacket.java: New file. * java/net/DatagramSocket.java: New file. * java/net/DatagramSocketImpl.java: New file. * java/net/MulticastSocket.java: New file. * java/net/PlainDatagramSocketImpl.java: New file. * java/net/SocketOptions.java: New file. * java/net/natPlainDatagramSocketImpl.cc: New file. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@26997 138bc75d-0d04-0410-961f-82ee72b054a4 --- libjava/ChangeLog | 16 ++ libjava/Makefile.am | 7 + libjava/Makefile.in | 21 ++- libjava/java/net/DatagramPacket.java | 175 +++++++++++++++++++ libjava/java/net/DatagramSocket.java | 160 +++++++++++++++++ libjava/java/net/DatagramSocketImpl.java | 60 +++++++ libjava/java/net/MulticastSocket.java | 91 ++++++++++ libjava/java/net/PlainDatagramSocketImpl.java | 56 ++++++ libjava/java/net/SocketOptions.java | 40 +++++ libjava/java/net/natPlainDatagramSocketImpl.cc | 227 +++++++++++++++++++++++++ 10 files changed, 848 insertions(+), 5 deletions(-) create mode 100644 libjava/java/net/DatagramPacket.java create mode 100644 libjava/java/net/DatagramSocket.java create mode 100644 libjava/java/net/DatagramSocketImpl.java create mode 100644 libjava/java/net/MulticastSocket.java create mode 100644 libjava/java/net/PlainDatagramSocketImpl.java create mode 100644 libjava/java/net/SocketOptions.java create mode 100644 libjava/java/net/natPlainDatagramSocketImpl.cc diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 1e5b4224ba5..8a24d83c8e3 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,19 @@ +1999-05-18 Warren Levy + + * Makefile.am (ordinary_java_source_files): Added DatagramPacket.java, + DatagramSocket.java, DatagramSocketImpl.java, MulticastSocket.java, + PlainDatagramSocketImpl.java, and SocketOptions.java. + (nat_source_files): Added natPlainDatagramSocketImpl.cc. + * Makefile.in: Rebuilt. + + * java/net/DatagramPacket.java: New file. + * java/net/DatagramSocket.java: New file. + * java/net/DatagramSocketImpl.java: New file. + * java/net/MulticastSocket.java: New file. + * java/net/PlainDatagramSocketImpl.java: New file. + * java/net/SocketOptions.java: New file. + * java/net/natPlainDatagramSocketImpl.cc: New file. + 1999-05-18 Tom Tromey * java/util/zip/ZipOutputStream.java (level): Initial value is diff --git a/libjava/Makefile.am b/libjava/Makefile.am index 48bf5742e19..88260d2f44c 100644 --- a/libjava/Makefile.am +++ b/libjava/Makefile.am @@ -584,11 +584,16 @@ java/net/BindException.java \ java/net/ConnectException.java \ java/net/ContentHandler.java \ java/net/ContentHandlerFactory.java \ +java/net/DatagramPacket.java \ +java/net/DatagramSocket.java \ +java/net/DatagramSocketImpl.java \ java/net/FileNameMap.java \ java/net/HttpURLConnection.java \ java/net/InetAddress.java \ java/net/MalformedURLException.java \ +java/net/MulticastSocket.java \ java/net/NoRouteToHostException.java \ +java/net/PlainDatagramSocketImpl.java \ java/net/PlainSocketImpl.java \ java/net/ProtocolException.java \ java/net/ServerSocket.java \ @@ -596,6 +601,7 @@ java/net/Socket.java \ java/net/SocketException.java \ java/net/SocketImpl.java \ java/net/SocketImplFactory.java \ +java/net/SocketOptions.java \ java/net/URL.java \ java/net/URLConnection.java \ java/net/URLDecoder.java \ @@ -718,6 +724,7 @@ java/lang/reflect/natArray.cc \ java/lang/reflect/natField.cc \ java/lang/reflect/natMethod.cc \ java/net/natInetAddress.cc \ +java/net/natPlainDatagramSocketImpl.cc \ java/net/natPlainSocketImpl.cc \ java/text/natCollator.cc \ java/util/natDate.cc \ diff --git a/libjava/Makefile.in b/libjava/Makefile.in index 8550997742d..e22c69b0105 100644 --- a/libjava/Makefile.in +++ b/libjava/Makefile.in @@ -435,11 +435,16 @@ java/net/BindException.java \ java/net/ConnectException.java \ java/net/ContentHandler.java \ java/net/ContentHandlerFactory.java \ +java/net/DatagramPacket.java \ +java/net/DatagramSocket.java \ +java/net/DatagramSocketImpl.java \ java/net/FileNameMap.java \ java/net/HttpURLConnection.java \ java/net/InetAddress.java \ java/net/MalformedURLException.java \ +java/net/MulticastSocket.java \ java/net/NoRouteToHostException.java \ +java/net/PlainDatagramSocketImpl.java \ java/net/PlainSocketImpl.java \ java/net/ProtocolException.java \ java/net/ServerSocket.java \ @@ -447,6 +452,7 @@ java/net/Socket.java \ java/net/SocketException.java \ java/net/SocketImpl.java \ java/net/SocketImplFactory.java \ +java/net/SocketOptions.java \ java/net/URL.java \ java/net/URLConnection.java \ java/net/URLDecoder.java \ @@ -569,6 +575,7 @@ java/lang/reflect/natArray.cc \ java/lang/reflect/natField.cc \ java/lang/reflect/natMethod.cc \ java/net/natInetAddress.cc \ +java/net/natPlainDatagramSocketImpl.cc \ java/net/natPlainSocketImpl.cc \ java/text/natCollator.cc \ java/util/natDate.cc \ @@ -800,15 +807,19 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \ .deps/java/lang/w_remainder.P .deps/java/lang/w_sqrt.P \ .deps/java/net/BindException.P .deps/java/net/ConnectException.P \ .deps/java/net/ContentHandler.P .deps/java/net/ContentHandlerFactory.P \ -.deps/java/net/FileNameMap.P .deps/java/net/HttpURLConnection.P \ -.deps/java/net/InetAddress.P .deps/java/net/MalformedURLException.P \ +.deps/java/net/DatagramPacket.P .deps/java/net/DatagramSocket.P \ +.deps/java/net/DatagramSocketImpl.P .deps/java/net/FileNameMap.P \ +.deps/java/net/HttpURLConnection.P .deps/java/net/InetAddress.P \ +.deps/java/net/MalformedURLException.P .deps/java/net/MulticastSocket.P \ .deps/java/net/NoRouteToHostException.P \ +.deps/java/net/PlainDatagramSocketImpl.P \ .deps/java/net/PlainSocketImpl.P .deps/java/net/ProtocolException.P \ .deps/java/net/ServerSocket.P .deps/java/net/Socket.P \ .deps/java/net/SocketException.P .deps/java/net/SocketImpl.P \ -.deps/java/net/SocketImplFactory.P .deps/java/net/URL.P \ -.deps/java/net/URLConnection.P .deps/java/net/URLDecoder.P \ -.deps/java/net/URLEncoder.P .deps/java/net/URLStreamHandler.P \ +.deps/java/net/SocketImplFactory.P .deps/java/net/SocketOptions.P \ +.deps/java/net/URL.P .deps/java/net/URLConnection.P \ +.deps/java/net/URLDecoder.P .deps/java/net/URLEncoder.P \ +.deps/java/net/URLStreamHandler.P \ .deps/java/net/URLStreamHandlerFactory.P \ .deps/java/net/UnknownHostException.P \ .deps/java/net/UnknownServiceException.P \ diff --git a/libjava/java/net/DatagramPacket.java b/libjava/java/net/DatagramPacket.java new file mode 100644 index 00000000000..72b7bbdd4bb --- /dev/null +++ b/libjava/java/net/DatagramPacket.java @@ -0,0 +1,175 @@ +// DatagramPacket.java - Represents packets in a connectionless protocol. + +/* Copyright (C) 1999 Cygnus Solutions + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package java.net; + +/** + * @author Warren Levy + * @date April 28, 1999. + */ + +/** + * Written using on-line Java Platform 1.2 API Specification, as well + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). + * Status: Believed complete and correct. + */ + +public final class DatagramPacket +{ + private byte[] buffer; + private int offset; + private int length; + private InetAddress address; + private int port; + + // JDK1.2 + public DatagramPacket(byte[] buf, int offset, int length) + { + // FIXME: We can't currently rely on NullPointerException being + // thrown when we invoke a method on a null object. + if (buf == null) + throw new NullPointerException("Null buffer"); + if (offset < 0) + throw new IllegalArgumentException("Invalid offset: " + offset); + if (length < 0) + throw new IllegalArgumentException("Invalid length: " + length); + if (offset + length > buf.length) + throw new IllegalArgumentException("Potential buffer overflow - offset: " + + offset + " length: " + length); + + buffer = buf; + this.offset = offset; + this.length = length; + this.address = null; + this.port = -1; + } + + public DatagramPacket(byte[] buf, int length) + { + this(buf, 0, length); + } + + // JDK1.2 + public DatagramPacket(byte[] buf, int offset, int length, + InetAddress address, int port) + { + // FIXME: We can't currently rely on NullPointerException being + // thrown when we invoke a method on a null object. + if (buf == null) + throw new NullPointerException("Null buffer"); + if (offset < 0) + throw new IllegalArgumentException("Invalid offset: " + offset); + if (length < 0) + throw new IllegalArgumentException("Invalid length: " + length); + if (offset + length > buf.length) + throw new IllegalArgumentException("Potential buffer overflow - offset: " + + offset + " length: " + length); + if (port < 0 || port > 65535) + throw new IllegalArgumentException("Invalid port: " + port); + if (address == null) + throw new NullPointerException("Null address"); + + buffer = buf; + this.offset = offset; + this.length = length; + this.address = address; + this.port = port; + } + + public DatagramPacket(byte[] buf, int length, InetAddress address, int port) + { + this(buf, 0, length, address, port); + } + + public synchronized InetAddress getAddress() + { + return address; + } + + public synchronized int getPort() + { + return port; + } + + public synchronized byte[] getData() + { + return buffer; + } + + // JDK1.2 + public synchronized int getOffset() + { + return offset; + } + + public synchronized int getLength() + { + return length; + } + + public synchronized void setAddress(InetAddress iaddr) + { + if (iaddr == null) + throw new NullPointerException("Null address"); + + address = iaddr; + } + + public synchronized void setPort(int iport) + { + if (iport < 0 || iport > 65535) + throw new IllegalArgumentException("Invalid port: " + iport); + + port = iport; + } + + public synchronized void setData(byte[] buf) + { + // This form of setData requires setLength to be called separately + // and subsequently. + if (buf == null) + throw new NullPointerException("Null buffer"); + + buffer = buf; + } + + // JDK1.2 + public synchronized void setData(byte[] buf, int offset, int length) + { + // This form of setData must be used if offset is to be changed. + + // FIXME: We can't currently rely on NullPointerException being + // thrown when we invoke a method on a null object. + if (buf == null) + throw new NullPointerException("Null buffer"); + if (offset < 0) + throw new IllegalArgumentException("Invalid offset: " + offset); + if (length < 0) + throw new IllegalArgumentException("Invalid length: " + length); + if (offset + length > buf.length) + throw new IllegalArgumentException("Potential buffer overflow - offset: " + + offset + " length: " + length); + + buffer = buf; + this.offset = offset; + this.length = length; + } + + public synchronized void setLength(int length) + { + if (length < 0) + throw new IllegalArgumentException("Invalid length: " + length); + if (offset + length > buffer.length) + throw new IllegalArgumentException("Potential buffer overflow - offset: " + + offset + " length: " + length); + + this.length = length; + } +} diff --git a/libjava/java/net/DatagramSocket.java b/libjava/java/net/DatagramSocket.java new file mode 100644 index 00000000000..e82681a7f61 --- /dev/null +++ b/libjava/java/net/DatagramSocket.java @@ -0,0 +1,160 @@ +// DatagramSocket.java + +/* Copyright (C) 1999 Cygnus Solutions + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package java.net; +import java.io.IOException; + +/** + * @author Warren Levy + * @date May 3, 1999. + */ + +/** + * Written using on-line Java Platform 1.2 API Specification, as well + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). + * Status: Believed complete and correct. + */ + +public class DatagramSocket +{ + DatagramSocketImpl impl; + // FIXME: Shouldn't this be determined by getsockname() instead? + InetAddress laddr; + + public DatagramSocket() throws SocketException + { + this(0, null); + } + + public DatagramSocket(int port) throws SocketException + { + this(port, null); + } + + public DatagramSocket(int port, InetAddress laddr) throws SocketException + { + if (port < 0 || port > 65535) + throw new IllegalArgumentException("Invalid port: " + port); + + SecurityManager s = System.getSecurityManager(); + if (s != null) + s.checkListen(port); + + String propVal = System.getProperty("impl.prefix"); + if (propVal == null || propVal.equals("")) + propVal = "Plain"; + impl = (DatagramSocketImpl) Class.forName("java.net." + propVal + + "DatagramSocketImpl").newInstance(); + impl.create(); + // TBD: if this is right then the same should be done in Socket(). + try + { + impl.bind(port, laddr == null ? InetAddress.getLocalHost() : laddr); + } + catch (UnknownHostException e) + { + throw new BindException(e.getMessage()); + } + this.laddr = laddr; + } + + public void close() + { + impl.close(); + } + + public InetAddress getLocalAddress() + { + return laddr; + } + + public int getLocalPort() + { + return impl.getLocalPort(); + } + + public synchronized int getSoTimeout() throws SocketException + { + // FIXME: TODO - DatagramSocket.getSoTimeout + throw new SocketException("DatagramSocket.getSoTimeout - not yet implemented"); + } + + public synchronized void receive(DatagramPacket p) throws IOException + { + SecurityManager s = System.getSecurityManager(); + if (s != null) + s.checkAccept(p.getAddress().getHostAddress(), p.getPort()); + + impl.receive(p); + } + + public void send(DatagramPacket p) throws IOException + { + // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api. + SecurityManager s = System.getSecurityManager(); + if (s != null) + { + InetAddress addr = p.getAddress(); + if (addr.isMulticastAddress()) + s.checkMulticast(addr); + else + s.checkConnect(addr.getHostAddress(), p.getPort()); + } + + // FIXME: if this is a subclass of MulticastSocket, use getTTL for TTL val. + impl.send(p); + } + + public synchronized void setSoTimeout(int timeout) throws SocketException + { + // FIXME: TODO - DatagramSocket.setSoTimeout + throw new SocketException("DatagramSocket.setSoTimeout - not yet implemented"); + } + + // JDK1.2 + // public void connect(InetAddress address, int port) + // { + // } + + // JDK1.2 + // public void disconnect() + // { + // } + + // JDK1.2 + // public InetAddress getInetAddress() + // { + // } + + // JDK1.2 + // public int getPort() + // { + // } + + // JDK1.2 + // public int getReceiveBufferSize() throws SocketException + // { + // } + + // JDK1.2 + // public int getSendBufferSize() throws SocketException + // { + // } + + // JDK1.2 + // public void setReceiveBufferSize(int size) throws SocketException + // { + // } + + // JDK1.2 + // public void setSendBufferSize(int size) throws SocketException + // { + // } +} diff --git a/libjava/java/net/DatagramSocketImpl.java b/libjava/java/net/DatagramSocketImpl.java new file mode 100644 index 00000000000..2ab8c2a2fc4 --- /dev/null +++ b/libjava/java/net/DatagramSocketImpl.java @@ -0,0 +1,60 @@ +// DatagramSocketImpl.java - Abstract datagram socket implementation. + +/* Copyright (C) 1999 Cygnus Solutions + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package java.net; +import java.io.IOException; +import java.io.FileDescriptor; + +/** + * @author Warren Levy + * @date May 3, 1999. + */ + +/** + * Written using on-line Java Platform 1.2 API Specification, as well + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). + * Status: Believed complete and correct. + */ + +// JDK1.2: needs to implement SocketOptions. +// JDK1.2: public abstract class DatagramSocketImpl implements SocketOptions +public abstract class DatagramSocketImpl +{ + protected int localport; + protected FileDescriptor fd; + + public DatagramSocketImpl() + { + } + + protected abstract void bind(int lport, InetAddress laddr) + throws SocketException; + protected abstract void close(); + protected abstract void create() throws SocketException; + protected abstract int peek(InetAddress i) throws IOException; + protected abstract void send(DatagramPacket p) throws IOException; + protected abstract void receive(DatagramPacket p) throws IOException; + protected abstract void setTTL(byte ttl) throws IOException; + protected abstract byte getTTL() throws IOException; + protected abstract void setTimeToLive(int ttl) throws IOException; + protected abstract int getTimeToLive() throws IOException; + protected abstract void join(InetAddress inetaddr) throws IOException; + protected abstract void leave(InetAddress inetaddr) throws IOException; + + protected FileDescriptor getFileDescriptor() + { + return fd; + } + + protected int getLocalPort() + { + return localport; + } +} diff --git a/libjava/java/net/MulticastSocket.java b/libjava/java/net/MulticastSocket.java new file mode 100644 index 00000000000..1cb01c125ce --- /dev/null +++ b/libjava/java/net/MulticastSocket.java @@ -0,0 +1,91 @@ +// MulticastSocket.java + +/* Copyright (C) 1999 Cygnus Solutions + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package java.net; +import java.io.IOException; + +/** + * @author Warren Levy + * @date May 18, 1999. + */ + +/** + * Written using on-line Java Platform 1.2 API Specification, as well + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). + * Status: Believed complete and correct. + */ + +public class MulticastSocket extends DatagramSocket +{ + // FIXME: the local addr bound to the multicast socket can be reused; + // unlike unicast sockets. see p.1159 JCL book. + + public MulticastSocket() throws IOException + { + super(0, null); + } + + public MulticastSocket(int port) throws IOException + { + super(port, null); + } + + public InetAddress getInterface() throws SocketException + { + // FIXME: TODO - MulticastSocket.getInterface + throw new SocketException("MulticastSocket.getInterface - not yet implemented"); + } + + // Deprecated in JDK1.2 + public byte getTTL() throws IOException + { + return impl.getTTL(); + } + + // JDK1.2 + public int getTimeToLive() throws IOException + { + return impl.getTimeToLive(); + } + + public void setInterface(InetAddress inf) throws SocketException + { + // FIXME: TODO - MulticastSocket.setInterface + throw new SocketException("MulticastSocket.setInterface - not yet implemented"); + } + + // Deprecated in JDK1.2 + public void setTTL(byte ttl) throws IOException + { + impl.setTTL(ttl); + } + + // JDK1.2 + public void setTimeToLive(int ttl) throws IOException + { + impl.setTimeToLive(ttl); + } + + public void joinGroup(InetAddress mcastaddr) throws IOException + { + impl.join(mcastaddr); + } + + public void leaveGroup(InetAddress mcastaddr) throws IOException + { + impl.leave(mcastaddr); + } + + public void send(DatagramPacket p, byte ttl) throws IOException + { + // FIXME: use ttl instead of getTTL() for time to live. + impl.send(p); + } +} diff --git a/libjava/java/net/PlainDatagramSocketImpl.java b/libjava/java/net/PlainDatagramSocketImpl.java new file mode 100644 index 00000000000..7628c5087e3 --- /dev/null +++ b/libjava/java/net/PlainDatagramSocketImpl.java @@ -0,0 +1,56 @@ +// PlainDatagramSocketImpl.java - Implementation of DatagramSocketImpl. + +/* Copyright (C) 1999 Cygnus Solutions + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package java.net; +import java.io.IOException; + +/** + * @author Warren Levy + * @date May 3, 1999. + */ + +/** + * Written using on-line Java Platform 1.2 API Specification, as well + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). + * Status: Believed complete and correct. + */ + +class PlainDatagramSocketImpl extends DatagramSocketImpl +{ + int fnum = -1; + InetAddress address; // TBD: DatagramSocket.getLocalAddress()? + + // FIXME: Probably should have bind (and create?) calls from DatagramSocket + // constuctor. If so, then same change should be made to the corresponding + // Socket (non-datagram) classes. This allows the implementation more + // compleete control over how the socket is set up and used (e.g. connect, + // setting options, etc.). + public PlainDatagramSocketImpl() + { + } + + protected native void bind(int lport, InetAddress laddr) + throws SocketException; + protected native void create() throws SocketException; + protected native int peek(InetAddress i) throws IOException; + protected native void setTTL(byte ttl) throws IOException; + protected native byte getTTL() throws IOException; + protected native void setTimeToLive(int ttl) throws IOException; + protected native int getTimeToLive() throws IOException; + protected native void join(InetAddress inetaddr) throws IOException; + protected native void leave(InetAddress inetaddr) throws IOException; + protected native void send(DatagramPacket p) throws IOException; + protected native void receive(DatagramPacket p) throws IOException; + + protected void close() throws IOException + { + fd.close(); + } +} diff --git a/libjava/java/net/SocketOptions.java b/libjava/java/net/SocketOptions.java new file mode 100644 index 00000000000..40f7649aa60 --- /dev/null +++ b/libjava/java/net/SocketOptions.java @@ -0,0 +1,40 @@ +// SocketOptions.java - Interface for get/set socket options. + +/* Copyright (C) 1999 Cygnus Solutions + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package java.net; + +/** + * @author Warren Levy + * @date May 3, 1999. + */ + +/** + * Written using on-line Java Platform 1.2 API Specification. + * Status: Believed complete and correct. + */ + +public abstract interface SocketOptions +{ + public static final int TCP_NODELAY = 0x1; + public static final int SO_BINDADDR = 0xF; + public static final int SO_REUSEADDR = 0x4; + public static final int IP_MULTICAST_IF = 0x10; + public static final int SO_LINGER = 0x80; + public static final int SO_TIMEOUT = 0x1006; + +// JDK1.2 + public static final int SO_SNDBUF = 0x1001; + +// JDK1.2 + public static final int SO_RCVBUF = 0x1002; + + public void setOption(int optID, Object value) throws SocketException; + public Object getOption(int optID) throws SocketException; +} diff --git a/libjava/java/net/natPlainDatagramSocketImpl.cc b/libjava/java/net/natPlainDatagramSocketImpl.cc new file mode 100644 index 00000000000..1bdcdd59ae0 --- /dev/null +++ b/libjava/java/net/natPlainDatagramSocketImpl.cc @@ -0,0 +1,227 @@ +/* Copyright (C) 1999 Cygnus Solutions + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef HAVE_SOCKLEN_T +typedef int socklen_t; +#endif + +union SockAddr +{ + struct sockaddr_in address; +#ifdef HAVE_INET6 + struct sockaddr_in6 address6; +#endif +}; + +// FIXME: routines here and/or in natPlainSocketImpl.cc could throw +// NoRouteToHostException; also consider UnknownHostException, ConnectException. + +void +java::net::PlainDatagramSocketImpl::create () +{ + int sock = ::socket (AF_INET, SOCK_DGRAM, 0); + if (sock < 0) + { + char msg[100]; + char* strerr = strerror (errno); + sprintf (msg, "DatagramSocketImpl.create: %.*s", 80, strerr); + JvThrow (new java::net::SocketException (JvNewStringUTF (msg))); + } + fnum = sock; + fd = new java::io::FileDescriptor (sock); +} + +void +java::net::PlainDatagramSocketImpl::bind (jint lport, + java::net::InetAddress *host) +{ + union SockAddr u; + jbyteArray haddress = host->address; + jbyte *bytes = elements (haddress); + int len = haddress->length; + struct sockaddr *ptr = (struct sockaddr *) &u.address; + if (len == 4) + { + u.address.sin_family = AF_INET; + memcpy (&u.address.sin_addr, bytes, len); + len = sizeof (struct sockaddr_in); + u.address.sin_port = htons (lport); + } +#ifdef HAVE_INET6 + else if (len == 16) + { + u.address6.sin6_family = AF_INET6; + memcpy (&u.address6.sin6_addr, bytes, len); + len = sizeof (struct sockaddr_in6); + u.address6.sin6_port = htons (lport); + } +#endif + else + goto error; + if (::bind (fnum, ptr, len) == 0) + { + address = host; + localport = lport; + return; + } + error: + char msg[100]; + char* strerr = strerror (errno); + sprintf (msg, "DatagramSocketImpl.bind: %.*s", 80, strerr); + JvThrow (new java::net::BindException (JvNewStringUTF (msg))); +} + +jint +java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *i) +{ + // FIXME: TODO - PlainDatagramSocketImpl::peek + // throws IOException; + return 0; +} + +void +java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *p) +{ + // FIXME: Deal with Multicast and if the socket is connected. + jint rport = p->getPort(); + union SockAddr u; + jbyteArray haddress = p->getAddress()->address; + jbyte *bytes = elements (haddress); + int len = haddress->length; + struct sockaddr *ptr = (struct sockaddr *) &u.address; + jbyte *dbytes = elements (p->getData()); + if (len == 4) + { + u.address.sin_family = AF_INET; + memcpy (&u.address.sin_addr, bytes, len); + len = sizeof (struct sockaddr_in); + u.address.sin_port = htons (rport); + } +#ifdef HAVE_INET6 + else if (len == 16) + { + u.address6.sin6_family = AF_INET6; + memcpy (&u.address6.sin6_addr, bytes, len); + len = sizeof (struct sockaddr_in6); + u.address6.sin6_port = htons (rport); + } +#endif + else + goto error; + if (::sendto (fnum, (char *) dbytes, p->getLength(), 0, ptr, len) >= 0) + return; + error: + char msg[100]; + char* strerr = strerror (errno); + sprintf (msg, "DatagramSocketImpl.send: %.*s", 80, strerr); + JvThrow (new java::io::IOException (JvNewStringUTF (msg))); +} + +void +java::net::PlainDatagramSocketImpl::receive (java::net::DatagramPacket *p) +{ + // FIXME: Deal with Multicast and if the socket is connected. + union SockAddr u; + socklen_t addrlen = sizeof(u); + jbyte *dbytes = elements (p->getData()); + ssize_t retlen = + ::recvfrom (fnum, (char *) dbytes, p->getLength(), 0, (sockaddr*) &u, + &addrlen); + if (retlen < 0) + goto error; + // FIXME: Deal with Multicast addressing and if the socket is connected. + jbyteArray raddr; + jint rport; + if (u.address.sin_family == AF_INET) + { + raddr = JvNewByteArray (4); + memcpy (elements (raddr), &u.address.sin_addr, 4); + rport = ntohs (u.address.sin_port); + } +#ifdef HAVE_INET6 + else if (u.address.sin_family == AF_INET6) + { + raddr = JvNewByteArray (16); + memcpy (elements (raddr), &u.address6.sin6_addr, 16); + rport = ntohs (u.address6.sin6_port); + } +#endif + else + goto error; + // FIXME: Multicast: s->address = new InetAddress (raddr, NULL); + p->setPort (rport); + p->setLength ((jint) retlen); + return; + error: + char msg[100]; + char* strerr = strerror (errno); + sprintf (msg, "DatagramSocketImpl.receive: %.*s", 80, strerr); + JvThrow (new java::io::IOException (JvNewStringUTF (msg))); +} + +void +java::net::PlainDatagramSocketImpl::setTTL (jbyte ttl) +{ + // FIXME: TODO - :PlainDatagramSocketImpl::setTTL + // throws IOException; +} + +jbyte +java::net::PlainDatagramSocketImpl::getTTL () +{ + // FIXME: TODO - PlainDatagramSocketImpl::getTTL + // throws IOException; + return 0; +} + +void +java::net::PlainDatagramSocketImpl::setTimeToLive (jint ttl) +{ + // throws IOException; + // FIXME: TODO - PlainDatagramSocketImpl::setTimeToLive +} + +jint +java::net::PlainDatagramSocketImpl::getTimeToLive () +{ + // throws IOException; + // FIXME: TODO - PlainDatagramSocketImpl::getTimeToLive + return 0; +} + +void +java::net::PlainDatagramSocketImpl::join (java::net::InetAddress *inetaddr) +{ + // throws IOException; + // FIXME: TODO - PlainDatagramSocketImpl::join +} + +void +java::net::PlainDatagramSocketImpl::leave (java::net::InetAddress *inetaddr) +{ + // throws IOException; + // FIXME: TODO - PlainDatagramSocketImpl::leave +} -- 2.11.0