Added readme
[mtetherd] / device.c
1 /*
2 maemo-tethering
3 (c) 2010 Gregor Riepl <onitake@gmail.com>
4
5 Tethering utility for Maemo
6
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.
11
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.
16
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/>.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <arpa/inet.h>
25 #include "device.h"
26
27 Device *device_new(const char *name) {
28         if (!name) {
29                 fprintf(stderr, "No device name given.\n");
30                 return NULL;
31         }
32         Device *ret = malloc(sizeof(Device));
33         if (!ret) {
34                 fprintf(stderr, "Error allocating memory for device structure.\n");
35                 return NULL;
36         }
37         memset(ret, 0, sizeof(Device));
38         size_t length = strlen(name) + 1;
39         ret->name = malloc(length);
40         if (!ret->name) {
41                 fprintf(stderr, "Error allocating memory for device name.\n");
42                 free(ret);
43                 return NULL;
44         }
45         memcpy(ret->name, name, length);
46         return ret;
47 }
48
49 Device *device_delete(Device *device) {
50         if (!device) {
51                 fprintf(stderr, "Error: trying to deallocate NULL device structure.\n");
52                 return NULL;
53         }
54         Device *ret = device->next;
55         free(device->name);
56         free(device->address);
57         free(device->startaddress);
58         free(device->endaddress);
59         if (device->previous) {
60                 device->previous->next = device->next;
61         }
62         if (device->next) {
63                 device->next->previous = device->previous;
64         }
65         free(device);
66         return ret;
67 }
68
69 Device *device_delete_all(Device *device) {
70         Device *ret = device->previous;
71         while (device) {
72                 device = device_delete(device);
73         }
74         return ret;
75 }
76
77 Device *device_append(Device *previous, Device *next) {
78         if (previous) {
79                 if (previous->next) {
80                         previous->next->previous = next;
81                         next->next = previous->next;
82                 }
83                 previous->next = next;
84                 next->previous = previous;
85         }
86         return next;
87 }
88
89 Device *device_set_name(Device *device, const char *name) {
90         if (!device) {
91                 fprintf(stderr, "Error: trying to set name of NULL device structure.\n");
92                 return NULL;
93         }
94         free(device->name);
95         size_t length = strlen(name) + 1;
96         device->name = malloc(length);
97         if (!device->name) {
98                 fprintf(stderr, "Error allocating memory for device name.\n");
99         }
100         return device;
101 }
102
103 Device *device_set_address(Device *device, const char *address) {
104         if (!device) {
105                 fprintf(stderr, "Error: trying to set address of NULL device structure.\n");
106                 return NULL;
107         }
108         free(device->address);
109         struct in_addr addr_in;
110         int err = inet_pton(AF_INET, address, &addr_in);
111         if (err == 0) {
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;
117         } else {
118                 device->address = malloc(16);
119                 if (!device->address) {
120                         fprintf(stderr, "Error allocating memory for converted address.\n");
121                 } else {
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;
126                         }
127                 }
128         }
129         return device;
130 }
131
132 Device *device_set_startaddress(Device *device, const char *address) {
133         if (!device) {
134                 fprintf(stderr, "Error: trying to set start address of NULL device structure.\n");
135                 return NULL;
136         }
137         free(device->startaddress);
138         struct in_addr addr_in;
139         int err = inet_pton(AF_INET, address, &addr_in);
140         if (err == 0) {
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;
146         } else {
147                 device->startaddress = malloc(16);
148                 if (!device->startaddress) {
149                         fprintf(stderr, "Error allocating memory for converted address.\n");
150                 } else {
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;
155                         }
156                 }
157         }
158         return device;
159 }
160
161 Device *device_set_endaddress(Device *device, const char *address) {
162         if (!device) {
163                 fprintf(stderr, "Error: trying to set end address of NULL device structure.\n");
164                 return NULL;
165         }
166         free(device->endaddress);
167         struct in_addr addr_in;
168         int err = inet_pton(AF_INET, address, &addr_in);
169         if (err == 0) {
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;
175         } else {
176                 device->endaddress = malloc(16);
177                 if (!device->endaddress) {
178                         fprintf(stderr, "Error allocating memory for converted address.\n");
179                 } else {
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;
184                         }
185                 }
186         }
187         return device;
188 }
189
190 int device_validate(Device *device) {
191         return device && device->name && device->address && device->startaddress && device->endaddress;
192 }
193
194 Device *device_search(Device *start, const char *name) {
195         while (start) {
196                 if (strcmp(start->name, name) == 0) {
197                         return start;
198                 }
199         }
200         return NULL;
201 }