当前位置: 首页>>代码示例>>Java>>正文


Java EthernetPacket类代码示例

本文整理汇总了Java中org.pcap4j.packet.EthernetPacket的典型用法代码示例。如果您正苦于以下问题:Java EthernetPacket类的具体用法?Java EthernetPacket怎么用?Java EthernetPacket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


EthernetPacket类属于org.pcap4j.packet包,在下文中一共展示了EthernetPacket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getIpV4Packet_pppoe

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
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;
}
 
开发者ID:gmirr,项目名称:finalspeed-91yun,代码行数:22,代码来源:CapEnv.java

示例2: pingTest

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
/**
 * This is a specific test which is not related to standalone Java SDK
 */
@Test
@Ignore
public void pingTest() {
    List<Port> ports = client.getPorts();
    Port port = ports.get(0);
    try {
        client.acquirePort(port.getIndex(), true);
        client.serviceMode(port.getIndex(), true);
        String host = "192.168.9.1";
        EthernetPacket icmpReply = client.sendIcmpEcho(port.getIndex(), host, 10, 10, 1000);
        Assert.assertNotNull(icmpReply);
    } catch (UnknownHostException ignored) {
    } finally {
        client.serviceMode(port.getIndex(), false);
        client.releasePort(port.getIndex());
    }
}
 
开发者ID:cisco-system-traffic-generator,项目名称:trex-java-sdk,代码行数:21,代码来源:TRexClientTest.java

示例3: _getPacketBuilder4IPv4

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
private EthernetPacket.Builder _getPacketBuilder4IPv4(InetAddress remoteAddress, MacAddress localMacAddress, InetAddress
        localIpAdress) {
    ArpPacket.Builder arpBuilder = new ArpPacket.Builder();
    arpBuilder
            .hardwareType(ArpHardwareType.ETHERNET)
            .protocolType(EtherType.IPV4)
            .hardwareAddrLength((byte) MacAddress.SIZE_IN_BYTES)
            .protocolAddrLength((byte) ByteArrays.INET4_ADDRESS_SIZE_IN_BYTES)
            .srcHardwareAddr(localMacAddress)
            .srcProtocolAddr(localIpAdress)
            .dstHardwareAddr(MacAddress.ETHER_BROADCAST_ADDRESS)
            .operation(ArpOperation.REQUEST)
            .dstProtocolAddr(remoteAddress);


    EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
    etherBuilder.dstAddr(MacAddress.ETHER_BROADCAST_ADDRESS)
            .srcAddr(localMacAddress)
            .type(EtherType.ARP)
            .payloadBuilder(arpBuilder)
            .paddingAtBuild(true);

    return etherBuilder;
}
 
开发者ID:gaoxingliang,项目名称:mac-address-detector-java,代码行数:25,代码来源:MacAddressHelper.java

示例4: getIpV4Packet_pppoe

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
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("Length don't match!");
		}
	}
	return ipV4Packet;
}
 
开发者ID:RCD-Y,项目名称:FinalSpeed-X,代码行数:22,代码来源:CapEnv.java

示例5: handleExportPcapFile

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
/**
 * 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);
    }
}
 
开发者ID:exalt-tech,项目名称:trex-stateless-gui,代码行数:26,代码来源:PacketTableView.java

示例6: getPacketInfo

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
/**
 *
 * @param packetBinary
 * @return
 */
public PacketInfo getPacketInfo(String packetBinary) {

    PacketInfo packetInfo = null;

    try {
        byte[] pkt = Base64.decodeBase64(packetBinary);
        Packet packet = EthernetPacket.newPacket(pkt, 0, pkt.length);
        packetInfo = getPacketTypeText(packet);
    } catch (IllegalRawDataException ex) {
        LOG.error("Error reading packet info", ex);
    }

    return packetInfo;

}
 
开发者ID:exalt-tech,项目名称:trex-stateless-gui,代码行数:21,代码来源:TrafficProfile.java

