Added readme
authorGregor Riepl <onitake@gmail.com>
Thu, 22 Jul 2010 00:01:24 +0000 (02:01 +0200)
committerGregor Riepl <onitake@gmail.com>
Thu, 22 Jul 2010 00:01:24 +0000 (02:01 +0200)
Moved device list and address validation into separate file

Makefile
README [new file with mode: 0644]
device.c [new file with mode: 0644]
device.h [new file with mode: 0644]
main.c

index d60af75..5e7494f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,19 +1,21 @@
 MAD = mad
 CC = gcc
-CFLAGS = -Wall -O2 -D_GNU_SOURCE
+CFLAGS = -Wall -O0 -g -D_GNU_SOURCE
 INCLUDES = -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include
+LDFLAGS = -g
 LIBS = -ldbus-1 -lpthread
 SSH_ADDRESS = developer@192.168.253.254
 SSH_SCP = scp
 SSH_SSH = ssh
 SSH_PATH = /home/developer
+DATA_FILES = main.c device.c device.h
 
-maemo-tethering: main.o
-       $(MAD) $(CC) $(LDLFAGS) $(LIBS) -o $@ $^
+maemo-tethering: main.o device.o
+       $(MAD) $(CC) $(LDFLAGS) $(LIBS) -o $@ $^
 
 %PHONY: copy run clean
 
