private Tuple<HostName, HostName> GetStartEndIP()
{
Windows.Networking.HostName hostname = GetMyIP();
if (hostname != null)
{
string[] ipsplit;
byte[] ipBytes;
int bits;
uint mask;
byte[] maskBytes;
byte[] startIPBytes;
byte[] endIPBytes;
try
{
ipsplit = hostname.DisplayName.Split(".".ToCharArray(), 4);
ipBytes = new byte[] { Convert.ToByte(ipsplit[0]), Convert.ToByte(ipsplit[1]), Convert.ToByte(ipsplit[2]), Convert.ToByte(ipsplit[3]) };
bits = Convert.ToInt32(hostname.IPInformation.PrefixLength.ToString());
mask = ~(uint.MaxValue >> bits);
maskBytes = BitConverter.GetBytes(mask).Reverse().ToArray();
startIPBytes = new byte[ipBytes.Length];
endIPBytes = new byte[ipBytes.Length];
for (int i = 0; i < ipBytes.Length; i++)
{
startIPBytes[i] = (byte)(ipBytes[i] & maskBytes[i]);
endIPBytes[i] = (byte)(ipBytes[i] | ~maskBytes[i]);
}
}
catch
{
return null;
}
//System.Net.IPAddress ip = new System.Net.IPAddress(new byte[] { 192, 168, 0, 1 });
// Convert the IP address to bytes.
//byte[] ipBytes = ip.GetAddressBytes();
// BitConverter gives bytes in opposite order to GetAddressBytes().
// Calculate the bytes of the start and end IP addresses.
// Convert the bytes to IP addresses.
//IPAddress startIP = new IPAddress(startIPBytes);
//IPAddress endIP = new IPAddress(endIPBytes);
if (startIPBytes.Length > 3 && endIPBytes.Length > 3)
{
HostName startIP = new HostName(startIPBytes[0] + "." + startIPBytes[1] + "." + startIPBytes[2] + "." + startIPBytes[3]);
HostName endIP = new HostName(endIPBytes[0] + "." + endIPBytes[1] + "." + endIPBytes[2] + "." + endIPBytes[3]);
return new Tuple<HostName, HostName>(startIP, endIP);
}
}
return null;
}