3

A 有一个问题,因为我已经工作了将近 3 天来让库和代码毫无例外地工作,我现在真的很失望。

我只想创建一个带有欺骗性 IPv4 地址的 Udp 包并发送它。听起来很容易,不是吗?但事实并非如此!这是我创建包的代码:

import org.jnetpcap.nio.JBuffer;
import org.jnetpcap.packet.JMemoryPacket;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.protocol.lan.Ethernet;
import org.jnetpcap.protocol.network.Ip4;
import org.jnetpcap.protocol.tcpip.Udp;

public class UdpPacket {
private JPacket packet = null;
private Ethernet eth = new Ethernet();
private Ip4 ip = new Ip4();
private Udp udp = new Udp();
private byte[] payload = null;

private UdpPacket() {
  packet = new JMemoryPacket(Ethernet.ID,
               "30469a975316485d6083d14908004500021c2257000080112610c0a802844a7de2c0f251029a02081688");

eth = packet.getHeader(new Ethernet()); // eth = new Ethernet();
ip = packet.getHeader(new Ip4()); // ip = new Ip4();
udp = packet.getHeader(new Udp()); // udp = new Udp();

packet.check();

/*
 * These aren't really needed now since I can't figure out how to create
 * empty headers .. sort of better this way tho
 */

// eth.type(0x0800); // Set the type to IP
// ip.version(4); // Set to version 4
// ip.length(20); // Set the IP header length to 20 bytes
// ?? Set Differentiated Service Field ??
// ip.id(0x2257); // Identification
// ip.flags(0); // Flags
// ip.offset(0); // Fragment offset
// ip.ttl(128); // Time-to-live
// ip.type(Ip4.Ip4Type.UDP); // Type: UDP

}

public UdpPacket(byte[] sourceMac, String sourceIp, int sourcePort, byte[] destMac,   String destIp, int destPort, byte[] p) {
this();

setSourceMac(sourceMac);
setDestMac(destMac);
setSourceIp(sourceIp);
setDestIp(destIp);
setPayload(p);
setSourcePort(sourcePort);
setDestPort(destPort);

}

public void setSourcePort(int sourcePort) {
  udp.source(sourcePort);
}

public void setDestPort(int destPort) {
  udp.destination(destPort);
}

public void setSourceMac(byte[] source) {
  eth.source(source);
}

public void setDestMac(byte[] dest) {
  eth.destination(dest);
}

public void setSourceIp(String source) {
String[] subs = source.split("\\.");

byte[] buff =     {Integer.valueOf(subs[0]).byteValue(),Integer.valueOf(subs[1]).byteValue(),
            Integer.valueOf(subs[2]).byteValue(),Integer.valueOf(subs[3]).byteValue()};

ip.source(buff);
}

public void setDestIp(String dest) {
String[] subs = dest.split("\\.");

byte[]buff=    {Integer.valueOf(subs[0]).byteValue(),Integer.valueOf(subs[1]).byteValue(),
            Integer.valueOf(subs[2]).byteValue(),Integer.valueOf(subs[3]).byteValue()};

ip.destination(buff);
}

public void setPayload(byte[] p) {
  payload = p.clone();
}

public JPacket getJPacket() {
  JBuffer buff = null;

  // Update checksums and lengths
  ip.length(ip.size() + udp.size() + payload.length);
  ip.checksum(ip.calculateChecksum());
  udp.length(udp.size() + payload.length);

  buff = new JBuffer(eth.size() + ip.size() + udp.size() + payload.length);

  eth.transferTo(buff);
  ip.transferTo(buff, 0, ip.size(), eth.size());
  udp.transferTo(buff, 0, udp.size(), eth.size() + ip.size());
  buff.setByteArray(eth.size() + ip.size() + udp.size(), payload);

  return new JMemoryPacket(buff);
}

}

现在我用这段代码创建包,并尝试发送它:

public void fire(Reflector ammu, int device) throws Exception
{
    UdpPacket packet = new UdpPacket(getMac(InetAddress.getByName(targetIP)), targetIP, 29860,
            getMac(InetAddress.getByName(ammu.getAdress())), ammu.getAdress(), Integer.valueOf(ammu.getPort()),
            "getstatus".getBytes());

    byte[] buff = null;

    buff = packet.getJPacket().getByteArray(0, packet.getJPacket().size());

    send(buff);
}

private byte[] getMac(InetAddress address) throws Exception
{
    //NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
    NetworkInterface nwi = NetworkInterface.getByInetAddress(InetAddress.getByName("localhost"));
    byte mac[] = nwi.getHardwareAddress();

    return mac;
}

@SuppressWarnings("deprecation")
private void send(byte[] packet)
{
    List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs  
    StringBuilder errbuf = new StringBuilder(); // For any error msgs  

    /*************************************************************************** 
     * First get a list of devices on this system 
     **************************************************************************/  
    int r = Pcap.findAllDevs(alldevs, errbuf);  
    if (r == Pcap.NOT_OK || alldevs.isEmpty()) {  
      System.err.printf("Can't read list of devices, error is %s", errbuf.toString());  
      return;  
    }  
    PcapIf device = alldevs.get(0); // We know we have atleast 1 device  

    /***************************************** 
     * Second we open a network interface 
     *****************************************/  
    int snaplen = 64 * 1024; // Capture all packets, no trucation  
    int flags = Pcap.MODE_PROMISCUOUS; // capture all packets  
    int timeout = 10 * 1000; // 10 seconds in millis  
    Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);  

    /******************************************************* 
     * Fourth We send our packet off using open device 
     *******************************************************/  
    if (pcap.sendPacket(packet) != Pcap.OK) {  
      System.err.println(pcap.getErr());  
    }  

    /******************************************************** 
     * Lastly we close 
     ********************************************************/  
    pcap.close();  
}

但它似乎没有被发送。因为我对网络、协议和其他东西不太了解,所以我想我去问问别人。有没有人可以告诉我错误是什么?

4

0 回答 0