dns/example_test.go

150 lines
3.3 KiB
Go
Raw Permalink Normal View History

package dns_test
2012-09-02 17:02:59 +10:00
import (
"errors"
2012-09-02 17:02:59 +10:00
"fmt"
"log"
"net"
2015-10-01 02:08:17 +10:00
"github.com/miekg/dns"
2012-09-02 17:02:59 +10:00
)
// Retrieve the MX records for miek.nl.
2013-04-02 18:09:36 +11:00
func ExampleMX() {
config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion("miek.nl.", dns.TypeMX)
2012-09-02 17:02:59 +10:00
m.RecursionDesired = true
r, _, err := c.Exchange(m, net.JoinHostPort(config.Servers[0], config.Port))
2012-09-02 17:02:59 +10:00
if err != nil {
return
}
if r.Rcode != dns.RcodeSuccess {
2012-09-02 17:02:59 +10:00
return
}
for _, a := range r.Answer {
if mx, ok := a.(*dns.MX); ok {
2012-09-02 17:02:59 +10:00
fmt.Printf("%s\n", mx.String())
}
}
}
2012-09-03 20:02:37 +10:00
// Retrieve the DNSKEY records of a zone and convert them
// to DS records for SHA1, SHA256 and SHA384.
2015-10-01 02:08:17 +10:00
func ExampleDS() {
config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
c := new(dns.Client)
m := new(dns.Msg)
2015-10-01 02:42:47 +10:00
zone := "miek.nl"
m.SetQuestion(dns.Fqdn(zone), dns.TypeDNSKEY)
2012-09-03 20:02:37 +10:00
m.SetEdns0(4096, true)
r, _, err := c.Exchange(m, net.JoinHostPort(config.Servers[0], config.Port))
2012-09-03 20:02:37 +10:00
if err != nil {
return
}
if r.Rcode != dns.RcodeSuccess {
2012-09-03 20:02:37 +10:00
return
}
for _, k := range r.Answer {
if key, ok := k.(*dns.DNSKEY); ok {
for _, alg := range []uint8{dns.SHA1, dns.SHA256, dns.SHA384} {
2012-09-03 20:02:37 +10:00
fmt.Printf("%s; %d\n", key.ToDS(alg).String(), key.Flags)
}
}
}
}
const TypeAPAIR = 0x0F99
type APAIR struct {
addr [2]net.IP
}
func NewAPAIR() dns.PrivateRdata { return new(APAIR) }
func (rd *APAIR) String() string { return rd.addr[0].String() + " " + rd.addr[1].String() }
func (rd *APAIR) Parse(txt []string) error {
if len(txt) != 2 {
2014-09-21 21:10:55 +10:00
return errors.New("two addresses required for APAIR")
}
for i, s := range txt {
ip := net.ParseIP(s)
if ip == nil {
2014-09-21 21:10:55 +10:00
return errors.New("invalid IP in APAIR text representation")
}
rd.addr[i] = ip
}
return nil
}
func (rd *APAIR) Pack(buf []byte) (int, error) {
b := append([]byte(rd.addr[0]), []byte(rd.addr[1])...)
n := copy(buf, b)
if n != len(b) {
return n, dns.ErrBuf
}
return n, nil
}
func (rd *APAIR) Unpack(buf []byte) (int, error) {
ln := net.IPv4len * 2
if len(buf) != ln {
2014-09-21 21:10:55 +10:00
return 0, errors.New("invalid length of APAIR rdata")
}
cp := make([]byte, ln)
copy(cp, buf) // clone bytes to use them in IPs
rd.addr[0] = net.IP(cp[:3])
rd.addr[1] = net.IP(cp[4:])
return len(buf), nil
}
func (rd *APAIR) Copy(dest dns.PrivateRdata) error {
2014-09-22 03:21:34 +10:00
cp := make([]byte, rd.Len())
_, err := rd.Pack(cp)
if err != nil {
return err
}
d := dest.(*APAIR)
d.addr[0] = net.IP(cp[:3])
d.addr[1] = net.IP(cp[4:])
return nil
}
2014-09-22 03:21:34 +10:00
func (rd *APAIR) Len() int {
return net.IPv4len * 2
}
2014-09-22 03:14:34 +10:00
func ExamplePrivateHandle() {
dns.PrivateHandle("APAIR", TypeAPAIR, NewAPAIR)
defer dns.PrivateHandleRemove(TypeAPAIR)
var oldId = dns.Id
dns.Id = func() uint16 { return 3 }
defer func() { dns.Id = oldId }()
rr, err := dns.NewRR("miek.nl. APAIR (1.2.3.4 1.2.3.5)")
if err != nil {
2014-09-21 21:10:55 +10:00
log.Fatal("could not parse APAIR record: ", err)
}
fmt.Println(rr) // see first line of Output below
m := new(dns.Msg)
m.SetQuestion("miek.nl.", TypeAPAIR)
m.Answer = append(m.Answer, rr)
fmt.Println(m)
// Output: miek.nl. 3600 IN APAIR 1.2.3.4 1.2.3.5
// ;; opcode: QUERY, status: NOERROR, id: 3
// ;; flags: rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
//
// ;; QUESTION SECTION:
// ;miek.nl. IN APAIR
//
// ;; ANSWER SECTION:
// miek.nl. 3600 IN APAIR 1.2.3.4 1.2.3.5
}