github.com/MerlinKodo/sing-tun@v0.1.15/internal/winipcfg/types.go (about) 1 /* SPDX-License-Identifier: MIT 2 * 3 * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved. 4 */ 5 6 package winipcfg 7 8 import ( 9 "encoding/binary" 10 "fmt" 11 "net/netip" 12 "strconv" 13 "unsafe" 14 15 "golang.org/x/sys/windows" 16 ) 17 18 const ( 19 anySize = 1 20 maxDNSSuffixStringLength = 256 21 maxDHCPv6DUIDLength = 130 22 ifMaxStringSize = 256 23 ifMaxPhysAddressLength = 32 24 ) 25 26 // AddressFamily enumeration specifies protocol family and is one of the windows.AF_* constants. 27 type AddressFamily uint16 28 29 // IPAAFlags enumeration describes adapter addresses flags 30 // https://docs.microsoft.com/en-us/windows/desktop/api/iptypes/ns-iptypes-_ip_adapter_addresses_lh 31 type IPAAFlags uint32 32 33 const ( 34 IPAAFlagDdnsEnabled IPAAFlags = 1 << iota 35 IPAAFlagRegisterAdapterSuffix 36 IPAAFlagDhcpv4Enabled 37 IPAAFlagReceiveOnly 38 IPAAFlagNoMulticast 39 IPAAFlagIpv6OtherStatefulConfig 40 IPAAFlagNetbiosOverTcpipEnabled 41 IPAAFlagIpv4Enabled 42 IPAAFlagIpv6Enabled 43 IPAAFlagIpv6ManagedAddressConfigurationSupported 44 ) 45 46 // IfOperStatus enumeration specifies the operational status of an interface. 47 // https://docs.microsoft.com/en-us/windows/desktop/api/ifdef/ne-ifdef-if_oper_status 48 type IfOperStatus uint32 49 50 const ( 51 IfOperStatusUp IfOperStatus = iota + 1 52 IfOperStatusDown 53 IfOperStatusTesting 54 IfOperStatusUnknown 55 IfOperStatusDormant 56 IfOperStatusNotPresent 57 IfOperStatusLowerLayerDown 58 ) 59 60 // IfType enumeration specifies interface type. 61 type IfType uint32 62 63 const ( 64 IfTypeOther IfType = 1 // None of the below 65 IfTypeRegular1822 = 2 66 IfTypeHdh1822 = 3 67 IfTypeDdnX25 = 4 68 IfTypeRfc877X25 = 5 69 IfTypeEthernetCSMACD = 6 70 IfTypeISO88023CSMACD = 7 71 IfTypeISO88024Tokenbus = 8 72 IfTypeISO88025Tokenring = 9 73 IfTypeISO88026Man = 10 74 IfTypeStarlan = 11 75 IfTypeProteon10Mbit = 12 76 IfTypeProteon80Mbit = 13 77 IfTypeHyperchannel = 14 78 IfTypeFddi = 15 79 IfTypeLapB = 16 80 IfTypeSdlc = 17 81 IfTypeDs1 = 18 // DS1-MIB 82 IfTypeE1 = 19 // Obsolete; see DS1-MIB 83 IfTypeBasicISDN = 20 84 IfTypePrimaryISDN = 21 85 IfTypePropPoint2PointSerial = 22 // proprietary serial 86 IfTypePPP = 23 87 IfTypeSoftwareLoopback = 24 88 IfTypeEon = 25 // CLNP over IP 89 IfTypeEthernet3Mbit = 26 90 IfTypeNsip = 27 // XNS over IP 91 IfTypeSlip = 28 // Generic Slip 92 IfTypeUltra = 29 // ULTRA Technologies 93 IfTypeDs3 = 30 // DS3-MIB 94 IfTypeSip = 31 // SMDS, coffee 95 IfTypeFramerelay = 32 // DTE only 96 IfTypeRs232 = 33 97 IfTypePara = 34 // Parallel port 98 IfTypeArcnet = 35 99 IfTypeArcnetPlus = 36 100 IfTypeAtm = 37 // ATM cells 101 IfTypeMioX25 = 38 102 IfTypeSonet = 39 // SONET or SDH 103 IfTypeX25Ple = 40 104 IfTypeIso88022LLC = 41 105 IfTypeLocaltalk = 42 106 IfTypeSmdsDxi = 43 107 IfTypeFramerelayService = 44 // FRNETSERV-MIB 108 IfTypeV35 = 45 109 IfTypeHssi = 46 110 IfTypeHippi = 47 111 IfTypeModem = 48 // Generic Modem 112 IfTypeAal5 = 49 // AAL5 over ATM 113 IfTypeSonetPath = 50 114 IfTypeSonetVt = 51 115 IfTypeSmdsIcip = 52 // SMDS InterCarrier Interface 116 IfTypePropVirtual = 53 // Proprietary virtual/internal 117 IfTypePropMultiplexor = 54 // Proprietary multiplexing 118 IfTypeIEEE80212 = 55 // 100BaseVG 119 IfTypeFibrechannel = 56 120 IfTypeHippiinterface = 57 121 IfTypeFramerelayInterconnect = 58 // Obsolete, use 32 or 44 122 IfTypeAflane8023 = 59 // ATM Emulated LAN for 802.3 123 IfTypeAflane8025 = 60 // ATM Emulated LAN for 802.5 124 IfTypeCctemul = 61 // ATM Emulated circuit 125 IfTypeFastether = 62 // Fast Ethernet (100BaseT) 126 IfTypeISDN = 63 // ISDN and X.25 127 IfTypeV11 = 64 // CCITT V.11/X.21 128 IfTypeV36 = 65 // CCITT V.36 129 IfTypeG703_64k = 66 // CCITT G703 at 64Kbps 130 IfTypeG703_2mb = 67 // Obsolete; see DS1-MIB 131 IfTypeQllc = 68 // SNA QLLC 132 IfTypeFastetherFX = 69 // Fast Ethernet (100BaseFX) 133 IfTypeChannel = 70 134 IfTypeIEEE80211 = 71 // Radio spread spectrum 135 IfTypeIBM370parchan = 72 // IBM System 360/370 OEMI Channel 136 IfTypeEscon = 73 // IBM Enterprise Systems Connection 137 IfTypeDlsw = 74 // Data Link Switching 138 IfTypeISDNS = 75 // ISDN S/T interface 139 IfTypeISDNU = 76 // ISDN U interface 140 IfTypeLapD = 77 // Link Access Protocol D 141 IfTypeIpswitch = 78 // IP Switching Objects 142 IfTypeRsrb = 79 // Remote Source Route Bridging 143 IfTypeAtmLogical = 80 // ATM Logical Port 144 IfTypeDs0 = 81 // Digital Signal Level 0 145 IfTypeDs0Bundle = 82 // Group of ds0s on the same ds1 146 IfTypeBsc = 83 // Bisynchronous Protocol 147 IfTypeAsync = 84 // Asynchronous Protocol 148 IfTypeCnr = 85 // Combat Net Radio 149 IfTypeIso88025rDtr = 86 // ISO 802.5r DTR 150 IfTypeEplrs = 87 // Ext Pos Loc Report Sys 151 IfTypeArap = 88 // Appletalk Remote Access Protocol 152 IfTypePropCnls = 89 // Proprietary Connectionless Proto 153 IfTypeHostpad = 90 // CCITT-ITU X.29 PAD Protocol 154 IfTypeTermpad = 91 // CCITT-ITU X.3 PAD Facility 155 IfTypeFramerelayMpi = 92 // Multiproto Interconnect over FR 156 IfTypeX213 = 93 // CCITT-ITU X213 157 IfTypeAdsl = 94 // Asymmetric Digital Subscrbr Loop 158 IfTypeRadsl = 95 // Rate-Adapt Digital Subscrbr Loop 159 IfTypeSdsl = 96 // Symmetric Digital Subscriber Loop 160 IfTypeVdsl = 97 // Very H-Speed Digital Subscrb Loop 161 IfTypeIso88025Crfprint = 98 // ISO 802.5 CRFP 162 IfTypeMyrinet = 99 // Myricom Myrinet 163 IfTypeVoiceEm = 100 // Voice recEive and transMit 164 IfTypeVoiceFxo = 101 // Voice Foreign Exchange Office 165 IfTypeVoiceFxs = 102 // Voice Foreign Exchange Station 166 IfTypeVoiceEncap = 103 // Voice encapsulation 167 IfTypeVoiceOverip = 104 // Voice over IP encapsulation 168 IfTypeAtmDxi = 105 // ATM DXI 169 IfTypeAtmFuni = 106 // ATM FUNI 170 IfTypeAtmIma = 107 // ATM IMA 171 IfTypePPPmultilinkbundle = 108 // PPP Multilink Bundle 172 IfTypeIpoverCdlc = 109 // IBM ipOverCdlc 173 IfTypeIpoverClaw = 110 // IBM Common Link Access to Workstn 174 IfTypeStacktostack = 111 // IBM stackToStack 175 IfTypeVirtualipaddress = 112 // IBM VIPA 176 IfTypeMpc = 113 // IBM multi-proto channel support 177 IfTypeIpoverAtm = 114 // IBM ipOverAtm 178 IfTypeIso88025Fiber = 115 // ISO 802.5j Fiber Token Ring 179 IfTypeTdlc = 116 // IBM twinaxial data link control 180 IfTypeGigabitethernet = 117 181 IfTypeHdlc = 118 182 IfTypeLapF = 119 183 IfTypeV37 = 120 184 IfTypeX25Mlp = 121 // Multi-Link Protocol 185 IfTypeX25Huntgroup = 122 // X.25 Hunt Group 186 IfTypeTransphdlc = 123 187 IfTypeInterleave = 124 // Interleave channel 188 IfTypeFast = 125 // Fast channel 189 IfTypeIP = 126 // IP (for APPN HPR in IP networks) 190 IfTypeDocscableMaclayer = 127 // CATV Mac Layer 191 IfTypeDocscableDownstream = 128 // CATV Downstream interface 192 IfTypeDocscableUpstream = 129 // CATV Upstream interface 193 IfTypeA12mppswitch = 130 // Avalon Parallel Processor 194 IfTypeTunnel = 131 // Encapsulation interface 195 IfTypeCoffee = 132 // Coffee pot 196 IfTypeCes = 133 // Circuit Emulation Service 197 IfTypeAtmSubinterface = 134 // ATM Sub Interface 198 IfTypeL2Vlan = 135 // Layer 2 Virtual LAN using 802.1Q 199 IfTypeL3Ipvlan = 136 // Layer 3 Virtual LAN using IP 200 IfTypeL3Ipxvlan = 137 // Layer 3 Virtual LAN using IPX 201 IfTypeDigitalpowerline = 138 // IP over Power Lines 202 IfTypeMediamailoverip = 139 // Multimedia Mail over IP 203 IfTypeDtm = 140 // Dynamic syncronous Transfer Mode 204 IfTypeDcn = 141 // Data Communications Network 205 IfTypeIpforward = 142 // IP Forwarding Interface 206 IfTypeMsdsl = 143 // Multi-rate Symmetric DSL 207 IfTypeIEEE1394 = 144 // IEEE1394 High Perf Serial Bus 208 IfTypeIfGsn = 145 209 IfTypeDvbrccMaclayer = 146 210 IfTypeDvbrccDownstream = 147 211 IfTypeDvbrccUpstream = 148 212 IfTypeAtmVirtual = 149 213 IfTypeMplsTunnel = 150 214 IfTypeSrp = 151 215 IfTypeVoiceoveratm = 152 216 IfTypeVoiceoverframerelay = 153 217 IfTypeIdsl = 154 218 IfTypeCompositelink = 155 219 IfTypeSs7Siglink = 156 220 IfTypePropWirelessP2P = 157 221 IfTypeFrForward = 158 222 IfTypeRfc1483 = 159 223 IfTypeUsb = 160 224 IfTypeIEEE8023adLag = 161 225 IfTypeBgpPolicyAccounting = 162 226 IfTypeFrf16MfrBundle = 163 227 IfTypeH323Gatekeeper = 164 228 IfTypeH323Proxy = 165 229 IfTypeMpls = 166 230 IfTypeMfSiglink = 167 231 IfTypeHdsl2 = 168 232 IfTypeShdsl = 169 233 IfTypeDs1Fdl = 170 234 IfTypePos = 171 235 IfTypeDvbAsiIn = 172 236 IfTypeDvbAsiOut = 173 237 IfTypePlc = 174 238 IfTypeNfas = 175 239 IfTypeTr008 = 176 240 IfTypeGr303Rdt = 177 241 IfTypeGr303Idt = 178 242 IfTypeIsup = 179 243 IfTypePropDocsWirelessMaclayer = 180 244 IfTypePropDocsWirelessDownstream = 181 245 IfTypePropDocsWirelessUpstream = 182 246 IfTypeHiperlan2 = 183 247 IfTypePropBwaP2MP = 184 248 IfTypeSonetOverheadChannel = 185 249 IfTypeDigitalWrapperOverheadChannel = 186 250 IfTypeAal2 = 187 251 IfTypeRadioMac = 188 252 IfTypeAtmRadio = 189 253 IfTypeImt = 190 254 IfTypeMvl = 191 255 IfTypeReachDsl = 192 256 IfTypeFrDlciEndpt = 193 257 IfTypeAtmVciEndpt = 194 258 IfTypeOpticalChannel = 195 259 IfTypeOpticalTransport = 196 260 IfTypeIEEE80216Wman = 237 261 IfTypeWwanpp = 243 // WWAN devices based on GSM technology 262 IfTypeWwanpp2 = 244 // WWAN devices based on CDMA technology 263 IfTypeIEEE802154 = 259 // IEEE 802.15.4 WPAN interface 264 IfTypeXboxWireless = 281 265 ) 266 267 // MibIfEntryLevel enumeration specifies level of interface information to retrieve in GetIfTable2Ex function call. 268 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getifentry2ex 269 type MibIfEntryLevel uint32 270 271 const ( 272 MibIfEntryNormal MibIfEntryLevel = 0 273 MibIfEntryNormalWithoutStatistics = 2 274 ) 275 276 // NdisMedium enumeration type identifies the medium types that NDIS drivers support. 277 // https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/ntddndis/ne-ntddndis-_ndis_medium 278 type NdisMedium uint32 279 280 const ( 281 NdisMedium802_3 NdisMedium = iota 282 NdisMedium802_5 283 NdisMediumFddi 284 NdisMediumWan 285 NdisMediumLocalTalk 286 NdisMediumDix // defined for convenience, not a real medium 287 NdisMediumArcnetRaw 288 NdisMediumArcnet878_2 289 NdisMediumAtm 290 NdisMediumWirelessWan 291 NdisMediumIrda 292 NdisMediumBpc 293 NdisMediumCoWan 294 NdisMedium1394 295 NdisMediumInfiniBand 296 NdisMediumTunnel 297 NdisMediumNative802_11 298 NdisMediumLoopback 299 NdisMediumWiMAX 300 NdisMediumIP 301 NdisMediumMax 302 ) 303 304 // NdisPhysicalMedium describes NDIS physical medium type. 305 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/ns-netioapi-_mib_if_row2 306 type NdisPhysicalMedium uint32 307 308 const ( 309 NdisPhysicalMediumUnspecified NdisPhysicalMedium = iota 310 NdisPhysicalMediumWirelessLan 311 NdisPhysicalMediumCableModem 312 NdisPhysicalMediumPhoneLine 313 NdisPhysicalMediumPowerLine 314 NdisPhysicalMediumDSL // includes ADSL and UADSL (G.Lite) 315 NdisPhysicalMediumFibreChannel 316 NdisPhysicalMedium1394 317 NdisPhysicalMediumWirelessWan 318 NdisPhysicalMediumNative802_11 319 NdisPhysicalMediumBluetooth 320 NdisPhysicalMediumInfiniband 321 NdisPhysicalMediumWiMax 322 NdisPhysicalMediumUWB 323 NdisPhysicalMedium802_3 324 NdisPhysicalMedium802_5 325 NdisPhysicalMediumIrda 326 NdisPhysicalMediumWiredWAN 327 NdisPhysicalMediumWiredCoWan 328 NdisPhysicalMediumOther 329 NdisPhysicalMediumNative802_15_4 330 NdisPhysicalMediumMax 331 ) 332 333 // NetIfAccessType enumeration type specifies the NDIS network interface access type. 334 // https://docs.microsoft.com/en-us/windows/desktop/api/ifdef/ne-ifdef-_net_if_access_type 335 type NetIfAccessType uint32 336 337 const ( 338 NetIfAccessLoopback NetIfAccessType = iota + 1 339 NetIfAccessBroadcast 340 NetIfAccessPointToPoint 341 NetIfAccessPointToMultiPoint 342 NetIfAccessMax 343 ) 344 345 // NetIfAdminStatus enumeration type specifies the NDIS network interface administrative status, as described in RFC 2863. 346 // https://docs.microsoft.com/en-us/windows/desktop/api/ifdef/ne-ifdef-net_if_admin_status 347 type NetIfAdminStatus uint32 348 349 const ( 350 NetIfAdminStatusUp NetIfAdminStatus = iota + 1 351 NetIfAdminStatusDown 352 NetIfAdminStatusTesting 353 ) 354 355 // NetIfConnectionType enumeration type specifies the NDIS network interface connection type. 356 // https://docs.microsoft.com/en-us/windows/desktop/api/ifdef/ne-ifdef-_net_if_connection_type 357 type NetIfConnectionType uint32 358 359 const ( 360 NetIfConnectionDedicated NetIfConnectionType = iota + 1 361 NetIfConnectionPassive 362 NetIfConnectionDemand 363 NetIfConnectionMaximum 364 ) 365 366 // NetIfDirectionType enumeration type specifies the NDIS network interface direction type. 367 // https://docs.microsoft.com/en-us/windows/desktop/api/ifdef/ne-ifdef-net_if_direction_type 368 type NetIfDirectionType uint32 369 370 const ( 371 NetIfDirectionSendReceive NetIfDirectionType = iota 372 NetIfDirectionSendOnly 373 NetIfDirectionReceiveOnly 374 NetIfDirectionMaximum 375 ) 376 377 // NetIfMediaConnectState enumeration type specifies the NDIS network interface connection state. 378 // https://docs.microsoft.com/en-us/windows/desktop/api/ifdef/ne-ifdef-_net_if_media_connect_state 379 type NetIfMediaConnectState uint32 380 381 const ( 382 MediaConnectStateUnknown NetIfMediaConnectState = iota 383 MediaConnectStateConnected 384 MediaConnectStateDisconnected 385 ) 386 387 // DadState enumeration specifies information about the duplicate address detection (DAD) state for an IPv4 or IPv6 address. 388 // https://docs.microsoft.com/en-us/windows/desktop/api/nldef/ne-nldef-nl_dad_state 389 type DadState uint32 390 391 const ( 392 DadStateInvalid DadState = iota 393 DadStateTentative 394 DadStateDuplicate 395 DadStateDeprecated 396 DadStatePreferred 397 ) 398 399 // PrefixOrigin enumeration specifies the origin of an IPv4 or IPv6 address prefix, and is used with the IP_ADAPTER_UNICAST_ADDRESS structure. 400 // https://docs.microsoft.com/en-us/windows/desktop/api/nldef/ne-nldef-nl_prefix_origin 401 type PrefixOrigin uint32 402 403 const ( 404 PrefixOriginOther PrefixOrigin = iota 405 PrefixOriginManual 406 PrefixOriginWellKnown 407 PrefixOriginDHCP 408 PrefixOriginRouterAdvertisement 409 PrefixOriginUnchanged = 1 << 4 410 ) 411 412 // LinkLocalAddressBehavior enumeration type defines the link local address behavior. 413 // https://docs.microsoft.com/en-us/windows/desktop/api/nldef/ne-nldef-_nl_link_local_address_behavior 414 type LinkLocalAddressBehavior int32 415 416 const ( 417 LinkLocalAddressAlwaysOff LinkLocalAddressBehavior = iota // Never use link locals. 418 LinkLocalAddressDelayed // Use link locals only if no other addresses. (default for IPv4). Legacy mapping: IPAutoconfigurationEnabled. 419 LinkLocalAddressAlwaysOn // Always use link locals (default for IPv6). 420 LinkLocalAddressUnchanged = -1 421 ) 422 423 // OffloadRod enumeration specifies a set of flags that indicate the offload capabilities for an IP interface. 424 // https://docs.microsoft.com/en-us/windows/desktop/api/nldef/ns-nldef-_nl_interface_offload_rod 425 type OffloadRod uint8 426 427 const ( 428 ChecksumSupported OffloadRod = 1 << iota 429 OptionsSupported 430 DatagramChecksumSupported 431 StreamChecksumSupported 432 StreamOptionsSupported 433 FastPathCompatible 434 LargeSendOffloadSupported 435 GiantSendOffloadSupported 436 ) 437 438 // RouteOrigin enumeration type defines the origin of the IP route. 439 // https://docs.microsoft.com/en-us/windows/desktop/api/nldef/ne-nldef-nl_route_origin 440 type RouteOrigin uint32 441 442 const ( 443 RouteOriginManual RouteOrigin = iota 444 RouteOriginWellKnown 445 RouteOriginDHCP 446 RouteOriginRouterAdvertisement 447 RouteOrigin6to4 448 ) 449 450 // RouteProtocol enumeration type defines the routing mechanism that an IP route was added with, as described in RFC 4292. 451 // https://docs.microsoft.com/en-us/windows/desktop/api/nldef/ne-nldef-nl_route_protocol 452 type RouteProtocol uint32 453 454 const ( 455 RouteProtocolOther RouteProtocol = iota + 1 456 RouteProtocolLocal 457 RouteProtocolNetMgmt 458 RouteProtocolIcmp 459 RouteProtocolEgp 460 RouteProtocolGgp 461 RouteProtocolHello 462 RouteProtocolRip 463 RouteProtocolIsIs 464 RouteProtocolEsIs 465 RouteProtocolCisco 466 RouteProtocolBbn 467 RouteProtocolOspf 468 RouteProtocolBgp 469 RouteProtocolIdpr 470 RouteProtocolEigrp 471 RouteProtocolDvmrp 472 RouteProtocolRpl 473 RouteProtocolDHCP 474 RouteProtocolNTAutostatic = 10002 475 RouteProtocolNTStatic = 10006 476 RouteProtocolNTStaticNonDOD = 10007 477 ) 478 479 // RouterDiscoveryBehavior enumeration type defines the router discovery behavior, as described in RFC 2461. 480 // https://docs.microsoft.com/en-us/windows/desktop/api/nldef/ne-nldef-_nl_router_discovery_behavior 481 type RouterDiscoveryBehavior int32 482 483 const ( 484 RouterDiscoveryDisabled RouterDiscoveryBehavior = iota 485 RouterDiscoveryEnabled 486 RouterDiscoveryDHCP 487 RouterDiscoveryUnchanged = -1 488 ) 489 490 // SuffixOrigin enumeration specifies the origin of an IPv4 or IPv6 address suffix, and is used with the IP_ADAPTER_UNICAST_ADDRESS structure. 491 // https://docs.microsoft.com/en-us/windows/desktop/api/nldef/ne-nldef-nl_suffix_origin 492 type SuffixOrigin uint32 493 494 const ( 495 SuffixOriginOther SuffixOrigin = iota 496 SuffixOriginManual 497 SuffixOriginWellKnown 498 SuffixOriginDHCP 499 SuffixOriginLinkLayerAddress 500 SuffixOriginRandom 501 SuffixOriginUnchanged = 1 << 4 502 ) 503 504 // MibNotificationType enumeration defines the notification type passed to a callback function when a notification occurs. 505 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/ne-netioapi-_mib_notification_type 506 type MibNotificationType uint32 507 508 const ( 509 MibParameterNotification MibNotificationType = iota // Parameter change 510 MibAddInstance // Addition 511 MibDeleteInstance // Deletion 512 MibInitialNotification // Initial notification 513 ) 514 515 type ChangeCallback interface { 516 Unregister() error 517 } 518 519 // TunnelType enumeration type defines the encapsulation method used by a tunnel, as described by the Internet Assigned Names Authority (IANA). 520 // https://docs.microsoft.com/en-us/windows/desktop/api/ifdef/ne-ifdef-tunnel_type 521 type TunnelType uint32 522 523 const ( 524 TunnelTypeNone TunnelType = 0 525 TunnelTypeOther = 1 526 TunnelTypeDirect = 2 527 TunnelType6to4 = 11 528 TunnelTypeIsatap = 13 529 TunnelTypeTeredo = 14 530 TunnelTypeIPHTTPS = 15 531 ) 532 533 // InterfaceAndOperStatusFlags enumeration type defines interface and operation flags 534 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/ns-netioapi-_mib_if_row2 535 type InterfaceAndOperStatusFlags uint8 536 537 const ( 538 IAOSFHardwareInterface InterfaceAndOperStatusFlags = 1 << iota 539 IAOSFFilterInterface 540 IAOSFConnectorPresent 541 IAOSFNotAuthenticated 542 IAOSFNotMediaConnected 543 IAOSFPaused 544 IAOSFLowPower 545 IAOSFEndPointInterface 546 ) 547 548 // GAAFlags enumeration defines flags used in GetAdaptersAddresses calls 549 // https://docs.microsoft.com/en-us/windows/desktop/api/iphlpapi/nf-iphlpapi-getadaptersaddresses 550 type GAAFlags uint32 551 552 const ( 553 GAAFlagSkipUnicast GAAFlags = 1 << iota 554 GAAFlagSkipAnycast 555 GAAFlagSkipMulticast 556 GAAFlagSkipDNSServer 557 GAAFlagIncludePrefix 558 GAAFlagSkipFriendlyName 559 GAAFlagIncludeWinsInfo 560 GAAFlagIncludeGateways 561 GAAFlagIncludeAllInterfaces 562 GAAFlagIncludeAllCompartments 563 GAAFlagIncludeTunnelBindingOrder 564 GAAFlagSkipDNSInfo 565 566 GAAFlagDefault GAAFlags = 0 567 GAAFlagSkipAll = GAAFlagSkipUnicast | GAAFlagSkipAnycast | GAAFlagSkipMulticast | GAAFlagSkipDNSServer | GAAFlagSkipFriendlyName | GAAFlagSkipDNSInfo 568 GAAFlagIncludeAll = GAAFlagIncludePrefix | GAAFlagIncludeWinsInfo | GAAFlagIncludeGateways | GAAFlagIncludeAllInterfaces | GAAFlagIncludeAllCompartments | GAAFlagIncludeTunnelBindingOrder 569 ) 570 571 // ScopeLevel enumeration is used with the IP_ADAPTER_ADDRESSES structure to identify scope levels for IPv6 addresses. 572 // https://docs.microsoft.com/en-us/windows/desktop/api/ws2def/ne-ws2def-scope_level 573 type ScopeLevel uint32 574 575 const ( 576 ScopeLevelInterface ScopeLevel = 1 577 ScopeLevelLink = 2 578 ScopeLevelSubnet = 3 579 ScopeLevelAdmin = 4 580 ScopeLevelSite = 5 581 ScopeLevelOrganization = 8 582 ScopeLevelGlobal = 14 583 ScopeLevelCount = 16 584 ) 585 586 // RouteData structure describes a route to add 587 type RouteData struct { 588 Destination netip.Prefix 589 NextHop netip.Addr 590 Metric uint32 591 } 592 593 func (routeData *RouteData) String() string { 594 return fmt.Sprintf("%+v", *routeData) 595 } 596 597 // IPAdapterDNSSuffix structure stores a DNS suffix in a linked list of DNS suffixes for a particular adapter. 598 // https://docs.microsoft.com/en-us/windows/desktop/api/iptypes/ns-iptypes-_ip_adapter_dns_suffix 599 type IPAdapterDNSSuffix struct { 600 Next *IPAdapterDNSSuffix 601 str [maxDNSSuffixStringLength]uint16 602 } 603 604 // String method returns the DNS suffix for this DNS suffix entry. 605 func (obj *IPAdapterDNSSuffix) String() string { 606 return windows.UTF16ToString(obj.str[:]) 607 } 608 609 // AdapterName method returns the name of the adapter with which these addresses are associated. 610 // Unlike an adapter's friendly name, the adapter name returned by AdapterName is permanent and cannot be modified by the user. 611 func (addr *IPAdapterAddresses) AdapterName() string { 612 return windows.BytePtrToString(addr.adapterName) 613 } 614 615 // DNSSuffix method returns adapter DNS suffix associated with this adapter. 616 func (addr *IPAdapterAddresses) DNSSuffix() string { 617 if addr.dnsSuffix == nil { 618 return "" 619 } 620 return windows.UTF16PtrToString(addr.dnsSuffix) 621 } 622 623 // Description method returns description for the adapter. 624 func (addr *IPAdapterAddresses) Description() string { 625 if addr.description == nil { 626 return "" 627 } 628 return windows.UTF16PtrToString(addr.description) 629 } 630 631 // FriendlyName method returns a user-friendly name for the adapter. For example: "Local Area Connection 1." 632 // This name appears in contexts such as the ipconfig command line program and the Connection folder. 633 func (addr *IPAdapterAddresses) FriendlyName() string { 634 if addr.friendlyName == nil { 635 return "" 636 } 637 return windows.UTF16PtrToString(addr.friendlyName) 638 } 639 640 // PhysicalAddress method returns the Media Access Control (MAC) address for the adapter. 641 // For example, on an Ethernet network this member would specify the Ethernet hardware address. 642 func (addr *IPAdapterAddresses) PhysicalAddress() []byte { 643 return addr.physicalAddress[:addr.physicalAddressLength] 644 } 645 646 // DHCPv6ClientDUID method returns the DHCP unique identifier (DUID) for the DHCPv6 client. 647 // This information is only applicable to an IPv6 adapter address configured using DHCPv6. 648 func (addr *IPAdapterAddresses) DHCPv6ClientDUID() []byte { 649 return addr.dhcpv6ClientDUID[:addr.dhcpv6ClientDUIDLength] 650 } 651 652 // Init method initializes the members of an MIB_IPINTERFACE_ROW entry with default values. 653 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-initializeipinterfaceentry 654 func (row *MibIPInterfaceRow) Init() { 655 initializeIPInterfaceEntry(row) 656 } 657 658 // get method retrieves IP information for the specified interface on the local computer. 659 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getipinterfaceentry 660 func (row *MibIPInterfaceRow) get() error { 661 if err := getIPInterfaceEntry(row); err != nil { 662 return err 663 } 664 665 // Patch that fixes SitePrefixLength issue 666 // https://stackoverflow.com/questions/54857292/setipinterfaceentry-returns-error-invalid-parameter?noredirect=1 667 switch row.Family { 668 case windows.AF_INET: 669 if row.SitePrefixLength > 32 { 670 row.SitePrefixLength = 0 671 } 672 case windows.AF_INET6: 673 if row.SitePrefixLength > 128 { 674 row.SitePrefixLength = 128 675 } 676 } 677 678 return nil 679 } 680 681 // Set method sets the properties of an IP interface on the local computer. 682 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-setipinterfaceentry 683 func (row *MibIPInterfaceRow) Set() error { 684 return setIPInterfaceEntry(row) 685 } 686 687 // get method returns all table rows as a Go slice. 688 func (tab *mibIPInterfaceTable) get() (s []MibIPInterfaceRow) { 689 return unsafe.Slice(&tab.table[0], tab.numEntries) 690 } 691 692 // free method frees the buffer allocated by the functions that return tables of network interfaces, addresses, and routes. 693 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-freemibtable 694 func (tab *mibIPInterfaceTable) free() { 695 freeMibTable(unsafe.Pointer(tab)) 696 } 697 698 // Alias method returns a string that contains the alias name of the network interface. 699 func (row *MibIfRow2) Alias() string { 700 return windows.UTF16ToString(row.alias[:]) 701 } 702 703 // Description method returns a string that contains a description of the network interface. 704 func (row *MibIfRow2) Description() string { 705 return windows.UTF16ToString(row.description[:]) 706 } 707 708 // PhysicalAddress method returns the physical hardware address of the adapter for this network interface. 709 func (row *MibIfRow2) PhysicalAddress() []byte { 710 return row.physicalAddress[:row.physicalAddressLength] 711 } 712 713 // PermanentPhysicalAddress method returns the permanent physical hardware address of the adapter for this network interface. 714 func (row *MibIfRow2) PermanentPhysicalAddress() []byte { 715 return row.permanentPhysicalAddress[:row.physicalAddressLength] 716 } 717 718 // get method retrieves information for the specified interface on the local computer. 719 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getifentry2 720 func (row *MibIfRow2) get() (ret error) { 721 return getIfEntry2(row) 722 } 723 724 // get method returns all table rows as a Go slice. 725 func (tab *mibIfTable2) get() (s []MibIfRow2) { 726 return unsafe.Slice(&tab.table[0], tab.numEntries) 727 } 728 729 // free method frees the buffer allocated by the functions that return tables of network interfaces, addresses, and routes. 730 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-freemibtable 731 func (tab *mibIfTable2) free() { 732 freeMibTable(unsafe.Pointer(tab)) 733 } 734 735 // RawSockaddrInet union contains an IPv4, an IPv6 address, or an address family. 736 // https://docs.microsoft.com/en-us/windows/desktop/api/ws2ipdef/ns-ws2ipdef-_sockaddr_inet 737 type RawSockaddrInet struct { 738 Family AddressFamily 739 data [26]byte 740 } 741 742 func ntohs(i uint16) uint16 { 743 return binary.BigEndian.Uint16((*[2]byte)(unsafe.Pointer(&i))[:]) 744 } 745 746 func htons(i uint16) uint16 { 747 b := make([]byte, 2) 748 binary.BigEndian.PutUint16(b, i) 749 return *(*uint16)(unsafe.Pointer(&b[0])) 750 } 751 752 // SetAddrPort method sets family, address, and port to the given IPv4 or IPv6 address and port. 753 // All other members of the structure are set to zero. 754 func (addr *RawSockaddrInet) SetAddrPort(addrPort netip.AddrPort) error { 755 if addrPort.Addr().Is4() { 756 addr4 := (*windows.RawSockaddrInet4)(unsafe.Pointer(addr)) 757 addr4.Family = windows.AF_INET 758 addr4.Addr = addrPort.Addr().As4() 759 addr4.Port = htons(addrPort.Port()) 760 for i := 0; i < 8; i++ { 761 addr4.Zero[i] = 0 762 } 763 return nil 764 } else if addrPort.Addr().Is6() { 765 addr6 := (*windows.RawSockaddrInet6)(unsafe.Pointer(addr)) 766 addr6.Family = windows.AF_INET6 767 addr6.Addr = addrPort.Addr().As16() 768 addr6.Port = htons(addrPort.Port()) 769 addr6.Flowinfo = 0 770 scopeId := uint32(0) 771 if z := addrPort.Addr().Zone(); z != "" { 772 if s, err := strconv.ParseUint(z, 10, 32); err == nil { 773 scopeId = uint32(s) 774 } 775 } 776 addr6.Scope_id = scopeId 777 return nil 778 } 779 return windows.ERROR_INVALID_PARAMETER 780 } 781 782 // SetAddr method sets family and address to the given IPv4 or IPv6 address. 783 // All other members of the structure are set to zero. 784 func (addr *RawSockaddrInet) SetAddr(netAddr netip.Addr) error { 785 return addr.SetAddrPort(netip.AddrPortFrom(netAddr, 0)) 786 } 787 788 // AddrPort returns the IP address and port. 789 func (addr *RawSockaddrInet) AddrPort() netip.AddrPort { 790 return netip.AddrPortFrom(addr.Addr(), addr.Port()) 791 } 792 793 // Addr returns IPv4 or IPv6 address, or an invalid address if the address is neither. 794 func (addr *RawSockaddrInet) Addr() netip.Addr { 795 switch addr.Family { 796 case windows.AF_INET: 797 return netip.AddrFrom4((*windows.RawSockaddrInet4)(unsafe.Pointer(addr)).Addr) 798 case windows.AF_INET6: 799 raw := (*windows.RawSockaddrInet6)(unsafe.Pointer(addr)) 800 a := netip.AddrFrom16(raw.Addr) 801 if raw.Scope_id != 0 { 802 a = a.WithZone(strconv.FormatUint(uint64(raw.Scope_id), 10)) 803 } 804 return a 805 } 806 return netip.Addr{} 807 } 808 809 // Port returns the port if the address if IPv4 or IPv6, or 0 if neither. 810 func (addr *RawSockaddrInet) Port() uint16 { 811 switch addr.Family { 812 case windows.AF_INET: 813 return ntohs((*windows.RawSockaddrInet4)(unsafe.Pointer(addr)).Port) 814 case windows.AF_INET6: 815 return ntohs((*windows.RawSockaddrInet6)(unsafe.Pointer(addr)).Port) 816 } 817 return 0 818 } 819 820 // Init method initializes a MibUnicastIPAddressRow structure with default values for a unicast IP address entry on the local computer. 821 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-initializeunicastipaddressentry 822 func (row *MibUnicastIPAddressRow) Init() { 823 initializeUnicastIPAddressEntry(row) 824 } 825 826 // get method retrieves information for an existing unicast IP address entry on the local computer. 827 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getunicastipaddressentry 828 func (row *MibUnicastIPAddressRow) get() error { 829 return getUnicastIPAddressEntry(row) 830 } 831 832 // Set method sets the properties of an existing unicast IP address entry on the local computer. 833 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-setunicastipaddressentry 834 func (row *MibUnicastIPAddressRow) Set() error { 835 return setUnicastIPAddressEntry(row) 836 } 837 838 // Create method adds a new unicast IP address entry on the local computer. 839 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-createunicastipaddressentry 840 func (row *MibUnicastIPAddressRow) Create() error { 841 return createUnicastIPAddressEntry(row) 842 } 843 844 // Delete method deletes an existing unicast IP address entry on the local computer. 845 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-deleteunicastipaddressentry 846 func (row *MibUnicastIPAddressRow) Delete() error { 847 return deleteUnicastIPAddressEntry(row) 848 } 849 850 // get method returns all table rows as a Go slice. 851 func (tab *mibUnicastIPAddressTable) get() (s []MibUnicastIPAddressRow) { 852 return unsafe.Slice(&tab.table[0], tab.numEntries) 853 } 854 855 // free method frees the buffer allocated by the functions that return tables of network interfaces, addresses, and routes. 856 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-freemibtable 857 func (tab *mibUnicastIPAddressTable) free() { 858 freeMibTable(unsafe.Pointer(tab)) 859 } 860 861 // get method retrieves information for an existing anycast IP address entry on the local computer. 862 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getanycastipaddressentry 863 func (row *MibAnycastIPAddressRow) get() error { 864 return getAnycastIPAddressEntry(row) 865 } 866 867 // Create method adds a new anycast IP address entry on the local computer. 868 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-createanycastipaddressentry 869 func (row *MibAnycastIPAddressRow) Create() error { 870 return createAnycastIPAddressEntry(row) 871 } 872 873 // Delete method deletes an existing anycast IP address entry on the local computer. 874 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-deleteanycastipaddressentry 875 func (row *MibAnycastIPAddressRow) Delete() error { 876 return deleteAnycastIPAddressEntry(row) 877 } 878 879 // get method returns all table rows as a Go slice. 880 func (tab *mibAnycastIPAddressTable) get() (s []MibAnycastIPAddressRow) { 881 return unsafe.Slice(&tab.table[0], tab.numEntries) 882 } 883 884 // free method frees the buffer allocated by the functions that return tables of network interfaces, addresses, and routes. 885 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-freemibtable 886 func (tab *mibAnycastIPAddressTable) free() { 887 freeMibTable(unsafe.Pointer(tab)) 888 } 889 890 // IPAddressPrefix structure stores an IP address prefix. 891 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/ns-netioapi-_ip_address_prefix 892 type IPAddressPrefix struct { 893 RawPrefix RawSockaddrInet 894 PrefixLength uint8 895 _ [2]byte 896 } 897 898 // SetPrefix method sets IP address prefix using netip.Prefix. 899 func (prefix *IPAddressPrefix) SetPrefix(netPrefix netip.Prefix) error { 900 err := prefix.RawPrefix.SetAddr(netPrefix.Addr()) 901 if err != nil { 902 return err 903 } 904 prefix.PrefixLength = uint8(netPrefix.Bits()) 905 return nil 906 } 907 908 // Prefix returns IP address prefix as netip.Prefix. 909 func (prefix *IPAddressPrefix) Prefix() netip.Prefix { 910 switch prefix.RawPrefix.Family { 911 case windows.AF_INET: 912 return netip.PrefixFrom(netip.AddrFrom4((*windows.RawSockaddrInet4)(unsafe.Pointer(&prefix.RawPrefix)).Addr), int(prefix.PrefixLength)) 913 case windows.AF_INET6: 914 return netip.PrefixFrom(netip.AddrFrom16((*windows.RawSockaddrInet6)(unsafe.Pointer(&prefix.RawPrefix)).Addr), int(prefix.PrefixLength)) 915 } 916 return netip.Prefix{} 917 } 918 919 // MibIPforwardRow2 structure stores information about an IP route entry. 920 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/ns-netioapi-_mib_ipforward_row2 921 type MibIPforwardRow2 struct { 922 InterfaceLUID LUID 923 InterfaceIndex uint32 924 DestinationPrefix IPAddressPrefix 925 NextHop RawSockaddrInet 926 SitePrefixLength uint8 927 ValidLifetime uint32 928 PreferredLifetime uint32 929 Metric uint32 930 Protocol RouteProtocol 931 Loopback bool 932 AutoconfigureAddress bool 933 Publish bool 934 Immortal bool 935 Age uint32 936 Origin RouteOrigin 937 } 938 939 // Init method initializes a MIB_IPFORWARD_ROW2 structure with default values for an IP route entry on the local computer. 940 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-initializeipforwardentry 941 func (row *MibIPforwardRow2) Init() { 942 initializeIPForwardEntry(row) 943 } 944 945 // get method retrieves information for an IP route entry on the local computer. 946 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getipforwardentry2 947 func (row *MibIPforwardRow2) get() error { 948 return getIPForwardEntry2(row) 949 } 950 951 // Set method sets the properties of an IP route entry on the local computer. 952 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-setipforwardentry2 953 func (row *MibIPforwardRow2) Set() error { 954 return setIPForwardEntry2(row) 955 } 956 957 // Create method creates a new IP route entry on the local computer. 958 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-createipforwardentry2 959 func (row *MibIPforwardRow2) Create() error { 960 return createIPForwardEntry2(row) 961 } 962 963 // Delete method deletes an IP route entry on the local computer. 964 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-deleteipforwardentry2 965 func (row *MibIPforwardRow2) Delete() error { 966 return deleteIPForwardEntry2(row) 967 } 968 969 // get method returns all table rows as a Go slice. 970 func (tab *mibIPforwardTable2) get() (s []MibIPforwardRow2) { 971 return unsafe.Slice(&tab.table[0], tab.numEntries) 972 } 973 974 // free method frees the buffer allocated by the functions that return tables of network interfaces, addresses, and routes. 975 // https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-freemibtable 976 func (tab *mibIPforwardTable2) free() { 977 freeMibTable(unsafe.Pointer(tab)) 978 } 979 980 // 981 // DNS API 982 // 983 984 // DnsInterfaceSettings is meant to be used with SetInterfaceDnsSettings 985 type DnsInterfaceSettings struct { 986 Version uint32 987 _ [4]byte 988 Flags uint64 989 Domain *uint16 990 NameServer *uint16 991 SearchList *uint16 992 RegistrationEnabled uint32 993 RegisterAdapterName uint32 994 EnableLLMNR uint32 995 QueryAdapterName uint32 996 ProfileNameServer *uint16 997 } 998 999 const ( 1000 DnsInterfaceSettingsVersion1 = 1 // for DnsInterfaceSettings 1001 DnsInterfaceSettingsVersion2 = 2 // for DnsInterfaceSettingsEx 1002 DnsInterfaceSettingsVersion3 = 3 // for DnsInterfaceSettings3 1003 1004 DnsInterfaceSettingsFlagIPv6 = 0x0001 1005 DnsInterfaceSettingsFlagNameserver = 0x0002 1006 DnsInterfaceSettingsFlagSearchList = 0x0004 1007 DnsInterfaceSettingsFlagRegistrationEnabled = 0x0008 1008 DnsInterfaceSettingsFlagRegisterAdapterName = 0x0010 1009 DnsInterfaceSettingsFlagDomain = 0x0020 1010 DnsInterfaceSettingsFlagHostname = 0x0040 1011 DnsInterfaceSettingsFlagEnableLLMNR = 0x0080 1012 DnsInterfaceSettingsFlagQueryAdapterName = 0x0100 1013 DnsInterfaceSettingsFlagProfileNameserver = 0x0200 1014 DnsInterfaceSettingsFlagDisableUnconstrainedQueries = 0x0400 // v2 only 1015 DnsInterfaceSettingsFlagSupplementalSearchList = 0x0800 // v2 only 1016 DnsInterfaceSettingsFlagDOH = 0x1000 // v3 only 1017 DnsInterfaceSettingsFlagDOHProfile = 0x2000 // v3 only 1018 )