pub struct IphlpNetworkAdapterInfo { /* private fields */ }
Expand description
Represents information about a network adapter.
This struct provides an easy-to-use and safe interface for working with network adapter information, such as IP addresses, DNS server addresses, gateway addresses, and more.
Implementations§
Source§impl IphlpNetworkAdapterInfo
impl IphlpNetworkAdapterInfo
Sourcepub fn add_unicast_address_ipv4(
&self,
address: Ipv4Addr,
prefix_length: u8,
) -> Option<MIB_UNICASTIPADDRESS_ROW>
pub fn add_unicast_address_ipv4( &self, address: Ipv4Addr, prefix_length: u8, ) -> Option<MIB_UNICASTIPADDRESS_ROW>
Adds an IPv4 unicast address to the network interface.
This function creates a new MIB_UNICASTIPADDRESS_ROW
with the provided IPv4 address
and prefix length, and then calls the CreateUnicastIpAddressEntry
Windows API function
to add the unicast address to the network interface. On success, it returns
Some(MIB_UNICASTIPADDRESS_ROW)
; otherwise, it sets the last error and returns None
.
§Arguments
address
- The IPv4 address to assign to the network interface.prefix_length
- The subnet prefix length for the IPv4 address.
§Returns
Option<MIB_UNICASTIPADDRESS_ROW>
- ReturnsSome(MIB_UNICASTIPADDRESS_ROW)
if the unicast address is successfully added to the network interface, orNone
if the operation fails.
§Safety
This function uses unsafe Windows API calls (InitializeUnicastIpAddressEntry
and
CreateUnicastIpAddressEntry
). The function takes care of initializing the
MIB_UNICASTIPADDRESS_ROW
struct and ensuring that the arguments and struct fields
are properly set before making the API calls. However, any changes to this function or
the underlying Windows APIs could introduce potential safety issues. Ensure that you
understand the risks and consequences of using unsafe code before modifying this
function or its dependencies.
Sourcepub fn add_unicast_address_ipv6(
&self,
address: Ipv6Addr,
prefix_length: u8,
) -> Option<MIB_UNICASTIPADDRESS_ROW>
pub fn add_unicast_address_ipv6( &self, address: Ipv6Addr, prefix_length: u8, ) -> Option<MIB_UNICASTIPADDRESS_ROW>
Adds an IPv6 unicast address to the network interface.
This function creates a new MIB_UNICASTIPADDRESS_ROW
with the provided IPv6 address
and prefix length, and then calls the CreateUnicastIpAddressEntry
Windows API function
to add the unicast address to the network interface. On success, it returns
Some(MIB_UNICASTIPADDRESS_ROW)
; otherwise, it sets the last error and returns None
.
§Arguments
address
- The IPv6 address to assign to the network interface.prefix_length
- The subnet prefix length for the IPv6 address.
§Returns
Option<MIB_UNICASTIPADDRESS_ROW>
- ReturnsSome(MIB_UNICASTIPADDRESS_ROW)
if the unicast address is successfully added to the network interface, orNone
if the operation fails.
§Safety
This function uses unsafe Windows API calls (InitializeUnicastIpAddressEntry
and
CreateUnicastIpAddressEntry
). The function takes care of initializing the
MIB_UNICASTIPADDRESS_ROW
struct and ensuring that the arguments and struct fields
are properly set before making the API calls. However, any changes to this function or
the underlying Windows APIs could introduce potential safety issues. Ensure that you
understand the risks and consequences of using unsafe code before modifying this
function or its dependencies.
Sourcepub fn delete_unicast_address(address: &MIB_UNICASTIPADDRESS_ROW) -> bool
pub fn delete_unicast_address(address: &MIB_UNICASTIPADDRESS_ROW) -> bool
Removes a unicast IP address from the network adapter using a reference to a MIB_UNICASTIPADDRESS_ROW
.
This function calls the DeleteUnicastIpAddressEntry
Windows API function to remove the
unicast IP address from the network adapter. On success, it returns true
; otherwise,
it sets the last error and returns false
.
§Arguments
address
- A reference to aMIB_UNICASTIPADDRESS_ROW
.
§Returns
bool
- Returnstrue
if the unicast IP address is successfully removed from the network adapter, orfalse
if the operation fails.
§Safety
This function contains unsafe code as it calls the DeleteUnicastIpAddressEntry
Windows API function, which is an unsafe operation due to the use of raw pointers.
The caller must ensure that the provided address
reference is valid and correctly
initialized to avoid any undefined behavior.
Sourcepub fn reset_unicast_addresses(&self) -> bool
pub fn reset_unicast_addresses(&self) -> bool
Removes all unicast addresses associated with the network interface.
§Returns
bool
: true if successful, false otherwise.
§Safety
This function uses unsafe Windows API calls to get and delete unicast IP addresses. The caller should ensure that the network interface is properly configured before calling this function.
Sourcepub fn delete_unicast_address_ipv4(&self, address: Ipv4Addr) -> bool
pub fn delete_unicast_address_ipv4(&self, address: Ipv4Addr) -> bool
Removes the specified IPv4 address from the network interface.
§Arguments
address
: Ipv4Addr to remove
§Returns
bool
: true if successful, false otherwise.
§Safety
This function uses unsafe Windows API calls to get and delete unicast IP address entries. The caller should ensure that the provided IPv4 address is valid and that the network interface is properly configured before calling this function.
Sourcepub fn delete_unicast_address_ipv6(&self, address: Ipv6Addr) -> bool
pub fn delete_unicast_address_ipv6(&self, address: Ipv6Addr) -> bool
Removes the specified IPv6 address from the network interface.
§Arguments
address
: Ipv6Addr to remove
§Returns
bool
: true if successful, false otherwise.
§Safety
This function uses unsafe Windows API calls to get and delete unicast IP address entries. The caller should ensure that the provided IPv6 address is valid and that the network interface is properly configured before calling this function.
Source§impl IphlpNetworkAdapterInfo
impl IphlpNetworkAdapterInfo
Sourcepub fn add_default_gateway_ipv4(
&self,
address: Ipv4Addr,
) -> Option<MIB_IPFORWARD_ROW2>
pub fn add_default_gateway_ipv4( &self, address: Ipv4Addr, ) -> Option<MIB_IPFORWARD_ROW2>
Adds a default IPv4 gateway to the network interface.
§Arguments
address
: Default gateway IPv4 address
§Returns
Option<MIB_IPFORWARD_ROW2>
: Some(MIB_IPFORWARD_ROW2) on success, None on failure
§Safety
This function calls unsafe Windows API functions (InitializeIpForwardEntry
, SetLastError
, and
CreateIpForwardEntry2
). Make sure to handle errors and exceptions properly when using this function.
Sourcepub fn add_default_gateway_ipv6(
&self,
address: Ipv6Addr,
) -> Option<MIB_IPFORWARD_ROW2>
pub fn add_default_gateway_ipv6( &self, address: Ipv6Addr, ) -> Option<MIB_IPFORWARD_ROW2>
Adds a default IPv6 gateway to the network interface.
§Arguments
address
: Default gateway IPv6 address
§Returns
Option<MIB_IPFORWARD_ROW2>
: Some(MIB_IPFORWARD_ROW2) on success, None on failure
§Safety
This function uses FFI to interact with the Windows API, specifically the InitializeIpForwardEntry
,
CreateIpForwardEntry2
, and SetLastError
functions. It is the caller’s responsibility to ensure that
the library containing these functions is properly loaded and the FFI definitions are correct.
Sourcepub fn assign_default_gateway_ipv4(
&self,
metric: u32,
) -> Option<MIB_IPFORWARD_ROW2>
pub fn assign_default_gateway_ipv4( &self, metric: u32, ) -> Option<MIB_IPFORWARD_ROW2>
Configures the network interface as a default IPv4 gateway with the specified metric.
§Arguments
metric
: Network metric (priority), defaults to 0
§Returns
Option<MIB_IPFORWARD_ROW2>
: Some(MIB_IPFORWARD_ROW2) on success, None on failure
§Safety
This function uses unsafe Windows API calls to initialize and create IP forward entries. The caller should ensure that the provided metric is a valid network metric and that the network interface is properly configured before calling this function.
Sourcepub fn assign_default_gateway_ipv6(
&self,
metric: u32,
) -> Option<MIB_IPFORWARD_ROW2>
pub fn assign_default_gateway_ipv6( &self, metric: u32, ) -> Option<MIB_IPFORWARD_ROW2>
Configures the network interface as a default IPv6 gateway with the specified metric.
§Arguments
metric
: Network metric (priority), defaults to 0
§Returns
Option<MIB_IPFORWARD_ROW2>
: Some(MIB_IPFORWARD_ROW2) on success, None on failure
§Safety
This function uses unsafe Windows API calls to initialize and create IP forward entries. The caller should ensure that the provided metric is a valid network metric and that the network interface is properly configured before calling this function.
Source§impl IphlpNetworkAdapterInfo
impl IphlpNetworkAdapterInfo
Sourcepub fn ipv6_if_index(&self) -> u32
pub fn ipv6_if_index(&self) -> u32
Sourcepub fn adapter_name(&self) -> &String
pub fn adapter_name(&self) -> &String
Sourcepub fn true_adapter_name(&self) -> &GuidWrapper
pub fn true_adapter_name(&self) -> &GuidWrapper
Sourcepub fn description(&self) -> &String
pub fn description(&self) -> &String
Sourcepub fn friendly_name(&self) -> &String
pub fn friendly_name(&self) -> &String
Sourcepub fn unicast_address_list(&self) -> &Vec<IpAddr>
pub fn unicast_address_list(&self) -> &Vec<IpAddr>
Sourcepub fn dns_server_address_list(&self) -> &Vec<IpAddr>
pub fn dns_server_address_list(&self) -> &Vec<IpAddr>
Sourcepub fn gateway_address_list(&self) -> &Vec<IpGatewayInfo>
pub fn gateway_address_list(&self) -> &Vec<IpGatewayInfo>
Sourcepub fn physical_address(&self) -> &MacAddress
pub fn physical_address(&self) -> &MacAddress
Sourcepub fn transmit_link_speed(&self) -> u64
pub fn transmit_link_speed(&self) -> u64
Sourcepub fn receive_link_speed(&self) -> u64
pub fn receive_link_speed(&self) -> u64
Sourcepub fn media_type(&self) -> &NDIS_MEDIUM
pub fn media_type(&self) -> &NDIS_MEDIUM
Sourcepub fn physical_medium_type(&self) -> &NDIS_PHYSICAL_MEDIUM
pub fn physical_medium_type(&self) -> &NDIS_PHYSICAL_MEDIUM
Sourcepub fn true_medium_type(&self) -> &NDIS_PHYSICAL_MEDIUM
pub fn true_medium_type(&self) -> &NDIS_PHYSICAL_MEDIUM
Sourcepub fn ndis_wan_ip_link(&self) -> &MacAddress
pub fn ndis_wan_ip_link(&self) -> &MacAddress
Sourcepub fn ndis_wan_ipv6_link(&self) -> &MacAddress
pub fn ndis_wan_ipv6_link(&self) -> &MacAddress
Source§impl IphlpNetworkAdapterInfo
impl IphlpNetworkAdapterInfo
Sourcepub fn add_ndp_entry_ipv4(
&self,
address: Ipv4Addr,
hw_address: [u8; 6],
) -> Option<MIB_IPNET_ROW2>
pub fn add_ndp_entry_ipv4( &self, address: Ipv4Addr, hw_address: [u8; 6], ) -> Option<MIB_IPNET_ROW2>
Adds an IPv4 NDP entry for the network interface.
§Arguments
address
: Ipv4Addrhw_address
: [u8; 6] hardware address
§Returns
Option<MIB_IPNET_ROW2>
: Some with a MIB_IPNET_ROW2 if successful, None otherwise.
§Safety
This function uses unsafe Windows API calls to create an IP Net entry. The caller should ensure that the provided IPv4 address and hardware address are valid and that the network interface is properly configured before calling this function.
Sourcepub fn add_ndp_entry_ipv6(
&self,
address: Ipv6Addr,
hw_address: [u8; 6],
) -> Option<MIB_IPNET_ROW2>
pub fn add_ndp_entry_ipv6( &self, address: Ipv6Addr, hw_address: [u8; 6], ) -> Option<MIB_IPNET_ROW2>
Adds an IPv6 NDP entry for the network interface.
§Arguments
address
: Ipv6Addrhw_address
: [u8; 6] hardware address
§Returns
Option<MIB_IPNET_ROW2>
: Some with a MIB_IPNET_ROW2 if successful, None otherwise.
§Safety
This function uses unsafe Windows API calls to create an IP Net entry. The caller should ensure that the provided IPv6 address and hardware address are valid and that the network interface is properly configured before calling this function.
Sourcepub fn delete_ndp_entry(address: &MIB_IPNET_ROW2) -> bool
pub fn delete_ndp_entry(address: &MIB_IPNET_ROW2) -> bool
Removes an NDP entry by a MIB_IPNET_ROW2 reference.
§Arguments
address
: A reference to MIB_IPNET_ROW2
§Returns
bool
:true
if successful,false
otherwise.
§Safety
This function uses unsafe Windows API calls to delete an IP Net entry. The caller should ensure that the provided MIB_IPNET_ROW2 reference is valid and points to an existing NDP entry before calling this function.
Source§impl IphlpNetworkAdapterInfo
impl IphlpNetworkAdapterInfo
Sourcepub fn add_routes_ipv4(
&self,
ips: Vec<IpNetwork>,
) -> VecDeque<MIB_IPFORWARD_ROW2>
pub fn add_routes_ipv4( &self, ips: Vec<IpNetwork>, ) -> VecDeque<MIB_IPFORWARD_ROW2>
Configures IPv4 network interface routes (Wireguard AllowedIps parameter).
§Arguments
ips
: Vec of IpNetwork (Ipv4Network or Ipv6Network) subnets to configure
§Returns
VecDeque<MIB_IPFORWARD_ROW2>
: VecDeque of MIB_IPFORWARD_ROW2 on success
§Safety
This function uses unsafe Windows API calls to initialize and create IP forward entries. The caller should ensure that the provided IPv4/IPv6 subnets are valid and that the network interface is properly configured before calling this function.
Sourcepub fn add_routes_ipv6(
&self,
ips: Vec<IpNetwork>,
) -> VecDeque<MIB_IPFORWARD_ROW2>
pub fn add_routes_ipv6( &self, ips: Vec<IpNetwork>, ) -> VecDeque<MIB_IPFORWARD_ROW2>
Configures IPv6 network interface routes (Wireguard AllowedIps parameter).
§Arguments
ips
: Vec of IpNetwork (Ipv4Network or Ipv6Network) subnets to configure
§Returns
VecDeque<MIB_IPFORWARD_ROW2>
: VecDeque of MIB_IPFORWARD_ROW2 on success
§Safety
This function uses unsafe Windows API calls to initialize and create IP forward entries. The caller should ensure that the provided IPv4/IPv6 subnets are valid and that the network interface is properly configured before calling this function.
Sourcepub fn delete_route(address: &MIB_IPFORWARD_ROW2) -> bool
pub fn delete_route(address: &MIB_IPFORWARD_ROW2) -> bool
Deletes routing table entry by MIB_IPFORWARD_ROW2 reference.
§Arguments
address
: Reference to MIB_IPFORWARD_ROW2
§Returns
bool
:true
if successful,false
otherwise
§Safety
This function uses an unsafe Windows API call to delete IP forward entries. The caller should ensure that the provided MIB_IPFORWARD_ROW2 reference is valid before calling this function.
Sourcepub fn delete_routes(addresses: &mut [MIB_IPFORWARD_ROW2]) -> bool
pub fn delete_routes(addresses: &mut [MIB_IPFORWARD_ROW2]) -> bool
Deletes routing table entries by MIB_IPFORWARD_ROW2 references.
§Arguments
addresses
: Vec of MIB_IPFORWARD_ROW2 references
§Returns
bool
:true
if successful,false
otherwise
§Safety
This function uses an unsafe Windows API call to delete IP forward entries. The caller should ensure that the provided MIB_IPFORWARD_ROW2 reference is valid before calling this function.
Sourcepub fn reset_adapter_routes(&self) -> bool
pub fn reset_adapter_routes(&self) -> bool
Removes all routing table entries associated with the network interface.
§Returns
bool
:true
if successful,false
otherwise
§Safety
This function uses unsafe Windows API calls to get the IP forward table, delete IP forward entries, and free the MIB table. The caller should ensure that the network interface is properly configured before calling this function.
Source§impl IphlpNetworkAdapterInfo
impl IphlpNetworkAdapterInfo
Sourcepub fn get_external_network_connections() -> Vec<IphlpNetworkAdapterInfo>
pub fn get_external_network_connections() -> Vec<IphlpNetworkAdapterInfo>
Returns a list of network interfaces which:
- Have at least one unicast address assigned
- Operational (IfOperStatusUp)
- Not software loopback
§Returns
Vec<IphlpNetworkAdapterInfo>
: A vector ofIphlpNetworkAdapterInfo
objects.
§Safety
This function uses unsafe Windows API calls to get the network interface information.
The caller should ensure that the returned IphlpNetworkAdapterInfo
objects are not used
after the corresponding network interfaces have been removed or disabled.
Sourcepub fn get_connection_by_luid(luid: IfLuid) -> Option<IphlpNetworkAdapterInfo>
pub fn get_connection_by_luid(luid: IfLuid) -> Option<IphlpNetworkAdapterInfo>
Finds a network interface by the provided LUID.
§Arguments
luid
- AnIfLuid
to look up.
§Returns
Option<IphlpNetworkAdapterInfo>
- An optionalIphlpNetworkAdapterInfo
class instance.
§Safety
This function uses unsafe Windows API calls to get the network interface information.
The caller should ensure that the returned IphlpNetworkAdapterInfo
objects are not used
after the corresponding network interfaces have been removed or disabled.
Sourcepub fn get_connection_by_hw_address(
address: &MacAddress,
) -> Option<IphlpNetworkAdapterInfo>
pub fn get_connection_by_hw_address( address: &MacAddress, ) -> Option<IphlpNetworkAdapterInfo>
Finds network interface by provided hardware address.
§Arguments
address
- AMacAddress
to lookup.
§Returns
Option<IphlpNetworkAdapterInfo>
- An optionalIphlpNetworkAdapterInfo
object.
§Safety
This function uses unsafe Windows API calls to get the network interface information.
The caller should ensure that the returned IphlpNetworkAdapterInfo
objects are not used
after the corresponding network interfaces have been removed or disabled.
Sourcepub fn get_connection_by_guid(guid: &str) -> Option<IphlpNetworkAdapterInfo>
pub fn get_connection_by_guid(guid: &str) -> Option<IphlpNetworkAdapterInfo>
Finds network interface by provided GUID.
§Arguments
guid
- A&str
containing the GUID to lookup.
§Returns
Option<IphlpNetworkAdapterInfo>
- An optionalIphlpNetworkAdapterInfo
object.
§Safety
This function uses unsafe Windows API calls to get the network interface information.
The caller should ensure that the returned IphlpNetworkAdapterInfo
objects are not used
after the corresponding network interfaces have been removed or disabled.
Source§impl IphlpNetworkAdapterInfo
impl IphlpNetworkAdapterInfo
Sourcepub unsafe fn new(
address: *const IP_ADAPTER_ADDRESSES_LH,
if_row: *const MIB_IF_ROW2,
) -> Self
pub unsafe fn new( address: *const IP_ADAPTER_ADDRESSES_LH, if_row: *const MIB_IF_ROW2, ) -> Self
Constructs a new IphlpNetworkAdapterInfo
instance from raw pointers to IP_ADAPTER_ADDRESSES_LH
and MIB_IF_ROW2
structures.
This method is marked as unsafe
because it dereferences raw pointers, which can lead
to undefined behavior if not used correctly.
§Safety
The caller must ensure that address
and if_row
are valid pointers to
IP_ADAPTER_ADDRESSES_LH
and MIB_IF_ROW2
structures, respectively, and that the
structures remain valid for the duration of the method call.
§Arguments
address
- A pointer to anIP_ADAPTER_ADDRESSES_LH
structure containing information about the network adapter.if_row
- A pointer to aMIB_IF_ROW2
structure containing information about the network interface.
§Examples
use my_crate::IphlpNetworkAdapterInfo;
use my_crate::{IP_ADAPTER_ADDRESSES_LH, MIB_IF_ROW2};
unsafe {
let address = /* ... */;
let if_row = /* ... */;
let network_adapter_info = IphlpNetworkAdapterInfo::new(address, if_row);
}
Sourcepub fn set_friendly_name(
&mut self,
friendly_name: impl Into<String>,
) -> Result<()>
pub fn set_friendly_name( &mut self, friendly_name: impl Into<String>, ) -> Result<()>
Sets the friendly name of the network adapter.
This function sets the friendly name of the network adapter by updating the Windows registry.
The registry key for the adapter is located at:
SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{adapter_name}\Connection
.
§Arguments
friendly_name
- A string or any type that can be converted into aString
representing the friendly name of the adapter.
§Returns
Result<()>
- ReturnsOk(())
if the friendly name is set successfully. Returns anErr
containing the error code if there is a failure.
Sourcepub fn is_same_address_info(
&self,
rhs: &IphlpNetworkAdapterInfo,
check_gateway: bool,
) -> bool
pub fn is_same_address_info( &self, rhs: &IphlpNetworkAdapterInfo, check_gateway: bool, ) -> bool
Checks if IP address information in the provided network_adapter_info is different from the current one.
§Arguments
rhs
: IphlpNetworkAdapterInfo to compare tocheck_gateway
: If true, also checks the gateway information
§Returns
bool
: true if provided IphlpNetworkAdapterInfo contains the same IP addresses, false otherwise
Sourcepub fn reset_adapter(&self) -> bool
pub fn reset_adapter(&self) -> bool
Trait Implementations§
Source§impl Clone for IphlpNetworkAdapterInfo
impl Clone for IphlpNetworkAdapterInfo
Source§fn clone(&self) -> IphlpNetworkAdapterInfo
fn clone(&self) -> IphlpNetworkAdapterInfo
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more