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 {
47 enum connman_ipconfig_method method;
52 static void free_address_list(struct connman_ipconfig *ipconfig)
56 for (list = ipconfig->address_list; list; list = list->next) {
57 struct connman_ipaddress *ipaddress = list->data;
59 g_free(ipaddress->address);
64 g_slist_free(ipconfig->address_list);
65 ipconfig->address_list = NULL;
68 static struct connman_ipaddress *find_ipaddress(struct connman_ipconfig *ipconfig,
69 unsigned char prefixlen, const char *address)
73 for (list = ipconfig->address_list; list; list = list->next) {
74 struct connman_ipaddress *ipaddress = list->data;
76 if (g_strcmp0(ipaddress->address, address) == 0 &&
77 ipaddress->prefixlen == prefixlen)
85 * connman_ipconfig_create:
87 * Allocate a new ipconfig structure.
89 * Returns: a newly-allocated #connman_ipconfig structure
91 struct connman_ipconfig *connman_ipconfig_create(int index)
93 struct connman_ipconfig *ipconfig;
97 ipconfig = g_try_new0(struct connman_ipconfig, 1);
101 ipconfig->refcount = 1;
103 ipconfig->index = index;
104 ipconfig->interface = connman_inet_ifname(index);
106 DBG("ipconfig %p", ipconfig);
108 connman_info("%s {create} index %d", ipconfig->interface,
115 * connman_ipconfig_ref:
116 * @ipconfig: ipconfig structure
118 * Increase reference counter of ipconfig
120 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
122 g_atomic_int_inc(&ipconfig->refcount);
128 * connman_ipconfig_unref:
129 * @ipconfig: ipconfig structure
131 * Decrease reference counter of ipconfig
133 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
135 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
136 connman_info("%s {remove} index %d", ipconfig->interface,
139 g_free(ipconfig->gateway);
141 free_address_list(ipconfig);
143 g_free(ipconfig->interface);
149 * connman_ipconfig_set_method:
150 * @ipconfig: ipconfig structure
151 * @method: configuration method
153 * Set the configuration method
155 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
156 enum connman_ipconfig_method method)
158 ipconfig->method = method;
163 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
165 return ipconfig->index;
168 unsigned short __connman_ipconfig_get_type(struct connman_ipconfig *ipconfig)
170 return ipconfig->type;
173 unsigned int __connman_ipconfig_get_flags(struct connman_ipconfig *ipconfig)
175 return ipconfig->flags;
178 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
180 return ipconfig->gateway;
183 void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
184 unsigned flags, unsigned change)
188 if (flags == ipconfig->flags)
191 ipconfig->flags = flags;
193 str = g_string_new(NULL);
198 g_string_append(str, "UP");
200 g_string_append(str, "DOWN");
202 if (flags & IFF_RUNNING)
203 g_string_append(str, ",RUNNING");
205 if (flags & IFF_LOWER_UP)
206 g_string_append(str, ",LOWER_UP");
208 connman_info("%s {update} flags %u change %u <%s>",
209 ipconfig->interface, flags, change, str->str);
211 g_string_free(str, TRUE);
214 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
215 const char *label, unsigned char prefixlen,
216 const char *address, const char *broadcast)
218 struct connman_ipaddress *ipaddress;
220 ipaddress = g_try_new0(struct connman_ipaddress, 1);
221 if (ipaddress == NULL)
224 ipaddress->prefixlen = prefixlen;
225 ipaddress->address = g_strdup(address);
227 ipconfig->address_list = g_slist_append(ipconfig->address_list,
230 connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
231 address, prefixlen, label);
234 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
235 const char *label, unsigned char prefixlen,
236 const char *address, const char *broadcast)
238 struct connman_ipaddress *ipaddress;
240 ipaddress = find_ipaddress(ipconfig, prefixlen, address);
241 if (ipaddress == NULL)
244 ipconfig->address_list = g_slist_remove(ipconfig->address_list,
247 g_free(ipaddress->address);
250 connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
251 address, prefixlen, label);
254 static const char *scope2str(unsigned char scope)
266 void __connman_ipconfig_add_route(struct connman_ipconfig *ipconfig,
267 unsigned char scope, const char *destination,
270 if (scope == 0 && g_strcmp0(destination, "0.0.0.0") == 0) {
271 g_free(ipconfig->gateway);
272 ipconfig->gateway = g_strdup(gateway);
275 connman_info("%s {add} route %s gw %s scope %u <%s>",
276 ipconfig->interface, destination,
277 gateway, scope, scope2str(scope));
280 void __connman_ipconfig_del_route(struct connman_ipconfig *ipconfig,
281 unsigned char scope, const char *destination,
284 if (scope == 0 && g_strcmp0(destination, "0.0.0.0") == 0) {
285 g_free(ipconfig->gateway);
286 ipconfig->gateway = NULL;
289 connman_info("%s {del} route %s gw %s scope %u <%s>",
290 ipconfig->interface, destination,
291 gateway, scope, scope2str(scope));
294 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
297 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
299 case CONNMAN_IPCONFIG_METHOD_OFF:
301 case CONNMAN_IPCONFIG_METHOD_STATIC:
303 case CONNMAN_IPCONFIG_METHOD_DHCP:
310 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
312 if (g_strcmp0(method, "off") == 0)
313 return CONNMAN_IPCONFIG_METHOD_OFF;
314 else if (g_strcmp0(method, "static") == 0)
315 return CONNMAN_IPCONFIG_METHOD_STATIC;
316 else if (g_strcmp0(method, "dhcp") == 0)
317 return CONNMAN_IPCONFIG_METHOD_DHCP;
319 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
322 static void append_variant(DBusMessageIter *iter, const char *prefix,
323 const char *key, int type, void *val)
327 if (prefix == NULL) {
328 connman_dbus_dict_append_variant(iter, key, type, val);
332 str = g_strdup_printf("%s%s", prefix, key);
334 connman_dbus_dict_append_variant(iter, str, type, val);
339 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
340 DBusMessageIter *iter, const char *prefix)
344 str = __connman_ipconfig_method2string(ipconfig->method);
348 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
351 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
352 const char *key, DBusMessageIter *value)
354 int type = dbus_message_iter_get_arg_type(value);
356 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
358 if (g_strcmp0(key, "Method") == 0) {
361 if (type != DBUS_TYPE_STRING)
364 dbus_message_iter_get_basic(value, &method);
366 ipconfig->method = __connman_ipconfig_string2method(method);
373 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
374 GKeyFile *keyfile, const char *identifier, const char *prefix)
376 DBG("ipconfig %p identifier %s", ipconfig, identifier);
381 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
382 GKeyFile *keyfile, const char *identifier, const char *prefix)
384 DBG("ipconfig %p identifier %s", ipconfig, identifier);
389 static GSList *driver_list = NULL;
391 static gint compare_priority(gconstpointer a, gconstpointer b)
393 const struct connman_ipconfig_driver *driver1 = a;
394 const struct connman_ipconfig_driver *driver2 = b;
396 return driver2->priority - driver1->priority;
400 * connman_ipconfig_driver_register:
401 * @driver: IP configuration driver
403 * Register a new IP configuration driver
405 * Returns: %0 on success
407 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
409 DBG("driver %p name %s", driver, driver->name);
411 driver_list = g_slist_insert_sorted(driver_list, driver,
418 * connman_ipconfig_driver_unregister:
419 * @driver: IP configuration driver
421 * Remove a previously registered IP configuration driver.
423 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
425 DBG("driver %p name %s", driver, driver->name);
427 driver_list = g_slist_remove(driver_list, driver);