-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixes the errors in byte conversion of Header and Question
- Loading branch information
1 parent
4c81883
commit 0f87728
Showing
4 changed files
with
72 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters