1 // This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
2 package edu.uci.iotproject;
4 import io.kaitai.struct.ByteBufferKaitaiStream;
5 import io.kaitai.struct.KaitaiStruct;
6 import io.kaitai.struct.KaitaiStream;
7 import java.io.IOException;
9 import java.util.HashMap;
10 import java.util.ArrayList;
11 import java.nio.charset.Charset;
13 import java.nio.BufferUnderflowException;
17 * (No support for Auth-Name + Add-Name for simplicity)
19 public class DnsPacket extends KaitaiStruct {
20 public static DnsPacket fromFile(String fileName) throws IOException {
21 return new DnsPacket(new ByteBufferKaitaiStream(fileName));
24 public enum ClassType {
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);
35 for (ClassType e : ClassType.values())
38 public static ClassType byId(long id) { return byId.get(id); }
41 public enum TypeType {
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);
64 for (TypeType e : TypeType.values())
67 public static TypeType byId(long id) { return byId.get(id); }
70 public DnsPacket(KaitaiStream _io) {
71 this(_io, null, null);
74 public DnsPacket(KaitaiStream _io, KaitaiStruct _parent) {
75 this(_io, _parent, null);
78 public DnsPacket(KaitaiStream _io, KaitaiStruct _parent, DnsPacket _root) {
80 this._parent = _parent;
81 this._root = _root == null ? this : _root;
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));
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));
100 public static class PointerStruct extends KaitaiStruct {
101 public static PointerStruct fromFile(String fileName) throws IOException {
102 return new PointerStruct(new ByteBufferKaitaiStream(fileName));
105 public PointerStruct(KaitaiStream _io) {
106 this(_io, null, null);
109 public PointerStruct(KaitaiStream _io, DnsPacket.Label _parent) {
110 this(_io, _parent, null);
113 public PointerStruct(KaitaiStream _io, DnsPacket.Label _parent, DnsPacket _root) {
115 this._parent = _parent;
119 private void _read() {
120 this.value = this._io.readU1();
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();
129 this.contents = new DomainName(io, this, _root);
131 return this.contents;
134 private DnsPacket _root;
135 private DnsPacket.Label _parent;
138 * Read one byte, then offset to that position, read one domain-name and return
140 public int value() { return value; }
141 public DnsPacket _root() { return _root; }
142 public DnsPacket.Label _parent() { return _parent; }
144 public static class Label extends KaitaiStruct {
145 public static Label fromFile(String fileName) throws IOException {
146 return new Label(new ByteBufferKaitaiStream(fileName));
149 public Label(KaitaiStream _io) {
150 this(_io, null, null);
153 public Label(KaitaiStream _io, DnsPacket.DomainName _parent) {
154 this(_io, _parent, null);
157 public Label(KaitaiStream _io, DnsPacket.DomainName _parent, DnsPacket _root) {
159 this._parent = _parent;
163 private void _read() {
164 this.length = this._io.readU1();
166 this.pointer = new PointerStruct(this._io, this, _root);
168 // TODO: This part causes BufferUnderflowException
169 if (!(isPointer())) {
171 this.name = new String(this._io.readBytes(length()), Charset.forName("ASCII"));
172 } catch (BufferUnderflowException ex) {
173 //ex.printStackTrace();
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;
187 private PointerStruct pointer;
189 private DnsPacket _root;
190 private DnsPacket.DomainName _parent;
193 * RFC1035 4.1.4: If the first two bits are raised it's a pointer-offset to a previously defined name
195 public int length() { return length; }
196 public PointerStruct pointer() { return pointer; }
199 * Otherwise its a string the length of the length value
201 public String name() { return name; }
202 public DnsPacket _root() { return _root; }
203 public DnsPacket.DomainName _parent() { return _parent; }
205 public static class Query extends KaitaiStruct {
206 public static Query fromFile(String fileName) throws IOException {
207 return new Query(new ByteBufferKaitaiStream(fileName));
210 public Query(KaitaiStream _io) {
211 this(_io, null, null);
214 public Query(KaitaiStream _io, DnsPacket _parent) {
215 this(_io, _parent, null);
218 public Query(KaitaiStream _io, DnsPacket _parent, DnsPacket _root) {
220 this._parent = _parent;
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());
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; }
240 public static class DomainName extends KaitaiStruct {
241 public static DomainName fromFile(String fileName) throws IOException {
242 return new DomainName(new ByteBufferKaitaiStream(fileName));
245 public DomainName(KaitaiStream _io) {
246 this(_io, null, null);
249 public DomainName(KaitaiStream _io, KaitaiStruct _parent) {
250 this(_io, _parent, null);
253 public DomainName(KaitaiStream _io, KaitaiStruct _parent, DnsPacket _root) {
255 this._parent = _parent;
259 private void _read() {
260 this.name = new ArrayList<Label>();
265 _it = new Label(this._io, this, _root);
268 } while (!( ((_it.length() == 0) || (_it.length() == 192)) ));
271 private ArrayList<Label> name;
272 private DnsPacket _root;
273 private KaitaiStruct _parent;
276 * Repeat until the length is 0 or it is a pointer (bit-hack to get around lack of OR operator)
278 public ArrayList<Label> name() { return name; }
279 public DnsPacket _root() { return _root; }
280 public KaitaiStruct _parent() { return _parent; }
282 public static class Address extends KaitaiStruct {
283 public static Address fromFile(String fileName) throws IOException {
284 return new Address(new ByteBufferKaitaiStream(fileName));
287 public Address(KaitaiStream _io) {
288 this(_io, null, null);
291 public Address(KaitaiStream _io, DnsPacket.Answer _parent) {
292 this(_io, _parent, null);
295 public Address(KaitaiStream _io, DnsPacket.Answer _parent, DnsPacket _root) {
297 this._parent = _parent;
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());
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; }
314 public static class Answer extends KaitaiStruct {
315 public static Answer fromFile(String fileName) throws IOException {
316 return new Answer(new ByteBufferKaitaiStream(fileName));
319 public Answer(KaitaiStream _io) {
320 this(_io, null, null);
323 public Answer(KaitaiStream _io, DnsPacket _parent) {
324 this(_io, _parent, null);
327 public Answer(KaitaiStream _io, DnsPacket _parent, DnsPacket _root) {
329 this._parent = _parent;
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);
342 if (type() == DnsPacket.TypeType.A) {
343 this.address = new Address(this._io, this, _root);
346 private DomainName name;
347 private TypeType type;
348 private ClassType answerClass;
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; }
360 * Time to live (in seconds)
362 public int ttl() { return ttl; }
365 * Length in octets of the following payload
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; }
373 public static class PacketFlags extends KaitaiStruct {
374 public static PacketFlags fromFile(String fileName) throws IOException {
375 return new PacketFlags(new ByteBufferKaitaiStream(fileName));
378 public PacketFlags(KaitaiStream _io) {
379 this(_io, null, null);
382 public PacketFlags(KaitaiStream _io, DnsPacket _parent) {
383 this(_io, _parent, null);
386 public PacketFlags(KaitaiStream _io, DnsPacket _parent, DnsPacket _root) {
388 this._parent = _parent;
392 private void _read() {
393 this.flag = this._io.readU2be();
396 public Integer qr() {
399 int _tmp = (int) (((flag() & 32768) >> 15));
404 public Integer ra() {
407 int _tmp = (int) (((flag() & 128) >> 7));
412 public Integer tc() {
415 int _tmp = (int) (((flag() & 512) >> 9));
419 private Integer rcode;
420 public Integer rcode() {
421 if (this.rcode != null)
423 int _tmp = (int) (((flag() & 15) >> 0));
427 private Integer opcode;
428 public Integer opcode() {
429 if (this.opcode != null)
431 int _tmp = (int) (((flag() & 30720) >> 11));
436 public Integer aa() {
439 int _tmp = (int) (((flag() & 1024) >> 10));
447 int _tmp = (int) (((flag() & 64) >> 6));
452 public Integer rd() {
455 int _tmp = (int) (((flag() & 256) >> 8));
460 public Integer cd() {
463 int _tmp = (int) (((flag() & 16) >> 4));
468 public Integer ad() {
471 int _tmp = (int) (((flag() & 32) >> 5));
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; }
482 private int transactionId;
483 private PacketFlags flags;
488 private ArrayList<Query> queries;
489 private ArrayList<Answer> answers;
490 private DnsPacket _root;
491 private KaitaiStruct _parent;
494 * ID to keep track of request/responces
496 public int transactionId() { return transactionId; }
497 public PacketFlags flags() { return flags; }
500 * How many questions are there
502 public int qdcount() { return qdcount; }
505 * Number of resource records answering the question
507 public int ancount() { return ancount; }
510 * Number of resource records pointing toward an authority
512 public int nscount() { return nscount; }
515 * Number of resource records holding additional information
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; }