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;
51 static void free_address_list(struct connman_ipconfig *ipconfig)
55 for (list = ipconfig->address_list; list; list = list->next) {
56 struct connman_ipaddress *ipaddress = list->data;
58 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 free_address_list(ipconfig);
141 g_free(ipconfig->interface);
147 * connman_ipconfig_set_method:
148 * @ipconfig: ipconfig structure
149 * @method: configuration method
151 * Set the configuration method
153 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
154 enum connman_ipconfig_method method)
156 ipconfig->method = method;
161 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
163 return ipconfig->index;
166 unsigned short __connman_ipconfig_get_type(struct connman_ipconfig *ipconfig)
168 return ipconfig->type;
171 unsigned int __connman_ipconfig_get_flags(struct connman_ipconfig *ipconfig)
173 return ipconfig->flags;
176 void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
177 unsigned flags, unsigned change)
181 if (flags == ipconfig->flags)
184 ipconfig->flags = flags;
186 str = g_string_new(NULL);
191 g_string_append(str, "UP");
193 g_string_append(str, "DOWN");
195 if (flags & IFF_RUNNING)
196 g_string_append(str, ",RUNNING");
198 if (flags & IFF_LOWER_UP)
199 g_string_append(str, ",LOWER_UP");
201 connman_info("%s {update} flags %u change %u <%s>",
202 ipconfig->interface, flags, change, str->str);
204 g_string_free(str, TRUE);
207 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
208 const char *label, unsigned char prefixlen,
209 const char *address, const char *broadcast)
211 struct connman_ipaddress *ipaddress;
213 ipaddress = g_try_new0(struct connman_ipaddress, 1);
214 if (ipaddress == NULL)
217 ipaddress->prefixlen = prefixlen;
218 ipaddress->address = g_strdup(address);
220 ipconfig->address_list = g_slist_append(ipconfig->address_list,
223 connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
224 address, prefixlen, label);
227 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
228 const char *label, unsigned char prefixlen,
229 const char *address, const char *broadcast)
231 struct connman_ipaddress *ipaddress;
233 ipaddress = find_ipaddress(ipconfig, prefixlen, address);
234 if (ipaddress == NULL)
237 ipconfig->address_list = g_slist_remove(ipconfig->address_list,
240 g_free(ipaddress->address);
243 connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
244 address, prefixlen, label);
247 static const char *scope2str(unsigned char scope)
259 void __connman_ipconfig_add_route(struct connman_ipconfig *ipconfig,
260 unsigned char scope, const char *destination,
263 connman_info("%s {add} route %s gw %s scope %u <%s>",
264 ipconfig->interface, destination,
265 gateway, scope, scope2str(scope));
268 void __connman_ipconfig_del_route(struct connman_ipconfig *ipconfig,
269 unsigned char scope, const char *destination,
272 connman_info("%s {del} route %s gw %s scope %u <%s>",
273 ipconfig->interface, destination,
274 gateway, scope, scope2str(scope));
277 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
280 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
282 case CONNMAN_IPCONFIG_METHOD_OFF:
284 case CONNMAN_IPCONFIG_METHOD_STATIC:
286 case CONNMAN_IPCONFIG_METHOD_DHCP:
293 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
295 if (g_strcmp0(method, "off") == 0)
296 return CONNMAN_IPCONFIG_METHOD_OFF;
297 else if (g_strcmp0(method, "static") == 0)
298 return CONNMAN_IPCONFIG_METHOD_STATIC;
299 else if (g_strcmp0(method, "dhcp") == 0)
300 return CONNMAN_IPCONFIG_METHOD_DHCP;
302 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
305 static void append_variant(DBusMessageIter *iter, const char *prefix,
306 const char *key, int type, void *val)
310 if (prefix == NULL) {
311 connman_dbus_dict_append_variant(iter, key, type, val);
315 str = g_strdup_printf("%s%s", prefix, key);
317 connman_dbus_dict_append_variant(iter, str, type, val);
322 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
323 DBusMessageIter *iter, const char *prefix)
327 str = __connman_ipconfig_method2string(ipconfig->method);
331 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
334 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
335 const char *key, DBusMessageIter *value)
337 int type = dbus_message_iter_get_arg_type(value);
339 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
341 if (g_strcmp0(key, "Method") == 0) {
344 if (type != DBUS_TYPE_STRING)
347 dbus_message_iter_get_basic(value, &method);
349 ipconfig->method = __connman_ipconfig_string2method(method);
356 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
357 GKeyFile *keyfile, const char *identifier, const char *prefix)
359 DBG("ipconfig %p identifier %s", ipconfig, identifier);
364 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
365 GKeyFile *keyfile, const char *identifier, const char *prefix)
367 DBG("ipconfig %p identifier %s", ipconfig, identifier);
372 static GSList *driver_list = NULL;
374 static gint compare_priority(gconstpointer a, gconstpointer b)
376 const struct connman_ipconfig_driver *driver1 = a;
377 const struct connman_ipconfig_driver *driver2 = b;
379 return driver2->priority - driver1->priority;
383 * connman_ipconfig_driver_register:
384 * @driver: IP configuration driver
386 * Register a new IP configuration driver
388 * Returns: %0 on success
390 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
392 DBG("driver %p name %s", driver, driver->name);
394 driver_list = g_slist_insert_sorted(driver_list, driver,
401 * connman_ipconfig_driver_unregister:
402 * @driver: IP configuration driver
404 * Remove a previously registered IP configuration driver.
406 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
408 DBG("driver %p name %s", driver, driver->name);
410 driver_list = g_slist_remove(driver_list, driver);