本文整理汇总了Java中org.pcap4j.packet.IpV4Packet类的典型用法代码示例。如果您正苦于以下问题:Java IpV4Packet类的具体用法?Java IpV4Packet怎么用?Java IpV4Packet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IpV4Packet类属于org.pcap4j.packet包,在下文中一共展示了IpV4Packet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getIpV4Packet_pppoe
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
IpV4Packet getIpV4Packet_pppoe(EthernetPacket packet_eth) throws IllegalRawDataException{
IpV4Packet ipV4Packet=null;
byte[] pppData=packet_eth.getPayload().getRawData();
if(pppData.length>8&&pppData[8]==0x45){
byte[] b2=new byte[2];
System.arraycopy(pppData, 4, b2, 0, 2);
short len=(short) ByteShortConvert.toShort(b2, 0);
int ipLength=toUnsigned(len)-2;
byte[] ipData=new byte[ipLength];
//设置ppp参数
PacketUtils.pppHead_static[2]=pppData[2];
PacketUtils.pppHead_static[3]=pppData[3];
if(ipLength==(pppData.length-8)){
System.arraycopy(pppData, 8, ipData, 0, ipLength);
ipV4Packet=IpV4Packet.newPacket(ipData, 0, ipData.length);
}else {
MLog.println("长度不符!");
}
}
return ipV4Packet;
}
示例2: getIpV4Packet_pppoe
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
IpV4Packet getIpV4Packet_pppoe(EthernetPacket packet_eth) throws IllegalRawDataException{
IpV4Packet ipV4Packet=null;
byte[] pppData=packet_eth.getPayload().getRawData();
if(pppData.length>8&&pppData[8]==0x45){
byte[] b2=new byte[2];
System.arraycopy(pppData, 4, b2, 0, 2);
short len=(short) ByteShortConvert.toShort(b2, 0);
int ipLength=toUnsigned(len)-2;
byte[] ipData=new byte[ipLength];
//设置ppp参数
PacketUtils.pppHead_static[2]=pppData[2];
PacketUtils.pppHead_static[3]=pppData[3];
if(ipLength==(pppData.length-8)){
System.arraycopy(pppData, 8, ipData, 0, ipLength);
ipV4Packet=IpV4Packet.newPacket(ipData, 0, ipData.length);
}else {
MLog.println("Length don't match!");
}
}
return ipV4Packet;
}
示例3: validatePacket
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
/**
* Validate packet
*
* @param packet
* @return
*/
public boolean validatePacket(Packet packet) {
validPacket = true;
if(packet.get(IpV4Packet.class) == null){
validPacket = false;
}else if (defaultSrcAddress == null) {
return true;
} else if (!packet.get(IpV4Packet.class).getHeader().getProtocol().name().equals(protocolType)) {
validPacket = false;
} else {
// check src/dst port combination
currentSrcAddressPort = packet.get(IpV4Packet.class).getHeader().getSrcAddr().getHostAddress() + ":" + getSrcPort(packet);
currentDstAddressPort = packet.get(IpV4Packet.class).getHeader().getDstAddr().getHostAddress() + ":" + getDstPort(packet);
if ((!currentSrcAddressPort.equals(defaultSrcAddressPort) && !currentSrcAddressPort.equals(defaultDstAddressPort))
|| (!currentDstAddressPort.equals(defaultSrcAddressPort) && !currentDstAddressPort.equals(defaultDstAddressPort))) {
validPacket = false;
}
}
return validPacket;
}
示例4: updateSrcAddress
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
/**
* Update source IP address
*/
private Packet updateSrcAddress(Packet packet) {
try {
if (importedProperties.isSourceEnabled()) {
Inet4Address modifiedAddress = (Inet4Address) Inet4Address.getByAddress(convertIPToByte(importedProperties.getSrcAddress()));
Packet.Builder builder = packet.getBuilder();
if (packet.get(IpV4Packet.class).getHeader().getSrcAddr().getHostAddress().equals(defaultSrcAddress)) {
builder.get(IpV4Packet.Builder.class).srcAddr(modifiedAddress);
} else {
builder.get(IpV4Packet.Builder.class).dstAddr(modifiedAddress);
}
packet = builder.build();
}
} catch (Exception ex) {
LOG.error("Error updating source IP", ex);
}
return packet;
}
示例5: updateDstAddress
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
/**
* Update destination IP address
*/
private Packet updateDstAddress(Packet packet) {
try {
if (importedProperties.isDestinationEnabled()) {
Inet4Address modifiedAddress = (Inet4Address) Inet4Address.getByAddress(convertIPToByte(importedProperties.getDstAddress()));
Packet.Builder builder = packet.getBuilder();
if (packet.get(IpV4Packet.class).getHeader().getDstAddr().getHostAddress().equals(defaultDstAddress)) {
builder.get(IpV4Packet.Builder.class).dstAddr(modifiedAddress);
} else {
builder.get(IpV4Packet.Builder.class).srcAddr(modifiedAddress);
}
packet = builder.build();
}
} catch (Exception ex) {
LOG.error("Error updating destination IP", ex);
}
return packet;
}
示例6: buildIPV4Packet
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
/**
* Build IPV4 packet
*
* @param packetData
* @return
* @throws IOException
* @throws InterruptedException
* @throws PcapNativeException
* @throws NotOpenException
* @throws java.net.URISyntaxException
*/
public TrexEthernetPacket buildIPV4Packet(PacketData packetData) throws IOException, InterruptedException, PcapNativeException, NotOpenException, URISyntaxException {
LOG.info("create ethernet packet");
int packetLength = PacketBuilderHelper.getPacketLength(packetData.getPacketLength().getLengthType(), packetData.getPacketLength().getFrameLength(), packetData.getPacketLength().getMaxLength());
Payload payload = packetUtil.getPayload(packetData.getPayload());
TrexEthernetPacket ethernetPacket = packetUtil.prepareEthernetPacket(packetData, packetLength, payload);
// define VLAN
IpV4Packet.Builder ipV4Packet = getIPV4PacketBuilder(PacketBuilderHelper.getIPV4TotalLength(packetData.isTaggedVlan()), packetLength, payload, packetData);
if (!packetData.isTaggedVlan()) {
LOG.info("Add IPV4 packet");
ethernetPacket.buildPacket(ipV4Packet);
} else {
packetUtil.addVlanToPacket(ethernetPacket, ipV4Packet);
}
return ethernetPacket;
}
示例7: matchesDestination
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
public static boolean matchesDestination(
Packet packet, Inet4Address addr, Inet4Address subnetmask
) {
IpV4Packet ipv4Packet = packet.get(IpV4Packet.class);
if (ipv4Packet == null) {
throw new IllegalArgumentException(packet.toString());
}
Inet4Address dstAddr = ipv4Packet.getHeader().getDstAddr();
if (dstAddr.equals(addr)) {
return true;
}
if (!isSameNetwork(addr, dstAddr, subnetmask)) {
return false;
}
return isBroadcastAddr(dstAddr, subnetmask);
}
示例8: gotPacket
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
public void gotPacket(Packet packet) {
NetworkInterface getter = nifs.get(ifName);
if (packet.contains(IpV4Packet.class)) {
handleIpV4(packet, getter);
}
if (packet.contains(IpV6Packet.class)) {
if (packet.contains(IcmpV6NeighborSolicitationPacket.class)) {
handleIcmpV6Ns(packet, getter);
}
else if (packet.contains(IcmpV6NeighborAdvertisementPacket.class)) {
handleIcmpV6Na(packet, getter);
}
else {
handleIpV6(packet, getter);
}
}
else if (packet.contains(ArpPacket.class)) {
handleArp(packet, getter);
}
}
示例9: onEvent
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
@Override
public void onEvent(PacketContainer container, long sequence, boolean endOfBatch) throws Exception {
IpV4Packet ipV4Packet = container.getIpV4Packet();
if (ipV4Packet == null) {
return;
}
IpV4Packet.IpV4Header ipV4Header = ipV4Packet.getHeader();
if (!ipV4Header.getMoreFragmentFlag() && ipV4Header.getFragmentOffset() == 0) {
return;
}
int identification = ipV4Header.getIdentificationAsInt();
if (shouldSkipEvent(identification, order, total)) {
return;
}
// It seems to be the best place to call 'checkExpired()'...
checkExpired();
FragmentedPackets fragmentedPackets = accumulator.computeIfAbsent(identification, i -> new FragmentedPackets());
fragmentedPackets.addPacket(ipV4Packet);
if (fragmentedPackets.isAllPacketsReceived()) {
try {
container.setIpV4Packet(IpV4Helper.defragment(fragmentedPackets.getPackets()));
} finally {
accumulator.remove(identification);
}
} else {
container.setIpV4Packet(null);
}
}
示例10: isAllPacketsReceived
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
public boolean isAllPacketsReceived() {
if (!isLastPacketReceived) {
return false;
}
int expectedOffset = 0;
for (Map.Entry<Integer, IpV4Packet> entry : packets.entrySet()) {
int offset = entry.getKey();
if (offset != expectedOffset) {
return false;
}
IpV4Packet p = entry.getValue();
expectedOffset = offset + p.getHeader().getTotalLength() - 4 * p.getHeader().getIhl();
}
return true;
}
示例11: addFragment
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
public void addFragment(int identification, IpV4Packet fragment) {
List<IpV4Packet> collected = fragments.get(identification);
if (collected == null) {
collected = new LinkedList<IpV4Packet>();
}
collected.add(fragment);
fragments.put(identification, collected);
}
示例12: defragment
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
public IpV4Packet defragment(int identification) {
List<IpV4Packet> collected = fragments.get(identification);
if (collected == null) {
throw new IllegalArgumentException("No fragments collected for ID: " + identification);
}
fragments.remove(identification);
return IpV4Helper.defragment(collected);
}
示例13: handleDnsResponse
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
/**
* Handles a responsePayload from an upstream DNS server
*
* @param requestPacket The original request packet
* @param responsePayload The payload of the response
*/
void handleDnsResponse(IpPacket requestPacket, byte[] responsePayload) {
UdpPacket udpOutPacket = (UdpPacket) requestPacket.getPayload();
UdpPacket.Builder payLoadBuilder = new UdpPacket.Builder(udpOutPacket)
.srcPort(udpOutPacket.getHeader().getDstPort())
.dstPort(udpOutPacket.getHeader().getSrcPort())
.srcAddr(requestPacket.getHeader().getDstAddr())
.dstAddr(requestPacket.getHeader().getSrcAddr())
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.payloadBuilder(
new UnknownPacket.Builder()
.rawData(responsePayload)
);
IpPacket ipOutPacket;
if (requestPacket instanceof IpV4Packet) {
ipOutPacket = new IpV4Packet.Builder((IpV4Packet) requestPacket)
.srcAddr((Inet4Address) requestPacket.getHeader().getDstAddr())
.dstAddr((Inet4Address) requestPacket.getHeader().getSrcAddr())
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.payloadBuilder(payLoadBuilder)
.build();
} else {
ipOutPacket = new IpV6Packet.Builder((IpV6Packet) requestPacket)
.srcAddr((Inet6Address) requestPacket.getHeader().getDstAddr())
.dstAddr((Inet6Address) requestPacket.getHeader().getSrcAddr())
.correctLengthAtBuild(true)
.payloadBuilder(payLoadBuilder)
.build();
}
eventLoop.queueDeviceWrite(ipOutPacket);
}
示例14: testHandleDnsRequestNotUdpPacket
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
@Test
public void testHandleDnsRequestNotUdpPacket() throws Exception {
TcpPacket.Builder payLoadBuilder = new TcpPacket.Builder()
.srcPort(TcpPort.HTTP)
.dstPort(TcpPort.HTTP)
.srcAddr(InetAddress.getByAddress(new byte[]{8, 8, 4, 4}))
.dstAddr(InetAddress.getByAddress(new byte[]{8, 8, 8, 8}))
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.payloadBuilder(
new UnknownPacket.Builder()
.rawData(new byte[0])
);
IpPacket ipOutPacket = new IpV4Packet.Builder()
.version(IpVersion.IPV4)
.tos(IpV4Rfc791Tos.newInstance((byte) 0))
.protocol(IpNumber.UDP)
.srcAddr((Inet4Address) Inet4Address.getByAddress(new byte[]{8, 8, 4, 4}))
.dstAddr((Inet4Address) Inet4Address.getByAddress(new byte[]{8, 8, 8, 8}))
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.payloadBuilder(payLoadBuilder)
.build();
dnsPacketProxy.handleDnsRequest(ipOutPacket.getRawData());
assertNull(mockEventLoop.lastOutgoing);
assertNull(mockEventLoop.lastResponse);
}
示例15: testHandleDnsRequestNotDnsPacket
import org.pcap4j.packet.IpV4Packet; //导入依赖的package包/类
@Test
public void testHandleDnsRequestNotDnsPacket() throws Exception {
UdpPacket.Builder payLoadBuilder = new UdpPacket.Builder()
.srcPort(UdpPort.HTTP)
.dstPort(UdpPort.HTTP)
.srcAddr(InetAddress.getByAddress(new byte[]{8, 8, 4, 4}))
.dstAddr(InetAddress.getByAddress(new byte[]{8, 8, 8, 8}))
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.payloadBuilder(
new UnknownPacket.Builder()
.rawData(new byte[]{1, 2, 3, 4, 5})
);
IpPacket ipOutPacket = new IpV4Packet.Builder()
.version(IpVersion.IPV4)
.tos(IpV4Rfc791Tos.newInstance((byte) 0))
.protocol(IpNumber.UDP)
.srcAddr((Inet4Address) Inet4Address.getByAddress(new byte[]{8, 8, 4, 4}))
.dstAddr((Inet4Address) Inet4Address.getByAddress(new byte[]{8, 8, 8, 8}))
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.payloadBuilder(payLoadBuilder)
.build();
dnsPacketProxy.handleDnsRequest(ipOutPacket.getRawData());
assertNull(mockEventLoop.lastOutgoing);
assertNull(mockEventLoop.lastResponse);
}