Attach IP addresses to configuration
[platform/upstream/connman.git] / src / ipconfig.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
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 version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <net/if.h>
27
28 #ifndef IFF_LOWER_UP
29 #define IFF_LOWER_UP    0x10000
30 #endif
31
32 #include <gdbus.h>
33
34 #include "connman.h"
35
36 struct connman_ipaddress {
37         unsigned char prefixlen;
38         char *address;
39 };
40
41 struct connman_ipconfig {
42         gint refcount;
43         int index;
44         char *interface;
45         unsigned short type;
46         unsigned int flags;
47         enum connman_ipconfig_method method;
48         GSList *address_list;
49 };
50
51 static void free_address_list(struct connman_ipconfig *ipconfig)
52 {
53         GSList *list;
54
55         for (list = ipconfig->address_list; list; list = list->next) {
56                 struct connman_ipaddress *ipaddress = list->data;
57
58                 g_free(ipaddress->address);
59                 g_free(ipaddress);
60         }
61
62         g_slist_free(ipconfig->address_list);
63         ipconfig->address_list = NULL;
64 }
65
66 static struct connman_ipaddress *find_ipaddress(struct connman_ipconfig *ipconfig,
67                                 unsigned char prefixlen, const char *address)
68 {
69         GSList *list;
70
71         for (list = ipconfig->address_list; list; list = list->next) {
72                 struct connman_ipaddress *ipaddress = list->data;
73
74                 if (g_strcmp0(ipaddress->address, address) == 0 &&
75                                         ipaddress->prefixlen == prefixlen)
76                         return ipaddress;
77         }
78
79         return NULL;
80 }
81
82 /**
83  * connman_ipconfig_create:
84  *
85  * Allocate a new ipconfig structure.
86  *
87  * Returns: a newly-allocated #connman_ipconfig structure
88  */
89 struct connman_ipconfig *connman_ipconfig_create(int index)
90 {
91         struct connman_ipconfig *ipconfig;
92
93         DBG("");
94
95         ipconfig = g_try_new0(struct connman_ipconfig, 1);
96         if (ipconfig == NULL)
97                 return NULL;
98
99         ipconfig->refcount = 1;
100
101         ipconfig->index = index;
102         ipconfig->interface = connman_inet_ifname(index);
103
104         DBG("ipconfig %p", ipconfig);
105
106         connman_info("%s {create} index %d", ipconfig->interface,
107                                                         ipconfig->index);
108
109         return ipconfig;
110 }
111
112 /**
113  * connman_ipconfig_ref:
114  * @ipconfig: ipconfig structure
115  *
116  * Increase reference counter of ipconfig
117  */
118 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
119 {
120         g_atomic_int_inc(&ipconfig->refcount);
121
122         return ipconfig;
123 }
124
125 /**
126  * connman_ipconfig_unref:
127  * @ipconfig: ipconfig structure
128  *
129  * Decrease reference counter of ipconfig
130  */
131 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
132 {
133         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
134                 connman_info("%s {remove} index %d", ipconfig->interface,
135                                                         ipconfig->index);
136
137                 free_address_list(ipconfig);
138
139                 g_free(ipconfig->interface);
140                 g_free(ipconfig);
141         }
142 }
143
144 /**
145  * connman_ipconfig_set_method:
146  * @ipconfig: ipconfig structure
147  * @method: configuration method
148  *
149  * Set the configuration method
150  */
151 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
152                                         enum connman_ipconfig_method method)
153 {
154         ipconfig->method = method;
155
156         return 0;
157 }
158
159 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
160 {
161         return ipconfig->index;
162 }
163
164 unsigned short __connman_ipconfig_get_type(struct connman_ipconfig *ipconfig)
165 {
166         return ipconfig->type;
167 }
168
169 unsigned int __connman_ipconfig_get_flags(struct connman_ipconfig *ipconfig)
170 {
171         return ipconfig->flags;
172 }
173
174 void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
175                                         unsigned flags, unsigned change)
176 {
177         GString *str;
178
179         if (flags == ipconfig->flags)
180                 return;
181
182         ipconfig->flags = flags;
183
184         str = g_string_new(NULL);
185         if (str == NULL)
186                 return;
187
188         if (flags & IFF_UP)
189                 g_string_append(str, "UP");
190         else
191                 g_string_append(str, "DOWN");
192
193         if (flags & IFF_RUNNING)
194                 g_string_append(str, ",RUNNING");
195
196         if (flags & IFF_LOWER_UP)
197                 g_string_append(str, ",LOWER_UP");
198
199         connman_info("%s {update} flags %u change %u <%s>",
200                                 ipconfig->interface, flags, change, str->str);
201
202         g_string_free(str, TRUE);
203 }
204
205 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
206                                 const char *label, unsigned char prefixlen,
207                                 const char *address, const char *broadcast)
208 {
209         struct connman_ipaddress *ipaddress;
210
211         ipaddress = g_try_new0(struct connman_ipaddress, 1);
212         if (ipaddress == NULL)
213                 return;
214
215         ipaddress->prefixlen = prefixlen;
216         ipaddress->address = g_strdup(address);
217
218         ipconfig->address_list = g_slist_append(ipconfig->address_list,
219                                                                 ipaddress);
220
221         connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
222                                                 address, prefixlen, label);
223 }
224
225 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
226                                 const char *label, unsigned char prefixlen,
227                                 const char *address, const char *broadcast)
228 {
229         struct connman_ipaddress *ipaddress;
230
231         ipaddress = find_ipaddress(ipconfig, prefixlen, address);
232         if (ipaddress == NULL)
233                 return;
234
235         ipconfig->address_list = g_slist_remove(ipconfig->address_list,
236                                                                 ipaddress);
237
238         g_free(ipaddress->address);
239         g_free(ipaddress);
240
241         connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
242                                                 address, prefixlen, label);
243 }
244
245 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
246 {
247         switch (method) {
248         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
249                 break;
250         case CONNMAN_IPCONFIG_METHOD_OFF:
251                 return "off";
252         case CONNMAN_IPCONFIG_METHOD_STATIC:
253                 return "static";
254         case CONNMAN_IPCONFIG_METHOD_DHCP:
255                 return "dhcp";
256         }
257
258         return NULL;
259 }
260
261 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
262 {
263         if (g_strcmp0(method, "off") == 0)
264                 return CONNMAN_IPCONFIG_METHOD_OFF;
265         else if (g_strcmp0(method, "static") == 0)
266                 return CONNMAN_IPCONFIG_METHOD_STATIC;
267         else if (g_strcmp0(method, "dhcp") == 0)
268                 return CONNMAN_IPCONFIG_METHOD_DHCP;
269         else
270                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
271 }
272
273 static void append_variant(DBusMessageIter *iter, const char *prefix,
274                                         const char *key, int type, void *val)
275 {
276         char *str;
277
278         if (prefix == NULL) {
279                 connman_dbus_dict_append_variant(iter, key, type, val);
280                 return;
281         }
282
283         str = g_strdup_printf("%s%s", prefix, key);
284         if (str != NULL)
285                 connman_dbus_dict_append_variant(iter, str, type, val);
286
287         g_free(str);
288 }
289
290 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
291                                 DBusMessageIter *iter, const char *prefix)
292 {
293         const char *str;
294
295         str = __connman_ipconfig_method2string(ipconfig->method);
296         if (str == NULL)
297                 return;
298
299         append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
300 }
301
302 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
303                                 const char *key, DBusMessageIter *value)
304 {
305         int type = dbus_message_iter_get_arg_type(value);
306
307         DBG("ipconfig %p key %s type %d", ipconfig, key, type);
308
309         if (g_strcmp0(key, "Method") == 0) {
310                 const char *method;
311
312                 if (type != DBUS_TYPE_STRING)
313                         return -EINVAL;
314
315                 dbus_message_iter_get_basic(value, &method);
316
317                 ipconfig->method = __connman_ipconfig_string2method(method);
318         } else
319                 return -EINVAL;
320
321         return 0;
322 }
323
324 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
325                 GKeyFile *keyfile, const char *identifier, const char *prefix)
326 {
327         DBG("ipconfig %p identifier %s", ipconfig, identifier);
328
329         return 0;
330 }
331
332 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
333                 GKeyFile *keyfile, const char *identifier, const char *prefix)
334 {
335         DBG("ipconfig %p identifier %s", ipconfig, identifier);
336
337         return 0;
338 }
339
340 static GSList *driver_list = NULL;
341
342 static gint compare_priority(gconstpointer a, gconstpointer b)
343 {
344         const struct connman_ipconfig_driver *driver1 = a;
345         const struct connman_ipconfig_driver *driver2 = b;
346
347         return driver2->priority - driver1->priority;
348 }
349
350 /**
351  * connman_ipconfig_driver_register:
352  * @driver: IP configuration driver
353  *
354  * Register a new IP configuration driver
355  *
356  * Returns: %0 on success
357  */
358 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
359 {
360         DBG("driver %p name %s", driver, driver->name);
361
362         driver_list = g_slist_insert_sorted(driver_list, driver,
363                                                         compare_priority);
364
365         return 0;
366 }
367
368 /**
369  * connman_ipconfig_driver_unregister:
370  * @driver: IP configuration driver
371  *
372  * Remove a previously registered IP configuration driver.
373  */
374 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
375 {
376         DBG("driver %p name %s", driver, driver->name);
377
378         driver_list = g_slist_remove(driver_list, driver);
379 }