aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Wallace <doof@doof.net>2021-06-25 23:04:34 -0700
committerKevin Wallace <doof@doof.net>2021-06-25 23:08:29 -0700
commit94f1e2f000dc79cbb1ef57202f5e6fc354205a21 (patch)
treec91764ba9b7069b8163ed19c0dc1b9459eaf21ff
parentupdate README.md examples now that NXDOMAINs are unreported (diff)
add -asn flag to look up ASN info
-rw-r--r--main.go42
1 files changed, 40 insertions, 2 deletions
diff --git a/main.go b/main.go
index ca024a9..01401d4 100644
--- a/main.go
+++ b/main.go
@@ -10,6 +10,10 @@ import (
var (
txt = flag.Bool("txt", false, "also look up TXT records")
+ asn = flag.Bool("asn", false, "also look up ASN info from -asn-zone4/-asn-zone6 reverse zones")
+
+ asnZone4 = flag.String("asn-zone4", "origin.asn.cymru.com", "v4 reverse zone for -asn")
+ asnZone6 = flag.String("asn-zone6", "origin6.asn.cymru.com", "v6 reverse zone for -asn")
)
func main() {
@@ -20,6 +24,16 @@ func main() {
} else {
fmt.Printf("# %s\n", arg)
for _, ipnet := range ipnets {
+ if *asn {
+ asnInfos, err := lookupAsnInfo(ipnet)
+ if err != nil {
+ printErr(ipnet, "ASN", err)
+ } else {
+ for _, asnInfo := range asnInfos {
+ fmt.Printf("# %s\n", asnInfo)
+ }
+ }
+ }
ip := ipnet.IP
for ipnet.Contains(ip) {
if names, err := net.LookupAddr(ip.String()); err != nil {
@@ -44,12 +58,12 @@ func main() {
}
}
-func printErr(ip net.IP, rrtype string, err error) {
+func printErr(what interface{}, rrtype string, err error) {
// the error types here are awful and this feels like it might
// accidentally catch some non-NXDOMAIN errors
if !strings.HasSuffix(err.Error(), "no such host") &&
!strings.HasSuffix(err.Error(), "nodename nor servname provided, or not known") {
- fmt.Fprintf(os.Stderr, "%s\t# %s error: %s\n", ip, rrtype, err)
+ fmt.Fprintf(os.Stderr, "%s\t# %s error: %s\n", what, rrtype, err)
}
}
@@ -126,6 +140,9 @@ func resolveArg(arg string) ([]net.IPNet, error) {
ipnets[i] = *ipnet
}
} else {
+ if ip4 := ip.To4(); ip4 != nil {
+ ip = ip4
+ }
ipnets[i].IP = ip
ipnets[i].Mask = net.CIDRMask(len(ip)*8, len(ip)*8)
}
@@ -143,3 +160,24 @@ func resolveHost(host string, canLookup bool) ([]net.IP, error) {
}
return nil, fmt.Errorf("can't parse IP")
}
+
+func lookupAsnInfo(ipnet net.IPNet) ([]string, error) {
+ ones, bits := ipnet.Mask.Size()
+ if bits == 0 {
+ return nil, fmt.Errorf("invalid netmask in %s", ipnet)
+ }
+ var name string
+ if ip4 := ipnet.IP.To4(); ip4 != nil {
+ for b := (ones-1)/8; b >= 0; b-- {
+ name += fmt.Sprintf("%d.", ip4[b])
+ }
+ name += *asnZone4
+ } else {
+ ip6 := ipnet.IP
+ for n := (ones-1)/4; n >= 0; n-- {
+ name += fmt.Sprintf("%x.", (ip6[n/2] >> (4-4*(n%2))) & 0xf)
+ }
+ name += *asnZone6
+ }
+ return net.LookupTXT(name)
+}