blob: b24b2d4c84a4158e9b99fef46b89d5ae39cefd87 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25#include <windows.h>
26#include <winsock2.h>
27
28#include "sysSocket.h"
29#include "socketTransport.h"
30
31typedef jboolean bool_t;
32
33/*
34 * Table of Windows Sockets errors, the specific exception we
35 * throw for the error, and the error text.
36 *
37 * Note that this table excludes OS dependent errors.
38 */
39static struct {
40 int errCode;
41 const char *errString;
42} const winsock_errors[] = {
43 { WSAEPROVIDERFAILEDINIT, "Provider initialization failed (check %SystemRoot%)" },
44 { WSAEACCES, "Permission denied" },
45 { WSAEADDRINUSE, "Address already in use" },
46 { WSAEADDRNOTAVAIL, "Cannot assign requested address" },
47 { WSAEAFNOSUPPORT, "Address family not supported by protocol family" },
48 { WSAEALREADY, "Operation already in progress" },
49 { WSAECONNABORTED, "Software caused connection abort" },
50 { WSAECONNREFUSED, "Connection refused" },
51 { WSAECONNRESET, "Connection reset by peer" },
52 { WSAEDESTADDRREQ, "Destination address required" },
53 { WSAEFAULT, "Bad address" },
54 { WSAEHOSTDOWN, "Host is down" },
55 { WSAEHOSTUNREACH, "No route to host" },
56 { WSAEINPROGRESS, "Operation now in progress" },
57 { WSAEINTR, "Interrupted function call" },
58 { WSAEINVAL, "Invalid argument" },
59 { WSAEISCONN, "Socket is already connected" },
60 { WSAEMFILE, "Too many open files" },
61 { WSAEMSGSIZE, "The message is larger than the maximum supported by the underlying transport" },
62 { WSAENETDOWN, "Network is down" },
63 { WSAENETRESET, "Network dropped connection on reset" },
64 { WSAENETUNREACH, "Network is unreachable" },
65 { WSAENOBUFS, "No buffer space available (maximum connections reached?)" },
66 { WSAENOPROTOOPT, "Bad protocol option" },
67 { WSAENOTCONN, "Socket is not connected" },
68 { WSAENOTSOCK, "Socket operation on nonsocket" },
69 { WSAEOPNOTSUPP, "Operation not supported" },
70 { WSAEPFNOSUPPORT, "Protocol family not supported" },
71 { WSAEPROCLIM, "Too many processes" },
72 { WSAEPROTONOSUPPORT, "Protocol not supported" },
73 { WSAEPROTOTYPE, "Protocol wrong type for socket" },
74 { WSAESHUTDOWN, "Cannot send after socket shutdown" },
75 { WSAESOCKTNOSUPPORT, "Socket type not supported" },
76 { WSAETIMEDOUT, "Connection timed out" },
77 { WSATYPE_NOT_FOUND, "Class type not found" },
78 { WSAEWOULDBLOCK, "Resource temporarily unavailable" },
79 { WSAHOST_NOT_FOUND, "Host not found" },
80 { WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available" },
81 { WSANOTINITIALISED, "Successful WSAStartup not yet performed" },
82 { WSANO_DATA, "Valid name, no data record of requested type" },
83 { WSANO_RECOVERY, "This is a nonrecoverable error" },
84 { WSASYSNOTREADY, "Network subsystem is unavailable" },
85 { WSATRY_AGAIN, "Nonauthoritative host not found" },
86 { WSAVERNOTSUPPORTED, "Winsock.dll version out of range" },
87 { WSAEDISCON, "Graceful shutdown in progress" },
88 { WSA_OPERATION_ABORTED, "Overlapped operation aborted" },
89};
90
91
92/*
93 * Initialize Windows Sockets API support
94 */
95BOOL WINAPI
96DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
97{
98 WSADATA wsadata;
99
100 switch (reason) {
101 case DLL_PROCESS_ATTACH:
102 if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0) {
103 return FALSE;
104 }
105 break;
106
107 case DLL_PROCESS_DETACH:
108 WSACleanup();
109 break;
110
111 default:
112 break;
113 }
114 return TRUE;
115}
116
117/*
118 * If we get a nonnull function pointer it might still be the case
119 * that some other thread is in the process of initializing the socket
120 * function pointer table, but our pointer should still be good.
121 */
122int
123dbgsysListen(int fd, INT32 count) {
124 return listen(fd, (int)count);
125}
126
127int
128dbgsysConnect(int fd, struct sockaddr *name, int namelen) {
129 int rv = connect(fd, name, namelen);
130 if (rv == SOCKET_ERROR) {
131 if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK) {
132 return DBG_EINPROGRESS;
133 }
134 }
135 return rv;
136}
137
138int dbgsysFinishConnect(int fd, long timeout) {
139 int rv;
140 struct timeval t;
141 fd_set wr, ex;
142
143 t.tv_sec = timeout / 1000;
144 t.tv_usec = (timeout % 1000) * 1000;
145
146 FD_ZERO(&wr);
147 FD_ZERO(&ex);
148 FD_SET((unsigned int)fd, &wr);
149 FD_SET((unsigned int)fd, &ex);
150
151 rv = select(fd+1, 0, &wr, &ex, &t);
152 if (rv == 0) {
153 return SYS_ERR; /* timeout */
154 }
155
156 /*
157 * Check if there was an error - this is preferable to check if
158 * the socket is writable because some versions of Windows don't
159 * report a connected socket as being writable.
160 */
161 if (!FD_ISSET(fd, &ex)) {
162 return SYS_OK;
163 }
164
165 /*
166 * Unable to establish connection - to get the reason we must
167 * call getsockopt.
168 */
169 return SYS_ERR;
170}
171
172
173int
174dbgsysAccept(int fd, struct sockaddr *name, int *namelen) {
175 return accept(fd, name, namelen);
176}
177
178int
179dbgsysRecvFrom(int fd, char *buf, int nBytes,
180 int flags, struct sockaddr *from, int *fromlen) {
181 return recvfrom(fd, buf, nBytes, flags, from, fromlen);
182}
183
184int
185dbgsysSendTo(int fd, char *buf, int len,
186 int flags, struct sockaddr *to, int tolen) {
187 return sendto(fd, buf, len, flags, to, tolen);
188}
189
190int
191dbgsysRecv(int fd, char *buf, int nBytes, int flags) {
192 return recv(fd, buf, nBytes, flags);
193}
194
195int
196dbgsysSend(int fd, char *buf, int nBytes, int flags) {
197 return send(fd, buf, nBytes, flags);
198}
199
200struct hostent *
201dbgsysGetHostByName(char *hostname) {
202 return gethostbyname(hostname);
203}
204
205unsigned short
206dbgsysHostToNetworkShort(unsigned short hostshort) {
207 return htons(hostshort);
208}
209
210int
211dbgsysSocket(int domain, int type, int protocol) {
212 int fd = socket(domain, type, protocol);
213 if (fd != SOCKET_ERROR) {
214 SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE);
215 }
216 return fd;
217}
218
219int
220dbgsysSocketClose(int fd) {
221 struct linger l;
222 int len = sizeof(l);
223
224 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, &len) == 0) {
225 if (l.l_onoff == 0) {
226 WSASendDisconnect(fd, NULL);
227 }
228 }
229 return closesocket(fd);
230}
231
232INT32
233dbgsysSocketAvailable(int fd, INT32 *pbytes) {
234 u_long arg = (u_long)*pbytes;
235 return (INT32)ioctlsocket(fd, FIONREAD, &arg);
236}
237
238/* Additions to original follow */
239
240int
241dbgsysBind(int fd, struct sockaddr *name, int namelen) {
242 return bind(fd, name, namelen);
243}
244
245
246UINT32
247dbgsysInetAddr(const char* cp) {
248 return (UINT32)inet_addr(cp);
249}
250
251UINT32
252dbgsysHostToNetworkLong(UINT32 hostlong) {
253 return (UINT32)htonl((u_long)hostlong);
254}
255
256unsigned short
257dbgsysNetworkToHostShort(unsigned short netshort) {
258 return ntohs(netshort);
259}
260
261int
262dbgsysGetSocketName(int fd, struct sockaddr *name, int *namelen) {
263 return getsockname(fd, name, namelen);
264}
265
266UINT32
267dbgsysNetworkToHostLong(UINT32 netlong) {
268 return (UINT32)ntohl((u_long)netlong);
269}
270
271/*
272 * Below Adapted from PlainSocketImpl.c, win32 version 1.18. Changed exception
273 * throws to returns of SYS_ERR; we should improve the error codes
274 * eventually. Changed java objects to values the debugger back end can
275 * more easily deal with.
276 */
277
278int
279dbgsysSetSocketOption(int fd, jint cmd, jboolean on, jvalue value)
280{
281 if (cmd == TCP_NODELAY) {
282 struct protoent *proto = getprotobyname("TCP");
283 int tcp_level = (proto == 0 ? IPPROTO_TCP: proto->p_proto);
284 long onl = (long)on;
285
286 if (setsockopt(fd, tcp_level, TCP_NODELAY,
287 (char *)&onl, sizeof(long)) < 0) {
288 return SYS_ERR;
289 }
290 } else if (cmd == SO_LINGER) {
291 struct linger arg;
292 arg.l_onoff = on;
293
294 if(on) {
295 arg.l_linger = (unsigned short)value.i;
296 if(setsockopt(fd, SOL_SOCKET, SO_LINGER,
297 (char*)&arg, sizeof(arg)) < 0) {
298 return SYS_ERR;
299 }
300 } else {
301 if (setsockopt(fd, SOL_SOCKET, SO_LINGER,
302 (char*)&arg, sizeof(arg)) < 0) {
303 return SYS_ERR;
304 }
305 }
306 } else if (cmd == SO_SNDBUF) {
307 jint buflen = value.i;
308 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
309 (char *)&buflen, sizeof(buflen)) < 0) {
310 return SYS_ERR;
311 }
312 } else if (cmd == SO_REUSEADDR) {
313 /*
314 * On Windows the SO_REUSEADDR socket option doesn't implement
315 * BSD semantics. Specifically, the socket option allows multiple
316 * processes to bind to the same address/port rather than allowing
317 * a process to bind with a previous connection in the TIME_WAIT
318 * state. Hence on Windows we never enable this option for TCP
319 * option.
320 */
321 int sotype, arglen=sizeof(sotype);
322 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == SOCKET_ERROR) {
323 return SYS_ERR;
324 }
325 if (sotype != SOCK_STREAM) {
326 int oni = (int)on;
327 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
328 (char *)&oni, sizeof(oni)) == SOCKET_ERROR) {
329 return SYS_ERR;
330 }
331 }
332 } else {
333 return SYS_ERR;
334 }
335 return SYS_OK;
336}
337
338int dbgsysConfigureBlocking(int fd, jboolean blocking) {
339 u_long argp;
340 int result = 0;
341
342 if (blocking == JNI_FALSE) {
343 argp = 1;
344 } else {
345 argp = 0;
346 }
347 result = ioctlsocket(fd, FIONBIO, &argp);
348 if (result == SOCKET_ERROR) {
349 return SYS_ERR;
350 } else {
351 return SYS_OK;
352 }
353}
354
355int
356dbgsysPoll(int fd, jboolean rd, jboolean wr, long timeout) {
357 int rv;
358 struct timeval t;
359 fd_set rd_tbl, wr_tbl;
360
361 t.tv_sec = timeout / 1000;
362 t.tv_usec = (timeout % 1000) * 1000;
363
364 FD_ZERO(&rd_tbl);
365 if (rd) {
366 FD_SET((unsigned int)fd, &rd_tbl);
367 }
368
369 FD_ZERO(&wr_tbl);
370 if (wr) {
371 FD_SET((unsigned int)fd, &wr_tbl);
372 }
373
374 rv = select(fd+1, &rd_tbl, &wr_tbl, 0, &t);
375 if (rv >= 0) {
376 rv = 0;
377 if (FD_ISSET(fd, &rd_tbl)) {
378 rv |= DBG_POLLIN;
379 }
380 if (FD_ISSET(fd, &wr_tbl)) {
381 rv |= DBG_POLLOUT;
382 }
383 }
384 return rv;
385}
386
387int
388dbgsysGetLastIOError(char *buf, jint size) {
389 int table_size = sizeof(winsock_errors) /
390 sizeof(winsock_errors[0]);
391 int i;
392 int error = WSAGetLastError();
393
394 /*
395 * Check table for known winsock errors
396 */
397 i=0;
398 while (i < table_size) {
399 if (error == winsock_errors[i].errCode) {
400 break;
401 }
402 i++;
403 }
404
405 if (i < table_size) {
406 strcpy(buf, winsock_errors[i].errString);
407 } else {
408 sprintf(buf, "winsock error %d", error);
409 }
410 return 0;
411}
412
413
414int
415dbgsysTlsAlloc() {
416 return TlsAlloc();
417}
418
419void
420dbgsysTlsFree(int index) {
421 TlsFree(index);
422}
423
424void
425dbgsysTlsPut(int index, void *value) {
426 TlsSetValue(index, value);
427}
428
429void *
430dbgsysTlsGet(int index) {
431 return TlsGetValue(index);
432}
433
434#define FT2INT64(ft) \
435 ((long)(ft).dwHighDateTime << 32 | (long)(ft).dwLowDateTime)
436
437long
438dbgsysCurrentTimeMillis() {
439 static long fileTime_1_1_70 = 0; /* midnight 1/1/70 */
440 SYSTEMTIME st0;
441 FILETIME ft0;
442
443 /* initialize on first usage */
444 if (fileTime_1_1_70 == 0) {
445 memset(&st0, 0, sizeof(st0));
446 st0.wYear = 1970;
447 st0.wMonth = 1;
448 st0.wDay = 1;
449 SystemTimeToFileTime(&st0, &ft0);
450 fileTime_1_1_70 = FT2INT64(ft0);
451 }
452
453 GetSystemTime(&st0);
454 SystemTimeToFileTime(&st0, &ft0);
455
456 return (FT2INT64(ft0) - fileTime_1_1_70) / 10000;
457}