src/windows/native/java/net/Inet4AddressImpl.c
static jboolean
ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
      struct sockaddr_in* netif, jint ttl) {
    jint size;
    jint n, len, hlen1, icmplen;
    char sendbuf[1500];
    char recvbuf[1500];
    struct icmp *icmp;
    struct ip *ip;
    WSAEVENT hEvent;
    struct sockaddr sa_recv;
    jint tmout2;
    u_short pid, seq;
    int read_rv = 0;

    /* Initialize the sequence number to a suitable random number and
       shift right one place to allow sufficient room for increamenting. */
    seq = ((unsigned short)rand()) >> 1;

    /* icmp_id is a 16 bit data type, therefore down cast the pid */
    pid = (u_short) _getpid();
    size = 60*1024;
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *) &size, sizeof(size));
    /**
     * A TTL was specified, let's set the socket option.
     */
    if (ttl > 0) {
      setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *) &ttl, sizeof(ttl));
    }

    /**
     * A network interface was specified, let's bind to it.
     */
    if (netif != NULL) {
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
        closesocket(fd);
        return JNI_FALSE;
      }
    }

    /**
     * Let's make the socket non blocking
     */
    hEvent = WSACreateEvent();
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);

    /**
     * send 1 ICMP REQUEST every second until either we get a valid reply
     * or the timeout expired.
     */
    do {
      /**
       * construct the ICMP header
       */
      memset(sendbuf, 0, 1500);
      icmp = (struct icmp *) sendbuf;
      icmp->icmp_type = ICMP_ECHO;
      icmp->icmp_code = 0;
      icmp->icmp_id = htons(pid);
      icmp->icmp_seq = htons(seq);
      /**
       * checksum has to be set to zero before we can calculate the
       * real checksum!
       */
      icmp->icmp_cksum = 0;
      icmp->icmp_cksum = in_cksum((u_short *)icmp, 64);
      /**
       * Ping!
       */
      n = sendto(fd, sendbuf, 64, 0, (struct sockaddr *)him,
                 sizeof(struct sockaddr));
      if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
        NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet");
        closesocket(fd);
        WSACloseEvent(hEvent);
        return JNI_FALSE;
      }

      /*
       * wait for 1 second at most
       */
      tmout2 = timeout > 1000 ? 1000 : timeout;
      do {
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
        if (tmout2 >= 0) {
          len = sizeof(sa_recv);
          n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, &sa_recv, &len);
          ip = (struct ip*) recvbuf;
          hlen1 = (ip->ip_hl) << 2;
          icmp = (struct icmp *) (recvbuf + hlen1);
          icmplen = n - hlen1;
          /**
           * Is that a proper ICMP reply?
           */
          if (icmplen >= 8 && icmp->icmp_type == ICMP_ECHOREPLY &&
              (ntohs(icmp->icmp_seq) == seq) && (ntohs(icmp->icmp_id) == pid)) {
            closesocket(fd);
            WSACloseEvent(hEvent);
            return JNI_TRUE;
          }
        }
      } while (tmout2 > 0);
      timeout -= 1000;
      seq++;
    } while (timeout > 0);
    closesocket(fd);
    WSACloseEvent(hEvent);
    return JNI_FALSE;
}
/**
 * ping implementation.
 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
 * expires or a answer is received.
 * Returns true is an ECHO_REPLY is received, otherwise, false.
 */
