本文整理汇总了Java中org.pcap4j.packet.Packet.get方法的典型用法代码示例。如果您正苦于以下问题:Java Packet.get方法的具体用法?Java Packet.get怎么用?Java Packet.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.pcap4j.packet.Packet
的用法示例。
在下文中一共展示了Packet.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validatePacket
import org.pcap4j.packet.Packet; //导入方法依赖的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;
}
示例2: shouldCapture
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
/**
* Checks if the given {@link Packet} should be captured.
*
* @param packet The packet to be checked
* @return Returns true, if the packet should be captured, otherwise false
*/
private boolean shouldCapture(final Packet packet) {
if (packet.contains(ArpPacket.class)) {
ArpPacket arpPacket = packet.get(ArpPacket.class);
if (arpPacket.getHeader().getOperation().equals(ArpOperation.REQUEST)) {
return true;
}
}
if (packet.contains(UdpPacket.class)) {
final UdpPacket udpPacket = packet.get(UdpPacket.class);
if (UdpPort.BOOTPS == udpPacket.getHeader().getDstPort()) {
return true;
}
}
return false;
}
示例3: matchesDestination
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
public static boolean matchesDestination(Packet packet, Inet6Address addr) {
IpV6Packet ipv6Packet = packet.get(IpV6Packet.class);
if (ipv6Packet == null) {
throw new IllegalArgumentException(packet.toString());
}
Inet6Address dstAddr = ipv6Packet.getHeader().getDstAddr();
if (dstAddr.equals(addr)) {
return true;
}
if (dstAddr.equals(LINK_LOCAL_ALL_NODES_ADDRESS)) {
return true;
}
if (dstAddr.equals(LINK_LOCAL_ALL_ROUTERS_ADDRESS)) {
return true;
}
return false;
}
示例4: matchesDestination
import org.pcap4j.packet.Packet; //导入方法依赖的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);
}
示例5: cacheByNa
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
private static void cacheByNa(Packet packet, NdpCache ndpCache) {
IcmpV6NeighborAdvertisementPacket naPacket
= packet.get(IcmpV6NeighborAdvertisementPacket.class);
InetAddress ipAddr = naPacket.getHeader().getTargetAddress();
MacAddress macAddr = null;
for (IpV6NeighborDiscoveryOption opt: naPacket.getHeader().getOptions()) {
if (opt.getType().equals(IpV6NeighborDiscoveryOptionType.TARGET_LINK_LAYER_ADDRESS)) {
macAddr
= ((IpV6NeighborDiscoveryTargetLinkLayerAddressOption)opt).getLinkLayerAddressAsMacAddress();
}
}
if (macAddr == null) {
return;
}
// According to RFC 2461, If no entry exists, the advertisement SHOULD be silently discarded.
// But cache it here with no check for entry existance.
NeighborDiscoveryHelper.cache(ndpCache, ipAddr, macAddr);
}
示例6: tag
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
public static Packet tag(Packet packet, int vid) {
EthernetPacket ep = packet.get(EthernetPacket.class);
if (ep == null) {
return packet;
}
Dot1qVlanTagPacket.Builder vb = new Dot1qVlanTagPacket.Builder();
vb.vid((short)vid)
.type(ep.getHeader().getType())
.payloadBuilder(new SimpleBuilder(ep.getPayload()));
Packet.Builder pb = packet.getBuilder();
pb.get(EthernetPacket.Builder.class)
.type(EtherType.DOT1Q_VLAN_TAGGED_FRAMES)
.payloadBuilder(vb);
return pb.build();
}
示例7: handleIcmpV6Ns
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
private void handleIcmpV6Ns(Packet packet, NetworkInterface getter) {
IcmpV6Helper.cache(packet, ndpCache);
IcmpV6NeighborSolicitationPacket nsPacket
= packet.get(IcmpV6NeighborSolicitationPacket.class);
boolean toMe = false;
for (NifIpAddress nifIpAddr: getter.getIpAddresses()) {
if (nifIpAddr.getIpAddr().equals(nsPacket.getHeader().getTargetAddress())) {
toMe = true;
break;
}
}
if (!toMe) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Dropped an NS packet not to me: " + packet);
}
return;
}
IcmpV6Helper.sendSolicitedNeighborAdvertisement(
packet, Node.this, getter
);
}
示例8: updatePacketSrcDst
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
/**
* Update packet source/destination address
*
* @param packet
* @return
*/
public Packet updatePacketSrcDst(Packet packet) {
// initialze default src/dst address and protocol type
if (packet.get(IpV4Packet.class) != null) {
if (defaultSrcAddress == null && defaultDstAddress == null) {
initializeUpdater(packet);
}
packet = updateSrcAddress(packet);
packet = updateDstAddress(packet);
}
return packet;
}
示例9: getPacketTypeText
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
/**
*
* @param packet
* @return
*/
public PacketInfo getPacketTypeText(Packet packet) {
PacketInfo packetInfo = new PacketInfo();
String packetType = "";
// Default values for packet info
packetInfo.setType("Unknown");
packetInfo.setLength(packet.length());
if (packet != null) {
// EthernetPacket
if (packet.get(EthernetPacket.class) != null) {
packetType += "Ethernet/";
}
// IPPacket
if (packet.get(IpV4Packet.class) != null) {
packetType += "IPV4/";
}
// TCPPacket
if (packet.get(TcpPacket.class) != null) {
packetType += "TCP/";
}
// UDPPacket
if (packet.get(UdpPacket.class) != null) {
packetType += "UDP";
}
if (packetType.endsWith("/")) {
packetType = packetType.substring(0, packetType.length() - 1);
}
packetInfo.setType(packetType);
}
return packetInfo;
}
示例10: cacheByNs
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
private static void cacheByNs(Packet packet, NdpCache ndpCache) {
IpV6Packet ipv6Packet = packet.get(IpV6Packet.class);
if (ipv6Packet == null) {
return;
}
Inet6Address srcAddr = ipv6Packet.getHeader().getSrcAddr();
if (srcAddr.equals(IpV6Helper.UNSPECIFIED_ADDRESS)) {
return;
}
IcmpV6NeighborSolicitationPacket nsPacket
= packet.get(IcmpV6NeighborSolicitationPacket.class);
IpV6NeighborDiscoverySourceLinkLayerAddressOption srcLinkOpt = null;
for (IpV6NeighborDiscoveryOption opt: nsPacket.getHeader().getOptions()) {
if (opt.getType().equals(IpV6NeighborDiscoveryOptionType.SOURCE_LINK_LAYER_ADDRESS)) {
srcLinkOpt = (IpV6NeighborDiscoverySourceLinkLayerAddressOption)opt;
break;
}
}
if (srcLinkOpt == null) {
return;
}
// if new, isRouter flag of Neighbor Cache is false.
// if update, isRouter is not changed.
NeighborDiscoveryHelper.cache(ndpCache, srcAddr, srcLinkOpt.getLinkLayerAddressAsMacAddress());
}
示例11: processMessage
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
public void processMessage(Packet packet) {
UdpPacket udpPacket = packet.get(UdpPacket.class);
byte[] snmpMessage = udpPacket.getPayload().getRawData();
InetAddress srcAddr;
IpV4Packet ipV4Packet = packet.get(IpV4Packet.class);
if (ipV4Packet != null) {
srcAddr = ipV4Packet.getHeader().getSrcAddr();
}
else {
srcAddr = packet.get(IpV6Packet.class).getHeader().getSrcAddr();
}
int srcPort = udpPacket.getHeader().getSrcPort().value() & 0xFFFF;
ByteBuffer bis;
if (isAsyncMsgProcessingSupported()) {
byte[] rawData = new byte[snmpMessage.length];
System.arraycopy(snmpMessage, 0, rawData, 0, rawData.length);
bis = ByteBuffer.wrap(rawData);
}
else {
bis = ByteBuffer.wrap(snmpMessage);
}
TransportStateReference stateReference
= new TransportStateReference(
this,
getAddress(),
null,
SecurityLevel.undefined,
SecurityLevel.undefined,
false,
null
);
fireProcessMessage(
new UdpAddress(srcAddr, srcPort),
bis,
stateReference
);
}
示例12: matchesDestination
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
public static boolean matchesDestination(Packet packet, MacAddress addr) {
EthernetPacket etherPacket = packet.get(EthernetPacket.class);
if (etherPacket == null) {
throw new IllegalArgumentException(packet.toString());
}
MacAddress dst
= etherPacket.getHeader().getDstAddr();
return dst.equals(addr)
|| dst.equals(MacAddress.ETHER_BROADCAST_ADDRESS);
}
示例13: untag
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
public static Packet untag(Packet packet) {
Dot1qVlanTagPacket vp = packet.get(Dot1qVlanTagPacket.class);
if (vp == null) {
return packet;
}
Packet.Builder pb = packet.getBuilder();
pb.get(EthernetPacket.Builder.class)
.type(vp.getHeader().getType())
.payloadBuilder(new SimpleBuilder(vp.getPayload()));
return pb.build();
}
示例14: reply
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
public static void reply(Packet packet, Node node, NetworkInterface nif) {
IcmpV4EchoPacket echo = packet.get(IcmpV4EchoPacket.class);
Packet.Builder outer = packet.getBuilder().getOuterOf(IcmpV4EchoPacket.Builder.class);
IpV4Packet ipv4 = packet.get(IpV4Packet.class);
if (
echo == null
|| ipv4 == null
|| outer == null
|| !(outer instanceof IcmpV4CommonPacket.Builder)
) {
throw new IllegalArgumentException(packet.toString());
}
IcmpV4EchoReplyPacket.Builder repb = new IcmpV4EchoReplyPacket.Builder();
repb.identifier(echo.getHeader().getIdentifier())
.sequenceNumber(echo.getHeader().getSequenceNumber())
.payloadBuilder(new SimpleBuilder(echo.getPayload()));
((IcmpV4CommonPacket.Builder)outer).type(IcmpV4Type.ECHO_REPLY)
.payloadBuilder(repb)
.correctChecksumAtBuild(true);
try {
node.sendL4Packet(
outer.build(),
ipv4.getHeader().getDstAddr(),
ipv4.getHeader().getSrcAddr(),
nif
);
} catch (SendPacketException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
示例15: matchesDestination
import org.pcap4j.packet.Packet; //导入方法依赖的package包/类
public static boolean matchesDestination(Packet packet, InetAddress addr) {
ArpPacket arpPacket = packet.get(ArpPacket.class);
if (arpPacket == null) {
throw new IllegalArgumentException(packet.toString());
}
return addr.equals(arpPacket.getHeader().getDstProtocolAddr());
}