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 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 != NULL &&
298 dhcp->nameservers[i] != NULL; i++) {
299 __connman_service_nameserver_append(service,
300 dhcp->nameservers[i], FALSE);
303 g_strfreev(nameservers);
306 if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
307 if (dhcp->timeservers != NULL) {
308 for (i = 0; dhcp->timeservers[i] != NULL; i++) {
309 __connman_service_timeserver_remove(service,
310 dhcp->timeservers[i]);
312 g_strfreev(dhcp->timeservers);
315 dhcp->timeservers = timeservers;
317 for (i = 0; dhcp->timeservers != NULL &&
318 dhcp->timeservers[i] != NULL; i++) {
319 __connman_service_timeserver_append(service,
320 dhcp->timeservers[i]);
323 g_strfreev(timeservers);
326 if (g_strcmp0(pac, dhcp->pac) != 0) {
330 __connman_service_set_pac(service, dhcp->pac);
333 __connman_service_set_domainname(service, domainname);
335 if (domainname != NULL)
336 __connman_utsname_set_domainname(domainname);
338 if (hostname != NULL)
339 __connman_utsname_set_hostname(hostname);
341 if (ip_change == TRUE)
344 __connman_6to4_probe(service);
353 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
355 struct connman_dhcp *dhcp = user_data;
356 char *address, *netmask;
357 struct connman_service *service;
358 struct connman_ipconfig *ipconfig;
359 unsigned char prefixlen;
361 DBG("IPV4LL available");
363 service = __connman_service_lookup_from_network(dhcp->network);
367 ipconfig = __connman_service_get_ip4config(service);
368 if (ipconfig == NULL)
371 address = g_dhcp_client_get_address(dhcp_client);
372 netmask = g_dhcp_client_get_netmask(dhcp_client);
374 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
376 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
377 __connman_ipconfig_set_local(ipconfig, address);
378 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
379 __connman_ipconfig_set_gateway(ipconfig, NULL);
387 static void dhcp_debug(const char *str, void *data)
389 connman_info("%s: %s\n", (const char *) data, str);
392 static int dhcp_request(struct connman_dhcp *dhcp)
394 struct connman_service *service;
395 struct connman_ipconfig *ipconfig;
396 GDHCPClient *dhcp_client;
397 GDHCPClientError error;
398 const char *hostname;
401 DBG("dhcp %p", dhcp);
403 index = connman_network_get_index(dhcp->network);
405 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
406 if (error != G_DHCP_CLIENT_ERROR_NONE)
409 if (getenv("CONNMAN_DHCP_DEBUG"))
410 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
412 g_dhcp_client_set_id(dhcp_client);
414 hostname = connman_utsname_get_hostname();
415 if (hostname != NULL)
416 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
418 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
419 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
420 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
421 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
422 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
423 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
424 g_dhcp_client_set_request(dhcp_client, 252);
426 g_dhcp_client_register_event(dhcp_client,
427 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
428 lease_available_cb, dhcp);
430 g_dhcp_client_register_event(dhcp_client,
431 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
432 ipv4ll_available_cb, dhcp);
434 g_dhcp_client_register_event(dhcp_client,
435 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
437 g_dhcp_client_register_event(dhcp_client,
438 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
440 g_dhcp_client_register_event(dhcp_client,
441 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
443 dhcp->dhcp_client = dhcp_client;
445 service = __connman_service_lookup_from_network(dhcp->network);
446 ipconfig = __connman_service_get_ip4config(service);
448 return g_dhcp_client_start(dhcp_client,
449 __connman_ipconfig_get_dhcp_address(ipconfig));
452 static int dhcp_release(struct connman_dhcp *dhcp)
454 DBG("dhcp %p", dhcp);
456 if (dhcp->dhcp_client == NULL)
459 g_dhcp_client_stop(dhcp->dhcp_client);
460 g_dhcp_client_unref(dhcp->dhcp_client);
462 dhcp->dhcp_client = NULL;
467 static void remove_network(gpointer user_data)
469 struct connman_dhcp *dhcp = user_data;
471 DBG("dhcp %p", dhcp);
473 dhcp_invalidate(dhcp, FALSE);
479 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
481 struct connman_dhcp *dhcp;
485 dhcp = g_try_new0(struct connman_dhcp, 1);
489 dhcp->network = network;
490 dhcp->callback = callback;
492 connman_network_ref(network);
494 g_hash_table_replace(network_table, network, dhcp);
496 return dhcp_request(dhcp);
499 void __connman_dhcp_stop(struct connman_network *network)
503 if (network_table == NULL)
506 if (g_hash_table_remove(network_table, network) == TRUE)
507 connman_network_unref(network);
510 int __connman_dhcp_init(void)
514 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
515 NULL, remove_network);
520 void __connman_dhcp_cleanup(void)
524 g_hash_table_destroy(network_table);
525 network_table = NULL;