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);
62 g_slist_free(ipconfig->address_list);
63 ipconfig->address_list = NULL;
66 static struct connman_ipaddress *find_ipaddress(struct connman_ipconfig *ipconfig,
67 unsigned char prefixlen, const char *address)
71 for (list = ipconfig->address_list; list; list = list->next) {
72 struct connman_ipaddress *ipaddress = list->data;
74 if (g_strcmp0(ipaddress->address, address) == 0 &&
75 ipaddress->prefixlen == prefixlen)
83 * connman_ipconfig_create:
85 * Allocate a new ipconfig structure.
87 * Returns: a newly-allocated #connman_ipconfig structure
89 struct connman_ipconfig *connman_ipconfig_create(int index)
91 struct connman_ipconfig *ipconfig;
95 ipconfig = g_try_new0(struct connman_ipconfig, 1);
99 ipconfig->refcount = 1;
101 ipconfig->index = index;
102 ipconfig->interface = connman_inet_ifname(index);
104 DBG("ipconfig %p", ipconfig);
106 connman_info("%s {create} index %d", ipconfig->interface,
113 * connman_ipconfig_ref:
114 * @ipconfig: ipconfig structure
116 * Increase reference counter of ipconfig
118 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
120 g_atomic_int_inc(&ipconfig->refcount);
126 * connman_ipconfig_unref:
127 * @ipconfig: ipconfig structure
129 * Decrease reference counter of ipconfig
131 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
133 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
134 connman_info("%s {remove} index %d", ipconfig->interface,
137 free_address_list(ipconfig);
139 g_free(ipconfig->interface);
145 * connman_ipconfig_set_method:
146 * @ipconfig: ipconfig structure
147 * @method: configuration method
149 * Set the configuration method
151 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
152 enum connman_ipconfig_method method)
154 ipconfig->method = method;
159 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
161 return ipconfig->index;
164 unsigned short __connman_ipconfig_get_type(struct connman_ipconfig *ipconfig)
166 return ipconfig->type;
169 unsigned int __connman_ipconfig_get_flags(struct connman_ipconfig *ipconfig)
171 return ipconfig->flags;
174 void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
175 unsigned flags, unsigned change)
179 if (flags == ipconfig->flags)
182 ipconfig->flags = flags;
184 str = g_string_new(NULL);
189 g_string_append(str, "UP");
191 g_string_append(str, "DOWN");
193 if (flags & IFF_RUNNING)
194 g_string_append(str, ",RUNNING");
196 if (flags & IFF_LOWER_UP)
197 g_string_append(str, ",LOWER_UP");
199 connman_info("%s {update} flags %u change %u <%s>",
200 ipconfig->interface, flags, change, str->str);
202 g_string_free(str, TRUE);
205 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
206 const char *label, unsigned char prefixlen,
207 const char *address, const char *broadcast)
209 struct connman_ipaddress *ipaddress;
211 ipaddress = g_try_new0(struct connman_ipaddress, 1);
212 if (ipaddress == NULL)
215 ipaddress->prefixlen = prefixlen;
216 ipaddress->address = g_strdup(address);
218 ipconfig->address_list = g_slist_append(ipconfig->address_list,
221 connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
222 address, prefixlen, label);
225 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
226 const char *label, unsigned char prefixlen,
227 const char *address, const char *broadcast)
229 struct connman_ipaddress *ipaddress;
231 ipaddress = find_ipaddress(ipconfig, prefixlen, address);
232 if (ipaddress == NULL)
235 ipconfig->address_list = g_slist_remove(ipconfig->address_list,
238 g_free(ipaddress->address);
241 connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
242 address, prefixlen, label);
245 static const char *scope2str(unsigned char scope)
257 void __connman_ipconfig_add_route(struct connman_ipconfig *ipconfig,
258 unsigned char scope, const char *destination,
261 connman_info("%s {add} route %s gw %s scope %u <%s>",
262 ipconfig->interface, destination,
263 gateway, scope, scope2str(scope));
266 void __connman_ipconfig_del_route(struct connman_ipconfig *ipconfig,
267 unsigned char scope, const char *destination,
270 connman_info("%s {add} route %s gw %s scope %u <%s>",
271 ipconfig->interface, destination,
272 gateway, scope, scope2str(scope));
275 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
278 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
280 case CONNMAN_IPCONFIG_METHOD_OFF:
282 case CONNMAN_IPCONFIG_METHOD_STATIC:
284 case CONNMAN_IPCONFIG_METHOD_DHCP:
291 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
293 if (g_strcmp0(method, "off") == 0)
294 return CONNMAN_IPCONFIG_METHOD_OFF;
295 else if (g_strcmp0(method, "static") == 0)
296 return CONNMAN_IPCONFIG_METHOD_STATIC;
297 else if (g_strcmp0(method, "dhcp") == 0)
298 return CONNMAN_IPCONFIG_METHOD_DHCP;
300 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
303 static void append_variant(DBusMessageIter *iter, const char *prefix,
304 const char *key, int type, void *val)
308 if (prefix == NULL) {
309 connman_dbus_dict_append_variant(iter, key, type, val);
313 str = g_strdup_printf("%s%s", prefix, key);
315 connman_dbus_dict_append_variant(iter, str, type, val);
320 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
321 DBusMessageIter *iter, const char *prefix)
325 str = __connman_ipconfig_method2string(ipconfig->method);
329 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
332 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
333 const char *key, DBusMessageIter *value)
335 int type = dbus_message_iter_get_arg_type(value);
337 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
339 if (g_strcmp0(key, "Method") == 0) {
342 if (type != DBUS_TYPE_STRING)
345 dbus_message_iter_get_basic(value, &method);
347 ipconfig->method = __connman_ipconfig_string2method(method);
354 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
355 GKeyFile *keyfile, const char *identifier, const char *prefix)
357 DBG("ipconfig %p identifier %s", ipconfig, identifier);
362 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
363 GKeyFile *keyfile, const char *identifier, const char *prefix)
365 DBG("ipconfig %p identifier %s", ipconfig, identifier);
370 static GSList *driver_list = NULL;
372 static gint compare_priority(gconstpointer a, gconstpointer b)
374 const struct connman_ipconfig_driver *driver1 = a;
375 const struct connman_ipconfig_driver *driver2 = b;
377 return driver2->priority - driver1->priority;
381 * connman_ipconfig_driver_register:
382 * @driver: IP configuration driver
384 * Register a new IP configuration driver
386 * Returns: %0 on success
388 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
390 DBG("driver %p name %s", driver, driver->name);
392 driver_list = g_slist_insert_sorted(driver_list, driver,
399 * connman_ipconfig_driver_unregister:
400 * @driver: IP configuration driver
402 * Remove a previously registered IP configuration driver.
404 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
406 DBG("driver %p name %s", driver, driver->name);
408 driver_list = g_slist_remove(driver_list, driver);