Skip to content

Regression - NET5.0 RC1 returns empty MAC address for network interface on Linux #42870

@michaldobrodenka

Description

@michaldobrodenka

I've noticed that my app is returning empty Mac adress info when running on Linux. So I made a small snippet to test the code, Network interfaces are returned just fine, but MAC address from Network interface is empty. On net5.0 rc.1 & win-x64 it works normally as well as with Net Core 3.1.8 on Linux ARM32 machine.

using System;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace GetMyIP
{
    class Program
    {
        static void Main(string[] args)
        {
            var macAddress = GetMacAddress();
            Console.WriteLine("MAC:" + macAddress);
            Console.WriteLine("Is null: " + macAddress == null);
            Console.WriteLine("Bytes:" + macAddress.GetAddressBytes().Length);
            Console.WriteLine("Hash code:" + macAddress.GetHashCode());
        }


        /// <summary>
        /// Gets the MAC address of the current PC.
        /// </summary>
        /// <returns></returns>
        public static PhysicalAddress GetMacAddress()
        {
            var nic = GetNetworkInterface();
            if (nic != null)
            {
                Console.WriteLine($"Selected interface:{nic.Name}");

                return nic.GetPhysicalAddress();
            }

            return new PhysicalAddress(new byte[] { 1, 2, 3, 4, 5, 6 });
        }

        public static string GetMyIp()
        {
            var firstUpInterface = GetNetworkInterface();

            if (firstUpInterface != null)
            {
                var props = firstUpInterface.GetIPProperties();
                // get first IPV4 address assigned to this interface
                var firstIpV4Address = props.UnicastAddresses
                    .Where(c => c.Address.AddressFamily == AddressFamily.InterNetwork)
                    .Select(c => c.Address)
                    .FirstOrDefault();

                return firstIpV4Address.ToString();
            }

            return null;
        }

        private static NetworkInterface GetNetworkInterface()
        {
            // order interfaces by speed and filter out down and loopback
            // take first of the remaining
            var interfaces = NetworkInterface.GetAllNetworkInterfaces().ToList();

            foreach(var i in interfaces)
            {
                Console.WriteLine($"I name:{i.Name}, desc: {i.Description}, op stats: {i.OperationalStatus}, i net intrface type: {i.NetworkInterfaceType}");
            }

            if (interfaces == null || interfaces.Count == 0)
                return null;

            //.OrderByDescending(c => c.Speed)
            var firstUpInterface = interfaces.
                FirstOrDefault(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback
                    && c.OperationalStatus == OperationalStatus.Up
                    && c.Name.ToLower().Contains("eth")
                    && (!c.Description.ToLower().Contains("virtu"))
                    && (!c.Description.Contains("Pseudo")));

            return firstUpInterface;
        }
    }
}

Output on netcoreapp3.1:

xxx:/tmp/GetMyIp# dotnet ./GetMyIP.dll
I name:lo, desc: lo, op stats: Unknown, i net intrface type: Loopback
I name:eth0, desc: eth0, op stats: Up, i net intrface type: Ethernet
I name:sit0, desc: sit0, op stats: Down, i net intrface type: Unknown
Selected interface:eth0
MAC:70xxxxxxxx3E
False
Bytes:6
Hash code:634752358

Output on net5.0 rc.1:

xxx:/tmp/GetMyIp# dotnet GetMyIP.dll
I name:lo, desc: lo, op stats: Up, i net intrface type: Loopback
I name:eth0, desc: eth0, op stats: Up, i net intrface type: Ethernet
I name:sit0, desc: sit0, op stats: Down, i net intrface type: Unknown
Selected interface:eth0
MAC:
False
Bytes:0
Hash code:1

GetAddressBytes() returns empty array and hash code is "1"

Update: tested also on My PC under WSL with NET5 rc.1 and the result is empty:

I name:eth0, desc: eth0, op stats: Up, i net intrface type: Ethernet
I name:eth1, desc: eth1, op stats: Up, i net intrface type: Ethernet
I name:eth2, desc: eth2, op stats: Up, i net intrface type: Ethernet
I name:eth3, desc: eth3, op stats: Down, i net intrface type: Ethernet
I name:lo, desc: lo, op stats: Up, i net intrface type: Loopback
I name:eth4, desc: eth4, op stats: Down, i net intrface type: Ethernet
Selected interface:eth0
MAC:
False
Bytes:0
Hash code:1
Hello World!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions