-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPHelper.cs
103 lines (84 loc) · 3.67 KB
/
IPHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
public class IPHelper
{
/// This code derived from https://stackoverflow.com/a/4553625/4906348
/// and https://stackoverflow.com/a/39338188/4906348
public static List<IPAddressDetail> GetInterfaceIPAddress(bool debug = false)
{
var nics = NetworkInterface.GetAllNetworkInterfaces();
// Using struct for making data
var IPAddresses = new List<IPAddressDetail>();
foreach (var nic in nics) {
var ipProps = nic.GetIPProperties();
// We're only interested in IPv4 addresses for this example.
var ipv4Addrs = ipProps.UnicastAddresses
.Where(addr => addr.Address.AddressFamily == AddressFamily.InterNetwork)
.Where(addr => addr.Address.ToString() != "127.0.0.1") // Exclude localhost
.Where(addr => addr.Address.ToString().Substring(0,7) != "169.254" ); // Exclude DHCP IP, Windows based
foreach (var addr in ipv4Addrs) {
var network = CalculateNetwork(addr);
var broadcast = GetBroadcastAddress(addr);
if (network != null)
{
if (debug)
Console.WriteLine("Addr: {0} Mask: {1} Network: {2} Broadcast : {3}", addr.Address, addr.IPv4Mask, network, broadcast);
IPAddresses.Add(new IPAddressDetail(addr: addr.Address.ToString(), broadcast: broadcast.ToString(), mask: addr.IPv4Mask.ToString(), unicast: addr));
}
}
}
return IPAddresses;
}
public static IPAddress? CalculateNetwork(UnicastIPAddressInformation addr)
{
// The mask will be null in some scenarios, like a dhcp address 169.254.x.x
if (addr.IPv4Mask == null)
return null;
var ip = addr.Address.GetAddressBytes();
var mask = addr.IPv4Mask.GetAddressBytes();
var result = new Byte[4];
for (int i = 0; i < 4; ++i) {
result[i] = (Byte)(ip[i] & mask[i]);
}
return new IPAddress(result);
}
/// Only Unicast Address can have mask
/// @see https://stackoverflow.com/a/39338188/4906348
///
public static IPAddress GetBroadcastAddress(UnicastIPAddressInformation unicastAddress)
{
var address = unicastAddress.Address;
var mask = unicastAddress.IPv4Mask;
uint ipAddress = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
uint ipMaskV4 = BitConverter.ToUInt32(mask.GetAddressBytes(), 0);
uint broadCastIpAddress = ipAddress | ~ipMaskV4;
return new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
}
public static bool IsOneBroadcast(UnicastIPAddressInformation ownIP, IPAddress target)
{
bool status = false;
var ownBroadcast = GetBroadcastAddress(ownIP);
var mask = ownIP.IPv4Mask;
uint ipAddress = BitConverter.ToUInt32(target.GetAddressBytes(), 0);
uint ipMaskV4 = BitConverter.ToUInt32(mask.GetAddressBytes(), 0);
uint broadCastIpAddress = ipAddress | ~ipMaskV4;
var targetBroadcast = new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
if (targetBroadcast.Equals(ownBroadcast)) status = true;
return status;
}
}
public struct IPAddressDetail
{
public IPAddressDetail(string addr, string broadcast, string mask, UnicastIPAddressInformation unicast)
{
address = addr;
this.broadcast = broadcast;
this.mask = mask;
this.unicast = unicast;
}
public string address;
public string broadcast;
public string mask;
public UnicastIPAddressInformation unicast;
}