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
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_interface:
68 * @dhcp: DHCP structure
70 * Get network interface of DHCP
72 char *connman_dhcp_get_interface(struct connman_dhcp *dhcp)
74 return connman_inet_ifname(dhcp->index);
79 * @dhcp: DHCP structure
81 * Report successful bound of the interface
83 void connman_dhcp_bound(struct connman_dhcp *dhcp)
88 static GSList *driver_list = NULL;
90 static gint compare_priority(gconstpointer a, gconstpointer b)
92 const struct connman_dhcp_driver *driver1 = a;
93 const struct connman_dhcp_driver *driver2 = b;
95 return driver2->priority - driver1->priority;
99 * connman_dhcp_driver_register:
100 * @driver: DHCP driver definition
102 * Register a new DHCP driver
104 * Returns: %0 on success
106 int connman_dhcp_driver_register(struct connman_dhcp_driver *driver)
108 DBG("driver %p name %s", driver, driver->name);
110 driver_list = g_slist_insert_sorted(driver_list, driver,
117 * connman_dhcp_driver_unregister:
118 * @driver: DHCP driver definition
120 * Remove a previously registered DHCP driver
122 void connman_dhcp_driver_unregister(struct connman_dhcp_driver *driver)
124 DBG("driver %p name %s", driver, driver->name);
126 driver_list = g_slist_remove(driver_list, driver);
129 static int dhcp_probe(struct connman_element *element)
131 struct connman_dhcp *dhcp;
134 DBG("element %p name %s", element, element->name);
136 dhcp = g_try_new0(struct connman_dhcp, 1);
141 dhcp->index = element->index;
142 dhcp->state = CONNMAN_DHCP_STATE_IDLE;
144 dhcp->element = element;
146 connman_element_set_data(element, dhcp);
148 for (list = driver_list; list; list = list->next) {
149 struct connman_dhcp_driver *driver = list->data;
151 DBG("driver %p name %s", driver, driver->name);
153 if (driver->request(dhcp) == 0) {
154 dhcp->driver = driver;
159 if (dhcp->driver == NULL) {
160 connman_dhcp_unref(dhcp);
167 static void dhcp_remove(struct connman_element *element)
169 struct connman_dhcp *dhcp = connman_element_get_data(element);
171 DBG("element %p name %s", element, element->name);
173 connman_element_set_data(element, NULL);
176 dhcp->driver->release(dhcp);
180 connman_dhcp_unref(dhcp);
183 static struct connman_driver dhcp_driver = {
185 .type = CONNMAN_ELEMENT_TYPE_DHCP,
186 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
188 .remove = dhcp_remove,
191 int __connman_dhcp_init(void)
193 return connman_driver_register(&dhcp_driver);
196 void __connman_dhcp_cleanup(void)
198 connman_driver_unregister(&dhcp_driver);