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;
129 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
131 struct connman_dhcp *dhcp = user_data;
132 GList *list, *option = NULL;
133 char *address, *netmask = NULL, *gateway = NULL;
134 const char *c_address, *c_gateway;
135 char *domainname = NULL, *hostname = NULL;
137 struct connman_ipconfig *ipconfig;
138 struct connman_service *service;
139 unsigned char prefixlen, c_prefixlen;
143 DBG("Lease available");
145 service = __connman_service_lookup_from_network(dhcp->network);
146 if (service == NULL) {
147 connman_error("Can not lookup service");
151 ipconfig = __connman_service_get_ip4config(service);
152 if (ipconfig == NULL) {
153 connman_error("Could not lookup ipconfig");
157 c_address = __connman_ipconfig_get_local(ipconfig);
158 c_gateway = __connman_ipconfig_get_gateway(ipconfig);
159 c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
161 address = g_dhcp_client_get_address(dhcp_client);
163 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
165 netmask = g_strdup(option->data);
167 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
169 gateway = g_strdup(option->data);
171 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
173 DBG("c_address %s", c_address);
175 if (address != NULL && c_address != NULL &&
176 g_strcmp0(address, c_address) != 0)
178 else if (gateway != NULL && c_gateway != NULL &&
179 g_strcmp0(gateway, c_gateway) != 0)
181 else if (prefixlen != c_prefixlen)
183 else if (c_address == NULL || c_gateway == NULL)
189 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
190 for (ns_entries = 0, list = option; list; list = list->next)
192 dhcp->nameservers = g_try_new0(char *, ns_entries + 1);
193 if (dhcp->nameservers) {
194 for (i = 0, list = option; list; list = list->next, i++)
195 dhcp->nameservers[i] = g_strdup(list->data);
196 dhcp->nameservers[ns_entries] = NULL;
199 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
201 domainname = g_strdup(option->data);
203 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
205 hostname = g_strdup(option->data);
207 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
209 dhcp->timeserver = g_strdup(option->data);
211 option = g_dhcp_client_get_option(dhcp_client, 252);
213 dhcp->pac = g_strdup(option->data);
215 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
217 if (ip_change == TRUE) {
218 __connman_ipconfig_set_local(ipconfig, address);
219 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
220 __connman_ipconfig_set_gateway(ipconfig, gateway);
223 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
224 __connman_service_nameserver_append(service,
225 dhcp->nameservers[i]);
227 __connman_service_timeserver_append(service, dhcp->timeserver);
228 __connman_service_set_pac(service, dhcp->pac);
229 __connman_service_set_domainname(service, domainname);
231 if (domainname != NULL)
232 __connman_utsname_set_domainname(domainname);
234 if (hostname != NULL)
235 __connman_utsname_set_hostname(hostname);
237 if (ip_change == TRUE)
247 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
249 struct connman_dhcp *dhcp = user_data;
250 char *address, *netmask;
251 struct connman_service *service;
252 struct connman_ipconfig *ipconfig;
253 unsigned char prefixlen;
255 DBG("IPV4LL available");
257 service = __connman_service_lookup_from_network(dhcp->network);
261 ipconfig = __connman_service_get_ip4config(service);
262 if (ipconfig == NULL)
265 address = g_dhcp_client_get_address(dhcp_client);
266 netmask = g_dhcp_client_get_netmask(dhcp_client);
268 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
270 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
271 __connman_ipconfig_set_local(ipconfig, address);
272 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
273 __connman_ipconfig_set_gateway(ipconfig, NULL);
281 static void dhcp_debug(const char *str, void *data)
283 connman_info("%s: %s\n", (const char *) data, str);
286 static int dhcp_request(struct connman_dhcp *dhcp)
288 GDHCPClient *dhcp_client;
289 GDHCPClientError error;
290 const char *hostname;
293 DBG("dhcp %p", dhcp);
295 index = connman_network_get_index(dhcp->network);
297 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
298 if (error != G_DHCP_CLIENT_ERROR_NONE)
301 if (getenv("CONNMAN_DHCP_DEBUG"))
302 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
304 hostname = connman_utsname_get_hostname();
305 if (hostname != NULL)
306 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
308 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
309 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
310 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
311 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
312 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
313 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
314 g_dhcp_client_set_request(dhcp_client, 252);
316 g_dhcp_client_register_event(dhcp_client,
317 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
318 lease_available_cb, dhcp);
320 g_dhcp_client_register_event(dhcp_client,
321 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
322 ipv4ll_available_cb, dhcp);
324 g_dhcp_client_register_event(dhcp_client,
325 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
327 g_dhcp_client_register_event(dhcp_client,
328 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
330 g_dhcp_client_register_event(dhcp_client,
331 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
333 dhcp->dhcp_client = dhcp_client;
335 return g_dhcp_client_start(dhcp_client);
338 static int dhcp_release(struct connman_dhcp *dhcp)
340 DBG("dhcp %p", dhcp);
342 if (dhcp->dhcp_client == NULL)
345 g_dhcp_client_stop(dhcp->dhcp_client);
346 g_dhcp_client_unref(dhcp->dhcp_client);
348 dhcp->dhcp_client = NULL;
353 static void remove_network(gpointer user_data)
355 struct connman_dhcp *dhcp = user_data;
357 DBG("dhcp %p", dhcp);
365 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
367 struct connman_dhcp *dhcp;
371 dhcp = g_try_new0(struct connman_dhcp, 1);
375 dhcp->network = network;
376 dhcp->callback = callback;
378 g_hash_table_replace(network_table, network, dhcp);
380 return dhcp_request(dhcp);
383 void __connman_dhcp_stop(struct connman_network *network)
385 struct connman_dhcp *dhcp;
389 dhcp = g_hash_table_lookup(network_table, network);
395 g_hash_table_remove(network_table, network);
398 int __connman_dhcp_init(void)
402 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
403 NULL, remove_network);
408 void __connman_dhcp_cleanup(void)
412 g_hash_table_destroy(network_table);