Search in sources :

Example 1 with CapturedPackets

use of com.cisco.trex.stateless.model.capture.CapturedPackets in project trex-stateless-gui by cisco-system-traffic-generator.

the class RecordController method handleSavePkts.

public void handleSavePkts(int monitorId) {
    List<CapturedPkt> capturedPkts = new ArrayList<>();
    int pendingPkts = 1;
    while (pendingPkts > 0) {
        try {
            CapturedPackets capturedPackets = pktCaptureService.fetchCapturedPkts(monitorId, 1000);
            pendingPkts = capturedPackets.getPendingPkts();
            capturedPkts.addAll(capturedPackets.getPkts());
        } catch (PktCaptureServiceException e) {
            LOG.error("Unable to fetch packets.", e);
            break;
        }
    }
    File outFile = fileChooser.showSaveDialog(getScene().getWindow());
    if (outFile != null) {
        try {
            dumpPkts(capturedPkts, outFile.getAbsolutePath());
        } catch (Exception e) {
            LOG.error("Unable to dump packets.", e);
        }
    }
}
Also used : CapturedPackets(com.cisco.trex.stateless.model.capture.CapturedPackets) PktCaptureServiceException(com.cisco.trex.stl.gui.services.capture.PktCaptureServiceException) CapturedPkt(com.cisco.trex.stateless.model.capture.CapturedPkt) File(java.io.File) PktCaptureServiceException(com.cisco.trex.stl.gui.services.capture.PktCaptureServiceException) IllegalRawDataException(org.pcap4j.packet.IllegalRawDataException)

Example 2 with CapturedPackets

use of com.cisco.trex.stateless.model.capture.CapturedPackets in project trex-stateless-gui by cisco-system-traffic-generator.

the class MonitorController method startWireshark.

public void startWireshark() {
    final List<Integer> rxPorts = filter.getRxPorts();
    final List<Integer> txPorts = filter.getTxPorts();
    final String bpfFilter = filter.getBPFFilter();
    final String errorTitle = "Start wireshark error";
    List<Integer> portsWithDisabledSM = guardEnabledServiceMode(rxPorts, txPorts);
    if (!portsWithDisabledSM.isEmpty()) {
        String msg = "Unable to start record due to disabled service mode on following ports: " + portsWithDisabledSM.stream().map(Objects::toString).collect(joining(", "));
        TrexAlertBuilder.build().setType(Alert.AlertType.ERROR).setTitle(errorTitle).setHeader(errorTitle).setContent(msg).getAlert().showAndWait();
        return;
    }
    if (rxPorts.isEmpty() && txPorts.isEmpty()) {
        TrexAlertBuilder.build().setType(Alert.AlertType.ERROR).setTitle("Start wireshark error").setHeader("Start wireshark error").setContent("Ports are not specified. Please specify ports in a filter.").getAlert().showAndWait();
        return;
    }
    if (!checkWiresharkLocation()) {
        return;
    }
    final String wireSharkLocation = PreferencesManager.getInstance().getPreferences().getWireSharkLocation();
    executorService.submit(() -> {
        PktDumpService dumpService;
        if (SystemUtils.IS_OS_WINDOWS) {
            dumpService = new WindowsPktDumpService();
        } else {
            dumpService = new UnixPktDumpService();
        }
        try {
            final int wsMonitorId = pktCaptureService.startMonitor(rxPorts, txPorts, bpfFilter, false);
            Process wiresharkProcess = dumpService.init(wireSharkLocation);
            while (wiresharkProcess.isAlive()) {
                CapturedPackets capturedPackets = pktCaptureService.fetchCapturedPkts(wsMonitorId, 500);
                dumpService.dump(capturedPackets);
            }
            pktCaptureService.stopMonitor(wsMonitorId);
        } catch (PktCaptureServiceException e) {
            TrexAlertBuilder.build().setType(Alert.AlertType.ERROR).setTitle(errorTitle).setHeader("Unable to start monitor").setContent(e.getLocalizedMessage()).getAlert().showAndWait();
        } catch (PktDumpServiceInitException e) {
            LOG.error("Unable to initialize pkt dump service", e);
            Platform.runLater(() -> TrexAlertBuilder.build().setType(Alert.AlertType.ERROR).setTitle(errorTitle).setHeader("Unable to initialize pkt dump service").setContent(e.getLocalizedMessage()).getAlert().showAndWait());
        } catch (PktDumpServiceException e) {
            LOG.error("Unable to dump packet.", e);
        } finally {
            dumpService.close();
        }
    });
}
Also used : CapturedPackets(com.cisco.trex.stateless.model.capture.CapturedPackets)

Aggregations

CapturedPackets (com.cisco.trex.stateless.model.capture.CapturedPackets)2 CapturedPkt (com.cisco.trex.stateless.model.capture.CapturedPkt)1 PktCaptureServiceException (com.cisco.trex.stl.gui.services.capture.PktCaptureServiceException)1 File (java.io.File)1 IllegalRawDataException (org.pcap4j.packet.IllegalRawDataException)1