jdk/src/share/classes/java/net/AbstractPlainDatagramSocketImpl.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8817 5351579c46a9
child 10594 d6723ee39ebf
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8817
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    31
import sun.net.ResourceManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Abstract datagram and multicast socket implementation base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Note: This is not a public class, so that applets cannot call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * into the implementation directly and hence cannot bypass the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * security checks present in the DatagramSocket and MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Pavani Diwanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /* timeout value for receive() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private int trafficClass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private InetAddress connectedAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private int connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /* cached socket options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int multicastInterface = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean loopbackMode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int ttl = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Load net library into runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                  new sun.security.action.LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates a datagram socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected synchronized void create() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        fd = new FileDescriptor();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    70
        ResourceManager.beforeUdpCreate();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    71
        try {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    72
            datagramSocketCreate();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    73
        } catch (SocketException ioe) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    74
            ResourceManager.afterUdpClose();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    75
            fd = null;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    76
            throw ioe;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    77
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Binds a datagram socket to a local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected synchronized void bind(int lport, InetAddress laddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        bind0(lport, laddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected abstract void bind0(int lport, InetAddress laddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Sends a datagram packet. The packet contains the data and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * destination address to send the packet to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param packet to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected abstract void send(DatagramPacket p) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Connects a datagram socket to a remote destination. This associates the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * address with the local socket so that datagrams may only be sent to this destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * and received from this destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param address the remote InetAddress to connect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param port the remote port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected void connect(InetAddress address, int port) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        connect0(address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        connectedAddress = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        connectedPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Disconnects a previously connected socket. Does nothing if the socket was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * not connected already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected void disconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        disconnect0(connectedAddress.family);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        connectedAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Peek at the packet to see who it is from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param return the address which the packet came from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected abstract int peek(InetAddress i) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected abstract int peekData(DatagramPacket p) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Receive the datagram packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param Packet Received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    protected synchronized void receive(DatagramPacket p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        receive0(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected abstract void receive0(DatagramPacket p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Set the TTL (time-to-live) option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param TTL to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected abstract void setTimeToLive(int ttl) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Get the TTL (time-to-live) option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected abstract int getTimeToLive() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Set the TTL (time-to-live) option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param TTL to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected abstract void setTTL(byte ttl) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Get the TTL (time-to-live) option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected abstract byte getTTL() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Join the multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param multicast address to join.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected void join(InetAddress inetaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        join(inetaddr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Leave the multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param multicast address to leave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    protected void leave(InetAddress inetaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        leave(inetaddr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Join the multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param multicast address to join.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param netIf specifies the local interface to receive multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *        datagram packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        join(((InetSocketAddress)mcastaddr).getAddress(), netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected abstract void join(InetAddress inetaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Leave the multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param multicast address to leave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param netIf specified the local interface to leave the group at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        leave(((InetSocketAddress)mcastaddr).getAddress(), netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Close the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            datagramSocketClose();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   222
            ResourceManager.afterUdpClose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    protected boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return (fd == null) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * set a value - since we only support (setting) binary options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * here, o must be a Boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     public void setOption(int optID, Object o) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
             throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         switch (optID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            /* check type safety b4 going native.  These should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
             * fail, since only java.Socket* has access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
             * PlainSocketImpl.setOption().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         case SO_TIMEOUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
             if (o == null || !(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                 throw new SocketException("bad argument for SO_TIMEOUT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
             int tmp = ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
             if (tmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                 throw new IllegalArgumentException("timeout < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
             timeout = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
             return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
             if (o == null || !(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                 throw new SocketException("bad argument for IP_TOS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
             trafficClass = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
             if (o == null || !(o instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                 throw new SocketException("bad argument for SO_REUSEADDR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         case SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
             if (o == null || !(o instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                 throw new SocketException("bad argument for SO_BROADCAST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
             throw new SocketException("Cannot re-bind Socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
             if (o == null || !(o instanceof Integer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                 ((Integer)o).intValue() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                 throw new SocketException("bad argument for SO_SNDBUF or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                           "SO_RCVBUF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         case IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
             if (o == null || !(o instanceof InetAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                 throw new SocketException("bad argument for IP_MULTICAST_IF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         case IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
             if (o == null || !(o instanceof NetworkInterface))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                 throw new SocketException("bad argument for IP_MULTICAST_IF2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         case IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
             if (o == null || !(o instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                 throw new SocketException("bad argument for IP_MULTICAST_LOOP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
             throw new SocketException("invalid option: " + optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         socketSetOption(optID, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * get option's state - set or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public Object getOption(int optID) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        Object result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        switch (optID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            case SO_TIMEOUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                result = new Integer(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                result = socketGetOption(optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if ( ((Integer)result).intValue() == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    result = new Integer(trafficClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            case IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            case IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            case IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            case SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                result = socketGetOption(optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                throw new SocketException("invalid option: " + optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    protected abstract void datagramSocketCreate() throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    protected abstract void datagramSocketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    protected abstract void socketSetOption(int opt, Object val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    protected abstract Object socketGetOption(int opt) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    protected abstract void connect0(InetAddress address, int port) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    protected abstract void disconnect0(int family);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
}