5 * Copyright (C) 2007-2010 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
33 enum connman_dhcp_state state;
35 struct connman_element *element;
37 struct connman_dhcp_driver *driver;
43 * @dhcp: DHCP structure
45 * Increase reference counter of DHCP
47 struct connman_dhcp *connman_dhcp_ref(struct connman_dhcp *dhcp)
49 g_atomic_int_inc(&dhcp->refcount);
56 * @dhcp: DHCP structure
58 * Decrease reference counter of DHCP
60 void connman_dhcp_unref(struct connman_dhcp *dhcp)
62 if (g_atomic_int_dec_and_test(&dhcp->refcount) == TRUE)
67 * connman_dhcp_get_index:
68 * @dhcp: DHCP structure
70 * Get network index of DHCP
72 int connman_dhcp_get_index(struct connman_dhcp *dhcp)
78 * connman_dhcp_get_interface:
79 * @dhcp: DHCP structure
81 * Get network interface of DHCP
83 char *connman_dhcp_get_interface(struct connman_dhcp *dhcp)
85 return connman_inet_ifname(dhcp->index);
89 * connman_dhcp_set_value:
90 * @dhcp: DHCP structure
91 * @key: unique identifier
92 * @value: string value
94 * Set string value for specific key
96 void connman_dhcp_set_value(struct connman_dhcp *dhcp,
97 const char *key, const char *value)
99 if (g_strcmp0(key, "Address") == 0) {
100 g_free(dhcp->element->ipv4.address);
101 dhcp->element->ipv4.address = g_strdup(value);
102 } else if (g_strcmp0(key, "Netmask") == 0) {
103 g_free(dhcp->element->ipv4.netmask);
104 dhcp->element->ipv4.netmask = g_strdup(value);
105 } else if (g_strcmp0(key, "Gateway") == 0) {
106 g_free(dhcp->element->ipv4.gateway);
107 dhcp->element->ipv4.gateway = g_strdup(value);
108 } else if (g_strcmp0(key, "Network") == 0) {
109 g_free(dhcp->element->ipv4.network);
110 dhcp->element->ipv4.network = g_strdup(value);
111 } else if (g_strcmp0(key, "Broadcast") == 0) {
112 g_free(dhcp->element->ipv4.broadcast);
113 dhcp->element->ipv4.broadcast = g_strdup(value);
114 } else if (g_strcmp0(key, "Nameserver") == 0) {
115 g_free(dhcp->element->ipv4.nameserver);
116 dhcp->element->ipv4.nameserver = g_strdup(value);
117 } else if (g_strcmp0(key, "Domainname") == 0) {
118 __connman_utsname_set_domainname(value);
119 } else if (g_strcmp0(key, "Hostname") == 0) {
120 __connman_utsname_set_hostname(value);
121 } else if (g_strcmp0(key, "Timeserver") == 0) {
122 g_free(dhcp->element->ipv4.timeserver);
123 dhcp->element->ipv4.timeserver = g_strdup(value);
124 } else if (g_strcmp0(key, "MTU") == 0) {
125 } else if (g_strcmp0(key, "PAC") == 0) {
126 connman_info("PAC configuration %s", value);
128 g_free(dhcp->element->ipv4.pac);
129 dhcp->element->ipv4.pac = g_strdup(value);
134 * connman_dhcp_bound:
135 * @dhcp: DHCP structure
137 * Report successful bound of the interface
139 void connman_dhcp_bound(struct connman_dhcp *dhcp)
141 struct connman_element *element;
143 DBG("dhcp %p", dhcp);
145 element = connman_element_create(NULL);
149 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
150 element->index = dhcp->index;
152 connman_element_update(dhcp->element);
154 if (connman_element_register(element, dhcp->element) < 0)
155 connman_element_unref(element);
159 * connman_dhcp_renew:
160 * @dhcp: DHCP structure
162 * Report successful renew of the interface
164 void connman_dhcp_renew(struct connman_dhcp *dhcp)
166 DBG("dhcp %p", dhcp);
168 connman_element_update(dhcp->element);
173 * @dhcp: DHCP structure
175 * Report DHCP failure of the interface
177 void connman_dhcp_fail(struct connman_dhcp *dhcp)
179 DBG("dhcp %p", dhcp);
181 connman_element_set_error(dhcp->element,
182 CONNMAN_ELEMENT_ERROR_FAILED);
186 * connman_dhcp_get_data:
187 * @dhcp: DHCP structure
189 * Get private DHCP data pointer
191 void *connman_dhcp_get_data(struct connman_dhcp *dhcp)
193 return dhcp->driver_data;
197 * connman_dhcp_set_data:
198 * @dhcp: DHCP structure
199 * @data: data pointer
201 * Set private DHCP data pointer
203 void connman_dhcp_set_data(struct connman_dhcp *dhcp, void *data)
205 dhcp->driver_data = data;
208 static GSList *driver_list = NULL;
210 static gint compare_priority(gconstpointer a, gconstpointer b)
212 const struct connman_dhcp_driver *driver1 = a;
213 const struct connman_dhcp_driver *driver2 = b;
215 return driver2->priority - driver1->priority;
219 * connman_dhcp_driver_register:
220 * @driver: DHCP driver definition
222 * Register a new DHCP driver
224 * Returns: %0 on success
226 int connman_dhcp_driver_register(struct connman_dhcp_driver *driver)
228 DBG("driver %p name %s", driver, driver->name);
230 driver_list = g_slist_insert_sorted(driver_list, driver,
237 * connman_dhcp_driver_unregister:
238 * @driver: DHCP driver definition
240 * Remove a previously registered DHCP driver
242 void connman_dhcp_driver_unregister(struct connman_dhcp_driver *driver)
244 DBG("driver %p name %s", driver, driver->name);
246 driver_list = g_slist_remove(driver_list, driver);
249 static int dhcp_probe(struct connman_element *element)
251 struct connman_dhcp *dhcp;
254 DBG("element %p name %s", element, element->name);
256 dhcp = g_try_new0(struct connman_dhcp, 1);
261 dhcp->index = element->index;
262 dhcp->state = CONNMAN_DHCP_STATE_IDLE;
264 dhcp->element = element;
266 connman_element_set_data(element, dhcp);
268 for (list = driver_list; list; list = list->next) {
269 struct connman_dhcp_driver *driver = list->data;
271 DBG("driver %p name %s", driver, driver->name);
273 if (driver->request(dhcp) == 0) {
274 dhcp->driver = driver;
279 if (dhcp->driver == NULL) {
280 connman_dhcp_unref(dhcp);
287 static void dhcp_remove(struct connman_element *element)
289 struct connman_dhcp *dhcp = connman_element_get_data(element);
291 DBG("element %p name %s", element, element->name);
293 connman_element_set_data(element, NULL);
296 dhcp->driver->release(dhcp);
300 connman_dhcp_unref(dhcp);
303 static void dhcp_change(struct connman_element *element)
305 DBG("element %p name %s", element, element->name);
307 if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
308 connman_element_set_error(element->parent,
309 CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
312 static struct connman_driver dhcp_driver = {
314 .type = CONNMAN_ELEMENT_TYPE_DHCP,
315 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
317 .remove = dhcp_remove,
318 .change = dhcp_change,
321 int __connman_dhcp_init(void)
323 return connman_driver_register(&dhcp_driver);
326 void __connman_dhcp_cleanup(void)
328 connman_driver_unregister(&dhcp_driver);