export Unpack/PackDomainName

This commit is contained in:
Miek Gieben 2011-10-06 20:16:23 +02:00
parent 11c7307b5b
commit ad11f2425e
4 changed files with 10 additions and 8 deletions

View File

@ -7,6 +7,8 @@ import (
"fmt"
)
var privatealg = "7.nsec4.nlnetlabs.nl."
func main() {
if len(os.Args) != 2 {
fmt.Printf("%s DOMAIN\n", os.Args[0])

View File

@ -130,7 +130,7 @@ func (k *RR_DNSKEY) ToDS(h int) *RR_DS {
wire = wire[:n]
owner := make([]byte, 255)
off, ok1 := packDomainName(k.Hdr.Name, owner, 0)
off, ok1 := PackDomainName(k.Hdr.Name, owner, 0)
if !ok1 {
return nil
}
@ -451,8 +451,8 @@ type wireSlice [][]byte
func (p wireSlice) Len() int { return len(p) }
func (p wireSlice) Less(i, j int) bool {
_, ioff, _ := unpackDomainName(p[i], 0)
_, joff, _ := unpackDomainName(p[j], 0)
_, ioff, _ := UnpackDomainName(p[i], 0)
_, joff, _ := UnpackDomainName(p[j], 0)
return bytes.Compare(p[i][ioff+10:], p[j][joff+10:]) < 0
}
func (p wireSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

8
msg.go
View File

@ -174,7 +174,7 @@ var Rcode_str = map[int]string{
// Pack a domain name s into msg[off:].
// Domain names are a sequence of counted strings
// split at the dots. They end with a zero-length string.
func packDomainName(s string, msg []byte, off int) (off1 int, ok bool) {
func PackDomainName(s string, msg []byte, off int) (off1 int, ok bool) {
// Add trailing dot to canonicalize name.
lenmsg := len(msg)
if n := len(s); n == 0 || s[n-1] != '.' {
@ -240,7 +240,7 @@ func packDomainName(s string, msg []byte, off int) (off1 int, ok bool) {
// which is where the next record will start.
// In theory, the pointers are only allowed to jump backward.
// We let them jump anywhere and stop jumping after a while.
func unpackDomainName(msg []byte, off int) (s string, off1 int, ok bool) {
func UnpackDomainName(msg []byte, off int) (s string, off1 int, ok bool) {
s = ""
lenmsg := len(msg)
ptr := 0 // number of pointers followed
@ -437,7 +437,7 @@ func packStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok bool)
copy(msg[off:off+len(b64)], b64)
off += len(b64)
case "domain-name":
off, ok = packDomainName(s, msg, off)
off, ok = PackDomainName(s, msg, off)
if !ok {
//fmt.Fprintf(os.Stderr, "dns: overflow packing domain-name")
return lenmsg, false
@ -677,7 +677,7 @@ func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok boo
s = unpackBase64(msg[off : off+rdlength-consumed])
off += rdlength - consumed
case "domain-name":
s, off, ok = unpackDomainName(msg, off)
s, off, ok = UnpackDomainName(msg, off)
if !ok {
//fmt.Fprintf(os.Stderr, "dns: failure unpacking domain-name")
return lenmsg, false

View File

@ -22,7 +22,7 @@ func HashName(label string, ha int, iterations int, salt string) string {
}
wire = wire[:n]
name := make([]byte, 255)
off, ok1 := packDomainName(strings.ToLower(label), name, 0)
off, ok1 := PackDomainName(strings.ToLower(label), name, 0)
if !ok1 {
return ""
}