org.pcap4j.packet.namednumber.EtherType Java Examples

The following examples show how to use org.pcap4j.packet.namednumber.EtherType. 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: TrexVlanPacket.java    From trex-stateless-gui with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param builder
 */
public void buildPacket(AbstractPacket.AbstractBuilder builder) {

    if (!this.type.equals(EtherType.getInstance((short) 0x0000))) {
        // do nothing if type not 0x0000
    } else if (builder == null) {
        this.type = EtherType.getInstance((short) 0xFFFF);
    } else {
        this.type = EtherType.IPV4;
    }
    db = new Dot1qVlanTagPacket.Builder();
    db.priority(priority)
            .cfi(cfi)
            .vid(vid)
            .type(type)
            .payloadBuilder(builder);
    this.packet = db.build();
}
 
Example #2
Source File: MonitorController.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> parseDot1Q(Stack<String> headers, Dot1qVlanTagPacket pkt) {
    headers.push("Dot1Q");
    
    EtherType etherType = pkt.getHeader().getType();
    
    if(EtherType.DOT1Q_VLAN_TAGGED_FRAMES.equals(etherType)) {
        return parseDot1Q(headers, (Dot1qVlanTagPacket) pkt.getPayload());
    } else if (EtherType.IPV4.equals(etherType)) {
        return parseIP(headers, (IpV4Packet) pkt.getPayload());
    } else {
        return parseIP(headers, (IpV6Packet) pkt.getPayload());
    }

}
 
Example #3
Source File: TrexEthernetPacket.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 *
 */
public TrexEthernetPacket() {

    this.srcAddr = MacAddress.getByName("00:00:00:00:00:00");
    this.dstAddr = MacAddress.getByName("00:00:00:00:00:00");
    this.type = EtherType.IPV4;
    this.addPad = false;
}
 
Example #4
Source File: TrexEthernetPacket.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @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();
    }
}
 
Example #5
Source File: TrexVlanPacket.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 *
 */
public TrexVlanPacket() {
    this.priority = (byte) 0;
    this.cfi = false;
    this.vid = (short) 0;
    this.type = EtherType.getInstance((short) 0x0000);
}
 
Example #6
Source File: PacketUtil.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Add VLan to the generated packet
 *
 * @param ethernetPacket
 * @param ipV4Packet
 */
public void addVlanToPacket(TrexEthernetPacket ethernetPacket, IpV4Packet.Builder ipV4Packet) {
    LOG.info("Add VLAN data");
    ethernetPacket.setType(EtherType.DOT1Q_VLAN_TAGGED_FRAMES.value());
    TrexVlanPacket vlanPacket = new TrexVlanPacket();
    if (ipV4Packet == null) {
        ethernetPacket.setAddPad(true);
        vlanPacket.setType((short) 0xFFFF);
    } else {
        vlanPacket.setType(EtherType.IPV4.value());
    }
    vlanPacket.buildPacket(ipV4Packet);
    ethernetPacket.buildPacket(vlanPacket.getBuilder());
}
 
Example #7
Source File: DhcpPacketListener.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
public void gotPacket(Packet rawPacket) {
    try {
        LOG.info("Read raw " + rawPacket);
        IpV4Packet ipPacket = rawPacket.get(IpV4Packet.class);
        UdpPacket udpPacket = rawPacket.get(UdpPacket.class);
        byte[] dhcpData = udpPacket.getPayload().getRawData();
        InetSocketAddress remoteAddress = new InetSocketAddress(ipPacket.getHeader().getSrcAddr(), udpPacket.getHeader().getSrcPort().valueAsInt());
        InetSocketAddress localAddress = new InetSocketAddress(ipPacket.getHeader().getDstAddr(), udpPacket.getHeader().getDstPort().valueAsInt());
        DhcpRequestContext context = new DhcpRequestContext(interfaceAddresses, remoteAddress, localAddress);
        DhcpMessage request = decoder.decode(ByteBuffer.wrap(dhcpData));
        LOG.info("Read DHCP " + request);
        DhcpMessage reply = service.getReplyFor(context, request);
        if (reply == null)
            return;
        byte[] replyData = new byte[1536];
        ByteBuffer buffer = ByteBuffer.wrap(replyData);
        encoder.encode(buffer, reply);
        replyData = Arrays.copyOf(replyData, buffer.position());    // Truncate array to writer position.
        UnknownPacket.Builder dhcpBuilder = new UnknownPacket.Builder()
                .rawData(replyData);
        UdpPacket.Builder udpBuilder = new UdpPacket.Builder()
                .payloadBuilder(dhcpBuilder)
                .srcPort(udpPacket.getHeader().getDstPort())
                .dstPort(udpPacket.getHeader().getSrcPort())
                .correctChecksumAtBuild(true)
                .correctLengthAtBuild(true);
        IpV4Packet.Builder ipBuilder = new IpV4Packet.Builder()
                .payloadBuilder(udpBuilder)
                .srcAddr(null) // TODO
                .dstAddr(null) // TODO
                .protocol(IpNumber.UDP)
                .correctChecksumAtBuild(true)
                .correctLengthAtBuild(true)
                .paddingAtBuild(true);
        EthernetPacket.Builder ethernetBuilder = new EthernetPacket.Builder()
                .payloadBuilder(ipBuilder)
                .type(EtherType.IPV4)
                .paddingAtBuild(true);
        Packet replyPacket = ethernetBuilder.build();
        // handle.sendPacket(replyPacket);
    } catch (Exception e) {
        LOG.error("DHCP failed", e);
    }
}
 
