0f14ebd081940dfac18c1e7d07f2bad745872c02
[framework/connectivity/connman.git] / src / iface-storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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 <errno.h>
27 #include <arpa/inet.h>
28
29 #include <glib.h>
30
31 #include "connman.h"
32
33 #define GROUP_CONFIG  "Config"
34
35 int __connman_iface_load(struct connman_iface *iface)
36 {
37         GKeyFile *keyfile;
38         gchar *pathname, *str;
39
40         DBG("iface %p", iface);
41
42         if (iface->identifier == NULL)
43                 return -EIO;
44
45         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
46                                                         iface->identifier);
47         if (pathname == NULL)
48                 return -ENOMEM;
49
50         keyfile = g_key_file_new();
51
52         if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
53                 goto done;
54
55         if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
56                 goto done;
57
58         str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Policy", NULL);
59         if (str != NULL) {
60                 iface->policy = __connman_iface_string2policy(str);
61                 g_free(str);
62         }
63
64 #if 0
65         str = g_key_file_get_string(keyfile, iface->identifier,
66                                                         "Network.ESSID", NULL);
67         if (str != NULL) {
68                 g_free(iface->network.essid);
69                 iface->network.essid = str;
70                 if (iface->driver->set_network)
71                         iface->driver->set_network(iface, str);
72         }
73
74         str = g_key_file_get_string(keyfile, iface->identifier,
75                                                         "IPv4.Method", NULL);
76         if (str != NULL) {
77                 iface->ipv4.method = __connman_ipv4_string2method(str);
78                 g_free(str);
79         }
80
81         str = g_key_file_get_string(keyfile, iface->identifier,
82                                                         "IPv4.Address", NULL);
83         if (str != NULL) {
84                 iface->ipv4.address.s_addr = inet_addr(str);
85                 g_free(str);
86         }
87
88         str = g_key_file_get_string(keyfile, iface->identifier,
89                                                         "IPv4.Netmask", NULL);
90         if (str != NULL) {
91                 iface->ipv4.netmask.s_addr = inet_addr(str);
92                 g_free(str);
93         }
94
95         str = g_key_file_get_string(keyfile, iface->identifier,
96                                                         "IPv4.Gateway", NULL);
97         if (str != NULL) {
98                 iface->ipv4.gateway.s_addr = inet_addr(str);
99                 g_free(str);
100         }
101 #endif
102
103 done:
104         g_key_file_free(keyfile);
105
106         g_free(pathname);
107
108         return 0;
109 }
110
111 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
112 {
113         const char *str;
114
115         DBG("iface %p", iface);
116
117         str = __connman_iface_policy2string(iface->policy);
118         g_key_file_set_string(keyfile, GROUP_CONFIG, "Policy", str);
119
120 #if 0
121         if (iface->network.essid != NULL) {
122                 g_key_file_set_string(keyfile, iface->identifier,
123                                         "Network.ESSID", iface->network.essid);
124         } else
125                 g_key_file_remove_key(keyfile, iface->identifier,
126                                                         "Network.ESSID", NULL);
127
128         if (iface->ipv4.method != CONNMAN_IPV4_METHOD_UNKNOWN) {
129                 str = __connman_ipv4_method2string(iface->ipv4.method);
130                 g_key_file_set_string(keyfile, iface->identifier,
131                                                         "IPv4.Method", str);
132         } else
133                 g_key_file_remove_key(keyfile, iface->identifier,
134                                                         "IPv4.Method", NULL);
135
136         if (iface->ipv4.address.s_addr != INADDR_ANY) {
137                 str = inet_ntoa(iface->ipv4.address);
138                 g_key_file_set_string(keyfile, iface->identifier,
139                                                         "IPv4.Address", str);
140         } else
141                 g_key_file_remove_key(keyfile, iface->identifier,
142                                                         "IPv4.Address", NULL);
143
144         if (iface->ipv4.netmask.s_addr != INADDR_ANY) {
145                 str = inet_ntoa(iface->ipv4.netmask);
146                 g_key_file_set_string(keyfile, iface->identifier,
147                                                         "IPv4.Netmask", str);
148         } else
149                 g_key_file_remove_key(keyfile, iface->identifier,
150                                                         "IPv4.Netmask", NULL);
151
152         if (iface->ipv4.gateway.s_addr != INADDR_ANY) {
153                 str = inet_ntoa(iface->ipv4.gateway);
154                 g_key_file_set_string(keyfile, iface->identifier,
155                                                         "IPv4.Gateway", str);
156         } else
157                 g_key_file_remove_key(keyfile, iface->identifier,
158                                                         "IPv4.Gateway", NULL);
159 #endif
160 }
161
162 int __connman_iface_store(struct connman_iface *iface)
163 {
164         GKeyFile *keyfile;
165         gchar *pathname, *data = NULL;
166         gsize length;
167
168         DBG("iface %p", iface);
169
170         if (iface->identifier == NULL)
171                 return -EIO;
172
173         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
174                                                         iface->identifier);
175         if (pathname == NULL)
176                 return -ENOMEM;
177
178         keyfile = g_key_file_new();
179
180         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
181                 goto update;
182
183         if (length > 0) {
184                 if (g_key_file_load_from_data(keyfile, data, length,
185                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
186                         goto done;
187         }
188
189         g_free(data);
190
191 update:
192         do_update(keyfile, iface);
193
194         data = g_key_file_to_data(keyfile, &length, NULL);
195
196         g_file_set_contents(pathname, data, length, NULL);
197
198 done:
199         g_free(data);
200
201         g_key_file_free(keyfile);
202
203         g_free(pathname);
204
205         return 0;
206 }