Skip to content

Instantly share code, notes, and snippets.

@jftuga
Created August 6, 2019 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jftuga/20edf748450cd57257e3d5de697d073e to your computer and use it in GitHub Desktop.
Save jftuga/20edf748450cd57257e3d5de697d073e to your computer and use it in GitHub Desktop.
getDNSInfo.go
// +build windows
package main
import (
"bytes"
"fmt"
"syscall"
"unsafe"
)
const (
MAX_HOSTNAME_LEN = 128
MAX_DOMAIN_NAME_LEN = 128
MAX_SCOPE_ID_LEN = 256
)
// https://docs.microsoft.com/en-us/windows/win32/api/iptypes/ns-iptypes-ip_addr_string
type ipAddressString struct {
next *ipAddressString
address [16]byte
mask [16]byte
context uint32
}
// https://docs.microsoft.com/en-us/windows/win32/api/iptypes/ns-iptypes-fixed_info_w2ksp1
type fixedInfo struct {
hostName [MAX_HOSTNAME_LEN + 4]byte
domainName [MAX_DOMAIN_NAME_LEN + 4]byte
currentDNSServer *ipAddressString
dnsServerList ipAddressString
nodeType uint16
scopeID [MAX_SCOPE_ID_LEN + 4]byte
enableRouting uint16
enableProxy uint16
enableDNS uint16
}
var (
getNetworkParams = syscall.NewLazyDLL("Iphlpapi.dll").NewProc("GetNetworkParams")
)
func p(desc string, obj []byte) {
fmt.Println(desc, ":", sliceToString(obj))
}
func sliceToString(slice []byte) string {
n := bytes.IndexByte(slice, 0)
return string(slice[:n])
}
func getDNSInfo() {
err := getNetworkParams.Find()
if err != nil {
fmt.Println("err:", err)
return
}
netInfo := [2]fixedInfo{}
size := unsafe.Sizeof(netInfo)
result, _, err := getNetworkParams.Call(uintptr(unsafe.Pointer(&netInfo[0])), uintptr(unsafe.Pointer(&size)))
if result != 0 {
fmt.Println("err:", err)
return
}
fmt.Printf("raw: %T %v\n\n", netInfo[0], netInfo[0])
p("hostName ", netInfo[0].hostName[:])
p("domainName ", netInfo[0].domainName[:])
p("dns1 address", netInfo[0].dnsServerList.address[:])
p("dns1 netmask", netInfo[0].dnsServerList.mask[:])
fmt.Println("dns1 context :", netInfo[0].dnsServerList.context)
nextDNS := netInfo[0].dnsServerList.next
if nextDNS != nil {
p("dns2 address", nextDNS.address[:])
}
}
func main() {
getDNSInfo()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment