Created
August 6, 2019 10:33
-
-
Save jftuga/20edf748450cd57257e3d5de697d073e to your computer and use it in GitHub Desktop.
getDNSInfo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +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