Search in sources :

Example 1 with EthernetPacket

use of org.pcap4j.packet.EthernetPacket in project trex-stateless-gui by cisco-system-traffic-generator.

the class PortInfoTabConfig method runPingCmd.

private void runPingCmd(Event event) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.submit(() -> {
        trexClient.serviceMode(port.getIndex(), true);
        savedPingIPv4 = textFieldTabConfigPortPingIPv4.getText();
        guiLogger.appendText(LogType.PING, " Start ping " + savedPingIPv4 + ":");
        AsyncResponseManager.getInstance().muteLogger();
        try {
            int icmp_id = new Random().nextInt(100);
            for (int icmp_sec = 1; icmp_sec < 6; icmp_sec++) {
                EthernetPacket reply = trexClient.sendIcmpEcho(port.getIndex(), savedPingIPv4, icmp_id, icmp_sec, 1000);
                if (reply != null) {
                    IpV4Packet ip = reply.get(IpV4Packet.class);
                    String ttl = String.valueOf(ip.getHeader().getTtlAsInt());
                    IcmpV4CommonPacket echoReplyPacket = reply.get(IcmpV4CommonPacket.class);
                    IcmpV4Type replyType = echoReplyPacket.getHeader().getType();
                    if (IcmpV4Type.ECHO_REPLY.equals(replyType)) {
                        guiLogger.appendText(LogType.PING, " Reply from " + savedPingIPv4 + " size=" + reply.getRawData().length + " ttl=" + ttl + " icmp_sec=" + icmp_sec);
                    } else if (IcmpV4Type.DESTINATION_UNREACHABLE.equals(replyType)) {
                        guiLogger.appendText(LogType.PING, " Destination host unreachable");
                    }
                } else {
                    guiLogger.appendText(LogType.PING, " Request timeout for icmp_seq " + icmp_sec);
                }
            }
        } catch (UnknownHostException e) {
            guiLogger.appendText(LogType.PING, " Unknown host");
        } finally {
            trexClient.serviceMode(port.getIndex(), false);
            AsyncResponseManager.getInstance().unmuteLogger();
        }
    });
}
Also used : IcmpV4CommonPacket(org.pcap4j.packet.IcmpV4CommonPacket) IcmpV4Type(org.pcap4j.packet.namednumber.IcmpV4Type) Random(java.util.Random) UnknownHostException(java.net.UnknownHostException) EthernetPacket(org.pcap4j.packet.EthernetPacket) ExecutorService(java.util.concurrent.ExecutorService) IpV4Packet(org.pcap4j.packet.IpV4Packet) Paint(javafx.scene.paint.Paint)

Example 2 with EthernetPacket

use of org.pcap4j.packet.EthernetPacket in project trex-stateless-gui by cisco-system-traffic-generator.

the class RecordController method toEtherPkt.

private EthernetPacket toEtherPkt(CapturedPkt pkt) {
    byte[] pktBinary = Base64.getDecoder().decode(pkt.getBinary());
    EthernetPacket ethPkt = null;
    try {
        ethPkt = EthernetPacket.newPacket(pktBinary, 0, pktBinary.length);
    } catch (IllegalRawDataException e) {
        LOG.error("Save PCAP. Unable to parse pkt from server.", e);
        return null;
    }
    return ethPkt;
}
Also used : IllegalRawDataException(org.pcap4j.packet.IllegalRawDataException) EthernetPacket(org.pcap4j.packet.EthernetPacket)

Example 3 with EthernetPacket

use of org.pcap4j.packet.EthernetPacket in project trex-stateless-gui by cisco-system-traffic-generator.

the class TrexEthernetPacket method fixPacketLength.