示例7: tag

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
public static Packet tag(Packet packet, int vid) {
  EthernetPacket ep = packet.get(EthernetPacket.class);
  if (ep == null) {
    return packet;
  }

  Dot1qVlanTagPacket.Builder vb = new Dot1qVlanTagPacket.Builder();
  vb.vid((short)vid)
    .type(ep.getHeader().getType())
    .payloadBuilder(new SimpleBuilder(ep.getPayload()));

  Packet.Builder pb = packet.getBuilder();
  pb.get(EthernetPacket.Builder.class)
    .type(EtherType.DOT1Q_VLAN_TAGGED_FRAMES)
    .payloadBuilder(vb);

  return pb.build();
}
 
开发者ID:kaitoy,项目名称:sneo,代码行数:19,代码来源:Dot1qVlanTagHelper.java

示例8: arpTest

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
/**
 * This is a specific test which is not related to standalone Java SDK
 */
@Test
@Ignore
public void arpTest() {
    List<Port> ports = client.getPorts();
    Port port = ports.get(0);
    Port port1 = ports.get(1);
    client.acquirePort(port.getIndex(), true);
    client.acquirePort(port1.getIndex(), true);
    
    client.serviceMode(port.getIndex(), true);
    client.serviceMode(port1.getIndex(), true);

    client.removeRxQueue(port.getIndex());
    client.setRxQueue(port.getIndex(), 5);
        
    EthernetPacket pkt = buildArpPkt();
    client.sendPacket(port.getIndex(), pkt);

    Predicate<EthernetPacket> arpReplyFilter = etherPkt -> {
        if(etherPkt.contains(ArpPacket.class)) {
            ArpPacket arp = (ArpPacket) etherPkt.getPayload();
            if (ArpOperation.REPLY.equals(arp.getHeader().getOperation())) {
                return true;
            }
        }
        return false;
    };
    List<EthernetPacket> pkts = client.getRxQueue(port.getIndex(), arpReplyFilter);
    client.removeRxQueue(port.getIndex());
    client.serviceMode(port.getIndex(), false);
    client.serviceMode(port1.getIndex(), false);
    Assert.assertTrue(pkts.size() > 0);
}
 
开发者ID:cisco-system-traffic-generator,项目名称:trex-java-sdk,代码行数:37,代码来源:TRexClientTest.java

示例9: sendPacketTest

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
@Test
@Ignore
public void sendPacketTest() throws TRexTimeoutException{
    List<Port> ports = client.getPorts();
    Port port = ports.get(0);
    
    client.acquirePort(port.getIndex(), true);
    client.serviceMode(port.getIndex(), true);
    client.setRxQueue(port.getIndex(), 10);
    List<EthernetPacket> pkts = client.getRxQueue(port.getIndex(), p -> true);
}
 
开发者ID:cisco-system-traffic-generator,项目名称:trex-java-sdk,代码行数:12,代码来源:TRexClientTest.java

