Get Subnet Mask : IP Address « Network « C# / C Sharp






Get Subnet Mask

        
/*

Copyright (c) 2010 Jean-Paul Mikkers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.IO;

namespace CodePlex.JPMikkers.DHCP
{
    public class Utils
    {

        public static IPAddress GetSubnetMask(IPAddress address)
        {
            foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses)
                {
                    if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        if (address.Equals(unicastIPAddressInformation.Address))
                        {
                            return unicastIPAddressInformation.IPv4Mask;
                        }
                    }
                }
            }
            throw new ArgumentException(string.Format("Can't find subnetmask for IP address '{0}'", address));
        }

    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.IPAddress AddressFamily
2.Parse an IPAddress
3.Loopback IPAddress
4.Broadcast IPAddress
5.Any IPAddress
6.None IPAddress
7.IsLoopback IPAddress
8.IP Address parse, lookup IP Address parse, lookup
9.IPEndPoint sampleIPEndPoint sample
10.Get Host By Name, Get Host Name
11.Get Host Entry
12.Parse Host String
13.Get Current Ip Address
14.Get Host IP Address
15.Get IP address by query whatismyip.com
16.Get Local IP Address
17.IP to value
18.Get user IP from HttpContext
19.IP to Uint
20.UInt32 To IP Address
21.IP Address To UInt 32
22.Get IP address from notation (xxx.xxx.xxx.xxx) or hostname
23.Gets my local IP address (not necessarily external) and subnet mask
24.Returns true if the IPEndPoint supplied is on the same subnet as this host
25.Is Ip Address By Regular Expression
26.Checks if a given string is a valid IP address.
27.IP Address To NumberIP Address To Number
28.Get the description of a Enum value.
29.Get Available Socket Port