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
30 struct connman_ipconfig {
34 enum connman_ipconfig_method method;
38 * connman_ipconfig_create:
40 * Allocate a new ipconfig structure.
42 * Returns: a newly-allocated #connman_ipconfig structure
44 struct connman_ipconfig *connman_ipconfig_create(unsigned int index)
46 struct connman_ipconfig *ipconfig;
50 ipconfig = g_try_new0(struct connman_ipconfig, 1);
54 ipconfig->index = index;
55 ipconfig->interface = connman_inet_ifname(index);
57 DBG("ipconfig %p", ipconfig);
59 __connman_rtnl_register_ipconfig(ipconfig);
65 * connman_ipconfig_ref:
66 * @ipconfig: ipconfig structure
68 * Increase reference counter of ipconfig
70 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
72 g_atomic_int_inc(&ipconfig->refcount);
78 * connman_ipconfig_unref:
79 * @ipconfig: ipconfig structure
81 * Decrease reference counter of ipconfig
83 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
85 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
86 __connman_rtnl_unregister_ipconfig(ipconfig);
88 g_free(ipconfig->interface);
94 * connman_ipconfig_set_method:
95 * @ipconfig: ipconfig structure
96 * @method: configuration method
98 * Set the configuration method
100 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
101 enum connman_ipconfig_method method)
103 ipconfig->method = method;
108 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
110 return ipconfig->index;
113 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
114 const char *label, unsigned int prefixlen,
115 const char *address, const char *broadcast)
117 connman_info("%s {add} address %s/%d label %s", ipconfig->interface,
118 address, prefixlen, label);
121 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
122 const char *label, unsigned int prefixlen,
123 const char *address, const char *broadcast)
125 connman_info("%s {del} address %s/%d label %s", ipconfig->interface,
126 address, prefixlen, label);
129 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
132 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
134 case CONNMAN_IPCONFIG_METHOD_OFF:
136 case CONNMAN_IPCONFIG_METHOD_STATIC:
138 case CONNMAN_IPCONFIG_METHOD_DHCP:
145 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
147 if (g_strcmp0(method, "off") == 0)
148 return CONNMAN_IPCONFIG_METHOD_OFF;
149 else if (g_strcmp0(method, "static") == 0)
150 return CONNMAN_IPCONFIG_METHOD_STATIC;
151 else if (g_strcmp0(method, "dhcp") == 0)
152 return CONNMAN_IPCONFIG_METHOD_DHCP;
154 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
157 static void append_variant(DBusMessageIter *iter, const char *prefix,
158 const char *key, int type, void *val)
162 if (prefix == NULL) {
163 connman_dbus_dict_append_variant(iter, key, type, val);
167 str = g_strdup_printf("%s%s", prefix, key);
169 connman_dbus_dict_append_variant(iter, str, type, val);
174 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
175 DBusMessageIter *iter, const char *prefix)
179 str = __connman_ipconfig_method2string(ipconfig->method);
183 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
186 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
187 const char *key, DBusMessageIter *value)
189 int type = dbus_message_iter_get_arg_type(value);
191 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
193 if (g_strcmp0(key, "Method") == 0) {
196 if (type != DBUS_TYPE_STRING)
199 dbus_message_iter_get_basic(value, &method);
201 ipconfig->method = __connman_ipconfig_string2method(method);
208 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
209 GKeyFile *keyfile, const char *identifier, const char *prefix)
211 DBG("ipconfig %p identifier %s", ipconfig, identifier);
216 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
217 GKeyFile *keyfile, const char *identifier, const char *prefix)
219 DBG("ipconfig %p identifier %s", ipconfig, identifier);
224 static GSList *driver_list = NULL;
226 static gint compare_priority(gconstpointer a, gconstpointer b)
228 const struct connman_ipconfig_driver *driver1 = a;
229 const struct connman_ipconfig_driver *driver2 = b;
231 return driver2->priority - driver1->priority;
235 * connman_ipconfig_driver_register:
236 * @driver: IP configuration driver
238 * Register a new IP configuration driver
240 * Returns: %0 on success
242 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
244 DBG("driver %p name %s", driver, driver->name);
246 driver_list = g_slist_insert_sorted(driver_list, driver,
253 * connman_ipconfig_driver_unregister:
254 * @driver: IP configuration driver
256 * Remove a previously registered IP configuration driver.
258 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
260 DBG("driver %p name %s", driver, driver->name);
262 driver_list = g_slist_remove(driver_list, driver);