Managed to pick and parse DNS packets; but, still need to get the detailed informatio...
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / DnsPacket.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 import java.util.ArrayList;
11 import java.nio.charset.Charset;
12
13 import java.nio.BufferUnderflowException;
14
15
16 /**
17  * (No support for Auth-Name + Add-Name for simplicity)
18  */
19 public class DnsPacket extends KaitaiStruct {
20     public static DnsPacket fromFile(String fileName) throws IOException {
21         return new DnsPacket(new ByteBufferKaitaiStream(fileName));
22     }
23
24     public enum ClassType {
25         IN_CLASS(1),
26         CS(2),
27         CH(3),
28         HS(4);
29
30         private final long id;
31         ClassType(long id) { this.id = id; }
32         public long id() { return id; }
33         private static final Map<Long, ClassType> byId = new HashMap<Long, ClassType>(4);
34         static {
35             for (ClassType e : ClassType.values())
36                 byId.put(e.id(), e);
37         }
38         public static ClassType byId(long id) { return byId.get(id); }
39     }
40
41     public enum TypeType {
42         A(1),
43         NS(2),
44         MD(3),
45         MF(4),
46         CNAME(5),
47         SOE(6),
48         MB(7),
49         MG(8),
50         MR(9),
51         NULL(10),
52         WKS(11),
53         PTR(12),
54         HINFO(13),
55         MINFO(14),
56         MX(15),
57         TXT(16);
58
59         private final long id;
60         TypeType(long id) { this.id = id; }
61         public long id() { return id; }
62         private static final Map<Long, TypeType> byId = new HashMap<Long, TypeType>(16);
63         static {
64             for (TypeType e : TypeType.values())
65                 byId.put(e.id(), e);
66         }
67         public static TypeType byId(long id) { return byId.get(id); }
68     }
69
70     public DnsPacket(KaitaiStream _io) {
71         this(_io, null, null);
72     }
73
74     public DnsPacket(KaitaiStream _io, KaitaiStruct _parent) {
75         this(_io, _parent, null);
76     }
77
78     public DnsPacket(KaitaiStream _io, KaitaiStruct _parent, DnsPacket _root) {
79         super(_io);
80         this._parent = _parent;
81         this._root = _root == null ? this : _root;
82         _read();
83     }
84     private void _read() {
85         this.transactionId = this._io.readU2be();
86         this.flags = new PacketFlags(this._io, this, _root);
87         this.qdcount = this._io.readU2be();
88         this.ancount = this._io.readU2be();
89         this.nscount = this._io.readU2be();
90         this.arcount = this._io.readU2be();
91         queries = new ArrayList<Query>((int) (qdcount()));
92         for (int i = 0; i < qdcount(); i++) {
93             this.queries.add(new Query(this._io, this, _root));
94         }
95         answers = new ArrayList<Answer>((int) (ancount()));
96         for (int i = 0; i < ancount(); i++) {
97             this.answers.add(new Answer(this._io, this, _root));
98         }
99     }
100     public static class PointerStruct extends KaitaiStruct {
101         public static PointerStruct fromFile(String fileName) throws IOException {
102             return new PointerStruct(new ByteBufferKaitaiStream(fileName));
103         }
104
105         public PointerStruct(KaitaiStream _io) {
106             this(_io, null, null);
107         }
108
109         public PointerStruct(KaitaiStream _io, DnsPacket.Label _parent) {
110             this(_io, _parent, null);
111         }
112
113         public PointerStruct(KaitaiStream _io, DnsPacket.Label _parent, DnsPacket _root) {
114             super(_io);
115             this._parent = _parent;
116             this._root = _root;
117             _read();
118         }
119         private void _read() {
120             this.value = this._io.readU1();
121         }
122         private DomainName contents;
123         public DomainName contents() {
124             if (this.contents != null)
125                 return this.contents;
126             KaitaiStream io = _root._io();
127             long _pos = io.pos();
128             io.seek(value());
129             this.contents = new DomainName(io, this, _root);
130             io.seek(_pos);
131             return this.contents;
132         }
133         private int value;
134         private DnsPacket _root;
135         private DnsPacket.Label _parent;
136
137         /**
138          * Read one byte, then offset to that position, read one domain-name and return
139          */
140         public int value() { return value; }
141         public DnsPacket _root() { return _root; }
142         public DnsPacket.Label _parent() { return _parent; }
143     }
144     public static class Label extends KaitaiStruct {
145         public static Label fromFile(String fileName) throws IOException {
146             return new Label(new ByteBufferKaitaiStream(fileName));
147         }
148
149         public Label(KaitaiStream _io) {
150             this(_io, null, null);
151         }
152
153         public Label(KaitaiStream _io, DnsPacket.DomainName _parent) {
154             this(_io, _parent, null);
155         }
156
157         public Label(KaitaiStream _io, DnsPacket.DomainName _parent, DnsPacket _root) {
158             super(_io);
159             this._parent = _parent;
160             this._root = _root;
161             _read();
162         }
163         private void _read() {
164             this.length = this._io.readU1();
165             if (isPointer()) {
166                 this.pointer = new PointerStruct(this._io, this, _root);
167             }
168             // TODO: This part causes BufferUnderflowException
169             if (!(isPointer())) {
170                 try {
171                     this.name = new String(this._io.readBytes(length()), Charset.forName("ASCII"));
172                 } catch (BufferUnderflowException ex) {
173                     //ex.printStackTrace();
174                     this.name = null;
175                 }
176             }
177         }
178         private Boolean isPointer;
179         public Boolean isPointer() {
180             if (this.isPointer != null)
181                 return this.isPointer;
182             boolean _tmp = (boolean) (length() == 192);
183             this.isPointer = _tmp;
184             return this.isPointer;
185         }
186         private int length;
187         private PointerStruct pointer;
188         private String name;
189         private DnsPacket _root;
190         private DnsPacket.DomainName _parent;
191
192         /**
193          * RFC1035 4.1.4: If the first two bits are raised it's a pointer-offset to a previously defined name
194          */
195         public int length() { return length; }
196         public PointerStruct pointer() { return pointer; }
197
198         /**
199          * Otherwise its a string the length of the length value
200          */
201         public String name() { return name; }
202         public DnsPacket _root() { return _root; }
203         public DnsPacket.DomainName _parent() { return _parent; }
204     }
205     public static class Query extends KaitaiStruct {
206         public static Query fromFile(String fileName) throws IOException {
207             return new Query(new ByteBufferKaitaiStream(fileName));
208         }
209
210         public Query(KaitaiStream _io) {
211             this(_io, null, null);
212         }
213
214         public Query(KaitaiStream _io, DnsPacket _parent) {
215             this(_io, _parent, null);
216         }
217
218         public Query(KaitaiStream _io, DnsPacket _parent, DnsPacket _root) {
219             super(_io);
220             this._parent = _parent;
221             this._root = _root;
222             _read();
223         }
224         private void _read() {
225             this.name = new DomainName(this._io, this, _root);
226             this.type = DnsPacket.TypeType.byId(this._io.readU2be());
227             this.queryClass = DnsPacket.ClassType.byId(this._io.readU2be());
228         }
229         private DomainName name;
230         private TypeType type;
231         private ClassType queryClass;
232         private DnsPacket _root;
233         private DnsPacket _parent;
234         public DomainName name() { return name; }
235         public TypeType type() { return type; }
236         public ClassType queryClass() { return queryClass; }
237         public DnsPacket _root() { return _root; }
238         public DnsPacket _parent() { return _parent; }
239     }
240     public static class DomainName extends KaitaiStruct {
241         public static DomainName fromFile(String fileName) throws IOException {
242             return new DomainName(new ByteBufferKaitaiStream(fileName));
243         }
244
245         public DomainName(KaitaiStream _io) {
246             this(_io, null, null);
247         }
248
249         public DomainName(KaitaiStream _io, KaitaiStruct _parent) {
250             this(_io, _parent, null);
251         }
252
253         public DomainName(KaitaiStream _io, KaitaiStruct _parent, DnsPacket _root) {
254             super(_io);
255             this._parent = _parent;
256             this._root = _root;
257             _read();
258         }
259         private void _read() {
260             this.name = new ArrayList<Label>();
261             {
262                 Label _it;
263                 int i = 0;
264                 do {
265                     _it = new Label(this._io, this, _root);
266                     this.name.add(_it);
267                     i++;
268                 } while (!( ((_it.length() == 0) || (_it.length() == 192)) ));
269             }
270         }
271         private ArrayList<Label> name;
272         private DnsPacket _root;
273         private KaitaiStruct _parent;
274
275         /**
276          * Repeat until the length is 0 or it is a pointer (bit-hack to get around lack of OR operator)
277          */
278         public ArrayList<Label> name() { return name; }
279         public DnsPacket _root() { return _root; }
280         public KaitaiStruct _parent() { return _parent; }
281     }
282     public static class Address extends KaitaiStruct {
283         public static Address fromFile(String fileName) throws IOException {
284             return new Address(new ByteBufferKaitaiStream(fileName));
285         }
286
287         public Address(KaitaiStream _io) {
288             this(_io, null, null);
289         }
290
291         public Address(KaitaiStream _io, DnsPacket.Answer _parent) {
292             this(_io, _parent, null);
293         }
294
295         public Address(KaitaiStream _io, DnsPacket.Answer _parent, DnsPacket _root) {
296             super(_io);
297             this._parent = _parent;
298             this._root = _root;
299             _read();
300         }
301         private void _read() {
302             ip = new ArrayList<Integer>((int) (4));
303             for (int i = 0; i < 4; i++) {
304                 this.ip.add(this._io.readU1());
305             }
306         }
307         private ArrayList<Integer> ip;
308         private DnsPacket _root;
309         private DnsPacket.Answer _parent;
310         public ArrayList<Integer> ip() { return ip; }
311         public DnsPacket _root() { return _root; }
312         public DnsPacket.Answer _parent() { return _parent; }
313     }
314     public static class Answer extends KaitaiStruct {
315         public static Answer fromFile(String fileName) throws IOException {
316             return new Answer(new ByteBufferKaitaiStream(fileName));
317         }
318
319         public Answer(KaitaiStream _io) {
320             this(_io, null, null);
321         }
322
323         public Answer(KaitaiStream _io, DnsPacket _parent) {
324             this(_io, _parent, null);
325         }
326
327         public Answer(KaitaiStream _io, DnsPacket _parent, DnsPacket _root) {
328             super(_io);
329             this._parent = _parent;
330             this._root = _root;
331             _read();
332         }
333         private void _read() {
334             this.name = new DomainName(this._io, this, _root);
335             this.type = DnsPacket.TypeType.byId(this._io.readU2be());
336             this.answerClass = DnsPacket.ClassType.byId(this._io.readU2be());
337             this.ttl = this._io.readS4be();
338             this.rdlength = this._io.readU2be();
339             if (type() == DnsPacket.TypeType.PTR) {
340                 this.ptrdname = new DomainName(this._io, this, _root);
341             }
342             if (type() == DnsPacket.TypeType.A) {
343                 this.address = new Address(this._io, this, _root);
344             }
345         }
346         private DomainName name;
347         private TypeType type;
348         private ClassType answerClass;
349         private int ttl;
350         private int rdlength;
351         private DomainName ptrdname;
352         private Address address;
353         private DnsPacket _root;
354         private DnsPacket _parent;
355         public DomainName name() { return name; }
356         public TypeType type() { return type; }
357         public ClassType answerClass() { return answerClass; }
358
359         /**
360          * Time to live (in seconds)
361          */
362         public int ttl() { return ttl; }
363
364         /**
365          * Length in octets of the following payload
366          */
367         public int rdlength() { return rdlength; }
368         public DomainName ptrdname() { return ptrdname; }
369         public Address address() { return address; }
370         public DnsPacket _root() { return _root; }
371         public DnsPacket _parent() { return _parent; }
372     }
373     public static class PacketFlags extends KaitaiStruct {
374         public static PacketFlags fromFile(String fileName) throws IOException {
375             return new PacketFlags(new ByteBufferKaitaiStream(fileName));
376         }
377
378         public PacketFlags(KaitaiStream _io) {
379             this(_io, null, null);
380         }
381
382         public PacketFlags(KaitaiStream _io, DnsPacket _parent) {
383             this(_io, _parent, null);
384         }
385
386         public PacketFlags(KaitaiStream _io, DnsPacket _parent, DnsPacket _root) {
387             super(_io);
388             this._parent = _parent;
389             this._root = _root;
390             _read();
391         }
392         private void _read() {
393             this.flag = this._io.readU2be();
394         }
395         private Integer qr;
396         public Integer qr() {
397             if (this.qr != null)
398                 return this.qr;
399             int _tmp = (int) (((flag() & 32768) >> 15));
400             this.qr = _tmp;
401             return this.qr;
402         }
403         private Integer ra;
404         public Integer ra() {
405             if (this.ra != null)
406                 return this.ra;
407             int _tmp = (int) (((flag() & 128) >> 7));
408             this.ra = _tmp;
409             return this.ra;
410         }
411         private Integer tc;
412         public Integer tc() {
413             if (this.tc != null)
414                 return this.tc;
415             int _tmp = (int) (((flag() & 512) >> 9));
416             this.tc = _tmp;
417             return this.tc;
418         }
419         private Integer rcode;
420         public Integer rcode() {
421             if (this.rcode != null)
422                 return this.rcode;
423             int _tmp = (int) (((flag() & 15) >> 0));
424             this.rcode = _tmp;
425             return this.rcode;
426         }
427         private Integer opcode;
428         public Integer opcode() {
429             if (this.opcode != null)
430                 return this.opcode;
431             int _tmp = (int) (((flag() & 30720) >> 11));
432             this.opcode = _tmp;
433             return this.opcode;
434         }
435         private Integer aa;
436         public Integer aa() {
437             if (this.aa != null)
438                 return this.aa;
439             int _tmp = (int) (((flag() & 1024) >> 10));
440             this.aa = _tmp;
441             return this.aa;
442         }
443         private Integer z;
444         public Integer z() {
445             if (this.z != null)
446                 return this.z;
447             int _tmp = (int) (((flag() & 64) >> 6));
448             this.z = _tmp;
449             return this.z;
450         }
451         private Integer rd;
452         public Integer rd() {
453             if (this.rd != null)
454                 return this.rd;
455             int _tmp = (int) (((flag() & 256) >> 8));
456             this.rd = _tmp;
457             return this.rd;
458         }
459         private Integer cd;
460         public Integer cd() {
461             if (this.cd != null)
462                 return this.cd;
463             int _tmp = (int) (((flag() & 16) >> 4));
464             this.cd = _tmp;
465             return this.cd;
466         }
467         private Integer ad;
468         public Integer ad() {
469             if (this.ad != null)
470                 return this.ad;
471             int _tmp = (int) (((flag() & 32) >> 5));
472             this.ad = _tmp;
473             return this.ad;
474         }
475         private int flag;
476         private DnsPacket _root;
477         private DnsPacket _parent;
478         public int flag() { return flag; }
479         public DnsPacket _root() { return _root; }
480         public DnsPacket _parent() { return _parent; }
481     }
482     private int transactionId;
483     private PacketFlags flags;
484     private int qdcount;
485     private int ancount;
486     private int nscount;
487     private int arcount;
488     private ArrayList<Query> queries;
489     private ArrayList<Answer> answers;
490     private DnsPacket _root;
491     private KaitaiStruct _parent;
492
493     /**
494      * ID to keep track of request/responces
495      */
496     public int transactionId() { return transactionId; }
497     public PacketFlags flags() { return flags; }
498
499     /**
500      * How many questions are there
501      */
502     public int qdcount() { return qdcount; }
503
504     /**
505      * Number of resource records answering the question
506      */
507     public int ancount() { return ancount; }
508
509     /**
510      * Number of resource records pointing toward an authority
511      */
512     public int nscount() { return nscount; }
513
514     /**
515      * Number of resource records holding additional information
516      */
517     public int arcount() { return arcount; }
518     public ArrayList<Query> queries() { return queries; }
519     public ArrayList<Answer> answers() { return answers; }
520     public DnsPacket _root() { return _root; }
521     public KaitaiStruct _parent() { return _parent; }
522 }