Split DHCP-provided nameserver string.
[platform/upstream/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                 __connman_utsname_set_domainname(value);
128         } else if (g_strcmp0(key, "Hostname") == 0) {
129                 __connman_utsname_set_hostname(value);
130         } else if (g_strcmp0(key, "Timeserver") == 0) {
131                 g_free(dhcp->element->ipv4.timeserver);
132                 dhcp->element->ipv4.timeserver = g_strdup(value);
133         } else if (g_strcmp0(key, "MTU") == 0) {
134         } else if (g_strcmp0(key, "PAC") == 0) {
135                 connman_info("PAC configuration %s", value);
136
137                 g_free(dhcp->element->ipv4.pac);
138                 dhcp->element->ipv4.pac = g_strdup(value);
139         }
140 }
141
142 /**
143  * connman_dhcp_bound:
144  * @dhcp: DHCP structure
145  *
146  * Report successful bound of the interface
147  */
148 void connman_dhcp_bound(struct connman_dhcp *dhcp)
149 {
150         struct connman_element *element;
151
152         DBG("dhcp %p", dhcp);
153
154         element = connman_element_create(NULL);
155         if (element == NULL)
156                 return;
157
158         element->type = CONNMAN_ELEMENT_TYPE_IPV4;
159         element->index = dhcp->index;
160
161         connman_element_update(dhcp->element);
162
163         if (connman_element_register(element, dhcp->element) < 0)
164                 connman_element_unref(element);
165 }
166
167 /**
168  * connman_dhcp_renew:
169  * @dhcp: DHCP structure
170  *
171  * Report successful renew of the interface
172  */
173 void connman_dhcp_renew(struct connman_dhcp *dhcp)
174 {
175         DBG("dhcp %p", dhcp);
176
177         connman_element_update(dhcp->element);
178 }
179
180 /**
181  * connman_dhcp_fail:
182  * @dhcp: DHCP structure
183  *
184  * Report DHCP failure of the interface
185  */
186 void connman_dhcp_fail(struct connman_dhcp *dhcp)
187 {
188         DBG("dhcp %p", dhcp);
189
190         connman_element_set_error(dhcp->element,
191                                         CONNMAN_ELEMENT_ERROR_FAILED);
192 }
193
194 /**
195  * connman_dhcp_get_data:
196  * @dhcp: DHCP structure
197  *
198  * Get private DHCP data pointer
199  */
200 void *connman_dhcp_get_data(struct connman_dhcp *dhcp)
201 {
202         return dhcp->driver_data;
203 }
204
205 /**
206  * connman_dhcp_set_data:
207  * @dhcp: DHCP structure
208  * @data: data pointer
209  *
210  * Set private DHCP data pointer
211  */
212 void connman_dhcp_set_data(struct connman_dhcp *dhcp, void *data)
213 {
214         dhcp->driver_data = data;
215 }
216
217 static GSList *driver_list = NULL;
218
219 static gint compare_priority(gconstpointer a, gconstpointer b)
220 {
221         const struct connman_dhcp_driver *driver1 = a;
222         const struct connman_dhcp_driver *driver2 = b;
223
224         return driver2->priority - driver1->priority;
225 }
226
227 /**
228  * connman_dhcp_driver_register:
229  * @driver: DHCP driver definition
230  *
231  * Register a new DHCP driver
232  *
233  * Returns: %0 on success
234  */
235 int connman_dhcp_driver_register(struct connman_dhcp_driver *driver)
236 {
237         DBG("driver %p name %s", driver, driver->name);
238
239         driver_list = g_slist_insert_sorted(driver_list, driver,
240                                                         compare_priority);
241
242         return 0;
243 }
244
245 /**
246  * connman_dhcp_driver_unregister:
247  * @driver: DHCP driver definition
248  *
249  * Remove a previously registered DHCP driver
250  */
251 void connman_dhcp_driver_unregister(struct connman_dhcp_driver *driver)
252 {
253         DBG("driver %p name %s", driver, driver->name);
254
255         driver_list = g_slist_remove(driver_list, driver);
256 }
257
258 static int dhcp_probe(struct connman_element *element)
259 {
260         struct connman_dhcp *dhcp;
261         GSList *list;
262
263         DBG("element %p name %s", element, element->name);
264
265         dhcp = g_try_new0(struct connman_dhcp, 1);
266         if (dhcp == NULL)
267                 return -ENOMEM;
268
269         dhcp->refcount = 1;
270         dhcp->index = element->index;
271         dhcp->state = CONNMAN_DHCP_STATE_IDLE;
272
273         dhcp->element = element;
274
275         connman_element_set_data(element, dhcp);
276
277         for (list = driver_list; list; list = list->next) {
278                 struct connman_dhcp_driver *driver = list->data;
279
280                 DBG("driver %p name %s", driver, driver->name);
281
282                 if (driver->request(dhcp) == 0) {
283                         dhcp->driver = driver;
284                         break;
285                 }
286         }
287
288         if (dhcp->driver == NULL) {
289                 connman_dhcp_unref(dhcp);
290                 return -ENOENT;
291         }
292
293         return 0;
294 }
295
296 static void dhcp_remove(struct connman_element *element)
297 {
298         struct connman_dhcp *dhcp = connman_element_get_data(element);
299
300         DBG("element %p name %s", element, element->name);
301
302         connman_element_set_data(element, NULL);
303
304         if (dhcp->driver) {
305                 dhcp->driver->release(dhcp);
306                 dhcp->driver = NULL;
307         }
308
309         connman_dhcp_unref(dhcp);
310 }
311
312 static void dhcp_change(struct connman_element *element)
313 {
314         DBG("element %p name %s", element, element->name);
315
316         if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
317                 connman_element_set_error(element->parent,
318                                         CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
319 }
320
321 static struct connman_driver dhcp_driver = {
322         .name           = "dhcp",
323         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
324         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
325         .probe          = dhcp_probe,
326         .remove         = dhcp_remove,
327         .change         = dhcp_change,
328 };
329
330 int __connman_dhcp_init(void)
331 {
332         return connman_driver_register(&dhcp_driver);
333 }
334
335 void __connman_dhcp_cleanup(void)
336 {
337         connman_driver_unregister(&dhcp_driver);
338 }