IPAddressHelpers
Namespace: SnowBank.Networking · class
Helpers for working with IP addresses (or MAC addresses)
Methods
AddOffset
static IPAddress AddOffset(IPAddress address, int offset)
Adds an offset to an IP address
address— Base address (e.g. 192.168.1.23)offset— Offset (e.g. 42)
Returns: New address (e.g. 192.168.1.65)
DecodeIPRange
static void DecodeIPRange(string range, out IPAddress first, out IPAddress last, bool include0 = false)
Returns the bounds of an IP address range
range— IP range ("192.168.1.0/24", "192.168.1.0|255.255.255.0", "192.168.1.1-192.168.1.255", "192.168.1")first— Receives the first IP address of the rangelast— Receives the last IP address of the rangeinclude0— If true, includes "192.168.0.0" as a valid address
Throws an exception on error, in which case first and last are set to null
GetHostCountBetween
static long GetHostCountBetween(IPAddress from, IPAddress to, bool include0 = false, bool include255 = false)
Returns the number of addresses between (and including) two bounds
from— Start addressto— Destination addressinclude0— true if we want to include .0 addressesinclude255— true if we want to include .255 addresses
Returns: Count
GetPreferredAddress
static IPAddress GetPreferredAddress(IPAddress[] list)
Returns the first IPv4 in the list, or otherwise the first IPv6
list— List of candidate IP addresses
Returns: First IPv4 address found, or null if none (or only IPv6)
IPMatchRange
static bool IPMatchRange(string ip, string range)
Tests whether an IP address is part of a range. several formats are accepted. e.g. for "between 192.168.1.0 and 192.168.1.255") "192.168.1.*" "192.168.1.0-255" "192.168.1.0/255.255.255.0" "192.168.1.0/24"
ip— IP address to testrange— IP range
Returns: 'true' if the IP is within the range (bounds included)
IPToBroadcast
static string IPToBroadcast(string ip, string subnet)
Determines the broadcast IP address from an IP address and a subnet mask
ip— Host IP address (e.g. 192.168.1.156)subnet— Subnet mask (e.g. 255.255.255.0)
Returns: Corresponding broadcast IP address (192.168.1.255)
IPToMask
static long IPToMask(string ip)
Converts an IP address into a long (e.g. "255.255.255.0" -> 0xFFFFFF00)
ip— IP address
Returns: Corresponding binary mask
static long IPToMask(IPAddress ip)
Converts an IP address into a long (e.g. "255.255.255.0" -> 0xFFFFFF00)
ip— IP address
Returns: Corresponding binary mask
IsAny
static bool IsAny(IPAddress address)
Determines whether this is an "any" IP address (0.0.0.0 or '::')
IsPrivateRange
static bool IsPrivateRange(IPAddress address)
Test if the IP address is a Private Network (192.168., 10., ...) or not.
IsValidIP
static bool IsValidIP(string ip)
Indicates whether an IP address (v4/v6) is syntactically valid
ip— IPv4 address to check (e.g. "192.168.1.0")
Returns: True if the IP address is syntactically valid (4 numbers from 0 to 255)
static bool IsValidIP(ReadOnlySpan<char> ip)
Indicates whether an IP address (v4/v6) is syntactically valid
ip— IPv4 address to check (e.g. "192.168.1.0")
Returns: True if the IP address is syntactically valid (4 numbers from 0 to 255)
IsValidIPv4
static bool IsValidIPv4(string ip)
Determines whether this is a valid IPv4 address
ip— String to check
Returns: true if it is a valid IPv4, false in all other cases
static bool IsValidIPv4(ReadOnlySpan<char> ip)
Determines whether this is a valid IPv4 address
ip— String to check
Returns: true if it is a valid IPv4, false in all other cases
IsValidIPv6
static bool IsValidIPv6(string ip)
Determines whether this is a valid IPv6 address
ip— String to check
Returns: true if it is a valid IPv6, false in all other cases
static bool IsValidIPv6(ReadOnlySpan<char> ip)
Determines whether this is a valid IPv6 address
ip— String to check
Returns: true if it is a valid IPv6, false in all other cases
MACAddressToString
static string MACAddressToString(byte[] mac)
Converts a binary MAC address into its string representation
mac— Array of 6 bytes containing a MAC address
Returns: "00-11-22-33-44-55"
static string MACAddressToString(Slice mac)
Converts a binary MAC address into its string representation
mac— Buffer of 6 bytes containing a MAC address
Returns: "00-11-22-33-44-55"
static string MACAddressToString(ReadOnlySpan<byte> mac)
Converts a binary MAC address into its string representation
Returns: "00-11-22-33-44-55"
static string MACAddressToString(byte[] mac, int offset, int count)
Converts a binary MAC address into its string representation
Returns: "00-11-22-33-44-55"
PingAsync
static Task<PingReply> PingAsync(IPAddress addr, TimeSpan timeout, byte[] buffer, PingOptions options, CancellationToken ct)
Send a ping request to the specified target
This implementation supports cancellation
StringToMACAddress
static byte[] StringToMACAddress(string mac)
SubnetToCidr
static IPAddress SubnetToCidr(IPAddress address, IPAddress subnet)
ToSortableAddress
static string ToSortableAddress(IPAddress address)
Converts an IP address into a lexicographically sortable version
ToSortableAddress("172.16.1.1") => "172.016.001.001"
TracerouteAsync
static Task<TracerouteReply> TracerouteAsync(IPAddress address, int maxDistance, TimeSpan timeout, CancellationToken ct)
Perform a parallel traceroute from the current host to the specified target
address— Target addressmaxDistance— Maximum number of hops to scan (must be greater than 0)timeout— Maximum delay when waiting for ICMP repliesct— Cancellation token
Returns: List of hops needed to reach the target
TryGetLocalAddressForRemoteAddress
static bool TryGetLocalAddressForRemoteAddress(IPAddress remoteAddress, out IPAddress localAddress)