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_address_remove(ipconfig);
108 __connman_ipconfig_set_local(ipconfig, NULL);
109 __connman_ipconfig_set_broadcast(ipconfig, NULL);
110 __connman_ipconfig_set_gateway(ipconfig, NULL);
111 __connman_ipconfig_set_prefixlen(ipconfig, 0);
113 if (dhcp->callback != NULL && callback)
114 dhcp->callback(dhcp->network, FALSE);
119 static void dhcp_valid(struct connman_dhcp *dhcp)
121 if (dhcp->callback != NULL)
122 dhcp->callback(dhcp->network, TRUE);
125 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
127 struct connman_dhcp *dhcp = user_data;
129 DBG("No lease available");
131 dhcp_invalidate(dhcp, TRUE);
134 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
136 struct connman_dhcp *dhcp = user_data;
140 dhcp_invalidate(dhcp, TRUE);
143 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
145 struct connman_dhcp *dhcp = user_data;
149 dhcp_invalidate(dhcp, TRUE);
153 static gboolean compare_string_arrays(char **array_a, char **array_b)
157 if (array_a == NULL || array_b == NULL)
160 if (g_strv_length(array_a) != g_strv_length(array_b))
163 for (i = 0; array_a[i] != NULL &&
164 array_b[i] != NULL; i++) {
165 if (g_strcmp0(array_a[i], array_b[i]) != 0)
172 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
174 struct connman_dhcp *dhcp = user_data;
175 GList *list, *option = NULL;
176 char *address, *netmask = NULL, *gateway = NULL;
177 const char *c_address, *c_gateway;
178 char *domainname = NULL, *hostname = NULL;
179 char **nameservers, *timeserver = NULL, *pac = NULL;
181 struct connman_ipconfig *ipconfig;
182 struct connman_service *service;
183 unsigned char prefixlen, c_prefixlen;
187 DBG("Lease available");
189 service = __connman_service_lookup_from_network(dhcp->network);
190 if (service == NULL) {
191 connman_error("Can not lookup service");
195 ipconfig = __connman_service_get_ip4config(service);
196 if (ipconfig == NULL) {
197 connman_error("Could not lookup ipconfig");
201 c_address = __connman_ipconfig_get_local(ipconfig);
202 c_gateway = __connman_ipconfig_get_gateway(ipconfig);
203 c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
205 address = g_dhcp_client_get_address(dhcp_client);
207 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
209 netmask = g_strdup(option->data);
211 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
213 gateway = g_strdup(option->data);
215 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
217 DBG("c_address %s", c_address);
219 if (address != NULL && c_address != NULL &&
220 g_strcmp0(address, c_address) != 0)
222 else if (gateway != NULL && c_gateway != NULL &&
223 g_strcmp0(gateway, c_gateway) != 0)
225 else if (prefixlen != c_prefixlen)
227 else if (c_address == NULL || c_gateway == NULL)
232 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
233 for (ns_entries = 0, list = option; list; list = list->next)
235 nameservers = g_try_new0(char *, ns_entries + 1);
236 if (nameservers != NULL) {
237 for (i = 0, list = option; list; list = list->next, i++)
238 nameservers[i] = g_strdup(list->data);
239 nameservers[ns_entries] = NULL;
242 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
244 domainname = g_strdup(option->data);
246 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
248 hostname = g_strdup(option->data);
250 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
252 timeserver = g_strdup(option->data);
254 option = g_dhcp_client_get_option(dhcp_client, 252);
256 pac = g_strdup(option->data);
258 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
260 if (ip_change == TRUE) {
261 __connman_ipconfig_set_local(ipconfig, address);
262 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
263 __connman_ipconfig_set_gateway(ipconfig, gateway);
266 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
267 if (dhcp->nameservers != NULL) {
268 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
269 __connman_service_nameserver_remove(service,
270 dhcp->nameservers[i]);
272 g_strfreev(dhcp->nameservers);
275 dhcp->nameservers = nameservers;
277 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
278 __connman_service_nameserver_append(service,
279 dhcp->nameservers[i]);
283 if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
284 if (dhcp->timeserver != NULL) {
285 __connman_service_timeserver_remove(service,
287 g_free(dhcp->timeserver);
290 dhcp->timeserver = timeserver;
292 if (dhcp->timeserver != NULL)
293 __connman_service_timeserver_append(service,
297 if (g_strcmp0(pac, dhcp->pac) != 0) {
301 __connman_service_set_pac(service, dhcp->pac);
304 __connman_service_set_domainname(service, domainname);
306 if (domainname != NULL)
307 __connman_utsname_set_domainname(domainname);
309 if (hostname != NULL)
310 __connman_utsname_set_hostname(hostname);
312 if (ip_change == TRUE)
315 __connman_6to4_probe(service);
324 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
326 struct connman_dhcp *dhcp = user_data;
327 char *address, *netmask;
328 struct connman_service *service;
329 struct connman_ipconfig *ipconfig;
330 unsigned char prefixlen;
332 DBG("IPV4LL available");
334 service = __connman_service_lookup_from_network(dhcp->network);
338 ipconfig = __connman_service_get_ip4config(service);
339 if (ipconfig == NULL)
342 address = g_dhcp_client_get_address(dhcp_client);
343 netmask = g_dhcp_client_get_netmask(dhcp_client);
345 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
347 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
348 __connman_ipconfig_set_local(ipconfig, address);
349 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
350 __connman_ipconfig_set_gateway(ipconfig, NULL);
358 static void dhcp_debug(const char *str, void *data)
360 connman_info("%s: %s\n", (const char *) data, str);
363 static int dhcp_request(struct connman_dhcp *dhcp)
365 GDHCPClient *dhcp_client;
366 GDHCPClientError error;
367 const char *hostname;
370 DBG("dhcp %p", dhcp);
372 index = connman_network_get_index(dhcp->network);
374 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
375 if (error != G_DHCP_CLIENT_ERROR_NONE)
378 if (getenv("CONNMAN_DHCP_DEBUG"))
379 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
381 hostname = connman_utsname_get_hostname();
382 if (hostname != NULL)
383 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
385 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
386 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
387 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
388 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
389 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
390 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
391 g_dhcp_client_set_request(dhcp_client, 252);
393 g_dhcp_client_register_event(dhcp_client,
394 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
395 lease_available_cb, dhcp);
397 g_dhcp_client_register_event(dhcp_client,
398 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
399 ipv4ll_available_cb, dhcp);
401 g_dhcp_client_register_event(dhcp_client,
402 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
404 g_dhcp_client_register_event(dhcp_client,
405 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
407 g_dhcp_client_register_event(dhcp_client,
408 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
410 dhcp->dhcp_client = dhcp_client;
412 return g_dhcp_client_start(dhcp_client);
415 static int dhcp_release(struct connman_dhcp *dhcp)
417 DBG("dhcp %p", dhcp);
419 if (dhcp->dhcp_client == NULL)
422 g_dhcp_client_stop(dhcp->dhcp_client);
423 g_dhcp_client_unref(dhcp->dhcp_client);
425 dhcp->dhcp_client = NULL;
430 static void remove_network(gpointer user_data)
432 struct connman_dhcp *dhcp = user_data;
434 DBG("dhcp %p", dhcp);
436 dhcp_invalidate(dhcp, FALSE);
442 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
444 struct connman_dhcp *dhcp;
448 dhcp = g_try_new0(struct connman_dhcp, 1);
452 dhcp->network = network;
453 dhcp->callback = callback;
455 g_hash_table_replace(network_table, network, dhcp);
457 return dhcp_request(dhcp);
460 void __connman_dhcp_stop(struct connman_network *network)
464 g_hash_table_remove(network_table, network);
467 int __connman_dhcp_init(void)
471 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
472 NULL, remove_network);
477 void __connman_dhcp_cleanup(void)
481 g_hash_table_destroy(network_table);