Imported GNU Classpath 0.20
[official-gcc.git] / libjava / classpath / vm / reference / gnu / java / net / VMPlainDatagramSocketImpl.java
blob958c2187412be5057783520cfa48eeaf966e77ef
1 /* PlainDatagramSocketImpl.java -- VM interface for DatagramSocket impl
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.net;
41 import gnu.classpath.Configuration;
43 import java.io.IOException;
44 import java.net.DatagramPacket;
45 import java.net.InetAddress;
46 import java.net.NetworkInterface;
47 import java.net.SocketAddress;
48 import java.net.SocketException;
50 /**
51 * The VM interface for {@link gnu.java.net.PlainDatagramSocketImpl}.
53 * @author Ingo Proetel (proetel@aicas.com)
54 * @author Roman Kennke (kennke@aicas.com)
56 public final class VMPlainDatagramSocketImpl
58 /**
59 * Option id for the IP_TTL (time to live) value.
61 static final int IP_TTL = 0x1E61; // 7777
64 // Static initializer to load native library
65 static
67 if (Configuration.INIT_LOAD_LIBRARY)
69 System.loadLibrary("javanet");
73 /**
74 * Binds this socket to a particular port and interface
76 * @param socket the socket object
77 * @param port the port to bind to
78 * @param addr the address to bind to
80 * @throws SocketException If an error occurs
81 */
82 static native void bind(PlainDatagramSocketImpl socket, int port,
83 InetAddress addr)
84 throws SocketException;
86 /**
87 * Creates a new datagram socket.
89 * @param socket the socket object
91 * @throws SocketException If an error occurs
93 static native void create(PlainDatagramSocketImpl socket)
94 throws SocketException;
96 /**
97 * Connects to the remote address and port specified as arguments.
99 * @param socket the socket object
100 * @param addr the remote address to connect to
101 * @param port the remote port to connect to
103 * @throws SocketException If an error occurs
105 static native void connect(PlainDatagramSocketImpl socket, InetAddress addr,
106 int port)
107 throws SocketException;
110 * Sends a packet of data to a remote host.
112 * @param socket the socket object
113 * @param packet the packet to send
115 * @throws IOException If an error occurs
117 static void send(PlainDatagramSocketImpl socket, DatagramPacket packet)
118 throws IOException
120 nativeSendTo(socket, packet.getAddress(), packet.getPort(),
121 packet.getData(), packet.getOffset(), packet.getLength());
126 * Sends a packet of data to a remote host.
128 * @param socket the socket object
129 * @param addr the address to send to
130 * @param port the port to send to
131 * @param buf the buffer to send
132 * @param offset the offset of the data in the buffer to send
133 * @param len the length of the data to send
135 * @throws IOException If an error occurs
137 private static native void nativeSendTo(PlainDatagramSocketImpl socket,
138 InetAddress addr, int port,
139 byte[] buf, int offset, int len)
140 throws IOException;
143 * Receives a UDP packet from the network
145 * @param socket the socket object
146 * @param packet the packet to fill in with the data received
148 * @throws IOException IOException If an error occurs
150 static void receive(PlainDatagramSocketImpl socket, DatagramPacket packet)
151 throws IOException
153 byte[] receiveFromAddress = new byte[4];
154 int[] receiveFromPort = new int[1];
155 int[] receivedLength = new int[1];
157 nativeReceive(socket, packet.getData(), packet.getOffset(),
158 packet.getLength(),
159 receiveFromAddress, receiveFromPort, receivedLength);
161 packet.setAddress(InetAddress.getByAddress(receiveFromAddress));
162 packet.setPort(receiveFromPort[0]);
163 packet.setLength(receivedLength[0]);
166 private static native void nativeReceive(PlainDatagramSocketImpl socket,
167 byte[] buf, int offset, int len,
168 byte[] receiveFromAddress,
169 int[] receiveFromPort,
170 int[] receivedLength)
171 throws IOException;
174 * Sets the value of an option on the socket
176 * @param socket the socket object
177 * @param optionId the identifier of the option to set
178 * @param value the value of the option to set
180 * @exception SocketException If an error occurs
182 static native void setOption(PlainDatagramSocketImpl socket, int optionId,
183 Object value)
184 throws SocketException;
187 * Retrieves the value of an option on the socket.
189 * @param socket the socket object
190 * @param optionId the identifier of the option to retrieve
192 * @return the value of the option
194 * @throws SocketException if an error occurs
196 static native Object getOption(PlainDatagramSocketImpl socket, int optionId)
197 throws SocketException;
200 * Closes the socket.
202 * @param socket the socket object
204 static native void close(PlainDatagramSocketImpl socket);
207 * Joins a multicast group
209 * @param addr The group to join
211 * @exception IOException If an error occurs
213 static native void join(PlainDatagramSocketImpl socket, InetAddress addr)
214 throws IOException;
217 * Leaves a multicast group
219 * @param addr The group to leave
221 * @exception IOException If an error occurs
223 static native void leave(PlainDatagramSocketImpl socket, InetAddress addr)
224 throws IOException;
227 * Joins a multicast group.
229 * @param socket the socket object
230 * @param address the socket address
231 * @param netIf the network interface
233 * @throws IOException if I/O errors occur
235 static void joinGroup(PlainDatagramSocketImpl socket, SocketAddress address,
236 NetworkInterface netIf)
237 throws IOException
239 throw new InternalError
240 ("PlainDatagramSocketImpl::joinGroup is not implemented");
244 * Leaves a multicast group.
246 * @param socket the socket object
247 * @param address the socket address
248 * @param netIf the network interface
250 * @throws IOException if I/O errors occur
252 static void leaveGroup(PlainDatagramSocketImpl socket, SocketAddress address,
253 NetworkInterface netIf)
254 throws IOException
256 throw new InternalError
257 ("PlainDatagramSocketImpl::leaveGroup is not implemented");