1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
import java.io.IOException;
import java.net.InetAddress;
import java.net.Inet6Address;
import java.util.Enumeration;
import java.util.concurrent.*;
import org.savarese.vserv.tcpip.*;
import org.savarese.rocksaw.net.RawSocket;
import static org.savarese.rocksaw.net.RawSocket.PF_INET;
import static org.savarese.rocksaw.net.RawSocket.PF_INET6;
import static org.savarese.rocksaw.net.RawSocket.getProtocolByName;
import static org.savarese.vserv.tcpip.ICMPPacket.OFFSET_ICMP_CHECKSUM;
/**
* <p>The Ping class is a simple demo showing how you can send
* ICMP echo requests and receive echo replies using raw sockets.
* It has been updated to work with both IPv4 and IPv6.</p>
*
* <p>Note, this is not a model of good programming. The point
* of the example is to show how the RawSocket API calls work. There
* is much kluginess surrounding the actual packet and protocol
* handling, all of which is outside of the scope of what RockSaw
* does.</p>
*
* @author <a href="http://www.savarese.org/">Daniel F. Savarese</a>
*/
public class Ping {
public static interface EchoReplyListener {
public void notifyEchoReply(ICMPEchoPacket packet,
byte[] data, int dataOffset,
byte[] srcAddress)
throws IOException;
}
public static class Pinger {
private static final int TIMEOUT = 10000;
protected RawSocket socket;
protected ICMPEchoPacket sendPacket, recvPacket;
protected int offset, length, dataOffset;
protected int requestType, replyType;
protected byte[] sendData, recvData, srcAddress;
protected int sequence, identifier;
protected EchoReplyListener listener;
protected Pinger(int id, int protocolFamily, int protocol)
throws IOException
{
sequence = 0;
identifier = id;
setEchoReplyListener(null);
sendPacket = new ICMPEchoPacket(1);
recvPacket = new ICMPEchoPacket(1);
sendData = new byte[84];
recvData = new byte[84];
sendPacket.setData(sendData);
recvPacket.setData(recvData);
sendPacket.setIPHeaderLength(5);
recvPacket.setIPHeaderLength(5);
sendPacket.setICMPDataByteLength(56);
recvPacket.setICMPDataByteLength(56);
offset = sendPacket.getIPHeaderByteLength();
dataOffset = offset + sendPacket.getICMPHeaderByteLength();
length = sendPacket.getICMPPacketByteLength();
socket = new RawSocket();
socket.open(protocolFamily, protocol);
try {
socket.setSendTimeout(TIMEOUT);
socket.setReceiveTimeout(TIMEOUT);
} catch(java.net.SocketException se) {
socket.setUseSelectTimeout(true);
socket.setSendTimeout(TIMEOUT);
socket.setReceiveTimeout(TIMEOUT);
}
}
public Pinger(int id) throws IOException {
this(id, PF_INET, getProtocolByName("icmp"));
srcAddress = new byte[4];
requestType = ICMPPacket.TYPE_ECHO_REQUEST;
replyType = ICMPPacket.TYPE_ECHO_REPLY;
}
protected void computeSendChecksum(InetAddress host)
throws IOException
{
sendPacket.computeICMPChecksum();
}
public void setEchoReplyListener(EchoReplyListener l) {
listener = l;
}
/**
* Closes the raw socket opened by the constructor. After calling
* this method, the object cannot be used.
*/
public void close() throws IOException {
socket.close();
}
public void sendEchoRequest(InetAddress host) throws IOException {
sendPacket.setType(requestType);
sendPacket.setCode(0);
sendPacket.setIdentifier(identifier);
sendPacket.setSequenceNumber(sequence++);
OctetConverter.longToOctets(System.nanoTime(), sendData, dataOffset);
computeSendChecksum(host);
socket.write(host, sendData, offset, length);
}
public void receive() throws IOException {
socket.read(recvData, srcAddress);
}
public void receiveEchoReply() throws IOException {
do {
receive();
} while(recvPacket.getType() != replyType ||
recvPacket.getIdentifier() != identifier);
if(listener != null)
listener.notifyEchoReply(recvPacket, recvData, dataOffset, srcAddress);
}
/**
* Issues a synchronous ping.
*
* @param host The host to ping.
* @return The round trip time in nanoseconds.
*/
public long ping(InetAddress host) throws IOException {
sendEchoRequest(host);
receiveEchoReply();
long end = System.nanoTime();
long start = OctetConverter.octetsToLong(recvData, dataOffset);
return (end - start);
}
/**
* @return The number of bytes in the data portion of the ICMP ping request
* packet.
*/
public int getRequestDataLength() {
return sendPacket.getICMPDataByteLength();
}
/** @return The number of bytes in the entire IP ping request packet. */
public int getRequestPacketLength() {
return sendPacket.getIPPacketLength();
}
}
public static class PingerIPv6 extends Pinger {
private static final int IPPROTO_ICMPV6 = 58;
private static final int ICMPv6_TYPE_ECHO_REQUEST = 128;
private static final int ICMPv6_TYPE_ECHO_REPLY = 129;
/**
* Operating system kernels are supposed to calculate the ICMPv6
* checksum for the sender, but Microsoft's IPv6 stack does not do
* this. Nor does it support the IPV6_CHECKSUM socket option.
* Therefore, in order to work on the Windows family of operating
* systems, we have to calculate the ICMPv6 checksum.
*/
private static class ICMPv6ChecksumCalculator extends IPPacket {
ICMPv6ChecksumCalculator() { super(1); }
private int computeVirtualHeaderTotal(byte[] destination, byte[] source,
int icmpLength)
{
int total = 0;
for(int i = 0; i < source.length;)
total+=(((source[i++] & 0xff) << 8) | (source[i++] & 0xff));
for(int i = 0; i < destination.length;)
total+=(((destination[i++] & 0xff) << 8) | (destination[i++] & 0xff));
total+=(icmpLength >>> 16);
total+=(icmpLength & 0xffff);
total+=IPPROTO_ICMPV6;
return total;
}
int computeChecksum(byte[] data, ICMPPacket packet, byte[] destination,
byte[] source)
{
int startOffset = packet.getIPHeaderByteLength();
int checksumOffset = startOffset + OFFSET_ICMP_CHECKSUM;
int ipLength = packet.getIPPacketLength();
int icmpLength = packet.getICMPPacketByteLength();
setData(data);
return
_computeChecksum_(startOffset, checksumOffset, ipLength,
computeVirtualHeaderTotal(destination, source,
icmpLength), true);
}
}
private byte[] localAddress;
private ICMPv6ChecksumCalculator icmpv6Checksummer;
public PingerIPv6(int id) throws IOException {
//socket.open(protocolFamily,
super(id, PF_INET6, IPPROTO_ICMPV6 /*getProtocolByName("ipv6-icmp")*/);
icmpv6Checksummer = new ICMPv6ChecksumCalculator();
srcAddress = new byte[16];
localAddress = new byte[16];
requestType = ICMPv6_TYPE_ECHO_REQUEST;
replyType = ICMPv6_TYPE_ECHO_REPLY;
}
protected void computeSendChecksum(InetAddress host)
throws IOException
{
// This is necessary only for Windows, which doesn't implement
// RFC 2463 correctly.
socket.getSourceAddressForDestination(host, localAddress);
icmpv6Checksummer.computeChecksum(sendData, sendPacket,
host.getAddress(), localAddress);
}
public void receive() throws IOException {
socket.read(recvData, offset, length, srcAddress);
}
}
public static final void main(String[] args) throws Exception {
if(args.length < 1 || args.length > 2) {
System.err.println("usage: Ping host [count]");
System.exit(1);
}
final ScheduledThreadPoolExecutor executor =
new ScheduledThreadPoolExecutor(2);
try{
final InetAddress address = InetAddress.getByName(args[0]);
final String hostname = address.getCanonicalHostName();
final String hostaddr = address.getHostAddress();
final int count;
// Ping programs usually use the process ID for the identifier,
// but we can't get it and this is only a demo.
final int id = 65535;
final Pinger ping;
if(args.length == 2)
count = Integer.parseInt(args[1]);
else
count = 5;
if(address instanceof Inet6Address)
ping = new Ping.PingerIPv6(id);
else
ping = new Ping.Pinger(id);
ping.setEchoReplyListener(new EchoReplyListener() {
StringBuffer buffer = new StringBuffer(128);
public void notifyEchoReply(ICMPEchoPacket packet,
byte[] data, int dataOffset,
byte[] srcAddress)
throws IOException
{
long end = System.nanoTime();
long start = OctetConverter.octetsToLong(data, dataOffset);
// Note: Java and JNI overhead will be noticeable (100-200
// microseconds) for sub-millisecond transmission times.
// The first ping may even show several seconds of delay
// because of initial JIT compilation overhead.
double rtt = (double)(end - start) / 1e6;
buffer.setLength(0);
buffer.append(packet.getICMPPacketByteLength())
.append(" bytes from ").append(hostname).append(" (");
buffer.append(InetAddress.getByAddress(srcAddress).toString());
buffer.append("): icmp_seq=")
.append(packet.getSequenceNumber())
.append(" ttl=").append(packet.getTTL()).append(" time=")
.append(rtt).append(" ms");
System.out.println(buffer.toString());
}
});
System.out.println("PING " + hostname + " (" + hostaddr + ") " +
ping.getRequestDataLength() + "(" +
ping.getRequestPacketLength() + ") bytes of data).");
final CountDownLatch latch = new CountDownLatch(1);
executor.scheduleAtFixedRate(new Runnable() {
int counter = count;
public void run() {
try {
if(counter > 0) {
ping.sendEchoRequest(address);
if(counter == count)
latch.countDown();
--counter;
} else
executor.shutdown();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}, 0, 1, TimeUnit.SECONDS);
// We wait for first ping to be sent because Windows times out
// with WSAETIMEDOUT if echo request hasn't been sent first.
// POSIX does the right thing and just blocks on the first receive.
// An alternative is to bind the socket first, which should allow a
// receive to be performed frst on Windows.
latch.await();
for(int i = 0; i < count; ++i)
ping.receiveEchoReply();
ping.close();
} catch(Exception e) {
executor.shutdown();
e.printStackTrace();
}
}
} |