Example #1
0
  public PingResult ping(ScanningSubject subject, int count) throws IOException {
    PingResult result = new PingResult(subject.getAddress());

    DatagramSocket socket = sockets.bind(new DatagramSocket());
    socket.setSoTimeout(timeout);
    socket.connect(subject.getAddress(), PROBE_UDP_PORT);

    for (int i = 0; i < count && !Thread.currentThread().isInterrupted(); i++) {
      long startTime = System.currentTimeMillis();
      byte[] payload = new byte[8];
      ByteBuffer.wrap(payload).putLong(startTime);
      DatagramPacket packet = new DatagramPacket(payload, payload.length);
      try {
        socket.send(packet);
        socket.receive(packet);
      } catch (PortUnreachableException e) {
        result.addReply(System.currentTimeMillis() - startTime);
      } catch (SocketTimeoutException ignore) {
      } catch (NoRouteToHostException e) {
        // this means that the host is down
        break;
      } catch (SocketException e) {
        if (e.getMessage().contains(/*No*/ "route to host")) {
          // sometimes 'No route to host' also gets here...
          break;
        }
      } catch (IOException e) {
        LOG.log(FINER, subject.toString(), e);
      }
    }
    return result;
  }
Example #2
0
  @Test
  public void requestedPortsAreDetected() throws Exception {
    StringReader reader =
        new StringReader("1.2.3.4:1234\n2.3.4.5:\n 7.6.5.4:789004\n 1.2.3.5:80  1.2.3.5:3128 ");
    FileFeeder fileFeeder = new FileFeeder(reader);

    assertEquals(1234, (int) fileFeeder.next().requestedPortsIterator().next());
    assertFalse(fileFeeder.next().isAnyPortRequested());
    assertFalse(fileFeeder.next().isAnyPortRequested());

    ScanningSubject lastSubject = fileFeeder.next();
    assertEquals("1.2.3.5", lastSubject.getAddress().getHostAddress());
    Iterator<Integer> portIterator = lastSubject.requestedPortsIterator();
    assertEquals(80, (int) portIterator.next());
    assertEquals(3128, (int) portIterator.next());
  }