Search code examples
c#linuxmonodbus

Accessing NetworkManager Connection Settings through dbus-sharp


I'm running Arch Linux with the following version of mcs:

>mcs --version
Mono C# compiler version 4.0.4.0

And the following version of dbus-sharp

>pacman -Ss dbus-sharp
extra/dbus-sharp 0.8.1-1 [installed] C# implementation of D-Bus
extra/dbus-sharp-glib 0.6.0-1 [installed] C# GLib implementation of D-Bus

This is my test program, based on the example code found here: https://gist.github.com/Ummon/4317268

I'm just trying to access the settings of the currently active connection, which should be returned as a 'Dict of {String, Dict of {String, Variant}}' as I've checked in the d-feet tool for the org.freedesktop.NetworkManager.Settings.Connection interface

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using DBus;

namespace NetworkManagerDictTest {
    public class MyTest {
        [Interface("org.freedesktop.NetworkManager.Settings.Connection")]
        public interface IConnection {
            IDictionary<string, IDictionary<string, object>> GetSettings();
        }
        readonly static string BUS_NAME = "org.freedesktop.NetworkManager";

        public static void Main(string[] argv) {
            org.freedesktop.DBus.Properties NetworkManagerProps = Bus.System.GetObject<org.freedesktop.DBus.Properties>(BUS_NAME, new ObjectPath("/org/freedesktop/NetworkManager"));
            ObjectPath[] activeConnections = NetworkManagerProps.Get(BUS_NAME, "ActiveConnections") as ObjectPath[];
            if (activeConnections.Length > 0) {
                org.freedesktop.DBus.Properties ActiveConnectionProperties = Bus.System.GetObject<org.freedesktop.DBus.Properties>(BUS_NAME, activeConnections[0]);
                ObjectPath ActiveConnectionPath = ActiveConnectionProperties.Get("org.freedesktop.NetworkManager.Connection.Active", "Connection") as ObjectPath;
                Console.WriteLine("Using connection path: " + ActiveConnectionPath);
                IConnection connection = Bus.System.GetObject<IConnection>(BUS_NAME, ActiveConnectionPath);
                Console.WriteLine("Connection Object ok");
                IDictionary<string, IDictionary<string, object>> settings = connection.GetSettings();
                Console.WriteLine(settings);
            }
        }
    }
}

Compilation went without errors nor warnings:

mcs Test.cs -r:/usr/lib/mono/dbus-sharp-2.0/dbus-sharp.dll -r:/usr/lib/mono/dbus-sharp-glib-2.0/dbus-sharp-glib.dll

However my program crashes during execution with the following output:

>mono Test.exe
Using connection path: /org/freedesktop/NetworkManager/Settings/0
Connection Object ok

Unhandled Exception:
DBus.Protocol.MessageReader+PaddingException: Read non-zero byte at position 28 while expecting padding. Value given: 200
  at DBus.Protocol.MessageReader.ReadPad (Int32 alignment) [0x00000] in <filename unknown>:0 
  at DBus.Protocol.MessageReader.ReadStruct (System.Type type) [0x00000] in <filename unknown>:0 
  at DBus.Protocol.MessageReader.ReadValue (System.Type type) [0x00000] in <filename unknown>:0 
  at DBus.Protocol.MessageReader.ReadDictionary[String,IDictionary`2] () [0x00000] in <filename unknown>:0 
  at NetworkManagerDictTest.MyTest+IConnectionProxy.GetSettings () [0x00000] in <filename unknown>:0 
  at NetworkManagerDictTest.MyTest.Main (System.String[] argv) [0x00000] in <filename unknown>:0 

[ERROR] FATAL UNHANDLED EXCEPTION: DBus.Protocol.MessageReader+PaddingException: Read non-zero byte at position 28 while expecting padding. Value given: 200
  at DBus.Protocol.MessageReader.ReadPad (Int32 alignment) [0x00000] in <filename unknown>:0 
  at DBus.Protocol.MessageReader.ReadStruct (System.Type type) [0x00000] in <filename unknown>:0 
  at DBus.Protocol.MessageReader.ReadValue (System.Type type) [0x00000] in <filename unknown>:0 
  at DBus.Protocol.MessageReader.ReadDictionary[String,IDictionary`2] () [0x00000] in <filename unknown>:0 
  at NetworkManagerDictTest.MyTest+IConnectionProxy.GetSettings () [0x00000] in <filename unknown>:0 
  at NetworkManagerDictTest.MyTest.Main (System.String[] argv) [0x00000] in <filename unknown>:0 

What can I do to work around this problem? Am I making a mistake when working with the DBus? It seems that all the method calls up to GetSettings went without issues. I've also encountered a similar problem while trying to fix a bug in another project where dbus-sharp would throw an exception when calling GetSettings. Could this be an issue of dbus-sharp instead?


Solution

  • Taking a look at the source code it seems that dbus-sharp infers the return type directly from the signature of the declared method. Unfortunately it doesn't check if I'm using a parent class or interface of Dictionary, eventually it tries to read a DBus struct because of a fallback case which causes the exception as DBus structs are 8 byte padded while dicts use 4 bytes

    Replacing all the IDictionary types with Dictionary worked fine and solved my problem.