ethernet_frame: Java parsing library

KS implementation details

Minimal Kaitai Struct required: 0.7

This page hosts a formal specification of ethernet_frame using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Parse a local file and get structure in memory:

EthernetFrame data = EthernetFrame.fromFile("path/to/local/file.ethernet_frame");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
EthernetFrame data = new EthernetFrame(new KaitaiStream(someArray));

After that, one can get various attributes from the structure by invoking getter methods like:

data.dstMac() // => get dst mac

Java source code to parse ethernet_frame

EthernetFrame.java

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.KaitaiStream;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;

public class EthernetFrame extends KaitaiStruct {
    public static EthernetFrame fromFile(String fileName) throws IOException {
        return new EthernetFrame(new ByteBufferKaitaiStream(fileName));
    }

    public enum EtherTypeEnum {
        IPV4(2048),
        X_75_INTERNET(2049),
        NBS_INTERNET(2050),
        ECMA_INTERNET(2051),
        CHAOSNET(2052),
        X_25_LEVEL_3(2053),
        ARP(2054),
        IPV6(34525);

        private final long id;
        EtherTypeEnum(long id) { this.id = id; }
        public long id() { return id; }
        private static final Map<Long, EtherTypeEnum> byId = new HashMap<Long, EtherTypeEnum>(8);
        static {
            for (EtherTypeEnum e : EtherTypeEnum.values())
                byId.put(e.id(), e);
        }
        public static EtherTypeEnum byId(long id) { return byId.get(id); }
    }

    public EthernetFrame(KaitaiStream _io) {
        this(_io, null, null);
    }

    public EthernetFrame(KaitaiStream _io, KaitaiStruct _parent) {
        this(_io, _parent, null);
    }

    public EthernetFrame(KaitaiStream _io, KaitaiStruct _parent, EthernetFrame _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
        _read();
    }
    private void _read() {
        this.dstMac = this._io.readBytes(6);
        this.srcMac = this._io.readBytes(6);
        this.etherType = EtherTypeEnum.byId(this._io.readU2be());
        switch (etherType()) {
        case IPV4: {
            this._raw_body = this._io.readBytesFull();
            KaitaiStream _io__raw_body = new ByteBufferKaitaiStream(_raw_body);
            this.body = new Ipv4Packet(_io__raw_body);
            break;
        }
        case IPV6: {
            this._raw_body = this._io.readBytesFull();
            KaitaiStream _io__raw_body = new ByteBufferKaitaiStream(_raw_body);
            this.body = new Ipv6Packet(_io__raw_body);
            break;
        }
        default: {
            this.body = this._io.readBytesFull();
            break;
        }
        }
    }
    private byte[] dstMac;
    private byte[] srcMac;
    private EtherTypeEnum etherType;
    private Object body;
    private EthernetFrame _root;
    private KaitaiStruct _parent;
    private byte[] _raw_body;
    public byte[] dstMac() { return dstMac; }
    public byte[] srcMac() { return srcMac; }
    public EtherTypeEnum etherType() { return etherType; }
    public Object body() { return body; }
    public EthernetFrame _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
    public byte[] _raw_body() { return _raw_body; }
}