5 * Copyright (C) 2007-2012 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 ns_entries = g_list_length(option);
248 nameservers = g_try_new0(char *, ns_entries + 1);
249 if (nameservers != NULL) {
250 for (i = 0, list = option; list; list = list->next, i++)
251 nameservers[i] = g_strdup(list->data);
252 nameservers[ns_entries] = NULL;
255 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
257 domainname = g_strdup(option->data);
259 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
261 hostname = g_strdup(option->data);
263 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
264 ns_entries = g_list_length(option);
265 timeservers = g_try_new0(char *, ns_entries + 1);
266 if (timeservers != NULL) {
267 for (i = 0, list = option; list; list = list->next, i++)
268 timeservers[i] = g_strdup(list->data);
269 timeservers[ns_entries] = NULL;
272 option = g_dhcp_client_get_option(dhcp_client, 252);
274 pac = g_strdup(option->data);
276 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
278 if (ip_change == TRUE) {
279 __connman_ipconfig_set_local(ipconfig, address);
280 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
281 __connman_ipconfig_set_gateway(ipconfig, gateway);
284 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
285 if (dhcp->nameservers != NULL) {
286 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
287 __connman_service_nameserver_remove(service,
288 dhcp->nameservers[i], FALSE);
290 g_strfreev(dhcp->nameservers);
293 dhcp->nameservers = nameservers;
295 for (i = 0; dhcp->nameservers != NULL &&
296 dhcp->nameservers[i] != NULL; i++) {
297 __connman_service_nameserver_append(service,
298 dhcp->nameservers[i], FALSE);
301 g_strfreev(nameservers);
304 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
305 if (dhcp->timeservers != NULL) {
306 for (i = 0; dhcp->timeservers[i] != NULL; i++) {
307 __connman_service_timeserver_remove(service,
308 dhcp->timeservers[i]);
310 g_strfreev(dhcp->timeservers);
313 dhcp->timeservers = timeservers;
315 for (i = 0; dhcp->timeservers != NULL &&
316 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 #if defined DISABLE_LINK_LOCAL_ADDR
366 dhcp_invalidate(dhcp, TRUE);
368 __connman_service_ipconfig_indicate_state(service,
369 CONNMAN_SERVICE_STATE_IDLE,
370 CONNMAN_IPCONFIG_TYPE_IPV4);
371 __connman_service_ipconfig_indicate_state(service,
372 CONNMAN_SERVICE_STATE_IDLE,
373 CONNMAN_IPCONFIG_TYPE_IPV6);
376 ipconfig = __connman_service_get_ip4config(service);
377 if (ipconfig == NULL)
380 address = g_dhcp_client_get_address(dhcp_client);
381 netmask = g_dhcp_client_get_netmask(dhcp_client);
383 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
385 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
386 __connman_ipconfig_set_local(ipconfig, address);
387 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
388 __connman_ipconfig_set_gateway(ipconfig, NULL);
396 static void dhcp_debug(const char *str, void *data)
398 connman_info("%s: %s\n", (const char *) data, str);
401 static int dhcp_request(struct connman_dhcp *dhcp)
403 struct connman_service *service;
404 struct connman_ipconfig *ipconfig;
405 GDHCPClient *dhcp_client;
406 GDHCPClientError error;
407 const char *hostname;
409 #if defined TIZEN_EXT
410 const char *last_address;
413 DBG("dhcp %p", dhcp);
415 index = connman_network_get_index(dhcp->network);
417 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
418 if (error != G_DHCP_CLIENT_ERROR_NONE)
421 if (getenv("CONNMAN_DHCP_DEBUG"))
422 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
424 g_dhcp_client_set_id(dhcp_client);
426 hostname = connman_utsname_get_hostname();
427 if (hostname != NULL)
428 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
430 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
431 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
432 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
433 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
434 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
435 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
436 g_dhcp_client_set_request(dhcp_client, 252);
438 g_dhcp_client_register_event(dhcp_client,
439 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
440 lease_available_cb, dhcp);
442 g_dhcp_client_register_event(dhcp_client,
443 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
444 ipv4ll_available_cb, dhcp);
446 g_dhcp_client_register_event(dhcp_client,
447 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
449 g_dhcp_client_register_event(dhcp_client,
450 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
452 g_dhcp_client_register_event(dhcp_client,
453 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
455 dhcp->dhcp_client = dhcp_client;
457 service = __connman_service_lookup_from_network(dhcp->network);
458 ipconfig = __connman_service_get_ip4config(service);
460 #if defined TIZEN_EXT
461 last_address = __connman_ipconfig_get_dhcp_address(ipconfig);
463 if (last_address != NULL && strlen(last_address) > 0)
464 g_dhcp_client_set_address_known(dhcp_client, TRUE);
466 return g_dhcp_client_start(dhcp_client, last_address);
468 return g_dhcp_client_start(dhcp_client,
469 __connman_ipconfig_get_dhcp_address(ipconfig));
473 static int dhcp_release(struct connman_dhcp *dhcp)
475 DBG("dhcp %p", dhcp);
477 if (dhcp->dhcp_client == NULL)
480 g_dhcp_client_stop(dhcp->dhcp_client);
481 g_dhcp_client_unref(dhcp->dhcp_client);
483 dhcp->dhcp_client = NULL;
488 static void remove_network(gpointer user_data)
490 struct connman_dhcp *dhcp = user_data;
492 DBG("dhcp %p", dhcp);
494 dhcp_invalidate(dhcp, FALSE);
500 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
502 struct connman_dhcp *dhcp;
506 dhcp = g_try_new0(struct connman_dhcp, 1);
510 dhcp->network = network;
511 dhcp->callback = callback;
513 connman_network_ref(network);
515 g_hash_table_replace(network_table, network, dhcp);
517 return dhcp_request(dhcp);
520 void __connman_dhcp_stop(struct connman_network *network)
524 if (network_table == NULL)
527 if (g_hash_table_remove(network_table, network) == TRUE)
528 connman_network_unref(network);
531 int __connman_dhcp_init(void)
535 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
536 NULL, remove_network);
541 void __connman_dhcp_cleanup(void)
545 g_hash_table_destroy(network_table);
546 network_table = NULL;