2e3d22bc25f70b3e6e5cea3b03816183c502b8e6
[platform/upstream/connman.git] / src / ipconfig.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 <gdbus.h>
27
28 #include "connman.h"
29
30 struct connman_ipconfig {
31         gint refcount;
32         int index;
33         char *interface;
34         enum connman_ipconfig_method method;
35 };
36
37 /**
38  * connman_ipconfig_create:
39  *
40  * Allocate a new ipconfig structure.
41  *
42  * Returns: a newly-allocated #connman_ipconfig structure
43  */
44 struct connman_ipconfig *connman_ipconfig_create(const char *interface)
45 {
46         struct connman_ipconfig *ipconfig;
47         int index;
48
49         DBG("");
50
51         index = connman_inet_ifindex(interface);
52         if (index < 0)
53                 return NULL;
54
55         ipconfig = g_try_new0(struct connman_ipconfig, 1);
56         if (ipconfig == NULL)
57                 return NULL;
58
59         ipconfig->index = index;
60         ipconfig->interface = g_strdup(interface);
61
62         DBG("ipconfig %p", ipconfig);
63
64         __connman_rtnl_register_ipconfig(ipconfig);
65
66         return ipconfig;
67 }
68
69 /**
70  * connman_ipconfig_ref:
71  * @ipconfig: ipconfig structure
72  *
73  * Increase reference counter of ipconfig
74  */
75 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
76 {
77         g_atomic_int_inc(&ipconfig->refcount);
78
79         return ipconfig;
80 }
81
82 /**
83  * connman_ipconfig_unref:
84  * @ipconfig: ipconfig structure
85  *
86  * Decrease reference counter of ipconfig
87  */
88 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
89 {
90         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
91                 __connman_rtnl_unregister_ipconfig(ipconfig);
92
93                 g_free(ipconfig->interface);
94                 g_free(ipconfig);
95         }
96 }
97
98 /**
99  * connman_ipconfig_set_method:
100  * @ipconfig: ipconfig structure
101  * @method: configuration method
102  *
103  * Set the configuration method
104  */
105 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
106                                         enum connman_ipconfig_method method)
107 {
108         ipconfig->method = method;
109
110         return 0;
111 }
112
113 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
114 {
115         return ipconfig->index;
116 }
117
118 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
119                                 const char *label, unsigned int prefixlen,
120                                 const char *address, const char *broadcast)
121 {
122         connman_info("%s {add} address %s/%d label %s", ipconfig->interface,
123                                                 address, prefixlen, label);
124 }
125
126 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
127                                 const char *label, unsigned int prefixlen,
128                                 const char *address, const char *broadcast)
129 {
130         connman_info("%s {del} address %s/%d label %s", ipconfig->interface,
131                                                 address, prefixlen, label);
132 }
133
134 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
135 {
136         switch (method) {
137         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
138                 break;
139         case CONNMAN_IPCONFIG_METHOD_OFF:
140                 return "off";
141         case CONNMAN_IPCONFIG_METHOD_STATIC:
142                 return "static";
143         case CONNMAN_IPCONFIG_METHOD_DHCP:
144                 return "dhcp";
145         }
146
147         return NULL;
148 }
149
150 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
151 {
152         if (g_strcmp0(method, "off") == 0)
153                 return CONNMAN_IPCONFIG_METHOD_OFF;
154         else if (g_strcmp0(method, "static") == 0)
155                 return CONNMAN_IPCONFIG_METHOD_STATIC;
156         else if (g_strcmp0(method, "dhcp") == 0)
157                 return CONNMAN_IPCONFIG_METHOD_DHCP;
158         else
159                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
160 }
161
162 static void append_variant(DBusMessageIter *iter, const char *prefix,
163                                         const char *key, int type, void *val)
164 {
165         char *str;
166
167         if (prefix == NULL) {
168                 connman_dbus_dict_append_variant(iter, key, type, val);
169                 return;
170         }
171
172         str = g_strdup_printf("%s%s", prefix, key);
173         if (str != NULL)
174                 connman_dbus_dict_append_variant(iter, str, type, val);
175
176         g_free(str);
177 }
178
179 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
180                                 DBusMessageIter *iter, const char *prefix)
181 {
182         const char *str;
183
184         str = __connman_ipconfig_method2string(ipconfig->method);
185         if (str == NULL)
186                 return;
187
188         append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
189 }
190
191 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
192                                 const char *key, DBusMessageIter *value)
193 {
194         int type = dbus_message_iter_get_arg_type(value);
195
196         DBG("ipconfig %p key %s type %d", ipconfig, key, type);
197
198         if (g_strcmp0(key, "Method") == 0) {
199                 const char *method;
200
201                 if (type != DBUS_TYPE_STRING)
202                         return -EINVAL;
203
204                 dbus_message_iter_get_basic(value, &method);
205
206                 ipconfig->method = __connman_ipconfig_string2method(method);
207         } else
208                 return -EINVAL;
209
210         return 0;
211 }
212
213 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
214                 GKeyFile *keyfile, const char *identifier, const char *prefix)
215 {
216         DBG("ipconfig %p identifier %s", ipconfig, identifier);
217
218         return 0;
219 }
220
221 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
222                 GKeyFile *keyfile, const char *identifier, const char *prefix)
223 {
224         DBG("ipconfig %p identifier %s", ipconfig, identifier);
225
226         return 0;
227 }
228
229 static GSList *driver_list = NULL;
230
231 static gint compare_priority(gconstpointer a, gconstpointer b)
232 {
233         const struct connman_ipconfig_driver *driver1 = a;
234         const struct connman_ipconfig_driver *driver2 = b;
235
236         return driver2->priority - driver1->priority;
237 }
238
239 /**
240  * connman_ipconfig_driver_register:
241  * @driver: IP configuration driver
242  *
243  * Register a new IP configuration driver
244  *
245  * Returns: %0 on success
246  */
247 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
248 {
249         DBG("driver %p name %s", driver, driver->name);
250
251         driver_list = g_slist_insert_sorted(driver_list, driver,
252                                                         compare_priority);
253
254         return 0;
255 }
256
257 /**
258  * connman_ipconfig_driver_unregister:
259  * @driver: IP configuration driver
260  *
261  * Remove a previously registered IP configuration driver.
262  */
263 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
264 {
265         DBG("driver %p name %s", driver, driver->name);
266
267         driver_list = g_slist_remove(driver_list, driver);
268 }