Kod programu :
package com.tracert;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;
import com.act365.net.GeneralSocketImpl;
import com.act365.net.IProtocolMessage;
import com.act365.net.JSWDatagramSocket;
import com.act365.net.SocketUtils;
import com.act365.net.SocketWrenchSession;
import com.act365.net.icmp.ICMPMessage;
import com.act365.net.ip.IP4Message;
import com.act365.net.udp.UDPMessage;
public class Traceroute {
public static final int ICMP_PROTOCOL = 1;
public static final int UDP_PROTOCOL = 17;
public static void main(String args[]) {
if (args.length == 0) {
System.err
.println("Usage: Traceroute -p protocol -l localhost -d -f first_ttl host");
System.exit(1);
}
String hostname = args[args.length - 1];
String localhost = null;
String protocollabel = null;
short ttl = 1;
boolean debug = false;
for (int i = 0; i < args.length - 1; i++) {
if (args[i].equals("-p") && i < args.length - 2)
protocollabel = args[++i];
else if (args[i].equals("-l") && i < args.length - 2)
localhost = args[++i];
else if (args[i].equals("-d"))
debug = true;
else if (args[i].equals("-f")) {
try {
ttl = Short.parseShort(args[++i]);
} catch (NumberFormatException e) {
System.err.println("Invalid TTL value");
System.exit(3);
}
} else {
System.err
.println("Usage: Traceroute -p protocol -l localhost -d -f first_ttl host");
System.exit(1);
}
}
int protocol = ICMP_PROTOCOL;
if (protocollabel instanceof String) {
if (protocollabel.equalsIgnoreCase("ICMP"))
protocol = ICMP_PROTOCOL;
else if (protocollabel.equalsIgnoreCase("UDP")) {
protocol = UDP_PROTOCOL;
} else {
System.err.println("Unsupported protocol");
System.exit(2);
}
}
InetAddress hostaddr = null;
InetAddress localaddr = null;
try {
hostaddr = InetAddress.getByName(hostname);
if (localhost instanceof String) {
localaddr = InetAddress.getByName(localhost);
}
} catch (UnknownHostException e) {
System.err.println(e.getMessage());
System.exit(5);
}
new Traceroute(protocol, hostaddr, localaddr, ttl, debug);
}
public Traceroute(int protocol, InetAddress hostaddr,
InetAddress localaddr, int ttl, boolean debug) {
new SocketWrenchSession();
try {
SocketWrenchSession.setProtocol(2);
} catch (IOException e) {
System.err.println("Unsupported protocol");
System.exit(4);
}
JSWDatagramSocket socket = null;
try {
socket = new JSWDatagramSocket();
socket.setTypeOfService(0);
if (localaddr instanceof InetAddress)
socket.setSourceAddress(localaddr.getAddress());
if (debug)
socket.setDebug(System.err);
} catch (SocketException e) {
System.err.println(e.getMessage());
System.exit(7);
}
short identifier = ICMPMessage.icmpIdentifier = (short) hashCode();
try {
byte timebuffer[] = new byte[8];
short recIdentifier = 0;
IP4Message ip4Message = new IP4Message();
ICMPMessage icmpMessage = new ICMPMessage();
IProtocolMessage message = null;
int sourceport = 42000;
int destinationport = 64000;
socket.setSoTimeout(3000);
for (; protocol == 1 && icmpMessage.isQuery()
&& identifier != recIdentifier || icmpMessage.type != 0
&& icmpMessage.code != 3; ttl++) {
socket.setTimeToLive(ttl);
SocketUtils.longToBytes((new Date()).getTime(), timebuffer, 0);
switch (protocol) {
case 1:
message = new ICMPMessage((byte) 8, (byte) 0, timebuffer,
0, timebuffer.length);
break;
case 17:
message = new UDPMessage((short) sourceport,
(short) (destinationport++), timebuffer, 0,
timebuffer.length);
socket.setSourcePort(sourceport++);
break;
}
socket.send(message, hostaddr);
boolean breaked = false;
do {
try {
socket.receive(ip4Message, icmpMessage);
recIdentifier = icmpMessage.identifier;
} catch (InterruptedIOException e) {
recIdentifier = 0;
System.out.println(ttl + ". *.*.*.*/*.*.*.*");
breaked = true;
break;
}
} while (protocol == 1 && icmpMessage.isQuery()
&& recIdentifier != identifier
|| icmpMessage.type != 11 && icmpMessage.type != 0
&& icmpMessage.code != 3);
if (breaked) {
continue;
}
System.out.println(ttl
+ ". "
+ GeneralSocketImpl.createInetAddress(2,
ip4Message.source));
}
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(8);
}
}
}