本文整理汇总了Java中org.pcap4j.packet.UdpPacket.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java UdpPacket.Builder方法的具体用法?Java UdpPacket.Builder怎么用?Java UdpPacket.Builder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.pcap4j.packet.UdpPacket
的用法示例。
在下文中一共展示了UdpPacket.Builder方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: 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);
}
示例3: 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);
}
示例4: 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());
}
示例5: 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());
}
示例6: 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();
}
示例7: testHandleDnsRequestEmptyPacket
import org.pcap4j.packet.UdpPacket; //导入方法依赖的package包/类
@Test
public void testHandleDnsRequestEmptyPacket() throws Exception {
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(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());
assertNotNull(mockEventLoop.lastOutgoing);
assertEquals(0, mockEventLoop.lastOutgoing.getLength());
assertEquals(Inet4Address.getByAddress(new byte[]{8, 8, 8, 8}), mockEventLoop.lastOutgoing.getAddress());
assertNull(mockEventLoop.lastResponse);
// Check the same thing with one upstream DNS server configured.
tinySetUp();
dnsPacketProxy.upstreamDnsServers.add(Inet4Address.getByAddress(new byte[]{1, 1, 1, 2}));
dnsPacketProxy.handleDnsRequest(ipOutPacket.getRawData());
assertNull(mockEventLoop.lastOutgoing);
assertNull(mockEventLoop.lastResponse);
// Check the same thing with enough upstream DNS servers configured.
tinySetUp();
for (byte i = 2; i < 9; i++)
dnsPacketProxy.upstreamDnsServers.add(Inet4Address.getByAddress(new byte[]{1, 1, 1, i}));
dnsPacketProxy.handleDnsRequest(ipOutPacket.getRawData());
assertNotNull(mockEventLoop.lastOutgoing);
assertEquals(0, mockEventLoop.lastOutgoing.getLength());
assertEquals(Inet4Address.getByAddress(new byte[]{1, 1, 1, 8}), mockEventLoop.lastOutgoing.getAddress());
assertNull(mockEventLoop.lastResponse);
}
示例8: testBlockedDnsQuery
import org.pcap4j.packet.UdpPacket; //导入方法依赖的package包/类
@Test
public void testBlockedDnsQuery() throws Exception {
Message message = Message.newQuery(new ARecord(new Name("blocked.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());
assertNotNull(mockEventLoop.lastResponse);
assertNull(mockEventLoop.lastOutgoing);
assertTrue(mockEventLoop.lastResponse instanceof IpPacket);
assertTrue(mockEventLoop.lastResponse.getPayload() instanceof UdpPacket);
Message responseMsg = new Message(mockEventLoop.lastResponse.getPayload().getPayload().getRawData());
assertEquals(NOERROR, responseMsg.getHeader().getRcode());
assertArrayEquals(new Record[] {}, responseMsg.getSectionArray(Section.ANSWER));
assertNotEquals(0, responseMsg.getSectionArray(Section.AUTHORITY).length);
assertTrue(responseMsg.getSectionArray(Section.AUTHORITY)[0] instanceof SOARecord);
assertTrue(responseMsg.getSectionArray(Section.AUTHORITY)[0].getTTL() > 0);
}
示例9: testBlockedInet6DnsQuery
import org.pcap4j.packet.UdpPacket; //导入方法依赖的package包/类
@Test
public void testBlockedInet6DnsQuery() throws Exception {
Message message = Message.newQuery(new ARecord(new Name("blocked.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((Inet6Address) Inet6Address.getByName("::0"))
.dstAddr((Inet6Address) Inet6Address.getByName("::1"))
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.payloadBuilder(
new UnknownPacket.Builder()
.rawData(message.toWire())
);
IpPacket ipOutPacket = new IpV6Packet.Builder()
.version(IpVersion.IPV6)
.trafficClass(IpV6SimpleTrafficClass.newInstance((byte) 0))
.flowLabel(IpV6SimpleFlowLabel.newInstance(0))
.nextHeader(IpNumber.UDP)
.srcAddr((Inet6Address) Inet6Address.getByName("::0"))
.dstAddr((Inet6Address) Inet6Address.getByName("::1"))
.correctLengthAtBuild(true)
.payloadBuilder(payLoadBuilder)
.build();
dnsPacketProxy.handleDnsRequest(ipOutPacket.getRawData());
assertNotNull(mockEventLoop.lastResponse);
assertNull(mockEventLoop.lastOutgoing);
assertTrue(mockEventLoop.lastResponse instanceof IpPacket);
assertTrue(mockEventLoop.lastResponse.getPayload() instanceof UdpPacket);
Message responseMsg = new Message(mockEventLoop.lastResponse.getPayload().getPayload().getRawData());
assertEquals(NOERROR, responseMsg.getHeader().getRcode());
assertArrayEquals(new Record[] {}, responseMsg.getSectionArray(Section.ANSWER));
assertNotEquals(0, responseMsg.getSectionArray(Section.AUTHORITY).length);
assertTrue(responseMsg.getSectionArray(Section.AUTHORITY)[0] instanceof SOARecord);
assertTrue(responseMsg.getSectionArray(Section.AUTHORITY)[0].getTTL() > 0);
}
示例10: gotPacket
import org.pcap4j.packet.UdpPacket; //导入方法依赖的package包/类
@Override
public void gotPacket(Packet rawPacket) {
try {
LOG.info("Read raw " + rawPacket);
IpV4Packet ipPacket = rawPacket.get(IpV4Packet.class);
UdpPacket udpPacket = rawPacket.get(UdpPacket.class);
byte[] dhcpData = udpPacket.getPayload().getRawData();
InetSocketAddress remoteAddress = new InetSocketAddress(ipPacket.getHeader().getSrcAddr(), udpPacket.getHeader().getSrcPort().valueAsInt());
InetSocketAddress localAddress = new InetSocketAddress(ipPacket.getHeader().getDstAddr(), udpPacket.getHeader().getDstPort().valueAsInt());
DhcpRequestContext context = new DhcpRequestContext(interfaceAddresses, remoteAddress, localAddress);
DhcpMessage request = decoder.decode(ByteBuffer.wrap(dhcpData));
LOG.info("Read DHCP " + request);
DhcpMessage reply = service.getReplyFor(context, request);
if (reply == null)
return;
byte[] replyData = new byte[1536];
ByteBuffer buffer = ByteBuffer.wrap(replyData);
encoder.encode(buffer, reply);
replyData = Arrays.copyOf(replyData, buffer.position()); // Truncate array to writer position.
UnknownPacket.Builder dhcpBuilder = new UnknownPacket.Builder()
.rawData(replyData);
UdpPacket.Builder udpBuilder = new UdpPacket.Builder()
.payloadBuilder(dhcpBuilder)
.srcPort(udpPacket.getHeader().getDstPort())
.dstPort(udpPacket.getHeader().getSrcPort())
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true);
IpV4Packet.Builder ipBuilder = new IpV4Packet.Builder()
.payloadBuilder(udpBuilder)
.srcAddr(null) // TODO
.dstAddr(null) // TODO
.protocol(IpNumber.UDP)
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true)
.paddingAtBuild(true);
EthernetPacket.Builder ethernetBuilder = new EthernetPacket.Builder()
.payloadBuilder(ipBuilder)
.type(EtherType.IPV4)
.paddingAtBuild(true);
Packet replyPacket = ethernetBuilder.build();
// handle.sendPacket(replyPacket);
} catch (Exception e) {
LOG.error("DHCP failed", e);
}
}
示例11: getBuilder
import org.pcap4j.packet.UdpPacket; //导入方法依赖的package包/类
/**
*
* @return
*/
public UdpPacket.Builder getBuilder() {
return udpBuilder;
}