/
main.go
92 lines (74 loc) · 2.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// The systemd-networkd-dns command obtains the DHCP DNS server via DBus.
//
// For this to work, you must be running both systemd-networkd and
// systemd-resolved and provide the network interface name for your NIC
// that's connected to the DHCP network.
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"github.com/godbus/dbus"
)
func main() {
args := os.Args
flag.Usage = func() {
fmt.Printf(`usage: %s <interface>
The systemd-networkd-dns command obtains the DHCP DNS server via DBus.
For this to work, you must be running both systemd-networkd and
systemd-resolved and provide the network interface name for your NIC
that's connected to the DHCP network.
`, args[0])
flag.PrintDefaults()
}
flag.Parse()
if len(args) < 2 {
log.Println("error: must provide interface name for DHCP-enabled NIC")
flag.Usage()
os.Exit(2)
}
dns, err := getDHCPDNSForInterfaceFromDBus(args[1])
if err != nil {
log.Fatalln(err)
}
fmt.Println(dns)
}
func getDHCPDNSForInterfaceFromDBus(iface string) (string, error) {
// First, we need to determine the index of the interface
// for this given interface name. Indicies start at 1.
i, err := net.InterfaceByName(iface)
if err != nil {
return "", fmt.Errorf("net.InterfaceByName() failed: %v", err)
}
// Connect to the system DBus
conn, err := dbus.SystemBus()
if err != nil {
return "", fmt.Errorf("failed to connect to system DBus: %v", err)
}
var linkPath dbus.ObjectPath
var callFlags dbus.Flags
netO := conn.Object("org.freedesktop.resolve1", "/org/freedesktop/resolve1")
netO.Call("org.freedesktop.resolve1.Manager.GetLink", callFlags, i.Index).Store(&linkPath)
linkO := conn.Object("org.freedesktop.resolve1", linkPath)
variant, err := linkO.GetProperty("org.freedesktop.resolve1.Link.DNS")
if err != nil {
return "", fmt.Errorf("error fetching DNS property from DBus: %v", err)
}
var variantVal [][]interface{}
variantVal = variant.Value().([][]interface{})
// Check the IP version of the nameserver address that was returned
// 2 == AF_INET, 26 == AF_INET6
if variantVal[0][0].(int32) != 2 {
return "", fmt.Errorf("IPv6 nameserver addresses are not currently supported")
}
ipVariantBytes := variantVal[0][1].([]uint8)
s := make([]string, len(ipVariantBytes))
for v := range ipVariantBytes {
s[v] = strconv.Itoa(int(ipVariantBytes[v]))
}
return strings.Join(s, "."), nil
}