Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/net/portconfig.c
32287 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <windows.h>26#include "jni.h"27#include "net_util.h"28#include "sun_net_PortConfig.h"2930#ifdef __cplusplus31extern "C" {32#endif3334struct portrange {35int lower;36int higher;37};3839static int getPortRange(struct portrange *range)40{41OSVERSIONINFO ver;42ver.dwOSVersionInfoSize = sizeof(ver);43GetVersionEx(&ver);4445/* Check for major version 5 or less = Windows XP/2003 or older */46if (ver.dwMajorVersion <= 5) {47LONG ret;48HKEY hKey;49range->lower = 1024;50range->higher = 4999;5152/* check registry to see if upper limit was raised */53ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,54"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",550, KEY_READ, (PHKEY)&hKey56);57if (ret == ERROR_SUCCESS) {58DWORD maxuserport;59ULONG ulType;60DWORD dwLen = sizeof(maxuserport);61ret = RegQueryValueEx(hKey, "MaxUserPort", NULL, &ulType,62(LPBYTE)&maxuserport, &dwLen);63RegCloseKey(hKey);64if (ret == ERROR_SUCCESS) {65range->higher = maxuserport;66}67}68} else {69/* There doesn't seem to be an API to access this. "MaxUserPort"70* is affected, but is not sufficient to determine.71* so we just use the defaults, which are less likely to change72*/73range->lower = 49152;74range->higher = 65535;75}76return 0;77}7879/*80* Class: sun_net_PortConfig81* Method: getLower082* Signature: ()I83*/84JNIEXPORT jint JNICALL Java_sun_net_PortConfig_getLower085(JNIEnv *env, jclass clazz)86{87struct portrange range;88getPortRange(&range);89return range.lower;90}9192/*93* Class: sun_net_PortConfig94* Method: getUpper095* Signature: ()I96*/97JNIEXPORT jint JNICALL Java_sun_net_PortConfig_getUpper098(JNIEnv *env, jclass clazz)99{100struct portrange range;101getPortRange(&range);102return range.higher;103}104#ifdef __cplusplus105}106#endif107108109