Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:56:19+00:00 2026-05-26T22:56:19+00:00

In my application (admin web interface written in MVC3) running on open-embedded Linux I

  • 0

In my application (admin web interface written in MVC3) running on open-embedded Linux I have to list all the TCP/IP settings. This includes IP-Adresse, Gateway and the subnet mask.

The following code runs well under MS .Net but Mono 2.10 throws a NotImplemntedException for the "IPv4Mask" property:

var ipProperties = networkIntf.GetIPProperties(); 
var unicastIpInfo = ipProperties.UnicastAddresses.FirstOrDefault(); 
var subnetMask = unicastAddress != null ? unicastAddress.IPv4Mask.ToString() : ""; 

Does anybody know how one can get the IPv4 subnet mask using Mono?

I found this question was asked already in 2009 but didn’t find any answer to it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:56:19+00:00Added an answer on May 26, 2026 at 10:56 pm

    I took a look at some Mono sourcecode and extracted some code-snippets to build a helper that returns a the IPv4 subnet mask of the given network interface. The code is not an absolute beauty but it works.

    [StructLayout(LayoutKind.Explicit)]
    struct ifa_ifu
    {
        [FieldOffset(0)]
        public IntPtr ifu_broadaddr;
    
        [FieldOffset(0)]
        public IntPtr ifu_dstaddr;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct ifaddrs
    {
        public IntPtr ifa_next;
        public string ifa_name;
        public uint ifa_flags;
        public IntPtr ifa_addr;
        public IntPtr ifa_netmask;
        public ifa_ifu ifa_ifu;
        public IntPtr ifa_data;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct sockaddr_in
    {
        public ushort sin_family;
        public ushort sin_port;
        public uint sin_addr;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct sockaddr_in6
    {
        public ushort sin6_family;   /* AF_INET6 */
        public ushort sin6_port;     /* Transport layer port # */
        public uint sin6_flowinfo; /* IPv6 flow information */
        public in6_addr sin6_addr;     /* IPv6 address */
        public uint sin6_scope_id; /* scope id (new in RFC2553) */
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct in6_addr
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] u6_addr8;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct sockaddr_ll
    {
        public ushort sll_family;
        public ushort sll_protocol;
        public int sll_ifindex;
        public ushort sll_hatype;
        public byte sll_pkttype;
        public byte sll_halen;
    
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] sll_addr;
    }
    
    internal class IPInfoTools
    {
        const int AF_INET = 2;
        const int AF_INET6 = 10;
        const int AF_PACKET = 17;
    
        [DllImport("libc")]
        static extern int getifaddrs (out IntPtr ifap);
    
        [DllImport ("libc")]
        static extern void freeifaddrs (IntPtr ifap);
    
        internal static string GetIPv4Mask(string networkInterfaceName)
        {
            IntPtr ifap;
            if (getifaddrs(out ifap) != 0)
            {
                throw new SystemException("getifaddrs() failed");
            }
    
            try
            {
                var next = ifap;
                while (next != IntPtr.Zero)
                {
                    var addr = (ifaddrs)Marshal.PtrToStructure(next, typeof(ifaddrs));
                    var name = addr.ifa_name;
    
                    if (addr.ifa_addr != IntPtr.Zero)
                    {
                        var sockaddr = (sockaddr_in)Marshal.PtrToStructure(addr.ifa_addr, typeof(sockaddr_in));
                        switch (sockaddr.sin_family)
                        {
                            case AF_INET6:
                                //sockaddr_in6 sockaddr6 = (sockaddr_in6)Marshal.PtrToStructure(addr.ifa_addr, typeof(sockaddr_in6));
                                break;
                            case AF_INET:
                                if (name == networkInterfaceName)
                                {
                                    var netmask = (sockaddr_in)Marshal.PtrToStructure(addr.ifa_netmask, typeof(sockaddr_in));
                                    var ipAddr = new IPAddress(netmask.sin_addr);  // IPAddress to format into default string notation
                                    return ipAddr.ToString();
                                }
                                break;
                            case AF_PACKET:
                                {
                                    var sockaddrll = (sockaddr_ll)Marshal.PtrToStructure(addr.ifa_addr, typeof(sockaddr_ll));
                                    if (sockaddrll.sll_halen > sockaddrll.sll_addr.Length)
                                    {
                                        Console.Error.WriteLine("Got a bad hardware address length for an AF_PACKET {0} {1}",
                                                                sockaddrll.sll_halen, sockaddrll.sll_addr.Length);
                                        next = addr.ifa_next;
                                        continue;
                                    }
                                }
                                break;
                        }
                    }
    
                    next = addr.ifa_next;
                }
            }
            finally
            {
                freeifaddrs(ifap);
            }
    
            return null;
        }
    }
    

    Usage of the above helper is like this:

    String subnetMask = IPInfoTools.GetIPv4Mask("etc0");
    

    I didn’t yet manage to fix this in the Mono sourcecode as one needs to change quite some files in Mono to get the above information from the place where it is queried (LinuxNetworkInterface) to the place where it is used (LinuxUnicastIPAddressInfo). But I will post my code to the Mono bug-report so maybe one of the Mono developers can take a look.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the Django admin interface for a very simple web application
Ok, strange setup, strange question. We've got a Client and an Admin web application
Hello I need to have multiple language support of my django admin application.I can
Hi Sitepoint wizard people, Say we have an admin application that has multiple users
I have a web application that I'll be distributing to customers. I'll give them
I would like to create web application with admin/checkout sections being secured. Assuming I
I'm coding an admin panel with a web interface that will only be run
I have the requirement to build a simplier admin interface for a website. I
We have a web application that manages inventory for our computer support group. One
I am developing a Java application that is to have two web interfaces: a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.