+ if (self->priv) {
+ self->priv->interface = NULL;
+ self->priv->udi = NULL;
+ self->priv->addr = 0;
+ self->priv->netmask = 0;
+ self->priv->dhcp_start = 0;
+ self->priv->dhcp_end = 0;
+ }
+}
+
+static void mtetherd_device_set_interface(MTetherDDevice *self, const gchar *interface) {
+ if (self) {
+ if (self->priv->interface) {
+ g_free(self->priv->interface);
+ }
+ if (interface) {
+ size_t len = strlen(interface);
+ self->priv->interface = g_malloc(len + 1);
+ size_t i, j;
+ for (i = 0, j = 0; i < len; i++) {
+ if (g_ascii_isalnum(interface[i])) {
+ self->priv->interface[j] = interface[i];
+ j++;
+ }
+ }
+ self->priv->interface[j] = '\0';
+ }
+ }
+}
+
+static void mtetherd_device_set_udi(MTetherDDevice *self, const gchar *udi) {
+ if (self) {
+ if (self->priv->udi) {
+ g_free(self->priv->udi);
+ }
+ if (udi) {
+ size_t len = strlen(udi);
+ self->priv->udi = g_malloc(len + 1);
+ size_t i, j;
+ for (i = 0, j = 0; i < len; i++) {
+ if (g_ascii_isalnum(udi[i]) || g_ascii_ispunct(udi[i])) {
+ self->priv->udi[j] = udi[i];
+ j++;
+ }
+ }
+ self->priv->udi[j] = '\0';
+ }
+ }
+}
+
+static void mtetherd_device_set_index(MTetherDDevice *self, guint index) {
+ if (self && self->priv) {
+ // Maximum is 63, we need four addresses per subnet
+ if (index < 64) {
+ in_addr_t subnet = HOST_BASE_ADDRESS | (index << 2);
+ in_addr_t addr = subnet | HOST_ROUTER;
+ in_addr_t start = subnet | HOST_DHCP;
+ self->priv->addr = htonl(addr);
+ self->priv->netmask = htonl(HOST_NETMASK);
+ self->priv->dhcp_start = htonl(start);
+ self->priv->dhcp_end = htonl(addr);
+ } else {
+ g_warning("Invalid subnet index: %u (0..63 expected)", index);
+ }
+ }
+}
+
+MTetherDDevice *mtetherd_device_new(const gchar *interface, const gchar *udi, guint index) {
+ MTetherDDevice *self = MTETHERD_DEVICE(g_object_new(TYPE_MTETHERD_DEVICE, NULL));
+
+ if (self && self->priv) {
+ mtetherd_device_set_interface(self, interface);
+ mtetherd_device_set_udi(self, udi);
+ mtetherd_device_set_index(self, index);
+ }
+
+ return self;
+}
+
+static gint mtetherd_device_find_udi(gconstpointer a, gconstpointer b) {
+ const MTetherDDevice *self = MTETHERD_DEVICE(a);
+ const gchar *udi = (const gchar *) b;