a go dns packet parser
1package magna 2 3import "encoding/binary" 4 5// Decode decodes a question from buf at the offset 6func (q *Question) Decode(buf []byte, offset int) (int, error) { 7 var err error 8 q.QName, offset, err = decode_domain(buf, offset) 9 if err != nil { 10 return offset, err 11 } 12 13 qtype, offset, err := getU16(buf, offset) 14 if err != nil { 15 return offset, err 16 } 17 18 qclass, offset, err := getU16(buf, offset) 19 if err != nil { 20 return offset, err 21 } 22 23 q.QType = DNSType(qtype) 24 q.QClass = DNSClass(qclass) 25 return offset, nil 26} 27 28// Encode serializes a Question into bytes, using a map to handle domain name compression offsets. 29func (q *Question) Encode(bytes []byte, offsets *map[string]uint8) []byte { 30 bytes = encode_domain(bytes, q.QName, offsets) 31 bytes = binary.BigEndian.AppendUint16(bytes, uint16(q.QType)) 32 bytes = binary.BigEndian.AppendUint16(bytes, uint16(q.QClass)) 33 return bytes 34}