Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
michaldobrodenka opened this issue Sep 21, 2020 · 11 comments · Fixed by #42878
Closed

Comments

@michaldobrodenka
Copy link

michaldobrodenka commented Sep 21, 2020

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!
@michaldobrodenka michaldobrodenka changed the title Regression - NET5.0 RC1 returns empty MAC address for network interface on Linux ARM32 Regression - NET5.0 RC1 returns empty MAC address for network interface on Linux Sep 22, 2020
@GrabYourPitchforks GrabYourPitchforks transferred this issue from dotnet/core Sep 29, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Sep 29, 2020
@ghost
Copy link

ghost commented Sep 29, 2020

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@danmoseley
Copy link
Member

@dotnet/ncl I reproduce on Ubuntu 20.04 WSL2 using 5.0 RC2 and 3.1:

dan@LAPTOP-P6UJDVTA:~/1$ dotnet run --framework netcoreapp3.1
I name:lo, desc: lo, op stats: Unknown, i net intrface type: Loopback
I name:bond0, desc: bond0, op stats: Down, i net intrface type: Ethernet
I name:dummy0, desc: dummy0, op stats: Down, i net intrface type: Ethernet
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:00155DE41BB1
False
Bytes:6
Hash code:-463625189
dan@LAPTOP-P6UJDVTA:~/1$ dotnet run --framework net5.0
I name:lo, desc: lo, op stats: Up, i net intrface type: Loopback
I name:bond0, desc: bond0, op stats: Down, i net intrface type: Ethernet
I name:dummy0, desc: dummy0, op stats: Down, i net intrface type: Ethernet
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

@danmoseley
Copy link
Member

Adding blocking tag, feel free to remove it if it turns out it's not.

@wfurt
Copy link
Member

wfurt commented Sep 29, 2020

It works for me with 5.0.100-rc.2.20479.15 (and daily build) on Ubuntu18

I name:lo, desc: lo, op stats: Up, i net intrface type: Loopback
I name:enp0s5, desc: enp0s5, op stats: Up, i net intrface type: Ethernet
I name:br-caaadc850f4a, desc: br-caaadc850f4a, op stats: Down, i net intrface type: Ethernet
I name:br-62a4f3fc9564, desc: br-62a4f3fc9564, op stats: Down, i net intrface type: Ethernet
I name:docker0, desc: docker0, op stats: Down, i net intrface type: Ethernet
MAC:010203040506
False
Bytes:6
Hash code:67306500

Can you post output from ifconfig -a @danmosemsft and @michaldobrodenka ?

#37546 looks like main suspect. cc: @tkp1n

@wfurt wfurt self-assigned this Sep 29, 2020
@wfurt
Copy link
Member

wfurt commented Sep 29, 2020

actually I was wrong. I missed the eth selection so the example code fabricated MAC for me. I will take a look.

@wfurt
Copy link
Member

wfurt commented Sep 30, 2020

I put up #42878 with fix. Broken since last fall ;(

@danmoseley
Copy link
Member

Reopening for 5.0

@danmoseley danmoseley reopened this Sep 30, 2020
@stephentoub stephentoub added this to the 5.0.0 milestone Sep 30, 2020
@stephentoub stephentoub removed the untriaged New issue has not been triaged by the area owner label Sep 30, 2020
@wfurt
Copy link
Member

wfurt commented Oct 1, 2020

5.0 fixed by #42882

@danmoseley
Copy link
Member

fixed and backported

@michaldobrodenka
Copy link
Author

Thank you guys!

@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants