Skip to content

Commit a8309cd

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 ca04041 commit a8309cd

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

windows/syscall_windows.go

+5
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,11 @@ const socket_error = uintptr(^uint32(0))
886886
//sys GetACP() (acp uint32) = kernel32.GetACP
887887
//sys MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar
888888
//sys getBestInterfaceEx(sockaddr unsafe.Pointer, pdwBestIfIndex *uint32) (errcode error) = iphlpapi.GetBestInterfaceEx
889+
//sys GetIfEntry2Ex(level uint32, row *MibIfRow2) (errcode error) = iphlpapi.GetIfEntry2Ex
890+
//sys GetUnicastIpAddressEntry(row *MibUnicastIpAddressRow) (errcode error) = iphlpapi.GetUnicastIpAddressEntry
891+
//sys NotifyIpInterfaceChange(family uint16, callback uintptr, callerContext unsafe.Pointer, initialNotification bool, notificationHandle *Handle) (errcode error) = iphlpapi.NotifyIpInterfaceChange
892+
//sys NotifyUnicastIpAddressChange(family uint16, callback uintptr, callerContext unsafe.Pointer, initialNotification bool, notificationHandle *Handle) (errcode error) = iphlpapi.NotifyUnicastIpAddressChange
893+
//sys CancelMibChangeNotify2(notificationHandle Handle) (errcode error) = iphlpapi.CancelMibChangeNotify2
889894

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

windows/types_windows.go

+126
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,132 @@ const (
22032203
IfOperStatusLowerLayerDown = 7
22042204
)
22052205

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

windows/zsyscall_windows.go

+53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)