Store host and domain names retrieved from DHCP
[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
26 #include <glib.h>
27
28 #include "connman.h"
29
30 struct connman_dhcp {
31         gint refcount;
32         int index;
33         enum connman_dhcp_state state;
34
35         struct connman_element *element;
36
37         struct connman_dhcp_driver *driver;
38         void *driver_data;
39 };
40
41 /**
42  * connman_dhcp_ref:
43  * @dhcp: DHCP structure
44  *
45  * Increase reference counter of DHCP
46  */
47 struct connman_dhcp *connman_dhcp_ref(struct connman_dhcp *dhcp)
48 {
49         g_atomic_int_inc(&dhcp->refcount);
50
51         return dhcp;
52 }
53
54 /**
55  * connman_dhcp_unref:
56  * @dhcp: DHCP structure
57  *
58  * Decrease reference counter of DHCP
59  */
60 void connman_dhcp_unref(struct connman_dhcp *dhcp)
61 {
62         if (g_atomic_int_dec_and_test(&dhcp->refcount) == TRUE)
63                 g_free(dhcp);
64 }
65
66 /**
67  * connman_dhcp_get_index:
68  * @dhcp: DHCP structure
69  *
70  * Get network index of DHCP
71  */
72 int connman_dhcp_get_index(struct connman_dhcp *dhcp)
73 {
74         return dhcp->index;
75 }
76
77 /**
78  * connman_dhcp_get_interface:
79  * @dhcp: DHCP structure
80  *
81  * Get network interface of DHCP
82  */
83 char *connman_dhcp_get_interface(struct connman_dhcp *dhcp)
84 {
85         return connman_inet_ifname(dhcp->index);
86 }
87
88 /**
89  * connman_dhcp_set_value:
90  * @dhcp: DHCP structure
91  * @key: unique identifier
92  * @value: string value
93  *
94  * Set string value for specific key
95  */
96 void connman_dhcp_set_value(struct connman_dhcp *dhcp,
97                                         const char *key, const char *value)
98 {
99         char **nameservers;
100
101         if (g_strcmp0(key, "Address") == 0) {
102                 g_free(dhcp->element->ipv4.address);
103                 dhcp->element->ipv4.address = g_strdup(value);
104         } else if (g_strcmp0(key, "Netmask") == 0) {
105                 g_free(dhcp->element->ipv4.netmask);
106                 dhcp->element->ipv4.netmask = g_strdup(value);
107         } else if (g_strcmp0(key, "Gateway") == 0) {
108                 g_free(dhcp->element->ipv4.gateway);
109                 dhcp->element->ipv4.gateway = g_strdup(value);
110         } else if (g_strcmp0(key, "Network") == 0) {
111                 g_free(dhcp->element->ipv4.network);
112                 dhcp->element->ipv4.network = g_strdup(value);
113         } else if (g_strcmp0(key, "Broadcast") == 0) {
114                 g_free(dhcp->element->ipv4.broadcast);
115                 dhcp->element->ipv4.broadcast = g_strdup(value);
116         } else if (g_strcmp0(key, "Nameserver") == 0) {
117                 g_free(dhcp->element->ipv4.nameserver);
118                 nameservers = g_strsplit_set(value, " ", 0);
119                 /* FIXME: The ipv4 structure can only hold one nameserver, so
120                  * we are only able to pass along the first nameserver sent by
121                  * the DHCP server.  If this situation changes, we should
122                  * retain all of them.
123                  */
124                 dhcp->element->ipv4.nameserver = g_strdup(nameservers[0]);
125                 g_strfreev(nameservers);
126         } else if (g_strcmp0(key, "Domainname") == 0) {
127                 g_free(dhcp->element->domainname);
128                 dhcp->element->domainname = g_strdup(value);
129                 __connman_utsname_set_domainname(value);
130         } else if (g_strcmp0(key, "Hostname") == 0) {
131                 g_free(dhcp->element->hostname);
132                 dhcp->element->hostname = g_strdup(value);
133                 __connman_utsname_set_hostname(value);
134         } else if (g_strcmp0(key, "Timeserver") == 0) {
135                 g_free(dhcp->element->ipv4.timeserver);
136                 dhcp->element->ipv4.timeserver = g_strdup(value);
137         } else if (g_strcmp0(key, "MTU") == 0) {
138         } else if (g_strcmp0(key, "PAC") == 0) {
139                 connman_info("PAC configuration %s", value);
140
141                 g_free(dhcp->element->ipv4.pac);
142                 dhcp->element->ipv4.pac = g_strdup(value);
143         }
144 }
145
146 /**
147  * connman_dhcp_bound:
148  * @dhcp: DHCP structure
149  *
150  * Report successful bound of the interface
151  */
152 void connman_dhcp_bound(struct connman_dhcp *dhcp)
153 {
154         struct connman_element *element;
155
156         DBG("dhcp %p", dhcp);
157
158         element = connman_element_create(NULL);
159         if (element == NULL)
160                 return;
161
162         element->type = CONNMAN_ELEMENT_TYPE_IPV4;
163         element->index = dhcp->index;
164
165         connman_element_update(dhcp->element);
166
167         if (connman_element_register(element, dhcp->element) < 0)
168                 connman_element_unref(element);
169 }
170
171 /**
172  * connman_dhcp_renew:
173  * @dhcp: DHCP structure
174  *
175  * Report successful renew of the interface
176  */
177 void connman_dhcp_renew(struct connman_dhcp *dhcp)
178 {
179         DBG("dhcp %p", dhcp);
180
181         connman_element_update(dhcp->element);
182 }
183
184 /**
185  * connman_dhcp_fail:
186  * @dhcp: DHCP structure
187  *
188  * Report DHCP failure of the interface
189  */
190 void connman_dhcp_fail(struct connman_dhcp *dhcp)
191 {
192         DBG("dhcp %p", dhcp);
193
194         connman_element_set_error(dhcp->element,
195                                         CONNMAN_ELEMENT_ERROR_FAILED);
196 }
197
198 /**
199  * connman_dhcp_get_data:
200  * @dhcp: DHCP structure
201  *
202  * Get private DHCP data pointer
203  */
204 void *connman_dhcp_get_data(struct connman_dhcp *dhcp)
205 {
206         return dhcp->driver_data;
207 }
208
209 /**
210  * connman_dhcp_set_data:
211  * @dhcp: DHCP structure
212  * @data: data pointer
213  *
214  * Set private DHCP data pointer
215  */
216 void connman_dhcp_set_data(struct connman_dhcp *dhcp, void *data)
217 {
218         dhcp->driver_data = data;
219 }
220
221 static GSList *driver_list = NULL;
222
223 static gint compare_priority(gconstpointer a, gconstpointer b)
224 {
225         const struct connman_dhcp_driver *driver1 = a;
226         const struct connman_dhcp_driver *driver2 = b;
227
228         return driver2->priority - driver1->priority;
229 }
230
231 /**
232  * connman_dhcp_driver_register:
233  * @driver: DHCP driver definition
234  *
235  * Register a new DHCP driver
236  *
237  * Returns: %0 on success
238  */
239 int connman_dhcp_driver_register(struct connman_dhcp_driver *driver)
240 {
241         DBG("driver %p name %s", driver, driver->name);
242
243         driver_list = g_slist_insert_sorted(driver_list, driver,
244                                                         compare_priority);
245
246         return 0;
247 }
248
249 /**
250  * connman_dhcp_driver_unregister:
251  * @driver: DHCP driver definition
252  *
253  * Remove a previously registered DHCP driver
254  */
255 void connman_dhcp_driver_unregister(struct connman_dhcp_driver *driver)
256 {
257         DBG("driver %p name %s", driver, driver->name);
258
259         driver_list = g_slist_remove(driver_list, driver);
260 }
261
262 static int dhcp_probe(struct connman_element *element)
263 {
264         struct connman_dhcp *dhcp;
265         GSList *list;
266
267         DBG("element %p name %s", element, element->name);
268
269         dhcp = g_try_new0(struct connman_dhcp, 1);
270         if (dhcp == NULL)
271                 return -ENOMEM;
272
273         dhcp->refcount = 1;
274         dhcp->index = element->index;
275         dhcp->state = CONNMAN_DHCP_STATE_IDLE;
276
277         dhcp->element = element;
278
279         connman_element_set_data(element, dhcp);
280
281         for (list = driver_list; list; list = list->next) {
282                 struct connman_dhcp_driver *driver = list->data;
283
284                 DBG("driver %p name %s", driver, driver->name);
285
286                 if (driver->request(dhcp) == 0) {
287                         dhcp->driver = driver;
288                         break;
289                 }
290         }
291
292         if (dhcp->driver == NULL) {
293                 connman_dhcp_unref(dhcp);
294                 return -ENOENT;
295         }
296
297         return 0;
298 }
299
300 static void dhcp_remove(struct connman_element *element)
301 {
302         struct connman_dhcp *dhcp = connman_element_get_data(element);
303
304         DBG("element %p name %s", element, element->name);
305
306         connman_element_set_data(element, NULL);
307
308         if (dhcp->driver) {
309                 dhcp->driver->release(dhcp);
310                 dhcp->driver = NULL;
311         }
312
313         connman_dhcp_unref(dhcp);
314 }
315
316 static void dhcp_change(struct connman_element *element)
317 {
318         DBG("element %p name %s", element, element->name);
319
320         if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
321                 connman_element_set_error(element->parent,
322                                         CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
323 }
324
325 static struct connman_driver dhcp_driver = {
326         .name           = "dhcp",
327         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
328         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
329         .probe          = dhcp_probe,
330         .remove         = dhcp_remove,
331         .change         = dhcp_change,
332 };
333
334 int __connman_dhcp_init(void)
335 {
336         return connman_driver_register(&dhcp_driver);
337 }
338
339 void __connman_dhcp_cleanup(void)
340 {
341         connman_driver_unregister(&dhcp_driver);
342 }