Exemple #1
1
 private NetworkInterface getDevice() throws Exception {
   devices = JpcapCaptor.getDeviceList();
   device = devices[0];
   jpcap = JpcapCaptor.openDevice(device, 2000, false, 10000);
   jpcap.setFilter("ip", true);
   // sender = jpcap.getJpcapSenderInstance();
   jpcap.loopPacket(-1, new PacketReceiverImpl());
   return device;
 }
Exemple #2
0
  public void nextTuple() {
    // TODO Auto-generated method stub
    try {

      if (sampLen < 0) sampLen = 65535;

      while (true) {
        Packet packet = captor.getPacket();
        // if some error occurred or EOF has reached, break the loop
        if (packet == null) {
          System.out.println("null");
          break;
        }
        // otherwise, print out the packet
        IPPacket ip = (IPPacket) packet;
        countPacket++;

        this.outputCollector.emit(createValues(ip));
        // this.outputCollector.ack(tuple);
      }

    } catch (Exception e) {

    }
  }
Exemple #3
0
  public void open(Map arg0, TopologyContext arg1, SpoutOutputCollector spoutOutputCollector) {
    // TODO Auto-generated method stub
    this.outputCollector = spoutOutputCollector;
    try {

      if (srcFilename != null) {
        captor = JpcapCaptor.openFile(srcFilename);
      } else {

        device = getDevice(deviceName);
        System.out.println(device);
        captor = JpcapCaptor.openDevice(device, sampLen, false, 20);
        if (filter != null) captor.setFilter(filter, true);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemple #4
0
  public static String median() throws IOException {

    int m = 0, n = 0, i = 0;
    int baochang[] = new int[3858];

    JpcapCaptor captor = JpcapCaptor.openFile("./data/tcpudptest.pcap");

    while (true) {
      Packet packet = captor.getPacket();
      if (packet == null || packet == Packet.EOF) break;
      if (packet instanceof jpcap.packet.TCPPacket || packet instanceof jpcap.packet.UDPPacket) {

        baochang[i] = packet.len;
        i++;
      }
    }
    boolean exchage = true;
    int j, k, l;
    for (j = 0; j < 3858; j++) {
      exchage = false;
      // 两两比较,找出每趟最小数据元素
      for (k = 3857; k > j; k--) {
        // 将最小数据元素前移
        if (baochang[k - 1] > baochang[k]) {
          l = baochang[k - 1];
          baochang[k - 1] = baochang[k];
          baochang[k] = l;
          exchage = true;
        }
      }
    }
    if (!exchage) {
      m = baochang[1928];
      n = baochang[1929];
    }
    String median = m + "/" + n;
    return median;
  }
Exemple #5
0
  static NetworkInterface[] getNetworkInterfaces() {
    // Obtain the list of network interfaces
    NetworkInterface[] devices = JpcapCaptor.getDeviceList();

    // for each network interface
    for (int i = 0; i < devices.length; i++) {
      System.out.println(i + ": " + devices[i].name + "(" + devices[i].description + ")");
      System.out.println(
          " datalink: " + devices[i].datalink_name + "(" + devices[i].datalink_description + ")");
      System.out.print(" MAC address:");

      for (byte b : devices[i].mac_address) System.out.print(Integer.toHexString(b & 0xff) + ":");
      System.out.println();

      // print out its IP address, subnet mask and broadcast address
      for (NetworkInterfaceAddress a : devices[i].addresses)
        System.out.println(" address:" + a.address + " " + a.subnet + " " + a.broadcast);
    }
    return devices;
  }
Exemple #6
0
  public static byte[] arp(InetAddress ip) throws java.io.IOException {
    // find network interface
    NetworkInterface[] devices = JpcapCaptor.getDeviceList();
    NetworkInterface device = null;

    loop:
    for (NetworkInterface d : devices) {
      for (NetworkInterfaceAddress addr : d.addresses) {
        if (!(addr.address instanceof Inet4Address)) continue;
        byte[] bip = ip.getAddress();
        byte[] subnet = addr.subnet.getAddress();
        byte[] bif = addr.address.getAddress();
        for (int i = 0; i < 4; i++) {
          bip[i] = (byte) (bip[i] & subnet[i]);
          bif[i] = (byte) (bif[i] & subnet[i]);
        }
        if (Arrays.equals(bip, bif)) {
          device = d;
          break loop;
        }
      }
    }

    if (device == null) throw new IllegalArgumentException(ip + " is not a local address");

    // open Jpcap
    JpcapCaptor captor = JpcapCaptor.openDevice(device, 2000, false, 3000);
    captor.setFilter("arp", true);
    JpcapSender sender = captor.getJpcapSenderInstance();

    InetAddress srcip = null;
    for (NetworkInterfaceAddress addr : device.addresses)
      if (addr.address instanceof Inet4Address) {
        srcip = addr.address;
        break;
      }

    byte[] broadcast =
        new byte[] {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255};
    ARPPacket arp = new ARPPacket();
    arp.hardtype = ARPPacket.HARDTYPE_ETHER;
    arp.prototype = ARPPacket.PROTOTYPE_IP;
    arp.operation = ARPPacket.ARP_REQUEST;
    arp.hlen = 6;
    arp.plen = 4;
    arp.sender_hardaddr = device.mac_address;
    arp.sender_protoaddr = srcip.getAddress();
    arp.target_hardaddr = broadcast;
    arp.target_protoaddr = ip.getAddress();

    EthernetPacket ether = new EthernetPacket();
    ether.frametype = EthernetPacket.ETHERTYPE_ARP;
    ether.src_mac = device.mac_address;
    ether.dst_mac = broadcast;
    arp.datalink = ether;

    sender.sendPacket(arp);

    while (true) {
      ARPPacket p = (ARPPacket) captor.getPacket();
      if (p == null) {
        throw new IllegalArgumentException(ip + " is not a local address");
      }
      if (Arrays.equals(p.target_protoaddr, srcip.getAddress())) {
        return p.sender_hardaddr;
      }
    }
  }