This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 48016 - System.Net.NetworkInformation.DnsAddresses is always empty. Fix included.
Summary: System.Net.NetworkInformation.DnsAddresses is always empty. Fix included.
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries (show other bugs)
Version: 7.1 (C9)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2016-11-26 11:40 UTC by softlion
Modified: 2017-01-12 22:58 UTC (History)
2 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description softlion 2016-11-26 11:40:11 UTC
// Decompiled with JetBrains decompiler
// Type: System.Net.NetworkInformation.UnixIPInterfaceProperties
// Assembly: System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
// MVID: 9D8F5F10-3A5E-4A27-A4FD-F703CE37CAE4
// Assembly location: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.dll

This buggy method always returns an empty collection for any interface.

  public override IPAddressCollection DnsAddresses
    {
      get
      {
        this.ParseResolvConf();
        return this.dns_servers;
      }
    }

This is because resolv.conf is not available on android systems. Instead you have to get the system properties: net.dns1, net.dns2 ... net.dns8

        public static IEnumerable<IPAddress> GetDnsServers()
        {
            var dnsEntries = new List<string>();
            try
            {
                var systemPropertiesClass = Java.Lang.Class.ForName("android.os.SystemProperties");
                var getMethod = systemPropertiesClass.GetMethod("get", Java.Lang.Class.FromType(typeof(Java.Lang.String)));
                for (var i = 1; i < 8; i++)
                {
                    var value = ((Java.Lang.String)getMethod.Invoke(null, $"net.dns{i}"))?.ToString();
                    if (!String.IsNullOrEmpty(value) && !dnsEntries.Contains(value))
                        dnsEntries.Add(value);
                }
            }
            catch (Java.Lang.Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }

            return dnsEntries.Select(entry => IPAddress.Parse(entry)).ToList();
        }
Comment 2 softlion 2016-11-26 11:56:42 UTC
Well i can't as it is not Xamarin Android.
Comment 3 Marek Habersack 2016-11-28 17:35:03 UTC
@softlion you can PR against mono/master, the code will eventually end up in XA - we use the standard Mono BCL. However, since you need to use Java classes, the code should live in Xamarin.Android and, preferably, should be done in C to directly use Java JNI without involving managed code (saves memory and time). See for instance here https://github.com/xamarin/xamarin-android/blob/master/src/monodroid/jni/monodroid-networkinfo.c#L113 and here https://github.com/mono/mono/blob/master/mcs/class/System/System.Net.NetworkInformation/NetworkInterface.cs#L634

Note how the C code caches information to save time.

You would need, therefore, two PRs - first for XA and after that one is accepted, another one for Mono (we don't want to introduce changes to Mono that will cause runtime failure because the XA part is missing)
Comment 4 Marek Habersack 2017-01-11 10:45:17 UTC
A PR for Xamarin.Android is opened https://github.com/xamarin/xamarin-android/pull/360
Comment 5 Marek Habersack 2017-01-12 22:58:15 UTC
Fixed in:

   mono/master, commit bb9981e61c009589fb16aeb9287cbb17f26db533
   mono/mono-4.8.0-branch, commit 28d9c96aec01023da0857d44b21e6032f7a2b81f
   xamarin-android/master, commit 5d6d4126513f8ce83ad09d394ad43ea36c94b5c3
   monodroid/master, commit 32da15e6ef8617bde182e92b6de42deaeb7e824c

Note You need to log in before you can comment on or make changes to this bug.