Add parsing of Domainname, Hostname and Timeserver results
[framework/connectivity/connman.git] / src / dhcp.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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         if (g_strcmp0(key, "Address") == 0) {
100                 g_free(dhcp->element->ipv4.address);
101                 dhcp->element->ipv4.address = g_strdup(value);
102         } else if (g_strcmp0(key, "Netmask") == 0) {
103                 g_free(dhcp->element->ipv4.netmask);
104                 dhcp->element->ipv4.netmask = g_strdup(value);
105         } else if (g_strcmp0(key, "Gateway") == 0) {
106                 g_free(dhcp->element->ipv4.gateway);
107                 dhcp->element->ipv4.gateway = g_strdup(value);
108         } else if (g_strcmp0(key, "Network") == 0) {
109                 g_free(dhcp->element->ipv4.network);
110                 dhcp->element->ipv4.network = g_strdup(value);
111         } else if (g_strcmp0(key, "Broadcast") == 0) {
112                 g_free(dhcp->element->ipv4.broadcast);
113                 dhcp->element->ipv4.broadcast = g_strdup(value);
114         } else if (g_strcmp0(key, "Nameserver") == 0) {
115                 g_free(dhcp->element->ipv4.nameserver);
116                 dhcp->element->ipv4.nameserver = g_strdup(value);
117         } else if (g_strcmp0(key, "Domainname") == 0) {
118         } else if (g_strcmp0(key, "Hostname") == 0) {
119         } else if (g_strcmp0(key, "Timeserver") == 0) {
120         }
121 }
122
123 /**
124  * connman_dhcp_bound:
125  * @dhcp: DHCP structure
126  *
127  * Report successful bound of the interface
128  */
129 void connman_dhcp_bound(struct connman_dhcp *dhcp)
130 {
131         struct connman_element *element;
132
133         DBG("dhcp %p", dhcp);
134
135         element = connman_element_create(NULL);
136         if (element == NULL)
137                 return;
138
139         element->type = CONNMAN_ELEMENT_TYPE_IPV4;
140         element->index = dhcp->index;
141
142         connman_element_update(dhcp->element);
143
144         if (connman_element_register(element, dhcp->element) < 0)
145                 connman_element_unref(element);
146 }
147
148 /**
149  * connman_dhcp_renew:
150  * @dhcp: DHCP structure
151  *
152  * Report successful renew of the interface
153  */
154 void connman_dhcp_renew(struct connman_dhcp *dhcp)
155 {
156         DBG("dhcp %p", dhcp);
157
158         connman_element_update(dhcp->element);
159 }
160
161 /**
162  * connman_dhcp_fail:
163  * @dhcp: DHCP structure
164  *
165  * Report DHCP failure of the interface
166  */
167 void connman_dhcp_fail(struct connman_dhcp *dhcp)
168 {
169         DBG("dhcp %p", dhcp);
170
171         connman_element_set_error(dhcp->element,
172                                         CONNMAN_ELEMENT_ERROR_FAILED);
173 }
174
175 static GSList *driver_list = NULL;
176
177 static gint compare_priority(gconstpointer a, gconstpointer b)
178 {
179         const struct connman_dhcp_driver *driver1 = a;
180         const struct connman_dhcp_driver *driver2 = b;
181
182         return driver2->priority - driver1->priority;
183 }
184
185 /**
186  * connman_dhcp_driver_register:
187  * @driver: DHCP driver definition
188  *
189  * Register a new DHCP driver
190  *
191  * Returns: %0 on success
192  */
193 int connman_dhcp_driver_register(struct connman_dhcp_driver *driver)
194 {
195         DBG("driver %p name %s", driver, driver->name);
196
197         driver_list = g_slist_insert_sorted(driver_list, driver,
198                                                         compare_priority);
199
200         return 0;
201 }
202
203 /**
204  * connman_dhcp_driver_unregister:
205  * @driver: DHCP driver definition
206  *
207  * Remove a previously registered DHCP driver
208  */
209 void connman_dhcp_driver_unregister(struct connman_dhcp_driver *driver)
210 {
211         DBG("driver %p name %s", driver, driver->name);
212
213         driver_list = g_slist_remove(driver_list, driver);
214 }
215
216 static int dhcp_probe(struct connman_element *element)
217 {
218         struct connman_dhcp *dhcp;
219         GSList *list;
220
221         DBG("element %p name %s", element, element->name);
222
223         dhcp = g_try_new0(struct connman_dhcp, 1);
224         if (dhcp == NULL)
225                 return -ENOMEM;
226
227         dhcp->refcount = 1;
228         dhcp->index = element->index;
229         dhcp->state = CONNMAN_DHCP_STATE_IDLE;
230
231         dhcp->element = element;
232
233         connman_element_set_data(element, dhcp);
234
235         for (list = driver_list; list; list = list->next) {
236                 struct connman_dhcp_driver *driver = list->data;
237
238                 DBG("driver %p name %s", driver, driver->name);
239
240                 if (driver->request(dhcp) == 0) {
241                         dhcp->driver = driver;
242                         break;
243                 }
244         }
245
246         if (dhcp->driver == NULL) {
247                 connman_dhcp_unref(dhcp);
248                 return -ENOENT;
249         }
250
251         return 0;
252 }
253
254 static void dhcp_remove(struct connman_element *element)
255 {
256         struct connman_dhcp *dhcp = connman_element_get_data(element);
257
258         DBG("element %p name %s", element, element->name);
259
260         connman_element_set_data(element, NULL);
261
262         if (dhcp->driver) {
263                 dhcp->driver->release(dhcp);
264                 dhcp->driver = NULL;
265         }
266
267         connman_dhcp_unref(dhcp);
268 }
269
270 static void dhcp_change(struct connman_element *element)
271 {
272         DBG("element %p name %s", element, element->name);
273
274         if (element->state == CONNMAN_ELEMENT_STATE_ERROR)
275                 connman_element_set_error(element->parent,
276                                         CONNMAN_ELEMENT_ERROR_DHCP_FAILED);
277 }
278
279 static struct connman_driver dhcp_driver = {
280         .name           = "dhcp",
281         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
282         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
283         .probe          = dhcp_probe,
284         .remove         = dhcp_remove,
285         .change         = dhcp_change,
286 };
287
288 int __connman_dhcp_init(void)
289 {
290         return connman_driver_register(&dhcp_driver);
291 }
292
293 void __connman_dhcp_cleanup(void)
294 {
295         connman_driver_unregister(&dhcp_driver);
296 }