src/java.base/windows/native/libnet/portconfig.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/windows/native/libnet/portconfig.c@3317bb8137f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     1
/*
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     4
 *
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    10
 *
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    15
 * accompanied this code).
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    16
 *
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    20
 *
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    23
 * questions.
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    24
 */
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    25
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    26
#include <windows.h>
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    27
#include "jni.h"
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    28
#include "net_util.h"
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    29
#include "sun_net_PortConfig.h"
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    30
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    31
#ifdef __cplusplus
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    32
extern "C" {
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    33
#endif
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    34
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    35
struct portrange {
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    36
    int lower;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    37
    int higher;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    38
};
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    39
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    40
static int getPortRange(struct portrange *range)
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    41
{
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    42
    OSVERSIONINFO ver;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    43
    ver.dwOSVersionInfoSize = sizeof(ver);
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    44
    GetVersionEx(&ver);
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    45
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    46
    /* Check for major version 5 or less = Windows XP/2003 or older */
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    47
    if (ver.dwMajorVersion <= 5) {
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    48
        LONG ret;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    49
        HKEY hKey;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    50
        range->lower = 1024;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    51
        range->higher = 4999;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    52
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    53
        /* check registry to see if upper limit was raised */
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    54
        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    55
                   "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    56
                   0, KEY_READ, (PHKEY)&hKey
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    57
        );
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    58
        if (ret == ERROR_SUCCESS) {
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    59
            DWORD maxuserport;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    60
            ULONG ulType;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    61
            DWORD dwLen = sizeof(maxuserport);
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    62
            ret = RegQueryValueEx(hKey, "MaxUserPort",  NULL, &ulType,
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    63
                             (LPBYTE)&maxuserport, &dwLen);
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    64
            RegCloseKey(hKey);
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    65
            if (ret == ERROR_SUCCESS) {
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    66
                range->higher = maxuserport;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    67
            }
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    68
        }
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    69
    } else {
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    70
        /* There doesn't seem to be an API to access this. "MaxUserPort"
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    71
          * is affected, but is not sufficient to determine.
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    72
         * so we just use the defaults, which are less likely to change
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    73
          */
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    74
        range->lower = 49152;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    75
        range->higher = 65535;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    76
    }
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    77
    return 0;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    78
}
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    79
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    80
/*
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    81
 * Class:     sun_net_PortConfig
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    82
 * Method:    getLower0
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    83
 * Signature: ()I
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    84
 */
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    85
JNIEXPORT jint JNICALL Java_sun_net_PortConfig_getLower0
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    86
  (JNIEnv *env, jclass clazz)
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    87
{
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    88
    struct portrange range;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    89
    getPortRange(&range);
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    90
    return range.lower;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    91
}
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    92
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    93
/*
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    94
 * Class:     sun_net_PortConfig
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    95
 * Method:    getUpper0
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    96
 * Signature: ()I
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    97
 */
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    98
JNIEXPORT jint JNICALL Java_sun_net_PortConfig_getUpper0
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
    99
  (JNIEnv *env, jclass clazz)
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
   100
{
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
   101
    struct portrange range;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
   102
    getPortRange(&range);
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
   103
    return range.higher;
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
   104
}
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
   105
#ifdef __cplusplus
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
   106
}
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
diff changeset
   107
#endif