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