Example #8
Source File: PacketUtils.java    From NSS with Apache License 2.0 4 votes vote down vote up
public static Packet buildIpV4(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		IpV4Packet.Builder builder_ipv4){

	org.pcap4j.packet.Packet.Builder builder=null;
	EtherType etherType=null;
	Packet p=null;
	if(ppp){
		etherType= EtherType.PPPOE_SESSION_STAGE;
		
		UnknownPacket.Builder pppBuilder=new UnknownPacket.Builder();
		byte[] ipData=builder_ipv4.build().getRawData();
		
		byte[] lenb=new byte[2];
		ByteShortConvert.toByteArray((short) (ipData.length+2), lenb, 0);
		
		byte[] pppHead=new byte[8];
		System.arraycopy(pppHead_static, 0, pppHead, 0, pppHead.length);
		System.arraycopy(lenb, 0, pppHead, 4, 2);
		
		byte[] newData=new byte[pppHead.length+ipData.length];
		System.arraycopy(pppHead, 0, newData, 0, pppHead.length);
		System.arraycopy(ipData, 0, newData, 8, ipData.length);
		pppBuilder.rawData(newData);
		
		builder=pppBuilder;
	}else {
		etherType= EtherType.IPV4;
		builder=builder_ipv4;
	}
	
	EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
	etherBuilder.dstAddr(dstAddrress_mac)
	.srcAddr(srcAddress_mac)
	.type(etherType)
	.payloadBuilder(builder)
	.paddingAtBuild(true);
	
	p = etherBuilder.build();
	
	return p;
}
 
Example #9
Source File: DhcpPacketListener.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
public void gotPacket(Packet rawPacket) {
    try {
        LOG.info("Read raw " + rawPacket);
        IpV4Packet ipPacket = rawPacket.get(IpV4Packet.class);
        UdpPacket udpPacket = rawPacket.get(UdpPacket.class);
        byte[] dhcpData = udpPacket.getPayload().getRawData();
        InetSocketAddress remoteAddress = new InetSocketAddress(ipPacket.getHeader().getSrcAddr(), udpPacket.getHeader().getSrcPort().valueAsInt());
        InetSocketAddress localAddress = new InetSocketAddress(ipPacket.getHeader().getDstAddr(), udpPacket.getHeader().getDstPort().valueAsInt());
        DhcpRequestContext context = new DhcpRequestContext(interfaceAddresses, remoteAddress, localAddress);
        DhcpMessage request = decoder.decode(ByteBuffer.wrap(dhcpData));
        LOG.info("Read DHCP " + request);
        DhcpMessage reply = service.getReplyFor(context, request);
        if (reply == null)
            return;
        byte[] replyData = new byte[1536];
        ByteBuffer buffer = ByteBuffer.wrap(replyData);
        encoder.encode(buffer, reply);
        replyData = Arrays.copyOf(replyData, buffer.position());    // Truncate array to writer position.
        UnknownPacket.Builder dhcpBuilder = new UnknownPacket.Builder()
                .rawData(replyData);
        UdpPacket.Builder udpBuilder = new UdpPacket.Builder()
                .payloadBuilder(dhcpBuilder)
                .srcPort(udpPacket.getHeader().getDstPort())
                .dstPort(udpPacket.getHeader().getSrcPort())
                .correctChecksumAtBuild(true)
                .correctLengthAtBuild(true);
        IpV4Packet.Builder ipBuilder = new IpV4Packet.Builder()
                .payloadBuilder(udpBuilder)
                .srcAddr(null) // TODO
                .dstAddr(null) // TODO
                .protocol(IpNumber.UDP)
                .correctChecksumAtBuild(true)
                .correctLengthAtBuild(true)
                .paddingAtBuild(true);
        EthernetPacket.Builder ethernetBuilder = new EthernetPacket.Builder()
                .payloadBuilder(ipBuilder)
                .type(EtherType.IPV4)
                .paddingAtBuild(true);
        Packet replyPacket = ethernetBuilder.build();
        // handle.sendPacket(replyPacket);
    } catch (Exception e) {
        LOG.error("DHCP failed", e);
    }
}
 
Example #10
Source File: PacketUtils.java    From finalspeed with GNU General Public License v2.0 4 votes vote down vote up
public static Packet buildIpV4(
        MacAddress srcAddress_mac,
        MacAddress dstAddrress_mac,
        IpV4Packet.Builder builder_ipv4) {

    org.pcap4j.packet.Packet.Builder builder;
    EtherType etherType;
    Packet p;
    if (ppp) {
        etherType = EtherType.PPPOE_SESSION_STAGE;

        UnknownPacket.Builder pppBuilder = new UnknownPacket.Builder();
        byte[] ipData = builder_ipv4.build().getRawData();

        byte[] lenb = new byte[2];
        ByteShortConvert.toByteArray((short) (ipData.length + 2), lenb, 0);

        byte[] pppHead = new byte[8];
        System.arraycopy(pppHead_static, 0, pppHead, 0, pppHead.length);
        System.arraycopy(lenb, 0, pppHead, 4, 2);

        byte[] newData = new byte[pppHead.length + ipData.length];
        System.arraycopy(pppHead, 0, newData, 0, pppHead.length);
        System.arraycopy(ipData, 0, newData, 8, ipData.length);
        pppBuilder.rawData(newData);

        builder = pppBuilder;
    } else {
        etherType = EtherType.IPV4;
        builder = builder_ipv4;
    }

    EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
    etherBuilder.dstAddr(dstAddrress_mac)
            .srcAddr(srcAddress_mac)
            .type(etherType)
            .payloadBuilder(builder)
            .paddingAtBuild(true);

    p = etherBuilder.build();

    return p;
}
 
Example #11
Source File: PacketUtils.java    From finalspeed-91yun with GNU General Public License v2.0 4 votes vote down vote up
public static Packet buildIpV4(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		IpV4Packet.Builder builder_ipv4){

	org.pcap4j.packet.Packet.Builder builder=null;
	EtherType etherType=null;
	Packet p=null;
	if(ppp){
		etherType=EtherType.PPPOE_SESSION_STAGE;
		
		UnknownPacket.Builder pppBuilder=new UnknownPacket.Builder();
		byte[] ipData=builder_ipv4.build().getRawData();
		
		byte[] lenb=new byte[2];
		ByteShortConvert.toByteArray((short) (ipData.length+2), lenb, 0);
		
		byte[] pppHead=new byte[8];
		System.arraycopy(pppHead_static, 0, pppHead, 0, pppHead.length);
		System.arraycopy(lenb, 0, pppHead, 4, 2);
		
		byte[] newData=new byte[pppHead.length+ipData.length];
		System.arraycopy(pppHead, 0, newData, 0, pppHead.length);
		System.arraycopy(ipData, 0, newData, 8, ipData.length);
		pppBuilder.rawData(newData);
		
		builder=pppBuilder;
	}else {
		etherType=EtherType.IPV4;
		builder=builder_ipv4;
	}
	
	EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
	etherBuilder.dstAddr(dstAddrress_mac)
	.srcAddr(srcAddress_mac)
	.type(etherType)
	.payloadBuilder(builder)
	.paddingAtBuild(true);
	
	p = etherBuilder.build();
	
	return p;
}
 
Example #12
Source File: ProtocolDataView.java    From trex-stateless-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Return protocol data view
 *
 * @return
 * @throws Exception
 */
