如何更改udp数据包中的源ip,然后用java将其发送到目标?

67up9zun  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(293)

我想更改udp数据包中的源ip,然后用java程序将其发送到目的地。我写了这个代码,但它不工作。我犯了个错误:
原因:java.lang.classnotfoundexception:org.pcap4j.core.pcapnativeexception at java.base/jdk.internal.loader.builtinclassloader.loadclass(builtinclassloader)。java:581)在java.base/jdk.internal.loader.classloaders$appclassloader.loadclass(classloaders。java:178)在java.base/java.lang.classloader.loadclass(classloader。java:521) ... 7个以上
我使用pcap4j来编写这个代码。请帮我写这个。谢谢

class Sender {
    public static void send(String message, String sysAddrIp)  {
        try {
            byte[] pck = message.getBytes();

            UnknownPacket unknownPacket = UnknownPacket.newPacket(pck);

            UdpPacket udpPacket = new UdpPacket.Builder()
                    .srcAddr(InetAddress.getByName(sysAddrIp))
                    .dstAddr(InetAddress.getByName("192.168.10.13"))
                    .dstPort(UdpPort.getInstance((short) 514))
                    .correctLengthAtBuild(true)
                    .correctChecksumAtBuild(true)
                    .payloadBuilder(unknownPacket.getBuilder())
                    .build();

            IpV4Packet.Builder ipV4Builder = new IpV4Packet.Builder();

            ipV4Builder.version(IpVersion.IPV4)
                    .tos(IpV4Rfc791Tos.newInstance((byte) 0))
                    .ttl((byte) 100)
                    .protocol(IpNumber.UDP)
                    .srcAddr((Inet4Address) InetAddress.getByName(sysAddrIp))
                    .dstAddr((Inet4Address) InetAddress.getByName("192.168.10.13"))
                    .payloadBuilder(udpPacket.getBuilder())
                    .correctChecksumAtBuild(true)
                    .correctLengthAtBuild(true);

            PcapNetworkInterface nif;
            try {
                nif = new NifSelector().selectNetworkInterface();
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }

            if (nif == null) {
                return;
            }

            System.out.println(nif.getName() + "(" + nif.getDescription() + ")");

            final PcapHandle handle4send = nif.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);

            EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
            etherBuilder.srcAddr(MacAddress.getByName("00:A0:C9:14:C8:29", ":"))
                    .type(EtherType.IPV4)
                    .payloadBuilder(ipV4Builder.build().getBuilder())
                    .paddingAtBuild(true);

            Packet p = etherBuilder.build();

            handle4send.sendPacket(p);
        }catch (IOException | PcapNativeException e){
            System.out.println(e);

        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题