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_free(dhcp->timeserver);
57 dhcp->nameservers = NULL;
58 dhcp->timeserver = 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);
97 __connman_service_timeserver_remove(service, dhcp->timeserver);
99 if (dhcp->nameservers != NULL) {
100 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
101 __connman_service_nameserver_remove(service,
102 dhcp->nameservers[i]);
106 __connman_ipconfig_set_dhcp_address(ipconfig,
107 __connman_ipconfig_get_local(ipconfig));
108 DBG("last address %s", __connman_ipconfig_get_dhcp_address(ipconfig));
110 __connman_ipconfig_address_remove(ipconfig);
112 __connman_ipconfig_set_local(ipconfig, NULL);
113 __connman_ipconfig_set_broadcast(ipconfig, NULL);
114 __connman_ipconfig_set_gateway(ipconfig, NULL);
115 __connman_ipconfig_set_prefixlen(ipconfig, 0);
117 if (dhcp->callback != NULL && callback)
118 dhcp->callback(dhcp->network, FALSE);
124 static void dhcp_valid(struct connman_dhcp *dhcp)
126 if (dhcp->callback != NULL)
127 dhcp->callback(dhcp->network, TRUE);
130 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
132 struct connman_dhcp *dhcp = user_data;
134 DBG("No lease available");
136 dhcp_invalidate(dhcp, TRUE);
139 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
141 struct connman_dhcp *dhcp = user_data;
145 dhcp_invalidate(dhcp, TRUE);
148 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
150 struct connman_dhcp *dhcp = user_data;
154 dhcp_invalidate(dhcp, TRUE);
158 static gboolean compare_string_arrays(char **array_a, char **array_b)
162 if (array_a == NULL || array_b == NULL)
165 if (g_strv_length(array_a) != g_strv_length(array_b))
168 for (i = 0; array_a[i] != NULL &&
169 array_b[i] != NULL; i++) {
170 if (g_strcmp0(array_a[i], array_b[i]) != 0)
177 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
179 struct connman_dhcp *dhcp = user_data;
180 GList *list, *option = NULL;
181 char *address, *netmask = NULL, *gateway = NULL;
182 const char *c_address, *c_gateway;
183 char *domainname = NULL, *hostname = NULL;
184 char **nameservers, *timeserver = NULL, *pac = NULL;
186 struct connman_ipconfig *ipconfig;
187 struct connman_service *service;
188 unsigned char prefixlen, c_prefixlen;
192 DBG("Lease available");
194 service = __connman_service_lookup_from_network(dhcp->network);
195 if (service == NULL) {
196 connman_error("Can not lookup service");
200 ipconfig = __connman_service_get_ip4config(service);
201 if (ipconfig == NULL) {
202 connman_error("Could not lookup ipconfig");
206 c_address = __connman_ipconfig_get_local(ipconfig);
207 c_gateway = __connman_ipconfig_get_gateway(ipconfig);
208 c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
210 address = g_dhcp_client_get_address(dhcp_client);
212 __connman_ipconfig_set_dhcp_address(ipconfig, address);
213 DBG("last address %s", address);
215 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
217 netmask = g_strdup(option->data);
219 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
221 gateway = g_strdup(option->data);
223 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
225 DBG("c_address %s", c_address);
227 if (address != NULL && c_address != NULL &&
228 g_strcmp0(address, c_address) != 0)
230 else if (gateway != NULL && c_gateway != NULL &&
231 g_strcmp0(gateway, c_gateway) != 0)
233 else if (prefixlen != c_prefixlen)
235 else if (c_address == NULL || c_gateway == NULL)
240 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
241 for (ns_entries = 0, list = option; list; list = list->next)
243 nameservers = g_try_new0(char *, ns_entries + 1);
244 if (nameservers != NULL) {
245 for (i = 0, list = option; list; list = list->next, i++)
246 nameservers[i] = g_strdup(list->data);
247 nameservers[ns_entries] = NULL;
250 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
252 domainname = g_strdup(option->data);
254 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
256 hostname = g_strdup(option->data);
258 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
260 timeserver = g_strdup(option->data);
262 option = g_dhcp_client_get_option(dhcp_client, 252);
264 pac = g_strdup(option->data);
266 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
268 if (ip_change == TRUE) {
269 __connman_ipconfig_set_local(ipconfig, address);
270 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
271 __connman_ipconfig_set_gateway(ipconfig, gateway);
274 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
275 if (dhcp->nameservers != NULL) {
276 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
277 __connman_service_nameserver_remove(service,
278 dhcp->nameservers[i]);
280 g_strfreev(dhcp->nameservers);
283 dhcp->nameservers = nameservers;
285 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
286 __connman_service_nameserver_append(service,
287 dhcp->nameservers[i]);
290 g_strfreev(nameservers);
293 if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
294 if (dhcp->timeserver != NULL) {
295 __connman_service_timeserver_remove(service,
297 g_free(dhcp->timeserver);
300 dhcp->timeserver = timeserver;
302 if (dhcp->timeserver != NULL)
303 __connman_service_timeserver_append(service,
307 if (g_strcmp0(pac, dhcp->pac) != 0) {
311 __connman_service_set_pac(service, dhcp->pac);
314 __connman_service_set_domainname(service, domainname);
316 if (domainname != NULL)
317 __connman_utsname_set_domainname(domainname);
319 if (hostname != NULL)
320 __connman_utsname_set_hostname(hostname);
322 if (ip_change == TRUE)
325 __connman_6to4_probe(service);
334 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
336 struct connman_dhcp *dhcp = user_data;
337 char *address, *netmask;
338 struct connman_service *service;
339 struct connman_ipconfig *ipconfig;
340 unsigned char prefixlen;
342 DBG("IPV4LL available");
344 service = __connman_service_lookup_from_network(dhcp->network);
348 ipconfig = __connman_service_get_ip4config(service);
349 if (ipconfig == NULL)
352 address = g_dhcp_client_get_address(dhcp_client);
353 netmask = g_dhcp_client_get_netmask(dhcp_client);
355 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
357 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
358 __connman_ipconfig_set_local(ipconfig, address);
359 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
360 __connman_ipconfig_set_gateway(ipconfig, NULL);
368 static void dhcp_debug(const char *str, void *data)
370 connman_info("%s: %s\n", (const char *) data, str);
373 static int dhcp_request(struct connman_dhcp *dhcp)
375 struct connman_service *service;
376 struct connman_ipconfig *ipconfig;
377 GDHCPClient *dhcp_client;
378 GDHCPClientError error;
379 const char *hostname;
382 DBG("dhcp %p", dhcp);
384 index = connman_network_get_index(dhcp->network);
386 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
387 if (error != G_DHCP_CLIENT_ERROR_NONE)
390 if (getenv("CONNMAN_DHCP_DEBUG"))
391 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
393 hostname = connman_utsname_get_hostname();
394 if (hostname != NULL)
395 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
397 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
398 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
399 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
400 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
401 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
402 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
403 g_dhcp_client_set_request(dhcp_client, 252);
405 g_dhcp_client_register_event(dhcp_client,
406 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
407 lease_available_cb, dhcp);
409 g_dhcp_client_register_event(dhcp_client,
410 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
411 ipv4ll_available_cb, dhcp);
413 g_dhcp_client_register_event(dhcp_client,
414 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
416 g_dhcp_client_register_event(dhcp_client,
417 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
419 g_dhcp_client_register_event(dhcp_client,
420 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
422 dhcp->dhcp_client = dhcp_client;
424 service = __connman_service_lookup_from_network(dhcp->network);
425 ipconfig = __connman_service_get_ip4config(service);
427 return g_dhcp_client_start(dhcp_client,
428 __connman_ipconfig_get_dhcp_address(ipconfig));
431 static int dhcp_release(struct connman_dhcp *dhcp)
433 DBG("dhcp %p", dhcp);
435 if (dhcp->dhcp_client == NULL)
438 g_dhcp_client_stop(dhcp->dhcp_client);
439 g_dhcp_client_unref(dhcp->dhcp_client);
441 dhcp->dhcp_client = NULL;
446 static void remove_network(gpointer user_data)
448 struct connman_dhcp *dhcp = user_data;
450 DBG("dhcp %p", dhcp);
452 dhcp_invalidate(dhcp, FALSE);
458 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
460 struct connman_dhcp *dhcp;
464 dhcp = g_try_new0(struct connman_dhcp, 1);
468 dhcp->network = network;
469 dhcp->callback = callback;
471 g_hash_table_replace(network_table, network, dhcp);
473 return dhcp_request(dhcp);
476 void __connman_dhcp_stop(struct connman_network *network)
480 if (network_table == NULL)
483 g_hash_table_remove(network_table, network);
486 int __connman_dhcp_init(void)
490 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
491 NULL, remove_network);
496 void __connman_dhcp_cleanup(void)
500 g_hash_table_destroy(network_table);
501 network_table = NULL;