private void fixPacketLength() {
    try {
        EthernetPacket newPacket = packet;
        if (packet.getRawData().length < getLength()) {
            byte[] pad = new byte[getLength() - packet.getRawData().length];
            newPacket = EthernetPacket.newPacket(ArrayUtils.addAll(packet.getRawData(), pad), 0, getLength());
        } else {
            newPacket = EthernetPacket.newPacket(packet.getRawData(), 0, getLength());
        }
        setPacket((EthernetPacket) newPacket);
    } catch (IllegalRawDataException ex) {
        Logger.getLogger(TrexEthernetPacket.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : IllegalRawDataException(org.pcap4j.packet.IllegalRawDataException) EthernetPacket(org.pcap4j.packet.EthernetPacket)

Example 4 with EthernetPacket

use of org.pcap4j.packet.EthernetPacket in project trex-stateless-gui by cisco-system-traffic-generator.

the class PortLayerConfiguration method runPingCmd.

private void runPingCmd(Event event) {
    if (model.getPortStatus().equalsIgnoreCase("tx")) {
        guiLogger.appendText(LogType.ERROR, "Port " + model.getIndex() + " is in TX mode. Please stop traffic first.");
        return;
    }
    if (Strings.isNullOrEmpty(pingDestination.getText())) {
        guiLogger.appendText(LogType.ERROR, "Empty ping destination address.");
        return;
    }
    final String targetIP = pingDestination.getText();
    if (!targetIP.contains(":") && !model.getL3LayerConfiguration().getState().equalsIgnoreCase("resolved")) {
        guiLogger.appendText(LogType.ERROR, "ARP resolution required. Configure L3IPv4 mode properly.");
        return;
    }
    pingCommandBtn.setDisable(true);
    Task<Void> pingTask = new Task<Void>() {

        @Override
        public Void call() {
            TRexClient trexClient = ConnectionManager.getInstance().getTrexClient();
            trexClient.serviceMode(model.getIndex(), true);
            guiLogger.appendText(LogType.PING, " Start ping " + targetIP);
            AsyncResponseManager.getInstance().muteLogger();
            AsyncResponseManager.getInstance().suppressIncomingEvents(true);
            try {
                int icmp_id = new Random().nextInt(100);
                for (int icmp_sec = 1; icmp_sec < 6; icmp_sec++) {
                    EthernetPacket reply = null;
                    if (targetIP.contains(":")) {
                        // IPv6
                        reply = trexClient.sendIcmpV6Echo(model.getIndex(), targetIP, icmp_id, icmp_sec, 2);
                        if (reply != null) {
                            IcmpV6CommonPacket icmpV6CommonPacket = reply.get(IcmpV6CommonPacket.class);
                            IcmpV6Type icmpReplyType = icmpV6CommonPacket.getHeader().getType();
                            String msg = null;
                            if (IcmpV6Type.ECHO_REPLY.equals(icmpReplyType)) {
                                msg = " Reply from " + targetIP + " size=" + reply.getRawData().length + " icmp_sec=" + icmp_sec;
                            } else if (IcmpV6Type.DESTINATION_UNREACHABLE.equals(icmpReplyType)) {
                                msg = " Destination host unreachable";
                            }
                            guiLogger.appendText(LogType.PING, msg);
                        } else {
                            guiLogger.appendText(LogType.PING, "Request timeout.");
                        }
                    } else {
                        // IPv4
                        reply = trexClient.sendIcmpEcho(model.getIndex(), targetIP, icmp_id, icmp_sec, 1000);
                        if (reply != null) {
                            IpV4Packet ip = reply.get(IpV4Packet.class);
                            String ttl = String.valueOf(ip.getHeader().getTtlAsInt());
                            IcmpV4CommonPacket echoReplyPacket = reply.get(IcmpV4CommonPacket.class);
                            IcmpV4Type replyType = echoReplyPacket.getHeader().getType();
                            if (IcmpV4Type.ECHO_REPLY.equals(replyType)) {
                                guiLogger.appendText(LogType.PING, " Reply from " + targetIP + " size=" + reply.getRawData().length + " ttl=" + ttl + " icmp_sec=" + icmp_sec);
                            } else if (IcmpV4Type.DESTINATION_UNREACHABLE.equals(replyType)) {
                                guiLogger.appendText(LogType.PING, " Destination host unreachable");
                            }
                        } else {
                            guiLogger.appendText(LogType.PING, " Request timeout for icmp_seq " + icmp_sec);
                        }
                    }
                }
                guiLogger.appendText(LogType.PING, " Ping finished.");
            } catch (UnknownHostException e) {
                guiLogger.appendText(LogType.PING, " Unknown host");
            } catch (ServiceModeRequiredException e) {
                e.printStackTrace();
            } finally {
                pingCommandBtn.setDisable(false);
                trexClient.serviceMode(model.getIndex(), false);
                AsyncResponseManager.getInstance().unmuteLogger();
                AsyncResponseManager.getInstance().suppressIncomingEvents(false);
            }
            return null;
        }
    };
    pingTask.setOnSucceeded(e -> pingCommandBtn.setDisable(false));
    new Thread(pingTask).start();
}
Also used : Task(javafx.concurrent.Task) IcmpV6CommonPacket(org.pcap4j.packet.IcmpV6CommonPacket) IcmpV4CommonPacket(org.pcap4j.packet.IcmpV4CommonPacket) TRexClient(com.cisco.trex.stateless.TRexClient) UnknownHostException(java.net.UnknownHostException) ServiceModeRequiredException(com.cisco.trex.stateless.exception.ServiceModeRequiredException) EthernetPacket(org.pcap4j.packet.EthernetPacket) IcmpV4Type(org.pcap4j.packet.namednumber.IcmpV4Type) IcmpV6Type(org.pcap4j.packet.namednumber.IcmpV6Type) IpV4Packet(org.pcap4j.packet.IpV4Packet)

Aggregations

EthernetPacket (org.pcap4j.packet.EthernetPacket)4 UnknownHostException (java.net.UnknownHostException)2 IcmpV4CommonPacket (org.pcap4j.packet.IcmpV4CommonPacket)2 IllegalRawDataException (org.pcap4j.packet.IllegalRawDataException)2 IpV4Packet (org.pcap4j.packet.IpV4Packet)2 IcmpV4Type (org.pcap4j.packet.namednumber.IcmpV4Type)2 TRexClient (com.cisco.trex.stateless.TRexClient)1 ServiceModeRequiredException (com.cisco.trex.stateless.exception.ServiceModeRequiredException)1 Random (java.util.Random)1 ExecutorService (java.util.concurrent.ExecutorService)1 Task (javafx.concurrent.Task)1 Paint (javafx.scene.paint.Paint)1 IcmpV6CommonPacket (org.pcap4j.packet.IcmpV6CommonPacket)1 IcmpV6Type (org.pcap4j.packet.namednumber.IcmpV6Type)1