X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=device.c;fp=device.c;h=9dbe424fd72aa52651ad979a4b87f368fc93fb9a;hb=62befab10c1a61d24d2e511758472dc05e6c8525;hp=0000000000000000000000000000000000000000;hpb=3139d9cfdf8d05b3bbc550768464947a228af427;p=mtetherd diff --git a/device.c b/device.c new file mode 100644 index 0000000..9dbe424 --- /dev/null +++ b/device.c @@ -0,0 +1,201 @@ +/* +maemo-tethering +(c) 2010 Gregor Riepl + +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 . +*/ + +#include +#include +#include +#include +#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; +}