aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net/DatagramSocket.java
blob: 93aea0754ed8be7f4d3c8d2323d54e1f0f55babf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// DatagramSocket.java

/* Copyright (C) 1999, 2000  Free Software Foundation

   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 <warrenl@cygnus.com>
 * @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;

  public DatagramSocket() throws SocketException
  {
    this(0, null);
  }

  /**
   * Creates a datagram socket that is bound to a specific port
   *
   * @param port The port number to bind to
   *
   * @exception SocketException If an error occurs
   */
  public DatagramSocket(int port) throws SocketException
  {
    this(port, null);
  }

  /**
   * Creates a datagram socket that is bound to a specific port/inet address
   *
   * @param port The port number to bind to
   * @param laddr The local address to bind to
   *
   * @exception SocketException If an error occurs
   */
  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(""))
      impl = new PlainDatagramSocketImpl();
    else
      try
	{
	  impl = (DatagramSocketImpl) Class.forName("java.net." + propVal +
					"DatagramSocketImpl").newInstance();
	}
      catch (Exception e)
	{
	  System.err.println("Could not instantiate class: java.net." +
	    propVal + "DatagramSocketImpl");
	  impl = new PlainDatagramSocketImpl();
	}
    impl.create();

    // For multicasting, set the socket to be reused (Stevens pp. 195-6).
    if (this instanceof MulticastSocket)
      impl.setOption(SocketOptions.SO_REUSEADDR, new Boolean(true));

    impl.bind(port, laddr == null ? InetAddress.ANY_IF : laddr);
  }

  /**
   * Closes the datagram socket
   */
  public void close()
  {
    impl.close();
  }

  /**
   * Returns the local address of the datagram socket
   * 
   * @since 1.1
   */
  public InetAddress getLocalAddress()
  {
    SecurityManager s = System.getSecurityManager();
    // FIXME: JCL p. 510 says this should call checkConnect.  But what
    // string should be used as the hostname?  Maybe this is just a side
    // effect of calling InetAddress.getLocalHost.
    //
    // And is getOption with SO_BINDADDR the right way to get the address?
    // Doesn't seem to be since this method doesn't throw a SocketException
    // and SO_BINADDR can throw one.
    //
    // Also see RETURNS section in JCL p. 510 about returning any local
    // addr "if the current execution context is not allowed to connect to
    // the network interface that is actually bound to this datagram socket."
    // How is that done?  via InetAddress.getLocalHost?  But that throws
    // an UnknownHostException and this method doesn't.
    //
    // if (s != null)
    //   s.checkConnect("localhost", -1);
    try
      {
	return (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
      }
    catch (SocketException ex)
      {
      }

    try
      {
	return InetAddress.getLocalHost();
      }
    catch (UnknownHostException ex)
      {
	// FIXME: This should never happen, so how can we avoid this construct?
	return null;
      }
  }

  /**
   * Returns the local port this socket uses
   *
   * @return The local port number
   */
  public int getLocalPort()
  {
    return impl.getLocalPort();
  }

  /**
   * Gets the SO_TIMEOUT value
   *
   * @return The current timeout in milliseconds
   *
   * @exception SocketException If an error occurs
   * 
   * @since 1.1
   */
  public synchronized int getSoTimeout() throws SocketException
  {
    Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
    if (timeout instanceof Integer) 
      return ((Integer)timeout).intValue();
    else
      return 0;
  }

  /**
   * Receive a datagram packet
   *
   * @param p The datagram packet to put the incoming data into
   * 
   * @exception IOException If an error occurs
   */
  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);
  }

  /**
   * Sends a datagram packet
   *
   * @param p The datagram packet to send
   *
   * @exception IOException If an error occurs
   */
  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 getTimeToLive for TTL val.
    impl.send(p);
  }

  /**
   * Sets a new value for SO_TIMEOUT
   *
   * @param timeout The timeout in milliseconds
   *
   * @exception SocketException If an error occurs
   *
   * @since 1.1
   */
  public synchronized void setSoTimeout(int timeout) throws SocketException
  {
    if (timeout < 0)
      throw new IllegalArgumentException("Invalid timeout: " + timeout);

    impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
  }

  /**
   * Connects the datagrem socket to a specified address/port
   *
   * @param address The address to connect to
   * @param port The port to connect to
   *
   * @exception SocketException If an error occurs
   *
   * @since 1.2
   */
  public void connect(InetAddress address, int port)
    throws SocketException
  {
    //impl.connect(address, port);
  }

  /**
   * Disconnects the datagram socket
   *
   * @since 1.2
   */
  public void disconnect()
  {
    //impl.disconnect();
  }

  /**
   * Returns the InetAddress the socket is connected to
   * or null if the socket is not connected
   * 
   * @since 1.2
   */
  public InetAddress getInetAddress()
  {
    // FIXME:
    return null;
  }

  /**
   * Returns the local port number of the socket
   * 
   * @since 1.2
   */
  public int getPort()
  {
    return impl.localPort;
  }

  /**
   * This method returns the value of the system level socket option
   * SO_RCVBUF, which is used by the operating system to tune buffer
   * sizes for data transfers.
   *
   * @return The receive buffer size.
   *
   * @exception SocketException If an error occurs.
   *
   * @since 1.2
   */
  public int getReceiveBufferSize() throws SocketException
  {
    Object obj = impl.getOption(SocketOptions.SO_RCVBUF);
  
    if (obj instanceof Integer)
      return(((Integer)obj).intValue());
    else 
      throw new SocketException("Unexpected type");
  }

  /**
   * Enables/Disables SO_REUSEADDR
   * 
   * @param on Whether or not to have SO_REUSEADDR turned on
   *
   * @exception SocketException If an error occurs
   *
   * @since 1.4
   */
  public void setReuseAddress(boolean on) throws SocketException
  {
    impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
  }

  /**
   * Checks if SO_REUSEADDR is enabled
   *
   * @exception SocketException If an error occurs
   * 
   * @since 1.4
   */
  public boolean getReuseAddress() throws SocketException
  {
    Object obj = impl.getOption (SocketOptions.SO_REUSEADDR);
  
    if (obj instanceof Boolean)
      return(((Boolean) obj).booleanValue ());
    else 
      throw new SocketException ("Unexpected type");
  }

  /**
   * Enables/Disables SO_BROADCAST
   * 
   * @param on Whether or not to have SO_BROADCAST turned on
   *
   * @exception SocketException If an error occurs
   *
   * @since 1.4
   */
  public void setBroadcast(boolean on) throws SocketException
  {
    impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on));
  }

  /**
   * Checks if SO_BROADCAST is enabled
   * 
   * @exception SocketException If an error occurs
   * 
   * @since 1.4
   */
  public boolean getBroadcast() throws SocketException
  {
    Object obj = impl.getOption (SocketOptions.SO_BROADCAST);
  
    if (obj instanceof Boolean)
      return ((Boolean) obj).booleanValue ();
    else 
      throw new SocketException ("Unexpected type");
  }

  /**
   * Sets the traffic class value
   *
   * @param tc The traffic class
   *
   * @exception SocketException If an error occurs
   * @exception IllegalArgumentException If tc < 0 or rc > 255
   *
   * @see DatagramSocket:getTrafficClass
   * 
   * @since 1.4
   */
  public void setTrafficClass(int tc)
    throws SocketException
  {
    if (tc < 0 || tc > 255)
      throw new IllegalArgumentException();

    impl.setOption (SocketOptions.IP_TOS, new Integer (tc));
  }
  
  /**
   * Returns the current traffic class
   * 
   * @see DatagramSocket:setTrafficClass
   *
   * @exception SocketException If an error occurs
   * 
   * @since 1.4
   */
  public int getTrafficClass() throws SocketException
  {
    Object obj = impl.getOption(SocketOptions.IP_TOS);

    if (obj instanceof Integer)
      return ((Integer) obj).intValue ();
    else
      throw new SocketException ("Unexpected type");
  }
  
  /**
   * This method returns the value of the system level socket option
   * SO_SNDBUF, which is used by the operating system to tune buffer
   * sizes for data transfers.
   *
   * @return The send buffer size.
   *
   * @exception SocketException If an error occurs.
   *
   * @since 1.2
   */
  public int getSendBufferSize() throws SocketException
  {
    Object obj = impl.getOption(SocketOptions.SO_SNDBUF);

    if (obj instanceof Integer)
      return(((Integer)obj).intValue());
    else
      throw new SocketException("Unexpected type");
  }

  /**
   * This method sets the value for the system level socket option
   * SO_RCVBUF to the specified value.  Note that valid values for this
   * option are specific to a given operating system.
   *
   * @param size The new receive buffer size.
   *
   * @exception SocketException If an error occurs.
   *  
   * @since 1.2
   */
  public void setReceiveBufferSize(int size) throws SocketException
  {
    if (size < 0)
      throw new IllegalArgumentException("Buffer size is less than 0");

    impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
  }

  /**
   * This method sets the value for the system level socket option
   * SO_SNDBUF to the specified value.  Note that valid values for this
   * option are specific to a given operating system.
   *
   * @param size The new send buffer size.
   *
   * @exception SocketException If an error occurs.
   *
   * @since 1.2
   */
  public void setSendBufferSize(int size) throws SocketException
  {
    if (size < 0)
      throw new IllegalArgumentException("Buffer size is less than 0");
  
    impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
  }
}