jdk/src/java.base/share/classes/sun/net/ResourceManager.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 25859 3317bb8137f4
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     1
/*
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     4
 *
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    10
 *
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    15
 * accompanied this code).
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    16
 *
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    20
 *
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    23
 * questions.
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    24
 */
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    25
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    26
package sun.net;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    27
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    28
import java.net.SocketException;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    29
import java.util.concurrent.atomic.AtomicInteger;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    30
import sun.security.action.GetPropertyAction;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    31
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    32
/**
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    33
 * Manages count of total number of UDP sockets and ensures
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    34
 * that exception is thrown if we try to create more than the
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    35
 * configured limit.
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    36
 *
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    37
 * This functionality could be put in NetHooks some time in future.
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    38
 */
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    39
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    40
public class ResourceManager {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    41
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    42
    /* default maximum number of udp sockets per VM
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    43
     * when a security manager is enabled.
10909
745b87239228 7032417: Fix for 6981922 does not address multiple VM case
asaha
parents: 8817
diff changeset
    44
     * The default is 25 which is high enough to be useful
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    45
     * but low enough to be well below the maximum number
10909
745b87239228 7032417: Fix for 6981922 does not address multiple VM case
asaha
parents: 8817
diff changeset
    46
     * of port numbers actually available on all OSes
745b87239228 7032417: Fix for 6981922 does not address multiple VM case
asaha
parents: 8817
diff changeset
    47
     * when multiplied by the maximum feasible number of VM processes
745b87239228 7032417: Fix for 6981922 does not address multiple VM case
asaha
parents: 8817
diff changeset
    48
     * that could practically be spawned.
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    49
     */
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    50
10909
745b87239228 7032417: Fix for 6981922 does not address multiple VM case
asaha
parents: 8817
diff changeset
    51
    private static final int DEFAULT_MAX_SOCKETS = 25;
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    52
    private static final int maxSockets;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    53
    private static final AtomicInteger numSockets;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    54
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    55
    static {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 25859
diff changeset
    56
        String prop =
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 25859
diff changeset
    57
                GetPropertyAction.getProperty("sun.net.maxDatagramSockets");
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    58
        int defmax = DEFAULT_MAX_SOCKETS;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    59
        try {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    60
            if (prop != null) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    61
                defmax = Integer.parseInt(prop);
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    62
            }
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    63
        } catch (NumberFormatException e) {}
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    64
        maxSockets = defmax;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    65
        numSockets = new AtomicInteger(0);
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    66
    }
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    67
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    68
    public static void beforeUdpCreate() throws SocketException {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    69
        if (System.getSecurityManager() != null) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    70
            if (numSockets.incrementAndGet() > maxSockets) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    71
                numSockets.decrementAndGet();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    72
                throw new SocketException("maximum number of DatagramSockets reached");
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    73
            }
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    74
        }
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    75
    }
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    76
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    77
    public static void afterUdpClose() {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    78
        if (System.getSecurityManager() != null) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    79
            numSockets.decrementAndGet();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    80
        }
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    81
    }
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
diff changeset
    82
}