mirror of
https://github.com/angryip/ipscan.git
synced 2025-10-26 11:18:17 +00:00
a little of code cleanup
This commit is contained in:
parent
21a202062b
commit
ab86fc0388
@ -5,6 +5,13 @@
|
||||
*/
|
||||
package net.azib.ipscan.core.net;
|
||||
|
||||
import net.azib.ipscan.core.ScanningSubject;
|
||||
import org.savarese.rocksaw.net.RawSocket;
|
||||
import org.savarese.vserv.tcpip.ICMPEchoPacket;
|
||||
import org.savarese.vserv.tcpip.ICMPPacket;
|
||||
import org.savarese.vserv.tcpip.IPPacket;
|
||||
import org.savarese.vserv.tcpip.OctetConverter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.net.InetAddress;
|
||||
@ -12,13 +19,7 @@ import java.net.UnknownHostException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.azib.ipscan.core.ScanningSubject;
|
||||
|
||||
import org.savarese.rocksaw.net.RawSocket;
|
||||
import org.savarese.vserv.tcpip.ICMPEchoPacket;
|
||||
import org.savarese.vserv.tcpip.ICMPPacket;
|
||||
import org.savarese.vserv.tcpip.IPPacket;
|
||||
import org.savarese.vserv.tcpip.OctetConverter;
|
||||
import static net.azib.ipscan.util.IOUtils.*;
|
||||
|
||||
/**
|
||||
* Pinging code is encapsulated here.
|
||||
@ -26,7 +27,6 @@ import org.savarese.vserv.tcpip.OctetConverter;
|
||||
* @author Anton Keks
|
||||
*/
|
||||
public class ICMPPinger implements Pinger {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ICMPPinger.class.getName());
|
||||
|
||||
private int timeout;
|
||||
@ -115,7 +115,6 @@ public class ICMPPinger implements Pinger {
|
||||
* @param count number of pings to perform
|
||||
*/
|
||||
public PingResult ping(ScanningSubject subject, int count) throws IOException {
|
||||
|
||||
PingResult result = new PingResult(subject.getAddress());
|
||||
RawSocket socket = createRawSocket();
|
||||
|
||||
@ -131,7 +130,7 @@ public class ICMPPinger implements Pinger {
|
||||
}
|
||||
}
|
||||
finally {
|
||||
socket.close();
|
||||
closeQuietly(socket);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -139,5 +138,4 @@ public class ICMPPinger implements Pinger {
|
||||
|
||||
public void close() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -5,39 +5,38 @@
|
||||
*/
|
||||
package net.azib.ipscan.core.net;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.azib.ipscan.core.ScanningSubject;
|
||||
|
||||
import org.savarese.rocksaw.net.RawSocket;
|
||||
import org.savarese.vserv.tcpip.ICMPEchoPacket;
|
||||
import org.savarese.vserv.tcpip.ICMPPacket;
|
||||
import org.savarese.vserv.tcpip.IPPacket;
|
||||
import org.savarese.vserv.tcpip.OctetConverter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.*;
|
||||
import static net.azib.ipscan.util.IOUtils.*;
|
||||
|
||||
/**
|
||||
* Shared multi-threaded pinger.
|
||||
*
|
||||
* @author Anton Keks
|
||||
*/
|
||||
public class ICMPSharedPinger implements Pinger {
|
||||
|
||||
static final Logger LOG = Logger.getLogger(ICMPSharedPinger.class.getName());
|
||||
|
||||
/** a single raw socket for sending of all ICMP packets */
|
||||
private RawSocket sendingSocket;
|
||||
private final RawSocket sendingSocket;
|
||||
/** a single raw socket for receiving of all ICMP packets */
|
||||
private RawSocket receivingSocket;
|
||||
private final RawSocket receivingSocket;
|
||||
/** the map with PingResults, keys are InetAddress */
|
||||
private Map<InetAddress, PingResult> results = Collections.synchronizedMap(new HashMap<InetAddress, PingResult>());
|
||||
private Map<InetAddress, PingResult> results = new ConcurrentHashMap<InetAddress, PingResult>();
|
||||
|
||||
private Thread receiverThread;
|
||||
|
||||
@ -78,7 +77,6 @@ public class ICMPSharedPinger implements Pinger {
|
||||
}
|
||||
|
||||
public PingResult ping(ScanningSubject subject, int count) throws IOException {
|
||||
|
||||
InetAddress address = subject.getAddress();
|
||||
PingResult result = new PingResult(address);
|
||||
results.put(address, result);
|
||||
@ -106,7 +104,7 @@ public class ICMPSharedPinger implements Pinger {
|
||||
OctetConverter.longToOctets(System.currentTimeMillis(), data, timeOffsetInPacket);
|
||||
packet.computeICMPChecksum();
|
||||
|
||||
if (LOG.isLoggable(Level.FINEST)) {
|
||||
if (LOG.isLoggable(FINEST)) {
|
||||
LOG.finest("Pinging " + i + result.address);
|
||||
}
|
||||
synchronized (sendingSocket) {
|
||||
@ -125,7 +123,7 @@ public class ICMPSharedPinger implements Pinger {
|
||||
|
||||
int totalTimeout = timeout * count;
|
||||
while (totalTimeout > 0 && result.getReplyCount() < count) {
|
||||
if (LOG.isLoggable(Level.FINEST)) {
|
||||
if (LOG.isLoggable(FINEST)) {
|
||||
LOG.finest("Waiting for response " + address + ": " + totalTimeout);
|
||||
}
|
||||
synchronized (result) {
|
||||
@ -133,12 +131,11 @@ public class ICMPSharedPinger implements Pinger {
|
||||
try {
|
||||
result.wait(timeout);
|
||||
}
|
||||
catch (InterruptedException e) {}
|
||||
catch (InterruptedException ignore) {}
|
||||
}
|
||||
totalTimeout -= timeout;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
finally {
|
||||
@ -172,7 +169,7 @@ public class ICMPSharedPinger implements Pinger {
|
||||
tmpAddress = InetAddress.getLocalHost();
|
||||
}
|
||||
catch (UnknownHostException e) {
|
||||
LOG.log(Level.SEVERE, null, e);
|
||||
LOG.log(SEVERE, null, e);
|
||||
}
|
||||
|
||||
try {
|
||||
@ -180,7 +177,7 @@ public class ICMPSharedPinger implements Pinger {
|
||||
receivingSocket.write(tmpAddress, data);
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.log(Level.WARNING, "Sending of test packet failed", e);
|
||||
LOG.log(WARNING, "Sending of test packet failed", e);
|
||||
}
|
||||
|
||||
do {
|
||||
@ -202,7 +199,7 @@ public class ICMPSharedPinger implements Pinger {
|
||||
long startTime = OctetConverter.octetsToLong(data, timeOffsetInPacket);
|
||||
long time = endTime - startTime;
|
||||
|
||||
if (LOG.isLoggable(Level.FINEST)) {
|
||||
if (LOG.isLoggable(FINEST)) {
|
||||
LOG.finest("Received " + packet.getSequenceNumber() + packet.getSourceAsInetAddress() + ": " + time);
|
||||
}
|
||||
|
||||
@ -227,21 +224,17 @@ public class ICMPSharedPinger implements Pinger {
|
||||
// TODO: make RawSocket to throw Exceptions without the stack trace (for speed)
|
||||
}
|
||||
catch (UnknownHostException e) {
|
||||
LOG.log(Level.WARNING, "Cannot retrieve the source address of an ICMP packet", e);
|
||||
LOG.log(WARNING, "Cannot retrieve the source address of an ICMP packet", e);
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.log(Level.WARNING, "Unable to read from the socket", e);
|
||||
LOG.log(WARNING, "Unable to read from the socket", e);
|
||||
}
|
||||
|
||||
}
|
||||
while(!interrupted());
|
||||
|
||||
try {
|
||||
receivingSocket.close();
|
||||
}
|
||||
catch (IOException e) { }
|
||||
|
||||
closeQuietly(receivingSocket);
|
||||
LOG.fine("Terminated");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ public class TCPPinger implements Pinger {
|
||||
result.enableTimeoutAdaptation();
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
public void close() {
|
||||
sockets.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ public class UDPPinger implements Pinger {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
public void close() {
|
||||
sockets.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ public class WindowsPinger implements Pinger {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
public void close() {
|
||||
// not needed in this pinger
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package net.azib.ipscan.util;
|
||||
|
||||
import org.savarese.rocksaw.net.RawSocket;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
@ -18,6 +20,14 @@ public class IOUtils {
|
||||
if (socket != null) socket.close();
|
||||
}
|
||||
|
||||
public static void closeQuietly(RawSocket socket) {
|
||||
if (socket != null) try {
|
||||
socket.close();
|
||||
}
|
||||
catch (IOException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeQuietly(Closeable closeable) {
|
||||
if (closeable != null) try {
|
||||
closeable.close();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user