Skip to content

Commit f2bb83c

Browse files
committed
windows: add iphlpapi functions for change notifications
The NotifyIpInterfaceChange and NotifyUnicastIpAddressChange functions register a user-defined callback function for receiving network interface and IP address change notifications. The GetIfEntry2Ex and GetUnicastIpAddressEntry functions can be called to retrieve complete information about the changed interface or address. The CancelMibChangeNotify2 function deregisters for change notifications.
1 parent 4c7077e commit f2bb83c

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

windows/syscall_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,11 @@ const socket_error = uintptr(^uint32(0))
890890
//sys GetACP() (acp uint32) = kernel32.GetACP
891891
//sys MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar
892892
//sys getBestInterfaceEx(sockaddr unsafe.Pointer, pdwBestIfIndex *uint32) (errcode error) = iphlpapi.GetBestInterfaceEx
893+
//sys GetIfEntry2Ex(level uint32, row *MibIfRow2) (errcode error) = iphlpapi.GetIfEntry2Ex
894+
//sys GetUnicastIpAddressEntry(row *MibUnicastIpAddressRow) (errcode error) = iphlpapi.GetUnicastIpAddressEntry
895+
//sys NotifyIpInterfaceChange(family uint16, callback uintptr, callerContext unsafe.Pointer, initialNotification bool, notificationHandle *Handle) (errcode error) = iphlpapi.NotifyIpInterfaceChange
896+
//sys NotifyUnicastIpAddressChange(family uint16, callback uintptr, callerContext unsafe.Pointer, initialNotification bool, notificationHandle *Handle) (errcode error) = iphlpapi.NotifyUnicastIpAddressChange
897+
//sys CancelMibChangeNotify2(notificationHandle Handle) (errcode error) = iphlpapi.CancelMibChangeNotify2
893898

894899
// For testing: clients can set this flag to force
895900
// creation of IPv6 sockets to return EAFNOSUPPORT.

windows/types_windows.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,132 @@ const (
22022202
IfOperStatusLowerLayerDown = 7
22032203
)
22042204

2205+
const (
2206+
IF_MAX_PHYS_ADDRESS_LENGTH = 32
2207+
IF_MAX_STRING_SIZE = 256
2208+
)
2209+
2210+
// MIB_IF_ENTRY_LEVEL enumeration from netioapi.h or
2211+
// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-getifentry2ex.
2212+
const (
2213+
MibIfEntryNormal = 0
2214+
MibIfEntryNormalWithoutStatistics = 2
2215+
)
2216+
2217+
// MIB_NOTIFICATION_TYPE enumeration from netioapi.h or
2218+
// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/ne-netioapi-mib_notification_type.
2219+
const (
2220+
MibParameterNotification = iota
2221+
MibAddInstance
2222+
MibDeleteInstance
2223+
MibInitialNotification
2224+
)
2225+
2226+
// MibIfRow2 stores information about a particular interface. See
2227+
// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-mib_if_row2.
2228+
type MibIfRow2 struct {
2229+
InterfaceLuid uint64
2230+
InterfaceIndex uint32
2231+
InterfaceGuid GUID
2232+
Alias [IF_MAX_STRING_SIZE + 1]uint16
2233+
Description [IF_MAX_STRING_SIZE + 1]uint16
2234+
PhysicalAddressLength uint32
2235+
PhysicalAddress [IF_MAX_PHYS_ADDRESS_LENGTH]uint8
2236+
PermanentPhysicalAddress [IF_MAX_PHYS_ADDRESS_LENGTH]uint8
2237+
Mtu uint32
2238+
Type uint32
2239+
TunnelType uint32
2240+
MediaType uint32
2241+
PhysicalMediumType uint32
2242+
AccessType uint32
2243+
DirectionType uint32
2244+
InterfaceAndOperStatusFlags uint8
2245+
OperStatus uint32
2246+
AdminStatus uint32
2247+
MediaConnectState uint32
2248+
NetworkGuid GUID
2249+
ConnectionType uint32
2250+
TransmitLinkSpeed uint64
2251+
ReceiveLinkSpeed uint64
2252+
InOctets uint64
2253+
InUcastPkts uint64
2254+
InNUcastPkts uint64
2255+
InDiscards uint64
2256+
InErrors uint64
2257+
InUnknownProtos uint64
2258+
InUcastOctets uint64
2259+
InMulticastOctets uint64
2260+
InBroadcastOctets uint64
2261+
OutOctets uint64
2262+
OutUcastPkts uint64
2263+
OutNUcastPkts uint64
2264+
OutDiscards uint64
2265+
OutErrors uint64
2266+
OutUcastOctets uint64
2267+
OutMulticastOctets uint64
2268+
OutBroadcastOctets uint64
2269+
OutQLen uint64
2270+
}
2271+
2272+
// MIB_UNICASTIPADDRESS_ROW stores information about a unicast IP address. See
2273+
// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-mib_unicastipaddress_row.
2274+
type MibUnicastIpAddressRow struct {
2275+
Address RawSockaddrInet6 // SOCKADDR_INET union
2276+
InterfaceLuid uint64
2277+
InterfaceIndex uint32
2278+
PrefixOrigin uint32
2279+
SuffixOrigin uint32
2280+
ValidLifetime uint32
2281+
PreferredLifetime uint32
2282+
OnLinkPrefixLength uint8
2283+
SkipAsSource uint8
2284+
DadState uint32
2285+
ScopeId uint32
2286+
CreationTimeStamp Filetime
2287+
}
2288+
2289+
const ScopeLevelCount = 16
2290+
2291+
// MIB_IPINTERFACE_ROW stores interface management information for a particular IP address family on a network interface.
2292+
// See https://learn.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-mib_ipinterface_row.
2293+
type MibIpInterfaceRow struct {
2294+
Family uint16
2295+
InterfaceLuid uint64
2296+
InterfaceIndex uint32
2297+
MaxReassemblySize uint32
2298+
InterfaceIdentifier uint64
2299+
MinRouterAdvertisementInterval uint32
2300+
MaxRouterAdvertisementInterval uint32
2301+
AdvertisingEnabled uint8
2302+
ForwardingEnabled uint8
2303+
WeakHostSend uint8
2304+
WeakHostReceive uint8
2305+
UseAutomaticMetric uint8
2306+
UseNeighborUnreachabilityDetection uint8
2307+
ManagedAddressConfigurationSupported uint8
2308+
OtherStatefulConfigurationSupported uint8
2309+
AdvertiseDefaultRoute uint8
2310+
RouterDiscoveryBehavior uint32
2311+
DadTransmits uint32
2312+
BaseReachableTime uint32
2313+
RetransmitTime uint32
2314+
PathMtuDiscoveryTimeout uint32
2315+
LinkLocalAddressBehavior uint32
2316+
LinkLocalAddressTimeout uint32
2317+
ZoneIndices [ScopeLevelCount]uint32
2318+
SitePrefixLength uint32
2319+
Metric uint32
2320+
NlMtu uint32
2321+
Connected uint8
2322+
SupportsWakeUpPatterns uint8
2323+
SupportsNeighborDiscovery uint8
2324+
SupportsRouterDiscovery uint8
2325+
ReachableTime uint32
2326+
TransmitOffload uint32
2327+
ReceiveOffload uint32
2328+
DisableDefaultRoutes uint8
2329+
}
2330+
22052331
// Console related constants used for the mode parameter to SetConsoleMode. See
22062332
// https://docs.microsoft.com/en-us/windows/console/setconsolemode for details.
22072333

windows/zsyscall_windows.go

Lines changed: 53 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)