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
42
43 struct portrange {
44 int lower;
45 int higher;
46 };
47
48 static 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
|
1 /*
2 * Copyright (c) 2013, 2020, 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
42
43 struct portrange {
44 int lower;
45 int higher;
46 };
47
48 static 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 #elif defined(_ALLBSD_SOURCE)
64 {
65 int ret;
66 size_t size = sizeof(range->lower);
67 ret = sysctlbyname(
68 "net.inet.ip.portrange.first", &range->lower, &size, 0, 0
69 );
70 if (ret == -1) {
71 return -1;
72 }
73 size = sizeof(range->higher);
74 ret = sysctlbyname(
75 "net.inet.ip.portrange.last", &range->higher, &size, 0, 0
76 );
77 return ret;
78 }
79 #else
80 return -1;
81 #endif
|