ping implementation.
Send a ICMP_ECHO_REQUEST packet every second until either the timeout
expires or a answer is received.
Returns true is an ECHO_REPLY is received, otherwise, false.
ping implementation . Send a ICMP_ECHO_REQUEST packet every second until either the timeout expires or a answer is received . Returns true is an ECHO_REPLY is received otherwise false .
static jboolean
ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
      struct sockaddr_in* netif, jint ttl) {
    jint size;
    jint n, len, hlen1, icmplen;
    char sendbuf[1500];
    char recvbuf[1500];
    struct icmp *icmp;
    struct ip *ip;
    WSAEVENT hEvent;
    struct sockaddr sa_recv;
    jint tmout2;
    u_short pid, seq;
    int read_rv = 0;
    seq = ((unsigned short)rand()) >> 1;
    pid = (u_short) _getpid();
    size = 60*1024;
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *) &size, sizeof(size));
    if (ttl > 0) {
      setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *) &ttl, sizeof(ttl));
    }
    if (netif != NULL) {
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
        closesocket(fd);
        return JNI_FALSE;
      }
    }
    hEvent = WSACreateEvent();
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
    do {
      memset(sendbuf, 0, 1500);
      icmp = (struct icmp *) sendbuf;
      icmp->icmp_type = ICMP_ECHO;
      icmp->icmp_code = 0;
      icmp->icmp_id = htons(pid);
      icmp->icmp_seq = htons(seq);
      icmp->icmp_cksum = 0;
      icmp->icmp_cksum = in_cksum((u_short *)icmp, 64);
      n = sendto(fd, sendbuf, 64, 0, (struct sockaddr *)him,
                 sizeof(struct sockaddr));
      if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
        NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet");
        closesocket(fd);
        WSACloseEvent(hEvent);
        return JNI_FALSE;
      }
      tmout2 = timeout > 1000 ? 1000 : timeout;
      do {
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
        if (tmout2 >= 0) {
          len = sizeof(sa_recv);
          n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, &sa_recv, &len);
          ip = (struct ip*) recvbuf;
          hlen1 = (ip->ip_hl) << 2;
          icmp = (struct icmp *) (recvbuf + hlen1);
          icmplen = n - hlen1;
          if (icmplen >= 8 && icmp->icmp_type == ICMP_ECHOREPLY &&
              (ntohs(icmp->icmp_seq) == seq) && (ntohs(icmp->icmp_id) == pid)) {
            closesocket(fd);
            WSACloseEvent(hEvent);
            return JNI_TRUE;
          }
        }
      } while (tmout2 > 0);
      timeout -= 1000;
      seq++;
    } while (timeout > 0);
    closesocket(fd);
    WSACloseEvent(hEvent);
    return JNI_FALSE;
}
static jboolean ping4 ( JNIEnv * env , jint fd , struct sockaddr_in * him , jint timeout , struct sockaddr_in * netif , jint ttl ) { jint size ; jint n , len , hlen1 , icmplen ; char sendbuf [ 1500 ] ; char recvbuf [ 1500 ] ; struct icmp * icmp ; struct ip * ip ; WSAEVENT hEvent ; struct sockaddr sa_recv ; jint tmout2 ; u_short pid , seq ; int read_rv = 0 ; seq = ( ( unsigned short ) rand ( ) ) >> 1 ; pid = ( u_short ) _getpid ( ) ; size = 60 * 1024 ; setsockopt ( fd , SOL_SOCKET , SO_RCVBUF , ( const char * ) & size , sizeof ( size ) ) ; if ( ttl > 0 ) { setsockopt ( fd , IPPROTO_IP , IP_TTL , ( const char * ) & ttl , sizeof ( ttl ) ) ; } if ( netif != NULL ) { if ( bind ( fd , ( struct sockaddr * ) netif , sizeof ( struct sockaddr_in ) ) < 0 ) { NET_ThrowNew ( env , WSAGetLastError ( ) , " " ) ; closesocket ( fd ) ; return JNI_FALSE ; } } hEvent = WSACreateEvent ( ) ; WSAEventSelect ( fd , hEvent , FD_READ | FD_CONNECT | FD_CLOSE ) ; do { memset ( sendbuf , 0 , 1500 ) ; icmp = ( struct icmp * ) sendbuf ; icmp -> icmp_type = ICMP_ECHO ; icmp -> icmp_code = 0 ; icmp -> icmp_id = htons ( pid ) ; icmp -> icmp_seq = htons ( seq ) ; icmp -> icmp_cksum = 0 ; icmp -> icmp_cksum = in_cksum ( ( u_short * ) icmp , 64 ) ; n = sendto ( fd , sendbuf , 64 , 0 , ( struct sockaddr * ) him , sizeof ( struct sockaddr ) ) ; if ( n < 0 && WSAGetLastError ( ) != WSAEWOULDBLOCK ) { NET_ThrowNew ( env , WSAGetLastError ( ) , " " ) ; closesocket ( fd ) ; WSACloseEvent ( hEvent ) ; return JNI_FALSE ; } tmout2 = timeout > 1000 ? 1000 : timeout ; do { tmout2 = NET_Wait ( env , fd , NET_WAIT_READ , tmout2 ) ; if ( tmout2 >= 0 ) { len = sizeof ( sa_recv ) ; n = recvfrom ( fd , recvbuf , sizeof ( recvbuf ) , 0 , & sa_recv , & len ) ; ip = ( struct ip * ) recvbuf ; hlen1 = ( ip -> ip_hl ) << 2 ; icmp = ( struct icmp * ) ( recvbuf + hlen1 ) ; icmplen = n - hlen1 ; if ( icmplen >= 8 && icmp -> icmp_type == ICMP_ECHOREPLY && ( ntohs ( icmp -> icmp_seq ) == seq ) && ( ntohs ( icmp -> icmp_id ) == pid ) ) { closesocket ( fd ) ; WSACloseEvent ( hEvent ) ; return JNI_TRUE ; } } } while ( tmout2 > 0 ) ; timeout -= 1000 ; seq ++ ; } while ( timeout > 0 ) ; closesocket ( fd ) ; WSACloseEvent ( hEvent ) ; return JNI_FALSE ; }
ping implementation.
ping implementation .
/* Initialize the sequence number to a suitable random number and
       shift right one place to allow sufficient room for increamenting. */ /* icmp_id is a 16 bit data type, therefore down cast the pid */ /**
     * A TTL was specified, let's set the socket option.
     */ /**
     * A network interface was specified, let's bind to it.
     */ /**
     * Let's make the socket non blocking
     */ /**
     * send 1 ICMP REQUEST every second until either we get a valid reply
     * or the timeout expired.
     */ /**
       * construct the ICMP header
       */ /**
       * checksum has to be set to zero before we can calculate the
       * real checksum!
       */ /**
       * Ping!
       */ /*
       * wait for 1 second at most
       */ /**
           * Is that a proper ICMP reply?
           */