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