3 (c) 2010 Gregor Riepl <onitake@gmail.com>
5 Tethering utility for Maemo
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <arpa/inet.h>
27 Device *device_new(const char *name) {
29 fprintf(stderr, "No device name given.\n");
32 Device *ret = malloc(sizeof(Device));
34 fprintf(stderr, "Error allocating memory for device structure.\n");
37 memset(ret, 0, sizeof(Device));
38 device_set_name(ret, name);
46 Device *device_delete(Device *device) {
48 fprintf(stderr, "Error: trying to deallocate NULL device structure.\n");
51 Device *ret = device->next;
53 free(device->address);
54 free(device->startaddress);
55 free(device->endaddress);
56 if (device->previous) {
57 device->previous->next = device->next;
60 device->next->previous = device->previous;
66 Device *device_delete_all(Device *device) {
67 Device *ret = device->previous;
69 device = device_delete(device);
74 Device *device_append(Device *previous, Device *next) {
77 previous->next->previous = next;
78 next->next = previous->next;
80 previous->next = next;
81 next->previous = previous;
86 Device *device_set_name(Device *device, const char *name) {
88 fprintf(stderr, "Error: trying to set name of NULL device structure.\n");
92 size_t length = strlen(name);
93 device->name = malloc(length + 1);
95 fprintf(stderr, "Error allocating memory for device name.\n");
99 for (i = 0, j = 0; i < length; i++) {
100 if ((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= '0' && name[i] <= '9')) {
101 device->name[j] = name[i];
107 device->name[j] = '\0';
109 fprintf(stderr, "Error: Invalid device name.\n");
114 fprintf(stderr, "Warning, device name contained invalid characters. They have been removed.\n");
121 Device *device_set_address(Device *device, const char *address) {
123 fprintf(stderr, "Error: trying to set address of NULL device structure.\n");
126 free(device->address);
127 struct in_addr addr_in;
128 int err = inet_pton(AF_INET, address, &addr_in);
130 fprintf(stderr, "Address %s is invalid.\n", address);
131 device->address = NULL;
132 } else if (err == -1) {
133 fprintf(stderr, "Can't convert address %s.\n", address);
134 device->address = NULL;
136 device->address = malloc(16);
137 if (!device->address) {
138 fprintf(stderr, "Error allocating memory for converted address.\n");
140 if (inet_ntop(AF_INET, &addr_in, device->address, 16) == NULL) {
141 fprintf(stderr, "Can't convert address %s\n", address);
142 free(device->address);
143 device->address = NULL;
150 Device *device_set_startaddress(Device *device, const char *address) {
152 fprintf(stderr, "Error: trying to set start address of NULL device structure.\n");
155 free(device->startaddress);
156 struct in_addr addr_in;
157 int err = inet_pton(AF_INET, address, &addr_in);
159 fprintf(stderr, "Address %s is invalid.\n", address);
160 device->startaddress = NULL;
161 } else if (err == -1) {
162 fprintf(stderr, "Can't convert address %s.\n", address);
163 device->startaddress = NULL;
165 device->startaddress = malloc(16);
166 if (!device->startaddress) {
167 fprintf(stderr, "Error allocating memory for converted address.\n");
169 if (inet_ntop(AF_INET, &addr_in, device->startaddress, 16) == NULL) {
170 fprintf(stderr, "Can't convert address %s\n", address);
171 free(device->startaddress);
172 device->startaddress = NULL;
179 Device *device_set_endaddress(Device *device, const char *address) {
181 fprintf(stderr, "Error: trying to set end address of NULL device structure.\n");
184 free(device->endaddress);
185 struct in_addr addr_in;
186 int err = inet_pton(AF_INET, address, &addr_in);
188 fprintf(stderr, "Address %s is invalid.\n", address);
189 device->endaddress = NULL;
190 } else if (err == -1) {
191 fprintf(stderr, "Can't convert address %s.\n", address);
192 device->endaddress = NULL;
194 device->endaddress = malloc(16);
195 if (!device->endaddress) {
196 fprintf(stderr, "Error allocating memory for converted address.\n");
198 if (inet_ntop(AF_INET, &addr_in, device->endaddress, 16) == NULL) {
199 fprintf(stderr, "Can't convert address %s\n", address);
200 free(device->endaddress);
201 device->endaddress = NULL;
208 int device_validate(Device *device) {
209 return device && device->name && device->address && device->startaddress && device->endaddress;
212 Device *device_search(Device *start, const char *name) {
213 for (; start; start = start->next) {
214 if (strcmp(start->name, name) == 0) {