1 /*
   2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.net;
  26 
  27 import java.security.AccessController;
  28 import java.security.PrivilegedAction;
  29 
  30 /**
  31  * This class defines a factory for creating DatagramSocketImpls. It defaults
  32  * to creating plain DatagramSocketImpls, but may create other DatagramSocketImpls
  33  * by setting the impl.prefix system property.
  34  *
  35  * For Windows versions lower than Windows Vista a TwoStacksPlainDatagramSocketImpl
  36  * is always created. This impl supports IPv6 on these platform where available.
  37  *
  38  * On Windows platforms greater than Vista that support a dual layer TCP/IP stack
  39  * a DualStackPlainDatagramSocketImpl is created for DatagramSockets. For MulticastSockets
  40  * a TwoStacksPlainDatagramSocketImpl is always created. This is to overcome the lack
  41  * of behavior defined for multicasting over a dual layer socket by the RFC.
  42  *
  43  * @author Chris Hegarty
  44  */
  45 
  46 class DefaultDatagramSocketImplFactory
  47 {
  48     static Class prefixImplClass = null;
  49 
  50     /* the windows version. */
  51     private static float version;
  52 
  53     /* java.net.preferIPv4Stack */
  54     private static boolean preferIPv4Stack = false;
  55 
  56     /* If the version supports a dual stack TCP implementation */
  57     private static boolean useDualStackImpl = false;
  58 
  59     /* sun.net.useExclusiveBind */
  60     private static String exclBindProp;
  61 
  62     /* True if exclusive binding is on for Windows */
  63     private static boolean exclusiveBind = true;
  64 
  65 
  66     static {
  67         // Determine Windows Version.
  68         java.security.AccessController.doPrivileged(
  69                 new PrivilegedAction<Object>() {
  70                     public Object run() {
  71                         version = 0;
  72                         try {
  73                             version = Float.parseFloat(System.getProperties()
  74                                     .getProperty("os.version"));
  75                             preferIPv4Stack = Boolean.parseBoolean(
  76                                               System.getProperties()
  77                                               .getProperty(
  78                                                    "java.net.preferIPv4Stack"));
  79                             exclBindProp = System.getProperty(
  80                                     "sun.net.useExclusiveBind");
  81                         } catch (NumberFormatException e ) {
  82                             assert false : e;
  83                         }
  84                         return null; // nothing to return
  85                     }
  86                 });
  87 
  88         // (version >= 6.0) implies Vista or greater.
  89         if (version >= 6.0 && !preferIPv4Stack) {
  90                 useDualStackImpl = true;
  91         }
  92         if (exclBindProp != null) {
  93             // sun.net.useExclusiveBind is true
  94             exclusiveBind = exclBindProp.length() == 0 ? true
  95                     : Boolean.parseBoolean(exclBindProp);
  96         } else if (version < 6.0) {
  97             exclusiveBind = false;
  98         }
  99 
 100         // impl.prefix
 101         String prefix = null;
 102         try {
 103             prefix = AccessController.doPrivileged(
 104                 new sun.security.action.GetPropertyAction("impl.prefix", null));
 105             if (prefix != null)
 106                 prefixImplClass = Class.forName("java.net."+prefix+"DatagramSocketImpl");
 107         } catch (Exception e) {
 108             System.err.println("Can't find class: java.net." +
 109                                 prefix +
 110                                 "DatagramSocketImpl: check impl.prefix property");
 111         }
 112     }
 113 
 114     /**
 115      * Creates a new <code>DatagramSocketImpl</code> instance.
 116      *
 117      * @param   isMulticast true if this impl is to be used for a MutlicastSocket
 118      * @return  a new instance of <code>PlainDatagramSocketImpl</code>.
 119      */
 120     static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast)
 121         throws SocketException {
 122         if (prefixImplClass != null) {
 123             try {
 124                 return (DatagramSocketImpl) prefixImplClass.newInstance();
 125             } catch (Exception e) {
 126                 throw new SocketException("can't instantiate DatagramSocketImpl");
 127             }
 128         } else {
 129             if (isMulticast)
 130                 exclusiveBind = false;
 131             if (useDualStackImpl && !isMulticast)
 132                 return new DualStackPlainDatagramSocketImpl(exclusiveBind);
 133             else
 134                 return new TwoStacksPlainDatagramSocketImpl(exclusiveBind);
 135         }
 136     }
 137 }