-copy: maemo-tethering
+copy: maemo-tethering $(DATA_FILES)
        $(SSH_SCP) $^ $(SSH_ADDRESS):$(SSH_PATH)
 
 run: copy
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..613db3f
--- /dev/null
+++ b/README
@@ -0,0 +1,64 @@
+maemo-tethering 1.0
+-------------------
+
+Description:
+
+maemo-tethering allows you to use your N900 as an Internet router.
+Once a USB network or Bluetooth PAN connection is opened to the device,
+it will automatically configure networking and start a dnsmasq instance
+that serves DHCP requests and forwards DNS queries.
+This tool can also be used to automatically configure network interfaces
+for development.
+
+Usage:
+
+Bluetooth:
+1. Pair your host with the N900 and make sure the PAN (Bluetooth networking)
+   service is recognized.
+2. Open a PAN connection to the N900.
+3. Routing should setup automatically and you can access the Internet
+   right away.
+
+USB:
+1. Connect host and N900 with a USB cable.
+2. Wait until the USB mode dialog pops up on the N900 and dismiss it.
+3. Open MAD Developer.
+4. Tap "Manage USB".
+5. Tap "Load g_ether".
+6. The USB mode is kept between cable disconnects, you only have to
+   reload the g_ether module after a reboot or mode change.
+7. Start surfing on the host.
+3a.It is also possible to load the module from the Terminal:
+rmmod g_nokia
+rmmod g_file_storage
+modprobe g_ether
+
+Notes:
+
+IP addresses are currently hardcoded into the program. A later version
+will add support for a configuration file.
+The assigned addresses are the following:
+USB: N900: 192.168.253.254 DHCP: 192.168.253.1-192.168.253.253
+Bluetooth: N900: 192.168.254.254 DHCP: 192.168.254.1-192.168.254.253
+
+If you connect while no GPRS/UMTS connection is active, forwarding will
+not be enabled. This is the case when the device is connected to a
+Wireless LAN, for example.
+You can still communicate with the device, but not surf the internet
+from your host. Just disconnect and reconnect again when the GPRS/UMTS
+connection is available.
+
+Legal:
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
diff --git a/device.c b/device.c
new file mode 100644 (file)
index 0000000..9dbe424
--- /dev/null
+++ b/device.c
@@ -0,0 +1,201 @@
+/*
+maemo-tethering
+(c) 2010 Gregor Riepl <onitake@gmail.com>
+
+Tethering utility for Maemo
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <arpa/inet.h>
+#include "device.h"
+
+Device *device_new(const char *name) {
+       if (!name) {
+               fprintf(stderr, "No device name given.\n");
+               return NULL;
+       }
+       Device *ret = malloc(sizeof(Device));
+       if (!ret) {
+               fprintf(stderr, "Error allocating memory for device structure.\n");
+               return NULL;
+       }
+       memset(ret, 0, sizeof(Device));
+       size_t length = strlen(name) + 1;
+       ret->name = malloc(length);
+       if (!ret->name) {
+               fprintf(stderr, "Error allocating memory for device name.\n");
+               free(ret);
+               return NULL;
+       }
+       memcpy(ret->name, name, length);
+       return ret;
+}
+
+Device *device_delete(Device *device) {
+       if (!device) {
+               fprintf(stderr, "Error: trying to deallocate NULL device structure.\n");
+               return NULL;
+       }
+       Device *ret = device->next;
+       free(device->name);
+       free(device->address);
+       free(device->startaddress);
+       free(device->endaddress);
+       if (device->previous) {
+               device->previous->next = device->next;
+       }
+       if (device->next) {
+               device->next->previous = device->previous;
+       }
+       free(device);
+       return ret;
+}
+
+Device *device_delete_all(Device *device) {
+       Device *ret = device->previous;
+       while (device) {
+               device = device_delete(device);
+       }
+       return ret;
+}
+
+Device *device_append(Device *previous, Device *next) {
+       if (previous) {
+               if (previous->next) {
+                       previous->next->previous = next;
+                       next->next = previous->next;
+               }
+               previous->next = next;
+               next->previous = previous;
+       }
+       return next;
+}
+
+Device *device_set_name(Device *device, const char *name) {
+       if (!device) {
+               fprintf(stderr, "Error: trying to set name of NULL device structure.\n");
+               return NULL;
+       }
+       free(device->name);
+       size_t length = strlen(name) + 1;
+       device->name = malloc(length);
+       if (!device->name) {
+               fprintf(stderr, "Error allocating memory for device name.\n");
+       }
+       return device;
+}
+
+Device *device_set_address(Device *device, const char *address) {
+       if (!device) {
+               fprintf(stderr, "Error: trying to set address of NULL device structure.\n");
+               return NULL;
+       }
+       free(device->address);
+       struct in_addr addr_in;
+       int err = inet_pton(AF_INET, address, &addr_in);
+       if (err == 0) {
+               fprintf(stderr, "Address %s is invalid.\n", address);
+               device->address = NULL;
+       } else if (err == -1) {
+               fprintf(stderr, "Can't convert address %s.\n", address);
+               device->address = NULL;
+       } else {
+               device->address = malloc(16);
+               if (!device->address) {
+                       fprintf(stderr, "Error allocating memory for converted address.\n");
+               } else {
+                       if (inet_ntop(AF_INET, &addr_in, device->address, 16) == NULL) {
+                               fprintf(stderr, "Can't convert address %s\n", address);
+                               free(device->address);
+                               device->address = NULL;
+                       }
+               }
+       }
+       return device;
+}
+
+Device *device_set_startaddress(Device *device, const char *address) {
+       if (!device) {
+               fprintf(stderr, "Error: trying to set start address of NULL device structure.\n");
+               return NULL;
+       }
+       free(device->startaddress);
+       struct in_addr addr_in;
+       int err = inet_pton(AF_INET, address, &addr_in);
+       if (err == 0) {
+               fprintf(stderr, "Address %s is invalid.\n", address);
+               device->startaddress = NULL;
+       } else if (err == -1) {
+               fprintf(stderr, "Can't convert address %s.\n", address);
+               device->startaddress = NULL;
+       } else {
+               device->startaddress = malloc(16);
+               if (!device->startaddress) {
+                       fprintf(stderr, "Error allocating memory for converted address.\n");
+               } else {
+                       if (inet_ntop(AF_INET, &addr_in, device->startaddress, 16) == NULL) {
+                               fprintf(stderr, "Can't convert address %s\n", address);
+                               free(device->startaddress);
+                               device->startaddress = NULL;
+                       }
+               }
+       }
+       return device;
+}
+
+Device *device_set_endaddress(Device *device, const char *address) {
+       if (!device) {
+               fprintf(stderr, "Error: trying to set end address of NULL device structure.\n");
+               return NULL;
+       }
+       free(device->endaddress);
+       struct in_addr addr_in;
+       int err = inet_pton(AF_INET, address, &addr_in);
+       if (err == 0) {
+               fprintf(stderr, "Address %s is invalid.\n", address);
+               device->endaddress = NULL;
+       } else if (err == -1) {
+               fprintf(stderr, "Can't convert address %s.\n", address);
+               device->endaddress = NULL;
+       } else {
+               device->endaddress = malloc(16);
+               if (!device->endaddress) {
+                       fprintf(stderr, "Error allocating memory for converted address.\n");
+               } else {
+                       if (inet_ntop(AF_INET, &addr_in, device->endaddress, 16) == NULL) {
+                               fprintf(stderr, "Can't convert address %s\n", address);
+                               free(device->endaddress);
+                               device->endaddress = NULL;
+                       }
+               }
+       }
+       return device;
+}
+
+int device_validate(Device *device) {
+       return device && device->name && device->address && device->startaddress && device->endaddress;
+}
+
+Device *device_search(Device *start, const char *name) {
+       while (start) {
+               if (strcmp(start->name, name) == 0) {
+                       return start;
+               }
+       }
+       return NULL;
+}
diff --git a/device.h b/device.h
new file mode 100644 (file)
index 0000000..66e0002
--- /dev/null
+++ b/device.h
@@ -0,0 +1,62 @@
+/*
+maemo-tethering
+(c) 2010 Gregor Riepl <onitake@gmail.com>
+
+Tethering utility for Maemo
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+struct Device;
+
+typedef struct Device {
+       char *name;
+       char *address;
+       char *startaddress;
+       char *endaddress;
+       struct Device *previous;
+       struct Device *next;
+} Device;
+
+// Allocates memory for a device structure and copies the name
+Device *device_new(const char *name);
+// Deallocates the device structure and its contents and adjusts the
+// previous and next pointers of the adjacent structures
+// Returns the next entry in the chain
+Device *device_delete(Device *device);
+// Walks through the device chain starting from device and deallocates all nodes
+// Does not delete nodes before device!
+// Returns the previous pointer of device
+Device *device_delete_all(Device *device);
+// Appends next to previous, shifting the link pointers if assigned
+// You can also pass NULL previous, creating a new head node
+// Returns next
+Device *device_append(Device *previous, Device *next);
+// Sets a new device name, deallocating the old one
+// device->name is set to NULL if an error occurs
+Device *device_set_name(Device *device, const char *name);
+// Validates and assigns a new device address, deallocating the old one
+// device->address is set to NULL if an error occurs
+Device *device_set_address(Device *device, const char *address);
+// Validates and assigns a new DHCP range start address, deallocating the old one
+// device->startaddress is set to NULL if an error occurs
+Device *device_set_startaddress(Device *device, const char *address);
+// Validates and assigns a new DHCP range end address, deallocating the old one
+// device->endaddress is set to NULL if an error occurs
+Device *device_set_endaddress(Device *device, const char *address);
+// Returns true if name, address, startaddress and endaddress are assigned, 0 if not
+int device_validate(Device *device);
+// Searches for a device name, starting from start and returns a pointer
+// to the matching node, or NULL if no name matches
+Device *device_search(Device *start, const char *name);
diff --git a/main.c b/main.c
index 0bc9a11..ac3f25b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -28,8 +28,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <sys/types.h>
 #include <sys/sysctl.h>
 #include <sys/wait.h>
-#include <arpa/inet.h>
 #include <dbus/dbus.h>
+#include "device.h"
 
 static const char *INETDEV = "gprs0";
 static const char *DEVICES[] = {
@@ -53,7 +53,9 @@ static const char *ENDADDRESSES[] = {
        NULL
 };
 
+// Run loop active flag
 static int running;
+// Number of active tethering connections
 static unsigned int active;
 
 static void siginthandler(int sig) {
@@ -90,48 +92,33 @@ static int launch(const char *args[]) {
        return 0;
 }
 
-static void added(const char *device, const char *inetdev, struct in_addr address, struct in_addr startaddress, struct in_addr endaddress) {
-       printf("Got org.kernel.kevent.add on %s\n", device);
-       char ascaddress[16];
-       if (inet_ntop(AF_INET, &address, ascaddress, sizeof(ascaddress)) == NULL) {
-               fprintf(stderr, "Can't convert address for device %s\n", device);
-               return;
-       }
-       char ascstartaddress[16];
-       if (inet_ntop(AF_INET, &startaddress, ascstartaddress, sizeof(ascstartaddress)) == NULL) {
-               fprintf(stderr, "Can't convert start address for device %s\n", device);
-               return;
-       }
-       char ascendaddress[16];
-       if (inet_ntop(AF_INET, &endaddress, ascendaddress, sizeof(ascendaddress)) == NULL) {
-               fprintf(stderr, "Can't convert end address for device %s\n", device);
-               return;
-       }
+static void added(Device *device, const char *inetdev) {
+       printf("Got org.kernel.kevent.add on %s\n", device->name);
        char *runfile = NULL;
-       if (asprintf(&runfile, "/var/run/tethering.%s.pid", device) == -1) {
-               fprintf(stderr, "Can't construct PID file name for device %s: %s\n", device, strerror(errno));
+       if (asprintf(&runfile, "/var/run/tethering.%s.pid", device->name) == -1) {
+               fprintf(stderr, "Can't construct PID file name for device %s: %s\n", device->name, strerror(errno));
                return;
        }
        char *range = NULL;
-       if (asprintf(&range, "%s-%s,3600", ascstartaddress, ascendaddress) == -1) {
-               fprintf(stderr, "Can't construct address range parameter for device %s: %s\n", device, strerror(errno));
+       if (asprintf(&range, "%s-%s,3600", device->startaddress, device->endaddress) == -1) {
+               fprintf(stderr, "Can't construct address range parameter for device %s: %s\n", device->name, strerror(errno));
                free(runfile);
                return;
        }
-       const char *ifconfig[] = { "/sbin/ifconfig", device, ascaddress, "up", NULL };
+       const char *ifconfig[] = { "/sbin/ifconfig", device->name, device->address, "up", NULL };
        const char *modprobe[] = { "/sbin/modprobe", "ipt_MASQUERADE", NULL };
        const char *iptables[] = { "/usr/sbin/iptables", "-t", "nat", "-A", "POSTROUTING", "-o", inetdev, "-j", "MASQUERADE", NULL };
-       const char *dnsmasq[] = { "/sbin/start-stop-daemon", "-S", "-p", runfile, "-m", "-b", "-x", "/usr/sbin/dnsmasq", "--", "-k", "-I", "lo", "-i", device, "-a", ascaddress, "-z", "-F", range, NULL };
+       const char *dnsmasq[] = { "/sbin/start-stop-daemon", "-S", "-p", runfile, "-m", "-b", "-x", "/usr/sbin/dnsmasq", "--", "-k", "-I", "lo", "-i", device->name, "-a", device->address, "-z", "-F", range, NULL };
        char *forwsysctl = NULL;
-       if (asprintf(&forwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", device) == -1) {
-               fprintf(stderr, "Can't construct sysctl path for device %s: %s\n", device, strerror(errno));
+       if (asprintf(&forwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", device->name) == -1) {
+               fprintf(stderr, "Can't construct sysctl path for device %s: %s\n", device->name, strerror(errno));
                free(runfile);
                free(range);
                return;
        }
        char *inetforwsysctl = NULL;
        if (asprintf(&inetforwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", inetdev) == -1) {
-               fprintf(stderr, "Can't construct inet sysctl path for device %s: %s\n", device, strerror(errno));
+               fprintf(stderr, "Can't construct inet sysctl path for device %s: %s\n", inetdev, strerror(errno));
                free(runfile);
                free(range);
                free(forwsysctl);
@@ -145,14 +132,14 @@ static void added(const char *device, const char *inetdev, struct in_addr addres
                                if (launch(dnsmasq) == 0) {
                                        int ffd = open(forwsysctl, O_WRONLY, 0666);
                                        if (ffd < 0) {
-                                               fprintf(stderr, "Can't enable forwarding on PAN device %s: %s\n", device, strerror(errno));
+                                               fprintf(stderr, "Can't enable forwarding on PAN device %s: %s\n", device->name, strerror(errno));
                                        } else {
                                                if (write(ffd, "1", 1) == -1) {
-                                                       fprintf(stderr, "Can't enable forwarding on PAN device %s: %s\n", device, strerror(errno));
+                                                       fprintf(stderr, "Can't enable forwarding on PAN device %s: %s\n", device->name, strerror(errno));
                                                } else {
                                                        int ifd = open(inetforwsysctl, O_WRONLY, 0666);
                                                        if (ifd < 0) {
-                                                               fprintf(stderr, "Can't enable forwarding on WAN device %s: %s\n", inetdev, strerror(errno));
+                                                               fprintf(stderr, "Can't enable forwarding on WAN device %s (path %s): %s\n", inetdev, inetforwsysctl, strerror(errno));
                                                        } else {
                                                                if (write(ifd, "1", 1) == -1) {
                                                                        fprintf(stderr, "Can't enable forwarding on WAN device %s: %s\n", inetdev, strerror(errno));
@@ -175,12 +162,12 @@ static void added(const char *device, const char *inetdev, struct in_addr addres
        active++;
 }
 
-static void removed(const char *device, const char *inetdev) {
-       printf("Got org.kernel.kevent.remove on %s\n", device);
+static void removed(Device *device, const char *inetdev) {
+       printf("Got org.kernel.kevent.remove on %s\n", device->name);
 
        char *runfile = NULL;
-       if (asprintf(&runfile, "/var/run/tethering.%s.pid", device) == -1) {
-               fprintf(stderr, "Can't construct PID file name for device %s: %s\n", device, strerror(errno));
+       if (asprintf(&runfile, "/var/run/tethering.%s.pid", device->name) == -1) {
+               fprintf(stderr, "Can't construct PID file name for device %s: %s\n", device->name, strerror(errno));
                return;
        }
        const char *dnsmasq[] = { "/sbin/start-stop-daemon", "-K", "-p", runfile, "-x", "/usr/sbin/dnsmasq", NULL };
@@ -196,7 +183,7 @@ static void removed(const char *device, const char *inetdev) {
        if (active == 0) {
                char *inetforwsysctl = NULL;
                if (asprintf(&inetforwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", inetdev) == -1) {
-                       fprintf(stderr, "Can't construct inet sysctl path for device %s: %s\n", device, strerror(errno));
+                       fprintf(stderr, "Can't construct inet sysctl path for device %s: %s\n", inetdev, strerror(errno));
                        return;
                }
                int fd = open(inetforwsysctl, O_WRONLY, 0666);
@@ -212,55 +199,32 @@ static void removed(const char *device, const char *inetdev) {
        }
 }
 
-static int finddev(const char* device, const char *devices[], size_t size) {
-       int i;
-       for (i = 0; i < size; i++) {
-               if (strcmp(device, devices[i]) == 0) {
-                       return i;
-               }
-       }
-       return -1;
-}
-
 int main(int argc, const char *argv[]) {
        running = 1;
        active = 0;
        signal(SIGINT, siginthandler);
 
-       size_t sizedevices = sizeof(DEVICES) / sizeof(DEVICES[0]);
-       const char *devices[sizedevices];
-       struct in_addr addresses[sizedevices];
-       struct in_addr startaddresses[sizedevices];
-       struct in_addr endaddresses[sizedevices];
-       size_t numdevices = 0;
+       Device *devices = NULL;
+       Device *node = NULL;
        size_t i;
-       for (i = 0; i < sizedevices; i++) {
-               int err = inet_pton(AF_INET, ADDRESSES[i], &addresses[numdevices]);
-               if (err == 0) {
-                       fprintf(stderr, "Invalid address for device %s: %s\n", DEVICES[i], ADDRESSES[i]);
-               } else if (err == -1) {
-                       fprintf(stderr, "Error converting address for device %s: %s\n", DEVICES[i], ADDRESSES[i]);
-               } else {
-                       int err = inet_pton(AF_INET, STARTADDRESSES[i], &startaddresses[numdevices]);
-                       if (err == 0) {
-                               fprintf(stderr, "Invalid DHCP start address for device %s: %s\n", DEVICES[i], STARTADDRESSES[i]);
-                       } else if (err == -1) {
-                               fprintf(stderr, "Error converting DHCP start address for device %s: %s\n", DEVICES[i], STARTADDRESSES[i]);
-                       } else {
-                               int err = inet_pton(AF_INET, ENDADDRESSES[i], &endaddresses[numdevices]);
-                               if (err == 0) {
-                                       fprintf(stderr, "Invalid DHCP end address for device %s: %s\n", DEVICES[i], ENDADDRESSES[i]);
-                               } else if (err == -1) {
-                                       fprintf(stderr, "Error converting DHCP end address for device %s: %s\n", DEVICES[i], ENDADDRESSES[i]);
-                               } else {
-                                       devices[numdevices] = DEVICES[i];
-                                       numdevices++;
+       for (i = 0; DEVICES[i]; i++) {
+               Device *device = device_new(DEVICES[i]);
+               if (device) {
+                       device_set_address(device, ADDRESSES[i]);
+                       device_set_startaddress(device, STARTADDRESSES[i]);
+                       device_set_endaddress(device, ENDADDRESSES[i]);
+                       if (device_validate(device)) {
+                               node = device_append(node, device);
+                               if (!devices) {
+                                       devices = node;
                                }
+                       } else {
+                               device_delete(device);
                        }
                }
        }
-       
-       if (numdevices == 0) {
+
+       if (!devices) {
                fprintf(stderr, "Warning, no devices configured. I will just sit here and wait for nicer weather.\n");
        }
 
@@ -275,10 +239,10 @@ int main(int argc, const char *argv[]) {
                return 1;
        }
 
-       for (i = 0; i < numdevices; i++) {
+       for (node = devices; node; node = node->next) {
                char *filter = NULL;
-               if (!asprintf(&filter, "type='signal',interface='org.kernel.kevent',path='/org/kernel/class/net/%s'", devices[i])) {
-                       fprintf(stderr, "Can't construct filter rule for device %s: %s\n", devices[i], strerror(errno));
+               if (!asprintf(&filter, "type='signal',interface='org.kernel.kevent',path='/org/kernel/class/net/%s'", node->name)) {
+                       fprintf(stderr, "Can't construct filter rule for device %s: %s\n", node->name, strerror(errno));
                } else {
                        dbus_bus_add_match(conn, filter, NULL);
                        free(filter);
@@ -300,10 +264,9 @@ int main(int argc, const char *argv[]) {
                                        if (last == 0 || last > 6) {
                                                fprintf(stderr, "Add message has no path or path too long!\n");
                                        } else {
-                                               int index = finddev(path[last - 1], devices, numdevices);
-                                               if (index >= 0) {
-                                                       
-                                                       added(devices[index], INETDEV, addresses[index], startaddresses[index], endaddresses[index]);
+                                               Device *device = device_search(devices, path[last - 1]);
+                                               if (device) {
+                                                       added(device, INETDEV);
                                                }
                                        }
                                        dbus_free_string_array(path);
@@ -318,9 +281,9 @@ int main(int argc, const char *argv[]) {
                                        if (last == 0 || last > 6) {
                                                fprintf(stderr, "Remove message has no path or path too long!\n");
                                        } else {
-                                               int index = finddev(path[last - 1], devices, numdevices);
-                                               if (index >= 0) {
-                                                       removed(devices[index], INETDEV);
+                                               Device *device = device_search(devices, path[last - 1]);
+                                               if (device) {
+                                                       removed(device, INETDEV);
                                                }
                                        }
                                        dbus_free_string_array(path);
@@ -330,6 +293,7 @@ int main(int argc, const char *argv[]) {
                }
        }
 
+       device_delete_all(devices);
        dbus_connection_unref(conn);
        return 0;
 }