Add a compression flag to msg struct

This commit is contained in:
Miek Gieben 2012-01-11 20:26:39 +01:00
parent 0008471adf
commit eafe995a56
4 changed files with 25 additions and 24 deletions

View File

@ -91,7 +91,7 @@ func (k *RR_DNSKEY) KeyTag() uint16 {
keywire.Algorithm = k.Algorithm
keywire.PublicKey = k.PublicKey
wire := make([]byte, DefaultMsgSize)
n, ok := packStruct(keywire, wire, 0, nil)
n, ok := packStruct(keywire, wire, 0, nil, false)
if !ok {
return 0
}
@ -126,7 +126,7 @@ func (k *RR_DNSKEY) ToDS(h int) *RR_DS {
keywire.Algorithm = k.Algorithm
keywire.PublicKey = k.PublicKey
wire := make([]byte, DefaultMsgSize)
n, ok := packStruct(keywire, wire, 0, nil)
n, ok := packStruct(keywire, wire, 0, nil, false)
if !ok {
return nil
}
@ -205,7 +205,7 @@ func (s *RR_RRSIG) Sign(k PrivateKey, rrset RRset) error {
// Create the desired binary blob
signdata := make([]byte, DefaultMsgSize)
n, ok := packStruct(sigwire, signdata, 0, nil)
n, ok := packStruct(sigwire, signdata, 0, nil, false)
if !ok {
return ErrPack
}
@ -300,7 +300,7 @@ func (s *RR_RRSIG) Verify(k *RR_DNSKEY, rrset RRset) error {
sigwire.SignerName = strings.ToLower(s.SignerName)
// Create the desired binary blob
signeddata := make([]byte, DefaultMsgSize)
n, ok := packStruct(sigwire, signeddata, 0, nil)
n, ok := packStruct(sigwire, signeddata, 0, nil, false)
if !ok {
return ErrPack
}
@ -492,7 +492,7 @@ func rawSignatureData(rrset RRset, s *RR_RRSIG) (buf []byte) {
ttl := h.Ttl
h.Ttl = s.OrigTtl
wire := make([]byte, DefaultMsgSize)
off, ok1 := packRR(r, wire, 0, nil)
off, ok1 := packRR(r, wire, 0, nil, false)
wire = wire[:off]
h.Ttl = ttl // restore the order in the universe TODO(mg) work on copy
h.Name = name

31
msg.go
View File

@ -74,6 +74,7 @@ type MsgHdr struct {
// The layout of a DNS message.
type Msg struct {
MsgHdr
Compress bool // If true, the message will be compressed when converted to wire format.
Question []Question
Answer []RR
Ns []RR
@ -338,7 +339,7 @@ Loop:
// Pack a reflect.StructValue into msg. Struct members can only be uint8, uint16, uint32, string,
// slices and other (often anonymous) structs.
func packStructValue(val reflect.Value, msg []byte, off int, compression map[string]int) (off1 int, ok bool) {
func packStructValue(val reflect.Value, msg []byte, off int, compression map[string]int, compress bool) (off1 int, ok bool) {
for i := 0; i < val.NumField(); i++ {
// f := val.Type().Field(i)
lenmsg := len(msg)
@ -417,7 +418,7 @@ func packStructValue(val reflect.Value, msg []byte, off int, compression map[str
// TODO(mg)
}
case reflect.Struct:
off, ok = packStructValue(fv, msg, off, compression)
off, ok = packStructValue(fv, msg, off, compression, compress)
case reflect.Uint8:
if off+1 > lenmsg {
//fmt.Fprintf(os.Stderr, "dns: overflow packing uint8")
@ -487,9 +488,9 @@ func packStructValue(val reflect.Value, msg []byte, off int, compression map[str
fallthrough // No compression
case "cdomain-name":
if val.Type().Field(i).Tag == "cdomain-name" {
off, ok = PackDomainName(s, msg, off, compression, true)
off, ok = PackDomainName(s, msg, off, compression, true && compress)
} else {
off, ok = PackDomainName(s, msg, off, compression, false)
off, ok = PackDomainName(s, msg, off, compression, false && compress)
}
if !ok {
//fmt.Fprintf(os.Stderr, "dns: overflow packing domain-name")
@ -541,8 +542,8 @@ func structValue(any interface{}) reflect.Value {
return reflect.ValueOf(any).Elem()
}
func packStruct(any interface{}, msg []byte, off int, compression map[string]int) (off1 int, ok bool) {
off, ok = packStructValue(structValue(any), msg, off, compression)
func packStruct(any interface{}, msg []byte, off int, compression map[string]int, compress bool) (off1 int, ok bool) {
off, ok = packStructValue(structValue(any), msg, off, compression, compress)
return off, ok
}
@ -872,12 +873,12 @@ func packBase32(s []byte) ([]byte, error) {
}
// Resource record packer.
func packRR(rr RR, msg []byte, off int, compression map[string]int) (off1 int, ok bool) {
func packRR(rr RR, msg []byte, off int, compression map[string]int, compress bool) (off1 int, ok bool) {
if rr == nil {
return len(msg), false
}
off1, ok = packStruct(rr, msg, off, compression)
off1, ok = packStruct(rr, msg, off, compression, compress)
if !ok {
return len(msg), false
}
@ -1018,21 +1019,21 @@ func (dns *Msg) Pack() (msg []byte, ok bool) {
// Pack it in: header and then the pieces.
off := 0
off, ok = packStruct(&dh, msg, off, compression)
off, ok = packStruct(&dh, msg, off, compression, dns.Compress)
for i := 0; i < len(question); i++ {
off, ok = packStruct(&question[i], msg, off, compression)
off, ok = packStruct(&question[i], msg, off, compression, dns.Compress)
// println("Question", off)
}
for i := 0; i < len(answer); i++ {
off, ok = packRR(answer[i], msg, off, compression)
off, ok = packRR(answer[i], msg, off, compression, dns.Compress)
// println("Answer", off)
}
for i := 0; i < len(ns); i++ {
off, ok = packRR(ns[i], msg, off, compression)
off, ok = packRR(ns[i], msg, off, compression, dns.Compress)
// println("Authority", off)
}
for i := 0; i < len(extra); i++ {
off, ok = packRR(extra[i], msg, off, compression)
off, ok = packRR(extra[i], msg, off, compression, dns.Compress)
// println("Additional", off)
}
if !ok {
@ -1160,8 +1161,8 @@ func (dns *Msg) Len() int {
// CompressedLen returns the length of the message when in
// compressed wire format.
func (dns *Msg) CompressedLen() int {
// Uhh. TODO
return 0
// Uhh. TODO
return 0
}
// Id return a 16 bits random number to be used as a

View File

@ -17,7 +17,7 @@ func HashName(label string, ha, iter int, salt string) string {
saltwire := new(saltWireFmt)
saltwire.Salt = salt
wire := make([]byte, DefaultMsgSize)
n, ok := packStruct(saltwire, wire, 0, nil)
n, ok := packStruct(saltwire, wire, 0, nil, false)
if !ok {
return ""
}

View File

@ -161,7 +161,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool)
m.MACSize = uint16(len(requestMAC) / 2)
m.MAC = requestMAC
macbuf = make([]byte, len(requestMAC)) // reqmac should be twice as long
n, _ := packStruct(m, macbuf, 0, nil)
n, _ := packStruct(m, macbuf, 0, nil, false)
macbuf = macbuf[:n]
}
@ -170,7 +170,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool)
tsig := new(timerWireFmt)
tsig.TimeSigned = rr.TimeSigned
tsig.Fudge = rr.Fudge
n, _ := packStruct(tsig, tsigvar, 0, nil)
n, _ := packStruct(tsig, tsigvar, 0, nil, false)
tsigvar = tsigvar[:n]
} else {
tsig := new(tsigWireFmt)
@ -183,7 +183,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool)
tsig.Error = rr.Error
tsig.OtherLen = rr.OtherLen
tsig.OtherData = rr.OtherData
n, _ := packStruct(tsig, tsigvar, 0, nil)
n, _ := packStruct(tsig, tsigvar, 0, nil, false)
tsigvar = tsigvar[:n]
}
if rr.MAC != "" {