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
30 #include <connman/ipconfig.h>
32 #include <gdhcp/gdhcp.h>
39 struct connman_network *network;
46 GDHCPClient *dhcp_client;
49 static GHashTable *network_table;
51 static void dhcp_free(struct connman_dhcp *dhcp)
53 g_strfreev(dhcp->nameservers);
54 g_strfreev(dhcp->timeservers);
57 dhcp->nameservers = NULL;
58 dhcp->timeservers = NULL;
63 * dhcp_invalidate: Invalidate an existing DHCP lease
64 * @dhcp: pointer to the DHCP lease to invalidate.
65 * @callback: flag indicating whether or not to invoke the client callback
68 * Invalidates an existing DHCP lease, optionally invoking the client
69 * callback. The caller may wish to avoid the client callback invocation
70 * when the invocation of that callback might otherwise unnecessarily upset
71 * service state due to the IP configuration change implied by this
74 static void dhcp_invalidate(struct connman_dhcp *dhcp, connman_bool_t callback)
76 struct connman_service *service;
77 struct connman_ipconfig *ipconfig;
80 DBG("dhcp %p callback %u", dhcp, callback);
85 service = __connman_service_lookup_from_network(dhcp->network);
89 ipconfig = __connman_service_get_ip4config(service);
93 __connman_6to4_remove(ipconfig);
95 __connman_service_set_domainname(service, NULL);
96 __connman_service_set_pac(service, NULL);
98 if (dhcp->timeservers != NULL) {
99 for (i = 0; dhcp->timeservers[i] != NULL; i++) {
100 __connman_service_timeserver_remove(service,
101 dhcp->timeservers[i]);
105 if (dhcp->nameservers != NULL) {
106 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
107 __connman_service_nameserver_remove(service,
108 dhcp->nameservers[i], FALSE);
112 __connman_ipconfig_set_dhcp_address(ipconfig,
113 __connman_ipconfig_get_local(ipconfig));
114 DBG("last address %s", __connman_ipconfig_get_dhcp_address(ipconfig));
116 __connman_ipconfig_address_remove(ipconfig);
118 __connman_ipconfig_set_local(ipconfig, NULL);
119 __connman_ipconfig_set_broadcast(ipconfig, NULL);
120 __connman_ipconfig_set_gateway(ipconfig, NULL);
121 __connman_ipconfig_set_prefixlen(ipconfig, 0);
123 if (dhcp->callback != NULL && callback)
124 dhcp->callback(dhcp->network, FALSE);
130 static void dhcp_valid(struct connman_dhcp *dhcp)
132 if (dhcp->callback != NULL)
133 dhcp->callback(dhcp->network, TRUE);
136 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
138 struct connman_dhcp *dhcp = user_data;
140 DBG("No lease available");
142 dhcp_invalidate(dhcp, TRUE);
145 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
147 struct connman_dhcp *dhcp = user_data;
151 dhcp_invalidate(dhcp, TRUE);
154 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
156 struct connman_dhcp *dhcp = user_data;
160 dhcp_invalidate(dhcp, TRUE);
164 static gboolean compare_string_arrays(char **array_a, char **array_b)
168 if (array_a == NULL || array_b == NULL)
171 if (g_strv_length(array_a) != g_strv_length(array_b))
174 for (i = 0; array_a[i] != NULL &&
175 array_b[i] != NULL; i++) {
176 if (g_strcmp0(array_a[i], array_b[i]) != 0)
183 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
185 struct connman_dhcp *dhcp = user_data;
186 GList *list, *option = NULL;
187 char *address, *netmask = NULL, *gateway = NULL;
188 const char *c_address, *c_gateway;
189 char *domainname = NULL, *hostname = NULL;
190 char **nameservers, **timeservers, *pac = NULL;
192 struct connman_ipconfig *ipconfig;
193 struct connman_service *service;
194 unsigned char prefixlen, c_prefixlen;
198 DBG("Lease available");
200 service = __connman_service_lookup_from_network(dhcp->network);
201 if (service == NULL) {
202 connman_error("Can not lookup service");
206 ipconfig = __connman_service_get_ip4config(service);
207 if (ipconfig == NULL) {
208 connman_error("Could not lookup ipconfig");
212 c_address = __connman_ipconfig_get_local(ipconfig);
213 c_gateway = __connman_ipconfig_get_gateway(ipconfig);
214 c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
216 address = g_dhcp_client_get_address(dhcp_client);
218 __connman_ipconfig_set_dhcp_address(ipconfig, address);
219 DBG("last address %s", address);
221 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
223 netmask = g_strdup(option->data);
225 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
227 gateway = g_strdup(option->data);
229 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
231 DBG("c_address %s", c_address);
233 if (address != NULL && c_address != NULL &&
234 g_strcmp0(address, c_address) != 0)
236 else if (gateway != NULL && c_gateway != NULL &&
237 g_strcmp0(gateway, c_gateway) != 0)
239 else if (prefixlen != c_prefixlen)
241 else if (c_address == NULL || c_gateway == NULL)
246 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
247 for (ns_entries = 0, list = option; list; list = list->next)
249 nameservers = g_try_new0(char *, ns_entries + 1);
250 if (nameservers != NULL) {
251 for (i = 0, list = option; list; list = list->next, i++)
252 nameservers[i] = g_strdup(list->data);
253 nameservers[ns_entries] = NULL;
256 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
258 domainname = g_strdup(option->data);
260 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
262 hostname = g_strdup(option->data);
264 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
265 for (ns_entries = 0, list = option; list; list = list->next)
267 timeservers = g_try_new0(char *, ns_entries + 1);
268 if (timeservers != NULL) {
269 for (i = 0, list = option; list; list = list->next, i++)
270 timeservers[i] = g_strdup(list->data);
271 timeservers[ns_entries] = NULL;
274 option = g_dhcp_client_get_option(dhcp_client, 252);
276 pac = g_strdup(option->data);
278 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
280 if (ip_change == TRUE) {
281 __connman_ipconfig_set_local(ipconfig, address);
282 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
283 __connman_ipconfig_set_gateway(ipconfig, gateway);
286 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
287 if (dhcp->nameservers != NULL) {
288 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
289 __connman_service_nameserver_remove(service,
290 dhcp->nameservers[i], FALSE);
292 g_strfreev(dhcp->nameservers);
295 dhcp->nameservers = nameservers;
297 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
298 __connman_service_nameserver_append(service,
299 dhcp->nameservers[i], FALSE);
302 g_strfreev(nameservers);
305 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
306 if (dhcp->timeservers != NULL) {
307 for (i = 0; dhcp->timeservers[i] != NULL; i++) {
308 __connman_service_timeserver_remove(service,
309 dhcp->timeservers[i]);
311 g_strfreev(dhcp->timeservers);
314 dhcp->timeservers = timeservers;
316 for (i = 0; dhcp->timeservers[i] != NULL; i++) {
317 __connman_service_timeserver_append(service,
318 dhcp->timeservers[i]);
321 g_strfreev(timeservers);
324 if (g_strcmp0(pac, dhcp->pac) != 0) {
328 __connman_service_set_pac(service, dhcp->pac);
331 __connman_service_set_domainname(service, domainname);
333 if (domainname != NULL)
334 __connman_utsname_set_domainname(domainname);
336 if (hostname != NULL)
337 __connman_utsname_set_hostname(hostname);
339 if (ip_change == TRUE)
342 __connman_6to4_probe(service);
351 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
353 struct connman_dhcp *dhcp = user_data;
354 char *address, *netmask;
355 struct connman_service *service;
356 struct connman_ipconfig *ipconfig;
357 unsigned char prefixlen;
359 DBG("IPV4LL available");
361 service = __connman_service_lookup_from_network(dhcp->network);
365 ipconfig = __connman_service_get_ip4config(service);
366 if (ipconfig == NULL)
369 address = g_dhcp_client_get_address(dhcp_client);
370 netmask = g_dhcp_client_get_netmask(dhcp_client);
372 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
374 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
375 __connman_ipconfig_set_local(ipconfig, address);
376 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
377 __connman_ipconfig_set_gateway(ipconfig, NULL);
385 static void dhcp_debug(const char *str, void *data)
387 connman_info("%s: %s\n", (const char *) data, str);
390 static int dhcp_request(struct connman_dhcp *dhcp)
392 struct connman_service *service;
393 struct connman_ipconfig *ipconfig;
394 GDHCPClient *dhcp_client;
395 GDHCPClientError error;
396 const char *hostname;
399 DBG("dhcp %p", dhcp);
401 index = connman_network_get_index(dhcp->network);
403 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
404 if (error != G_DHCP_CLIENT_ERROR_NONE)
407 if (getenv("CONNMAN_DHCP_DEBUG"))
408 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
410 g_dhcp_client_set_id(dhcp_client);
412 hostname = connman_utsname_get_hostname();
413 if (hostname != NULL)
414 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
416 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
417 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
418 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
419 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
420 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
421 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
422 g_dhcp_client_set_request(dhcp_client, 252);
424 g_dhcp_client_register_event(dhcp_client,
425 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
426 lease_available_cb, dhcp);
428 g_dhcp_client_register_event(dhcp_client,
429 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
430 ipv4ll_available_cb, dhcp);
432 g_dhcp_client_register_event(dhcp_client,
433 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
435 g_dhcp_client_register_event(dhcp_client,
436 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
438 g_dhcp_client_register_event(dhcp_client,
439 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
441 dhcp->dhcp_client = dhcp_client;
443 service = __connman_service_lookup_from_network(dhcp->network);
444 ipconfig = __connman_service_get_ip4config(service);
446 return g_dhcp_client_start(dhcp_client,
447 __connman_ipconfig_get_dhcp_address(ipconfig));
450 static int dhcp_release(struct connman_dhcp *dhcp)
452 DBG("dhcp %p", dhcp);
454 if (dhcp->dhcp_client == NULL)
457 g_dhcp_client_stop(dhcp->dhcp_client);
458 g_dhcp_client_unref(dhcp->dhcp_client);
460 dhcp->dhcp_client = NULL;
465 static void remove_network(gpointer user_data)
467 struct connman_dhcp *dhcp = user_data;
469 DBG("dhcp %p", dhcp);
471 dhcp_invalidate(dhcp, FALSE);
477 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
479 struct connman_dhcp *dhcp;
483 dhcp = g_try_new0(struct connman_dhcp, 1);
487 dhcp->network = network;
488 dhcp->callback = callback;
490 connman_network_ref(network);
492 g_hash_table_replace(network_table, network, dhcp);
494 return dhcp_request(dhcp);
497 void __connman_dhcp_stop(struct connman_network *network)
501 if (network_table == NULL)
504 if (g_hash_table_remove(network_table, network) == TRUE)
505 connman_network_unref(network);
508 int __connman_dhcp_init(void)
512 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
513 NULL, remove_network);
518 void __connman_dhcp_cleanup(void)
522 g_hash_table_destroy(network_table);
523 network_table = NULL;