dhcp: Reset IP settings upon changes
[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;
134         const char *c_address, *c_gateway;
135         char *domainname = NULL, *hostname = NULL;
136         int ns_entries;
137         struct connman_ipconfig *ipconfig;
138         struct connman_service *service;
139         unsigned char prefixlen, c_prefixlen;
140         gboolean ip_change;
141         int i;
142
143         DBG("Lease available");
144
145         service = __connman_service_lookup_from_network(dhcp->network);
146         if (service == NULL) {
147                 connman_error("Can not lookup service");
148                 return;
149         }
150
151         ipconfig = __connman_service_get_ip4config(service);
152         if (ipconfig == NULL) {
153                 connman_error("Could not lookup ipconfig");
154                 return;
155         }
156
157         c_address = __connman_ipconfig_get_local(ipconfig);
158         c_gateway = __connman_ipconfig_get_gateway(ipconfig);
159         c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
160
161         address = g_dhcp_client_get_address(dhcp_client);
162
163         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
164         if (option != NULL)
165                 netmask = g_strdup(option->data);
166
167         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
168         if (option != NULL)
169                 gateway = g_strdup(option->data);
170
171         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
172
173         DBG("c_address %s", c_address);
174
175         if (address != NULL && c_address != NULL &&
176                                         g_strcmp0(address, c_address) != 0)
177                 ip_change = TRUE;
178         else if (gateway != NULL && c_gateway != NULL &&
179                                         g_strcmp0(gateway, c_gateway) != 0)
180                 ip_change = TRUE;
181         else if (prefixlen != c_prefixlen)
182                 ip_change = TRUE;
183         else if (c_address == NULL || c_gateway == NULL)
184                 ip_change = TRUE;
185         else
186                 ip_change = FALSE;
187
188
189         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
190         for (ns_entries = 0, list = option; list; list = list->next)
191                 ns_entries += 1;
192         dhcp->nameservers = g_try_new0(char *, ns_entries + 1);
193         if (dhcp->nameservers) {
194                 for (i = 0, list = option; list; list = list->next, i++)
195                         dhcp->nameservers[i] = g_strdup(list->data);
196                 dhcp->nameservers[ns_entries] = NULL;
197         }
198
199         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
200         if (option != NULL)
201                 domainname = g_strdup(option->data);
202
203         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
204         if (option != NULL)
205                 hostname = g_strdup(option->data);
206
207         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
208         if (option != NULL)
209                 dhcp->timeserver = g_strdup(option->data);
210
211         option = g_dhcp_client_get_option(dhcp_client, 252);
212         if (option != NULL)
213                 dhcp->pac = g_strdup(option->data);
214
215         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
216
217         if (ip_change == TRUE) {
218                 __connman_ipconfig_set_local(ipconfig, address);
219                 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
220                 __connman_ipconfig_set_gateway(ipconfig, gateway);
221         }
222
223         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
224                 __connman_service_nameserver_append(service,
225                                         dhcp->nameservers[i]);
226         }
227         __connman_service_timeserver_append(service, dhcp->timeserver);
228         __connman_service_set_pac(service, dhcp->pac);
229         __connman_service_set_domainname(service, domainname);
230
231         if (domainname != NULL)
232                 __connman_utsname_set_domainname(domainname);
233
234         if (hostname != NULL)
235                 __connman_utsname_set_hostname(hostname);
236
237         if (ip_change == TRUE)
238                 dhcp_valid(dhcp);
239
240         g_free(address);
241         g_free(netmask);
242         g_free(gateway);
243         g_free(domainname);
244         g_free(hostname);
245 }
246
247 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
248 {
249         struct connman_dhcp *dhcp = user_data;
250         char *address, *netmask;
251         struct connman_service *service;
252         struct connman_ipconfig *ipconfig;
253         unsigned char prefixlen;
254
255         DBG("IPV4LL available");
256
257         service = __connman_service_lookup_from_network(dhcp->network);
258         if (service == NULL)
259                 return;
260
261         ipconfig = __connman_service_get_ip4config(service);
262         if (ipconfig == NULL)
263                 return;
264
265         address = g_dhcp_client_get_address(dhcp_client);
266         netmask = g_dhcp_client_get_netmask(dhcp_client);
267
268         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
269
270         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
271         __connman_ipconfig_set_local(ipconfig, address);
272         __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
273         __connman_ipconfig_set_gateway(ipconfig, NULL);
274
275         dhcp_valid(dhcp);
276
277         g_free(address);
278         g_free(netmask);
279 }
280
281 static void dhcp_debug(const char *str, void *data)
282 {
283         connman_info("%s: %s\n", (const char *) data, str);
284 }
285
286 static int dhcp_request(struct connman_dhcp *dhcp)
287 {
288         GDHCPClient *dhcp_client;
289         GDHCPClientError error;
290         const char *hostname;
291         int index;
292
293         DBG("dhcp %p", dhcp);
294
295         index = connman_network_get_index(dhcp->network);
296
297         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
298         if (error != G_DHCP_CLIENT_ERROR_NONE)
299                 return -EINVAL;
300
301         if (getenv("CONNMAN_DHCP_DEBUG"))
302                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
303
304         hostname = connman_utsname_get_hostname();
305         if (hostname != NULL)
306                 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
307
308         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
309         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
310         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
311         g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
312         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
313         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
314         g_dhcp_client_set_request(dhcp_client, 252);
315
316         g_dhcp_client_register_event(dhcp_client,
317                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
318                                                 lease_available_cb, dhcp);
319
320         g_dhcp_client_register_event(dhcp_client,
321                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
322                                                 ipv4ll_available_cb, dhcp);
323
324         g_dhcp_client_register_event(dhcp_client,
325                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
326
327         g_dhcp_client_register_event(dhcp_client,
328                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
329
330         g_dhcp_client_register_event(dhcp_client,
331                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
332
333         dhcp->dhcp_client = dhcp_client;
334
335         return g_dhcp_client_start(dhcp_client);
336 }
337
338 static int dhcp_release(struct connman_dhcp *dhcp)
339 {
340         DBG("dhcp %p", dhcp);
341
342         if (dhcp->dhcp_client == NULL)
343                 return 0;
344
345         g_dhcp_client_stop(dhcp->dhcp_client);
346         g_dhcp_client_unref(dhcp->dhcp_client);
347
348         dhcp->dhcp_client = NULL;
349
350         return 0;
351 }
352
353 static void remove_network(gpointer user_data)
354 {
355         struct connman_dhcp *dhcp = user_data;
356
357         DBG("dhcp %p", dhcp);
358
359         dhcp_release(dhcp);
360
361         dhcp_free(dhcp);
362         g_free(dhcp);
363 }
364
365 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
366 {
367         struct connman_dhcp *dhcp;
368
369         DBG("");
370
371         dhcp = g_try_new0(struct connman_dhcp, 1);
372         if (dhcp == NULL)
373                 return -ENOMEM;
374
375         dhcp->network = network;
376         dhcp->callback = callback;
377
378         g_hash_table_replace(network_table, network, dhcp);
379
380         return dhcp_request(dhcp);
381 }
382
383 void __connman_dhcp_stop(struct connman_network *network)
384 {
385         struct connman_dhcp *dhcp;
386
387         DBG("");
388
389         dhcp = g_hash_table_lookup(network_table, network);
390         if (dhcp == NULL)
391                 return;
392
393         dhcp_release(dhcp);
394
395         g_hash_table_remove(network_table, network);
396 }
397
398 int __connman_dhcp_init(void)
399 {
400         DBG("");
401
402         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
403                                                         NULL, remove_network);
404
405         return 0;
406 }
407
408 void __connman_dhcp_cleanup(void)
409 {
410         DBG("");
411
412         g_hash_table_destroy(network_table);
413 }