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_ipconfig {
42 enum connman_ipconfig_method method;
46 * connman_ipconfig_create:
48 * Allocate a new ipconfig structure.
50 * Returns: a newly-allocated #connman_ipconfig structure
52 struct connman_ipconfig *connman_ipconfig_create(int index)
54 struct connman_ipconfig *ipconfig;
58 ipconfig = g_try_new0(struct connman_ipconfig, 1);
62 ipconfig->refcount = 1;
64 ipconfig->index = index;
65 ipconfig->interface = connman_inet_ifname(index);
67 DBG("ipconfig %p", ipconfig);
69 connman_info("%s {create} index %d", ipconfig->interface,
76 * connman_ipconfig_ref:
77 * @ipconfig: ipconfig structure
79 * Increase reference counter of ipconfig
81 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
83 g_atomic_int_inc(&ipconfig->refcount);
89 * connman_ipconfig_unref:
90 * @ipconfig: ipconfig structure
92 * Decrease reference counter of ipconfig
94 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
96 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
97 connman_info("%s {remove} index %d", ipconfig->interface,
100 g_free(ipconfig->interface);
106 * connman_ipconfig_set_method:
107 * @ipconfig: ipconfig structure
108 * @method: configuration method
110 * Set the configuration method
112 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
113 enum connman_ipconfig_method method)
115 ipconfig->method = method;
120 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
122 return ipconfig->index;
125 unsigned short __connman_ipconfig_get_type(struct connman_ipconfig *ipconfig)
127 return ipconfig->type;
130 unsigned int __connman_ipconfig_get_flags(struct connman_ipconfig *ipconfig)
132 return ipconfig->flags;
135 void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
136 unsigned flags, unsigned change)
140 if (flags == ipconfig->flags)
143 ipconfig->flags = flags;
145 str = g_string_new(NULL);
150 g_string_append(str, "UP");
152 g_string_append(str, "DOWN");
154 if (flags & IFF_RUNNING)
155 g_string_append(str, ",RUNNING");
157 if (flags & IFF_LOWER_UP)
158 g_string_append(str, ",LOWER_UP");
160 connman_info("%s {update} flags %u change %u <%s>",
161 ipconfig->interface, flags, change, str->str);
163 g_string_free(str, TRUE);
166 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
167 const char *label, unsigned char prefixlen,
168 const char *address, const char *broadcast)
170 connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
171 address, prefixlen, label);
174 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
175 const char *label, unsigned char prefixlen,
176 const char *address, const char *broadcast)
178 connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
179 address, prefixlen, label);
182 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
185 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
187 case CONNMAN_IPCONFIG_METHOD_OFF:
189 case CONNMAN_IPCONFIG_METHOD_STATIC:
191 case CONNMAN_IPCONFIG_METHOD_DHCP:
198 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
200 if (g_strcmp0(method, "off") == 0)
201 return CONNMAN_IPCONFIG_METHOD_OFF;
202 else if (g_strcmp0(method, "static") == 0)
203 return CONNMAN_IPCONFIG_METHOD_STATIC;
204 else if (g_strcmp0(method, "dhcp") == 0)
205 return CONNMAN_IPCONFIG_METHOD_DHCP;
207 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
210 static void append_variant(DBusMessageIter *iter, const char *prefix,
211 const char *key, int type, void *val)
215 if (prefix == NULL) {
216 connman_dbus_dict_append_variant(iter, key, type, val);
220 str = g_strdup_printf("%s%s", prefix, key);
222 connman_dbus_dict_append_variant(iter, str, type, val);
227 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
228 DBusMessageIter *iter, const char *prefix)
232 str = __connman_ipconfig_method2string(ipconfig->method);
236 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
239 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
240 const char *key, DBusMessageIter *value)
242 int type = dbus_message_iter_get_arg_type(value);
244 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
246 if (g_strcmp0(key, "Method") == 0) {
249 if (type != DBUS_TYPE_STRING)
252 dbus_message_iter_get_basic(value, &method);
254 ipconfig->method = __connman_ipconfig_string2method(method);
261 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
262 GKeyFile *keyfile, const char *identifier, const char *prefix)
264 DBG("ipconfig %p identifier %s", ipconfig, identifier);
269 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
270 GKeyFile *keyfile, const char *identifier, const char *prefix)
272 DBG("ipconfig %p identifier %s", ipconfig, identifier);
277 static GSList *driver_list = NULL;
279 static gint compare_priority(gconstpointer a, gconstpointer b)
281 const struct connman_ipconfig_driver *driver1 = a;
282 const struct connman_ipconfig_driver *driver2 = b;
284 return driver2->priority - driver1->priority;
288 * connman_ipconfig_driver_register:
289 * @driver: IP configuration driver
291 * Register a new IP configuration driver
293 * Returns: %0 on success
295 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
297 DBG("driver %p name %s", driver, driver->name);
299 driver_list = g_slist_insert_sorted(driver_list, driver,
306 * connman_ipconfig_driver_unregister:
307 * @driver: IP configuration driver
309 * Remove a previously registered IP configuration driver.
311 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
313 DBG("driver %p name %s", driver, driver->name);
315 driver_list = g_slist_remove(driver_list, driver);