Example #1
0
File: pack.go Project: cznic/dns
// Pack packs RRs to b.
func (b *Bytes) Pack(rrs RRs) {
	w := dns.NewWirebuf()
	for _, rec := range rrs {
		//n0 := len(w.Buf)
		rec.Encode(w)
		//fmt.Printf("%d->%d, .Pack(%q)\n", n0, len(w.Buf), rec)
	}
	*b = make([]byte, len(w.Buf)) // repack tight
	copy(*b, w.Buf)
}
Example #2
0
func TestTSIG(t *testing.T) {
	// http://tools.ietf.org/html/rfc2845 3.2
	//
	// Field Name    Value       Wire Format         Meaning
	// ----------------------------------------------------------------------
	// Time Signed   853804800   00 00 32 e4 07 00   Tue Jan 21 00:00:00 1997
	// Fudge         300         01 2C               5 minutes

	const (
		mac   = "MAC"
		other = "OTHER"
	)
	ts := time.Date(1997, 1, 21, 0, 0, 0, 0, time.UTC)
	fudge := time.Second * 300
	tsig := &TSIG{"X.", ts, fudge, []byte(mac), 0x1234, 0x11, []byte(other)}
	w := dns.NewWirebuf()
	tsig.Encode(w)
	t.Logf("\n%s", hex.Dump(w.Buf))
	if g, e := len(w.Buf), 27; g != e {
		t.Fatalf("%d != %d", g, e)
	}

	if g, e := w.Buf, []byte{
		0x01, 'X', 0x00,
		0x00, 0x00, 0x32, 0xe4, 0x07, 0x00,
		0x01, 0x2c,
		0x00, 0x03, 'M', 'A', 'C',
		0x12, 0x34,
		0x00, 0x11,
		0x00, 0x05, 'O', 'T', 'H', 'E', 'R',
	}; !bytes.Equal(g, e) {
		t.Errorf("\n%s\n!=\n%s", hex.Dump(g), hex.Dump(e))
	}

	tsig2 := &TSIG{}
	p := 0
	if err := tsig2.Decode(w.Buf, &p, nil); err != nil {
		t.Fatal(err)
	}

	if g, e := p, 27; g != e {
		t.Fatalf("%d != %d", g, e)
	}

	if g, e := tsig2.String(), tsig.String(); g != e {
		t.Errorf("\n%v\n!=\n%v", g, e)
	}
}
Example #3
0
func (j *job) compression(id int, rtag string, src []byte) {
	cmp := dns.NewWirebuf()
	flag := false

	sniffer := func(p0, p9 *byte, tag dns.WireDecodeSniffed, info interface{}) {
		if flag {
			return
		}

		dn, ok := info.(dns.DomainName)
		if !ok {
			return
		}

		// "seek" according to p0
		srcX0 := idx(src, p0)
		cmp.Buf = append(cmp.Buf, make([]byte, srcX0-len(cmp.Buf))...)
		dn.Encode(cmp)
		srcX9 := idx(src, p9)
		cmpX9 := len(cmp.Buf) - 1
		dumpx := srcX0 //&^ 0xF
		if g, e := srcX9, cmpX9; g > e {
			j.err(
				id,
				1,
				SOC_ZIP,
				"%s\ndn: %q, ofs %d(%#x)\nexp:\n%sgot:\n%s",
				rtag, dn, srcX0, srcX0,
				hex.Dump(cmp.Buf[dumpx:cmpX9+1]),
				hex.Dump(src[dumpx:srcX9+1]),
			)
			flag = true
		}

	}

	m := &msg.Message{}
	p := 0
	if err := m.Decode(src, &p, sniffer); err != nil {
		log.Fatal("internal error", err)
	}
}
Example #4
0
func test0b(t *testing.T, domain string, addr net.IP, all bool) {
	buf := dns.NewWirebuf()
	msg := &Message{}
	msg.Header.ID = GenID()
	msg.Header.RD = true
	if all {
		msg.Question.STAR(domain, rr.CLASS_IN)
	} else {
		msg.Question.NS(domain, rr.CLASS_IN)
	}
	msg.Encode(buf)
	t.Log(msg)
	hd(t, "enc", buf.Buf)

	raddr, err := udpAddr(addr, 53)
	if err != nil {
		t.Fatal(err)
	}

	c, err := net.DialUDP("udp", nil, raddr)
	if err != nil {
		t.Fatal(err)
	}
	defer c.Close()

	c.SetDeadline(time.Now().Add(7 * time.Second))

	t.Logf("remote %s, local %s", raddr, c.LocalAddr())
	n, err := c.Write(buf.Buf)
	if err != nil {
		t.Fatal(err)
	}

	if n != len(buf.Buf) {
		t.Fatalf("%d != %d", n, len(buf.Buf))
	}

	rbuf := make([]byte, 2000)
	n, err = c.Read(rbuf)
	if err != nil {
		t.Fatal(err)
	}

	rbuf = rbuf[:n]
	hd(t, "rcv", rbuf)

	rxm := &Message{}
	p := 0
	if err = rxm.Decode(rbuf, &p, nil); err != nil {
		t.Fatal(err)
	}

	t.Log(rxm)

	buf2 := dns.NewWirebuf()
	rxm.Encode(buf2)
	hd(t, "reenc", buf2.Buf)
	for i, b := range rbuf {
		if b != buf2.Buf[i] {
			t.Logf("[%d] %x<>%x", i, b, buf2.Buf[i])
			hd(t, "rcv", rbuf[:i])
			break
		}
	}

	compr := &Message{}
	p = 0
	if err = compr.Decode(buf2.Buf, &p, nil); err != nil {
		t.Fatal(err)
	}

	t.Log(compr)
}