Add function to get index value from DHCP structure
[platform/upstream/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_bound:
90  * @dhcp: DHCP structure
91  *
92  * Report successful bound of the interface
93  */
94 void connman_dhcp_bound(struct connman_dhcp *dhcp)
95 {
96         DBG("dhcp %p", dhcp);
97 }
98
99 static GSList *driver_list = NULL;
100
101 static gint compare_priority(gconstpointer a, gconstpointer b)
102 {
103         const struct connman_dhcp_driver *driver1 = a;
104         const struct connman_dhcp_driver *driver2 = b;
105
106         return driver2->priority - driver1->priority;
107 }
108
109 /**
110  * connman_dhcp_driver_register:
111  * @driver: DHCP driver definition
112  *
113  * Register a new DHCP driver
114  *
115  * Returns: %0 on success
116  */
117 int connman_dhcp_driver_register(struct connman_dhcp_driver *driver)
118 {
119         DBG("driver %p name %s", driver, driver->name);
120
121         driver_list = g_slist_insert_sorted(driver_list, driver,
122                                                         compare_priority);
123
124         return 0;
125 }
126
127 /**
128  * connman_dhcp_driver_unregister:
129  * @driver: DHCP driver definition
130  *
131  * Remove a previously registered DHCP driver
132  */
133 void connman_dhcp_driver_unregister(struct connman_dhcp_driver *driver)
134 {
135         DBG("driver %p name %s", driver, driver->name);
136
137         driver_list = g_slist_remove(driver_list, driver);
138 }
139
140 static int dhcp_probe(struct connman_element *element)
141 {
142         struct connman_dhcp *dhcp;
143         GSList *list;
144
145         DBG("element %p name %s", element, element->name);
146
147         dhcp = g_try_new0(struct connman_dhcp, 1);
148         if (dhcp == NULL)
149                 return -ENOMEM;
150
151         dhcp->refcount = 1;
152         dhcp->index = element->index;
153         dhcp->state = CONNMAN_DHCP_STATE_IDLE;
154
155         dhcp->element = element;
156
157         connman_element_set_data(element, dhcp);
158
159         for (list = driver_list; list; list = list->next) {
160                 struct connman_dhcp_driver *driver = list->data;
161
162                 DBG("driver %p name %s", driver, driver->name);
163
164                 if (driver->request(dhcp) == 0) {
165                         dhcp->driver = driver;
166                         break;
167                 }
168         }
169
170         if (dhcp->driver == NULL) {
171                 connman_dhcp_unref(dhcp);
172                 return -ENOENT;
173         }
174
175         return 0;
176 }
177
178 static void dhcp_remove(struct connman_element *element)
179 {
180         struct connman_dhcp *dhcp = connman_element_get_data(element);
181
182         DBG("element %p name %s", element, element->name);
183
184         connman_element_set_data(element, NULL);
185
186         if (dhcp->driver) {
187                 dhcp->driver->release(dhcp);
188                 dhcp->driver = NULL;
189         }
190
191         connman_dhcp_unref(dhcp);
192 }
193
194 static struct connman_driver dhcp_driver = {
195         .name           = "dhcp",
196         .type           = CONNMAN_ELEMENT_TYPE_DHCP,
197         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
198         .probe          = dhcp_probe,
199         .remove         = dhcp_remove,
200 };
201
202 int __connman_dhcp_init(void)
203 {
204         return connman_driver_register(&dhcp_driver);
205 }
206
207 void __connman_dhcp_cleanup(void)
208 {
209         connman_driver_unregister(&dhcp_driver);
210 }