279 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout |
281 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout |
280 * expires or a answer is received. |
282 * expires or a answer is received. |
281 * Returns true is an ECHO_REPLY is received, otherwise, false. |
283 * Returns true is an ECHO_REPLY is received, otherwise, false. |
282 */ |
284 */ |
283 static jboolean |
285 static jboolean |
284 ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout, |
286 ping4(JNIEnv *env, unsigned long ipaddr, jint timeout) { |
285 struct sockaddr_in* netif, jint ttl) { |
287 |
286 jint size; |
288 // See https://msdn.microsoft.com/en-us/library/aa366050%28VS.85%29.aspx |
287 jint n, len, hlen1, icmplen; |
289 |
288 char sendbuf[1500]; |
290 HANDLE hIcmpFile; |
289 char recvbuf[1500]; |
291 DWORD dwRetVal = 0; |
290 struct icmp *icmp; |
292 char SendData[32] = {0}; |
291 struct ip *ip; |
293 LPVOID ReplyBuffer = NULL; |
292 WSAEVENT hEvent; |
294 DWORD ReplySize = 0; |
293 struct sockaddr sa_recv; |
295 |
294 jint tmout2; |
296 hIcmpFile = IcmpCreateFile(); |
295 u_short pid, seq; |
297 if (hIcmpFile == INVALID_HANDLE_VALUE) { |
296 int read_rv = 0; |
298 NET_ThrowNew(env, WSAGetLastError(), "Unable to open handle"); |
297 |
|
298 /* Initialize the sequence number to a suitable random number and |
|
299 shift right one place to allow sufficient room for increamenting. */ |
|
300 seq = ((unsigned short)rand()) >> 1; |
|
301 |
|
302 /* icmp_id is a 16 bit data type, therefore down cast the pid */ |
|
303 pid = (u_short) _getpid(); |
|
304 size = 60*1024; |
|
305 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *) &size, sizeof(size)); |
|
306 /** |
|
307 * A TTL was specified, let's set the socket option. |
|
308 */ |
|
309 if (ttl > 0) { |
|
310 setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *) &ttl, sizeof(ttl)); |
|
311 } |
|
312 |
|
313 /** |
|
314 * A network interface was specified, let's bind to it. |
|
315 */ |
|
316 if (netif != NULL) { |
|
317 if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) { |
|
318 NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket"); |
|
319 closesocket(fd); |
|
320 return JNI_FALSE; |
299 return JNI_FALSE; |
321 } |
300 } |
322 } |
301 |
323 |
302 ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData); |
324 /** |
303 ReplyBuffer = (VOID*) malloc(ReplySize); |
325 * Let's make the socket non blocking |
304 if (ReplyBuffer == NULL) { |
326 */ |
305 IcmpCloseHandle(hIcmpFile); |
327 hEvent = WSACreateEvent(); |
306 NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory"); |
328 WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE); |
|
329 |
|
330 /** |
|
331 * send 1 ICMP REQUEST every second until either we get a valid reply |
|
332 * or the timeout expired. |
|
333 */ |
|
334 do { |
|
335 /** |
|
336 * construct the ICMP header |
|
337 */ |
|
338 memset(sendbuf, 0, 1500); |
|
339 icmp = (struct icmp *) sendbuf; |
|
340 icmp->icmp_type = ICMP_ECHO; |
|
341 icmp->icmp_code = 0; |
|
342 icmp->icmp_id = htons(pid); |
|
343 icmp->icmp_seq = htons(seq); |
|
344 /** |
|
345 * checksum has to be set to zero before we can calculate the |
|
346 * real checksum! |
|
347 */ |
|
348 icmp->icmp_cksum = 0; |
|
349 icmp->icmp_cksum = in_cksum((u_short *)icmp, 64); |
|
350 /** |
|
351 * Ping! |
|
352 */ |
|
353 n = sendto(fd, sendbuf, 64, 0, (struct sockaddr *)him, |
|
354 sizeof(struct sockaddr)); |
|
355 if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) { |
|
356 NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet"); |
|
357 closesocket(fd); |
|
358 WSACloseEvent(hEvent); |
|
359 return JNI_FALSE; |
307 return JNI_FALSE; |
360 } |
308 } |
361 |
309 |
362 /* |
310 dwRetVal = IcmpSendEcho(hIcmpFile, // HANDLE IcmpHandle, |
363 * wait for 1 second at most |
311 ipaddr, // IPAddr DestinationAddress, |
364 */ |
312 SendData, // LPVOID RequestData, |
365 tmout2 = timeout > 1000 ? 1000 : timeout; |
313 sizeof(SendData), // WORD RequestSize, |
366 do { |
314 NULL, // PIP_OPTION_INFORMATION RequestOptions, |
367 tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2); |
315 ReplyBuffer,// LPVOID ReplyBuffer, |
368 if (tmout2 >= 0) { |
316 ReplySize, // DWORD ReplySize, |
369 len = sizeof(sa_recv); |
317 timeout); // DWORD Timeout |
370 n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, &sa_recv, &len); |
318 |
371 ip = (struct ip*) recvbuf; |
319 free(ReplyBuffer); |
372 hlen1 = (ip->ip_hl) << 2; |
320 IcmpCloseHandle(hIcmpFile); |
373 icmp = (struct icmp *) (recvbuf + hlen1); |
321 |
374 icmplen = n - hlen1; |
322 if (dwRetVal != 0) { |
375 /** |
323 return JNI_TRUE; |
376 * Is that a proper ICMP reply? |
324 } else { |
377 */ |
325 return JNI_FALSE; |
378 if (icmplen >= 8 && icmp->icmp_type == ICMP_ECHOREPLY && |
326 } |
379 (ntohs(icmp->icmp_seq) == seq) && (ntohs(icmp->icmp_id) == pid)) { |
|
380 closesocket(fd); |
|
381 WSACloseEvent(hEvent); |
|
382 return JNI_TRUE; |
|
383 } |
|
384 } |
|
385 } while (tmout2 > 0); |
|
386 timeout -= 1000; |
|
387 seq++; |
|
388 } while (timeout > 0); |
|
389 closesocket(fd); |
|
390 WSACloseEvent(hEvent); |
|
391 return JNI_FALSE; |
|
392 } |
327 } |
393 |
328 |
394 /* |
329 /* |
395 * Class: java_net_Inet4AddressImpl |
330 * Class: java_net_Inet4AddressImpl |
396 * Method: isReachable0 |
331 * Method: isReachable0 |
426 addr = ((caddr[0]<<24) & 0xff000000); |
355 addr = ((caddr[0]<<24) & 0xff000000); |
427 addr |= ((caddr[1] <<16) & 0xff0000); |
356 addr |= ((caddr[1] <<16) & 0xff0000); |
428 addr |= ((caddr[2] <<8) & 0xff00); |
357 addr |= ((caddr[2] <<8) & 0xff00); |
429 addr |= (caddr[3] & 0xff); |
358 addr |= (caddr[3] & 0xff); |
430 addr = htonl(addr); |
359 addr = htonl(addr); |
431 /** |
360 |
432 * Socket address |
361 return ping4(env, addr, timeout); |
433 */ |
362 } |
434 him.sin_addr.s_addr = addr; |
|
435 him.sin_family = AF_INET; |
|
436 len = sizeof(him); |
|
437 |
|
438 /** |
|
439 * If a network interface was specified, let's convert its address |
|
440 * as well. |
|
441 */ |
|
442 if (!(IS_NULL(ifArray))) { |
|
443 memset((char *) caddr, 0, sizeof(caddr)); |
|
444 (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr); |
|
445 addr = ((caddr[0]<<24) & 0xff000000); |
|
446 addr |= ((caddr[1] <<16) & 0xff0000); |
|
447 addr |= ((caddr[2] <<8) & 0xff00); |
|
448 addr |= (caddr[3] & 0xff); |
|
449 addr = htonl(addr); |
|
450 inf.sin_addr.s_addr = addr; |
|
451 inf.sin_family = AF_INET; |
|
452 inf.sin_port = 0; |
|
453 netif = &inf; |
|
454 } |
|
455 |
|
456 #if 0 |
|
457 /* |
|
458 * Windows implementation of ICMP & RAW sockets is too unreliable for now. |
|
459 * Therefore it's best not to try it at all and rely only on TCP |
|
460 * We may revisit and enable this code in the future. |
|
461 */ |
|
462 |
|
463 /* |
|
464 * Let's try to create a RAW socket to send ICMP packets |
|
465 * This usually requires "root" privileges, so it's likely to fail. |
|
466 */ |
|
467 fd = NET_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); |
|
468 if (fd != -1) { |
|
469 /* |
|
470 * It didn't fail, so we can use ICMP_ECHO requests. |
|
471 */ |
|
472 return ping4(env, fd, &him, timeout, netif, ttl); |
|
473 } |
|
474 #endif |
|
475 |
|
476 /* |
|
477 * Can't create a raw socket, so let's try a TCP socket |
|
478 */ |
|
479 fd = NET_Socket(AF_INET, SOCK_STREAM, 0); |
|
480 if (fd == SOCKET_ERROR) { |
|
481 /* note: if you run out of fds, you may not be able to load |
|
482 * the exception class, and get a NoClassDefFoundError |
|
483 * instead. |
|
484 */ |
|
485 NET_ThrowNew(env, WSAGetLastError(), "Can't create socket"); |
|
486 return JNI_FALSE; |
|
487 } |
|
488 if (ttl > 0) { |
|
489 setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl)); |
|
490 } |
|
491 /* |
|
492 * A network interface was specified, so let's bind to it. |
|
493 */ |
|
494 if (netif != NULL) { |
|
495 if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) { |
|
496 NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket"); |
|
497 closesocket(fd); |
|
498 return JNI_FALSE; |
|
499 } |
|
500 } |
|
501 |
|
502 /* |
|
503 * Make the socket non blocking so we can use select/poll. |
|
504 */ |
|
505 hEvent = WSACreateEvent(); |
|
506 WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE); |
|
507 |
|
508 /* no need to use NET_Connect as non-blocking */ |
|
509 him.sin_port = htons(7); /* Echo */ |
|
510 connect_rv = connect(fd, (struct sockaddr *)&him, len); |
|
511 |
|
512 /** |
|
513 * connection established or refused immediately, either way it means |
|
514 * we were able to reach the host! |
|
515 */ |
|
516 if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) { |
|
517 WSACloseEvent(hEvent); |
|
518 closesocket(fd); |
|
519 return JNI_TRUE; |
|
520 } else { |
|
521 int optlen; |
|
522 |
|
523 switch (WSAGetLastError()) { |
|
524 case WSAEHOSTUNREACH: /* Host Unreachable */ |
|
525 case WSAENETUNREACH: /* Network Unreachable */ |
|
526 case WSAENETDOWN: /* Network is down */ |
|
527 case WSAEPFNOSUPPORT: /* Protocol Family unsupported */ |
|
528 WSACloseEvent(hEvent); |
|
529 closesocket(fd); |
|
530 return JNI_FALSE; |
|
531 } |
|
532 |
|
533 if (WSAGetLastError() != WSAEWOULDBLOCK) { |
|
534 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", |
|
535 "connect failed"); |
|
536 WSACloseEvent(hEvent); |
|
537 closesocket(fd); |
|
538 return JNI_FALSE; |
|
539 } |
|
540 |
|
541 timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout); |
|
542 |
|
543 /* has connection been established */ |
|
544 |
|
545 if (timeout >= 0) { |
|
546 optlen = sizeof(connect_rv); |
|
547 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv, |
|
548 &optlen) <0) { |
|
549 connect_rv = WSAGetLastError(); |
|
550 } |
|
551 |
|
552 if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) { |
|
553 WSACloseEvent(hEvent); |
|
554 closesocket(fd); |
|
555 return JNI_TRUE; |
|
556 } |
|
557 } |
|
558 } |
|
559 WSACloseEvent(hEvent); |
|
560 closesocket(fd); |
|
561 return JNI_FALSE; |
|
562 } |
|