Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/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 <stdio.h>26#include <stdlib.h>27#include <unistd.h>28#include <errno.h>2930#if defined(_ALLBSD_SOURCE)31#include <sys/sysctl.h>32#endif3334#include "jni.h"35#include "net_util.h"36#include "sun_net_PortConfig.h"3738#ifdef __cplusplus39extern "C" {40#endif4142struct portrange {43int lower;44int higher;45};4647static int getPortRange(struct portrange *range)48{49#ifdef __linux__50{51FILE *f;52int ret;5354f = fopen("/proc/sys/net/ipv4/ip_local_port_range", "r");55if (f != NULL) {56ret = fscanf(f, "%d %d", &range->lower, &range->higher);57fclose(f);58return ret == 2 ? 0 : -1;59}60return -1;61}6263#elif defined(__solaris__)64{65range->higher = net_getParam("/dev/tcp", "tcp_largest_anon_port");66range->lower = net_getParam("/dev/tcp", "tcp_smallest_anon_port");67return 0;68}69#elif defined(_ALLBSD_SOURCE)70{71int ret;72size_t size = sizeof(range->lower);73ret = sysctlbyname(74"net.inet.ip.portrange.first", &range->lower, &size, 0, 075);76if (ret == -1) {77return -1;78}79size = sizeof(range->higher);80ret = sysctlbyname(81"net.inet.ip.portrange.last", &range->higher, &size, 0, 082);83return ret;84}85#else86return -1;87#endif88}8990/*91* Class: sun_net_PortConfig92* Method: getLower093* Signature: ()I94*/95JNIEXPORT jint JNICALL Java_sun_net_PortConfig_getLower096(JNIEnv *env, jclass clazz)97{98struct portrange range;99if (getPortRange(&range) < 0) {100return -1;101}102return range.lower;103}104105/*106* Class: sun_net_PortConfig107* Method: getUpper0108* Signature: ()I109*/110JNIEXPORT jint JNICALL Java_sun_net_PortConfig_getUpper0111(JNIEnv *env, jclass clazz)112{113struct portrange range;114if (getPortRange(&range) < 0) {115return -1;116}117return range.higher;118}119120#ifdef __cplusplus121}122#endif123124125