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 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
130 static gboolean compare_string_arrays(char **array_a, char **array_b)
131 {
132         int i;
133
134         if (array_a == NULL || array_b == NULL)
135                 return FALSE;
136
137         if (g_strv_length(array_a) != g_strv_length(array_b))
138                 return FALSE;
139
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)
143                         return FALSE;
144         }
145
146         return TRUE;
147 }
148
149 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
150 {
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;
157         int ns_entries;
158         struct connman_ipconfig *ipconfig;
159         struct connman_service *service;
160         unsigned char prefixlen, c_prefixlen;
161         gboolean ip_change;
162         int i;
163
164         DBG("Lease available");
165
166         service = __connman_service_lookup_from_network(dhcp->network);
167         if (service == NULL) {
168                 connman_error("Can not lookup service");
169                 return;
170         }
171
172         ipconfig = __connman_service_get_ip4config(service);
173         if (ipconfig == NULL) {
174                 connman_error("Could not lookup ipconfig");
175                 return;
176         }
177
178         c_address = __connman_ipconfig_get_local(ipconfig);
179         c_gateway = __connman_ipconfig_get_gateway(ipconfig);
180         c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
181
182         address = g_dhcp_client_get_address(dhcp_client);
183
184         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
185         if (option != NULL)
186                 netmask = g_strdup(option->data);
187
188         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
189         if (option != NULL)
190                 gateway = g_strdup(option->data);
191
192         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
193
194         DBG("c_address %s", c_address);
195
196         if (address != NULL && c_address != NULL &&
197                                         g_strcmp0(address, c_address) != 0)
198                 ip_change = TRUE;
199         else if (gateway != NULL && c_gateway != NULL &&
200                                         g_strcmp0(gateway, c_gateway) != 0)
201                 ip_change = TRUE;
202         else if (prefixlen != c_prefixlen)
203                 ip_change = TRUE;
204         else if (c_address == NULL || c_gateway == NULL)
205                 ip_change = TRUE;
206         else
207                 ip_change = FALSE;
208
209         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
210         for (ns_entries = 0, list = option; list; list = list->next)
211                 ns_entries += 1;
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;
217         }
218
219         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
220         if (option != NULL)
221                 domainname = g_strdup(option->data);
222
223         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
224         if (option != NULL)
225                 hostname = g_strdup(option->data);
226
227         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
228         if (option != NULL)
229                 timeserver = g_strdup(option->data);
230
231         option = g_dhcp_client_get_option(dhcp_client, 252);
232         if (option != NULL)
233                 pac = g_strdup(option->data);
234
235         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
236
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);
241         }
242
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]);
248                         }
249                         g_strfreev(dhcp->nameservers);
250                 }
251
252                 dhcp->nameservers = nameservers;
253
254                 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
255                         __connman_service_nameserver_append(service,
256                                                         dhcp->nameservers[i]);
257                 }
258         }
259
260         if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
261                 if (dhcp->timeserver != NULL) {
262                         __connman_service_timeserver_remove(service,
263                                                         dhcp->timeserver);
264                         g_free(dhcp->timeserver);
265                 }
266
267                 dhcp->timeserver = timeserver;
268
269                 if (dhcp->timeserver != NULL)
270                         __connman_service_timeserver_append(service,
271                                                         dhcp->timeserver);
272         }
273
274         if (g_strcmp0(pac, dhcp->pac) != 0) {
275                 g_free(dhcp->pac);
276                 dhcp->pac = pac;
277
278                 __connman_service_set_pac(service, dhcp->pac);
279         }
280
281         __connman_service_set_domainname(service, domainname);
282
283         if (domainname != NULL)
284                 __connman_utsname_set_domainname(domainname);
285
286         if (hostname != NULL)
287                 __connman_utsname_set_hostname(hostname);
288
289         if (ip_change == TRUE)
290                 dhcp_valid(dhcp);
291
292         g_free(address);
293         g_free(netmask);
294         g_free(gateway);
295         g_free(domainname);
296         g_free(hostname);
297 }
298
299 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
300 {
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;
306
307         DBG("IPV4LL available");
308
309         service = __connman_service_lookup_from_network(dhcp->network);
310         if (service == NULL)
311                 return;
312
313         ipconfig = __connman_service_get_ip4config(service);
314         if (ipconfig == NULL)
315                 return;
316
317         address = g_dhcp_client_get_address(dhcp_client);
318         netmask = g_dhcp_client_get_netmask(dhcp_client);
319
320         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
321
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);
326
327         dhcp_valid(dhcp);
328
329         g_free(address);
330         g_free(netmask);
331 }
332
333 static void dhcp_debug(const char *str, void *data)
334 {
335         connman_info("%s: %s\n", (const char *) data, str);
336 }
337
338 static int dhcp_request(struct connman_dhcp *dhcp)
339 {
340         GDHCPClient *dhcp_client;
341         GDHCPClientError error;
342         const char *hostname;
343         int index;
344
345         DBG("dhcp %p", dhcp);
346
347         index = connman_network_get_index(dhcp->network);
348
349         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
350         if (error != G_DHCP_CLIENT_ERROR_NONE)
351                 return -EINVAL;
352
353         if (getenv("CONNMAN_DHCP_DEBUG"))
354                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
355
356         hostname = connman_utsname_get_hostname();
357         if (hostname != NULL)
358                 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
359
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);
367
368         g_dhcp_client_register_event(dhcp_client,
369                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
370                                                 lease_available_cb, dhcp);
371
372         g_dhcp_client_register_event(dhcp_client,
373                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
374                                                 ipv4ll_available_cb, dhcp);
375
376         g_dhcp_client_register_event(dhcp_client,
377                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
378
379         g_dhcp_client_register_event(dhcp_client,
380                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
381
382         g_dhcp_client_register_event(dhcp_client,
383                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
384
385         dhcp->dhcp_client = dhcp_client;
386
387         return g_dhcp_client_start(dhcp_client);
388 }
389
390 static int dhcp_release(struct connman_dhcp *dhcp)
391 {
392         DBG("dhcp %p", dhcp);
393
394         if (dhcp->dhcp_client == NULL)
395                 return 0;
396
397         g_dhcp_client_stop(dhcp->dhcp_client);
398         g_dhcp_client_unref(dhcp->dhcp_client);
399
400         dhcp->dhcp_client = NULL;
401
402         return 0;
403 }
404
405 static void remove_network(gpointer user_data)
406 {
407         struct connman_dhcp *dhcp = user_data;
408
409         DBG("dhcp %p", dhcp);
410
411         dhcp_release(dhcp);
412
413         dhcp_free(dhcp);
414         g_free(dhcp);
415 }
416
417 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
418 {
419         struct connman_dhcp *dhcp;
420
421         DBG("");
422
423         dhcp = g_try_new0(struct connman_dhcp, 1);
424         if (dhcp == NULL)
425                 return -ENOMEM;
426
427         dhcp->network = network;
428         dhcp->callback = callback;
429
430         g_hash_table_replace(network_table, network, dhcp);
431
432         return dhcp_request(dhcp);
433 }
434
435 void __connman_dhcp_stop(struct connman_network *network)
436 {
437         struct connman_dhcp *dhcp;
438
439         DBG("");
440
441         dhcp = g_hash_table_lookup(network_table, network);
442         if (dhcp == NULL)
443                 return;
444
445         dhcp_release(dhcp);
446
447         g_hash_table_remove(network_table, network);
448 }
449
450 int __connman_dhcp_init(void)
451 {
452         DBG("");
453
454         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
455                                                         NULL, remove_network);
456
457         return 0;
458 }
459
460 void __connman_dhcp_cleanup(void)
461 {
462         DBG("");
463
464         g_hash_table_destroy(network_table);
465 }