jdk/src/windows/classes/java/net/DefaultDatagramSocketImplFactory.java
author khazra
Thu, 14 Mar 2013 13:46:15 -0700
changeset 18192 fa6bd0992104
parent 10596 39b3a979e600
child 23010 6dadb192ad81
permissions -rw-r--r--
7170730: Improve Windows network stack support. Summary: Enable exclusive binding of ports on Windows Reviewed-by: alanb, chegar, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 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.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class defines a factory for creating DatagramSocketImpls. It defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * to creating plain DatagramSocketImpls, but may create other DatagramSocketImpls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * by setting the impl.prefix system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * For Windows versions lower than Windows Vista a TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * is always created. This impl supports IPv6 on these platform where available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * On Windows platforms greater than Vista that support a dual layer TCP/IP stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * a DualStackPlainDatagramSocketImpl is created for DatagramSockets. For MulticastSockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * a TwoStacksPlainDatagramSocketImpl is always created. This is to overcome the lack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of behavior defined for multicasting over a dual layer socket by the RFC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Chris Hegarty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class DefaultDatagramSocketImplFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    48
    static Class<?> prefixImplClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* the windows version. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static float version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* java.net.preferIPv4Stack */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static boolean preferIPv4Stack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* If the version supports a dual stack TCP implementation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static boolean useDualStackImpl = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    59
    /* sun.net.useExclusiveBind */
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    60
    private static String exclBindProp;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    61
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    62
    /* True if exclusive binding is on for Windows */
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    63
    private static boolean exclusiveBind = true;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    64
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    65
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // Determine Windows Version.
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    68
        java.security.AccessController.doPrivileged(
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    69
                new PrivilegedAction<Object>() {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    70
                    public Object run() {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    71
                        version = 0;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    72
                        try {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    73
                            version = Float.parseFloat(System.getProperties()
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    74
                                    .getProperty("os.version"));
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    75
                            preferIPv4Stack = Boolean.parseBoolean(
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    76
                                              System.getProperties()
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    77
                                              .getProperty(
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    78
                                                   "java.net.preferIPv4Stack"));
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    79
                            exclBindProp = System.getProperty(
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    80
                                    "sun.net.useExclusiveBind");
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    81
                        } catch (NumberFormatException e ) {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    82
                            assert false : e;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    83
                        }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    84
                        return null; // nothing to return
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    }
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    86
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // (version >= 6.0) implies Vista or greater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (version >= 6.0 && !preferIPv4Stack) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    90
                useDualStackImpl = true;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    91
        }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    92
        if (exclBindProp != null) {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    93
            // sun.net.useExclusiveBind is true
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    94
            exclusiveBind = exclBindProp.length() == 0 ? true
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    95
                    : Boolean.parseBoolean(exclBindProp);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    96
        } else if (version < 6.0) {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    97
            exclusiveBind = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // impl.prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String prefix = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            prefix = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                new sun.security.action.GetPropertyAction("impl.prefix", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (prefix != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                prefixImplClass = Class.forName("java.net."+prefix+"DatagramSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.err.println("Can't find class: java.net." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                "DatagramSocketImpl: check impl.prefix property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Creates a new <code>DatagramSocketImpl</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param   isMulticast true if this impl is to be used for a MutlicastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return  a new instance of <code>PlainDatagramSocketImpl</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (prefixImplClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return (DatagramSocketImpl) prefixImplClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                throw new SocketException("can't instantiate DatagramSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } else {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
   129
            if (isMulticast)
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
   130
                exclusiveBind = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (useDualStackImpl && !isMulticast)
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
   132
                return new DualStackPlainDatagramSocketImpl(exclusiveBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            else
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
   134
                return new TwoStacksPlainDatagramSocketImpl(exclusiveBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}