org.pcap4j.packet.IllegalRawDataException Java Examples

The following examples show how to use org.pcap4j.packet.IllegalRawDataException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PacketTableView.java    From trex-stateless-gui with Apache License 2.0 6 votes vote down vote up
/**
 * Export stream to pcap file
 */
public void handleExportPcapFile() {
    try {
        Profile p = tabledata.getProfiles().get(streamPacketTableView.getSelectionModel().getSelectedIndex());
        String packetBinary = p.getStream().getPacket().getBinary();

        byte[] pkt = Base64.decodeBase64(packetBinary);
        Packet packet = EthernetPacket.newPacket(pkt, 0, pkt.length);
        File pcapFile = File.createTempFile("temp-file-name", ".pcap");
        PcapHandle handle = Pcaps.openDead(DataLinkType.EN10MB, 65536);
        PcapDumper dumper = handle.dumpOpen(pcapFile.getAbsolutePath());
        Timestamp ts = new Timestamp(0);
        dumper.dump(packet, ts);
        dumper.close();
        handle.close();

        String fileName = p.getName() + ".pcap";
        Window owner = streamPacketTableView.getScene().getWindow();
        FileManager.exportFile("Save Pcap File", fileName, pcapFile, owner, FileType.PCAP);
    } catch (IllegalRawDataException | IOException | PcapNativeException | NotOpenException ex) {
        LOG.error("Error during generate JSON file", ex);
    }
}
 
Example #2
Source File: CapEnv.java    From finalspeed-91yun with GNU General Public License v2.0 6 votes vote down vote up
IpV4Packet getIpV4Packet_pppoe(EthernetPacket packet_eth) throws IllegalRawDataException{
	IpV4Packet ipV4Packet=null;
	byte[] pppData=packet_eth.getPayload().getRawData();
	if(pppData.length>8&&pppData[8]==0x45){
		byte[] b2=new byte[2];
		System.arraycopy(pppData, 4, b2, 0, 2);
		short len=(short) ByteShortConvert.toShort(b2, 0);
		int ipLength=toUnsigned(len)-2;
		byte[] ipData=new byte[ipLength];
		//设置ppp参数
		PacketUtils.pppHead_static[2]=pppData[2];
		PacketUtils.pppHead_static[3]=pppData[3];
		if(ipLength==(pppData.length-8)){
			System.arraycopy(pppData, 8, ipData, 0, ipLength);
			ipV4Packet=IpV4Packet.newPacket(ipData, 0, ipData.length);
		}else {
			MLog.println("长度不符!");
		}
	}
	return ipV4Packet;
}
 
Example #3
Source File: UnixPktDumpService.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
private EthernetPacket toEtherPkt(byte[] pkt) {
    EthernetPacket ethPkt = null;
    try {
        ethPkt = EthernetPacket.newPacket(pkt, 0, pkt.length);
    } catch (IllegalRawDataException e) {
        LOG.error("Save PCAP. Unable to parse pkt from server.", e);
        return null;
    }
    return ethPkt;
}
 
Example #4
Source File: RecordController.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #5
Source File: TrexEthernetPacket.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #6
Source File: PacketHex.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Handle mouse clicked event
 */
