Java源码示例:jpcap.packet.IPPacket
示例1
/**
* @return
*/
private ICMPPacket createPacket(final InetAddress destIp, final short hop) {
final ICMPPacket packet = new ICMPPacket();
packet.type = ICMPPacket.ICMP_ECHO;
packet.seq = 100;
packet.id = (short) RandomUtils.nextInt(0, 100);
packet.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 0, 0, IPPacket.IPPROTO_ICMP, _deviceIp, destIp);
final String data = "ovtr";
packet.data = data.getBytes();
final EthernetPacket ether = new EthernetPacket();
ether.frametype = EthernetPacket.ETHERTYPE_IP;
ether.src_mac = _device.mac_address;
ether.dst_mac = _gatewayMac;
packet.datalink = ether;
packet.hop_limit = hop;
return packet;
}
示例2
private List<IPPacket> fragmentData(byte[] data, EthernetPacket eth)
{
List<IPPacket> ret = new ArrayList<IPPacket>();
short proto = (short) (data[9]&0xff);
byte[] IPSRC = new byte[] {data[12], data[13], data[14], data[15]};
byte[] IPDST = new byte[] {data[16], data[17], data[18], data[19]};
IPPacket p = new IPPacket();
IPPacket p2 = new IPPacket();
try {
p.setIPv4Parameter(0, false, false, false, 0, false, false, true, 0, 777, 128, proto, InetAddress.getByAddress(IPSRC), InetAddress.getByAddress(IPDST));
p2.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 777, 128, proto, InetAddress.getByAddress(IPSRC), InetAddress.getByAddress(IPDST));
} catch (Exception e) {}
p.ident = 777;
p2.ident = 777;
p2.offset = (short) 0xb900;
p.offset = 0;
p.datalink = eth;
p2.datalink = eth;
byte[] tmp = new byte[1480];
int j = 0;
for (int i = 20; i < 1500; i++)
tmp[j++] = data[i];
p.data = tmp;
tmp = null;
tmp = new byte[data.length - 1500];
j = 0;
for (int i = 1500; i < data.length; i++)
tmp[j++] = data[i];
p2.data = tmp;
ret.add(p);
ret.add(p2);
return ret;
}
示例3
private static void udp() throws Exception {
int sourcePort = 42000;
final int destinationPort = 33434;
final InetAddress add = InetAddress.getByName(Util.getPublicIp());
final NetworkInterface device = JpcapCaptor.getDeviceList()[1];
final JpcapCaptor captor = JpcapCaptor.openDevice(device, 65535, false, 100);
final InetAddress pingAddr = InetAddress.getByName(TEST_URL);
final JpcapSender sender = captor.getJpcapSenderInstance();
final InetAddress thisIP = JpcapTraceRoute.getIpOfDevice(device);
final InetAddress dest = InetAddress.getByName(TEST_URL);
short ttl = 1;
for (int i = 0; i < 10; i++) {
captor.setFilter("icmp and dst host " + thisIP.getHostAddress(), true);
// captor.setFilter("port " + destinationPort, true);
// captor.setFilter("icmp", true);
final UDPPacket p = new UDPPacket(sourcePort++, destinationPort);
// p.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 0, 0, IPPacket.IPPROTO_ICMP,
// thisIP, InetAddress.getByName("www.google.com"));
p.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 0, ttl++, IPPacket.IPPROTO_UDP,
thisIP, dest);
p.data = "ping".getBytes();
final EthernetPacket ether = new EthernetPacket();
ether.frametype = EthernetPacket.ETHERTYPE_IP;
ether.src_mac = new byte[] { (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
ether.dst_mac = new byte[] { (byte) 0, (byte) 6, (byte) 7, (byte) 8, (byte) 9, (byte) 10 };
p.datalink = ether;
System.out.println("Send " + p);
Packet ret = null;
while (ret == null) {
sender.sendPacket(p);
ret = captor.getPacket();
System.out.println("Got " + ret);
}
}
}
示例4
/**
* @param tpt
*/
private void print(final IPPacket tpt, final int c) {
System.out.println("Packet " + c);
final int ppp = tpt.protocol;
System.out.println("dest ip :" + tpt.dst_ip);
System.out.println("source ip :" + tpt.src_ip);
System.out.println("hop limit :" + tpt.hop_limit);
System.out.println("identification field :" + tpt.ident);
System.out.println("packet length :" + tpt.length);
System.out.println("packet priority :" + (int) tpt.priority);
System.out.println("type of service field" + tpt.rsv_tos);
if (tpt.r_flag) {
System.out.println("reliable transmission");
} else {
System.out.println("not reliable");
}
System.out.println("protocol version : " + (int) tpt.version);
System.out.println("flow label field" + tpt.flow_label);
final DatalinkPacket dp = tpt.datalink;
final EthernetPacket ept = (EthernetPacket) dp;
System.out.println("Destination mac address :" + ept.getDestinationAddress());
System.out.println("Source mac address" + ept.getSourceAddress());
if (ppp == IPPacket.IPPROTO_TCP) {
System.out.println("TCP packet");
final TCPPacket tp = (TCPPacket) tpt;
System.out.println("destination port of tcp :" + tp.dst_port);
if (tp.ack) {
System.out.println("acknowledgement");
} else {
System.out.println("not an acknowledgment packet");
}
if (tp.rst) {
System.out.println("reset connection ");
}
System.out.println("protocol version is :" + tp.version);
System.out.println("destination ip " + tp.dst_ip);
System.out.println("source ip" + tp.src_ip);
if (tp.fin) {
System.out.println("sender does not have more data to transfer");
}
if (tp.syn) {
System.out.println("request for connection");
}
} else if (ppp == IPPacket.IPPROTO_ICMP) {
final ICMPPacket ipc = (ICMPPacket) tpt;
System.out.println("ICMP packet");
// java.net.InetAddress[] routers=ipc.router_ip;
//for(int t=0;t
// System.out.println("\n"+routers[t]);
// }
System.out.println("alive time :" + ipc.alive_time);
System.out.println("number of advertised address :" + (int) ipc.addr_num);
System.out.println("mtu of the packet is :" + (int) ipc.mtu);
System.out.println("subnet mask :" + ipc.subnetmask);
System.out.println("source ip :" + ipc.src_ip);
System.out.println("destination ip:" + ipc.dst_ip);
System.out.println("check sum :" + ipc.checksum);
System.out.println("icmp type :" + ipc.type);
} else if (ppp == IPPacket.IPPROTO_UDP) {
final UDPPacket pac = (UDPPacket) tpt;
System.out.println("UDP packet");
System.out.println("source port :" + pac.src_port);
System.out.println("destination port :" + pac.dst_port);
}
System.out.println("******************************************************");
}
示例5
/**
* Return the value of the field packet
* @return the value of packet
*/
public IPPacket getPacket() {
return _packet;
}
示例6
/**
* Set the value of the field packet
* @param packet the new packet to set
*/
public void setPacket(final IPPacket packet) {
_packet = packet;
}