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 size_t length = strlen(name) + 1;
39 ret->name = malloc(length);
41 fprintf(stderr, "Error allocating memory for device name.\n");
45 memcpy(ret->name, name, length);
49 Device *device_delete(Device *device) {
51 fprintf(stderr, "Error: trying to deallocate NULL device structure.\n");
54 Device *ret = device->next;
56 free(device->address);
57 free(device->startaddress);
58 free(device->endaddress);
59 if (device->previous) {
60 device->previous->next = device->next;
63 device->next->previous = device->previous;
69 Device *device_delete_all(Device *device) {
70 Device *ret = device->previous;
72 device = device_delete(device);
77 Device *device_append(Device *previous, Device *next) {
80 previous->next->previous = next;
81 next->next = previous->next;
83 previous->next = next;
84 next->previous = previous;
89 Device *device_set_name(Device *device, const char *name) {
91 fprintf(stderr, "Error: trying to set name of NULL device structure.\n");
95 size_t length = strlen(name) + 1;
96 device->name = malloc(length);
98 fprintf(stderr, "Error allocating memory for device name.\n");
103 Device *device_set_address(Device *device, const char *address) {
105 fprintf(stderr, "Error: trying to set address of NULL device structure.\n");
108 free(device->address);
109 struct in_addr addr_in;
110 int err = inet_pton(AF_INET, address, &addr_in);
112 fprintf(stderr, "Address %s is invalid.\n", address);
113 device->address = NULL;
114 } else if (err == -1) {
115 fprintf(stderr, "Can't convert address %s.\n", address);
116 device->address = NULL;
118 device->address = malloc(16);
119 if (!device->address) {
120 fprintf(stderr, "Error allocating memory for converted address.\n");
122 if (inet_ntop(AF_INET, &addr_in, device->address, 16) == NULL) {
123 fprintf(stderr, "Can't convert address %s\n", address);
124 free(device->address);
125 device->address = NULL;
132 Device *device_set_startaddress(Device *device, const char *address) {
134 fprintf(stderr, "Error: trying to set start address of NULL device structure.\n");
137 free(device->startaddress);
138 struct in_addr addr_in;
139 int err = inet_pton(AF_INET, address, &addr_in);
141 fprintf(stderr, "Address %s is invalid.\n", address);
142 device->startaddress = NULL;
143 } else if (err == -1) {
144 fprintf(stderr, "Can't convert address %s.\n", address);
145 device->startaddress = NULL;
147 device->startaddress = malloc(16);
148 if (!device->startaddress) {
149 fprintf(stderr, "Error allocating memory for converted address.\n");
151 if (inet_ntop(AF_INET, &addr_in, device->startaddress, 16) == NULL) {
152 fprintf(stderr, "Can't convert address %s\n", address);
153 free(device->startaddress);
154 device->startaddress = NULL;
161 Device *device_set_endaddress(Device *device, const char *address) {
163 fprintf(stderr, "Error: trying to set end address of NULL device structure.\n");
166 free(device->endaddress);
167 struct in_addr addr_in;
168 int err = inet_pton(AF_INET, address, &addr_in);
170 fprintf(stderr, "Address %s is invalid.\n", address);
171 device->endaddress = NULL;
172 } else if (err == -1) {
173 fprintf(stderr, "Can't convert address %s.\n", address);
174 device->endaddress = NULL;
176 device->endaddress = malloc(16);
177 if (!device->endaddress) {
178 fprintf(stderr, "Error allocating memory for converted address.\n");
180 if (inet_ntop(AF_INET, &addr_in, device->endaddress, 16) == NULL) {
181 fprintf(stderr, "Can't convert address %s\n", address);
182 free(device->endaddress);
183 device->endaddress = NULL;
190 int device_validate(Device *device) {
191 return device && device->name && device->address && device->startaddress && device->endaddress;
194 Device *device_search(Device *start, const char *name) {
196 if (strcmp(start->name, name) == 0) {