private void handleMouseClickedEvent() {
    TreeTableRow<HexData> selectedRow = (TreeTableRow<HexData>) textField.getParent().getParent();
    if (!selectedRow.getTreeItem().getValue().getOffset().contains("-")) {
        String originalLine = textField.getText();
        String selectedHex = textField.getSelectedText().trim();
        String replacedHex = showDialog(selectedHex);
        if (replacedHex != null) {
            try {
                textField.replaceSelection(replacedHex.toUpperCase());
                String payLoad = hexToASCII(textField.getText());
                TreeTableRow<HexData> hexTable = (TreeTableRow<HexData>) textField.getParent().getParent();
                TreeItem<HexData> selectedItem = hexTable.getTreeItem();
                selectedItem.setValue(new HexData(selectedItem.getValue().getOffset(), textField.getText(), packetParser.formatPayLoad(payLoad)));
                String originalHex = getPacketHexFromList();
                if (selectedItem.getValue().getOffset().contains("-")) {
                    originalHex = originalHex.replaceAll(originalLine.replaceAll(" ", "").replaceAll("\n", ""), textField.getText().replaceAll(" ", "").replaceAll("\n", ""));
                }
                byte[] rawdata = DatatypeConverter.parseHexBinary(originalHex);
                EthernetPacket p = EthernetPacket.newPacket(rawdata, 0, rawdata.length);
                packetParser.parsePacket(p, packetInfo);
                treeRoot.getChildren().clear();
                setData(packetInfo);
            } catch (IllegalRawDataException ex) {
                java.util.logging.Logger.getLogger(PacketHex.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
 
Example #7
Source File: TrexEthernetPacketTest.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Test of buildPacket method, of class TrexEthernetPacket.
 */
@Test
@Parameters({"macSrcAddress", "macDstAddress", "packetLength", "expectedHex"})
public void testEthernetPacketWithoutVlan(String macSrcAddress, String macDstAddress, int packetLength, String expectedHex) throws IOException, IllegalRawDataException {

    LOG.info("------------Testing Ethernet packet");
    // build ethernet packet

    LOG.info("Building Ethernet packet");
    AbstractPacket.AbstractBuilder builder = null;
    TrexEthernetPacket instance = new TrexEthernetPacket();
    instance.setSrcAddr(macSrcAddress);
    instance.setDstAddr(macDstAddress);
    instance.setLength(packetLength);
    instance.buildPacket(builder);

    LOG.info("Encoding packet data");
    
    // Encode packet data
    String encodedBinaryPacket = packetUtil.getEncodedPacket(instance.getPacket().getRawData());
    LOG.info("Decoding packets and returning packet data information");

    // Decode and return packet info
    PacketInfo packetInfo = packetUtil.getPacketInfoData(encodedBinaryPacket);
    LOG.info("Verifying packet data");

    // Assert mac src/destination address
    Assert.assertEquals(macSrcAddress, packetInfo.getSrcMac(), "Invalid MAC source address. ");
    Assert.assertEquals(macDstAddress, packetInfo.getDestMac(), "Invalid MAC destination address. ");

    // Verify packet length
    Packet packet = packetUtil.getPacketFromEncodedString(encodedBinaryPacket);
    Assert.assertEquals(packetLength, packetUtil.getPacketLength(packet), "Invalid Packet length. ");
    
    // Verify packet data
    String packetHex = DatatypeConverter.printHexBinary(packet.getRawData());
    Assert.assertEquals(expectedHex.toLowerCase(), packetHex.toLowerCase(), "Invalid Packet hex. ");

}
 
Example #8
Source File: CustomTcpSackOption.java    From NSS with Apache License 2.0 3 votes vote down vote up
/**
 * A static factory method.
 * This method validates the arguments by {@link ByteArrays#validateBounds(byte[], int, int)},
 * which may throw exceptions undocumented here.
 *
 * @param rawData rawData
 * @param offset offset
 * @param length length
 * @return a new TcpSackOption object.
 * @throws IllegalRawDataException if parsing the raw data fails.
 */
public static CustomTcpSackOption newInstance(
  byte[] rawData, int offset, int length
) throws IllegalRawDataException {
  ByteArrays.validateBounds(rawData, offset, length);
  return new CustomTcpSackOption(rawData, offset, length);
}
 
Example #9
Source File: CustomTcpSackOption.java    From finalspeed-91yun with GNU General Public License v2.0 3 votes vote down vote up
/**
 * A static factory method.
 * This method validates the arguments by {@link ByteArrays#validateBounds(byte[], int, int)},
 * which may throw exceptions undocumented here.
 *
 * @param rawData rawData
 * @param offset offset
 * @param length length
 * @return a new TcpSackOption object.
 * @throws IllegalRawDataException if parsing the raw data fails.
 */
public static CustomTcpSackOption newInstance(
  byte[] rawData, int offset, int length
) throws IllegalRawDataException {
  ByteArrays.validateBounds(rawData, offset, length);
  return new CustomTcpSackOption(rawData, offset, length);
}
 
Example #10
Source File: CustomTcpSackOption.java    From finalspeed with GNU General Public License v2.0 3 votes vote down vote up
/**
 * A static factory method.
 * This method validates the arguments by {@link ByteArrays#validateBounds(byte[], int, int)},
 * which may throw exceptions undocumented here.
 *
 * @param rawData rawData
 * @param offset  offset
 * @param length  length
 * @return a new TcpSackOption object.
 * @throws IllegalRawDataException if parsing the raw data fails.
 */
public static CustomTcpSackOption newInstance(
        byte[] rawData, int offset, int length
) throws IllegalRawDataException {
    ByteArrays.validateBounds(rawData, offset, length);
    return new CustomTcpSackOption(rawData, offset, length);
}
 
Example #11
Source File: PacketUtil.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 * @param packet
 * @return packet length
 * @throws org.pcap4j.packet.IllegalRawDataException
 */
public int getPacketLength(Packet packet) throws IllegalRawDataException {
    return trafficProfile.getPacketTypeText(packet).getLength();
}
 
Example #12
Source File: PacketUtil.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 * Get packet from encoded string
 *
 * @param encodedBinaryPacket
 * @return packet
 * @throws IllegalRawDataException
 */
public Packet getPacketFromEncodedString(String encodedBinaryPacket) throws IllegalRawDataException {
    byte[] pkt = Base64.decodeBase64(encodedBinaryPacket);
    return EthernetPacket.newPacket(pkt, 0, pkt.length);
}