Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add EDNS Client Subnet option as a client? #337

Closed
sajal opened this issue Apr 5, 2016 · 2 comments
Closed

How to add EDNS Client Subnet option as a client? #337

sajal opened this issue Apr 5, 2016 · 2 comments

Comments

@sajal
Copy link

sajal commented Apr 5, 2016

Hi, I am trying to issue query to a server that supports ECS. I see example code for creating the option, but how to attach it to an outgoing message?

I see there is ways to read using Msg.IsEdns0 when acting as server, but how do I set it as a client?

Example code:-

package main

import (
    "github.com/miekg/dns"
    "log"
    "net"
    "strings"
)

func main() {
    c := new(dns.Client)

    m := new(dns.Msg)
    m.SetQuestion("something.example.org.", dns.TypeCNAME)

    o := new(dns.OPT)
    o.Hdr.Name = "."
    o.Hdr.Rrtype = dns.TypeOPT
    e := new(dns.EDNS0_SUBNET)
    e.Code = dns.EDNS0SUBNET
    e.Family = 1         // 1 for IPv4 source address, 2 for IPv6
    e.SourceNetmask = 32 // 32 for IPV4, 128 for IPv6
    e.SourceScope = 0
    e.Address = net.ParseIP("1.2.3.4").To4()
    o.Option = append(o.Option, e)

    //QUESTION: How do I attach o to m?

    //log.Println(o)
    //m.SetEdns0(4096, false)
    //log.Println(m.IsEdns0())
    //log.Println(m)
    in, rtt, err := c.Exchange(m, "ns1.example.org:53")
    if err != nil {
        log.Fatal(err)
    }
    log.Println(in)
    log.Println(rtt)
    if t, ok := in.Answer[0].(*dns.CNAME); ok {
        log.Println(strings.ToUpper(strings.Split(t.Target, ".")[0]))
    } else {
        log.Fatal("should not reach here...")
    }
}
@miekg
Copy link
Owner

miekg commented Apr 5, 2016

[ Quoting notifications@github.com in "[miekg/dns] How to add EDNS Client ..." ]

m := new(dns.Msg)
m.SetQuestion("something.example.org.", dns.TypeCNAME)

o := new(dns.OPT)
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
e := new(dns.EDNS0_SUBNET)
e.Code = dns.EDNS0SUBNET
e.Family = 1 // 1 for IPv4 source address, 2 for IPv6
e.SourceNetmask = 32 // 32 for IPV4, 128 for IPv6
e.SourceScope = 0
e.Address = net.ParseIP("1.2.3.4").To4()
o.Option = append(o.Option, e)

//QUESTION: How do I attach o to m?

m.Extra = append(m.Extra, o)

@sajal
Copy link
Author

sajal commented Apr 5, 2016

Thanks a lot. Works as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants