doc tweaks, Len() tweaks

This commit is contained in:
Miek Gieben 2012-02-28 20:43:08 +01:00
parent 5fec355528
commit e816e64e3e
3 changed files with 33 additions and 19 deletions

6
dns.go
View File

@ -19,15 +19,15 @@
//
// Or directly from a string:
//
// mx := NewRR("miek.nl. 3600 IN MX 10 mx.miek.nl.")
// mx, err := NewRR("miek.nl. 3600 IN MX 10 mx.miek.nl.")
//
// Or when the default TTL (3600) and class (IN) suit you:
//
// mx := NewRR("miek.nl. MX 10 mx.miek.nl.")
// mx, err := NewRR("miek.nl. MX 10 mx.miek.nl.")
//
// Or even:
//
// mx := NewRR("$ORIGIN nl.\nmiek 1H IN MX 10 mx.miek")
// mx, err := NewRR("$ORIGIN nl.\nmiek 1H IN MX 10 mx.miek")
//
// The package dns supports (async) querying/replying, incoming/outgoing Axfr/Ixfr,
// TSIG, EDNS0, dynamic updates, notifies and DNSSEC validation/signing.

View File

@ -101,3 +101,21 @@ func TestBailiwick(t *testing.T) {
}
}
}
func TestPack(t *testing.T) {
rr := []string{"US. 86400 IN NSEC 0-.us. NS SOA RRSIG NSEC DNSKEY TYPE65534"}
m := new(Msg)
var err error
m.Answer = make([]RR, 1)
for _, r := range rr {
m.Answer[0], err = NewRR(r)
if err != nil {
t.Logf("Failed to create RR: %s\n", err.Error())
continue
}
if _, ok := m.Pack(); !ok {
t.Log("Packing failed")
t.Fail()
}
}
}

28
msg.go
View File

@ -1119,6 +1119,9 @@ func (h *MsgHdr) String() string {
// Pack packs a Msg: it is converted to to wire format.
// If the dns.Compress is true the message will be in compressed wire format.
func (dns *Msg) Pack() (msg []byte, ok bool) {
if dns == nil {
return nil, false
}
var dh Header
compression := make(map[string]int) // Compression pointer mappings
@ -1278,25 +1281,18 @@ func (dns *Msg) String() string {
func (dns *Msg) Len() int {
// Message header is always 12 bytes
l := 12
if len(dns.Question) > 0 {
for i := 0; i < len(dns.Question); i++ {
l += dns.Question[i].Len()
}
for i := 0; i < len(dns.Question); i++ {
l += dns.Question[i].Len()
}
if len(dns.Answer) > 0 {
for i := 0; i < len(dns.Answer); i++ {
l += dns.Answer[i].Len()
}
println("LENGTE", len(dns.Answer))
for i := 0; i < len(dns.Answer); i++ {
l += dns.Answer[i].Len()
}
if len(dns.Ns) > 0 {
for i := 0; i < len(dns.Ns); i++ {
l += dns.Ns[i].Len()
}
for i := 0; i < len(dns.Ns); i++ {
l += dns.Ns[i].Len()
}
if len(dns.Extra) > 0 {
for i := 0; i < len(dns.Extra); i++ {
l += dns.Extra[i].Len()
}
for i := 0; i < len(dns.Extra); i++ {
l += dns.Extra[i].Len()
}
return l
}