Skip to content

Commit ea436ef

Browse files
database64128qmuntal
authored andcommitted
windows: add iphlpapi routing functions
NotifyRouteChange2 registers to be notified for changes to IP route entries. Call GetIpForwardEntry2 on received row to retrieve complete information. GetIpForwardTable2 retrieves the full routing table. FreeMibTable frees the buffer allocated by the functions that return tables of network interfaces, addresses, and routes. Change-Id: I9fba75f1f728661e45dc3092e35eb9099b0570dd Reviewed-on: https://go-review.googlesource.com/c/sys/+/695195 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Quim Muntal <[email protected]> Reviewed-by: Michael Pratt <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 28c5bda commit ea436ef

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

windows/syscall_windows.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,12 @@ const socket_error = uintptr(^uint32(0))
892892
//sys MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar
893893
//sys getBestInterfaceEx(sockaddr unsafe.Pointer, pdwBestIfIndex *uint32) (errcode error) = iphlpapi.GetBestInterfaceEx
894894
//sys GetIfEntry2Ex(level uint32, row *MibIfRow2) (errcode error) = iphlpapi.GetIfEntry2Ex
895+
//sys GetIpForwardEntry2(row *MibIpForwardRow2) (errcode error) = iphlpapi.GetIpForwardEntry2
896+
//sys GetIpForwardTable2(family uint16, table **MibIpForwardTable2) (errcode error) = iphlpapi.GetIpForwardTable2
895897
//sys GetUnicastIpAddressEntry(row *MibUnicastIpAddressRow) (errcode error) = iphlpapi.GetUnicastIpAddressEntry
898+
//sys FreeMibTable(memory unsafe.Pointer) = iphlpapi.FreeMibTable
896899
//sys NotifyIpInterfaceChange(family uint16, callback uintptr, callerContext unsafe.Pointer, initialNotification bool, notificationHandle *Handle) (errcode error) = iphlpapi.NotifyIpInterfaceChange
900+
//sys NotifyRouteChange2(family uint16, callback uintptr, callerContext unsafe.Pointer, initialNotification bool, notificationHandle *Handle) (errcode error) = iphlpapi.NotifyRouteChange2
897901
//sys NotifyUnicastIpAddressChange(family uint16, callback uintptr, callerContext unsafe.Pointer, initialNotification bool, notificationHandle *Handle) (errcode error) = iphlpapi.NotifyUnicastIpAddressChange
898902
//sys CancelMibChangeNotify2(notificationHandle Handle) (errcode error) = iphlpapi.CancelMibChangeNotify2
899903

@@ -916,6 +920,17 @@ type RawSockaddrInet6 struct {
916920
Scope_id uint32
917921
}
918922

923+
// RawSockaddrInet is a union that contains an IPv4, an IPv6 address, or an address family. See
924+
// https://learn.microsoft.com/en-us/windows/win32/api/ws2ipdef/ns-ws2ipdef-sockaddr_inet.
925+
//
926+
// A [*RawSockaddrInet] may be converted to a [*RawSockaddrInet4] or [*RawSockaddrInet6] using
927+
// unsafe, depending on the address family.
928+
type RawSockaddrInet struct {
929+
Family uint16
930+
Port uint16
931+
Data [6]uint32
932+
}
933+
919934
type RawSockaddr struct {
920935
Family uint16
921936
Data [14]int8

windows/types_windows.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,82 @@ type MibIfRow2 struct {
23202320
OutQLen uint64
23212321
}
23222322

2323+
// IP_ADDRESS_PREFIX stores an IP address prefix. See
2324+
// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-ip_address_prefix.
2325+
type IpAddressPrefix struct {
2326+
Prefix RawSockaddrInet
2327+
PrefixLength uint8
2328+
}
2329+
2330+
// NL_ROUTE_ORIGIN enumeration from nldef.h or
2331+
// https://learn.microsoft.com/en-us/windows/win32/api/nldef/ne-nldef-nl_route_origin.
2332+
const (
2333+
NlroManual = 0
2334+
NlroWellKnown = 1
2335+
NlroDHCP = 2
2336+
NlroRouterAdvertisement = 3
2337+
Nlro6to4 = 4
2338+
)
2339+
2340+
// NL_ROUTE_ORIGIN enumeration from nldef.h or
2341+
// https://learn.microsoft.com/en-us/windows/win32/api/nldef/ne-nldef-nl_route_protocol.
2342+
const (
2343+
MIB_IPPROTO_OTHER = 1
2344+
MIB_IPPROTO_LOCAL = 2
2345+
MIB_IPPROTO_NETMGMT = 3
2346+
MIB_IPPROTO_ICMP = 4
2347+
MIB_IPPROTO_EGP = 5
2348+
MIB_IPPROTO_GGP = 6
2349+
MIB_IPPROTO_HELLO = 7
2350+
MIB_IPPROTO_RIP = 8
2351+
MIB_IPPROTO_IS_IS = 9
2352+
MIB_IPPROTO_ES_IS = 10
2353+
MIB_IPPROTO_CISCO = 11
2354+
MIB_IPPROTO_BBN = 12
2355+
MIB_IPPROTO_OSPF = 13
2356+
MIB_IPPROTO_BGP = 14
2357+
MIB_IPPROTO_IDPR = 15
2358+
MIB_IPPROTO_EIGRP = 16
2359+
MIB_IPPROTO_DVMRP = 17
2360+
MIB_IPPROTO_RPL = 18
2361+
MIB_IPPROTO_DHCP = 19
2362+
MIB_IPPROTO_NT_AUTOSTATIC = 10002
2363+
MIB_IPPROTO_NT_STATIC = 10006
2364+
MIB_IPPROTO_NT_STATIC_NON_DOD = 10007
2365+
)
2366+
2367+
// MIB_IPFORWARD_ROW2 stores information about an IP route entry. See
2368+
// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-mib_ipforward_row2.
2369+
type MibIpForwardRow2 struct {
2370+
InterfaceLuid uint64
2371+
InterfaceIndex uint32
2372+
DestinationPrefix IpAddressPrefix
2373+
NextHop RawSockaddrInet
2374+
SitePrefixLength uint8
2375+
ValidLifetime uint32
2376+
PreferredLifetime uint32
2377+
Metric uint32
2378+
Protocol uint32
2379+
Loopback uint8
2380+
AutoconfigureAddress uint8
2381+
Publish uint8
2382+
Immortal uint8
2383+
Age uint32
2384+
Origin uint32
2385+
}
2386+
2387+
// MIB_IPFORWARD_TABLE2 contains a table of IP route entries. See
2388+
// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-mib_ipforward_table2.
2389+
type MibIpForwardTable2 struct {
2390+
NumEntries uint32
2391+
Table [1]MibIpForwardRow2
2392+
}
2393+
2394+
// Rows returns the IP route entries in the table.
2395+
func (t *MibIpForwardTable2) Rows() []MibIpForwardRow2 {
2396+
return unsafe.Slice(&t.Table[0], t.NumEntries)
2397+
}
2398+
23232399
// MIB_UNICASTIPADDRESS_ROW stores information about a unicast IP address. See
23242400
// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-mib_unicastipaddress_row.
23252401
type MibUnicastIpAddressRow struct {

windows/zsyscall_windows.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)