From 394a1ac2cd9e791193d41f5f50d14a456efab399 Mon Sep 17 00:00:00 2001 From: Kamil Krawczyk Date: Thu, 19 Jul 2018 09:52:30 +0200 Subject: [PATCH] Get configuration returned by the IPv4 DHCP server. --- DHCP4Config.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ Device.go | 21 +++++++++++++++++++++ utils.go | 8 ++++++++ 3 files changed, 77 insertions(+) create mode 100644 DHCP4Config.go diff --git a/DHCP4Config.go b/DHCP4Config.go new file mode 100644 index 0000000..3b3a1c4 --- /dev/null +++ b/DHCP4Config.go @@ -0,0 +1,48 @@ +package gonetworkmanager + +import ( + "encoding/json" + + "github.com/godbus/dbus" +) + +const ( + DHCP4ConfigInterface = NetworkManagerInterface + ".DHCP4Config" + + DHCP4ConfigPropertyOptions = DHCP4ConfigInterface + ".Options" +) + +type DHCP4Options map[string]interface{} + +type DHCP4Config interface { + // GetOptions gets options map of configuration returned by the IPv4 DHCP server. + GetOptions() DHCP4Options + + MarshalJSON() ([]byte, error) +} + +func NewDHCP4Config(objectPath dbus.ObjectPath) (DHCP4Config, error) { + var c dhcp4Config + return &c, c.init(NetworkManagerInterface, objectPath) +} + +type dhcp4Config struct { + dbusBase +} + +func (c *dhcp4Config) GetOptions() DHCP4Options { + options := c.getMapStringVariantProperty(DHCP4ConfigPropertyOptions) + rv := make(DHCP4Options) + + for k, v := range options { + rv[k] = v.Value() + } + + return rv +} + +func (c *dhcp4Config) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]interface{}{ + "Options": c.GetOptions(), + }) +} diff --git a/Device.go b/Device.go index 8956922..9b1e1e1 100644 --- a/Device.go +++ b/Device.go @@ -14,6 +14,7 @@ const ( DevicePropertyIP4Config = DeviceInterface + ".Ip4Config" DevicePropertyDeviceType = DeviceInterface + ".DeviceType" DevicePropertyAvailableConnections = DeviceInterface + ".AvailableConnections" + DevicePropertyDhcp4Config = DeviceInterface + ".Dhcp4Config" ) func DeviceFactory(objectPath dbus.ObjectPath) (Device, error) { @@ -43,6 +44,11 @@ type Device interface { // state. GetIP4Config() IP4Config + // GetDHCP4Config gets the Dhcp4Config object describing the configuration of the + // device. Only valid when the device is in the NM_DEVICE_STATE_ACTIVATED + // state. + GetDHCP4Config() DHCP4Config + // GetDeviceType gets the general type of the network device; ie Ethernet, // WiFi, etc. GetDeviceType() NmDeviceType @@ -85,6 +91,20 @@ func (d *device) GetIP4Config() IP4Config { return cfg } +func (d *device) GetDHCP4Config() DHCP4Config { + path := d.getObjectProperty(DevicePropertyDhcp4Config) + if path == "/" { + return nil + } + + cfg, err := NewDHCP4Config(path) + if err != nil { + panic(err) + } + + return cfg +} + func (d *device) GetDeviceType() NmDeviceType { return NmDeviceType(d.getUint32Property(DevicePropertyDeviceType)) } @@ -109,6 +129,7 @@ func (d *device) marshalMap() map[string]interface{} { "Interface": d.GetInterface(), "State": d.GetState().String(), "IP4Config": d.GetIP4Config(), + "DHCP4Config": d.GetDHCP4Config(), "DeviceType": d.GetDeviceType().String(), "AvailableConnections": d.GetAvailableConnections(), } diff --git a/utils.go b/utils.go index b754260..785a86a 100644 --- a/utils.go +++ b/utils.go @@ -92,6 +92,14 @@ func (d *dbusBase) getSliceStringProperty(iface string) []string { return value } +func (d *dbusBase) getMapStringVariantProperty(iface string) map[string]dbus.Variant { + value, ok := d.getProperty(iface).(map[string]dbus.Variant) + if !ok { + panic(makeErrVariantType(iface)) + } + return value +} + func (d *dbusBase) getUint8Property(iface string) uint8 { value, ok := d.getProperty(iface).(uint8) if !ok {