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)
101 if (g_strcmp0(key, "Address") == 0) {
102 g_free(dhcp->element->ipv4.address);
103 dhcp->element->ipv4.address = g_strdup(value);
104 } else if (g_strcmp0(key, "Netmask") == 0) {
105 g_free(dhcp->element->ipv4.netmask);
106 dhcp->element->ipv4.netmask = g_strdup(value);
107 } else if (g_strcmp0(key, "Gateway") == 0) {
108 g_free(dhcp->element->ipv4.gateway);
109 dhcp->element->ipv4.gateway = g_strdup(value);
110 } else if (g_strcmp0(key, "Network") == 0) {
111 g_free(dhcp->element->ipv4.network);
112 dhcp->element->ipv4.network = g_strdup(value);
113 } else if (g_strcmp0(key, "Broadcast") == 0) {
114 g_free(dhcp->element->ipv4.broadcast);
115 dhcp->element->ipv4.broadcast = g_strdup(value);
116 } else if (g_strcmp0(key, "Nameserver") == 0) {
117 g_free(dhcp->element->ipv4.nameserver);
118 nameservers = g_strsplit_set(value, " ", 0);
119 /* FIXME: The ipv4 structure can only hold one nameserver, so
120 * we are only able to pass along the first nameserver sent by
121 * the DHCP server. If this situation changes, we should
122 * retain all of them.
124 dhcp->element->ipv4.nameserver = g_strdup(nameservers[0]);
125 g_strfreev(nameservers);
126 } else if (g_strcmp0(key, "Domainname") == 0) {
127 g_free(dhcp->element->domainname);
128 dhcp->element->domainname = g_strdup(value);
129 __connman_utsname_set_domainname(value);
130 } else if (g_strcmp0(key, "Hostname") == 0) {
131 g_free(dhcp->element->hostname);
132 dhcp->element->hostname = g_strdup(value);
133 __connman_utsname_set_hostname(value);
134 } else if (g_strcmp0(key, "Timeserver") == 0) {
135 g_free(dhcp->element->ipv4.timeserver);
136 dhcp->element->ipv4.timeserver = g_strdup(value);
137 } else if (g_strcmp0(key, "MTU") == 0) {
138 } else if (g_strcmp0(key, "PAC") == 0) {
139 connman_info("PAC configuration %s", value);
141 g_free(dhcp->element->ipv4.pac);
142 dhcp->element->ipv4.pac = g_strdup(value);
147 * connman_dhcp_bound:
148 * @dhcp: DHCP structure
150 * Report successful bound of the interface
152 void connman_dhcp_bound(struct connman_dhcp *dhcp)
154 struct connman_element *element;
156 DBG("dhcp %p", dhcp);
158 element = connman_element_create(NULL);
162 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
163 element->index = dhcp->index;
165 connman_element_update(dhcp->element);
167 if (connman_element_register(element, dhcp->element) < 0)
168 connman_element_unref(element);
172 * connman_dhcp_renew:
173 * @dhcp: DHCP structure
175 * Report successful renew of the interface
177 void connman_dhcp_renew(struct connman_dhcp *dhcp)
179 DBG("dhcp %p", dhcp);
181 connman_element_update(dhcp->element);
186 * @dhcp: DHCP structure
188 * Report DHCP failure of the interface
190 void connman_dhcp_fail(struct connman_dhcp *dhcp)
192 DBG("dhcp %p", dhcp);
194 connman_element_set_error(dhcp->element,
195 CONNMAN_ELEMENT_ERROR_FAILED);
199 * connman_dhcp_get_data:
200 * @dhcp: DHCP structure
202 * Get private DHCP data pointer
204 void *connman_dhcp_get_data(struct connman_dhcp *dhcp)
206 return dhcp->driver_data;
210 * connman_dhcp_set_data:
211 * @dhcp: DHCP structure
212 * @data: data pointer
214 * Set private DHCP data pointer
216 void connman_dhcp_set_data(struct connman_dhcp *dhcp, void *data)
218 dhcp->driver_data = data;
221 static GSList *driver_list = NULL;
223 static gint compare_priority(gconstpointer a, gconstpointer b)
225 const struct connman_dhcp_driver *driver1 = a;
226 const struct connman_dhcp_driver *driver2 = b;
228 return driver2->priority - driver1->priority;
232 * connman_dhcp_driver_register:
233 * @driver: DHCP driver definition
235 * Register a new DHCP driver
237 * Returns: %0 on success
239 int connman_dhcp_driver_register(struct connman_dhcp_driver *driver)
241 DBG("driver %p name %s", driver, driver->name);
243 driver_list = g_slist_insert_sorted(driver_list, driver,
250 * connman_dhcp_driver_unregister:
251 * @driver: DHCP driver definition
253 * Remove a previously registered DHCP driver
255 void connman_dhcp_driver_unregister(struct connman_dhcp_driver *driver)
257 DBG("driver %p name %s", driver, driver->name);
259 driver_list = g_slist_remove(driver_list, driver);
262 static int dhcp_probe(struct connman_element *element)
264 struct connman_dhcp *dhcp;
267 DBG("element %p name %s", element, element->name);
269 dhcp = g_try_new0(struct connman_dhcp, 1);
274 dhcp->index = element->index;
275 dhcp->state = CONNMAN_DHCP_STATE_IDLE;
277 dhcp->element = element;
279 connman_element_set_data(element, dhcp);
281 for (list = driver_list; list; list = list->next) {
282 struct connman_dhcp_driver *driver = list->data;
284 DBG("driver %p name %s", driver, driver->name);
286 if (driver->request(dhcp) == 0) {
287 dhcp->driver = driver;
292 if (dhcp->driver == NULL) {
293 connman_dhcp_unref(dhcp);
300 static void dhcp_remove(struct connman_element *element)
302 struct connman_dhcp *dhcp = connman_element_get_data(element);
304 DBG("element %p name %s", element, element->name);
306 connman_element_set_data(element, NULL);
309 dhcp->driver->release(dhcp);
313 connman_dhcp_unref(dhcp);
316 static void dhcp_change(struct connman_element *element)
318 DBG("element %p name %s", element, element->name);
320 if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
321 connman_element_set_error(element->parent,
322 CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
325 static struct connman_driver dhcp_driver = {
327 .type = CONNMAN_ELEMENT_TYPE_DHCP,
328 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
330 .remove = dhcp_remove,
331 .change = dhcp_change,
334 int __connman_dhcp_init(void)
336 return connman_driver_register(&dhcp_driver);
339 void __connman_dhcp_cleanup(void)
341 connman_driver_unregister(&dhcp_driver);