示例10: buildIdealPkt

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
private static EthernetPacket buildIdealPkt(String pkt) {
    byte[] pktBin = Base64.getDecoder().decode(pkt);
    try {
        return EthernetPacket.newPacket(pktBin, 0, pktBin.length);
    } catch (IllegalRawDataException e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:cisco-system-traffic-generator,项目名称:trex-java-sdk,代码行数:10,代码来源:TRexClientTest.java

示例11: buildArpPkt

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
private static EthernetPacket buildArpPkt() {
    ArpPacket.Builder arpBuilder = new ArpPacket.Builder();
    MacAddress srcMac = MacAddress.getByName("00:50:56:94:21:df");
    try {
        String strSrcIpAddress = "192.168.9.27";
        String strDstIpAddress = "192.168.9.28";
        arpBuilder
                .hardwareType(ArpHardwareType.ETHERNET)
                .protocolType(EtherType.IPV4)
                .hardwareAddrLength((byte) MacAddress.SIZE_IN_BYTES)
                .protocolAddrLength((byte) ByteArrays.INET4_ADDRESS_SIZE_IN_BYTES)
                .operation(ArpOperation.REQUEST)
                .srcHardwareAddr(srcMac)
                .srcProtocolAddr(InetAddress.getByName(strSrcIpAddress))
                .dstHardwareAddr(MacAddress.ETHER_BROADCAST_ADDRESS)
                .dstProtocolAddr(InetAddress.getByName(strDstIpAddress));
    } catch (UnknownHostException e) {
        throw new IllegalArgumentException(e);
    }

    EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
    etherBuilder.dstAddr(MacAddress.ETHER_BROADCAST_ADDRESS)
            .srcAddr(srcMac)
            .type(EtherType.ARP)
            .payloadBuilder(arpBuilder)
            .paddingAtBuild(true);

    return etherBuilder.build();
}
 
开发者ID:cisco-system-traffic-generator,项目名称:trex-java-sdk,代码行数:30,代码来源:TRexClientTest.java

示例12: processEthernetPacket

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
public static void processEthernetPacket(Packet packet, PcapFileSummary pcapFileSummary, PacketInfo packetInfo, Mode mode) {
    if (packet == null) {
        return; // skip empty packets
    }
    try {
        log.trace("Converting to ethernet packet");
        EthernetPacket ethernetPacket = EthernetPacket.newPacket(packet.getRawData(), 0, packet.length());
        EthernetPacket.EthernetHeader ethernetHeader = ethernetPacket.getHeader();
        MacAddress sourceMac = ethernetHeader.getSrcAddr();
        log.trace("Source MAC: " + sourceMac);
        packetInfo.put(PacketInfo.SOURCE_MAC, sourceMac.toString());
        MacAddress destMac = ethernetHeader.getDstAddr();
        log.trace("Destination MAC: " + destMac);
        packetInfo.put(PacketInfo.DESTINATION_MAC, destMac.toString());
        EtherType etherType = ethernetHeader.getType();
        log.trace("EtherType: " + etherType.toString());
        packetInfo.put(PacketInfo.ETHERTYPE, etherType.toString());
        Packet payload = ethernetPacket.getPayload();
        if (etherType == EtherType.IPV4) {
            IpPacketProcessor.processIpv4Packet(payload, pcapFileSummary, packetInfo, mode);
        } else if (etherType == EtherType.IPV6) {
            IpPacketProcessor.processIpv6Packet(payload, pcapFileSummary, packetInfo, mode);
        } else if ((mode == Mode.POSSIBLE_ATTACKS_ANALYSIS) && (etherType == EtherType.ARP)) {
            pcapFileSummary.nonIpPacketCount++;
            ArpPacketProcessor.processArpPacket(payload, pcapFileSummary);
        } else {
            pcapFileSummary.nonIpPacketCount++;
            log.trace("Skipping packet with EtherType: " + etherType);
        }
    } catch (IllegalRawDataException e) {
        log.error("Exception occurred while processing a packet. Exception was: " + e);
    }
}
 
开发者ID:rmcnew,项目名称:LiquidFortressPacketAnalyzer,代码行数:34,代码来源:PcapFileProcessor.java

示例13: buildPacket

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
/**
 *
 * @param builder
 */
public void buildPacket(AbstractPacket.AbstractBuilder builder) {

    if (builder == null) {

        this.type = EtherType.getInstance((short) 0xFFFF);
    }

    ethernetBuilder = new EthernetPacket.Builder();
    ethernetBuilder.dstAddr(dstAddr)
            .srcAddr(srcAddr)
            .type(type)
            .payloadBuilder(builder);

    byte[] pad;
    if (builder == null || addPad) {

        if (payload != null) {
            String payloadString = payload.getPayloadType().getPadPayloadString(payload.getPayloadPattern(), 100000);
            pad = new BigInteger(payloadString, 16).toByteArray();
        } else {
            pad = new byte[0];
        }

        ethernetBuilder.paddingAtBuild(false).pad(pad);
        this.packet = ethernetBuilder.build();
        fixPacketLength();
    } else {
        ethernetBuilder.paddingAtBuild(true);
        this.packet = ethernetBuilder.build();
    }
}
 
开发者ID:exalt-tech,项目名称:trex-stateless-gui,代码行数:36,代码来源:TrexEthernetPacket.java

示例14: fixPacketLength

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
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);
    }
}
 
开发者ID:exalt-tech,项目名称:trex-stateless-gui,代码行数:16,代码来源:TrexEthernetPacket.java

示例15: handleMouseClickedEvent

import org.pcap4j.packet.EthernetPacket; //导入依赖的package包/类
/**
 * 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);
            }
        }
    }
}
 
开发者ID:exalt-tech,项目名称:trex-stateless-gui,代码行数:32,代码来源:PacketHex.java


注:本文中的org.pcap4j.packet.EthernetPacket类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。