dhcp: Fix memory leak
[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 /**
63  * dhcp_invalidate: Invalidate an existing DHCP lease
64  * @dhcp: pointer to the DHCP lease to invalidate.
65  * @callback: flag indicating whether or not to invoke the client callback
66  *            if present.
67  *
68  * Invalidates an existing DHCP lease, optionally invoking the client
69  * callback. The caller may wish to avoid the client callback invocation
70  * when the invocation of that callback might otherwise unnecessarily upset
71  * service state due to the IP configuration change implied by this
72  * invalidation.
73  */
74 static void dhcp_invalidate(struct connman_dhcp *dhcp, connman_bool_t callback)
75 {
76         struct connman_service *service;
77         struct connman_ipconfig *ipconfig;
78         int i;
79
80         DBG("dhcp %p callback %u", dhcp, callback);
81
82         if (dhcp == NULL)
83                 return;
84
85         service = __connman_service_lookup_from_network(dhcp->network);
86         if (service == NULL)
87                 goto out;
88
89         ipconfig = __connman_service_get_ip4config(service);
90         if (ipconfig == NULL)
91                 goto out;
92
93         __connman_6to4_remove(ipconfig);
94
95         __connman_service_set_domainname(service, NULL);
96         __connman_service_set_pac(service, NULL);
97         __connman_service_timeserver_remove(service, dhcp->timeserver);
98
99         if (dhcp->nameservers != NULL) {
100                 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
101                         __connman_service_nameserver_remove(service,
102                                                         dhcp->nameservers[i]);
103                 }
104         }
105
106         __connman_ipconfig_address_remove(ipconfig);
107
108         __connman_ipconfig_set_local(ipconfig, NULL);
109         __connman_ipconfig_set_broadcast(ipconfig, NULL);
110         __connman_ipconfig_set_gateway(ipconfig, NULL);
111         __connman_ipconfig_set_prefixlen(ipconfig, 0);
112
113         if (dhcp->callback != NULL && callback)
114                 dhcp->callback(dhcp->network, FALSE);
115
116 out:
117         dhcp_free(dhcp);
118 }
119
120 static void dhcp_valid(struct connman_dhcp *dhcp)
121 {
122         if (dhcp->callback != NULL)
123                 dhcp->callback(dhcp->network, TRUE);
124 }
125
126 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
127 {
128         struct connman_dhcp *dhcp = user_data;
129
130         DBG("No lease available");
131
132         dhcp_invalidate(dhcp, TRUE);
133 }
134
135 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
136 {
137         struct connman_dhcp *dhcp = user_data;
138
139         DBG("Lease lost");
140
141         dhcp_invalidate(dhcp, TRUE);
142 }
143
144 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
145 {
146         struct connman_dhcp *dhcp = user_data;
147
148         DBG("Lease lost");
149
150         dhcp_invalidate(dhcp, TRUE);
151 }
152
153
154 static gboolean compare_string_arrays(char **array_a, char **array_b)
155 {
156         int i;
157
158         if (array_a == NULL || array_b == NULL)
159                 return FALSE;
160
161         if (g_strv_length(array_a) != g_strv_length(array_b))
162                 return FALSE;
163
164         for (i = 0; array_a[i] != NULL &&
165                              array_b[i] != NULL; i++) {
166                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
167                         return FALSE;
168         }
169
170         return TRUE;
171 }
172
173 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
174 {
175         struct connman_dhcp *dhcp = user_data;
176         GList *list, *option = NULL;
177         char *address, *netmask = NULL, *gateway = NULL;
178         const char *c_address, *c_gateway;
179         char *domainname = NULL, *hostname = NULL;
180         char **nameservers, *timeserver = NULL, *pac = NULL;
181         int ns_entries;
182         struct connman_ipconfig *ipconfig;
183         struct connman_service *service;
184         unsigned char prefixlen, c_prefixlen;
185         gboolean ip_change;
186         int i;
187
188         DBG("Lease available");
189
190         service = __connman_service_lookup_from_network(dhcp->network);
191         if (service == NULL) {
192                 connman_error("Can not lookup service");
193                 return;
194         }
195
196         ipconfig = __connman_service_get_ip4config(service);
197         if (ipconfig == NULL) {
198                 connman_error("Could not lookup ipconfig");
199                 return;
200         }
201
202         c_address = __connman_ipconfig_get_local(ipconfig);
203         c_gateway = __connman_ipconfig_get_gateway(ipconfig);
204         c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
205
206         address = g_dhcp_client_get_address(dhcp_client);
207
208         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
209         if (option != NULL)
210                 netmask = g_strdup(option->data);
211
212         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
213         if (option != NULL)
214                 gateway = g_strdup(option->data);
215
216         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
217
218         DBG("c_address %s", c_address);
219
220         if (address != NULL && c_address != NULL &&
221                                         g_strcmp0(address, c_address) != 0)
222                 ip_change = TRUE;
223         else if (gateway != NULL && c_gateway != NULL &&
224                                         g_strcmp0(gateway, c_gateway) != 0)
225                 ip_change = TRUE;
226         else if (prefixlen != c_prefixlen)
227                 ip_change = TRUE;
228         else if (c_address == NULL || c_gateway == NULL)
229                 ip_change = TRUE;
230         else
231                 ip_change = FALSE;
232
233         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
234         for (ns_entries = 0, list = option; list; list = list->next)
235                 ns_entries += 1;
236         nameservers = g_try_new0(char *, ns_entries + 1);
237         if (nameservers != NULL) {
238                 for (i = 0, list = option; list; list = list->next, i++)
239                         nameservers[i] = g_strdup(list->data);
240                 nameservers[ns_entries] = NULL;
241         }
242
243         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
244         if (option != NULL)
245                 domainname = g_strdup(option->data);
246
247         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
248         if (option != NULL)
249                 hostname = g_strdup(option->data);
250
251         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
252         if (option != NULL)
253                 timeserver = g_strdup(option->data);
254
255         option = g_dhcp_client_get_option(dhcp_client, 252);
256         if (option != NULL)
257                 pac = g_strdup(option->data);
258
259         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
260
261         if (ip_change == TRUE) {
262                 __connman_ipconfig_set_local(ipconfig, address);
263                 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
264                 __connman_ipconfig_set_gateway(ipconfig, gateway);
265         }
266
267         if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
268                 if (dhcp->nameservers != NULL) {
269                         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
270                                 __connman_service_nameserver_remove(service,
271                                                         dhcp->nameservers[i]);
272                         }
273                         g_strfreev(dhcp->nameservers);
274                 }
275
276                 dhcp->nameservers = nameservers;
277
278                 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
279                         __connman_service_nameserver_append(service,
280                                                         dhcp->nameservers[i]);
281                 }
282         } else {
283                 g_strfreev(nameservers);
284         }
285
286         if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
287                 if (dhcp->timeserver != NULL) {
288                         __connman_service_timeserver_remove(service,
289                                                         dhcp->timeserver);
290                         g_free(dhcp->timeserver);
291                 }
292
293                 dhcp->timeserver = timeserver;
294
295                 if (dhcp->timeserver != NULL)
296                         __connman_service_timeserver_append(service,
297                                                         dhcp->timeserver);
298         }
299
300         if (g_strcmp0(pac, dhcp->pac) != 0) {
301                 g_free(dhcp->pac);
302                 dhcp->pac = pac;
303
304                 __connman_service_set_pac(service, dhcp->pac);
305         }
306
307         __connman_service_set_domainname(service, domainname);
308
309         if (domainname != NULL)
310                 __connman_utsname_set_domainname(domainname);
311
312         if (hostname != NULL)
313                 __connman_utsname_set_hostname(hostname);
314
315         if (ip_change == TRUE)
316                 dhcp_valid(dhcp);
317
318         __connman_6to4_probe(service);
319
320         g_free(address);
321         g_free(netmask);
322         g_free(gateway);
323         g_free(domainname);
324         g_free(hostname);
325 }
326
327 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
328 {
329         struct connman_dhcp *dhcp = user_data;
330         char *address, *netmask;
331         struct connman_service *service;
332         struct connman_ipconfig *ipconfig;
333         unsigned char prefixlen;
334
335         DBG("IPV4LL available");
336
337         service = __connman_service_lookup_from_network(dhcp->network);
338         if (service == NULL)
339                 return;
340
341         ipconfig = __connman_service_get_ip4config(service);
342         if (ipconfig == NULL)
343                 return;
344
345         address = g_dhcp_client_get_address(dhcp_client);
346         netmask = g_dhcp_client_get_netmask(dhcp_client);
347
348         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
349
350         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
351         __connman_ipconfig_set_local(ipconfig, address);
352         __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
353         __connman_ipconfig_set_gateway(ipconfig, NULL);
354
355         dhcp_valid(dhcp);
356
357         g_free(address);
358         g_free(netmask);
359 }
360
361 static void dhcp_debug(const char *str, void *data)
362 {
363         connman_info("%s: %s\n", (const char *) data, str);
364 }
365
366 static int dhcp_request(struct connman_dhcp *dhcp)
367 {
368         GDHCPClient *dhcp_client;
369         GDHCPClientError error;
370         const char *hostname;
371         int index;
372
373         DBG("dhcp %p", dhcp);
374
375         index = connman_network_get_index(dhcp->network);
376
377         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
378         if (error != G_DHCP_CLIENT_ERROR_NONE)
379                 return -EINVAL;
380
381         if (getenv("CONNMAN_DHCP_DEBUG"))
382                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
383
384         hostname = connman_utsname_get_hostname();
385         if (hostname != NULL)
386                 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
387
388         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
389         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
390         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
391         g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
392         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
393         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
394         g_dhcp_client_set_request(dhcp_client, 252);
395
396         g_dhcp_client_register_event(dhcp_client,
397                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
398                                                 lease_available_cb, dhcp);
399
400         g_dhcp_client_register_event(dhcp_client,
401                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
402                                                 ipv4ll_available_cb, dhcp);
403
404         g_dhcp_client_register_event(dhcp_client,
405                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
406
407         g_dhcp_client_register_event(dhcp_client,
408                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
409
410         g_dhcp_client_register_event(dhcp_client,
411                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
412
413         dhcp->dhcp_client = dhcp_client;
414
415         return g_dhcp_client_start(dhcp_client);
416 }
417
418 static int dhcp_release(struct connman_dhcp *dhcp)
419 {
420         DBG("dhcp %p", dhcp);
421
422         if (dhcp->dhcp_client == NULL)
423                 return 0;
424
425         g_dhcp_client_stop(dhcp->dhcp_client);
426         g_dhcp_client_unref(dhcp->dhcp_client);
427
428         dhcp->dhcp_client = NULL;
429
430         return 0;
431 }
432
433 static void remove_network(gpointer user_data)
434 {
435         struct connman_dhcp *dhcp = user_data;
436
437         DBG("dhcp %p", dhcp);
438
439         dhcp_invalidate(dhcp, FALSE);
440         dhcp_release(dhcp);
441
442         g_free(dhcp);
443 }
444
445 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
446 {
447         struct connman_dhcp *dhcp;
448
449         DBG("");
450
451         dhcp = g_try_new0(struct connman_dhcp, 1);
452         if (dhcp == NULL)
453                 return -ENOMEM;
454
455         dhcp->network = network;
456         dhcp->callback = callback;
457
458         g_hash_table_replace(network_table, network, dhcp);
459
460         return dhcp_request(dhcp);
461 }
462
463 void __connman_dhcp_stop(struct connman_network *network)
464 {
465         DBG("");
466
467         if (network_table == NULL)
468                 return;
469
470         g_hash_table_remove(network_table, network);
471 }
472
473 int __connman_dhcp_init(void)
474 {
475         DBG("");
476
477         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
478                                                         NULL, remove_network);
479
480         return 0;
481 }
482
483 void __connman_dhcp_cleanup(void)
484 {
485         DBG("");
486
487         g_hash_table_destroy(network_table);
488         network_table = NULL;
489 }