Skip to content

Commit

Permalink
fix: Fixes the errors in byte conversion of Header and Question
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh-2909 committed Jul 9, 2024
1 parent 4c81883 commit 0f87728
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 40 deletions.
29 changes: 8 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,23 @@ func main() {
os.Exit(1)
}
domain := os.Args[1]
question := types.Question{
QType: 1,
QClass: 1,
}
question.SetName(domain)

question := types.NewQuestion(domain, 1, 1)
generateFlag := types.GenerateFlag(0, 0, 0, 0, 1, 0, 0, 0)
header := types.NewHeader(22, generateFlag, 1, 0, 0, 0)
DNSMessage := types.DNSMessage{
Header: types.Header{
ID: 22,
QR: false,
Opcode: 0,
AA: false,
TC: false,
RD: true,
RA: false,
Z: 0,
RCode: 0,
QDCount: 1,
ANCount: 0,
NSCount: 0,
ARCount: 0,
},
Header: *header,
Questions: []types.Question{
question,
*question,
},
}

fmt.Printf("Domain: %s\n", domain)
fmt.Printf("DNS Message: %+v\n\n", DNSMessage)
fmt.Printf("DNS Message in Bytes: %+v\n", DNSMessage.ToBytes())
fmt.Printf("DNS Message in Hex: %s\n", hex.EncodeToString(DNSMessage.ToBytes()))
fmt.Printf("Header in Hex: %s\n", hex.EncodeToString(header.ToBytes()))
fmt.Printf("Header in Hex: %s\n", hex.EncodeToString(question.ToBytes()))

// addr := &net.UDPAddr{
// IP: net.IPv4(127, 0, 0, 1),
Expand Down
3 changes: 1 addition & 2 deletions types/dns_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"bytes"
"encoding/binary"
)

// See https://datatracker.ietf.org/doc/html/rfc1035#section-4.1 for more information
Expand All @@ -19,7 +18,7 @@ func (m *DNSMessage) ToBytes() []byte {
buf := new(bytes.Buffer)

// Write the header to the buffer
binary.Write(buf, binary.BigEndian, m.Header)
buf.Write(m.Header.ToBytes())

// Write the questions to the buffer
for _, q := range m.Questions {
Expand Down
60 changes: 51 additions & 9 deletions types/header.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
package types

import (
"bytes"
"encoding/binary"
)

// See https://datatracker.ietf.org/doc/html/rfc1035#section-4.1.1 for more information
type Header struct {
ID uint16
QR bool
Opcode uint8
AA bool
TC bool
RD bool
RA bool
Z uint8
RCode uint8
ID uint16
Flags uint16
// QR bool
// Opcode uint8
// AA bool
// TC bool
// RD bool
// RA bool
// Z uint8
// RCode uint8
QDCount uint16
ANCount uint16
NSCount uint16
ARCount uint16
}

func NewHeader(id, flags, qdCount, anCount, nsCount, arCount uint16) *Header {
return &Header{
ID: id,
// QR: false,
// Opcode: 0,
// AA: false,
// TC: false,
// RD: false,
// RA: false,
// Z: 0,
// RCode: 0,
Flags: flags,
QDCount: qdCount,
ANCount: anCount,
NSCount: nsCount,
ARCount: arCount,
}
}

func GenerateFlag(qr, opcode, aa, tc, rd, ra, z, rcode uint16) uint16 {
return uint16(qr<<15 | opcode<<11 | aa<<10 | tc<<9 | rd<<8 | ra<<7 | z<<4 | rcode)
}

func (h *Header) ToBytes() []byte {
buf := new(bytes.Buffer)

binary.Write(buf, binary.BigEndian, h.ID)
binary.Write(buf, binary.BigEndian, h.Flags)
binary.Write(buf, binary.BigEndian, h.QDCount)
binary.Write(buf, binary.BigEndian, h.ANCount)
binary.Write(buf, binary.BigEndian, h.NSCount)
binary.Write(buf, binary.BigEndian, h.ARCount)

return buf.Bytes()
}
20 changes: 12 additions & 8 deletions types/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
)

Expand All @@ -15,24 +14,29 @@ type Question struct {
QClass uint16
}

func NewQuestion(name string, qType, qClass uint16) *Question {
q := &Question{
Name: name,
QType: qType,
QClass: qClass,
}
q.QName = convertToQName(name)
return q
}

func (q *Question) SetName(name string) {
q.Name = name
// Update the QName value based on some algorithm
// Replace this with your own algorithm
q.QName = convertToQName(name)
}

func convertToQName(name string) string {
// Implement your algorithm to convert the name to QName
// Example algorithm: replace '.' with '-'
// return strings.ReplaceAll(name, ".", "-")
domain_parts := strings.Split(name, ".")
qname := ""
for _, part := range domain_parts {
new_domain_part := fmt.Sprintf("%d%s", len(part), part)
new_domain_part := string(byte(len(part))) + part
qname += new_domain_part
}
return qname + "0"
return qname + "\x00"
}

func (q *Question) ToBytes() []byte {
Expand Down

0 comments on commit 0f87728

Please sign in to comment.