Adding connman-test subpackage
[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], FALSE);
103                 }
104         }
105
106         __connman_ipconfig_set_dhcp_address(ipconfig,
107                                 __connman_ipconfig_get_local(ipconfig));
108         DBG("last address %s", __connman_ipconfig_get_dhcp_address(ipconfig));
109
110         __connman_ipconfig_address_remove(ipconfig);
111
112         __connman_ipconfig_set_local(ipconfig, NULL);
113         __connman_ipconfig_set_broadcast(ipconfig, NULL);
114         __connman_ipconfig_set_gateway(ipconfig, NULL);
115         __connman_ipconfig_set_prefixlen(ipconfig, 0);
116
117         if (dhcp->callback != NULL && callback)
118                 dhcp->callback(dhcp->network, FALSE);
119
120 out:
121         dhcp_free(dhcp);
122 }
123
124 static void dhcp_valid(struct connman_dhcp *dhcp)
125 {
126         if (dhcp->callback != NULL)
127                 dhcp->callback(dhcp->network, TRUE);
128 }
129
130 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
131 {
132         struct connman_dhcp *dhcp = user_data;
133
134         DBG("No lease available");
135
136         dhcp_invalidate(dhcp, TRUE);
137 }
138
139 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
140 {
141         struct connman_dhcp *dhcp = user_data;
142
143         DBG("Lease lost");
144
145         dhcp_invalidate(dhcp, TRUE);
146 }
147
148 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
149 {
150         struct connman_dhcp *dhcp = user_data;
151
152         DBG("Lease lost");
153
154         dhcp_invalidate(dhcp, TRUE);
155 }
156
157
158 static gboolean compare_string_arrays(char **array_a, char **array_b)
159 {
160         int i;
161
162         if (array_a == NULL || array_b == NULL)
163                 return FALSE;
164
165         if (g_strv_length(array_a) != g_strv_length(array_b))
166                 return FALSE;
167
168         for (i = 0; array_a[i] != NULL &&
169                              array_b[i] != NULL; i++) {
170                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
171                         return FALSE;
172         }
173
174         return TRUE;
175 }
176
177 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
178 {
179         struct connman_dhcp *dhcp = user_data;
180         GList *list, *option = NULL;
181         char *address, *netmask = NULL, *gateway = NULL;
182         const char *c_address, *c_gateway;
183         char *domainname = NULL, *hostname = NULL;
184         char **nameservers, *timeserver = NULL, *pac = NULL;
185         int ns_entries;
186         struct connman_ipconfig *ipconfig;
187         struct connman_service *service;
188         unsigned char prefixlen, c_prefixlen;
189         gboolean ip_change;
190         int i;
191
192         DBG("Lease available");
193
194         service = __connman_service_lookup_from_network(dhcp->network);
195         if (service == NULL) {
196                 connman_error("Can not lookup service");
197                 return;
198         }
199
200         ipconfig = __connman_service_get_ip4config(service);
201         if (ipconfig == NULL) {
202                 connman_error("Could not lookup ipconfig");
203                 return;
204         }
205
206         c_address = __connman_ipconfig_get_local(ipconfig);
207         c_gateway = __connman_ipconfig_get_gateway(ipconfig);
208         c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
209
210         address = g_dhcp_client_get_address(dhcp_client);
211
212         __connman_ipconfig_set_dhcp_address(ipconfig, address);
213         DBG("last address %s", address);
214
215         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
216         if (option != NULL)
217                 netmask = g_strdup(option->data);
218
219         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
220         if (option != NULL)
221                 gateway = g_strdup(option->data);
222
223         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
224
225         DBG("c_address %s", c_address);
226
227         if (address != NULL && c_address != NULL &&
228                                         g_strcmp0(address, c_address) != 0)
229                 ip_change = TRUE;
230         else if (gateway != NULL && c_gateway != NULL &&
231                                         g_strcmp0(gateway, c_gateway) != 0)
232                 ip_change = TRUE;
233         else if (prefixlen != c_prefixlen)
234                 ip_change = TRUE;
235         else if (c_address == NULL || c_gateway == NULL)
236                 ip_change = TRUE;
237         else
238                 ip_change = FALSE;
239
240         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
241         for (ns_entries = 0, list = option; list; list = list->next)
242                 ns_entries += 1;
243         nameservers = g_try_new0(char *, ns_entries + 1);
244         if (nameservers != NULL) {
245                 for (i = 0, list = option; list; list = list->next, i++)
246                         nameservers[i] = g_strdup(list->data);
247                 nameservers[ns_entries] = NULL;
248         }
249
250         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
251         if (option != NULL)
252                 domainname = g_strdup(option->data);
253
254         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
255         if (option != NULL)
256                 hostname = g_strdup(option->data);
257
258         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
259         if (option != NULL)
260                 timeserver = g_strdup(option->data);
261
262         option = g_dhcp_client_get_option(dhcp_client, 252);
263         if (option != NULL)
264                 pac = g_strdup(option->data);
265
266         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
267
268         if (ip_change == TRUE) {
269                 __connman_ipconfig_set_local(ipconfig, address);
270                 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
271                 __connman_ipconfig_set_gateway(ipconfig, gateway);
272         }
273
274         if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
275                 if (dhcp->nameservers != NULL) {
276                         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
277                                 __connman_service_nameserver_remove(service,
278                                                 dhcp->nameservers[i], FALSE);
279                         }
280                         g_strfreev(dhcp->nameservers);
281                 }
282
283                 dhcp->nameservers = nameservers;
284
285                 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
286                         __connman_service_nameserver_append(service,
287                                                 dhcp->nameservers[i], FALSE);
288                 }
289         } else {
290                 g_strfreev(nameservers);
291         }
292
293         if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
294                 if (dhcp->timeserver != NULL) {
295                         __connman_service_timeserver_remove(service,
296                                                         dhcp->timeserver);
297                         g_free(dhcp->timeserver);
298                 }
299
300                 dhcp->timeserver = timeserver;
301
302                 if (dhcp->timeserver != NULL)
303                         __connman_service_timeserver_append(service,
304                                                         dhcp->timeserver);
305         }
306
307         if (g_strcmp0(pac, dhcp->pac) != 0) {
308                 g_free(dhcp->pac);
309                 dhcp->pac = pac;
310
311                 __connman_service_set_pac(service, dhcp->pac);
312         }
313
314         __connman_service_set_domainname(service, domainname);
315
316         if (domainname != NULL)
317                 __connman_utsname_set_domainname(domainname);
318
319         if (hostname != NULL)
320                 __connman_utsname_set_hostname(hostname);
321
322         if (ip_change == TRUE)
323                 dhcp_valid(dhcp);
324
325         __connman_6to4_probe(service);
326
327         g_free(address);
328         g_free(netmask);
329         g_free(gateway);
330         g_free(domainname);
331         g_free(hostname);
332 }
333
334 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
335 {
336         struct connman_dhcp *dhcp = user_data;
337         char *address, *netmask;
338         struct connman_service *service;
339         struct connman_ipconfig *ipconfig;
340         unsigned char prefixlen;
341
342         DBG("IPV4LL available");
343
344 #if defined TIZEN_EXT
345         /*
346          * Description: When DHCP is failed,
347          *              most of naive users cannot understand auto-generated IP
348          *              (IPV4 link local) and serious troubles to make Internet connection.
349          */
350         dhcp_invalidate(dhcp, TRUE);
351
352         service = __connman_service_lookup_from_network(dhcp->network);
353         if (service == NULL)
354                 return;
355
356         __connman_service_ipconfig_indicate_state(service,
357                         CONNMAN_SERVICE_STATE_IDLE,
358                         CONNMAN_IPCONFIG_TYPE_IPV4);
359         __connman_service_ipconfig_indicate_state(service,
360                         CONNMAN_SERVICE_STATE_IDLE,
361                         CONNMAN_IPCONFIG_TYPE_IPV6);
362
363         return;
364 #endif
365
366         service = __connman_service_lookup_from_network(dhcp->network);
367         if (service == NULL)
368                 return;
369
370         ipconfig = __connman_service_get_ip4config(service);
371         if (ipconfig == NULL)
372                 return;
373
374         address = g_dhcp_client_get_address(dhcp_client);
375         netmask = g_dhcp_client_get_netmask(dhcp_client);
376
377         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
378
379         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
380         __connman_ipconfig_set_local(ipconfig, address);
381         __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
382         __connman_ipconfig_set_gateway(ipconfig, NULL);
383
384         dhcp_valid(dhcp);
385
386         g_free(address);
387         g_free(netmask);
388 }
389
390 static void dhcp_debug(const char *str, void *data)
391 {
392         connman_info("%s: %s\n", (const char *) data, str);
393 }
394
395 static int dhcp_request(struct connman_dhcp *dhcp)
396 {
397         struct connman_service *service;
398         struct connman_ipconfig *ipconfig;
399         GDHCPClient *dhcp_client;
400         GDHCPClientError error;
401         const char *hostname;
402         int index;
403 #if defined TIZEN_EXT
404         const char *last_address;
405 #endif
406
407         DBG("dhcp %p", dhcp);
408
409         index = connman_network_get_index(dhcp->network);
410
411         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
412         if (error != G_DHCP_CLIENT_ERROR_NONE)
413                 return -EINVAL;
414
415         if (getenv("CONNMAN_DHCP_DEBUG"))
416                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
417
418         hostname = connman_utsname_get_hostname();
419         if (hostname != NULL)
420                 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
421
422         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
423         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
424         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
425         g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
426         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
427         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
428         g_dhcp_client_set_request(dhcp_client, 252);
429
430         g_dhcp_client_register_event(dhcp_client,
431                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
432                                                 lease_available_cb, dhcp);
433
434         g_dhcp_client_register_event(dhcp_client,
435                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
436                                                 ipv4ll_available_cb, dhcp);
437
438         g_dhcp_client_register_event(dhcp_client,
439                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
440
441         g_dhcp_client_register_event(dhcp_client,
442                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
443
444         g_dhcp_client_register_event(dhcp_client,
445                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
446
447         dhcp->dhcp_client = dhcp_client;
448
449         service = __connman_service_lookup_from_network(dhcp->network);
450         ipconfig = __connman_service_get_ip4config(service);
451
452 #if defined TIZEN_EXT
453         last_address = __connman_ipconfig_get_dhcp_address(ipconfig);
454
455         if (last_address != NULL && strlen(last_address) > 0)
456                 g_dhcp_client_set_address_known(dhcp_client, TRUE);
457
458         return g_dhcp_client_start(dhcp_client, last_address);
459 #else
460         return g_dhcp_client_start(dhcp_client,
461                                 __connman_ipconfig_get_dhcp_address(ipconfig));
462 #endif
463 }
464
465 static int dhcp_release(struct connman_dhcp *dhcp)
466 {
467         DBG("dhcp %p", dhcp);
468
469         if (dhcp->dhcp_client == NULL)
470                 return 0;
471
472         g_dhcp_client_stop(dhcp->dhcp_client);
473         g_dhcp_client_unref(dhcp->dhcp_client);
474
475         dhcp->dhcp_client = NULL;
476
477         return 0;
478 }
479
480 static void remove_network(gpointer user_data)
481 {
482         struct connman_dhcp *dhcp = user_data;
483
484         DBG("dhcp %p", dhcp);
485
486         dhcp_invalidate(dhcp, FALSE);
487         dhcp_release(dhcp);
488
489         g_free(dhcp);
490 }
491
492 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
493 {
494         struct connman_dhcp *dhcp;
495
496         DBG("");
497
498         dhcp = g_try_new0(struct connman_dhcp, 1);
499         if (dhcp == NULL)
500                 return -ENOMEM;
501
502         dhcp->network = network;
503         dhcp->callback = callback;
504
505         connman_network_ref(network);
506
507         g_hash_table_replace(network_table, network, dhcp);
508
509         return dhcp_request(dhcp);
510 }
511
512 void __connman_dhcp_stop(struct connman_network *network)
513 {
514         DBG("");
515
516         if (network_table == NULL)
517                 return;
518
519         if (g_hash_table_remove(network_table, network) == TRUE)
520                 connman_network_unref(network);
521 }
522
523 int __connman_dhcp_init(void)
524 {
525         DBG("");
526
527         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
528                                                         NULL, remove_network);
529
530         return 0;
531 }
532
533 void __connman_dhcp_cleanup(void)
534 {
535         DBG("");
536
537         g_hash_table_destroy(network_table);
538         network_table = NULL;
539 }