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, *net = NULL;
134 char *domainname = NULL, *hostname = NULL;
136 struct connman_ipconfig *ipconfig;
137 struct connman_service *service;
138 unsigned char prefixlen;
141 DBG("Lease available");
143 service = __connman_service_lookup_from_network(dhcp->network);
144 if (service == NULL) {
145 connman_error("Can not lookup service");
149 ipconfig = __connman_service_get_ip4config(service);
150 if (ipconfig == NULL) {
151 connman_error("Could not lookup ipconfig");
155 address = g_dhcp_client_get_address(dhcp_client);
157 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
159 netmask = g_strdup(option->data);
161 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
163 gateway = g_strdup(option->data);
165 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
166 for (ns_entries = 0, list = option; list; list = list->next)
168 dhcp->nameservers = g_try_new0(char *, ns_entries + 1);
169 if (dhcp->nameservers) {
170 for (i = 0, list = option; list; list = list->next)
171 dhcp->nameservers[i] = g_strdup(list->data);
172 dhcp->nameservers[ns_entries] = NULL;
175 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
177 domainname = g_strdup(option->data);
179 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
181 hostname = g_strdup(option->data);
183 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
185 dhcp->timeserver = g_strdup(option->data);
187 option = g_dhcp_client_get_option(dhcp_client, 252);
189 dhcp->pac = g_strdup(option->data);
191 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
193 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
194 __connman_ipconfig_set_local(ipconfig, address);
195 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
196 __connman_ipconfig_set_gateway(ipconfig, gateway);
198 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
199 __connman_service_nameserver_append(service,
200 dhcp->nameservers[i]);
202 __connman_service_timeserver_append(service, dhcp->timeserver);
203 __connman_service_set_pac(service, dhcp->pac);
204 __connman_service_set_domainname(service, domainname);
206 if (domainname != NULL)
207 __connman_utsname_set_domainname(domainname);
209 if (hostname != NULL)
210 __connman_utsname_set_hostname(hostname);
222 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
224 struct connman_dhcp *dhcp = user_data;
225 char *address, *netmask;
226 struct connman_service *service;
227 struct connman_ipconfig *ipconfig;
228 unsigned char prefixlen;
230 DBG("IPV4LL available");
232 service = __connman_service_lookup_from_network(dhcp->network);
236 ipconfig = __connman_service_get_ip4config(service);
237 if (ipconfig == NULL)
240 address = g_dhcp_client_get_address(dhcp_client);
241 netmask = g_dhcp_client_get_netmask(dhcp_client);
243 prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
245 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
246 __connman_ipconfig_set_local(ipconfig, address);
247 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
248 __connman_ipconfig_set_gateway(ipconfig, NULL);
256 static void dhcp_debug(const char *str, void *data)
258 connman_info("%s: %s\n", (const char *) data, str);
261 static int dhcp_request(struct connman_dhcp *dhcp)
263 GDHCPClient *dhcp_client;
264 GDHCPClientError error;
265 const char *hostname;
268 DBG("dhcp %p", dhcp);
270 index = connman_network_get_index(dhcp->network);
272 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
273 if (error != G_DHCP_CLIENT_ERROR_NONE)
276 if (getenv("CONNMAN_DHCP_DEBUG"))
277 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
279 hostname = connman_utsname_get_hostname();
280 if (hostname != NULL)
281 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
283 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
284 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
285 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
286 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
287 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
288 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
289 g_dhcp_client_set_request(dhcp_client, 252);
291 g_dhcp_client_register_event(dhcp_client,
292 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
293 lease_available_cb, dhcp);
295 g_dhcp_client_register_event(dhcp_client,
296 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
297 ipv4ll_available_cb, dhcp);
299 g_dhcp_client_register_event(dhcp_client,
300 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
302 g_dhcp_client_register_event(dhcp_client,
303 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
305 g_dhcp_client_register_event(dhcp_client,
306 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
308 dhcp->dhcp_client = dhcp_client;
310 return g_dhcp_client_start(dhcp_client);
313 static int dhcp_release(struct connman_dhcp *dhcp)
315 DBG("dhcp %p", dhcp);
317 g_dhcp_client_stop(dhcp->dhcp_client);
318 g_dhcp_client_unref(dhcp->dhcp_client);
323 static void remove_network(gpointer user_data)
325 struct connman_dhcp *dhcp = user_data;
327 DBG("dhcp %p", dhcp);
333 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
335 struct connman_dhcp *dhcp;
339 dhcp = g_try_new0(struct connman_dhcp, 1);
343 dhcp->network = network;
344 dhcp->callback = callback;
346 g_hash_table_replace(network_table, network, dhcp);
348 return dhcp_request(dhcp);
351 void __connman_dhcp_stop(struct connman_network *network)
353 struct connman_dhcp *dhcp;
357 dhcp = g_hash_table_lookup(network_table, network);
363 g_hash_table_remove(network_table, network);
366 int __connman_dhcp_init(void)
370 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
371 NULL, remove_network);
376 void __connman_dhcp_cleanup(void)
380 g_hash_table_destroy(network_table);