Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dns server test #4

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 1 addition & 60 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,3 @@
package main

import (
"log"
"net"
)

func main() {
qname := "google.com"
qtype := A

packet := NewDnsPacket()
packet.header.id = 1234
packet.header.questions = 1
packet.header.recursionDesired = true
packet.questions = append(packet.questions, DnsQuestion{name: qname, qtype: qtype})

requestBuf := NewBytePacketBuffer()
err := packet.write(requestBuf)
if err != nil {
log.Fatalf("error writing packet: %v", err)
}

udpAddr, err := net.ResolveUDPAddr("udp", "8.8.8.8:53")
if err != nil {
log.Fatalf("error resolving address: %v", err)
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
log.Fatalf("error dialing: %v", err)
}
defer conn.Close()
_, err = conn.Write(requestBuf.buf[0:requestBuf.pos])
if err != nil {
log.Fatalf("error writing request: %v", err)
}

responseBuf := NewBytePacketBuffer()
_, _, err = conn.ReadFromUDP(responseBuf.buf)
if err != nil {
log.Fatalf("error reading response: %v", err)
}

resPacket := NewDnsPacket()
err = resPacket.fromBuffer(responseBuf)
if err != nil {
log.Fatalf("error reading response: %v", err)
}

for _, q := range resPacket.questions {
log.Printf("question: %+v", q)
}
for _, a := range resPacket.answers {
log.Printf("answer: %+v", a)
}
for _, a := range resPacket.authorities {
log.Printf("authority: %+v", a)
}
for _, a := range resPacket.resources {
log.Printf("resource: %+v", a)
}
}
func main() {}
47 changes: 47 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"log"
"net"
"os"
"testing"
"time"
)

func TestQueryPacket(t *testing.T) {
Expand Down Expand Up @@ -114,3 +117,47 @@ func TestResponsePacket(t *testing.T) {
t.Errorf("expected record %+v, got %+v", expectedDnsRecords[0], packet.answers[0])
}
}

func TestDNSServer(t *testing.T) {
packet := NewDnsPacket()
packet.header.id = 1234
packet.header.questions = 1
packet.header.recursionDesired = true
packet.questions = append(packet.questions, DnsQuestion{name: "google.com", qtype: A})

requestBuf := NewBytePacketBuffer()
err := packet.write(requestBuf)
if err != nil {
log.Fatalf("error writing packet: %v", err)
}

udpAddr, err := net.ResolveUDPAddr("udp", "8.8.8.8:53")
if err != nil {
log.Fatalf("error resolving address: %v", err)
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
log.Fatalf("error dialing: %v", err)
}
defer conn.Close()
_, err = conn.Write(requestBuf.buf[0:requestBuf.pos])
if err != nil {
log.Fatalf("error writing request: %v", err)
}

time.Sleep(3 * time.Second)

responseBuf := NewBytePacketBuffer()
_, _, err = conn.ReadFromUDP(responseBuf.buf)
if err != nil {
log.Fatalf("error reading response: %v", err)
}

resPacket := NewDnsPacket()
err = resPacket.fromBuffer(responseBuf)
if err != nil {
log.Fatalf("error reading response: %v", err)
}

log.Printf("response packet header: %+v", resPacket)
}
Loading