| 1 | package dns_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "net" |
| 8 | |
| 9 | "github.com/miekg/dns" |
| 10 | ) |
| 11 | |
| 12 | // Retrieve the MX records for miek.nl. |
| 13 | func ExampleMX() { |
| 14 | config, _ := dns.ClientConfigFromFile("/etc/resolv.conf") |
| 15 | c := new(dns.Client) |
| 16 | m := new(dns.Msg) |
| 17 | m.SetQuestion("miek.nl.", dns.TypeMX) |
| 18 | m.RecursionDesired = true |
| 19 | r, _, err := c.Exchange(m, net.JoinHostPort(config.Servers[0], config.Port)) |
| 20 | if err != nil { |
| 21 | return |
| 22 | } |
| 23 | if r.Rcode != dns.RcodeSuccess { |
| 24 | return |
| 25 | } |
| 26 | for _, a := range r.Answer { |
| 27 | if mx, ok := a.(*dns.MX); ok { |
| 28 | fmt.Printf("%s\n", mx.String()) |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Retrieve the DNSKEY records of a zone and convert them |
| 34 | // to DS records for SHA1, SHA256 and SHA384. |
| 35 | func ExampleDS() { |
| 36 | config, _ := dns.ClientConfigFromFile("/etc/resolv.conf") |
| 37 | c := new(dns.Client) |
| 38 | m := new(dns.Msg) |
| 39 | zone := "miek.nl" |
| 40 | m.SetQuestion(dns.Fqdn(zone), dns.TypeDNSKEY) |
| 41 | m.SetEdns0(4096, true) |
| 42 | r, _, err := c.Exchange(m, net.JoinHostPort(config.Servers[0], config.Port)) |
| 43 | if err != nil { |
| 44 | return |
| 45 | } |
| 46 | if r.Rcode != dns.RcodeSuccess { |
| 47 | return |
| 48 | } |
| 49 | for _, k := range r.Answer { |
| 50 | if key, ok := k.(*dns.DNSKEY); ok { |
| 51 | for _, alg := range []uint8{dns.SHA1, dns.SHA256, dns.SHA384} { |
| 52 | fmt.Printf("%s; %d\n", key.ToDS(alg).String(), key.Flags) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const TypeAPAIR = 0x0F99 |
| 59 | |
| 60 | type APAIR struct { |
| 61 | addr [2]net.IP |
| 62 | } |
| 63 | |
| 64 | func NewAPAIR() dns.PrivateRdata { return new(APAIR) } |
| 65 | |
| 66 | func (rd *APAIR) String() string { return rd.addr[0].String() + " " + rd.addr[1].String() } |
| 67 | |
| 68 | func (rd *APAIR) Parse(txt []string) error { |
| 69 | if len(txt) != 2 { |
| 70 | return errors.New("two addresses required for APAIR") |
| 71 | } |
| 72 | for i, s := range txt { |
| 73 | ip := net.ParseIP(s) |
| 74 | if ip == nil { |
| 75 | return errors.New("invalid IP in APAIR text representation") |
| 76 | } |
| 77 | rd.addr[i] = ip |
| 78 | } |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | func (rd *APAIR) Pack(buf []byte) (int, error) { |
| 83 | b := append([]byte(rd.addr[0]), []byte(rd.addr[1])...) |
| 84 | n := copy(buf, b) |
| 85 | if n != len(b) { |
| 86 | return n, dns.ErrBuf |
| 87 | } |
| 88 | return n, nil |
| 89 | } |
| 90 | |
| 91 | func (rd *APAIR) Unpack(buf []byte) (int, error) { |
| 92 | ln := net.IPv4len * 2 |
| 93 | if len(buf) != ln { |
| 94 | return 0, errors.New("invalid length of APAIR rdata") |
| 95 | } |
| 96 | cp := make([]byte, ln) |
| 97 | copy(cp, buf) // clone bytes to use them in IPs |
| 98 | |
| 99 | rd.addr[0] = net.IP(cp[:3]) |
| 100 | rd.addr[1] = net.IP(cp[4:]) |
| 101 | |
| 102 | return len(buf), nil |
| 103 | } |
| 104 | |
| 105 | func (rd *APAIR) Copy(dest dns.PrivateRdata) error { |
| 106 | cp := make([]byte, rd.Len()) |
| 107 | _, err := rd.Pack(cp) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | |
| 112 | d := dest.(*APAIR) |
| 113 | d.addr[0] = net.IP(cp[:3]) |
| 114 | d.addr[1] = net.IP(cp[4:]) |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | func (rd *APAIR) Len() int { |
| 119 | return net.IPv4len * 2 |
| 120 | } |
| 121 | |
| 122 | func ExamplePrivateHandle() { |
| 123 | dns.PrivateHandle("APAIR", TypeAPAIR, NewAPAIR) |
| 124 | defer dns.PrivateHandleRemove(TypeAPAIR) |
| 125 | var oldId = dns.Id |
| 126 | dns.Id = func() uint16 { return 3 } |
| 127 | defer func() { dns.Id = oldId }() |
| 128 | |
| 129 | rr, err := dns.NewRR("miek.nl. APAIR (1.2.3.4 1.2.3.5)") |
| 130 | if err != nil { |
| 131 | log.Fatal("could not parse APAIR record: ", err) |
| 132 | } |
| 133 | fmt.Println(rr) // see first line of Output below |
| 134 | |
| 135 | m := new(dns.Msg) |
| 136 | m.SetQuestion("miek.nl.", TypeAPAIR) |
| 137 | m.Answer = append(m.Answer, rr) |
| 138 | |
| 139 | fmt.Println(m) |
| 140 | // Output: miek.nl. 3600 IN APAIR 1.2.3.4 1.2.3.5 |
| 141 | // ;; opcode: QUERY, status: NOERROR, id: 3 |
| 142 | // ;; flags: rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 |
| 143 | // |
| 144 | // ;; QUESTION SECTION: |
| 145 | // ;miek.nl. IN APAIR |
| 146 | // |
| 147 | // ;; ANSWER SECTION: |
| 148 | // miek.nl. 3600 IN APAIR 1.2.3.4 1.2.3.5 |
| 149 | } |