author | amenkov |
Thu, 31 Oct 2019 14:23:06 -0700 | |
changeset 58876 | 1a8d65e71a66 |
parent 58433 | a7c95e2f8814 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54884 | 2 |
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
#include <stdio.h> |
|
26 |
#include <string.h> |
|
27 |
#include <errno.h> |
|
28 |
#include <stdlib.h> |
|
29 |
#include <ctype.h> |
|
30 |
||
49440 | 31 |
#include "jni.h" |
2 | 32 |
#include "jdwpTransport.h" |
33 |
#include "sysSocket.h" |
|
34 |
||
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
35 |
#ifdef _WIN32 |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
36 |
#include <winsock2.h> |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
37 |
#include <ws2tcpip.h> |
58876 | 38 |
#include <iphlpapi.h> |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
39 |
#else |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
40 |
#include <arpa/inet.h> |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
41 |
#include <sys/socket.h> |
58876 | 42 |
#include <net/if.h> |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
43 |
#endif |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
44 |
|
2 | 45 |
/* |
46 |
* The Socket Transport Library. |
|
47 |
* |
|
48 |
* This module is an implementation of the Java Debug Wire Protocol Transport |
|
49 |
* Service Provider Interface - see src/share/javavm/export/jdwpTransport.h. |
|
50 |
*/ |
|
51 |
||
54884 | 52 |
static int serverSocketFD = -1; |
2 | 53 |
static int socketFD = -1; |
54 |
static jdwpTransportCallback *callback; |
|
55 |
static JavaVM *jvm; |
|
56 |
static int tlsIndex; |
|
57 |
static jboolean initialized; |
|
58 |
static struct jdwpTransportNativeInterface_ interface; |
|
59 |
static jdwpTransportEnv single_env = (jdwpTransportEnv)&interface; |
|
60 |
||
61 |
#define RETURN_ERROR(err, msg) \ |
|
62 |
if (1==1) { \ |
|
63 |
setLastError(err, msg); \ |
|
64 |
return err; \ |
|
65 |
} |
|
66 |
||
67 |
#define RETURN_IO_ERROR(msg) RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, msg); |
|
68 |
||
69 |
#define RETURN_RECV_ERROR(n) \ |
|
70 |
if (n == 0) { \ |
|
71 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, "premature EOF"); \ |
|
72 |
} else { \ |
|
73 |
RETURN_IO_ERROR("recv error"); \ |
|
74 |
} |
|
75 |
||
76 |
#define MAX_DATA_SIZE 1000 |
|
77 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
78 |
static jint recv_fully(int, char *, int); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
79 |
static jint send_fully(int, char *, int); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
80 |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
81 |
/* version >= JDWPTRANSPORT_VERSION_1_1 */ |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
82 |
typedef struct { |
54884 | 83 |
/* subnet and mask are stored as IPv6 addresses, IPv4 is stored as mapped IPv6 */ |
84 |
struct in6_addr subnet; |
|
85 |
struct in6_addr netmask; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
86 |
} AllowedPeerInfo; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
87 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
88 |
#define STR(x) #x |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
89 |
#define MAX_PEER_ENTRIES 32 |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
90 |
#define MAX_PEERS_STR STR(MAX_PEER_ENTRIES) |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
91 |
static AllowedPeerInfo _peers[MAX_PEER_ENTRIES]; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
92 |
static int _peers_cnt = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
93 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
94 |
|
54884 | 95 |
static int allowOnlyIPv4 = 0; // reflects "java.net.preferIPv4Stack" sys. property |
96 |
static int preferredAddressFamily = AF_INET; // "java.net.preferIPv6Addresses" |
|
97 |
||
2 | 98 |
/* |
99 |
* Record the last error for this thread. |
|
100 |
*/ |
|
101 |
static void |
|
102 |
setLastError(jdwpTransportError err, char *newmsg) { |
|
103 |
char buf[255]; |
|
104 |
char *msg; |
|
105 |
||
106 |
/* get any I/O first in case any system calls override errno */ |
|
107 |
if (err == JDWPTRANSPORT_ERROR_IO_ERROR) { |
|
108 |
dbgsysGetLastIOError(buf, sizeof(buf)); |
|
109 |
} |
|
110 |
||
111 |
msg = (char *)dbgsysTlsGet(tlsIndex); |
|
112 |
if (msg != NULL) { |
|
113 |
(*callback->free)(msg); |
|
114 |
} |
|
115 |
||
116 |
if (err == JDWPTRANSPORT_ERROR_IO_ERROR) { |
|
117 |
char *join_str = ": "; |
|
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
118 |
int msg_len = (int)strlen(newmsg) + (int)strlen(join_str) + |
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
119 |
(int)strlen(buf) + 3; |
2 | 120 |
msg = (*callback->alloc)(msg_len); |
121 |
if (msg != NULL) { |
|
122 |
strcpy(msg, newmsg); |
|
123 |
strcat(msg, join_str); |
|
124 |
strcat(msg, buf); |
|
125 |
} |
|
126 |
} else { |
|
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
127 |
msg = (*callback->alloc)((int)strlen(newmsg)+1); |
2 | 128 |
if (msg != NULL) { |
129 |
strcpy(msg, newmsg); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
dbgsysTlsPut(tlsIndex, msg); |
|
134 |
} |
|
135 |
||
136 |
/* |
|
137 |
* Return the last error for this thread (may be NULL) |
|
138 |
*/ |
|
139 |
static char* |
|
140 |
getLastError() { |
|
141 |
return (char *)dbgsysTlsGet(tlsIndex); |
|
142 |
} |
|
143 |
||
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
144 |
/* Set options common to client and server sides */ |
2 | 145 |
static jdwpTransportError |
54884 | 146 |
setOptionsCommon(int domain, int fd) |
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
147 |
{ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
148 |
jvalue dontcare; |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
149 |
int err; |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
150 |
|
54884 | 151 |
if (domain == AF_INET6) { |
152 |
int off = 0; |
|
153 |
// make the socket a dual mode socket |
|
154 |
// this may fail if IPv4 is not supported - it's ok |
|
155 |
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off)); |
|
156 |
} |
|
157 |
||
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
158 |
dontcare.i = 0; /* keep compiler happy */ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
159 |
err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare); |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
160 |
if (err < 0) { |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
161 |
RETURN_IO_ERROR("setsockopt TCPNODELAY failed"); |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
162 |
} |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
163 |
|
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
164 |
return JDWPTRANSPORT_ERROR_NONE; |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
165 |
} |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
166 |
|
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
167 |
/* Set the SO_REUSEADDR option */ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
168 |
static jdwpTransportError |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
169 |
setReuseAddrOption(int fd) |
2 | 170 |
{ |
171 |
jvalue dontcare; |
|
172 |
int err; |
|
173 |
||
174 |
dontcare.i = 0; /* keep compiler happy */ |
|
175 |
||
176 |
err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare); |
|
177 |
if (err < 0) { |
|
178 |
RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed"); |
|
179 |
} |
|
180 |
||
181 |
return JDWPTRANSPORT_ERROR_NONE; |
|
182 |
} |
|
183 |
||
184 |
static jdwpTransportError |
|
185 |
handshake(int fd, jlong timeout) { |
|
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
186 |
const char *hello = "JDWP-Handshake"; |
2 | 187 |
char b[16]; |
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
188 |
int rv, helloLen, received; |
2 | 189 |
|
190 |
if (timeout > 0) { |
|
191 |
dbgsysConfigureBlocking(fd, JNI_FALSE); |
|
192 |
} |
|
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
193 |
helloLen = (int)strlen(hello); |
2 | 194 |
received = 0; |
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
195 |
while (received < helloLen) { |
2 | 196 |
int n; |
197 |
char *buf; |
|
198 |
if (timeout > 0) { |
|
199 |
rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout); |
|
200 |
if (rv <= 0) { |
|
201 |
setLastError(0, "timeout during handshake"); |
|
202 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
203 |
} |
|
204 |
} |
|
205 |
buf = b; |
|
206 |
buf += received; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
207 |
n = recv_fully(fd, buf, helloLen-received); |
2 | 208 |
if (n == 0) { |
209 |
setLastError(0, "handshake failed - connection prematurally closed"); |
|
210 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
211 |
} |
|
212 |
if (n < 0) { |
|
213 |
RETURN_IO_ERROR("recv failed during handshake"); |
|
214 |
} |
|
215 |
received += n; |
|
216 |
} |
|
217 |
if (timeout > 0) { |
|
218 |
dbgsysConfigureBlocking(fd, JNI_TRUE); |
|
219 |
} |
|
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
220 |
if (strncmp(b, hello, received) != 0) { |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
221 |
char msg[80+2*16]; |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
222 |
b[received] = '\0'; |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
223 |
/* |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
224 |
* We should really use snprintf here but it's not available on Windows. |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
225 |
* We can't use jio_snprintf without linking the transport against the VM. |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
226 |
*/ |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
227 |
sprintf(msg, "handshake failed - received >%s< - expected >%s<", b, hello); |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
228 |
setLastError(0, msg); |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
229 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
2 | 230 |
} |
231 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
232 |
if (send_fully(fd, (char*)hello, helloLen) != helloLen) { |
2 | 233 |
RETURN_IO_ERROR("send failed during handshake"); |
234 |
} |
|
235 |
return JDWPTRANSPORT_ERROR_NONE; |
|
236 |
} |
|
237 |
||
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
238 |
static int |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
239 |
getPortNumber(const char *s_port) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
240 |
u_long n; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
241 |
char *eptr; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
242 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
243 |
if (*s_port == 0) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
244 |
// bad address - colon with no port number in parameters |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
245 |
return -1; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
246 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
247 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
248 |
n = strtoul(s_port, &eptr, 10); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
249 |
if (eptr != s_port + strlen(s_port)) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
250 |
// incomplete conversion - port number contains non-digit |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
251 |
return -1; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
252 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
253 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
254 |
if (n > (u_short) -1) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
255 |
// check that value supplied by user is less than |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
256 |
// maximum possible u_short value (65535) and |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
257 |
// will not be truncated later. |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
258 |
return -1; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
259 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
260 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
261 |
return n; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
262 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
263 |
|
54884 | 264 |
static unsigned short getPort(struct sockaddr *sa) |
265 |
{ |
|
266 |
return dbgsysNetworkToHostShort(sa->sa_family == AF_INET |
|
267 |
? (((struct sockaddr_in*)sa)->sin_port) |
|
268 |
: (((struct sockaddr_in6*)sa)->sin6_port)); |
|
269 |
} |
|
270 |
||
271 |
/* |
|
58876 | 272 |
* Parses scope id. |
273 |
* Scope id is ulong on Windows, uint32 on unix, so returns long which can be cast to uint32. |
|
274 |
* On error sets last error and returns -1. |
|
275 |
*/ |
|
276 |
static long parseScopeId(const char *str) { |
|
277 |
// try to handle scope as interface name |
|
278 |
unsigned long scopeId = if_nametoindex(str); |
|
279 |
if (scopeId == 0) { |
|
280 |
// try to parse integer value |
|
281 |
char *end; |
|
282 |
scopeId = strtoul(str, &end, 10); |
|
283 |
if (*end != '\0') { |
|
284 |
setLastError(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "failed to parse scope"); |
|
285 |
return -1; |
|
286 |
} |
|
287 |
} |
|
288 |
// ensure parsed value is in uint32 range |
|
289 |
if (scopeId > 0xFFFFFFFF) { |
|
290 |
setLastError(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "scope is out of range"); |
|
291 |
return -1; |
|
292 |
} |
|
293 |
return (long)scopeId; |
|
294 |
} |
|
295 |
||
296 |
/* |
|
297 |
* Wrapper for dbgsysGetAddrInfo (getaddrinfo). |
|
298 |
* Handles enclosing square brackets and scopes. |
|
299 |
*/ |
|
300 |
static jdwpTransportError |
|
301 |
getAddrInfo(const char *hostname, size_t hostnameLen, |
|
302 |
const char *service, |
|
303 |
const struct addrinfo *hints, |
|
304 |
struct addrinfo **result) |
|
305 |
{ |
|
306 |
int err = 0; |
|
307 |
char *buffer = NULL; |
|
308 |
long scopeId = 0; |
|
309 |
||
310 |
if (hostname != NULL) { |
|
311 |
char *scope = NULL; |
|
312 |
// skip surrounding |
|
313 |
if (hostnameLen > 2 && hostname[0] == '[' && hostname[hostnameLen - 1] == ']') { |
|
314 |
hostname++; |
|
315 |
hostnameLen -= 2; |
|
316 |
} |
|
317 |
buffer = (*callback->alloc)((int)hostnameLen + 1); |
|
318 |
if (buffer == NULL) { |
|
319 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory"); |
|
320 |
} |
|
321 |
memcpy(buffer, hostname, hostnameLen); |
|
322 |
buffer[hostnameLen] = '\0'; |
|
323 |
||
324 |
scope = strchr(buffer, '%'); |
|
325 |
if (scope != NULL) { |
|
326 |
// drop scope from the address |
|
327 |
*scope = '\0'; |
|
328 |
// and parse the value |
|
329 |
scopeId = parseScopeId(scope + 1); |
|
330 |
if (scopeId < 0) { |
|
331 |
(*callback->free)(buffer); |
|
332 |
return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT; |
|
333 |
} |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
err = dbgsysGetAddrInfo(buffer, service, hints, result); |
|
338 |
||
339 |
if (buffer != NULL) { |
|
340 |
(*callback->free)(buffer); |
|
341 |
} |
|
342 |
if (err != 0) { |
|
343 |
setLastError(err, "getaddrinfo: failed to parse address"); |
|
344 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
345 |
} |
|
346 |
||
347 |
if (scopeId > 0) { |
|
348 |
if ((*result)->ai_family != AF_INET6) { |
|
349 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "IPv4 address cannot contain scope"); |
|
350 |
} |
|
351 |
||
352 |
((struct sockaddr_in6 *)((*result)->ai_addr))->sin6_scope_id = (uint32_t)scopeId; |
|
353 |
} |
|
354 |
||
355 |
return JDWPTRANSPORT_ERROR_NONE; |
|
356 |
} |
|
357 |
||
358 |
/* |
|
54884 | 359 |
* Result must be released with dbgsysFreeAddrInfo. |
360 |
*/ |
|
2 | 361 |
static jdwpTransportError |
54884 | 362 |
parseAddress(const char *address, struct addrinfo **result) { |
363 |
const char *colon; |
|
58876 | 364 |
size_t hostnameLen; |
54884 | 365 |
const char *port; |
366 |
struct addrinfo hints; |
|
2 | 367 |
|
54884 | 368 |
*result = NULL; |
2 | 369 |
|
370 |
/* check for host:port or port */ |
|
54884 | 371 |
colon = strrchr(address, ':'); |
372 |
port = (colon == NULL ? address : colon + 1); |
|
373 |
||
374 |
/* ensure the port is valid (getaddrinfo allows port to be empty) */ |
|
375 |
if (getPortNumber(port) < 0) { |
|
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
376 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid port number specified"); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
377 |
} |
54884 | 378 |
|
379 |
memset (&hints, 0, sizeof(hints)); |
|
380 |
hints.ai_family = allowOnlyIPv4 ? AF_INET : AF_UNSPEC; |
|
381 |
hints.ai_socktype = SOCK_STREAM; |
|
382 |
hints.ai_protocol = IPPROTO_TCP; |
|
383 |
hints.ai_flags = AI_NUMERICSERV; // port must be a number |
|
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
384 |
|
58876 | 385 |
hostnameLen = (colon == NULL ? 0 : colon - address); |
386 |
if (hostnameLen == 0) { |
|
54884 | 387 |
/* no hostname - use localhost address (pass NULL to getaddrinfo) */ |
58876 | 388 |
address = NULL; |
389 |
} else if (*address == '*' && hostnameLen == 1) { |
|
54884 | 390 |
/* *:port - listen on all interfaces |
391 |
* use IPv6 socket (to accept IPv6 and mapped IPv4), |
|
392 |
* pass hostname == NULL to getaddrinfo. |
|
393 |
*/ |
|
394 |
hints.ai_family = allowOnlyIPv4 ? AF_INET : AF_INET6; |
|
395 |
hints.ai_flags |= AI_PASSIVE | (allowOnlyIPv4 ? 0 : AI_V4MAPPED | AI_ALL); |
|
58876 | 396 |
address = NULL; |
54884 | 397 |
} |
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
398 |
|
58876 | 399 |
return getAddrInfo(address, hostnameLen, port, &hints, result); |
2 | 400 |
} |
401 |
||
54884 | 402 |
/* |
403 |
* Input is sockaddr just because all clients have it. |
|
404 |
*/ |
|
405 |
static void convertIPv4ToIPv6(const struct sockaddr *addr4, struct in6_addr *addr6) { |
|
406 |
// Implement in a platform-independent way. |
|
407 |
// Spec requires in_addr has s_addr member, in6_addr has s6_addr[16] member. |
|
408 |
struct in_addr *a4 = &(((struct sockaddr_in*)addr4)->sin_addr); |
|
409 |
memset(addr6, 0, sizeof(*addr6)); // for safety |
|
410 |
||
411 |
// Mapped address contains 80 zero bits, then 16 "1" bits, then IPv4 address (4 bytes). |
|
412 |
addr6->s6_addr[10] = addr6->s6_addr[11] = 0xFF; |
|
413 |
memcpy(&(addr6->s6_addr[12]), &(a4->s_addr), 4); |
|
414 |
} |
|
415 |
||
416 |
/* |
|
417 |
* Parses address (IPv4 or IPv6), fills in result by parsed address. |
|
418 |
* For IPv4 mapped IPv6 is returned in result, isIPv4 is set. |
|
419 |
*/ |
|
420 |
static jdwpTransportError |
|
421 |
parseAllowedAddr(const char *buffer, struct in6_addr *result, int *isIPv4) { |
|
422 |
struct addrinfo hints; |
|
423 |
struct addrinfo *addrInfo = NULL; |
|
58876 | 424 |
jdwpTransportError err; |
54884 | 425 |
|
426 |
/* |
|
427 |
* To parse both IPv4 and IPv6 need to specify AF_UNSPEC family |
|
428 |
* (with AF_INET6 IPv4 addresses are not parsed even with AI_V4MAPPED and AI_ALL flags). |
|
429 |
*/ |
|
430 |
memset (&hints, 0, sizeof(hints)); |
|
431 |
hints.ai_family = AF_UNSPEC; // IPv6 or mapped IPv4 |
|
432 |
hints.ai_socktype = SOCK_STREAM; |
|
433 |
hints.ai_protocol = IPPROTO_TCP; |
|
434 |
hints.ai_flags = AI_NUMERICHOST; // only numeric addresses, no resolution |
|
435 |
||
58876 | 436 |
err = getAddrInfo(buffer, strlen(buffer), NULL, &hints, &addrInfo); |
54884 | 437 |
|
58876 | 438 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
439 |
return err; |
|
54884 | 440 |
} |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
441 |
|
54884 | 442 |
if (addrInfo->ai_family == AF_INET6) { |
443 |
memcpy(result, &(((struct sockaddr_in6 *)(addrInfo->ai_addr))->sin6_addr), sizeof(*result)); |
|
444 |
*isIPv4 = 0; |
|
445 |
} else { // IPv4 address - convert to mapped IPv6 |
|
446 |
struct in6_addr addr6; |
|
447 |
convertIPv4ToIPv6(addrInfo->ai_addr, &addr6); |
|
448 |
memcpy(result, &addr6, sizeof(*result)); |
|
449 |
*isIPv4 = 1; |
|
450 |
} |
|
451 |
||
452 |
dbgsysFreeAddrInfo(addrInfo); |
|
453 |
||
454 |
return JDWPTRANSPORT_ERROR_NONE; |
|
455 |
} |
|
456 |
||
457 |
/* |
|
458 |
* Parses prefix length from buffer (integer value), fills in result with corresponding net mask. |
|
459 |
* For IPv4 (isIPv4 is set), maximum prefix length is 32 bit, for IPv6 - 128 bit. |
|
460 |
*/ |
|
461 |
static jdwpTransportError |
|
462 |
parseAllowedMask(const char *buffer, int isIPv4, struct in6_addr *result) { |
|
463 |
int prefixLen = 0; |
|
464 |
int maxValue = isIPv4 ? 32 : 128; |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
465 |
int i; |
54884 | 466 |
|
467 |
do { |
|
468 |
if (*buffer < '0' || *buffer > '9') { |
|
469 |
return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
470 |
} |
54884 | 471 |
prefixLen = prefixLen * 10 + (*buffer - '0'); |
472 |
if (prefixLen > maxValue) { // avoid overflow |
|
473 |
return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
474 |
} |
54884 | 475 |
buffer++; |
476 |
} while (*buffer != '\0'); |
|
477 |
||
478 |
if (isIPv4) { |
|
479 |
// IPv4 are stored as mapped IPv6, prefixLen needs to be converted too |
|
480 |
prefixLen += 96; |
|
481 |
} |
|
482 |
||
483 |
if (prefixLen == 0) { |
|
484 |
return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
485 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
486 |
|
54884 | 487 |
// generate mask for prefix length |
488 |
memset(result, 0, sizeof(*result)); |
|
489 |
||
490 |
// prefixLen <= 128, so we won't go over result's size |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
491 |
for (i = 0; prefixLen > 0; i++, prefixLen -= 8) { |
54884 | 492 |
if (prefixLen >= 8) { |
493 |
// set the whole byte |
|
494 |
result->s6_addr[i] = 0xFF; |
|
495 |
} else { |
|
496 |
// set only "prefixLen" bits |
|
497 |
result->s6_addr[i] = (char)(0xFF << (8 - prefixLen)); |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
return JDWPTRANSPORT_ERROR_NONE; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
502 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
503 |
|
54884 | 504 |
/* |
505 |
* Internal implementation of parseAllowedPeers (requires writable buffer). |
|
506 |
*/ |
|
507 |
static jdwpTransportError |
|
508 |
parseAllowedPeersInternal(char *buffer) { |
|
509 |
char *next; |
|
510 |
int isIPv4 = 0; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
511 |
|
54884 | 512 |
do { |
513 |
char *mask = NULL; |
|
514 |
char *endOfAddr = strpbrk(buffer, "/+"); |
|
515 |
if (endOfAddr == NULL) { |
|
516 |
// this is the last address and there is no prefix length |
|
517 |
next = NULL; |
|
518 |
} else { |
|
519 |
next = endOfAddr + 1; |
|
520 |
if (*endOfAddr == '/') { |
|
521 |
// mask (prefix length) presents |
|
522 |
char *endOfMask = strchr(next, '+'); |
|
523 |
mask = next; |
|
524 |
if (endOfMask == NULL) { |
|
525 |
// no more addresses |
|
526 |
next = NULL; |
|
527 |
} else { |
|
528 |
next = endOfMask + 1; |
|
529 |
*endOfMask = '\0'; |
|
530 |
} |
|
531 |
} |
|
532 |
*endOfAddr = '\0'; |
|
533 |
} |
|
534 |
||
535 |
// parse subnet address (IPv4 is stored as mapped IPv6) |
|
536 |
if (parseAllowedAddr(buffer, &(_peers[_peers_cnt].subnet), &isIPv4) != JDWPTRANSPORT_ERROR_NONE) { |
|
537 |
_peers_cnt = 0; |
|
538 |
fprintf(stderr, "Error in allow option: '%s'\n", buffer); |
|
539 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, |
|
540 |
"invalid IP address in allow option"); |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
541 |
} |
54884 | 542 |
if (mask != NULL) { |
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
543 |
size_t i; |
54884 | 544 |
if (parseAllowedMask(mask, isIPv4, &(_peers[_peers_cnt].netmask)) != JDWPTRANSPORT_ERROR_NONE) { |
545 |
_peers_cnt = 0; |
|
546 |
fprintf(stderr, "Error in allow option: '%s'\n", mask); |
|
547 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, |
|
548 |
"invalid netmask in allow option"); |
|
549 |
} |
|
550 |
// for safety update subnet to satisfy the mask |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
551 |
for (i = 0; i < sizeof(_peers[_peers_cnt].subnet); i++) { |
54884 | 552 |
_peers[_peers_cnt].subnet.s6_addr[i] &= _peers[_peers_cnt].netmask.s6_addr[i]; |
553 |
} |
|
554 |
} else { |
|
555 |
memset(&(_peers[_peers_cnt].netmask), 0xFF, sizeof(_peers[_peers_cnt].netmask)); |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
556 |
} |
54884 | 557 |
_peers_cnt++; |
558 |
buffer = next; |
|
559 |
} while (next != NULL); |
|
560 |
||
561 |
return JDWPTRANSPORT_ERROR_NONE; |
|
562 |
} |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
563 |
|
54884 | 564 |
/* |
565 |
* Parses 'allow' argument (fills in list of allowed peers (global _peers variable)). |
|
566 |
* 'Allow' value consists of tokens separated by '+', |
|
567 |
* each token contains IP address (IPv4 or IPv6) and optional prefixLength: |
|
568 |
* '<addr>[/<prefixLength>]'. |
|
569 |
* Example: '192.168.1.10+192.168.0.0/24' |
|
570 |
* - connections are allowed from 192.168.1.10 and subnet 192.168.0.XX. |
|
571 |
*/ |
|
572 |
static jdwpTransportError |
|
573 |
parseAllowedPeers(const char *allowed_peers, size_t len) { |
|
574 |
// Build a list of allowed peers from char string |
|
575 |
// of format 192.168.0.10+192.168.0.0/24 |
|
576 |
||
577 |
// writable copy of the value |
|
578 |
char *buffer = (*callback->alloc)((int)len + 1); |
|
579 |
if (buffer == NULL) { |
|
580 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory"); |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
581 |
} |
58433
a7c95e2f8814
8231600: Avoid GCC 8 strncpy error in jdk.jdwp.agent
fweimer
parents:
54925
diff
changeset
|
582 |
memcpy(buffer, allowed_peers, len); |
54884 | 583 |
buffer[len] = '\0'; |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
584 |
|
54884 | 585 |
jdwpTransportError err = parseAllowedPeersInternal(buffer); |
586 |
||
587 |
(*callback->free)(buffer); |
|
588 |
||
589 |
return err; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
590 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
591 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
592 |
static int |
54884 | 593 |
isAddressInSubnet(const struct in6_addr *address, const struct in6_addr *subnet, const struct in6_addr *mask) { |
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
594 |
size_t i; |
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
595 |
for (i = 0; i < sizeof(struct in6_addr); i++) { |
54884 | 596 |
if ((address->s6_addr[i] & mask->s6_addr[i]) != subnet->s6_addr[i]) { |
597 |
return 0; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
598 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
599 |
} |
54884 | 600 |
return 1; |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
601 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
602 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
603 |
static int |
54884 | 604 |
isPeerAllowed(struct sockaddr_storage *peer) { |
605 |
struct in6_addr tmp; |
|
606 |
struct in6_addr *addr6; |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
607 |
int i; |
54884 | 608 |
// _peers contains IPv6 subnet and mask (IPv4 is converted to mapped IPv6) |
609 |
if (peer->ss_family == AF_INET) { |
|
610 |
convertIPv4ToIPv6((struct sockaddr *)peer, &tmp); |
|
611 |
addr6 = &tmp; |
|
612 |
} else { |
|
613 |
addr6 = &(((struct sockaddr_in6 *)peer)->sin6_addr); |
|
614 |
} |
|
615 |
||
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
616 |
for (i = 0; i < _peers_cnt; ++i) { |
54884 | 617 |
if (isAddressInSubnet(addr6, &(_peers[i].subnet), &(_peers[i].netmask))) { |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
618 |
return 1; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
619 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
620 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
621 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
622 |
return 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
623 |
} |
2 | 624 |
|
625 |
static jdwpTransportError JNICALL |
|
626 |
socketTransport_getCapabilities(jdwpTransportEnv* env, |
|
627 |
JDWPTransportCapabilities* capabilitiesPtr) |
|
628 |
{ |
|
629 |
JDWPTransportCapabilities result; |
|
630 |
||
631 |
memset(&result, 0, sizeof(result)); |
|
632 |
result.can_timeout_attach = JNI_TRUE; |
|
633 |
result.can_timeout_accept = JNI_TRUE; |
|
634 |
result.can_timeout_handshake = JNI_TRUE; |
|
635 |
||
636 |
*capabilitiesPtr = result; |
|
637 |
||
638 |
return JDWPTRANSPORT_ERROR_NONE; |
|
639 |
} |
|
640 |
||
54884 | 641 |
/* |
642 |
* Starts listening on the specified addrinfo, |
|
643 |
* returns listening socket and actual listening port. |
|
644 |
* If the function fails and returned socket != -1, the socket should be closed. |
|
645 |
*/ |
|
646 |
static jdwpTransportError startListening(struct addrinfo *ai, int *socket, char** actualAddress) |
|
2 | 647 |
{ |
648 |
int err; |
|
649 |
||
54884 | 650 |
*socket = dbgsysSocket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP); |
651 |
if (*socket < 0) { |
|
652 |
RETURN_IO_ERROR("socket creation failed"); |
|
2 | 653 |
} |
654 |
||
54884 | 655 |
err = setOptionsCommon(ai->ai_family, *socket); |
656 |
if (err) { |
|
2 | 657 |
return err; |
658 |
} |
|
659 |
||
54884 | 660 |
if (getPort(ai->ai_addr) != 0) { |
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
661 |
/* |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
662 |
* Only need SO_REUSEADDR if we're using a fixed port. If we |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
663 |
* start seeing EADDRINUSE due to collisions in free ports |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
664 |
* then we should retry the dbgsysBind() a few times. |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
665 |
*/ |
54884 | 666 |
err = setReuseAddrOption(*socket); |
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
667 |
if (err) { |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
668 |
return err; |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
669 |
} |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
670 |
} |
2 | 671 |
|
54884 | 672 |
err = dbgsysBind(*socket, ai->ai_addr, (socklen_t)ai->ai_addrlen); |
2 | 673 |
if (err < 0) { |
674 |
RETURN_IO_ERROR("bind failed"); |
|
675 |
} |
|
676 |
||
54884 | 677 |
err = dbgsysListen(*socket, 1); // only 1 debugger can attach |
2 | 678 |
if (err < 0) { |
679 |
RETURN_IO_ERROR("listen failed"); |
|
680 |
} |
|
681 |
||
682 |
{ |
|
683 |
char buf[20]; |
|
54884 | 684 |
struct sockaddr_storage addr; |
685 |
socklen_t len = sizeof(addr); |
|
2 | 686 |
jint portNum; |
54884 | 687 |
err = dbgsysGetSocketName(*socket, (struct sockaddr *)&addr, &len); |
688 |
if (err != 0) { |
|
689 |
RETURN_IO_ERROR("getsockname failed"); |
|
690 |
} |
|
691 |
||
692 |
portNum = getPort((struct sockaddr *)&addr); |
|
2 | 693 |
sprintf(buf, "%d", portNum); |
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
694 |
*actualAddress = (*callback->alloc)((int)strlen(buf) + 1); |
2 | 695 |
if (*actualAddress == NULL) { |
696 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory"); |
|
697 |
} else { |
|
698 |
strcpy(*actualAddress, buf); |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
return JDWPTRANSPORT_ERROR_NONE; |
|
703 |
} |
|
704 |
||
705 |
static jdwpTransportError JNICALL |
|
54884 | 706 |
socketTransport_startListening(jdwpTransportEnv* env, const char* address, |
707 |
char** actualAddress) |
|
708 |
{ |
|
709 |
int err; |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
710 |
int pass; |
54884 | 711 |
struct addrinfo *addrInfo = NULL; |
712 |
struct addrinfo *listenAddr = NULL; |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
713 |
struct addrinfo *ai = NULL; |
54884 | 714 |
|
715 |
/* no address provided */ |
|
716 |
if ((address == NULL) || (address[0] == '\0')) { |
|
717 |
address = "0"; |
|
718 |
} |
|
719 |
||
720 |
err = parseAddress(address, &addrInfo); |
|
721 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
|
722 |
return err; |
|
723 |
} |
|
724 |
||
725 |
/* 1st pass - preferredAddressFamily (by default IPv4), 2nd pass - the rest */ |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
726 |
for (pass = 0; pass < 2 && listenAddr == NULL; pass++) { |
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
727 |
for (ai = addrInfo; ai != NULL; ai = ai->ai_next) { |
54884 | 728 |
if ((pass == 0 && ai->ai_family == preferredAddressFamily) || |
729 |
(pass == 1 && ai->ai_family != preferredAddressFamily)) |
|
730 |
{ |
|
731 |
listenAddr = ai; |
|
732 |
break; |
|
733 |
} |
|
734 |
} |
|
735 |
} |
|
736 |
||
737 |
if (listenAddr == NULL) { |
|
738 |
dbgsysFreeAddrInfo(addrInfo); |
|
739 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_INTERNAL, "listen failed: wrong address"); |
|
740 |
} |
|
741 |
||
742 |
err = startListening(listenAddr, &serverSocketFD, actualAddress); |
|
743 |
||
744 |
dbgsysFreeAddrInfo(addrInfo); |
|
745 |
||
746 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
|
747 |
if (serverSocketFD >= 0) { |
|
748 |
dbgsysSocketClose(serverSocketFD); |
|
749 |
serverSocketFD = -1; |
|
750 |
} |
|
751 |
return err; |
|
752 |
} |
|
753 |
||
754 |
return JDWPTRANSPORT_ERROR_NONE; |
|
755 |
} |
|
756 |
||
757 |
static jdwpTransportError JNICALL |
|
2 | 758 |
socketTransport_accept(jdwpTransportEnv* env, jlong acceptTimeout, jlong handshakeTimeout) |
759 |
{ |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
760 |
int err = JDWPTRANSPORT_ERROR_NONE; |
54884 | 761 |
struct sockaddr_storage clientAddr; |
762 |
socklen_t clientAddrLen; |
|
763 |
jlong startTime = 0; |
|
2 | 764 |
|
765 |
/* |
|
766 |
* Use a default handshake timeout if not specified - this avoids an indefinite |
|
767 |
* hang in cases where something other than a debugger connects to our port. |
|
768 |
*/ |
|
769 |
if (handshakeTimeout == 0) { |
|
770 |
handshakeTimeout = 2000; |
|
771 |
} |
|
772 |
||
773 |
do { |
|
774 |
/* |
|
775 |
* If there is an accept timeout then we put the socket in non-blocking |
|
776 |
* mode and poll for a connection. |
|
777 |
*/ |
|
778 |
if (acceptTimeout > 0) { |
|
779 |
int rv; |
|
780 |
dbgsysConfigureBlocking(serverSocketFD, JNI_FALSE); |
|
781 |
startTime = dbgsysCurrentTimeMillis(); |
|
782 |
rv = dbgsysPoll(serverSocketFD, JNI_TRUE, JNI_FALSE, (long)acceptTimeout); |
|
783 |
if (rv <= 0) { |
|
784 |
/* set the last error here as could be overridden by configureBlocking */ |
|
785 |
if (rv == 0) { |
|
786 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "poll failed"); |
|
787 |
} |
|
788 |
/* restore blocking state */ |
|
789 |
dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE); |
|
790 |
if (rv == 0) { |
|
791 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "timed out waiting for connection"); |
|
792 |
} else { |
|
793 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
794 |
} |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
/* |
|
799 |
* Accept the connection |
|
800 |
*/ |
|
54884 | 801 |
clientAddrLen = sizeof(clientAddr); |
2 | 802 |
socketFD = dbgsysAccept(serverSocketFD, |
54884 | 803 |
(struct sockaddr *)&clientAddr, |
804 |
&clientAddrLen); |
|
2 | 805 |
/* set the last error here as could be overridden by configureBlocking */ |
806 |
if (socketFD < 0) { |
|
807 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "accept failed"); |
|
808 |
} |
|
809 |
/* |
|
810 |
* Restore the blocking state - note that the accepted socket may be in |
|
811 |
* blocking or non-blocking mode (platform dependent). However as there |
|
812 |
* is a handshake timeout set then it will go into non-blocking mode |
|
813 |
* anyway for the handshake. |
|
814 |
*/ |
|
815 |
if (acceptTimeout > 0) { |
|
816 |
dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE); |
|
817 |
} |
|
818 |
if (socketFD < 0) { |
|
819 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
820 |
} |
|
821 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
822 |
/* |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
823 |
* version >= JDWPTRANSPORT_VERSION_1_1: |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
824 |
* Verify that peer is allowed to connect. |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
825 |
*/ |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
826 |
if (_peers_cnt > 0) { |
54884 | 827 |
if (!isPeerAllowed(&clientAddr)) { |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
828 |
char ebuf[64] = { 0 }; |
54884 | 829 |
char addrStr[INET_ADDRSTRLEN] = { 0 }; |
830 |
int err2 = getnameinfo((struct sockaddr *)&clientAddr, clientAddrLen, |
|
831 |
addrStr, sizeof(addrStr), NULL, 0, |
|
832 |
NI_NUMERICHOST); |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
833 |
sprintf(ebuf, "ERROR: Peer not allowed to connect: %s\n", |
54884 | 834 |
(err2 != 0) ? "<bad address>" : addrStr); |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
835 |
dbgsysSocketClose(socketFD); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
836 |
socketFD = -1; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
837 |
err = JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
838 |
setLastError(err, ebuf); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
839 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
840 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
841 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
842 |
if (socketFD > 0) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
843 |
/* handshake with the debugger */ |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
844 |
err = handshake(socketFD, handshakeTimeout); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
845 |
} |
2 | 846 |
|
847 |
/* |
|
848 |
* If the handshake fails then close the connection. If there if an accept |
|
849 |
* timeout then we must adjust the timeout for the next poll. |
|
850 |
*/ |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
851 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
2 | 852 |
fprintf(stderr, "Debugger failed to attach: %s\n", getLastError()); |
853 |
dbgsysSocketClose(socketFD); |
|
854 |
socketFD = -1; |
|
855 |
if (acceptTimeout > 0) { |
|
856 |
long endTime = dbgsysCurrentTimeMillis(); |
|
857 |
acceptTimeout -= (endTime - startTime); |
|
858 |
if (acceptTimeout <= 0) { |
|
859 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, |
|
860 |
"timeout waiting for debugger to connect"); |
|
861 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
862 |
} |
|
863 |
} |
|
864 |
} |
|
865 |
} while (socketFD < 0); |
|
866 |
||
867 |
return JDWPTRANSPORT_ERROR_NONE; |
|
868 |
} |
|
869 |
||
870 |
static jdwpTransportError JNICALL |
|
871 |
socketTransport_stopListening(jdwpTransportEnv *env) |
|
872 |
{ |
|
873 |
if (serverSocketFD < 0) { |
|
874 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "connection not open"); |
|
875 |
} |
|
876 |
if (dbgsysSocketClose(serverSocketFD) < 0) { |
|
877 |
RETURN_IO_ERROR("close failed"); |
|
878 |
} |
|
879 |
serverSocketFD = -1; |
|
880 |
return JDWPTRANSPORT_ERROR_NONE; |
|
881 |
} |
|
882 |
||
54884 | 883 |
/* |
884 |
* Tries to connect to the specified addrinfo, returns connected socket. |
|
885 |
* If the function fails and returned socket != -1, the socket should be closed. |
|
886 |
*/ |
|
887 |
static jdwpTransportError connectToAddr(struct addrinfo *ai, jlong timeout, int *socket) { |
|
2 | 888 |
int err; |
889 |
||
54884 | 890 |
*socket = dbgsysSocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
891 |
if (*socket < 0) { |
|
2 | 892 |
RETURN_IO_ERROR("unable to create socket"); |
893 |
} |
|
894 |
||
54884 | 895 |
err = setOptionsCommon(ai->ai_family, socketFD); |
2 | 896 |
if (err) { |
897 |
return err; |
|
898 |
} |
|
899 |
||
900 |
/* |
|
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
901 |
* We don't call setReuseAddrOption() for the non-server socket |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
902 |
* case. If we start seeing EADDRINUSE due to collisions in free |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
903 |
* ports then we should retry the dbgsysConnect() a few times. |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
904 |
*/ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
905 |
|
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
906 |
/* |
2 | 907 |
* To do a timed connect we make the socket non-blocking |
908 |
* and poll with a timeout; |
|
909 |
*/ |
|
54884 | 910 |
if (timeout > 0) { |
2 | 911 |
dbgsysConfigureBlocking(socketFD, JNI_FALSE); |
912 |
} |
|
913 |
||
54884 | 914 |
err = dbgsysConnect(socketFD, ai->ai_addr, (socklen_t)ai->ai_addrlen); |
58876 | 915 |
|
54884 | 916 |
if (err == DBG_EINPROGRESS && timeout > 0) { |
917 |
err = dbgsysFinishConnect(socketFD, (long)timeout); |
|
2 | 918 |
|
919 |
if (err == DBG_ETIMEOUT) { |
|
920 |
dbgsysConfigureBlocking(socketFD, JNI_TRUE); |
|
921 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "connect timed out"); |
|
922 |
} |
|
923 |
} |
|
924 |
||
54884 | 925 |
if (err) { |
2 | 926 |
RETURN_IO_ERROR("connect failed"); |
927 |
} |
|
928 |
||
54884 | 929 |
return err; |
930 |
} |
|
931 |
||
932 |
||
933 |
static jdwpTransportError JNICALL |
|
934 |
socketTransport_attach(jdwpTransportEnv* env, const char* addressString, jlong attachTimeout, |
|
935 |
jlong handshakeTimeout) |
|
936 |
{ |
|
937 |
int err; |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
938 |
int pass; |
54884 | 939 |
struct addrinfo *addrInfo = NULL; |
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
940 |
struct addrinfo *ai; |
54884 | 941 |
|
942 |
if (addressString == NULL || addressString[0] == '\0') { |
|
943 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "address is missing"); |
|
944 |
} |
|
945 |
||
946 |
err = parseAddress(addressString, &addrInfo); |
|
947 |
if (err) { |
|
948 |
return err; |
|
949 |
} |
|
950 |
||
951 |
/* 1st pass - preferredAddressFamily (by default IPv4), 2nd pass - the rest */ |
|
54925
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
952 |
for (pass = 0; pass < 2 && socketFD < 0; pass++) { |
3205f4c40716
8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents:
54884
diff
changeset
|
953 |
for (ai = addrInfo; ai != NULL; ai = ai->ai_next) { |
54884 | 954 |
if ((pass == 0 && ai->ai_family == preferredAddressFamily) || |
955 |
(pass == 1 && ai->ai_family != preferredAddressFamily)) |
|
956 |
{ |
|
957 |
err = connectToAddr(ai, attachTimeout, &socketFD); |
|
958 |
if (err == JDWPTRANSPORT_ERROR_NONE) { |
|
959 |
break; |
|
960 |
} |
|
961 |
if (socketFD >= 0) { |
|
962 |
dbgsysSocketClose(socketFD); |
|
963 |
socketFD = -1; |
|
964 |
} |
|
965 |
} |
|
966 |
} |
|
967 |
} |
|
968 |
||
969 |
freeaddrinfo(addrInfo); |
|
970 |
||
971 |
/* err from the last connectToAddr() call */ |
|
972 |
if (err != 0) { |
|
973 |
return err; |
|
974 |
} |
|
975 |
||
2 | 976 |
if (attachTimeout > 0) { |
977 |
dbgsysConfigureBlocking(socketFD, JNI_TRUE); |
|
978 |
} |
|
979 |
||
980 |
err = handshake(socketFD, handshakeTimeout); |
|
981 |
if (err) { |
|
982 |
dbgsysSocketClose(socketFD); |
|
983 |
socketFD = -1; |
|
984 |
return err; |
|
985 |
} |
|
986 |
||
987 |
return JDWPTRANSPORT_ERROR_NONE; |
|
988 |
} |
|
989 |
||
990 |
static jboolean JNICALL |
|
991 |
socketTransport_isOpen(jdwpTransportEnv* env) |
|
992 |
{ |
|
993 |
if (socketFD >= 0) { |
|
994 |
return JNI_TRUE; |
|
995 |
} else { |
|
996 |
return JNI_FALSE; |
|
997 |
} |
|
998 |
} |
|
999 |
||
1000 |
static jdwpTransportError JNICALL |
|
1001 |
socketTransport_close(jdwpTransportEnv* env) |
|
1002 |
{ |
|
1003 |
int fd = socketFD; |
|
1004 |
socketFD = -1; |
|
1005 |
if (fd < 0) { |
|
1006 |
return JDWPTRANSPORT_ERROR_NONE; |
|
1007 |
} |
|
22605
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1008 |
#ifdef _AIX |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1009 |
/* |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1010 |
AIX needs a workaround for I/O cancellation, see: |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1011 |
http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/close.htm |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1012 |
... |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1013 |
The close subroutine is blocked until all subroutines which use the file |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1014 |
descriptor return to usr space. For example, when a thread is calling close |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1015 |
and another thread is calling select with the same file descriptor, the |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1016 |
close subroutine does not return until the select call returns. |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1017 |
... |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1018 |
*/ |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1019 |
shutdown(fd, 2); |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
1020 |
#endif |
2 | 1021 |
if (dbgsysSocketClose(fd) < 0) { |
1022 |
/* |
|
1023 |
* close failed - it's pointless to restore socketFD here because |
|
15274
a77cdd5ea6e3
8005120: Compiler warnings in socket transport native code
jzavgren
parents:
14342
diff
changeset
|
1024 |
* any subsequent close will likely fail as well. |
2 | 1025 |
*/ |
1026 |
RETURN_IO_ERROR("close failed"); |
|
1027 |
} |
|
1028 |
return JDWPTRANSPORT_ERROR_NONE; |
|
1029 |
} |
|
1030 |
||
1031 |
static jdwpTransportError JNICALL |
|
1032 |
socketTransport_writePacket(jdwpTransportEnv* env, const jdwpPacket *packet) |
|
1033 |
{ |
|
1034 |
jint len, data_len, id; |
|
1035 |
/* |
|
1036 |
* room for header and up to MAX_DATA_SIZE data bytes |
|
1037 |
*/ |
|
48242 | 1038 |
char header[JDWP_HEADER_SIZE + MAX_DATA_SIZE]; |
2 | 1039 |
jbyte *data; |
1040 |
||
1041 |
/* packet can't be null */ |
|
1042 |
if (packet == NULL) { |
|
1043 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is NULL"); |
|
1044 |
} |
|
1045 |
||
1046 |
len = packet->type.cmd.len; /* includes header */ |
|
48242 | 1047 |
data_len = len - JDWP_HEADER_SIZE; |
2 | 1048 |
|
1049 |
/* bad packet */ |
|
1050 |
if (data_len < 0) { |
|
1051 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid length"); |
|
1052 |
} |
|
1053 |
||
1054 |
/* prepare the header for transmission */ |
|
1055 |
len = (jint)dbgsysHostToNetworkLong(len); |
|
1056 |
id = (jint)dbgsysHostToNetworkLong(packet->type.cmd.id); |
|
1057 |
||
1058 |
memcpy(header + 0, &len, 4); |
|
1059 |
memcpy(header + 4, &id, 4); |
|
1060 |
header[8] = packet->type.cmd.flags; |
|
1061 |
if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) { |
|
1062 |
jshort errorCode = |
|
1063 |
dbgsysHostToNetworkShort(packet->type.reply.errorCode); |
|
1064 |
memcpy(header + 9, &errorCode, 2); |
|
1065 |
} else { |
|
1066 |
header[9] = packet->type.cmd.cmdSet; |
|
1067 |
header[10] = packet->type.cmd.cmd; |
|
1068 |
} |
|
1069 |
||
1070 |
data = packet->type.cmd.data; |
|
1071 |
/* Do one send for short packets, two for longer ones */ |
|
1072 |
if (data_len <= MAX_DATA_SIZE) { |
|
48242 | 1073 |
memcpy(header + JDWP_HEADER_SIZE, data, data_len); |
1074 |
if (send_fully(socketFD, (char *)&header, JDWP_HEADER_SIZE + data_len) != |
|
1075 |
JDWP_HEADER_SIZE + data_len) { |
|
2 | 1076 |
RETURN_IO_ERROR("send failed"); |
1077 |
} |
|
1078 |
} else { |
|
48242 | 1079 |
memcpy(header + JDWP_HEADER_SIZE, data, MAX_DATA_SIZE); |
1080 |
if (send_fully(socketFD, (char *)&header, JDWP_HEADER_SIZE + MAX_DATA_SIZE) != |
|
1081 |
JDWP_HEADER_SIZE + MAX_DATA_SIZE) { |
|
2 | 1082 |
RETURN_IO_ERROR("send failed"); |
1083 |
} |
|
1084 |
/* Send the remaining data bytes right out of the data area. */ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1085 |
if (send_fully(socketFD, (char *)data + MAX_DATA_SIZE, |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1086 |
data_len - MAX_DATA_SIZE) != data_len - MAX_DATA_SIZE) { |
2 | 1087 |
RETURN_IO_ERROR("send failed"); |
1088 |
} |
|
1089 |
} |
|
1090 |
||
1091 |
return JDWPTRANSPORT_ERROR_NONE; |
|
1092 |
} |
|
1093 |
||
1094 |
static jint |
|
1095 |
recv_fully(int f, char *buf, int len) |
|
1096 |
{ |
|
1097 |
int nbytes = 0; |
|
1098 |
while (nbytes < len) { |
|
1099 |
int res = dbgsysRecv(f, buf + nbytes, len - nbytes, 0); |
|
1100 |
if (res < 0) { |
|
1101 |
return res; |
|
1102 |
} else if (res == 0) { |
|
1103 |
break; /* eof, return nbytes which is less than len */ |
|
1104 |
} |
|
1105 |
nbytes += res; |
|
1106 |
} |
|
1107 |
return nbytes; |
|
1108 |
} |
|
1109 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1110 |
jint |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1111 |
send_fully(int f, char *buf, int len) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1112 |
{ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1113 |
int nbytes = 0; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1114 |
while (nbytes < len) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1115 |
int res = dbgsysSend(f, buf + nbytes, len - nbytes, 0); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1116 |
if (res < 0) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1117 |
return res; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1118 |
} else if (res == 0) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1119 |
break; /* eof, return nbytes which is less than len */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1120 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1121 |
nbytes += res; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1122 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1123 |
return nbytes; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1124 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1125 |
|
2 | 1126 |
static jdwpTransportError JNICALL |
1127 |
socketTransport_readPacket(jdwpTransportEnv* env, jdwpPacket* packet) { |
|
1128 |
jint length, data_len; |
|
1129 |
jint n; |
|
1130 |
||
1131 |
/* packet can't be null */ |
|
1132 |
if (packet == NULL) { |
|
1133 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null"); |
|
1134 |
} |
|
1135 |
||
1136 |
/* read the length field */ |
|
1137 |
n = recv_fully(socketFD, (char *)&length, sizeof(jint)); |
|
1138 |
||
1139 |
/* check for EOF */ |
|
1140 |
if (n == 0) { |
|
1141 |
packet->type.cmd.len = 0; |
|
1142 |
return JDWPTRANSPORT_ERROR_NONE; |
|
1143 |
} |
|
1144 |
if (n != sizeof(jint)) { |
|
1145 |
RETURN_RECV_ERROR(n); |
|
1146 |
} |
|
1147 |
||
1148 |
length = (jint)dbgsysNetworkToHostLong(length); |
|
1149 |
packet->type.cmd.len = length; |
|
1150 |
||
1151 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1152 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.id), sizeof(jint)); |
2 | 1153 |
if (n < (int)sizeof(jint)) { |
1154 |
RETURN_RECV_ERROR(n); |
|
1155 |
} |
|
1156 |
||
1157 |
packet->type.cmd.id = (jint)dbgsysNetworkToHostLong(packet->type.cmd.id); |
|
1158 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1159 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.flags), sizeof(jbyte)); |
2 | 1160 |
if (n < (int)sizeof(jbyte)) { |
1161 |
RETURN_RECV_ERROR(n); |
|
1162 |
} |
|
1163 |
||
1164 |
if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) { |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1165 |
n = recv_fully(socketFD,(char *)&(packet->type.reply.errorCode), sizeof(jbyte)); |
2 | 1166 |
if (n < (int)sizeof(jshort)) { |
1167 |
RETURN_RECV_ERROR(n); |
|
1168 |
} |
|
1169 |
||
1170 |
/* FIXME - should the error be converted to host order?? */ |
|
1171 |
||
1172 |
||
1173 |
} else { |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1174 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmdSet), sizeof(jbyte)); |
2 | 1175 |
if (n < (int)sizeof(jbyte)) { |
1176 |
RETURN_RECV_ERROR(n); |
|
1177 |
} |
|
1178 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1179 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmd), sizeof(jbyte)); |
2 | 1180 |
if (n < (int)sizeof(jbyte)) { |
1181 |
RETURN_RECV_ERROR(n); |
|
1182 |
} |
|
1183 |
} |
|
1184 |
||
1185 |
data_len = length - ((sizeof(jint) * 2) + (sizeof(jbyte) * 3)); |
|
1186 |
||
1187 |
if (data_len < 0) { |
|
1188 |
setLastError(0, "Badly formed packet received - invalid length"); |
|
1189 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
1190 |
} else if (data_len == 0) { |
|
1191 |
packet->type.cmd.data = NULL; |
|
1192 |
} else { |
|
1193 |
packet->type.cmd.data= (*callback->alloc)(data_len); |
|
1194 |
||
1195 |
if (packet->type.cmd.data == NULL) { |
|
1196 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory"); |
|
1197 |
} |
|
1198 |
||
1199 |
n = recv_fully(socketFD,(char *)packet->type.cmd.data, data_len); |
|
1200 |
if (n < data_len) { |
|
1201 |
(*callback->free)(packet->type.cmd.data); |
|
1202 |
RETURN_RECV_ERROR(n); |
|
1203 |
} |
|
1204 |
} |
|
1205 |
||
1206 |
return JDWPTRANSPORT_ERROR_NONE; |
|
1207 |
} |
|
1208 |
||
1209 |
static jdwpTransportError JNICALL |
|
1210 |
socketTransport_getLastError(jdwpTransportEnv* env, char** msgP) { |
|
1211 |
char *msg = (char *)dbgsysTlsGet(tlsIndex); |
|
1212 |
if (msg == NULL) { |
|
1213 |
return JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE; |
|
1214 |
} |
|
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
1215 |
*msgP = (*callback->alloc)((int)strlen(msg)+1); |
2 | 1216 |
if (*msgP == NULL) { |
1217 |
return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY; |
|
1218 |
} |
|
1219 |
strcpy(*msgP, msg); |
|
1220 |
return JDWPTRANSPORT_ERROR_NONE; |
|
1221 |
} |
|
1222 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1223 |
static jdwpTransportError JNICALL |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1224 |
socketTransport_setConfiguration(jdwpTransportEnv* env, jdwpTransportConfiguration* cfg) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1225 |
const char* allowed_peers = NULL; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1226 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1227 |
if (cfg == NULL) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1228 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1229 |
"NULL pointer to transport configuration is invalid"); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1230 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1231 |
allowed_peers = cfg->allowed_peers; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1232 |
_peers_cnt = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1233 |
if (allowed_peers != NULL) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1234 |
size_t len = strlen(allowed_peers); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1235 |
if (len == 0) { /* Impossible: parseOptions() would reject it */ |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1236 |
fprintf(stderr, "Error in allow option: '%s'\n", allowed_peers); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1237 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1238 |
"allow option should not be empty"); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1239 |
} else if (*allowed_peers == '*') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1240 |
if (len != 1) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1241 |
fprintf(stderr, "Error in allow option: '%s'\n", allowed_peers); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1242 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1243 |
"allow option '*' cannot be expanded"); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1244 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1245 |
} else { |
54884 | 1246 |
int err = parseAllowedPeers(allowed_peers, len); |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1247 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1248 |
return err; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1249 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1250 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1251 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1252 |
return JDWPTRANSPORT_ERROR_NONE; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1253 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1254 |
|
54884 | 1255 |
/* |
1256 |
* Reads boolean system value, sets *result to |
|
1257 |
* - trueValue if the property is "true"; |
|
1258 |
* - falseValue if the property is "false". |
|
1259 |
* Doesn't change *result if the property is not set or failed to read. |
|
1260 |
*/ |
|
1261 |
static int readBooleanSysProp(int *result, int trueValue, int falseValue, |
|
1262 |
JNIEnv* jniEnv, jclass sysClass, jmethodID getPropMethod, const char *propName) |
|
1263 |
{ |
|
1264 |
jstring value; |
|
1265 |
jstring name = (*jniEnv)->NewStringUTF(jniEnv, propName); |
|
1266 |
||
1267 |
if (name == NULL) { |
|
1268 |
return JNI_ERR; |
|
1269 |
} |
|
1270 |
value = (jstring)(*jniEnv)->CallStaticObjectMethod(jniEnv, sysClass, getPropMethod, name); |
|
1271 |
if ((*jniEnv)->ExceptionCheck(jniEnv)) { |
|
1272 |
return JNI_ERR; |
|
1273 |
} |
|
1274 |
if (value != NULL) { |
|
1275 |
const char *theValue = (*jniEnv)->GetStringUTFChars(jniEnv, value, NULL); |
|
1276 |
if (theValue == NULL) { |
|
1277 |
return JNI_ERR; |
|
1278 |
} |
|
1279 |
if (strcmp(theValue, "true") == 0) { |
|
1280 |
*result = trueValue; |
|
1281 |
} else if (strcmp(theValue, "false") == 0) { |
|
1282 |
*result = falseValue; |
|
1283 |
} |
|
1284 |
(*jniEnv)->ReleaseStringUTFChars(jniEnv, value, theValue); |
|
1285 |
} |
|
1286 |
return JNI_OK; |
|
1287 |
} |
|
1288 |
||
49440 | 1289 |
JNIEXPORT jint JNICALL |
2 | 1290 |
jdwpTransport_OnLoad(JavaVM *vm, jdwpTransportCallback* cbTablePtr, |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1291 |
jint version, jdwpTransportEnv** env) |
2 | 1292 |
{ |
54884 | 1293 |
JNIEnv* jniEnv = NULL; |
1294 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1295 |
if (version < JDWPTRANSPORT_VERSION_1_0 || |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1296 |
version > JDWPTRANSPORT_VERSION_1_1) { |
2 | 1297 |
return JNI_EVERSION; |
1298 |
} |
|
1299 |
if (initialized) { |
|
1300 |
/* |
|
1301 |
* This library doesn't support multiple environments (yet) |
|
1302 |
*/ |
|
1303 |
return JNI_EEXIST; |
|
1304 |
} |
|
1305 |
initialized = JNI_TRUE; |
|
1306 |
jvm = vm; |
|
1307 |
callback = cbTablePtr; |
|
1308 |
||
1309 |
/* initialize interface table */ |
|
1310 |
interface.GetCapabilities = &socketTransport_getCapabilities; |
|
1311 |
interface.Attach = &socketTransport_attach; |
|
1312 |
interface.StartListening = &socketTransport_startListening; |
|
1313 |
interface.StopListening = &socketTransport_stopListening; |
|
1314 |
interface.Accept = &socketTransport_accept; |
|
1315 |
interface.IsOpen = &socketTransport_isOpen; |
|
1316 |
interface.Close = &socketTransport_close; |
|
1317 |
interface.ReadPacket = &socketTransport_readPacket; |
|
1318 |
interface.WritePacket = &socketTransport_writePacket; |
|
1319 |
interface.GetLastError = &socketTransport_getLastError; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1320 |
if (version >= JDWPTRANSPORT_VERSION_1_1) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1321 |
interface.SetTransportConfiguration = &socketTransport_setConfiguration; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1322 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1323 |
*env = &single_env; |
2 | 1324 |
|
1325 |
/* initialized TLS */ |
|
1326 |
tlsIndex = dbgsysTlsAlloc(); |
|
54884 | 1327 |
|
1328 |
// retrieve network-related system properties |
|
1329 |
do { |
|
1330 |
jclass sysClass; |
|
1331 |
jmethodID getPropMethod; |
|
1332 |
if ((*vm)->GetEnv(vm, (void **)&jniEnv, JNI_VERSION_9) != JNI_OK) { |
|
1333 |
break; |
|
1334 |
} |
|
1335 |
sysClass = (*jniEnv)->FindClass(jniEnv, "java/lang/System"); |
|
1336 |
if (sysClass == NULL) { |
|
1337 |
break; |
|
1338 |
} |
|
1339 |
getPropMethod = (*jniEnv)->GetStaticMethodID(jniEnv, sysClass, |
|
1340 |
"getProperty", "(Ljava/lang/String;)Ljava/lang/String;"); |
|
1341 |
if (getPropMethod == NULL) { |
|
1342 |
break; |
|
1343 |
} |
|
1344 |
readBooleanSysProp(&allowOnlyIPv4, 1, 0, |
|
1345 |
jniEnv, sysClass, getPropMethod, "java.net.preferIPv4Stack"); |
|
1346 |
readBooleanSysProp(&preferredAddressFamily, AF_INET6, AF_INET, |
|
1347 |
jniEnv, sysClass, getPropMethod, "java.net.preferIPv6Addresses"); |
|
1348 |
} while (0); |
|
1349 |
||
1350 |
if (jniEnv != NULL && (*jniEnv)->ExceptionCheck(jniEnv)) { |
|
1351 |
(*jniEnv)->ExceptionClear(jniEnv); |
|
1352 |
} |
|
1353 |
||
1354 |
||
2 | 1355 |
return JNI_OK; |
1356 |
} |