From f45d4d933dae065dbd29873cf010a41b36b5969a Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sun, 19 Feb 2012 15:24:26 +0100 Subject: [PATCH] Add length for base64 encodings --- msg.go | 1 + types.go | 38 ++++++++++++++++++++++++-------------- zscan_rr.go | 3 +++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/msg.go b/msg.go index 393121a3..fe584c88 100644 --- a/msg.go +++ b/msg.go @@ -642,6 +642,7 @@ func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok boo default: println("dns: unknown tag unpacking slice", val.Type().Field(i).Tag) return lenmsg, false + // Need to add domain-name for HIP case "txt": txt := make([]string,0) rdlength := int(val.FieldByName("Hdr").FieldByName("Rdlength").Uint()) diff --git a/types.go b/types.go index 8df8e2a4..91f135c9 100644 --- a/types.go +++ b/types.go @@ -6,6 +6,7 @@ package dns import ( + "encoding/base64" "net" "strconv" "strings" @@ -503,7 +504,8 @@ func (rr *RR_CERT) String() string { } func (rr *RR_CERT) Len() int { - return rr.Hdr.Len() + 5 + len(rr.Certificate) //too much for base64 + return rr.Hdr.Len() + 5 + + base64.StdEncoding.DecodedLen(len(rr.Certificate)) } // See RFC 2672. @@ -612,8 +614,8 @@ func (rr *RR_RRSIG) String() string { } func (rr *RR_RRSIG) Len() int { - return rr.Hdr.Len() + len(rr.SignerName) + 1 + len(rr.Signature) + 18 - // TODO base64 string, should be less + return rr.Hdr.Len() + len(rr.SignerName) + 1 + + base64.StdEncoding.DecodedLen(len(rr.Signature)) + 18 } type RR_NSEC struct { @@ -794,8 +796,8 @@ func (rr *RR_IPSECKEY) String() string { } func (rr *RR_IPSECKEY) Len() int { - // TODO: this is not correct - return rr.Hdr.Len() + 3 + len(rr.Gateway) + len(rr.PublicKey) + return rr.Hdr.Len() + 3 + len(rr.Gateway) + 1 + + base64.StdEncoding.DecodedLen(len(rr.PublicKey)) } type RR_DNSKEY struct { @@ -818,7 +820,8 @@ func (rr *RR_DNSKEY) String() string { } func (rr *RR_DNSKEY) Len() int { - return rr.Hdr.Len() + 4 + len(rr.PublicKey) // todo: base64 + return rr.Hdr.Len() + 4 + + base64.StdEncoding.DecodedLen(len(rr.PublicKey)) } type RR_NSEC3 struct { @@ -856,7 +859,7 @@ func (rr *RR_NSEC3) String() string { func (rr *RR_NSEC3) Len() int { return rr.Hdr.Len() + 6 + len(rr.Salt)/2 + 1 + len(rr.NextDomain) + 1 + len(rr.TypeBitMap) - // TODO: size-base32 and typebitmap + // TODO: typebitmap } type RR_NSEC3PARAM struct { @@ -967,7 +970,8 @@ func (rr *RR_DHCID) String() string { } func (rr *RR_DHCID) Len() int { - return rr.Hdr.Len() + len(rr.Digest) // Slightly too much + return rr.Hdr.Len() + + base64.StdEncoding.DecodedLen(len(rr.Digest)) } // RFC 2845. @@ -1046,18 +1050,24 @@ func (rr *RR_HIP) Header() *RR_Header { } func (rr *RR_HIP) String() string { - s := rr.Hdr.String() + + s := rr.Hdr.String() + " " + strconv.Itoa(int(rr.PublicKeyAlgorithm)) + " " + rr.Hit + " " + rr.PublicKey - for _, d := range rr.RendezvousServers { - s += " " + d - } - return s + for _, d := range rr.RendezvousServers { + s += " " + d + } + return s } func (rr *RR_HIP) Len() int { - return rr.Hdr.Len() // TODO + l := rr.Hdr.Len() + 4 + + len(rr.Hit)/2 + + base64.StdEncoding.DecodedLen(len(rr.PublicKey)) + for _, d := range rr.RendezvousServers { + l += len(d) + 1 + } + return l } // Translate the RRSIG's incep. and expir. time to the correct date. diff --git a/zscan_rr.go b/zscan_rr.go index 5b62c8aa..5802765c 100644 --- a/zscan_rr.go +++ b/zscan_rr.go @@ -4,6 +4,7 @@ import ( "net" "strconv" "strings" + "encoding/base64" ) // Parse the rdata of each rrtype. @@ -407,10 +408,12 @@ func setHIP(h RR_Header, c chan lex, o, f string) (RR, *ParseError) { <-c // _BLANK l = <-c // _STRING rr.Hit = l.token // This can not contain spaces, see RFC 5205 Section 6. + rr.HitLength = uint8(len(rr.Hit))/2 <-c // _BLANK l = <-c // _STRING rr.PublicKey = l.token // This cannot contain spaces + rr.PublicKeyLength = uint16(base64.StdEncoding.DecodedLen(len(rr.PublicKey))) // RendezvousServers (if any) l = <-c