Added readme
[mtetherd] / device.c
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;
+}