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;
62 static void dhcp_invalid(struct connman_dhcp *dhcp)
64 struct connman_service *service;
65 struct connman_ipconfig *ipconfig;
68 service = __connman_service_lookup_from_network(dhcp->network);
72 ipconfig = __connman_service_get_ip4config(service);
76 __connman_6to4_remove(ipconfig);
78 __connman_service_set_domainname(service, NULL);
79 __connman_service_set_pac(service, NULL);
80 __connman_service_timeserver_remove(service, dhcp->timeserver);
82 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
83 __connman_service_nameserver_remove(service,
84 dhcp->nameservers[i]);
87 __connman_ipconfig_set_local(ipconfig, NULL);
88 __connman_ipconfig_set_broadcast(ipconfig, NULL);
89 __connman_ipconfig_set_gateway(ipconfig, NULL);
90 __connman_ipconfig_set_prefixlen(ipconfig, 0);
92 if (dhcp->callback != NULL)
93 dhcp->callback(dhcp->network, FALSE);
98 static void dhcp_valid(struct connman_dhcp *dhcp)
100 if (dhcp->callback != NULL)
101 dhcp->callback(dhcp->network, TRUE);
104 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
106 struct connman_dhcp *dhcp = user_data;
108 DBG("No lease available");
113 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
115 struct connman_dhcp *dhcp = user_data;
122 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
124 struct connman_dhcp *dhcp = user_data;
132 static gboolean compare_string_arrays(char **array_a, char **array_b)
136 if (array_a == NULL || array_b == NULL)
139 if (g_strv_length(array_a) != g_strv_length(array_b))
142 for (i = 0; array_a[i] != NULL &&
143 array_b[i] != NULL; i++) {
144 if (g_strcmp0(array_a[i], array_b[i]) != 0)
151 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
153 struct connman_dhcp *dhcp = user_data;
154 GList *list, *option = NULL;
155 char *address, *netmask = NULL, *gateway = NULL;
156 const char *c_address, *c_gateway;
157 char *domainname = NULL, *hostname = NULL;
158 char **nameservers, *timeserver = NULL, *pac = NULL;
160 struct connman_ipconfig *ipconfig;
161 struct connman_service *service;
162 unsigned char prefixlen, c_prefixlen;
166 DBG("Lease available");
168 service = __connman_service_lookup_from_network(dhcp->network);
169 if (service == NULL) {
170 connman_error("Can not lookup service");
174 ipconfig = __connman_service_get_ip4config(service);
175 if (ipconfig == NULL) {
176 connman_error("Could not lookup ipconfig");
180 c_address = __connman_ipconfig_get_local(ipconfig);
181 c_gateway = __connman_ipconfig_get_gateway(ipconfig);
182 c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
184 address = g_dhcp_client_get_address(dhcp_client);
186 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
188 netmask = g_strdup(option->data);
190 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
192 gateway = g_strdup(option->data);
194 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
196 DBG("c_address %s", c_address);
198 if (address != NULL && c_address != NULL &&
199 g_strcmp0(address, c_address) != 0)
201 else if (gateway != NULL && c_gateway != NULL &&
202 g_strcmp0(gateway, c_gateway) != 0)
204 else if (prefixlen != c_prefixlen)
206 else if (c_address == NULL || c_gateway == NULL)
211 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
212 for (ns_entries = 0, list = option; list; list = list->next)
214 nameservers = g_try_new0(char *, ns_entries + 1);
215 if (nameservers != NULL) {
216 for (i = 0, list = option; list; list = list->next, i++)
217 nameservers[i] = g_strdup(list->data);
218 nameservers[ns_entries] = NULL;
221 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
223 domainname = g_strdup(option->data);
225 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
227 hostname = g_strdup(option->data);
229 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
231 timeserver = g_strdup(option->data);
233 option = g_dhcp_client_get_option(dhcp_client, 252);
235 pac = g_strdup(option->data);
237 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
239 if (ip_change == TRUE) {
240 __connman_ipconfig_set_local(ipconfig, address);
241 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
242 __connman_ipconfig_set_gateway(ipconfig, gateway);
245 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
246 if (dhcp->nameservers != NULL) {
247 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
248 __connman_service_nameserver_remove(service,
249 dhcp->nameservers[i]);
251 g_strfreev(dhcp->nameservers);
254 dhcp->nameservers = nameservers;
256 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
257 __connman_service_nameserver_append(service,
258 dhcp->nameservers[i]);
262 if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
263 if (dhcp->timeserver != NULL) {
264 __connman_service_timeserver_remove(service,
266 g_free(dhcp->timeserver);
269 dhcp->timeserver = timeserver;
271 if (dhcp->timeserver != NULL)
272 __connman_service_timeserver_append(service,
276 if (g_strcmp0(pac, dhcp->pac) != 0) {
280 __connman_service_set_pac(service, dhcp->pac);
283 __connman_service_set_domainname(service, domainname);
285 if (domainname != NULL)
286 __connman_utsname_set_domainname(domainname);
288 if (hostname != NULL)
289 __connman_utsname_set_hostname(hostname);
291 if (ip_change == TRUE)
294 __connman_6to4_probe(service);
303 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
305 struct connman_dhcp *dhcp = user_data;
306 char *address, *netmask;
307 struct connman_service *service;
308 struct connman_ipconfig *ipconfig;
309 unsigned char prefixlen;
311 DBG("IPV4LL available");
313 service = __connman_service_lookup_from_network(dhcp->network);
317 ipconfig = __connman_service_get_ip4config(service);
318 if (ipconfig == NULL)
321 address = g_dhcp_client_get_address(dhcp_client);
322 netmask = g_dhcp_client_get_netmask(dhcp_client);
324 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
326 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
327 __connman_ipconfig_set_local(ipconfig, address);
328 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
329 __connman_ipconfig_set_gateway(ipconfig, NULL);
337 static void dhcp_debug(const char *str, void *data)
339 connman_info("%s: %s\n", (const char *) data, str);
342 static int dhcp_request(struct connman_dhcp *dhcp)
344 GDHCPClient *dhcp_client;
345 GDHCPClientError error;
346 const char *hostname;
349 DBG("dhcp %p", dhcp);
351 index = connman_network_get_index(dhcp->network);
353 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
354 if (error != G_DHCP_CLIENT_ERROR_NONE)
357 if (getenv("CONNMAN_DHCP_DEBUG"))
358 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
360 hostname = connman_utsname_get_hostname();
361 if (hostname != NULL)
362 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
364 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
365 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
366 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
367 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
368 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
369 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
370 g_dhcp_client_set_request(dhcp_client, 252);
372 g_dhcp_client_register_event(dhcp_client,
373 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
374 lease_available_cb, dhcp);
376 g_dhcp_client_register_event(dhcp_client,
377 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
378 ipv4ll_available_cb, dhcp);
380 g_dhcp_client_register_event(dhcp_client,
381 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
383 g_dhcp_client_register_event(dhcp_client,
384 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
386 g_dhcp_client_register_event(dhcp_client,
387 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
389 dhcp->dhcp_client = dhcp_client;
391 return g_dhcp_client_start(dhcp_client);
394 static int dhcp_release(struct connman_dhcp *dhcp)
396 DBG("dhcp %p", dhcp);
398 if (dhcp->dhcp_client == NULL)
401 g_dhcp_client_stop(dhcp->dhcp_client);
402 g_dhcp_client_unref(dhcp->dhcp_client);
404 dhcp->dhcp_client = NULL;
409 static void remove_network(gpointer user_data)
411 struct connman_dhcp *dhcp = user_data;
413 DBG("dhcp %p", dhcp);
421 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
423 struct connman_dhcp *dhcp;
427 dhcp = g_try_new0(struct connman_dhcp, 1);
431 dhcp->network = network;
432 dhcp->callback = callback;
434 g_hash_table_replace(network_table, network, dhcp);
436 return dhcp_request(dhcp);
439 void __connman_dhcp_stop(struct connman_network *network)
443 g_hash_table_remove(network_table, network);
446 int __connman_dhcp_init(void)
450 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
451 NULL, remove_network);
456 void __connman_dhcp_cleanup(void)
460 g_hash_table_destroy(network_table);