Skip to content

Instantly share code, notes, and snippets.

@lhysrc
Last active March 4, 2016 01:54
Show Gist options
  • Select an option

  • Save lhysrc/8c118cd83fd9e3fa1c72 to your computer and use it in GitHub Desktop.

Select an option

Save lhysrc/8c118cd83fd9e3fa1c72 to your computer and use it in GitHub Desktop.

Revisions

  1. lhysrc revised this gist Mar 4, 2016. No changes.
  2. lhysrc revised this gist Oct 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion IPAddressExtensions
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ using System.Linq;
    using System.Text;
    using System.Net;

    namespace Knom.Helpers.Net
    namespace Helpers
    {
    public static class SubnetMask
    {
  3. lhysrc created this gist Feb 10, 2015.
    100 changes: 100 additions & 0 deletions IPAddressExtensions
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;

    namespace Knom.Helpers.Net
    {
    public static class SubnetMask
    {
    public static readonly IPAddress ClassA = IPAddress.Parse("255.0.0.0");
    public static readonly IPAddress ClassB = IPAddress.Parse("255.255.0.0");
    public static readonly IPAddress ClassC = IPAddress.Parse("255.255.255.0");

    public static IPAddress CreateByHostBitLength(int hostpartLength)
    {
    int hostPartLength = hostpartLength;
    int netPartLength = 32 - hostPartLength;

    if (netPartLength < 2)
    throw new ArgumentException("Number of hosts is to large for IPv4");

    Byte[] binaryMask = new byte[4];

    for (int i = 0; i < 4; i++)
    {
    if (i * 8 + 8 <= netPartLength)
    binaryMask[i] = (byte)255;
    else if (i * 8 > netPartLength)
    binaryMask[i] = (byte)0;
    else
    {
    int oneLength = netPartLength - i * 8;
    string binaryDigit =
    String.Empty.PadLeft(oneLength, '1').PadRight(8, '0');
    binaryMask[i] = Convert.ToByte(binaryDigit, 2);
    }
    }
    return new IPAddress(binaryMask);
    }

    public static IPAddress CreateByNetBitLength(int netpartLength)
    {
    int hostPartLength = 32 - netpartLength;
    return CreateByHostBitLength(hostPartLength);
    }

    public static IPAddress CreateByHostNumber(int numberOfHosts)
    {
    int maxNumber = numberOfHosts + 1;

    string b = Convert.ToString(maxNumber, 2);

    return CreateByHostBitLength(b.Length);
    }
    }

    public static class IPAddressExtensions
    {
    public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask)
    {
    byte[] ipAdressBytes = address.GetAddressBytes();
    byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

    if (ipAdressBytes.Length != subnetMaskBytes.Length)
    throw new ArgumentException("Lengths of IP address and subnet mask do not match.");

    byte[] broadcastAddress = new byte[ipAdressBytes.Length];
    for (int i = 0; i < broadcastAddress.Length; i++)
    {
    broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255));
    }
    return new IPAddress(broadcastAddress);
    }

    public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask)
    {
    byte[] ipAdressBytes = address.GetAddressBytes();
    byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

    if (ipAdressBytes.Length != subnetMaskBytes.Length)
    throw new ArgumentException("Lengths of IP address and subnet mask do not match.");

    byte[] broadcastAddress = new byte[ipAdressBytes.Length];
    for (int i = 0; i < broadcastAddress.Length; i++)
    {
    broadcastAddress[i] = (byte)(ipAdressBytes[i] & (subnetMaskBytes[i]));
    }
    return new IPAddress(broadcastAddress);
    }

    public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
    {
    IPAddress network1 = address.GetNetworkAddress(subnetMask);
    IPAddress network2 = address2.GetNetworkAddress(subnetMask);

    return network1.Equals(network2);
    }
    }
    }