Examples of ICMPEchoPacket


Examples of org.savarese.vserv.tcpip.ICMPEchoPacket

    InetAddress address = subject.getAddress();
    PingResult result = new PingResult(address);
    results.put(address, result);
   
    // TODO: make ICMPEchoPacket accept byte array in the constructor
    ICMPEchoPacket packet = new ICMPEchoPacket(1);
    byte[] data = new byte[84];
    packet.setData(data);
    packet.setIPHeaderLength(5);
    packet.setICMPDataByteLength(56);
    packet.setType(ICMPPacket.TYPE_ECHO_REQUEST);
    packet.setCode(0);
    packet.setIdentifier(hashCode() & 0xFFFF); // some identification stuff
   
    try {
      // send a bunch of packets
      // note: we send sequence numbers starting from 1 (this is used by the ReceiverThread)
      for (int i = 1; i <= count  && !Thread.currentThread().isInterrupted(); i++) {
        packet.setSequenceNumber(i);
       
        int offset = packet.getIPHeaderByteLength();
        timeOffsetInPacket = offset + packet.getICMPHeaderByteLength();
        int length = packet.getICMPPacketByteLength();
       
        OctetConverter.longToOctets(System.currentTimeMillis(), data, timeOffsetInPacket);
        packet.computeICMPChecksum();
       
        if (LOG.isLoggable(FINEST)) {
          LOG.finest("Pinging " + i + result.address);
        }
        synchronized (sendingSocket) {
View Full Code Here

Examples of org.savarese.vserv.tcpip.ICMPEchoPacket

      setDaemon(true);
      setPriority(Thread.MAX_PRIORITY);
    }

    public void run() {
      ICMPEchoPacket packet = new ICMPEchoPacket(1);
      byte[] data = new byte[84];
      packet.setData(data);
      packet.setIPHeaderLength(5);
      packet.setICMPDataByteLength(56);
     
      // we use this address for receiving
      // due to some reason, raw sockets return packets coming from any addresses anyway
      InetAddress tmpAddress = null;
      try {
        tmpAddress = InetAddress.getLocalHost();
      }
      catch (UnknownHostException e) {
        LOG.log(SEVERE, null, e);
      }
     
      try {
        // Windows OS cannot read from a raw socket before anything has been sent through it
        receivingSocket.write(tmpAddress, data);
      }
      catch (IOException e) {
        LOG.log(WARNING, "Sending of test packet failed", e);
      }
     
      do {
        try {
          receivingSocket.read(tmpAddress, data);
         
          if (packet.getType() == ICMPPacket.TYPE_ECHO_REPLY &&
            packet.getIdentifier() == (ICMPSharedPinger.this.hashCode() & 0xFFFF) &&
            packet.getSequenceNumber() > 0) {
           
            long endTime = System.currentTimeMillis();
           
            PingResult result = results.get(packet.getSourceAsInetAddress());
            if (result == null) {
              LOG.warning("ICMP packet received from an unknown address: " + packet.getSourceAsInetAddress());
              continue;
            }
           
            long startTime = OctetConverter.octetsToLong(data, timeOffsetInPacket);
            long time = endTime - startTime;
           
            if (LOG.isLoggable(FINEST)) {
              LOG.finest("Received " + packet.getSequenceNumber() + packet.getSourceAsInetAddress() + ": " + time);
            }

            result.addReply(time);
            // TTL should be the same among all packets
            result.setTTL(packet.getTTL() & 0xFF);
           
            synchronized (result) {
              // notify the sender that we have an answer :-)
              result.notifyAll();
            }
          }
          else
          if (packet.getType() == ICMPPacket.TYPE_HOST_UNREACHABLE) {
            // TODO: received non-ECHO_REPLY packets may also be useful, saying "destination is unreachable"
            // packet body in this case is the sent ICMP_REQUEST packet
          }
        }
        catch (InterruptedIOException e) {
View Full Code Here

Examples of org.savarese.vserv.tcpip.ICMPEchoPacket

    return socket;
  }

  private void sendReceiveEchoPacket(RawSocket socket, InetAddress address, int sequence, PingResult result) throws IOException {   
   
    ICMPEchoPacket packet = new ICMPEchoPacket(1);
    byte[] data = new byte[84];
    packet.setData(data);
    packet.setIPHeaderLength(5);
    packet.setICMPDataByteLength(56);
    packet.setType(ICMPPacket.TYPE_ECHO_REQUEST);
    packet.setCode(0);
    packet.setIdentifier(hashCode() & 0xFFFF); // some identification stuff
    packet.setSequenceNumber(sequence);

    int offset = packet.getIPHeaderByteLength();
    int dataOffset = offset + packet.getICMPHeaderByteLength();
    int length = packet.getICMPPacketByteLength();

    OctetConverter.longToOctets(System.currentTimeMillis(), data, dataOffset);
    packet.computeICMPChecksum();

    socket.write(address, data, offset, length);

    try {
      int skippedCount = 0;
      do {
        socket.read(address, data);
        skippedCount++;
        //if (packet.getType() == ICMPPacket.TYPE_ECHO_REPLY)
        //  System.err.println(Thread.currentThread() + " " + packet.getSourceAsInetAddress().getHostAddress() + ": " + skippedCount);
      }
      while (packet.getType() != ICMPPacket.TYPE_ECHO_REPLY ||
          packet.getIdentifier() != (hashCode() & 0xFFFF) ||
          packet.getSequenceNumber() != sequence);

      if (packet.getSourceAsInetAddress().equals(address)) {
        long end = System.currentTimeMillis();
        long start = OctetConverter.octetsToLong(data, dataOffset);
        long time = end - start;
       
        result.addReply(time);
        result.setTTL(packet.getTTL() & 0xFF);
      }
    }
    catch (InterruptedIOException e) {
      // socket read timeout
      LOG.finer("Receive timeout");
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.