author | ihse |
Sat, 03 Mar 2018 08:21:47 +0100 | |
branch | ihse-warnings-cflags-branch |
changeset 56230 | 489867818774 |
parent 48978 | 93996c47d36f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
2 |
* Copyright (c) 1998, 2018, 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 |
||
31 |
#include "jdwpTransport.h" |
|
32 |
#include "sysSocket.h" |
|
33 |
||
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
34 |
#ifdef _WIN32 |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
35 |
#include <winsock2.h> |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
36 |
#include <ws2tcpip.h> |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
37 |
#else |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
38 |
#include <arpa/inet.h> |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
39 |
#include <sys/socket.h> |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
40 |
#endif |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
41 |
|
2 | 42 |
/* |
43 |
* The Socket Transport Library. |
|
44 |
* |
|
45 |
* This module is an implementation of the Java Debug Wire Protocol Transport |
|
46 |
* Service Provider Interface - see src/share/javavm/export/jdwpTransport.h. |
|
47 |
*/ |
|
48 |
||
49 |
static int serverSocketFD; |
|
50 |
static int socketFD = -1; |
|
51 |
static jdwpTransportCallback *callback; |
|
52 |
static JavaVM *jvm; |
|
53 |
static int tlsIndex; |
|
54 |
static jboolean initialized; |
|
55 |
static struct jdwpTransportNativeInterface_ interface; |
|
56 |
static jdwpTransportEnv single_env = (jdwpTransportEnv)&interface; |
|
57 |
||
58 |
#define RETURN_ERROR(err, msg) \ |
|
59 |
if (1==1) { \ |
|
60 |
setLastError(err, msg); \ |
|
61 |
return err; \ |
|
62 |
} |
|
63 |
||
64 |
#define RETURN_IO_ERROR(msg) RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, msg); |
|
65 |
||
66 |
#define RETURN_RECV_ERROR(n) \ |
|
67 |
if (n == 0) { \ |
|
68 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, "premature EOF"); \ |
|
69 |
} else { \ |
|
70 |
RETURN_IO_ERROR("recv error"); \ |
|
71 |
} |
|
72 |
||
73 |
#define MAX_DATA_SIZE 1000 |
|
74 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
75 |
static jint recv_fully(int, char *, int); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
76 |
static jint send_fully(int, char *, int); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
77 |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
78 |
/* version >= JDWPTRANSPORT_VERSION_1_1 */ |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
79 |
typedef struct { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
80 |
uint32_t subnet; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
81 |
uint32_t netmask; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
82 |
} AllowedPeerInfo; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
83 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
84 |
#define STR(x) #x |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
85 |
#define MAX_PEER_ENTRIES 32 |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
86 |
#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
|
87 |
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
|
88 |
static int _peers_cnt = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
89 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
90 |
|
2 | 91 |
/* |
92 |
* Record the last error for this thread. |
|
93 |
*/ |
|
94 |
static void |
|
95 |
setLastError(jdwpTransportError err, char *newmsg) { |
|
96 |
char buf[255]; |
|
97 |
char *msg; |
|
98 |
||
99 |
/* get any I/O first in case any system calls override errno */ |
|
100 |
if (err == JDWPTRANSPORT_ERROR_IO_ERROR) { |
|
101 |
dbgsysGetLastIOError(buf, sizeof(buf)); |
|
102 |
} |
|
103 |
||
104 |
msg = (char *)dbgsysTlsGet(tlsIndex); |
|
105 |
if (msg != NULL) { |
|
106 |
(*callback->free)(msg); |
|
107 |
} |
|
108 |
||
109 |
if (err == JDWPTRANSPORT_ERROR_IO_ERROR) { |
|
110 |
char *join_str = ": "; |
|
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
111 |
int msg_len = (int)strlen(newmsg) + (int)strlen(join_str) + |
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
112 |
(int)strlen(buf) + 3; |
2 | 113 |
msg = (*callback->alloc)(msg_len); |
114 |
if (msg != NULL) { |
|
115 |
strcpy(msg, newmsg); |
|
116 |
strcat(msg, join_str); |
|
117 |
strcat(msg, buf); |
|
118 |
} |
|
119 |
} else { |
|
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
120 |
msg = (*callback->alloc)((int)strlen(newmsg)+1); |
2 | 121 |
if (msg != NULL) { |
122 |
strcpy(msg, newmsg); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
dbgsysTlsPut(tlsIndex, msg); |
|
127 |
} |
|
128 |
||
129 |
/* |
|
130 |
* Return the last error for this thread (may be NULL) |
|
131 |
*/ |
|
132 |
static char* |
|
56230 | 133 |
getLastError(void) { |
2 | 134 |
return (char *)dbgsysTlsGet(tlsIndex); |
135 |
} |
|
136 |
||
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
137 |
/* Set options common to client and server sides */ |
2 | 138 |
static jdwpTransportError |
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
139 |
setOptionsCommon(int fd) |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
140 |
{ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
141 |
jvalue dontcare; |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
142 |
int err; |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
143 |
|
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
144 |
dontcare.i = 0; /* keep compiler happy */ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
145 |
|
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
146 |
err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare); |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
147 |
if (err < 0) { |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
148 |
RETURN_IO_ERROR("setsockopt TCPNODELAY failed"); |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
149 |
} |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
150 |
|
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
151 |
return JDWPTRANSPORT_ERROR_NONE; |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
152 |
} |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
153 |
|
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
154 |
/* Set the SO_REUSEADDR option */ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
155 |
static jdwpTransportError |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
156 |
setReuseAddrOption(int fd) |
2 | 157 |
{ |
158 |
jvalue dontcare; |
|
159 |
int err; |
|
160 |
||
161 |
dontcare.i = 0; /* keep compiler happy */ |
|
162 |
||
163 |
err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare); |
|
164 |
if (err < 0) { |
|
165 |
RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed"); |
|
166 |
} |
|
167 |
||
168 |
return JDWPTRANSPORT_ERROR_NONE; |
|
169 |
} |
|
170 |
||
171 |
static jdwpTransportError |
|
172 |
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
|
173 |
const char *hello = "JDWP-Handshake"; |
2 | 174 |
char b[16]; |
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
175 |
int rv, helloLen, received; |
2 | 176 |
|
177 |
if (timeout > 0) { |
|
178 |
dbgsysConfigureBlocking(fd, JNI_FALSE); |
|
179 |
} |
|
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
180 |
helloLen = (int)strlen(hello); |
2 | 181 |
received = 0; |
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
182 |
while (received < helloLen) { |
2 | 183 |
int n; |
184 |
char *buf; |
|
185 |
if (timeout > 0) { |
|
186 |
rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout); |
|
187 |
if (rv <= 0) { |
|
188 |
setLastError(0, "timeout during handshake"); |
|
189 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
190 |
} |
|
191 |
} |
|
192 |
buf = b; |
|
193 |
buf += received; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
194 |
n = recv_fully(fd, buf, helloLen-received); |
2 | 195 |
if (n == 0) { |
196 |
setLastError(0, "handshake failed - connection prematurally closed"); |
|
197 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
198 |
} |
|
199 |
if (n < 0) { |
|
200 |
RETURN_IO_ERROR("recv failed during handshake"); |
|
201 |
} |
|
202 |
received += n; |
|
203 |
} |
|
204 |
if (timeout > 0) { |
|
205 |
dbgsysConfigureBlocking(fd, JNI_TRUE); |
|
206 |
} |
|
3723
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
207 |
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
|
208 |
char msg[80+2*16]; |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
209 |
b[received] = '\0'; |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
210 |
/* |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
211 |
* 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
|
212 |
* 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
|
213 |
*/ |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
214 |
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
|
215 |
setLastError(0, msg); |
a27278866f80
6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents:
1247
diff
changeset
|
216 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
2 | 217 |
} |
218 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
219 |
if (send_fully(fd, (char*)hello, helloLen) != helloLen) { |
2 | 220 |
RETURN_IO_ERROR("send failed during handshake"); |
221 |
} |
|
222 |
return JDWPTRANSPORT_ERROR_NONE; |
|
223 |
} |
|
224 |
||
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
225 |
static uint32_t |
56230 | 226 |
getLocalHostAddress(void) { |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
227 |
// Simple routine to guess localhost address. |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
228 |
// it looks up "localhost" and returns 127.0.0.1 if lookup |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
229 |
// fails. |
24514
2440b44952d7
8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents:
24503
diff
changeset
|
230 |
struct addrinfo hints, *res = NULL; |
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
231 |
uint32_t addr; |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
232 |
int err; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
233 |
|
24514
2440b44952d7
8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents:
24503
diff
changeset
|
234 |
// Use portable way to initialize the structure |
2440b44952d7
8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents:
24503
diff
changeset
|
235 |
memset((void *)&hints, 0, sizeof(hints)); |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
236 |
hints.ai_family = AF_INET; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
237 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
238 |
err = getaddrinfo("localhost", NULL, &hints, &res); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
239 |
if (err < 0 || res == NULL) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
240 |
return dbgsysHostToNetworkLong(INADDR_LOOPBACK); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
241 |
} |
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 |
// getaddrinfo might return more than one address |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
244 |
// but we are using first one only |
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
245 |
addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr; |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
246 |
freeaddrinfo(res); |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
247 |
return addr; |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
248 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
249 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
250 |
static int |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
251 |
getPortNumber(const char *s_port) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
252 |
u_long n; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
253 |
char *eptr; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
254 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
255 |
if (*s_port == 0) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
256 |
// 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
|
257 |
return -1; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
258 |
} |
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 |
n = strtoul(s_port, &eptr, 10); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
261 |
if (eptr != s_port + strlen(s_port)) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
262 |
// incomplete conversion - port number contains non-digit |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
263 |
return -1; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
264 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
265 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
266 |
if (n > (u_short) -1) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
267 |
// 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
|
268 |
// maximum possible u_short value (65535) and |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
269 |
// will not be truncated later. |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
270 |
return -1; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
271 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
272 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
273 |
return n; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
274 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
275 |
|
2 | 276 |
static jdwpTransportError |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
277 |
parseAddress(const char *address, struct sockaddr_in *sa) { |
2 | 278 |
char *colon; |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
279 |
int port; |
2 | 280 |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
281 |
memset((void *)sa, 0, sizeof(struct sockaddr_in)); |
2 | 282 |
sa->sin_family = AF_INET; |
283 |
||
284 |
/* check for host:port or port */ |
|
285 |
colon = strchr(address, ':'); |
|
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
286 |
port = getPortNumber((colon == NULL) ? address : colon +1); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
287 |
if (port < 0) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
288 |
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
|
289 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
290 |
sa->sin_port = dbgsysHostToNetworkShort((u_short)port); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
291 |
|
2 | 292 |
if (colon == NULL) { |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
293 |
// bind to localhost only if no address specified |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
294 |
sa->sin_addr.s_addr = getLocalHostAddress(); |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
295 |
} else if (strncmp(address, "localhost:", 10) == 0) { |
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
296 |
// optimize for common case |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
297 |
sa->sin_addr.s_addr = getLocalHostAddress(); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
298 |
} else if (*address == '*' && *(address+1) == ':') { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
299 |
// we are explicitly asked to bind server to all available IP addresses |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
300 |
// has no meaning for client. |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
301 |
sa->sin_addr.s_addr = dbgsysHostToNetworkLong(INADDR_ANY); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
302 |
} else { |
2 | 303 |
char *buf; |
304 |
char *hostname; |
|
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
305 |
uint32_t addr; |
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
306 |
int ai; |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
307 |
buf = (*callback->alloc)((int)strlen(address) + 1); |
2 | 308 |
if (buf == NULL) { |
309 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory"); |
|
310 |
} |
|
311 |
strcpy(buf, address); |
|
312 |
buf[colon - address] = '\0'; |
|
313 |
hostname = buf; |
|
314 |
||
315 |
/* |
|
316 |
* First see if the host is a literal IP address. |
|
317 |
* If not then try to resolve it. |
|
318 |
*/ |
|
319 |
addr = dbgsysInetAddr(hostname); |
|
320 |
if (addr == 0xffffffff) { |
|
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
321 |
struct addrinfo hints; |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
322 |
struct addrinfo *results = NULL; |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
323 |
memset (&hints, 0, sizeof(hints)); |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
324 |
hints.ai_family = AF_INET; |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
325 |
hints.ai_socktype = SOCK_STREAM; |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
326 |
hints.ai_protocol = IPPROTO_TCP; |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
327 |
|
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
328 |
ai = dbgsysGetAddrInfo(hostname, NULL, &hints, &results); |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
329 |
|
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
330 |
if (ai != 0) { |
2 | 331 |
/* don't use RETURN_IO_ERROR as unknown host is normal */ |
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
332 |
setLastError(0, "getaddrinfo: unknown host"); |
2 | 333 |
(*callback->free)(buf); |
334 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
335 |
} |
|
336 |
||
337 |
/* lookup was successful */ |
|
48767
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
338 |
sa->sin_addr = ((struct sockaddr_in *)results->ai_addr)->sin_addr; |
0c6ce8fdb50a
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents:
48242
diff
changeset
|
339 |
freeaddrinfo(results); |
2 | 340 |
} else { |
341 |
sa->sin_addr.s_addr = addr; |
|
342 |
} |
|
343 |
||
344 |
(*callback->free)(buf); |
|
345 |
} |
|
346 |
||
347 |
return JDWPTRANSPORT_ERROR_NONE; |
|
348 |
} |
|
349 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
350 |
static const char * |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
351 |
ip_s2u(const char *instr, uint32_t *ip) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
352 |
// Convert string representation of ip to integer |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
353 |
// in network byte order (big-endian) |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
354 |
char t[4] = { 0, 0, 0, 0 }; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
355 |
const char *s = instr; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
356 |
int i = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
357 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
358 |
while (1) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
359 |
if (*s == '.') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
360 |
++i; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
361 |
++s; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
362 |
continue; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
363 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
364 |
if (*s == 0 || *s == '+' || *s == '/') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
365 |
break; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
366 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
367 |
if (*s < '0' || *s > '9') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
368 |
return instr; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
369 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
370 |
t[i] = (t[i] * 10) + (*s - '0'); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
371 |
++s; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
372 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
373 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
374 |
*ip = *(uint32_t*)(t); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
375 |
return s; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
376 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
377 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
378 |
static const char * |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
379 |
mask_s2u(const char *instr, uint32_t *mask) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
380 |
// Convert the number of bits to a netmask |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
381 |
// in network byte order (big-endian) |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
382 |
unsigned char m = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
383 |
const char *s = instr; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
384 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
385 |
while (1) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
386 |
if (*s == 0 || *s == '+') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
387 |
break; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
388 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
389 |
if (*s < '0' || *s > '9') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
390 |
return instr; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
391 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
392 |
m = (m * 10) + (*s - '0'); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
393 |
++s; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
394 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
395 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
396 |
if (m == 0 || m > 32) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
397 |
// Drop invalid input |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
398 |
return instr; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
399 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
400 |
|
48978
93996c47d36f
8196909: gcc 7.2.1 compiler warning in libdt_socket
cjplummer
parents:
48767
diff
changeset
|
401 |
*mask = htonl((uint32_t)(~0) << (32 - m)); |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
402 |
return s; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
403 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
404 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
405 |
static int |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
406 |
ip_in_subnet(uint32_t subnet, uint32_t mask, uint32_t ipaddr) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
407 |
return (ipaddr & mask) == subnet; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
408 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
409 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
410 |
static jdwpTransportError |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
411 |
parseAllowedPeers(const char *allowed_peers) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
412 |
// Build a list of allowed peers from char string |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
413 |
// of format 192.168.0.10+192.168.0.0/24 |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
414 |
const char *s = NULL; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
415 |
const char *p = allowed_peers; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
416 |
uint32_t ip = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
417 |
uint32_t mask = 0xFFFFFFFF; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
418 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
419 |
while (1) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
420 |
s = ip_s2u(p, &ip); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
421 |
if (s == p) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
422 |
_peers_cnt = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
423 |
fprintf(stderr, "Error in allow option: '%s'\n", s); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
424 |
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
|
425 |
"invalid IP address in allow option"); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
426 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
427 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
428 |
if (*s == '/') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
429 |
// netmask specified |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
430 |
s = mask_s2u(s + 1, &mask); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
431 |
if (*(s - 1) == '/') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
432 |
// Input is not consumed, something bad happened |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
433 |
_peers_cnt = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
434 |
fprintf(stderr, "Error in allow option: '%s'\n", s); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
435 |
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
|
436 |
"invalid netmask in allow option"); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
437 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
438 |
} else { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
439 |
// reset netmask |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
440 |
mask = 0xFFFFFFFF; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
441 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
442 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
443 |
if (*s == '+' || *s == 0) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
444 |
if (_peers_cnt >= MAX_PEER_ENTRIES) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
445 |
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
|
446 |
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
|
447 |
"exceeded max number of allowed peers: " MAX_PEERS_STR); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
448 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
449 |
_peers[_peers_cnt].subnet = ip; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
450 |
_peers[_peers_cnt].netmask = mask; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
451 |
_peers_cnt++; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
452 |
if (*s == 0) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
453 |
// end of options |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
454 |
break; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
455 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
456 |
// advance to next IP block |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
457 |
p = s + 1; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
458 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
459 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
460 |
return JDWPTRANSPORT_ERROR_NONE; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
461 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
462 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
463 |
static int |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
464 |
isPeerAllowed(struct sockaddr_in *peer) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
465 |
int i; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
466 |
for (i = 0; i < _peers_cnt; ++i) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
467 |
int peer_ip = peer->sin_addr.s_addr; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
468 |
if (ip_in_subnet(_peers[i].subnet, _peers[i].netmask, peer_ip)) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
469 |
return 1; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
470 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
471 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
472 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
473 |
return 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
474 |
} |
2 | 475 |
|
476 |
static jdwpTransportError JNICALL |
|
477 |
socketTransport_getCapabilities(jdwpTransportEnv* env, |
|
478 |
JDWPTransportCapabilities* capabilitiesPtr) |
|
479 |
{ |
|
480 |
JDWPTransportCapabilities result; |
|
481 |
||
482 |
memset(&result, 0, sizeof(result)); |
|
483 |
result.can_timeout_attach = JNI_TRUE; |
|
484 |
result.can_timeout_accept = JNI_TRUE; |
|
485 |
result.can_timeout_handshake = JNI_TRUE; |
|
486 |
||
487 |
*capabilitiesPtr = result; |
|
488 |
||
489 |
return JDWPTRANSPORT_ERROR_NONE; |
|
490 |
} |
|
491 |
||
492 |
||
493 |
static jdwpTransportError JNICALL |
|
494 |
socketTransport_startListening(jdwpTransportEnv* env, const char* address, |
|
495 |
char** actualAddress) |
|
496 |
{ |
|
497 |
struct sockaddr_in sa; |
|
498 |
int err; |
|
499 |
||
500 |
memset((void *)&sa,0,sizeof(struct sockaddr_in)); |
|
501 |
sa.sin_family = AF_INET; |
|
502 |
||
503 |
/* no address provided */ |
|
504 |
if ((address == NULL) || (address[0] == '\0')) { |
|
505 |
address = "0"; |
|
506 |
} |
|
507 |
||
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
508 |
err = parseAddress(address, &sa); |
2 | 509 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
510 |
return err; |
|
511 |
} |
|
512 |
||
513 |
serverSocketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0); |
|
514 |
if (serverSocketFD < 0) { |
|
515 |
RETURN_IO_ERROR("socket creation failed"); |
|
516 |
} |
|
517 |
||
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
518 |
err = setOptionsCommon(serverSocketFD); |
2 | 519 |
if (err) { |
520 |
return err; |
|
521 |
} |
|
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
522 |
if (sa.sin_port != 0) { |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
523 |
/* |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
524 |
* 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
|
525 |
* start seeing EADDRINUSE due to collisions in free ports |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
526 |
* then we should retry the dbgsysBind() a few times. |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
527 |
*/ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
528 |
err = setReuseAddrOption(serverSocketFD); |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
529 |
if (err) { |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
530 |
return err; |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
531 |
} |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
532 |
} |
2 | 533 |
|
534 |
err = dbgsysBind(serverSocketFD, (struct sockaddr *)&sa, sizeof(sa)); |
|
535 |
if (err < 0) { |
|
536 |
RETURN_IO_ERROR("bind failed"); |
|
537 |
} |
|
538 |
||
539 |
err = dbgsysListen(serverSocketFD, 1); |
|
540 |
if (err < 0) { |
|
541 |
RETURN_IO_ERROR("listen failed"); |
|
542 |
} |
|
543 |
||
544 |
{ |
|
545 |
char buf[20]; |
|
15274
a77cdd5ea6e3
8005120: Compiler warnings in socket transport native code
jzavgren
parents:
14342
diff
changeset
|
546 |
socklen_t len = sizeof(sa); |
2 | 547 |
jint portNum; |
548 |
err = dbgsysGetSocketName(serverSocketFD, |
|
549 |
(struct sockaddr *)&sa, &len); |
|
550 |
portNum = dbgsysNetworkToHostShort(sa.sin_port); |
|
551 |
sprintf(buf, "%d", portNum); |
|
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
552 |
*actualAddress = (*callback->alloc)((int)strlen(buf) + 1); |
2 | 553 |
if (*actualAddress == NULL) { |
554 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory"); |
|
555 |
} else { |
|
556 |
strcpy(*actualAddress, buf); |
|
557 |
} |
|
558 |
} |
|
559 |
||
560 |
return JDWPTRANSPORT_ERROR_NONE; |
|
561 |
} |
|
562 |
||
563 |
static jdwpTransportError JNICALL |
|
564 |
socketTransport_accept(jdwpTransportEnv* env, jlong acceptTimeout, jlong handshakeTimeout) |
|
565 |
{ |
|
15274
a77cdd5ea6e3
8005120: Compiler warnings in socket transport native code
jzavgren
parents:
14342
diff
changeset
|
566 |
socklen_t socketLen; |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
567 |
int err = JDWPTRANSPORT_ERROR_NONE; |
2 | 568 |
struct sockaddr_in socket; |
569 |
jlong startTime = (jlong)0; |
|
570 |
||
571 |
/* |
|
572 |
* Use a default handshake timeout if not specified - this avoids an indefinite |
|
573 |
* hang in cases where something other than a debugger connects to our port. |
|
574 |
*/ |
|
575 |
if (handshakeTimeout == 0) { |
|
576 |
handshakeTimeout = 2000; |
|
577 |
} |
|
578 |
||
579 |
do { |
|
580 |
/* |
|
581 |
* If there is an accept timeout then we put the socket in non-blocking |
|
582 |
* mode and poll for a connection. |
|
583 |
*/ |
|
584 |
if (acceptTimeout > 0) { |
|
585 |
int rv; |
|
586 |
dbgsysConfigureBlocking(serverSocketFD, JNI_FALSE); |
|
587 |
startTime = dbgsysCurrentTimeMillis(); |
|
588 |
rv = dbgsysPoll(serverSocketFD, JNI_TRUE, JNI_FALSE, (long)acceptTimeout); |
|
589 |
if (rv <= 0) { |
|
590 |
/* set the last error here as could be overridden by configureBlocking */ |
|
591 |
if (rv == 0) { |
|
592 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "poll failed"); |
|
593 |
} |
|
594 |
/* restore blocking state */ |
|
595 |
dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE); |
|
596 |
if (rv == 0) { |
|
597 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "timed out waiting for connection"); |
|
598 |
} else { |
|
599 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
600 |
} |
|
601 |
} |
|
602 |
} |
|
603 |
||
604 |
/* |
|
605 |
* Accept the connection |
|
606 |
*/ |
|
607 |
memset((void *)&socket,0,sizeof(struct sockaddr_in)); |
|
608 |
socketLen = sizeof(socket); |
|
609 |
socketFD = dbgsysAccept(serverSocketFD, |
|
610 |
(struct sockaddr *)&socket, |
|
611 |
&socketLen); |
|
612 |
/* set the last error here as could be overridden by configureBlocking */ |
|
613 |
if (socketFD < 0) { |
|
614 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "accept failed"); |
|
615 |
} |
|
616 |
/* |
|
617 |
* Restore the blocking state - note that the accepted socket may be in |
|
618 |
* blocking or non-blocking mode (platform dependent). However as there |
|
619 |
* is a handshake timeout set then it will go into non-blocking mode |
|
620 |
* anyway for the handshake. |
|
621 |
*/ |
|
622 |
if (acceptTimeout > 0) { |
|
623 |
dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE); |
|
624 |
} |
|
625 |
if (socketFD < 0) { |
|
626 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
627 |
} |
|
628 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
629 |
/* |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
630 |
* version >= JDWPTRANSPORT_VERSION_1_1: |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
631 |
* 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
|
632 |
*/ |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
633 |
if (_peers_cnt > 0) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
634 |
if (!isPeerAllowed(&socket)) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
635 |
char ebuf[64] = { 0 }; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
636 |
char buf[INET_ADDRSTRLEN] = { 0 }; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
637 |
const char* addr_str = inet_ntop(AF_INET, &(socket.sin_addr), buf, INET_ADDRSTRLEN); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
638 |
sprintf(ebuf, "ERROR: Peer not allowed to connect: %s\n", |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
639 |
(addr_str == NULL) ? "<bad address>" : addr_str); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
640 |
dbgsysSocketClose(socketFD); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
641 |
socketFD = -1; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
642 |
err = JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
643 |
setLastError(err, ebuf); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
644 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
645 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
646 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
647 |
if (socketFD > 0) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
648 |
/* handshake with the debugger */ |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
649 |
err = handshake(socketFD, handshakeTimeout); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
650 |
} |
2 | 651 |
|
652 |
/* |
|
653 |
* If the handshake fails then close the connection. If there if an accept |
|
654 |
* timeout then we must adjust the timeout for the next poll. |
|
655 |
*/ |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
656 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
2 | 657 |
fprintf(stderr, "Debugger failed to attach: %s\n", getLastError()); |
658 |
dbgsysSocketClose(socketFD); |
|
659 |
socketFD = -1; |
|
660 |
if (acceptTimeout > 0) { |
|
661 |
long endTime = dbgsysCurrentTimeMillis(); |
|
662 |
acceptTimeout -= (endTime - startTime); |
|
663 |
if (acceptTimeout <= 0) { |
|
664 |
setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, |
|
665 |
"timeout waiting for debugger to connect"); |
|
666 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
667 |
} |
|
668 |
} |
|
669 |
} |
|
670 |
} while (socketFD < 0); |
|
671 |
||
672 |
return JDWPTRANSPORT_ERROR_NONE; |
|
673 |
} |
|
674 |
||
675 |
static jdwpTransportError JNICALL |
|
676 |
socketTransport_stopListening(jdwpTransportEnv *env) |
|
677 |
{ |
|
678 |
if (serverSocketFD < 0) { |
|
679 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "connection not open"); |
|
680 |
} |
|
681 |
if (dbgsysSocketClose(serverSocketFD) < 0) { |
|
682 |
RETURN_IO_ERROR("close failed"); |
|
683 |
} |
|
684 |
serverSocketFD = -1; |
|
685 |
return JDWPTRANSPORT_ERROR_NONE; |
|
686 |
} |
|
687 |
||
688 |
static jdwpTransportError JNICALL |
|
689 |
socketTransport_attach(jdwpTransportEnv* env, const char* addressString, jlong attachTimeout, |
|
690 |
jlong handshakeTimeout) |
|
691 |
{ |
|
692 |
struct sockaddr_in sa; |
|
693 |
int err; |
|
694 |
||
695 |
if (addressString == NULL || addressString[0] == '\0') { |
|
696 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "address is missing"); |
|
697 |
} |
|
698 |
||
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
23010
diff
changeset
|
699 |
err = parseAddress(addressString, &sa); |
2 | 700 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
701 |
return err; |
|
702 |
} |
|
703 |
||
704 |
socketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0); |
|
705 |
if (socketFD < 0) { |
|
706 |
RETURN_IO_ERROR("unable to create socket"); |
|
707 |
} |
|
708 |
||
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
709 |
err = setOptionsCommon(socketFD); |
2 | 710 |
if (err) { |
711 |
return err; |
|
712 |
} |
|
713 |
||
714 |
/* |
|
46091
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
715 |
* We don't call setReuseAddrOption() for the non-server socket |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
716 |
* 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
|
717 |
* ports then we should retry the dbgsysConnect() a few times. |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
718 |
*/ |
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
719 |
|
a6f2dc91ba56
8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents:
36134
diff
changeset
|
720 |
/* |
2 | 721 |
* To do a timed connect we make the socket non-blocking |
722 |
* and poll with a timeout; |
|
723 |
*/ |
|
724 |
if (attachTimeout > 0) { |
|
725 |
dbgsysConfigureBlocking(socketFD, JNI_FALSE); |
|
726 |
} |
|
727 |
||
728 |
err = dbgsysConnect(socketFD, (struct sockaddr *)&sa, sizeof(sa)); |
|
729 |
if (err == DBG_EINPROGRESS && attachTimeout > 0) { |
|
730 |
err = dbgsysFinishConnect(socketFD, (long)attachTimeout); |
|
731 |
||
732 |
if (err == DBG_ETIMEOUT) { |
|
733 |
dbgsysConfigureBlocking(socketFD, JNI_TRUE); |
|
734 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "connect timed out"); |
|
735 |
} |
|
736 |
} |
|
737 |
||
738 |
if (err < 0) { |
|
739 |
RETURN_IO_ERROR("connect failed"); |
|
740 |
} |
|
741 |
||
742 |
if (attachTimeout > 0) { |
|
743 |
dbgsysConfigureBlocking(socketFD, JNI_TRUE); |
|
744 |
} |
|
745 |
||
746 |
err = handshake(socketFD, handshakeTimeout); |
|
747 |
if (err) { |
|
748 |
dbgsysSocketClose(socketFD); |
|
749 |
socketFD = -1; |
|
750 |
return err; |
|
751 |
} |
|
752 |
||
753 |
return JDWPTRANSPORT_ERROR_NONE; |
|
754 |
} |
|
755 |
||
756 |
static jboolean JNICALL |
|
757 |
socketTransport_isOpen(jdwpTransportEnv* env) |
|
758 |
{ |
|
759 |
if (socketFD >= 0) { |
|
760 |
return JNI_TRUE; |
|
761 |
} else { |
|
762 |
return JNI_FALSE; |
|
763 |
} |
|
764 |
} |
|
765 |
||
766 |
static jdwpTransportError JNICALL |
|
767 |
socketTransport_close(jdwpTransportEnv* env) |
|
768 |
{ |
|
769 |
int fd = socketFD; |
|
770 |
socketFD = -1; |
|
771 |
if (fd < 0) { |
|
772 |
return JDWPTRANSPORT_ERROR_NONE; |
|
773 |
} |
|
22605
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
774 |
#ifdef _AIX |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
775 |
/* |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
776 |
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
|
777 |
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
|
778 |
... |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
779 |
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
|
780 |
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
|
781 |
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
|
782 |
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
|
783 |
... |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
784 |
*/ |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
785 |
shutdown(fd, 2); |
dba3d6b22818
8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents:
15274
diff
changeset
|
786 |
#endif |
2 | 787 |
if (dbgsysSocketClose(fd) < 0) { |
788 |
/* |
|
789 |
* 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
|
790 |
* any subsequent close will likely fail as well. |
2 | 791 |
*/ |
792 |
RETURN_IO_ERROR("close failed"); |
|
793 |
} |
|
794 |
return JDWPTRANSPORT_ERROR_NONE; |
|
795 |
} |
|
796 |
||
797 |
static jdwpTransportError JNICALL |
|
798 |
socketTransport_writePacket(jdwpTransportEnv* env, const jdwpPacket *packet) |
|
799 |
{ |
|
800 |
jint len, data_len, id; |
|
801 |
/* |
|
802 |
* room for header and up to MAX_DATA_SIZE data bytes |
|
803 |
*/ |
|
48242 | 804 |
char header[JDWP_HEADER_SIZE + MAX_DATA_SIZE]; |
2 | 805 |
jbyte *data; |
806 |
||
807 |
/* packet can't be null */ |
|
808 |
if (packet == NULL) { |
|
809 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is NULL"); |
|
810 |
} |
|
811 |
||
812 |
len = packet->type.cmd.len; /* includes header */ |
|
48242 | 813 |
data_len = len - JDWP_HEADER_SIZE; |
2 | 814 |
|
815 |
/* bad packet */ |
|
816 |
if (data_len < 0) { |
|
817 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid length"); |
|
818 |
} |
|
819 |
||
820 |
/* prepare the header for transmission */ |
|
821 |
len = (jint)dbgsysHostToNetworkLong(len); |
|
822 |
id = (jint)dbgsysHostToNetworkLong(packet->type.cmd.id); |
|
823 |
||
824 |
memcpy(header + 0, &len, 4); |
|
825 |
memcpy(header + 4, &id, 4); |
|
826 |
header[8] = packet->type.cmd.flags; |
|
827 |
if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) { |
|
828 |
jshort errorCode = |
|
829 |
dbgsysHostToNetworkShort(packet->type.reply.errorCode); |
|
830 |
memcpy(header + 9, &errorCode, 2); |
|
831 |
} else { |
|
832 |
header[9] = packet->type.cmd.cmdSet; |
|
833 |
header[10] = packet->type.cmd.cmd; |
|
834 |
} |
|
835 |
||
836 |
data = packet->type.cmd.data; |
|
837 |
/* Do one send for short packets, two for longer ones */ |
|
838 |
if (data_len <= MAX_DATA_SIZE) { |
|
48242 | 839 |
memcpy(header + JDWP_HEADER_SIZE, data, data_len); |
840 |
if (send_fully(socketFD, (char *)&header, JDWP_HEADER_SIZE + data_len) != |
|
841 |
JDWP_HEADER_SIZE + data_len) { |
|
2 | 842 |
RETURN_IO_ERROR("send failed"); |
843 |
} |
|
844 |
} else { |
|
48242 | 845 |
memcpy(header + JDWP_HEADER_SIZE, data, MAX_DATA_SIZE); |
846 |
if (send_fully(socketFD, (char *)&header, JDWP_HEADER_SIZE + MAX_DATA_SIZE) != |
|
847 |
JDWP_HEADER_SIZE + MAX_DATA_SIZE) { |
|
2 | 848 |
RETURN_IO_ERROR("send failed"); |
849 |
} |
|
850 |
/* 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
|
851 |
if (send_fully(socketFD, (char *)data + MAX_DATA_SIZE, |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
852 |
data_len - MAX_DATA_SIZE) != data_len - MAX_DATA_SIZE) { |
2 | 853 |
RETURN_IO_ERROR("send failed"); |
854 |
} |
|
855 |
} |
|
856 |
||
857 |
return JDWPTRANSPORT_ERROR_NONE; |
|
858 |
} |
|
859 |
||
860 |
static jint |
|
861 |
recv_fully(int f, char *buf, int len) |
|
862 |
{ |
|
863 |
int nbytes = 0; |
|
864 |
while (nbytes < len) { |
|
865 |
int res = dbgsysRecv(f, buf + nbytes, len - nbytes, 0); |
|
866 |
if (res < 0) { |
|
867 |
return res; |
|
868 |
} else if (res == 0) { |
|
869 |
break; /* eof, return nbytes which is less than len */ |
|
870 |
} |
|
871 |
nbytes += res; |
|
872 |
} |
|
873 |
return nbytes; |
|
874 |
} |
|
875 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
876 |
jint |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
877 |
send_fully(int f, char *buf, int len) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
878 |
{ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
879 |
int nbytes = 0; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
880 |
while (nbytes < len) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
881 |
int res = dbgsysSend(f, buf + nbytes, len - nbytes, 0); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
882 |
if (res < 0) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
883 |
return res; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
884 |
} else if (res == 0) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
885 |
break; /* eof, return nbytes which is less than len */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
886 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
887 |
nbytes += res; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
888 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
889 |
return nbytes; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
890 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
891 |
|
2 | 892 |
static jdwpTransportError JNICALL |
893 |
socketTransport_readPacket(jdwpTransportEnv* env, jdwpPacket* packet) { |
|
894 |
jint length, data_len; |
|
895 |
jint n; |
|
896 |
||
897 |
/* packet can't be null */ |
|
898 |
if (packet == NULL) { |
|
899 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null"); |
|
900 |
} |
|
901 |
||
902 |
/* read the length field */ |
|
903 |
n = recv_fully(socketFD, (char *)&length, sizeof(jint)); |
|
904 |
||
905 |
/* check for EOF */ |
|
906 |
if (n == 0) { |
|
907 |
packet->type.cmd.len = 0; |
|
908 |
return JDWPTRANSPORT_ERROR_NONE; |
|
909 |
} |
|
910 |
if (n != sizeof(jint)) { |
|
911 |
RETURN_RECV_ERROR(n); |
|
912 |
} |
|
913 |
||
914 |
length = (jint)dbgsysNetworkToHostLong(length); |
|
915 |
packet->type.cmd.len = length; |
|
916 |
||
917 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
918 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.id), sizeof(jint)); |
2 | 919 |
if (n < (int)sizeof(jint)) { |
920 |
RETURN_RECV_ERROR(n); |
|
921 |
} |
|
922 |
||
923 |
packet->type.cmd.id = (jint)dbgsysNetworkToHostLong(packet->type.cmd.id); |
|
924 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
925 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.flags), sizeof(jbyte)); |
2 | 926 |
if (n < (int)sizeof(jbyte)) { |
927 |
RETURN_RECV_ERROR(n); |
|
928 |
} |
|
929 |
||
930 |
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
|
931 |
n = recv_fully(socketFD,(char *)&(packet->type.reply.errorCode), sizeof(jbyte)); |
2 | 932 |
if (n < (int)sizeof(jshort)) { |
933 |
RETURN_RECV_ERROR(n); |
|
934 |
} |
|
935 |
||
936 |
/* FIXME - should the error be converted to host order?? */ |
|
937 |
||
938 |
||
939 |
} else { |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
940 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmdSet), sizeof(jbyte)); |
2 | 941 |
if (n < (int)sizeof(jbyte)) { |
942 |
RETURN_RECV_ERROR(n); |
|
943 |
} |
|
944 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
945 |
n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmd), sizeof(jbyte)); |
2 | 946 |
if (n < (int)sizeof(jbyte)) { |
947 |
RETURN_RECV_ERROR(n); |
|
948 |
} |
|
949 |
} |
|
950 |
||
951 |
data_len = length - ((sizeof(jint) * 2) + (sizeof(jbyte) * 3)); |
|
952 |
||
953 |
if (data_len < 0) { |
|
954 |
setLastError(0, "Badly formed packet received - invalid length"); |
|
955 |
return JDWPTRANSPORT_ERROR_IO_ERROR; |
|
956 |
} else if (data_len == 0) { |
|
957 |
packet->type.cmd.data = NULL; |
|
958 |
} else { |
|
959 |
packet->type.cmd.data= (*callback->alloc)(data_len); |
|
960 |
||
961 |
if (packet->type.cmd.data == NULL) { |
|
962 |
RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory"); |
|
963 |
} |
|
964 |
||
965 |
n = recv_fully(socketFD,(char *)packet->type.cmd.data, data_len); |
|
966 |
if (n < data_len) { |
|
967 |
(*callback->free)(packet->type.cmd.data); |
|
968 |
RETURN_RECV_ERROR(n); |
|
969 |
} |
|
970 |
} |
|
971 |
||
972 |
return JDWPTRANSPORT_ERROR_NONE; |
|
973 |
} |
|
974 |
||
975 |
static jdwpTransportError JNICALL |
|
976 |
socketTransport_getLastError(jdwpTransportEnv* env, char** msgP) { |
|
977 |
char *msg = (char *)dbgsysTlsGet(tlsIndex); |
|
978 |
if (msg == NULL) { |
|
979 |
return JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE; |
|
980 |
} |
|
896
5c02031316bf
6725543: Compiler warnings in serviceability native code
ohair
parents:
2
diff
changeset
|
981 |
*msgP = (*callback->alloc)((int)strlen(msg)+1); |
2 | 982 |
if (*msgP == NULL) { |
983 |
return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY; |
|
984 |
} |
|
985 |
strcpy(*msgP, msg); |
|
986 |
return JDWPTRANSPORT_ERROR_NONE; |
|
987 |
} |
|
988 |
||
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
989 |
static jdwpTransportError JNICALL |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
990 |
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
|
991 |
const char* allowed_peers = NULL; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
992 |
|
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
993 |
if (cfg == NULL) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
994 |
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
|
995 |
"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
|
996 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
997 |
allowed_peers = cfg->allowed_peers; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
998 |
_peers_cnt = 0; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
999 |
if (allowed_peers != NULL) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1000 |
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
|
1001 |
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
|
1002 |
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
|
1003 |
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
|
1004 |
"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
|
1005 |
} else if (*allowed_peers == '*') { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1006 |
if (len != 1) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1007 |
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
|
1008 |
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
|
1009 |
"allow option '*' cannot be expanded"); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1010 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1011 |
} else { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1012 |
int err = parseAllowedPeers(allowed_peers); |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1013 |
if (err != JDWPTRANSPORT_ERROR_NONE) { |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1014 |
return err; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1015 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1016 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1017 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1018 |
return JDWPTRANSPORT_ERROR_NONE; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1019 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1020 |
|
36134 | 1021 |
jint JNICALL |
2 | 1022 |
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
|
1023 |
jint version, jdwpTransportEnv** env) |
2 | 1024 |
{ |
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1025 |
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
|
1026 |
version > JDWPTRANSPORT_VERSION_1_1) { |
2 | 1027 |
return JNI_EVERSION; |
1028 |
} |
|
1029 |
if (initialized) { |
|
1030 |
/* |
|
1031 |
* This library doesn't support multiple environments (yet) |
|
1032 |
*/ |
|
1033 |
return JNI_EEXIST; |
|
1034 |
} |
|
1035 |
initialized = JNI_TRUE; |
|
1036 |
jvm = vm; |
|
1037 |
callback = cbTablePtr; |
|
1038 |
||
1039 |
/* initialize interface table */ |
|
1040 |
interface.GetCapabilities = &socketTransport_getCapabilities; |
|
1041 |
interface.Attach = &socketTransport_attach; |
|
1042 |
interface.StartListening = &socketTransport_startListening; |
|
1043 |
interface.StopListening = &socketTransport_stopListening; |
|
1044 |
interface.Accept = &socketTransport_accept; |
|
1045 |
interface.IsOpen = &socketTransport_isOpen; |
|
1046 |
interface.Close = &socketTransport_close; |
|
1047 |
interface.ReadPacket = &socketTransport_readPacket; |
|
1048 |
interface.WritePacket = &socketTransport_writePacket; |
|
1049 |
interface.GetLastError = &socketTransport_getLastError; |
|
47121
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1050 |
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
|
1051 |
interface.SetTransportConfiguration = &socketTransport_setConfiguration; |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1052 |
} |
3aceb4fc0e84
8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents:
46091
diff
changeset
|
1053 |
*env = &single_env; |
2 | 1054 |
|
1055 |
/* initialized TLS */ |
|
1056 |
tlsIndex = dbgsysTlsAlloc(); |
|
1057 |
return JNI_OK; |
|
1058 |
} |