简体   繁体   中英

Retrieving Subnet Mask Address From .NET HttpRequest class

Giving an object instance from the class System.Web.HttpRequest , say myRequest , then using the property System.Web.HttpRequest.UserHostAddress I can retrieve the IP address of the remote client:

string myIp = myRequest.UserHostAddress;
(...)

My need is to retrieve the IP address of the subnet mask of the remote client exclusively from the instance myRequest .

I know that the following is not possible but I would like to accomplish something similar:

string myMaskIp = myRequest.UserHostMaskAddress;
(...)

This because I can't check the local device network interfaces as I could with System.Net.NetworkInformation namespace, so my only available object to probe is the http request made from the remote client to the server.

Thank you very much for your help

我非常确定您无法通过简单地向远程主机发出请求来确定子网掩码-即使您是远程主机。

Oldyis is right for the general case, however it is possible to determine a classful subnetmask from the IPv4 address. Without any further information, determining an classless subnetmask is indeed impossible.

Keep in mind that the classful adressing scheme is virtually not used any more.

Here is an example from this project :

    /// <summary>
    /// Returns the classfull subnet mask of a given IPv4 network
    /// </summary>
    /// <param name="ipa">The network to get the classfull subnetmask for</param>
    /// <returns>The classfull subnet mask of a given IPv4 network</returns>
    public static Subnetmask GetClassfullSubnetMask(IPAddress ipa)
    {
        if (ipa.AddressFamily != AddressFamily.InterNetwork)
        {
            throw new ArgumentException("Only IPv4 addresses are supported for classfull address calculations.");
        }

        IPv4AddressClass ipv4Class = GetClass(ipa);
        Subnetmask sm = new Subnetmask();

        if (ipv4Class == IPv4AddressClass.A)
        {
            sm.MaskBytes[0] = 255;
        }
        if (ipv4Class == IPv4AddressClass.B)
        {
            sm.MaskBytes[0] = 255;
            sm.MaskBytes[1] = 255;
        }
        if (ipv4Class == IPv4AddressClass.C)
        {
            sm.MaskBytes[0] = 255;
            sm.MaskBytes[1] = 255;
            sm.MaskBytes[2] = 255;
        }
        return sm;
    }

(Think of the SubnetMask class as a byte array of length four)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM