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