OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [vm/] [reference/] [gnu/] [java/] [net/] [VMPlainSocketImpl.java] - Blame information for rev 780

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 780 jeremybenn
/* VMPlainSocketImpl.java -- VM interface for default socket implementation
2
   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
package gnu.java.net;
39
 
40
import java.io.IOException;
41
import java.net.Inet4Address;
42
import java.net.Inet6Address;
43
import java.net.InetAddress;
44
import java.net.InetSocketAddress;
45
import java.net.NetworkInterface;
46
import java.net.SocketException;
47
import java.net.SocketOptions;
48
 
49
import gnu.classpath.Configuration;
50
import gnu.java.nio.VMChannel;
51
 
52
/**
53
 * The VM interface for {@link gnu.java.net.PlainSocketImpl}.
54
 *
55
 * @author Ingo Proetel (proetel@aicas.com)
56
 * @author Roman Kennke (kennke@aicas.com)
57
 */
58
public final class VMPlainSocketImpl
59
{
60
  /** Option id for time to live
61
   */
62
  private static final int CP_IP_TTL = 0x1E61;
63
 
64
  private final State nfd;
65
 
66
  /**
67
   * Static initializer to load native library.
68
   */
69
  static
70
  {
71
    if (Configuration.INIT_LOAD_LIBRARY)
72
      {
73
        System.loadLibrary("javanet");
74
      }
75
  }
76
 
77
  public VMPlainSocketImpl()
78
  {
79
    // XXX consider adding security check here.
80
    nfd = new State();
81
  }
82
 
83
  public VMPlainSocketImpl(VMChannel channel) throws IOException
84
  {
85
    this();
86
    nfd.setChannelFD(channel.getState());
87
  }
88
 
89
  public State getState()
90
  {
91
    return nfd;
92
  }
93
 
94
  /** This method exists to hide the CP_IP_TTL value from
95
   * higher levels.
96
   *
97
   * Always think of JNode ... :)
98
   */
99
  public void setTimeToLive(int ttl)
100
    throws SocketException
101
  {
102
    try
103
      {
104
        setOption(nfd.getNativeFD(), CP_IP_TTL, ttl);
105
      }
106
    catch (IOException ioe)
107
      {
108
        SocketException se = new SocketException();
109
        se.initCause(ioe);
110
        throw se;
111
      }
112
  }
113
 
114
  public int getTimeToLive()
115
    throws SocketException
116
  {
117
    try
118
      {
119
        return getOption(nfd.getNativeFD(), CP_IP_TTL);
120
      }
121
    catch (IOException ioe)
122
      {
123
        SocketException se = new SocketException();
124
        se.initCause(ioe);
125
        throw se;
126
      }
127
  }
128
 
129
  public void setOption(int optionId, Object optionValue)
130
    throws SocketException
131
  {
132
    int value;
133
    if (optionValue instanceof Integer)
134
      value = ((Integer) optionValue).intValue();
135
    else if (optionValue instanceof Boolean)
136
      // Switching off the linger behavior is done by setting
137
      // the value to -1. This is how the Java API interprets
138
      // it.
139
      value = ((Boolean) optionValue).booleanValue()
140
              ? 1
141
              : (optionId == SocketOptions.SO_LINGER)
142
              ? -1
143
              : 0;
144
    else
145
      throw new IllegalArgumentException("option value type "
146
                                         + optionValue.getClass().getName());
147
 
148
    try
149
      {
150
        setOption(nfd.getNativeFD(), optionId, value);
151
      }
152
    catch (IOException ioe)
153
      {
154
        SocketException se = new SocketException();
155
        se.initCause(ioe);
156
        throw se;
157
      }
158
  }
159
 
160
  private static native void setOption(int fd, int id, int value)
161
    throws SocketException;
162
 
163
  public void setMulticastInterface(int optionId, InetAddress addr)
164
    throws SocketException
165
  {
166
    try
167
      {
168
        if (addr instanceof Inet4Address)
169
          setMulticastInterface(nfd.getNativeFD(), optionId, (Inet4Address) addr);
170
        else if (addr instanceof Inet6Address)
171
          {
172
            NetworkInterface iface = NetworkInterface.getByInetAddress(addr);
173
            setMulticastInterface6(nfd.getNativeFD(), optionId, iface.getName());
174
          }
175
        else
176
          throw new SocketException("Unknown address format: " + addr);
177
      }
178
    catch (SocketException se)
179
      {
180
        throw se;
181
      }
182
    catch (IOException ioe)
183
      {
184
        SocketException se = new SocketException();
185
        se.initCause(ioe);
186
        throw se;
187
      }
188
  }
189
 
190
  private static native void setMulticastInterface(int fd,
191
                                                   int optionId,
192
                                                   Inet4Address addr);
193
 
194
  private static native void setMulticastInterface6(int fd,
195
                                                    int optionId,
196
                                                    String ifName);
197
 
198
  /**
199
   * Get a socket option. This implementation is only required to support
200
   * socket options that are boolean values, which include:
201
   *
202
   *  SocketOptions.IP_MULTICAST_LOOP
203
   *  SocketOptions.SO_BROADCAST
204
   *  SocketOptions.SO_KEEPALIVE
205
   *  SocketOptions.SO_OOBINLINE
206
   *  SocketOptions.SO_REUSEADDR
207
   *  SocketOptions.TCP_NODELAY
208
   *
209
   * and socket options that are integer values, which include:
210
   *
211
   *  SocketOptions.IP_TOS
212
   *  SocketOptions.SO_LINGER
213
   *  SocketOptions.SO_RCVBUF
214
   *  SocketOptions.SO_SNDBUF
215
   *  SocketOptions.SO_TIMEOUT
216
   *
217
   * @param optionId The option ID to fetch.
218
   * @return A {@link Boolean} or {@link Integer} containing the socket
219
   *  option.
220
   * @throws SocketException
221
   */
222
  public Object getOption(int optionId) throws SocketException
223
  {
224
    int value;
225
    try
226
      {
227
        value = getOption(nfd.getNativeFD(), optionId);
228
      }
229
    catch (IOException ioe)
230
      {
231
        SocketException se = new SocketException();
232
        se.initCause(ioe);
233
        throw se;
234
      }
235
 
236
    switch (optionId)
237
      {
238
        case SocketOptions.IP_MULTICAST_LOOP:
239
        case SocketOptions.SO_BROADCAST:
240
        case SocketOptions.SO_KEEPALIVE:
241
        case SocketOptions.SO_OOBINLINE:
242
        case SocketOptions.SO_REUSEADDR:
243
        case SocketOptions.TCP_NODELAY:
244
          return Boolean.valueOf(value != 0);
245
 
246
        case SocketOptions.IP_TOS:
247
        case SocketOptions.SO_LINGER:
248
        case SocketOptions.SO_RCVBUF:
249
        case SocketOptions.SO_SNDBUF:
250
        case SocketOptions.SO_TIMEOUT:
251
          return new Integer(value);
252
 
253
        default:
254
          throw new SocketException("getting option " + optionId +
255
                                    " not supported here");
256
      }
257
  }
258
 
259
  private static native int getOption(int fd, int id) throws SocketException;
260
 
261
  /**
262
   * Returns an Inet4Address or Inet6Address instance belonging to the
263
   * interface which is set as the multicast interface.
264
   *
265
   * The optionId is provided to make it possible that the native
266
   * implementation may do something different depending on whether
267
   * the value is SocketOptions.IP_MULTICAST_IF or
268
   * SocketOptions.IP_MULTICAST_IF2.
269
   */
270
  public InetAddress getMulticastInterface(int optionId)
271
    throws SocketException
272
  {
273
    try
274
      {
275
        return getMulticastInterface(nfd.getNativeFD(), optionId);
276
      }
277
    catch (IOException ioe)
278
      {
279
        SocketException se = new SocketException();
280
        se.initCause(ioe);
281
        throw se;
282
      }
283
  }
284
 
285
  private static native InetAddress getMulticastInterface(int fd,
286
                                                          int optionId);
287
 
288
  /**
289
   * Binds this socket to the given local address and port.
290
   *
291
   * @param address The address to bind to; the InetAddress is either
292
   *  an IPv4 or IPv6 address.
293
   * @throws IOException If binding fails; for example, if the port
294
   *  in the given InetSocketAddress is privileged, and the current
295
   *  process has insufficient privileges.
296
   */
297
  public void bind(InetSocketAddress address) throws IOException
298
  {
299
    InetAddress addr = address.getAddress();
300
    if (addr instanceof Inet4Address)
301
      {
302
        bind (nfd.getNativeFD(), addr.getAddress(), address.getPort());
303
      }
304
    else if (addr instanceof Inet6Address)
305
      bind6 (nfd.getNativeFD(), addr.getAddress(), address.getPort());
306
    else
307
      throw new SocketException ("unsupported address type");
308
  }
309
 
310
  /**
311
   * Native bind function for IPv4 addresses. The addr array must be
312
   * exactly four bytes long.
313
   *
314
   * VMs without native support need not implement this.
315
   *
316
   * @param fd The native file descriptor integer.
317
   * @param addr The IPv4 address, in network byte order.
318
   * @param port The port to bind to.
319
   * @throws IOException
320
   */
321
  private static native void bind(int fd, byte[] addr, int port)
322
    throws IOException;
323
 
324
  /**
325
   * Native bind function for IPv6 addresses. The addr array must be
326
   * exactly sixteen bytes long.
327
   *
328
   * VMs without native support need not implement this.
329
   *
330
   * @param fd The native file descriptor integer.
331
   * @param addr The IPv6 address, in network byte order.
332
   * @param port The port to bind to.
333
   * @throws IOException
334
   */
335
  private static native void bind6(int fd, byte[] addr, int port)
336
    throws IOException;
337
 
338
  /**
339
   * Listen on this socket for incoming connections.
340
   *
341
   * @param backlog The backlog of connections.
342
   * @throws IOException If listening fails.
343
   * @see gnu.java.nio.VMChannel#accept()
344
   */
345
  public void listen(int backlog) throws IOException
346
  {
347
    listen(nfd.getNativeFD(), backlog);
348
  }
349
 
350
  /**
351
   * Native listen function. VMs without native support need not implement
352
   * this.
353
   *
354
   * @param fd The file descriptor integer.
355
   * @param backlog The listen backlog size.
356
   * @throws IOException
357
   */
358
  private static native void listen(int fd, int backlog) throws IOException;
359
 
360
  public void join(InetAddress group) throws IOException
361
  {
362
    if (group instanceof Inet4Address)
363
      join(nfd.getNativeFD(), group.getAddress());
364
    else if (group instanceof Inet6Address)
365
      join6(nfd.getNativeFD(), group.getAddress());
366
    else
367
      throw new IllegalArgumentException("unknown address type");
368
  }
369
 
370
  private static native void join(int fd, byte[] addr) throws IOException;
371
 
372
  private static native void join6(int fd, byte[] addr) throws IOException;
373
 
374
  public void leave(InetAddress group) throws IOException
375
  {
376
    if (group instanceof Inet4Address)
377
      leave(nfd.getNativeFD(), group.getAddress());
378
    else if (group instanceof Inet6Address)
379
      leave6(nfd.getNativeFD(), group.getAddress());
380
    else
381
      throw new IllegalArgumentException("unknown address type");
382
  }
383
 
384
  private static native void leave(int fd, byte[] addr) throws IOException;
385
 
386
  private static native void leave6(int fd, byte[] addr) throws IOException;
387
 
388
  public void joinGroup(InetSocketAddress addr, NetworkInterface netif)
389
    throws IOException
390
  {
391
    InetAddress address = addr.getAddress();
392
 
393
    if (address instanceof Inet4Address)
394
      joinGroup(nfd.getNativeFD(), address.getAddress(),
395
                netif != null ? netif.getName() : null);
396
    else if (address instanceof Inet6Address)
397
      joinGroup6(nfd.getNativeFD(), address.getAddress(),
398
                 netif != null ? netif.getName() : null);
399
    else
400
      throw new IllegalArgumentException("unknown address type");
401
  }
402
 
403
  private static native void joinGroup(int fd, byte[] addr, String ifname)
404
    throws IOException;
405
 
406
  private static native void joinGroup6(int fd, byte[] addr, String ifname)
407
    throws IOException;
408
 
409
  public void leaveGroup(InetSocketAddress addr, NetworkInterface netif)
410
    throws IOException
411
  {
412
    InetAddress address = addr.getAddress();
413
    if (address instanceof Inet4Address)
414
      leaveGroup(nfd.getNativeFD(), address.getAddress(),
415
                 netif != null ? netif.getName() : null);
416
    else if (address instanceof Inet6Address)
417
      leaveGroup6(nfd.getNativeFD(), address.getAddress(),
418
                 netif != null ? netif.getName() : null);
419
    else
420
      throw new IllegalArgumentException("unknown address type");
421
  }
422
 
423
  private static native void leaveGroup(int fd, byte[] addr, String ifname)
424
    throws IOException;
425
 
426
  private static native void leaveGroup6(int fd, byte[] addr, String ifname)
427
    throws IOException;
428
 
429
 
430
  public void shutdownInput() throws IOException
431
  {
432
    shutdownInput(nfd.getNativeFD());
433
  }
434
 
435
  private static native void shutdownInput(int native_fd) throws IOException;
436
 
437
  public void shutdownOutput() throws IOException
438
  {
439
    shutdownOutput(nfd.getNativeFD());
440
  }
441
 
442
  private static native void shutdownOutput(int native_fd) throws IOException;
443
 
444
  public void sendUrgentData(int data) throws IOException
445
  {
446
    sendUrgentData(nfd.getNativeFD(), data);
447
  }
448
 
449
  private static native void sendUrgentData(int natfive_fd, int data) throws IOException;
450
 
451
  public void close() throws IOException
452
  {
453
    nfd.close();
454
  }
455
 
456
  // Inner classes.
457
 
458
  /**
459
   * Our wrapper for the native file descriptor. In this implementation,
460
   * it is a simple wrapper around {@link VMChannel.State}, to simplify
461
   * management of the native state.
462
   */
463
  public final class State
464
  {
465
    private VMChannel.State channelFd;
466
 
467
    State()
468
    {
469
      channelFd = null;
470
    }
471
 
472
    public boolean isValid()
473
    {
474
      if (channelFd != null)
475
        return channelFd.isValid();
476
      return false;
477
    }
478
 
479
    public int getNativeFD() throws IOException
480
    {
481
      return channelFd.getNativeFD();
482
    }
483
 
484
    public void setChannelFD(final VMChannel.State nfd) throws IOException
485
    {
486
      if (this.channelFd != null && this.channelFd.isValid())
487
        throw new IOException("file descriptor already initialized");
488
      this.channelFd = nfd;
489
    }
490
 
491
    public void close() throws IOException
492
    {
493
      if (channelFd == null)
494
        throw new IOException("invalid file descriptor");
495
      channelFd.close();
496
    }
497
 
498
    protected void finalize() throws Throwable
499
    {
500
      try
501
        {
502
          if (isValid())
503
            close();
504
        }
505
      finally
506
        {
507
          super.finalize();
508
        }
509
    }
510
  }
511
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.