本文整理汇总了Java中org.pcap4j.packet.namednumber.IpNumber类的典型用法代码示例。如果您正苦于以下问题:Java IpNumber类的具体用法?Java IpNumber怎么用?Java IpNumber使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IpNumber类属于org.pcap4j.packet.namednumber包,在下文中一共展示了IpNumber类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onEvent
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
@Override
public void onEvent(PacketContainer container, long sequence, boolean endOfBatch) throws Exception {
if (shouldSkipEvent(sequence)) {
return;
}
DataLinkType dlt = container.getDlt();
byte[] data = container.getRawPacket();
Packet packet = PacketFactories.getFactory(Packet.class, DataLinkType.class)
.newInstance(data, 0, data.length, dlt);
// Don't use packet.get(IpV4Packet.class).
// In case of 'IP in IP' packets it doesn't work properly.
IpV4Packet ipV4Packet = extractIpV4Packet(packet,
pkt -> pkt.getOuterOf(UdpPacket.class),
pkt -> pkt.getOuterOf(TcpPacket.class),
pkt -> pkt.getOuterOf(FragmentedPacket.class));
if (ipV4Packet != null) {
IpNumber protocol = ipV4Packet.getHeader().getProtocol();
if (protocol.compareTo(IpNumber.UDP) == 0 || protocol.compareTo(IpNumber.TCP) == 0) {
container.setIpV4Packet(ipV4Packet);
}
}
}
示例2: processIpv6Packet
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
public static void processIpv6Packet(Packet packet, PcapFileSummary pcapFileSummary, PacketInfo packetInfo, Mode mode) {
if (packet == null) {
return; // skip empty packets
}
try {
log.trace("Converting to IPv6 packet");
IpV6Packet ipV6Packet = IpV6Packet.newPacket(packet.getRawData(), 0, packet.length());
IpV6Packet.IpV6Header ipV6Header = ipV6Packet.getHeader();
Inet6Address sourceAddress = ipV6Header.getSrcAddr();
Inet6Address destAddress = ipV6Header.getDstAddr();
if (mode == Mode.POSSIBLE_ATTACKS_ANALYSIS) {
//pcapFileSummary.ipMacTracker.query(sourceAddress.getHostAddress(), sourceMac.toString());
}
packetInfo.put(PacketInfo.SOURCE_ADDRESS, sourceAddress.getHostAddress());
packetInfo.put(PacketInfo.DESTINATION_ADDRESS, destAddress.getHostAddress());
log.trace("Adding IPv6 addresses to set: source: " + sourceAddress.getHostAddress() + ", dest: " + destAddress.getHostAddress());
pcapFileSummary.uniqueIpAddresses.add(sourceAddress.getHostAddress());
pcapFileSummary.uniqueIpAddresses.add(destAddress.getHostAddress());
IpNumber ipNumber = ipV6Header.getProtocol();
packetInfo.put(PacketInfo.IP_PROTOCOL, ipNumber.toString());
pcapFileSummary.ipProtocolCounter.increment(ipNumber);
Packet payload = ipV6Packet.getPayload();
if (ipNumber == IpNumber.ICMPV4) {
IcmpPacketProcessor.processIcmpv4Packet(payload, pcapFileSummary, packetInfo, mode);
} else if (ipNumber == IpNumber.ICMPV6) {
IcmpPacketProcessor.processIcmpv6Packet(payload, pcapFileSummary, packetInfo, mode);
} else if (ipNumber == IpNumber.TCP) {
TcpPacketProcessor.processTcpPacket(payload, pcapFileSummary, packetInfo, mode);
} else if (ipNumber == IpNumber.UDP) {
UdpPacketProcessor.processUdpPacket(payload, pcapFileSummary, packetInfo, mode);
} else {
log.trace("Skipping packet: " + payload);
}
} catch (IllegalRawDataException e) {
log.error("Exception occurred while processing a packet. Exception was: " + e);
}
}
示例3: increment
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
public void increment(IpNumber ipNumber) {
Integer count = protocolCounts.get(ipNumber);
if (count == null) {
count = new Integer(1);
} else {
count++;
}
protocolCounts.put(ipNumber, count);
}
示例4: toString
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
public String toString() {
StringBuilder builder = new StringBuilder("=== IP Protocol Counts ===\n");
for (Map.Entry<IpNumber, Integer> entry : entrySet()) {
builder.append(entry.getKey());
builder.append(": ");
builder.append(entry.getValue());
builder.append("\n");
}
return builder.toString();
}
示例5: testHandleDnsRequestNotUdpPacket
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的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);
}
示例6: testHandleDnsRequestNotDnsPacket
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的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);
}
示例7: testDnsQuery
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
@Test
public void testDnsQuery() throws Exception {
Message message = Message.newQuery(new ARecord(new Name("notblocked.example.com."),
0x01,
3600,
Inet4Address.getByAddress(new byte[]{0, 0, 0, 0})
));
UdpPacket.Builder payLoadBuilder = new UdpPacket.Builder()
.srcPort(UdpPort.DOMAIN)
.dstPort(UdpPort.DOMAIN)
.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(message.toWire())
);
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.lastResponse);
assertNotNull(mockEventLoop.lastOutgoing);
assertEquals(Inet4Address.getByAddress(new byte[]{8, 8, 8, 8}), mockEventLoop.lastOutgoing.getAddress());
}
示例8: testNoQueryDnsQuery
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
@Test
public void testNoQueryDnsQuery() throws Exception {
Message message = new Message();
UdpPacket.Builder payLoadBuilder = new UdpPacket.Builder()
.srcPort(UdpPort.DOMAIN)
.dstPort(UdpPort.DOMAIN)
.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(message.toWire())
);
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.lastResponse);
assertNull(mockEventLoop.lastOutgoing);
dnsPacketProxy.handleDnsRequest(ipOutPacket.getRawData());
}
示例9: createSyncAck
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
static Packet createSyncAck(
MacAddress srcAddress_mac,
MacAddress dstAddrress_mac,
Inet4Address srcAddress,short srcPort,
Inet4Address dstAddress,short dstPort,
int ack_sequence,int sequence,short ident){
TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
//builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
builder_tcp.correctChecksumAtBuild(true);
builder_tcp.correctLengthAtBuild(true);
builder_tcp.paddingAtBuild(true);
builder_tcp.ack(true);
builder_tcp.acknowledgmentNumber(ack_sequence);
//builder_tcp.checksum(tcpHeader.getChecksum());
//builder_tcp.dataOffset((byte) 8);
builder_tcp.dstAddr(dstAddress);
builder_tcp.dstPort(new TcpPort(dstPort,""));
//builder_tcp.fin(tcpHeader.getFin());
ArrayList<TcpOption> tcp_options=new ArrayList<TcpOption>();
TcpNoOperationOption nop=TcpNoOperationOption.getInstance();
TcpMaximumSegmentSizeOption seg_option=new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu).correctLengthAtBuild(true).build();
tcp_options.add(seg_option);
tcp_options.add(nop);
tcp_options.add(nop);
TcpSackPermittedOption sack_permit_option=TcpSackPermittedOption.getInstance();
tcp_options.add(sack_permit_option);
tcp_options.add(nop);
TcpWindowScaleOption win_option=new TcpWindowScaleOption.Builder().shiftCount(shiftCount).correctLengthAtBuild(true).build();
tcp_options.add(win_option);
builder_tcp.options(tcp_options);
//builder_tcp.padding(tcpHeader.getPadding());
builder_tcp.psh(false);
builder_tcp.reserved((byte) 0);
builder_tcp.rst(false);
builder_tcp.sequenceNumber(sequence);
builder_tcp.srcAddr(srcAddress);
builder_tcp.srcPort(new TcpPort(srcPort,""));
builder_tcp.syn(true);
builder_tcp.urg(false);
//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
builder_tcp.window( window);
IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
builder_ipv4.correctChecksumAtBuild(true);
builder_ipv4.correctLengthAtBuild(true);
builder_ipv4.paddingAtBuild(true);
builder_ipv4.dstAddr(dstAddress);
builder_ipv4.dontFragmentFlag(true);
builder_ipv4.fragmentOffset((short)0);
//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
// short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
builder_ipv4.identification(ident);
builder_ipv4.ihl((byte) 5);
//builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
//builder_ipv4.options(ipV4Header.getOptions());
//builder_ipv4.padding(ipV4Header.getPadding());
builder_ipv4.protocol(IpNumber.TCP);
// builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
builder_ipv4.srcAddr(srcAddress);
builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
//builder_ipv4.totalLength( 52);
builder_ipv4.ttl(ttl);
builder_ipv4.version(IpVersion.IPV4);
builder_ipv4.payloadBuilder(builder_tcp);
//
Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);
return p;
}
示例10: buildPacket
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
/**
*
* @param builder
* @param protocol
*/
public void buildPacket(AbstractPacket.AbstractBuilder builder, IpNumber protocol) {
byte[] pad = new byte[1];
String payloadPattern = payload.getPayloadPattern();
String payloadString = payload.getPayloadType().getPadPayloadString(payloadPattern, (totalLength - 20) * 2);
if (packetLength > DEFAULT_PACKET_LENGTH) {
payloadString = payload.getPayloadType().getPadPayloadString(payloadPattern, payloadString.length() + ((packetLength - DEFAULT_PACKET_LENGTH) * 2));
this.totalLength = (short) ((int) totalLength + (packetLength - DEFAULT_PACKET_LENGTH));
}
pad = payload.getPayloadPad(payloadString, totalLength - 20);
ipV4Builder = new IpV4Packet.Builder();
ipV4Builder.version(version)
.ihl(ihl)
.tos(tos)
.totalLength(totalLength)
.identification(identification)
.reservedFlag(reservedFlag)
.ttl(ttl)
.fragmentOffset((short) 0)
.protocol(protocol)
.headerChecksum(headerChecksum)
.srcAddr(srcAddr)
.dstAddr(dstAddr)
.protocol(protocol)
.correctChecksumAtBuild(true)
// .correctLengthAtBuild(true)
.payloadBuilder(builder);
if (protocol != IpNumber.TCP && protocol != IpNumber.UDP) {
ipV4Builder.paddingAtBuild(false)
.padding(pad);
} else {
ipV4Builder.paddingAtBuild(true).correctLengthAtBuild(true);
}
this.packet1 = ipV4Builder.build();
}
示例11: getIPV4Packet
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
/**
* Prepare and return IPV4 builder
*
* @return
* @throws Exception
*/
private Builder getIPV4Packet(int totalLength, int packetLength) throws Exception {
if (selections.isIPV4Selected()) {
TrexIpV4Packet ipV4Packet = new TrexIpV4Packet();
ipV4Packet.setSrcAddr(ipv4View.getSourceAddress().getAddress());
ipV4Packet.setDstAddr(ipv4View.getDestinationAddress().getAddress());
ipV4Packet.setTotalLength((short) totalLength);
ipV4Packet.setPacketLength(packetLength + 4);
Payload payload = payloadView.getPayload();
ipV4Packet.setPayload(payload);
int ipv4PacketLength = PacketBuilderHelper.getIPV4PacketLength(selections.isTaggedVlanSelected(), packetLength, totalLength);
int tcpUdpPacketLength = PacketBuilderHelper.getTcpUdpPacketLength(selections.isTaggedVlanSelected(), packetLength, totalLength);
if (selections.isTCPSelected()) {
ipV4Packet.setPacketLength(ipv4PacketLength);
ipV4Packet.buildPacket(tcpView.getTcpPacket(payload, tcpUdpPacketLength).getBuilder(), IpNumber.TCP);
} else if (selections.isUDPSelected()) {
ipV4Packet.setPacketLength(ipv4PacketLength);
ipV4Packet.buildPacket(udpView.getUDPPacket(payload, tcpUdpPacketLength).getBuilder(), IpNumber.UDP);
} else {
ipV4Packet.buildPacket(null, IpNumber.getInstance((byte) 0));
}
return ipV4Packet.getBuilder();
}
return null;
}
示例12: getIPV4PacketBuilder
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
@Override
protected IpV4Packet.Builder getIPV4PacketBuilder(int totalLength, int packetLength, Payload payload, PacketData packetData) throws UnknownHostException {
LOG.info("Create IPV4 Packet");
TrexIpV4Packet ipV4Packet = prepareIPV4Packet(totalLength, packetLength, payload, packetData);
int ipv4PacketLength = PacketBuilderHelper.getIPV4PacketLength(packetData.isTaggedVlan(), packetLength, totalLength);
ipV4Packet.setPacketLength(ipv4PacketLength);
int tcpPacketLength = PacketBuilderHelper.getTcpUdpPacketLength(packetData.isTaggedVlan(), packetLength, totalLength);
// build packet
ipV4Packet.buildPacket(getTcpPacket(payload, tcpPacketLength, packetData).getBuilder(), IpNumber.TCP);
return ipV4Packet.getBuilder();
}
示例13: getIPV4PacketBuilder
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
/**
* Create IPV4 packet
*
* @param totalLength
* @param packetLength
* @param payload
* @param packetData
* @return IPV4 packet
* @throws java.net.UnknownHostException
*/
@Override
protected Builder getIPV4PacketBuilder(int totalLength, int packetLength, Payload payload, PacketData packetData) throws UnknownHostException {
LOG.info("Create IPV4 Packet");
TrexIpV4Packet ipV4Packet = prepareIPV4Packet(totalLength, packetLength, payload, packetData);
// build IPV4 packet
ipV4Packet.buildPacket(null, IpNumber.getInstance((byte) 0));
return ipV4Packet.getBuilder();
}
示例14: getIPV4PacketBuilder
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
@Override
protected IpV4Packet.Builder getIPV4PacketBuilder(int totalLength, int packetLength, Payload payload, PacketData packetData) throws UnknownHostException {
LOG.info("Create IPV4 Packet");
TrexIpV4Packet ipV4Packet = prepareIPV4Packet(totalLength, packetLength, payload, packetData);
int ipv4PacketLength = PacketBuilderHelper.getIPV4PacketLength(packetData.isTaggedVlan(), packetLength, totalLength);
ipV4Packet.setPacketLength(ipv4PacketLength);
int udpPacketLength = PacketBuilderHelper.getTcpUdpPacketLength(packetData.isTaggedVlan(), packetLength, totalLength);
// build packet
ipV4Packet.buildPacket(getUdpPacket(payload, udpPacketLength, packetData).getBuilder(), IpNumber.UDP);
return ipV4Packet.getBuilder();
}
示例15: pack
import org.pcap4j.packet.namednumber.IpNumber; //导入依赖的package包/类
public static IpV6Packet pack(
final Packet payload, Inet6Address src, Inet6Address dst, int hopLimit, short id
) {
IpNumber nextHeader;
if (payload instanceof UdpPacket) {
nextHeader = IpNumber.UDP;
}
else if (payload instanceof IcmpV6CommonPacket) {
nextHeader = IpNumber.ICMPV6;
}
else if (payload instanceof TcpPacket) {
nextHeader = IpNumber.TCP;
}
else {
throw new AssertionError();
}
IpV6Packet.Builder builder = new IpV6Packet.Builder();
return builder.version(IpVersion.IPV6)
.trafficClass(IpV6SimpleTrafficClass.newInstance((byte)0))
.flowLabel(IpV6SimpleFlowLabel.newInstance(0))
.nextHeader(nextHeader)
.hopLimit((byte)hopLimit)
.srcAddr(src)
.dstAddr(dst)
.payloadBuilder(new SimpleBuilder(payload))
.correctLengthAtBuild(true)
.build();
}