dhcp: Stop pending gdhcp client requests
[framework/connectivity/connman.git] / src / dhcp.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <connman/ipconfig.h>
31
32 #include <gdhcp/gdhcp.h>
33
34 #include <glib.h>
35
36 #include "connman.h"
37
38 struct connman_dhcp {
39         struct connman_network *network;
40         dhcp_cb callback;
41
42         char **nameservers;
43         char *timeserver;
44         char *pac;
45
46         GDHCPClient *dhcp_client;
47 };
48
49 static GHashTable *network_table;
50
51 static void dhcp_free(struct connman_dhcp *dhcp)
52 {
53         g_strfreev(dhcp->nameservers);
54         g_free(dhcp->timeserver);
55         g_free(dhcp->pac);
56
57         dhcp->nameservers = NULL;
58         dhcp->timeserver = NULL;
59         dhcp->pac = NULL;
60 }
61
62 static void dhcp_invalid(struct connman_dhcp *dhcp)
63 {
64         struct connman_service *service;
65         struct connman_ipconfig *ipconfig;
66         int i;
67
68         service = __connman_service_lookup_from_network(dhcp->network);
69         if (service == NULL)
70                 return;
71
72         ipconfig = __connman_service_get_ip4config(service);
73         if (ipconfig == NULL)
74                 return;
75
76         __connman_service_set_domainname(service, NULL);
77         __connman_service_set_pac(service, NULL);
78         __connman_service_timeserver_remove(service, dhcp->timeserver);
79
80         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
81                 __connman_service_nameserver_remove(service,
82                                                 dhcp->nameservers[i]);
83         }
84
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);
89
90         if (dhcp->callback != NULL)
91                 dhcp->callback(dhcp->network, FALSE);
92
93         dhcp_free(dhcp);
94 }
95
96 static void dhcp_valid(struct connman_dhcp *dhcp)
97 {
98         if (dhcp->callback != NULL)
99                 dhcp->callback(dhcp->network, TRUE);
100 }
101
102 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
103 {
104         struct connman_dhcp *dhcp = user_data;
105
106         DBG("No lease available");
107
108         dhcp_invalid(dhcp);
109 }
110
111 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
112 {
113         struct connman_dhcp *dhcp = user_data;
114
115         DBG("Lease lost");
116
117         dhcp_invalid(dhcp);
118 }
119
120 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
121 {
122         struct connman_dhcp *dhcp = user_data;
123
124         DBG("Lease lost");
125
126         dhcp_invalid(dhcp);
127 }
128
129 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
130 {
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;
135         int ns_entries;
136         struct connman_ipconfig *ipconfig;
137         struct connman_service *service;
138         unsigned char prefixlen;
139         int i;
140
141         DBG("Lease available");
142
143         service = __connman_service_lookup_from_network(dhcp->network);
144         if (service == NULL) {
145                 connman_error("Can not lookup service");
146                 return;
147         }
148
149         ipconfig = __connman_service_get_ip4config(service);
150         if (ipconfig == NULL) {
151                 connman_error("Could not lookup ipconfig");
152                 return;
153         }
154
155         address = g_dhcp_client_get_address(dhcp_client);
156
157         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
158         if (option != NULL)
159                 netmask = g_strdup(option->data);
160
161         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
162         if (option != NULL)
163                 gateway = g_strdup(option->data);
164
165         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
166         for (ns_entries = 0, list = option; list; list = list->next)
167                 ns_entries += 1;
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;
173         }
174
175         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
176         if (option != NULL)
177                 domainname = g_strdup(option->data);
178
179         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
180         if (option != NULL)
181                 hostname = g_strdup(option->data);
182
183         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
184         if (option != NULL)
185                 dhcp->timeserver = g_strdup(option->data);
186
187         option = g_dhcp_client_get_option(dhcp_client, 252);
188         if (option != NULL)
189                 dhcp->pac = g_strdup(option->data);
190
191         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
192
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);
197
198         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
199                 __connman_service_nameserver_append(service,
200                                         dhcp->nameservers[i]);
201         }
202         __connman_service_timeserver_append(service, dhcp->timeserver);
203         __connman_service_set_pac(service, dhcp->pac);
204         __connman_service_set_domainname(service, domainname);
205
206         if (domainname != NULL)
207                 __connman_utsname_set_domainname(domainname);
208
209         if (hostname != NULL)
210                 __connman_utsname_set_hostname(hostname);
211
212         dhcp_valid(dhcp);
213
214         g_free(address);
215         g_free(netmask);
216         g_free(gateway);
217         g_free(net);
218         g_free(domainname);
219         g_free(hostname);
220 }
221
222 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
223 {
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;
229
230         DBG("IPV4LL available");
231
232         service = __connman_service_lookup_from_network(dhcp->network);
233         if (service == NULL)
234                 return;
235
236         ipconfig = __connman_service_get_ip4config(service);
237         if (ipconfig == NULL)
238                 return;
239
240         address = g_dhcp_client_get_address(dhcp_client);
241         netmask = g_dhcp_client_get_netmask(dhcp_client);
242
243         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
244
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);
249
250         dhcp_valid(dhcp);
251
252         g_free(address);
253         g_free(netmask);
254 }
255
256 static void dhcp_debug(const char *str, void *data)
257 {
258         connman_info("%s: %s\n", (const char *) data, str);
259 }
260
261 static int dhcp_request(struct connman_dhcp *dhcp)
262 {
263         GDHCPClient *dhcp_client;
264         GDHCPClientError error;
265         const char *hostname;
266         int index;
267
268         DBG("dhcp %p", dhcp);
269
270         index = connman_network_get_index(dhcp->network);
271
272         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
273         if (error != G_DHCP_CLIENT_ERROR_NONE)
274                 return -EINVAL;
275
276         if (getenv("CONNMAN_DHCP_DEBUG"))
277                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
278
279         hostname = connman_utsname_get_hostname();
280         if (hostname != NULL)
281                 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
282
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);
290
291         g_dhcp_client_register_event(dhcp_client,
292                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
293                                                 lease_available_cb, dhcp);
294
295         g_dhcp_client_register_event(dhcp_client,
296                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
297                                                 ipv4ll_available_cb, dhcp);
298
299         g_dhcp_client_register_event(dhcp_client,
300                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
301
302         g_dhcp_client_register_event(dhcp_client,
303                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
304
305         g_dhcp_client_register_event(dhcp_client,
306                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
307
308         dhcp->dhcp_client = dhcp_client;
309
310         return g_dhcp_client_start(dhcp_client);
311 }
312
313 static int dhcp_release(struct connman_dhcp *dhcp)
314 {
315         DBG("dhcp %p", dhcp);
316
317         if (dhcp->dhcp_client == NULL)
318                 return 0;
319
320         g_dhcp_client_stop(dhcp->dhcp_client);
321         g_dhcp_client_unref(dhcp->dhcp_client);
322
323         dhcp->dhcp_client = NULL;
324
325         return 0;
326 }
327
328 static void remove_network(gpointer user_data)
329 {
330         struct connman_dhcp *dhcp = user_data;
331
332         DBG("dhcp %p", dhcp);
333
334         dhcp_release(dhcp);
335
336         dhcp_free(dhcp);
337         g_free(dhcp);
338 }
339
340 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
341 {
342         struct connman_dhcp *dhcp;
343
344         DBG("");
345
346         dhcp = g_try_new0(struct connman_dhcp, 1);
347         if (dhcp == NULL)
348                 return -ENOMEM;
349
350         dhcp->network = network;
351         dhcp->callback = callback;
352
353         g_hash_table_replace(network_table, network, dhcp);
354
355         return dhcp_request(dhcp);
356 }
357
358 void __connman_dhcp_stop(struct connman_network *network)
359 {
360         struct connman_dhcp *dhcp;
361
362         DBG("");
363
364         dhcp = g_hash_table_lookup(network_table, network);
365         if (dhcp == NULL)
366                 return;
367
368         dhcp_release(dhcp);
369
370         g_hash_table_remove(network_table, network);
371 }
372
373 int __connman_dhcp_init(void)
374 {
375         DBG("");
376
377         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
378                                                         NULL, remove_network);
379
380         return 0;
381 }
382
383 void __connman_dhcp_cleanup(void)
384 {
385         DBG("");
386
387         g_hash_table_destroy(network_table);
388 }