本文整理汇总了Java中org.jnetpcap.protocol.network.Ip4类的典型用法代码示例。如果您正苦于以下问题:Java Ip4类的具体用法?Java Ip4怎么用?Java Ip4使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Ip4类属于org.jnetpcap.protocol.network包,在下文中一共展示了Ip4类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: modifyPacket
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* In this function the packet is pseudonymization and checksums are recalculated as configured.
*
* @param packet the captured packet
*/
public void modifyPacket(JPacket packet) {
if ((!DNS_ONLY) || isDns(packet)) {
// If the original ip4 checksum is not valid, the checksum should not be modified.
boolean originalChecksumValid = true;
if (_pseudonymizerMap.containsKey(JProtocol.IP4) && packet.hasHeader(Ip4.ID)) {
Ip4 ip4 = packet.getHeader(new Ip4());
originalChecksumValid = ip4.isChecksumValid();
pseudonymizeIP4(_pseudonymizerMap.get(JProtocol.IP4), ip4);
} else if (_pseudonymizerMap.containsKey(JProtocol.IP6) && packet.hasHeader(Ip6.ID)) {
Ip6 ip6 = packet.getHeader(new Ip6());
pseudonymizeIP6(_pseudonymizerMap.get(JProtocol.IP6), ip6);
}
// Checksum updates.
recalculateChecksums(packet, originalChecksumValid);
}
}
示例2: recalculateChecksums
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* Recalculates the checksums as configured.
*
* @param packet the captured packet
* @param originalChecksumValid boolean that holds whether the checksum was valid before modification.
*/
private void recalculateChecksums(final JPacket packet, final boolean originalChecksumValid) {
for (int checksumType : _checksumList) {
// The ip4 or ip6 checksum should be updated first.
if (checksumType == Ip4.ID && originalChecksumValid) {
if (packet.hasHeader(Ip4.ID)) {
Ip4 ip4 = packet.getHeader(new Ip4());
ip4.recalculateChecksum();
}
}
if (checksumType == Udp.ID && packet.hasHeader(Udp.ID)) {
recalulateUdpChecksum(packet);
}
if (checksumType == Tcp.ID && packet.hasHeader(Tcp.ID)) {
Tcp tcp = packet.getHeader(new Tcp());
tcp.recalculateChecksum();
}
if (checksumType == Icmp.ID && packet.hasHeader(Icmp.ID)) {
Icmp icmp = packet.getHeader(new Icmp());
icmp.recalculateChecksum();
}
}
}
示例3: setPacket
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* Set the value of the field packet
* @param packet the new packet to set
*/
public boolean setPacket(final PcapPacket packet) {
boolean unkown = false;
_packet = packet;
eth = packet.getHeader(new Ethernet());
ip = packet.getHeader(new Ip4());
tcp = packet.getHeader(new Tcp());
if (tcp == null) {
udp = packet.getHeader(new Udp());
if (udp == null) {
icmp = packet.getHeader(new Icmp());
if (icmp == null) {
unkown = true;
setProtocol(Protocol.OTHER);
} else {
setProtocol(Protocol.ICMP);
}
} else {
setProtocol(Protocol.UDP);
}
} else {
setProtocol(Protocol.TCP);
}
return !unkown;
}
示例4: meetCriteria
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
@Override
public Boolean meetCriteria(PcapPacket itemToCheck)
{
if (!itemToCheck.hasHeader(Ip4.ID))
return false;
Ip4 ipHeader = new Ip4();
ipHeader = itemToCheck.getHeader(ipHeader);
if (direction == null) //direction wasn't set, so any direction
return subnetInfo.isInRange(ipHeader.sourceToInt()) || subnetInfo.isInRange(ipHeader.destinationToInt());
switch(direction)
{
case Incoming: return subnetInfo.isInRange(ipHeader.sourceToInt());
case Outgoing: return subnetInfo.isInRange(ipHeader.destinationToInt());
default: return null; //doesn't get here
}
}
示例5: testCompare2SetsOfPackets2
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* Test compare2 sets of packets2.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void testCompare2SetsOfPackets2() throws IOException {
List<PcapPacket> l1 = getPacketList(L2TP);
List<PcapPacket> l2 = getPacketList(L2TP);
Ip4 ip1 = new Ip4();
Ip4 ip2 = new Ip4();
assertEquals(l1.size(), l2.size());
System.out.println("------------------------------\n");
for (int i = 0; i < l1.size(); i++) {
PcapPacket p1 = l1.get(i);
PcapPacket p2 = l2.get(i);
p1.getHeader(ip1);
p2.getHeader(ip2);
assertTrue("ip1.size() == p2.size()", p1.size() == p2.size());
assertTrue(ip1.toString(), ip1.isChecksumValid());
assertTrue(ip2.toString(), ip2.isChecksumValid());
assertTrue(compareJBuffer(p1, p2));
}
}
示例6: testJHeaderIsFragmented
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* Test j header is fragmented.
*/
public void testJHeaderIsFragmented() {
JPacket packet = TestUtils.getPcapPacket(TestUtils.REASEMBLY, 1 - 1);
Ethernet eth = new Ethernet();
if (packet.hasHeader(eth)) {
assertFalse(eth.isFragmented());
}
Ip4 ip = new Ip4();
if (packet.hasHeader(ip)) {
assertTrue(ip.isFragmented());
}
Icmp icmp = new Icmp();
if (packet.hasHeader(icmp)) {
assertTrue(ip.isFragmented());
}
}
示例7: SKIP_testReadEntireSuspectFile
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* SKI p_test read entire suspect file.
*/
public void SKIP_testReadEntireSuspectFile() {
Ip4 ip = new Ip4();
for (PcapPacket packet : TestUtils.getIterable(BUG_FILE)) {
try {
if (packet.getFrameNumber() == 15) {
System.out.println(packet);
System.out.println(packet
.toHexdump(packet.size(), false, false, true));
}
packet.hasHeader(ip);
} catch (NullPointerException e) {
System.out.println(packet.getState().toDebugString());
System.out.println(packet.toHexdump());
throw e;
}
}
}
示例8: testJPayloadAccessorPeerPayloadToJBuffer
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* Test payload getter from header. The packet is exactly 489 bytes long, the
* starting offset of payload for IP header is 0x22 (34) bytes into the packet.
*/
public void testJPayloadAccessorPeerPayloadToJBuffer() {
JPacket packet = TestUtils.getPcapPacket(HTTP, 5);
JPayloadAccessor ip = packet.getHeader(new Ip4());
assertNotNull(ip);
JBuffer buffer = ip.peerPayloadTo(new JBuffer(JMemory.Type.POINTER));
assertEquals(0x00, buffer.getUByte(0));
assertEquals(0x50, buffer.getUByte(1));
assertEquals(0x0d, buffer.getUByte(487 - 34));
assertEquals(0x0a, buffer.getUByte(488 - 34));
// System.out.println(buffer.toHexdump());
assertEquals(489 - 34, buffer.size());
}
示例9: testJPayloadAccessorTransferPayloadToJBuffer
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* Test payload getter from header. The packet is exactly 489 bytes long, the
* starting offset of payload for IP header is 0x22 (34) bytes into the packet.
*/
public void testJPayloadAccessorTransferPayloadToJBuffer() {
JPacket packet = TestUtils.getPcapPacket(HTTP, 5);
JPayloadAccessor ip = packet.getHeader(new Ip4());
assertNotNull(ip);
JBuffer buffer = ip.transferPayloadTo(new JBuffer(489 - 34));
assertEquals(0x00, buffer.getUByte(0));
assertEquals(0x50, buffer.getUByte(1));
assertEquals(0x0d, buffer.getUByte(487 - 34));
assertEquals(0x0a, buffer.getUByte(488 - 34));
// System.out.println(buffer.toHexdump());
assertEquals(489 - 34, buffer.size());
}
示例10: nextPacket
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
@Override
public void nextPacket(JPacket packet, T cookie) {
if (packet.hasHeader(Ip4.ID) && packet.hasHeader(Tcp.ID)) {
packet.getHeader(ip4);
packet.getHeader(tcp);
final int payloadLength = tcp.getPayloadLength();
if (payloadLength > 0) {
switch (packetFilter.filter(ip4, tcp)) {
case Inbound:
inboundFlowHandler.parse(packet, tcp.getPayloadOffset(), payloadLength, cookie);
break;
case Outbound:
outboundFlowHandler.parse(packet, tcp.getPayloadOffset(), payloadLength, cookie);
break;
case Skip:
break;
}
}
}
}
示例11: recalulateUdpChecksum
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* Within JNetPcap the UDP checksum recalculation does not work.
* It is reimplemented here.
*
* @param packet packet to recalculate the udp checksum over.
*/
private void recalulateUdpChecksum(final JPacket packet) {
final Udp udp = packet.getHeader(new Udp());
udp.checksum(0); // The checksum is left out of the calculation but reset for correctness.
final byte[] mergedBytes;
if (packet.hasHeader(Ip4.ID)) {
Ip4 ip4 = packet.getHeader(new Ip4());
mergedBytes = constructIp4UdpChecksumByteArray(ip4, udp);
} else {
Ip6 ip6 = packet.getHeader(new Ip6());
mergedBytes = constructIp6UdpChecksumByteArray(ip6, udp);
}
udp.checksum(ByteBuffer.wrap(calculateInternetChecksum(mergedBytes)).order(ByteOrder.BIG_ENDIAN).getShort());
}
示例12: constructIp4UdpChecksumByteArray
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* Constructs the byte array over which the Ipv4 UDP checksum is calculated.
* Ip4 Pseudo header + data
*
* @param ip4 the ip4 header
* @param udp the udp header
* @return byte array
*/
private byte[] constructIp4UdpChecksumByteArray(final Ip4 ip4, final Udp udp) {
return (Util.concatBytes(ip4.source(), ip4.destination(), new byte[1], new byte[]{(byte)0x11},
Util.unsignedShortToByteArray(udp.length(), ByteOrder.BIG_ENDIAN),
Util.unsignedShortToByteArray(udp.source(), ByteOrder.BIG_ENDIAN),
Util.unsignedShortToByteArray(udp.destination(), ByteOrder.BIG_ENDIAN),
Util.unsignedShortToByteArray(udp.length(), ByteOrder.BIG_ENDIAN),
udp.getPayload()));
}
示例13: creatPacket
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
private JMemoryPacket creatPacket(final InetAddress destIp, final InetAddress sourceIP) {
final int packetSize = 500;
final JMemoryPacket packet = new JMemoryPacket(packetSize);
packet.order(ByteOrder.BIG_ENDIAN);
packet.setUShort(0 + 12, 0x800); // ethernet.type field
packet.scan(Ethernet.ID);
final Ethernet ethernet = packet.getHeader(new Ethernet());
ethernet.destination(destIp.getAddress());
ethernet.source(sourceIP.getAddress());
packet.setUByte(14, 0x04 | 0x05); // ip v4
packet.scan(Ethernet.ID);
// final JPacket sendPacket = new JMemoryPacket(JProtocol.ETHERNET_ID, " 001801bf 6adc0025 4bb7afec 08004500 "
// + " 0041a983 40004006 d69ac0a8 00342f8c " + " ca30c3ef 008f2e80 11f52ea8 4b578018 "
// + " ffffa6ea 00000101 080a152e ef03002a " + " 2c943538 322e3430 204e4f4f 500d0a");
final Ip4 sendPacketIP = packet.getHeader(new Ip4());
// sendPacketIP.destination(destIp.getAddress());
sendPacketIP.type(0x06); //TCP
sendPacketIP.length(packetSize - ethernet.size());
sendPacketIP.ttl(1);
packet.setUByte(46, 0x50); // TCP
packet.scan(Ethernet.ID);
final Tcp sendPacketTcp = packet.getHeader(new Tcp());
sendPacketTcp.destination(80);
sendPacketIP.checksum(sendPacketIP.calculateChecksum());
sendPacketTcp.checksum(sendPacketTcp.calculateChecksum());
final Payload payload = packet.getHeader(new Payload());
payload.setByteArray(1, "Neko".getBytes());
return packet;
}
示例14: converge
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* 将数据包汇聚成网络流
* @param data
*/
private void converge(List<PacketInfo> data) {
for (PacketInfo packetInfo : data) {
PcapPacket packet = packetInfo.getPacket();
Ip4 ip4 = new Ip4();
Tcp tcp = new Tcp();
if (!packet.hasHeader(ip4) || !packet.hasHeader(tcp)) continue;
NetFlow netFlow = find(packetInfo);
if (tcp.flags_SYN()) { // SYN 作为TCP建立连接的标识,创建网络流
if (netFlow == null) {
netFlow = new NetFlow(ip4.sourceToInt(), ip4.destinationToInt(),
tcp.source(), tcp.destination());
netFlow.add(packetInfo);
netFlows.add(netFlow);
}
} else if (tcp.flags_FIN()) {
if (netFlow != null) {
netFlow.setClosed(true); // FIN作为TCP结束标识,关闭网络流
}
} else { // 加入到已存在的网络流中
if (netFlow != null && !netFlow.isClosed()) {
netFlow.add(packetInfo);
}
}
}
}
示例15: contain
import org.jnetpcap.protocol.network.Ip4; //导入依赖的package包/类
/**
* 判断数据包是否属于当前网络流
* @param packetInfo
* @return
*/
public boolean contain(PacketInfo packetInfo) {
boolean b = false;
PcapPacket packet = packetInfo.getPacket();
Tcp tcp = new Tcp();
Ip4 ip4 = new Ip4();
if (packet.hasHeader(ip4)
&& packet.hasHeader(tcp) ) {
if (ip4.sourceToInt() == srcIp
&& ip4.destinationToInt() == dstIp
&& tcp.source() == srcPort
&& tcp.destination() == dstPort) {
packetInfo.setReversed(false);
b = true;
} else if (ip4.sourceToInt() == dstIp
&& ip4.destinationToInt() == srcIp
&& tcp.source() == dstPort
&& tcp.destination() == srcPort) {
packetInfo.setReversed(true);
b = true;
}
}
return b;
}