2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / net / PlainDatagramSocketImpl.java
bloba3146518783e5d67a3e456b1f13ac23c5ad31ca1
1 /* PlainDatagramSocketImpl.java -- Default DatagramSocket implementation
2 Copyright (C) 1998, 1999, 2001, 2003 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 java.io.IOException;
42 import java.net.DatagramPacket;
43 import java.net.DatagramSocketImpl;
44 import java.net.InetAddress;
45 import java.net.InetSocketAddress;
46 import java.net.NetworkInterface;
47 import java.net.SocketAddress;
48 import java.net.SocketException;
49 import java.net.SocketOptions;
50 import gnu.classpath.Configuration;
52 /**
53 * Written using on-line Java Platform 1.2 API Specification, as well
54 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
55 * Status: Believed complete and correct.
58 /**
59 * This is the default socket implementation for datagram sockets.
60 * It makes native calls to C routines that implement BSD style
61 * SOCK_DGRAM sockets in the AF_INET family.
63 * @author Aaron M. Renn <arenn@urbanophile.com>
64 * @author Warren Levy <warrenl@cygnus.com>
66 public final class PlainDatagramSocketImpl extends DatagramSocketImpl
68 // Static initializer to load native library
69 static
71 if (Configuration.INIT_LOAD_LIBRARY)
73 System.loadLibrary("javanet");
77 // These fields are mirrored for use in native code to avoid cpp conflicts
78 // when the #defines in system header files are the same as the public fields.
79 static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
80 _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
81 _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
82 _Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST,
83 _Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE,
84 _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
85 _Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2,
86 _Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP,
87 _Jv_IP_TOS_ = SocketOptions.IP_TOS,
88 _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
89 _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
90 _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
91 _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
92 _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
94 /**
95 * This is the actual underlying file descriptor
97 int native_fd = -1;
99 // FIXME: Is this necessary? Could it help w/ DatagramSocket.getLocalAddress?
100 // InetAddress address;
102 // localAddress cache
103 InetAddress localAddress;
105 // 'timeout' is set/read by setOption/getOption.
106 int timeout = 0;
109 * Default do nothing constructor
111 public PlainDatagramSocketImpl()
115 protected void finalize() throws Throwable
117 synchronized (this)
119 if (native_fd != -1)
120 close();
122 super.finalize();
125 public int getNativeFD()
127 return native_fd;
131 * Binds this socket to a particular port and interface
133 * @param port The port to bind to
134 * @param addr The address to bind to
136 * @exception SocketException If an error occurs
138 protected native void bind(int lport, InetAddress laddr)
139 throws SocketException;
141 protected native void connect (InetAddress i, int port)
142 throws SocketException;
144 protected native void disconnect ();
147 * Creates a new datagram socket
149 * @exception SocketException If an error occurs
151 protected native void create() throws SocketException;
153 protected native int peek(InetAddress i) throws IOException;
155 protected native int peekData (DatagramPacket dp) throws IOException;
158 * Sets the Time to Live value for the socket
160 * @param ttl The new TTL value
162 * @exception IOException If an error occurs
164 protected native void setTimeToLive(int ttl) throws IOException;
167 * Gets the Time to Live value for the socket
169 * @return The TTL value
171 * @exception IOException If an error occurs
173 protected native int getTimeToLive() throws IOException;
176 * Sends a packet of data to a remote host
178 * @param packet The packet to send
180 * @exception IOException If an error occurs
182 protected native void send(DatagramPacket p) throws IOException;
185 * Receives a UDP packet from the network
187 * @param packet The packet to fill in with the data received
189 * @exception IOException IOException If an error occurs
191 protected native void receive(DatagramPacket p) throws IOException;
194 * Sets the value of an option on the socket
196 * @param option_id The identifier of the option to set
197 * @param val The value of the option to set
199 * @exception SocketException If an error occurs
201 public native void setOption(int optID, Object value) throws SocketException;
204 * Retrieves the value of an option on the socket
206 * @param option_id The identifier of the option to retrieve
208 * @return The value of the option
210 * @exception SocketException If an error occurs
212 public native Object getOption(int optID) throws SocketException;
215 * Joins or leaves a broadcasting group on a given network interface.
216 * If the network interface is <code>null</code> the group is join/left on
217 * all locale network interfaces.
219 * @param inetAddr The broadcast address.
220 * @param netIf The network interface to join the group on.
221 * @param join True to join a broadcasting group, fals to leave it.
223 * @exception IOException If an error occurs.
225 private native void mcastGrp(InetAddress inetAddr,
226 NetworkInterface netIf,
227 boolean join)
228 throws IOException;
231 * Closes the socket
233 protected native void close();
236 * Gets the Time to Live value for the socket
238 * @return The TTL value
240 * @exception IOException If an error occurs
242 * @deprecated 1.2
244 protected byte getTTL() throws IOException
246 return (byte) getTimeToLive();
250 * Sets the Time to Live value for the socket
252 * @param ttl The new TTL value
254 * @exception IOException If an error occurs
256 * @deprecated 1.2
258 protected void setTTL(byte ttl) throws IOException
260 setTimeToLive(((int) ttl) & 0xFF);
264 * Joins a multicast group
266 * @param addr The group to join
268 * @exception IOException If an error occurs
270 protected void join(InetAddress inetaddr) throws IOException
272 mcastGrp(inetaddr, null, true);
276 * Leaves a multicast group
278 * @param addr The group to leave
280 * @exception IOException If an error occurs
282 protected void leave(InetAddress inetaddr) throws IOException
284 mcastGrp(inetaddr, null, false);
287 protected void joinGroup (SocketAddress mcastaddr, NetworkInterface netIf)
288 throws IOException
290 mcastGrp(((InetSocketAddress)mcastaddr).getAddress(), netIf, true);
293 protected void leaveGroup (SocketAddress mcastaddr, NetworkInterface netIf)
294 throws IOException
296 mcastGrp(((InetSocketAddress)mcastaddr).getAddress(), netIf, false);