JNetPcap发送UDP数据包

时间:2012-10-20 14:28:16

标签: java networking udp packages jnetpcap

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();  
}

但它似乎没有被发送。 因为我不太了解网络,协议和东西,我以为我会问别人。 有谁能告诉我这是错误的吗?

0 个答案:

没有答案