Skipping packets that do not have etherType, e.g. XID, EAPOL, etc.
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / EthernetFrame.java
1 // This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
2 package edu.uci.iotproject;
3
4 import io.kaitai.struct.ByteBufferKaitaiStream;
5 import io.kaitai.struct.KaitaiStruct;
6 import io.kaitai.struct.KaitaiStream;
7 import java.io.IOException;
8 import java.util.Map;
9 import java.util.HashMap;
10
11 public class EthernetFrame extends KaitaiStruct {
12     public static EthernetFrame fromFile(String fileName) throws IOException {
13         return new EthernetFrame(new ByteBufferKaitaiStream(fileName));
14     }
15
16     public enum EtherTypeEnum {
17         IPV4(2048),
18         X_75_INTERNET(2049),
19         NBS_INTERNET(2050),
20         ECMA_INTERNET(2051),
21         CHAOSNET(2052),
22         X_25_LEVEL_3(2053),
23         ARP(2054),
24         IPV6(34525);
25
26         private final long id;
27         EtherTypeEnum(long id) { this.id = id; }
28         public long id() { return id; }
29         private static final Map<Long, EtherTypeEnum> byId = new HashMap<Long, EtherTypeEnum>(8);
30         static {
31             for (EtherTypeEnum e : EtherTypeEnum.values())
32                 byId.put(e.id(), e);
33         }
34         public static EtherTypeEnum byId(long id) { return byId.get(id); }
35     }
36
37     public EthernetFrame(KaitaiStream _io) {
38         this(_io, null, null);
39     }
40
41     public EthernetFrame(KaitaiStream _io, KaitaiStruct _parent) {
42         this(_io, _parent, null);
43     }
44
45     public EthernetFrame(KaitaiStream _io, KaitaiStruct _parent, EthernetFrame _root) {
46         super(_io);
47         this._parent = _parent;
48         this._root = _root == null ? this : _root;
49         _read();
50     }
51     private void _read() {
52         this.dstMac = this._io.readBytes(6);
53         this.srcMac = this._io.readBytes(6);
54         this.etherType = EtherTypeEnum.byId(this._io.readU2be());
55
56         // We skip if etherType is NULL
57         // Some packets, e.g. EAPOL and XID do not have etherType
58         //      and we are not interested in these packets
59         if(etherType() != null) {
60             switch (etherType()) {
61                 case IPV4: {
62                     this._raw_body = this._io.readBytesFull();
63                     KaitaiStream _io__raw_body = new ByteBufferKaitaiStream(_raw_body);
64                     this.body = new Ipv4Packet(_io__raw_body);
65                     break;
66                 }
67                 case IPV6: {
68                     this._raw_body = this._io.readBytesFull();
69                     KaitaiStream _io__raw_body = new ByteBufferKaitaiStream(_raw_body);
70                     this.body = new Ipv6Packet(_io__raw_body);
71                     break;
72                 }
73                 default: {
74                     this.body = this._io.readBytesFull();
75                     break;
76                 }
77             }
78         }
79     }
80     private byte[] dstMac;
81     private byte[] srcMac;
82     private EtherTypeEnum etherType;
83     private Object body;
84     private EthernetFrame _root;
85     private KaitaiStruct _parent;
86     private byte[] _raw_body;
87     public byte[] dstMac() { return dstMac; }
88     public byte[] srcMac() { return srcMac; }
89     public EtherTypeEnum etherType() { return etherType; }
90     public Object body() { return body; }
91     public EthernetFrame _root() { return _root; }
92     public KaitaiStruct _parent() { return _parent; }
93     public byte[] _raw_body() { return _raw_body; }
94 }