Implemented logging into separate file for debugging
[mtetherd] / net.c
diff --git a/net.c b/net.c
index c1946d9..43d49f9 100644 (file)
--- a/net.c
+++ b/net.c
 #include <string.h>
 #include <netinet/in.h>
 #include "net.h"
+#include "plugin.h"
 
 #define MTETHERD_DEVICE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_MTETHERD_DEVICE, MTetherDDevicePrivate))
 
 G_DEFINE_TYPE(MTetherDDevice, mtetherd_device, G_TYPE_OBJECT);
 
+static const guint MAX_DEVICES = 64;
 // Host order 192.168.255.0
 static const in_addr_t HOST_BASE_ADDRESS = 0xc0a8ff00;
 // Host order 255.255.255.252
@@ -75,7 +77,7 @@ static void mtetherd_device_init(MTetherDDevice *self) {
 }
 
 static void mtetherd_device_set_interface(MTetherDDevice *self, const gchar *interface) {
-       if (self) {
+       if (self && self->priv) {
                if (self->priv->interface) {
                        g_free(self->priv->interface);
                }
@@ -95,7 +97,7 @@ static void mtetherd_device_set_interface(MTetherDDevice *self, const gchar *int
 }
 
 static void mtetherd_device_set_udi(MTetherDDevice *self, const gchar *udi) {
-       if (self) {
+       if (self && self->priv) {
                if (self->priv->udi) {
                        g_free(self->priv->udi);
                }
@@ -114,7 +116,7 @@ static void mtetherd_device_set_udi(MTetherDDevice *self, const gchar *udi) {
        }
 }
 
-static void mtetherd_device_set_index(MTetherDDevice *self, guint index) {
+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) {
@@ -126,40 +128,152 @@ static void mtetherd_device_set_index(MTetherDDevice *self, guint index) {
                        self->priv->dhcp_start = htonl(start);
                        self->priv->dhcp_end = htonl(addr);
                } else {
-                       g_warning("Invalid subnet index: %u (0..63 expected)", index);
+                       g_log(MTETHERD_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Invalid subnet index: %u (0..%u expected)", index, MAX_DEVICES - 1);
                }
        }
 }
 
-MTetherDDevice *mtetherd_device_new(const gchar *interface, const gchar *udi, guint index) {
+MTetherDDevice *mtetherd_device_new(const gchar *interface, const gchar *udi) {
        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;
-       
-       if (self && udi) {
-               if (g_strcmp0(self->priv->udi, udi) == 0) {
-                       return 0;
+const gchar *mtetherd_device_get_interface(MTetherDDevice *self) {
+       if (self && self->priv) {
+               return self->priv->interface;
+       }
+       return NULL;
+}
+
+gchar *mtetherd_device_get_addr(MTetherDDevice *self) {
+       if (self && self->priv) {
+               guchar a = self->priv->addr & 0xff;
+               guchar b = (self->priv->addr >> 8) & 0xff;
+               guchar c = (self->priv->addr >> 16) & 0xff;
+               guchar d = (self->priv->addr >> 24) & 0xff;
+               return g_strdup_printf("%u.%u.%u.%u", a, b, c, d);
+       }
+       return NULL;
+}
+
+gchar *mtetherd_device_get_netmask(MTetherDDevice *self) {
+       if (self && self->priv) {
+               guchar a = self->priv->netmask & 0xff;
+               guchar b = (self->priv->netmask >> 8) & 0xff;
+               guchar c = (self->priv->netmask >> 16) & 0xff;
+               guchar d = (self->priv->netmask >> 24) & 0xff;
+               return g_strdup_printf("%u.%u.%u.%u", a, b, c, d);
+       }
+       return NULL;
+}
+
+gchar *mtetherd_device_get_dhcp_start(MTetherDDevice *self) {
+       if (self && self->priv) {
+               guchar a = self->priv->dhcp_start & 0xff;
+               guchar b = (self->priv->dhcp_start >> 8) & 0xff;
+               guchar c = (self->priv->dhcp_start >> 16) & 0xff;
+               guchar d = (self->priv->dhcp_start >> 24) & 0xff;
+               return g_strdup_printf("%u.%u.%u.%u", a, b, c, d);
+       }
+       return NULL;
+}
+
+gchar *mtetherd_device_get_dhcp_end(MTetherDDevice *self) {
+       if (self && self->priv) {
+               guchar a = self->priv->dhcp_end & 0xff;
+               guchar b = (self->priv->dhcp_end >> 8) & 0xff;
+               guchar c = (self->priv->dhcp_end >> 16) & 0xff;
+               guchar d = (self->priv->dhcp_end >> 24) & 0xff;
+               return g_strdup_printf("%u.%u.%u.%u", a, b, c, d);
+       }
+       return NULL;
+}
+
+static gint mtetherd_device_list_find_index(gpointer *array, const gchar *udi) {
+       guint i;
+       for (i = 0; i < MAX_DEVICES; i++) {
+               MTetherDDevice *device = MTETHERD_DEVICE(array[i]);
+               if (device && device->priv) {
+                       if (g_strcmp0(device->priv->udi, udi) == 0) {
+                               return i;
+                       }
                }
        }
        return -1;
 }
 
-MTetherDDevice *mtetherd_device_find(GList *list, const gchar *udi) {
-       GList *entry = g_list_find_custom(list, udi, mtetherd_device_find_udi);
-       if (entry) {
-               return MTETHERD_DEVICE(entry->data);
+gpointer mtetherd_device_list_new() {
+       return g_malloc0(sizeof(gpointer) * MAX_DEVICES);
+}
+
+void mtetherd_device_list_free(gpointer list) {
+       if (list) {
+               gpointer *array = (gpointer *) list;
+               
+               guint i;
+               for (i = 0; i < MAX_DEVICES; i++) {
+                       if (array[i]) {
+                               g_object_unref(G_OBJECT(array[i]));
+                       }
+               }
+               g_free(array);
+       }
+}
+
+MTetherDDevice *mtetherd_device_list_find(gpointer list, const gchar *udi) {
+       if (list) {
+               gpointer *array = (gpointer *) list;
+       
+               gint index = mtetherd_device_list_find_index(array, udi);
+               if (index >= 0) {
+                       return array[index];
+               }
        }
        return NULL;
 }
 
+gboolean mtetherd_device_list_add(gpointer list, MTetherDDevice *device) {
+       if (list) {
+               gpointer *array = (gpointer *) list;
+               
+               guint i;
+               for (i = 0; i < MAX_DEVICES; i++) {
+                       if (!array[i]) {
+                               array[i] = (gpointer) device;
+                               return TRUE;
+                       }
+               }
+       }
+       return FALSE;
+}
+
+gboolean mtetherd_device_list_remove(gpointer list, const gchar *udi) {
+       if (list) {
+               gpointer *array = (gpointer *) list;
+               
+               gint index = mtetherd_device_list_find_index(array, udi);
+               if (index >= 0) {
+                       g_object_unref(G_OBJECT(array[index]));
+                       array[index] = NULL;
+                       return TRUE;
+               }
+       }
+       return FALSE;
+}
+
+gboolean mtetherd_device_ok(const gchar *interface) {
+       if (strncmp("usb", interface, sizeof("usb")) == 0) {
+               return TRUE;
+       }
+       if (strncmp("bnep", interface, sizeof("bnep")) == 0) {
+               return TRUE;
+       }
+       return FALSE;
+}
+