Track IP configuration based on RTNL interface list
[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 <gdbus.h>
27
28 #include "connman.h"
29
30 struct connman_ipconfig {
31         gint refcount;
32         int index;
33         char *interface;
34         enum connman_ipconfig_method method;
35 };
36
37 /**
38  * connman_ipconfig_create:
39  *
40  * Allocate a new ipconfig structure.
41  *
42  * Returns: a newly-allocated #connman_ipconfig structure
43  */
44 struct connman_ipconfig *connman_ipconfig_create(int index)
45 {
46         struct connman_ipconfig *ipconfig;
47
48         DBG("");
49
50         ipconfig = g_try_new0(struct connman_ipconfig, 1);
51         if (ipconfig == NULL)
52                 return NULL;
53
54         ipconfig->refcount = 1;
55
56         ipconfig->index = index;
57         ipconfig->interface = connman_inet_ifname(index);
58
59         DBG("ipconfig %p", ipconfig);
60
61         //__connman_rtnl_register_ipconfig(ipconfig);
62
63         connman_info("%s {create} index %d", ipconfig->interface,
64                                                         ipconfig->index);
65
66         return ipconfig;
67 }
68
69 /**
70  * connman_ipconfig_ref:
71  * @ipconfig: ipconfig structure
72  *
73  * Increase reference counter of ipconfig
74  */
75 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
76 {
77         g_atomic_int_inc(&ipconfig->refcount);
78
79         return ipconfig;
80 }
81
82 /**
83  * connman_ipconfig_unref:
84  * @ipconfig: ipconfig structure
85  *
86  * Decrease reference counter of ipconfig
87  */
88 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
89 {
90         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
91                 //__connman_rtnl_unregister_ipconfig(ipconfig);
92
93                 connman_info("%s {remove} index %d", ipconfig->interface,
94                                                         ipconfig->index);
95
96                 g_free(ipconfig->interface);
97                 g_free(ipconfig);
98         }
99 }
100
101 /**
102  * connman_ipconfig_set_method:
103  * @ipconfig: ipconfig structure
104  * @method: configuration method
105  *
106  * Set the configuration method
107  */
108 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
109                                         enum connman_ipconfig_method method)
110 {
111         ipconfig->method = method;
112
113         return 0;
114 }
115
116 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
117 {
118         return ipconfig->index;
119 }
120
121 void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
122                                         unsigned flags, unsigned change)
123 {
124         connman_info("%s {update} flags %u change %u", ipconfig->interface,
125                                                         flags, change);
126 }
127
128 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
129                                 const char *label, unsigned char prefixlen,
130                                 const char *address, const char *broadcast)
131 {
132         connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
133                                                 address, prefixlen, label);
134 }
135
136 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
137                                 const char *label, unsigned char prefixlen,
138                                 const char *address, const char *broadcast)
139 {
140         connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
141                                                 address, prefixlen, label);
142 }
143
144 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
145 {
146         switch (method) {
147         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
148                 break;
149         case CONNMAN_IPCONFIG_METHOD_OFF:
150                 return "off";
151         case CONNMAN_IPCONFIG_METHOD_STATIC:
152                 return "static";
153         case CONNMAN_IPCONFIG_METHOD_DHCP:
154                 return "dhcp";
155         }
156
157         return NULL;
158 }
159
160 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
161 {
162         if (g_strcmp0(method, "off") == 0)
163                 return CONNMAN_IPCONFIG_METHOD_OFF;
164         else if (g_strcmp0(method, "static") == 0)
165                 return CONNMAN_IPCONFIG_METHOD_STATIC;
166         else if (g_strcmp0(method, "dhcp") == 0)
167                 return CONNMAN_IPCONFIG_METHOD_DHCP;
168         else
169                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
170 }
171
172 static void append_variant(DBusMessageIter *iter, const char *prefix,
173                                         const char *key, int type, void *val)
174 {
175         char *str;
176
177         if (prefix == NULL) {
178                 connman_dbus_dict_append_variant(iter, key, type, val);
179                 return;
180         }
181
182         str = g_strdup_printf("%s%s", prefix, key);
183         if (str != NULL)
184                 connman_dbus_dict_append_variant(iter, str, type, val);
185
186         g_free(str);
187 }
188
189 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
190                                 DBusMessageIter *iter, const char *prefix)
191 {
192         const char *str;
193
194         str = __connman_ipconfig_method2string(ipconfig->method);
195         if (str == NULL)
196                 return;
197
198         append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
199 }
200
201 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
202                                 const char *key, DBusMessageIter *value)
203 {
204         int type = dbus_message_iter_get_arg_type(value);
205
206         DBG("ipconfig %p key %s type %d", ipconfig, key, type);
207
208         if (g_strcmp0(key, "Method") == 0) {
209                 const char *method;
210
211                 if (type != DBUS_TYPE_STRING)
212                         return -EINVAL;
213
214                 dbus_message_iter_get_basic(value, &method);
215
216                 ipconfig->method = __connman_ipconfig_string2method(method);
217         } else
218                 return -EINVAL;
219
220         return 0;
221 }
222
223 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
224                 GKeyFile *keyfile, const char *identifier, const char *prefix)
225 {
226         DBG("ipconfig %p identifier %s", ipconfig, identifier);
227
228         return 0;
229 }
230
231 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
232                 GKeyFile *keyfile, const char *identifier, const char *prefix)
233 {
234         DBG("ipconfig %p identifier %s", ipconfig, identifier);
235
236         return 0;
237 }
238
239 static GSList *driver_list = NULL;
240
241 static gint compare_priority(gconstpointer a, gconstpointer b)
242 {
243         const struct connman_ipconfig_driver *driver1 = a;
244         const struct connman_ipconfig_driver *driver2 = b;
245
246         return driver2->priority - driver1->priority;
247 }
248
249 /**
250  * connman_ipconfig_driver_register:
251  * @driver: IP configuration driver
252  *
253  * Register a new IP configuration driver
254  *
255  * Returns: %0 on success
256  */
257 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
258 {
259         DBG("driver %p name %s", driver, driver->name);
260
261         driver_list = g_slist_insert_sorted(driver_list, driver,
262                                                         compare_priority);
263
264         return 0;
265 }
266
267 /**
268  * connman_ipconfig_driver_unregister:
269  * @driver: IP configuration driver
270  *
271  * Remove a previously registered IP configuration driver.
272  */
273 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
274 {
275         DBG("driver %p name %s", driver, driver->name);
276
277         driver_list = g_slist_remove(driver_list, driver);
278 }