public TrexEthernetPacket getProtocolData() throws Exception {

    int packetLength = PacketBuilderHelper.getPacketLength(selections.getFrameLengthType(), Integer.parseInt(selections.getFrameLength()), Integer.parseInt(selections.getMaxLength()));

    TrexEthernetPacket ethernetPacket = new TrexEthernetPacket();
    // set mac address
    ethernetPacket.setSrcAddr(macView.getSourceAddress().getAddress());
    ethernetPacket.setDstAddr(macView.getDestinationAddress().getAddress());
    ethernetPacket.setLength(packetLength);

    // set payload in ethernet
    ethernetPacket.setPayload(payloadView.getPayload());
    if (ethernetView.isOverrideType()) {
        ethernetPacket.setType(ethernetView.getType());
    }

    Builder ipV4Packet = getIPV4Packet(PacketBuilderHelper.getIPV4TotalLength(selections.isTaggedVlanSelected()), packetLength);
    if (selections.isTaggedVlanSelected()) {
        if (!ethernetView.isOverrideType()) {
            ethernetPacket.setType(EtherType.DOT1Q_VLAN_TAGGED_FRAMES.value());
        }
        TrexVlanPacket vlanPacket = vlanView.getVlan();
        /// IF IPV4 is selected
        if (!vlanPacket.isOverrideType()) {
            if (!selections.isIPV4Selected()) {
                ethernetPacket.setAddPad(true);
                vlanPacket.setType((short) 0xFFFF);
            } else {
                vlanPacket.setType(EtherType.IPV4.value());
            }
        }
        vlanPacket.buildPacket(ipV4Packet);
        ethernetPacket.buildPacket(vlanPacket.getBuilder());

    } else {
        ethernetPacket.buildPacket(ipV4Packet);
    }

    return ethernetPacket;
}
 
Example #13
Source File: MonitorController.java    From trex-stateless-gui with Apache License 2.0 4 votes vote down vote up
private CapturedPktModel toModel(CapturedPkt pkt) {
    byte[] pktBin;
    try {
        pktBin = Base64.getDecoder().decode(pkt.getBinary());

        Stack<String> headers = new Stack<>();
        headers.push("Ether");
        EthernetPacket etherPkt = EthernetPacket.newPacket(pktBin, 0, pktBin.length);

        Map<String, Object> info = new HashMap<>();
        info.put("info", "");
        
        EtherType l3Type = etherPkt.getHeader().getType();
        if(l3Type.equals(EtherType.ARP)) {
            headers.push("ARP");
            info.put("src", etherPkt.getHeader().getSrcAddr().toString());
            info.put("dst", etherPkt.getHeader().getDstAddr().toString());
            info.putAll(parseARP((ArpPacket) etherPkt.getPayload()));
        } else if (l3Type.equals(EtherType.DOT1Q_VLAN_TAGGED_FRAMES)) {
            headers.push("Dot1Q");
            info = parseDot1Q(headers, (Dot1qVlanTagPacket) etherPkt.getPayload());
        } else if (l3Type.equals(EtherType.IPV4)) {
            headers.push("IPv4");
            info = parseIP(headers, (IpV4Packet) etherPkt.getPayload());
        } else if (l3Type.equals(EtherType.IPV6)) {
            headers.push("IPv6");
            info = parseIP(headers, (IpV6Packet) etherPkt.getPayload());
        } else {
            info.put("dst", etherPkt.getHeader().getDstAddr().toString());
            info.put("src", etherPkt.getHeader().getSrcAddr().toString());
            info.put("info", "Unknown or malformed packet");
        }
        
        Double time = abs(starTs - pkt.getTimeStamp());

        latestPktIndex = pkt.getIndex() + pktNumberOffset;
        return new CapturedPktModel(latestPktIndex,
                                    pkt.getPort(),
                                    pkt.getOrigin(),
                                    time,
                                    (String) info.get("dst"),
                                    (String) info.get("src"),
                                    headers.peek(),
                                    pktBin.length,
                                    (String) info.get("info"),
                                    pktBin);
    } catch (Exception e) {
        return null;
    }
}
 
Example #14
Source File: TrexVlanPacket.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param type
 */
public void setType(short type) {
    this.type = EtherType.getInstance(type);
}
 
Example #15
Source File: TrexVlanPacket.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @return
 */
public EtherType getType() {
    return type;
}
 
Example #16
Source File: TrexEthernetPacket.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @return
 */
public EtherType getType() {
    return type;
}
 
Example #17
Source File: TrexEthernetPacket.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param type
 */
public void setType(short type) {
    this.type = EtherType.getInstance(type);
}