本文整理汇总了Java中org.pcap4j.packet.UdpPacket类的典型用法代码示例。如果您正苦于以下问题:Java UdpPacket类的具体用法?Java UdpPacket怎么用?Java UdpPacket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UdpPacket类属于org.pcap4j.packet包,在下文中一共展示了UdpPacket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processUdpPacket
import org.pcap4j.packet.UdpPacket; //导入依赖的package包/类
public static void processUdpPacket(Packet packet, PcapFileSummary pcapFileSummary, PacketInfo packetInfo, Mode mode) {
if (packet == null) {
return; // skip empty packets
}
String sourceAddress = packetInfo.get(PacketInfo.SOURCE_ADDRESS);
String destinationAddress = packetInfo.get(PacketInfo.DESTINATION_ADDRESS);
try {
log.trace("Converting to UDP packet");
UdpPacket udpPacket = UdpPacket.newPacket(packet.getRawData(), 0, packet.length());
UdpPacket.UdpHeader udpHeader = udpPacket.getHeader();
UdpPort sourcePort = udpHeader.getSrcPort();
UdpPort destinationPort = udpHeader.getDstPort();
packetInfo.put(PacketInfo.SOURCE_PORT, sourcePort.valueAsString());
packetInfo.put(PacketInfo.DESTINATION_PORT, destinationPort.valueAsString());
if (mode == Mode.POSSIBLE_ATTACKS_ANALYSIS) {
//pcapFileSummary.portScanDetector.add(packetInfo);
}
String udpSource = sourceAddress + ":" + sourcePort.toString();
String udpDestination = destinationAddress + ":" + destinationPort.toString();
log.trace("Adding UDP source to set: " + udpSource);
pcapFileSummary.udpSources.add(udpSource);
log.trace("UDP{ source: " + udpSource + ", destination: " + udpDestination + " }");
} catch (IllegalRawDataException e) {
log.error("Exception occurred while processing a packet. Exception was: " + e);
}
}
示例2: shouldCapture
import org.pcap4j.packet.UdpPacket; //导入依赖的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: pack
import org.pcap4j.packet.UdpPacket; //导入依赖的package包/类
public static UdpPacket pack(
Packet payload,
UdpPort srcPort,
UdpPort dstPort,
InetAddress srcAddr,
InetAddress dstAddr
) {
UdpPacket.Builder builder = new UdpPacket.Builder();
return builder.srcPort(srcPort)
.dstPort(dstPort)
.payloadBuilder(new SimpleBuilder(payload))
.srcAddr(srcAddr)
.dstAddr(dstAddr)
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.build();
}
示例4: consumeIpV4
import org.pcap4j.packet.UdpPacket; //导入依赖的package包/类
protected void consumeIpV4(Packet packet, NetworkInterface getter) {
if (packet.contains(UdpPacket.class)) {
consumeUdp(packet, getter);
}
else if (packet.contains(IcmpV4EchoPacket.class)) {
if (!listeningIcmp) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("not listening ICMP. Drop a packet: " + packet);
}
return;
}
IcmpV4Helper.reply(packet, this, getter);
}
else {
LOGGER.warn("I don't care this packet: " + packet);
}
}
示例5: consumeIpV6
import org.pcap4j.packet.UdpPacket; //导入依赖的package包/类
protected void consumeIpV6(Packet packet, NetworkInterface getter) {
if (packet.contains(UdpPacket.class)) {
consumeUdp(packet, getter);
}
else if (packet.contains(IcmpV6EchoRequestPacket.class)) {
if (!listeningIcmp) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("not listening ICMP. Drop a packet: " + packet);
}
return;
}
IcmpV6Helper.sendEchoReply(packet, this, getter);
}
else {
LOGGER.warn("I don't care this packet: " + packet);
}
}
示例6: handleDnsResponse
import org.pcap4j.packet.UdpPacket; //导入依赖的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);
}
示例7: testHandleDnsRequestNotDnsPacket
import org.pcap4j.packet.UdpPacket; //导入依赖的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);
}
示例8: testDnsQuery
import org.pcap4j.packet.UdpPacket; //导入依赖的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());
}
示例9: testNoQueryDnsQuery
import org.pcap4j.packet.UdpPacket; //导入依赖的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());
}
示例10: buildPacket
import org.pcap4j.packet.UdpPacket; //导入依赖的package包/类
/**
* Build packet
*
* @throws java.net.UnknownHostException
*/
public void buildPacket() throws UnknownHostException {
int calculatedLength = getPacketLength();
String payloadString = payload.getPayloadType().getPadPayloadString(payload.getPayloadPattern(), calculatedLength * 2);
udpBuilder = new UdpPacket.Builder();
UnknownPacket.Builder unknownb = new UnknownPacket.Builder();
unknownb.rawData(payload.getPayloadPad(payloadString, calculatedLength));
Inet4Address srcAddr = (Inet4Address) InetAddress.getByAddress(
new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 0}
);
Inet4Address dstAddr = (Inet4Address) InetAddress.getByAddress(
new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 0}
);
udpBuilder.srcPort(UdpPort.getInstance((short) srcPort.value()))
.dstPort(UdpPort.getInstance((short) dstPort.value()))
.srcAddr(srcAddr)
.dstAddr(dstAddr)
.length((short) calculatedLength)
.payloadBuilder(unknownb)
.correctChecksumAtBuild(true)
.correctLengthAtBuild(false);
this.packet = udpBuilder.build();
}
示例11: getSrcPort
import org.pcap4j.packet.UdpPacket; //导入依赖的package包/类
/**
* Return source port
*
* @param packet
* @return
*/
private String getSrcPort(Packet packet) {
if (protocolType.equals("TCP")) {
return packet.get(TcpPacket.class).getHeader().getSrcPort().toString();
} else if (protocolType.equals("UDP")) {
return packet.get(UdpPacket.class).getHeader().getSrcPort().toString();
}
return "80";
}
示例12: getDstPort
import org.pcap4j.packet.UdpPacket; //导入依赖的package包/类
/**
* Return destination port
*
* @param packet
* @return
*/
private String getDstPort(Packet packet) {
if (protocolType.equals("TCP")) {
return packet.get(TcpPacket.class).getHeader().getDstPort().toString();
} else if (protocolType.equals("UDP")) {
return packet.get(UdpPacket.class).getHeader().getDstPort().toString();
}
return "80";
}
示例13: getPacketTypeText
import org.pcap4j.packet.UdpPacket; //导入依赖的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;
}
示例14: pack
import org.pcap4j.packet.UdpPacket; //导入依赖的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();
}
示例15: pack
import org.pcap4j.packet.UdpPacket; //导入依赖的package包/类
public static IpV4Packet pack(
final Packet payload, Inet4Address src, Inet4Address dst, int ttl, short id
) {
IpNumber ipNum;
if (payload instanceof UdpPacket) {
ipNum = IpNumber.UDP;
}
else if (payload instanceof IcmpV4CommonPacket) {
ipNum = IpNumber.ICMPV4;
}
else if (payload instanceof TcpPacket) {
ipNum = IpNumber.TCP;
}
else {
throw new AssertionError();
}
IpV4Packet.Builder builder = new IpV4Packet.Builder();
return
builder.version(IpVersion.IPV4)
.tos(IpV4Rfc791Tos.newInstance((byte)0))
.identification(id)
.ttl((byte)ttl)
.protocol(ipNum)
.srcAddr(src)
.dstAddr(dst)
.payloadBuilder(new SimpleBuilder(payload))
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.build();
}