1/*
2 * Copyright (c) 2013, 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
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <errno.h>
30
31#if defined(_ALLBSD_SOURCE)
32#include <sys/sysctl.h>
33#endif
34
35#include "jni.h"
36#include "net_util.h"
37#include "sun_net_PortConfig.h"
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43struct portrange {
44 int lower;
45 int higher;
46};
47
48static int getPortRange(struct portrange *range)
49{
50#ifdef __linux__
51 {
52 FILE *f;
53 int ret;
54
55 f = fopen("/proc/sys/net/ipv4/ip_local_port_range", "r");
56 if (f != NULL) {
57 ret = fscanf(f, "%d %d", &range->lower, &range->higher);
58 fclose(f);
59 return ret == 2 ? 0 : -1;
60 }
61 return -1;
62 }
63
64#elif defined(__solaris__)
65 {
66 range->higher = net_getParam("/dev/tcp", "tcp_largest_anon_port");
67 range->lower = net_getParam("/dev/tcp", "tcp_smallest_anon_port");
68 return 0;
69 }
70#elif defined(_ALLBSD_SOURCE)
71 {
72 int ret;
73 size_t size = sizeof(range->lower);
74 ret = sysctlbyname(
75 "net.inet.ip.portrange.first", &range->lower, &size, 0, 0
76 );
77 if (ret == -1) {
78 return -1;
79 }
80 size = sizeof(range->higher);
81 ret = sysctlbyname(
82 "net.inet.ip.portrange.last", &range->higher, &size, 0, 0
83 );
84 return ret;
85 }
86#else
87 return -1;
88#endif
89}
90
91/*
92 * Class: sun_net_PortConfig
93 * Method: getLower0
94 * Signature: ()I
95 */
96JNIEXPORT jint JNICALL Java_sun_net_PortConfig_getLower0
97 (JNIEnv *env, jclass clazz)
98{
99 struct portrange range;
100 if (getPortRange(&range) < 0) {
101 return -1;
102 }
103 return range.lower;
104}
105
106/*
107 * Class: sun_net_PortConfig
108 * Method: getUpper0
109 * Signature: ()I
110 */
111JNIEXPORT jint JNICALL Java_sun_net_PortConfig_getUpper0
112 (JNIEnv *env, jclass clazz)
113{
114 struct portrange range;
115 if (getPortRange(&range) < 0) {
116 return -1;
117 }
118 return range.higher;
119}
120
121#ifdef __cplusplus
122}
123#endif
124