5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
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.
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.
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
29 #define IFF_LOWER_UP 0x10000
36 struct connman_ipaddress {
37 unsigned char prefixlen;
41 struct connman_ipconfig {
48 struct connman_ipconfig_ops *ops;
51 enum connman_ipconfig_method method;
56 static void free_address_list(struct connman_ipconfig *ipconfig)
60 for (list = ipconfig->address_list; list; list = list->next) {
61 struct connman_ipaddress *ipaddress = list->data;
63 g_free(ipaddress->address);
68 g_slist_free(ipconfig->address_list);
69 ipconfig->address_list = NULL;
72 static struct connman_ipaddress *find_ipaddress(struct connman_ipconfig *ipconfig,
73 unsigned char prefixlen, const char *address)
77 for (list = ipconfig->address_list; list; list = list->next) {
78 struct connman_ipaddress *ipaddress = list->data;
80 if (g_strcmp0(ipaddress->address, address) == 0 &&
81 ipaddress->prefixlen == prefixlen)
89 * connman_ipconfig_create:
91 * Allocate a new ipconfig structure.
93 * Returns: a newly-allocated #connman_ipconfig structure
95 struct connman_ipconfig *connman_ipconfig_create(int index)
97 struct connman_ipconfig *ipconfig;
101 ipconfig = g_try_new0(struct connman_ipconfig, 1);
102 if (ipconfig == NULL)
105 ipconfig->refcount = 1;
107 ipconfig->index = index;
108 ipconfig->interface = connman_inet_ifname(index);
110 DBG("ipconfig %p", ipconfig);
112 connman_info("%s {create} index %d", ipconfig->interface,
119 * connman_ipconfig_ref:
120 * @ipconfig: ipconfig structure
122 * Increase reference counter of ipconfig
124 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
126 g_atomic_int_inc(&ipconfig->refcount);
132 * connman_ipconfig_unref:
133 * @ipconfig: ipconfig structure
135 * Decrease reference counter of ipconfig
137 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
139 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
140 connman_info("%s {remove} index %d", ipconfig->interface,
143 g_free(ipconfig->gateway);
145 free_address_list(ipconfig);
147 g_free(ipconfig->interface);
153 * connman_ipconfig_get_data:
154 * @ipconfig: ipconfig structure
156 * Get private data pointer
158 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
160 return ipconfig->ops_data;
164 * connman_ipconfig_set_data:
165 * @ipconfig: ipconfig structure
166 * @data: data pointer
168 * Set private data pointer
170 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
172 ipconfig->ops_data = data;
176 * connman_ipconfig_set_ops:
177 * @ipconfig: ipconfig structure
178 * @ops: operation callbacks
180 * Set the operation callbacks
182 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
183 struct connman_ipconfig_ops *ops)
189 * connman_ipconfig_set_method:
190 * @ipconfig: ipconfig structure
191 * @method: configuration method
193 * Set the configuration method
195 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
196 enum connman_ipconfig_method method)
198 ipconfig->method = method;
203 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
205 return ipconfig->index;
208 unsigned short __connman_ipconfig_get_type(struct connman_ipconfig *ipconfig)
210 return ipconfig->type;
213 unsigned int __connman_ipconfig_get_flags(struct connman_ipconfig *ipconfig)
215 return ipconfig->flags;
218 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
220 return ipconfig->gateway;
223 void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
224 unsigned flags, unsigned change)
228 if (flags == ipconfig->flags)
231 ipconfig->flags = flags;
233 str = g_string_new(NULL);
238 g_string_append(str, "UP");
240 g_string_append(str, "DOWN");
242 if (flags & IFF_RUNNING)
243 g_string_append(str, ",RUNNING");
245 if (flags & IFF_LOWER_UP)
246 g_string_append(str, ",LOWER_UP");
248 connman_info("%s {update} flags %u change %u <%s>",
249 ipconfig->interface, flags, change, str->str);
251 g_string_free(str, TRUE);
254 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
255 const char *label, unsigned char prefixlen,
256 const char *address, const char *broadcast)
258 struct connman_ipaddress *ipaddress;
260 ipaddress = g_try_new0(struct connman_ipaddress, 1);
261 if (ipaddress == NULL)
264 ipaddress->prefixlen = prefixlen;
265 ipaddress->address = g_strdup(address);
267 ipconfig->address_list = g_slist_append(ipconfig->address_list,
270 connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
271 address, prefixlen, label);
274 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
275 const char *label, unsigned char prefixlen,
276 const char *address, const char *broadcast)
278 struct connman_ipaddress *ipaddress;
280 ipaddress = find_ipaddress(ipconfig, prefixlen, address);
281 if (ipaddress == NULL)
284 ipconfig->address_list = g_slist_remove(ipconfig->address_list,
287 g_free(ipaddress->address);
290 connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
291 address, prefixlen, label);
294 static const char *scope2str(unsigned char scope)
306 void __connman_ipconfig_add_route(struct connman_ipconfig *ipconfig,
307 unsigned char scope, const char *destination,
310 if (scope == 0 && g_strcmp0(destination, "0.0.0.0") == 0) {
311 g_free(ipconfig->gateway);
312 ipconfig->gateway = g_strdup(gateway);
315 connman_info("%s {add} route %s gw %s scope %u <%s>",
316 ipconfig->interface, destination,
317 gateway, scope, scope2str(scope));
320 void __connman_ipconfig_del_route(struct connman_ipconfig *ipconfig,
321 unsigned char scope, const char *destination,
324 if (scope == 0 && g_strcmp0(destination, "0.0.0.0") == 0) {
325 g_free(ipconfig->gateway);
326 ipconfig->gateway = NULL;
329 connman_info("%s {del} route %s gw %s scope %u <%s>",
330 ipconfig->interface, destination,
331 gateway, scope, scope2str(scope));
334 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
337 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
339 case CONNMAN_IPCONFIG_METHOD_OFF:
341 case CONNMAN_IPCONFIG_METHOD_STATIC:
343 case CONNMAN_IPCONFIG_METHOD_DHCP:
350 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
352 if (g_strcmp0(method, "off") == 0)
353 return CONNMAN_IPCONFIG_METHOD_OFF;
354 else if (g_strcmp0(method, "static") == 0)
355 return CONNMAN_IPCONFIG_METHOD_STATIC;
356 else if (g_strcmp0(method, "dhcp") == 0)
357 return CONNMAN_IPCONFIG_METHOD_DHCP;
359 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
362 static void append_variant(DBusMessageIter *iter, const char *prefix,
363 const char *key, int type, void *val)
367 if (prefix == NULL) {
368 connman_dbus_dict_append_variant(iter, key, type, val);
372 str = g_strdup_printf("%s%s", prefix, key);
374 connman_dbus_dict_append_variant(iter, str, type, val);
379 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
380 DBusMessageIter *iter, const char *prefix)
384 str = __connman_ipconfig_method2string(ipconfig->method);
388 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
391 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
392 const char *key, DBusMessageIter *value)
394 int type = dbus_message_iter_get_arg_type(value);
396 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
398 if (g_strcmp0(key, "Method") == 0) {
401 if (type != DBUS_TYPE_STRING)
404 dbus_message_iter_get_basic(value, &method);
406 ipconfig->method = __connman_ipconfig_string2method(method);
413 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
414 GKeyFile *keyfile, const char *identifier, const char *prefix)
416 DBG("ipconfig %p identifier %s", ipconfig, identifier);
421 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
422 GKeyFile *keyfile, const char *identifier, const char *prefix)
424 DBG("ipconfig %p identifier %s", ipconfig, identifier);
429 static GSList *driver_list = NULL;
431 static gint compare_priority(gconstpointer a, gconstpointer b)
433 const struct connman_ipconfig_driver *driver1 = a;
434 const struct connman_ipconfig_driver *driver2 = b;
436 return driver2->priority - driver1->priority;
440 * connman_ipconfig_driver_register:
441 * @driver: IP configuration driver
443 * Register a new IP configuration driver
445 * Returns: %0 on success
447 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
449 DBG("driver %p name %s", driver, driver->name);
451 driver_list = g_slist_insert_sorted(driver_list, driver,
458 * connman_ipconfig_driver_unregister:
459 * @driver: IP configuration driver
461 * Remove a previously registered IP configuration driver.
463 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
465 DBG("driver %p name %s", driver, driver->name);
467 driver_list = g_slist_remove(driver_list, driver);