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_service_set_domainname(service, NULL);
77 __connman_service_set_pac(service, NULL);
78 __connman_service_timeserver_remove(service, dhcp->timeserver);
80 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
81 __connman_service_nameserver_remove(service,
82 dhcp->nameservers[i]);
85 __connman_ipconfig_set_local(ipconfig, NULL);
86 __connman_ipconfig_set_broadcast(ipconfig, NULL);
87 __connman_ipconfig_set_gateway(ipconfig, NULL);
88 __connman_ipconfig_set_prefixlen(ipconfig, 0);
90 if (dhcp->callback != NULL)
91 dhcp->callback(dhcp->network, FALSE);
96 static void dhcp_valid(struct connman_dhcp *dhcp)
98 if (dhcp->callback != NULL)
99 dhcp->callback(dhcp->network, TRUE);
102 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
104 struct connman_dhcp *dhcp = user_data;
106 DBG("No lease available");
111 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
113 struct connman_dhcp *dhcp = user_data;
120 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
122 struct connman_dhcp *dhcp = user_data;
130 static gboolean compare_string_arrays(char **array_a, char **array_b)
134 if (array_a == NULL || array_b == NULL)
137 if (g_strv_length(array_a) != g_strv_length(array_b))
140 for (i = 0; array_a[i] != NULL &&
141 array_b[i] != NULL; i++) {
142 if (g_strcmp0(array_a[i], array_b[i]) != 0)
149 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
151 struct connman_dhcp *dhcp = user_data;
152 GList *list, *option = NULL;
153 char *address, *netmask = NULL, *gateway = NULL;
154 const char *c_address, *c_gateway;
155 char *domainname = NULL, *hostname = NULL;
156 char **nameservers, *timeserver = NULL, *pac = NULL;
158 struct connman_ipconfig *ipconfig;
159 struct connman_service *service;
160 unsigned char prefixlen, c_prefixlen;
164 DBG("Lease available");
166 service = __connman_service_lookup_from_network(dhcp->network);
167 if (service == NULL) {
168 connman_error("Can not lookup service");
172 ipconfig = __connman_service_get_ip4config(service);
173 if (ipconfig == NULL) {
174 connman_error("Could not lookup ipconfig");
178 c_address = __connman_ipconfig_get_local(ipconfig);
179 c_gateway = __connman_ipconfig_get_gateway(ipconfig);
180 c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
182 address = g_dhcp_client_get_address(dhcp_client);
184 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
186 netmask = g_strdup(option->data);
188 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
190 gateway = g_strdup(option->data);
192 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
194 DBG("c_address %s", c_address);
196 if (address != NULL && c_address != NULL &&
197 g_strcmp0(address, c_address) != 0)
199 else if (gateway != NULL && c_gateway != NULL &&
200 g_strcmp0(gateway, c_gateway) != 0)
202 else if (prefixlen != c_prefixlen)
204 else if (c_address == NULL || c_gateway == NULL)
209 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
210 for (ns_entries = 0, list = option; list; list = list->next)
212 nameservers = g_try_new0(char *, ns_entries + 1);
213 if (nameservers != NULL) {
214 for (i = 0, list = option; list; list = list->next, i++)
215 nameservers[i] = g_strdup(list->data);
216 nameservers[ns_entries] = NULL;
219 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
221 domainname = g_strdup(option->data);
223 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
225 hostname = g_strdup(option->data);
227 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
229 timeserver = g_strdup(option->data);
231 option = g_dhcp_client_get_option(dhcp_client, 252);
233 pac = g_strdup(option->data);
235 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
237 if (ip_change == TRUE) {
238 __connman_ipconfig_set_local(ipconfig, address);
239 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
240 __connman_ipconfig_set_gateway(ipconfig, gateway);
243 if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
244 if (dhcp->nameservers != NULL) {
245 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
246 __connman_service_nameserver_remove(service,
247 dhcp->nameservers[i]);
249 g_strfreev(dhcp->nameservers);
252 dhcp->nameservers = nameservers;
254 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
255 __connman_service_nameserver_append(service,
256 dhcp->nameservers[i]);
260 if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
261 if (dhcp->timeserver != NULL) {
262 __connman_service_timeserver_remove(service,
264 g_free(dhcp->timeserver);
267 dhcp->timeserver = timeserver;
269 if (dhcp->timeserver != NULL)
270 __connman_service_timeserver_append(service,
274 if (g_strcmp0(pac, dhcp->pac) != 0) {
278 __connman_service_set_pac(service, dhcp->pac);
281 __connman_service_set_domainname(service, domainname);
283 if (domainname != NULL)
284 __connman_utsname_set_domainname(domainname);
286 if (hostname != NULL)
287 __connman_utsname_set_hostname(hostname);
289 if (ip_change == TRUE)
299 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
301 struct connman_dhcp *dhcp = user_data;
302 char *address, *netmask;
303 struct connman_service *service;
304 struct connman_ipconfig *ipconfig;
305 unsigned char prefixlen;
307 DBG("IPV4LL available");
309 service = __connman_service_lookup_from_network(dhcp->network);
313 ipconfig = __connman_service_get_ip4config(service);
314 if (ipconfig == NULL)
317 address = g_dhcp_client_get_address(dhcp_client);
318 netmask = g_dhcp_client_get_netmask(dhcp_client);
320 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
322 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
323 __connman_ipconfig_set_local(ipconfig, address);
324 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
325 __connman_ipconfig_set_gateway(ipconfig, NULL);
333 static void dhcp_debug(const char *str, void *data)
335 connman_info("%s: %s\n", (const char *) data, str);
338 static int dhcp_request(struct connman_dhcp *dhcp)
340 GDHCPClient *dhcp_client;
341 GDHCPClientError error;
342 const char *hostname;
345 DBG("dhcp %p", dhcp);
347 index = connman_network_get_index(dhcp->network);
349 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
350 if (error != G_DHCP_CLIENT_ERROR_NONE)
353 if (getenv("CONNMAN_DHCP_DEBUG"))
354 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
356 hostname = connman_utsname_get_hostname();
357 if (hostname != NULL)
358 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
360 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
361 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
362 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
363 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
364 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
365 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
366 g_dhcp_client_set_request(dhcp_client, 252);
368 g_dhcp_client_register_event(dhcp_client,
369 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
370 lease_available_cb, dhcp);
372 g_dhcp_client_register_event(dhcp_client,
373 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
374 ipv4ll_available_cb, dhcp);
376 g_dhcp_client_register_event(dhcp_client,
377 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
379 g_dhcp_client_register_event(dhcp_client,
380 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
382 g_dhcp_client_register_event(dhcp_client,
383 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
385 dhcp->dhcp_client = dhcp_client;
387 return g_dhcp_client_start(dhcp_client);
390 static int dhcp_release(struct connman_dhcp *dhcp)
392 DBG("dhcp %p", dhcp);
394 if (dhcp->dhcp_client == NULL)
397 g_dhcp_client_stop(dhcp->dhcp_client);
398 g_dhcp_client_unref(dhcp->dhcp_client);
400 dhcp->dhcp_client = NULL;
405 static void remove_network(gpointer user_data)
407 struct connman_dhcp *dhcp = user_data;
409 DBG("dhcp %p", dhcp);
417 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
419 struct connman_dhcp *dhcp;
423 dhcp = g_try_new0(struct connman_dhcp, 1);
427 dhcp->network = network;
428 dhcp->callback = callback;
430 g_hash_table_replace(network_table, network, dhcp);
432 return dhcp_request(dhcp);
435 void __connman_dhcp_stop(struct connman_network *network)
437 struct connman_dhcp *dhcp;
441 dhcp = g_hash_table_lookup(network_table, network);
447 g_hash_table_remove(network_table, network);
450 int __connman_dhcp_init(void)
454 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
455 NULL, remove_network);
460 void __connman_dhcp_cleanup(void)
464 g_hash_table_destroy(network_table);