aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/main.go16
-rw-r--r--app/message.go18
2 files changed, 29 insertions, 5 deletions
diff --git a/app/main.go b/app/main.go
index 6831c5c..79e3b82 100644
--- a/app/main.go
+++ b/app/main.go
@@ -30,19 +30,25 @@ func main() {
receivedData := string(buf[:size])
fmt.Printf("Received %d bytes from %s: %s\n", size, source, receivedData)
+ requestHeader := ParseHeader(buf[:size])
+
+ rcode := uint8(4)
+ if requestHeader.OPCODE == 0 {
+ rcode = 0
+ }
header := DNSHeader{
- ID: 1234,
+ ID: requestHeader.ID,
QR: 1,
- OPCODE: 1,
+ OPCODE: requestHeader.OPCODE,
AA: 0,
TC: 0,
- RD: 0,
+ RD: requestHeader.RD,
RA: 0,
Z: 0,
- RCODE: 0,
+ RCODE: rcode,
QDCOUNT: 1,
- ANCOUNT: 1,
+ ANCOUNT: 0,
NSCOUNT: 0,
ARCOUNT: 0,
}
diff --git a/app/message.go b/app/message.go
index 0604d0e..931a6f0 100644
--- a/app/message.go
+++ b/app/message.go
@@ -89,3 +89,21 @@ func (m *DNSMessage) AddAnswer(a DNSAnswer) {
func MakeAnswer(name []byte, rdata []byte) DNSAnswer {
return DNSAnswer{Name: name, Type: 1, Class: 1, TTL: 60, RDLength: uint16(len(rdata)), RData: rdata}
}
+
+func ParseHeader(buf []byte) DNSHeader {
+ return DNSHeader{
+ ID: binary.BigEndian.Uint16(buf[0:2]),
+ QR: uint8(buf[2] >> 7),
+ OPCODE: uint8(buf[2] >> 3 & 0x0f),
+ AA: uint8(buf[2] >> 2 & 0x01),
+ TC: uint8(buf[2] >> 1 & 0x01),
+ RD: uint8(buf[2] & 0x01),
+ RA: uint8(buf[3] >> 7),
+ Z: uint8(buf[3] >> 4 & 0x07),
+ RCODE: uint8(buf[3] & 0x0f),
+ QDCOUNT: binary.BigEndian.Uint16(buf[4:6]),
+ ANCOUNT: binary.BigEndian.Uint16(buf[6:8]),
+ NSCOUNT: binary.BigEndian.Uint16(buf[8:10]),
+ ARCOUNT: binary.BigEndian.Uint16(buf[10:12]),
+ }
+}