b7c84c38c59234e9438e2de03703aed18e8105f1
[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 int __connman_iface_load(struct connman_iface *iface)
34 {
35         GKeyFile *keyfile;
36         gchar *pathname, *str;
37
38         DBG("iface %p", iface);
39
40         if (iface->identifier == NULL)
41                 return -EIO;
42
43         pathname = g_strdup_printf("%s/interfaces.conf", STORAGEDIR);
44         if (pathname == NULL)
45                 return -ENOMEM;
46
47         keyfile = g_key_file_new();
48
49         if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
50                 goto done;
51
52         if (g_key_file_has_group(keyfile, iface->identifier) == FALSE)
53                 goto done;
54
55         str = g_key_file_get_string(keyfile, iface->identifier,
56                                                         "Policy", NULL);
57         if (str != NULL) {
58                 iface->policy = __connman_iface_string2policy(str);
59                 g_free(str);
60         }
61
62         str = g_key_file_get_string(keyfile, iface->identifier,
63                                                         "IPv4.Method", NULL);
64         if (str != NULL) {
65                 iface->ipv4.method = __connman_ipv4_string2method(str);
66                 g_free(str);
67         }
68
69         str = g_key_file_get_string(keyfile, iface->identifier,
70                                                         "IPv4.Address", NULL);
71         if (str != NULL) {
72                 iface->ipv4.address.s_addr = inet_addr(str);
73                 g_free(str);
74         }
75
76         str = g_key_file_get_string(keyfile, iface->identifier,
77                                                         "IPv4.Netmask", NULL);
78         if (str != NULL) {
79                 iface->ipv4.netmask.s_addr = inet_addr(str);
80                 g_free(str);
81         }
82
83         str = g_key_file_get_string(keyfile, iface->identifier,
84                                                         "IPv4.Gateway", NULL);
85         if (str != NULL) {
86                 iface->ipv4.gateway.s_addr = inet_addr(str);
87                 g_free(str);
88         }
89
90 done:
91         g_key_file_free(keyfile);
92
93         g_free(pathname);
94
95         return 0;
96 }
97
98 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
99 {
100         const char *str;
101         gchar *comment;
102
103         DBG("iface %p", iface);
104
105         comment = g_key_file_get_comment(keyfile,
106                                         iface->identifier, NULL, NULL);
107         if (comment == NULL || *comment == '\0') {
108                 if (iface->device.product != NULL)
109                         g_key_file_set_comment(keyfile, iface->identifier,
110                                         NULL, iface->device.product, NULL);
111         }
112         g_free(comment);
113
114         str = __connman_iface_policy2string(iface->policy);
115         g_key_file_set_string(keyfile, iface->identifier, "Policy", str);
116
117         if (iface->ipv4.method != CONNMAN_IPV4_METHOD_UNKNOWN) {
118                 str = __connman_ipv4_method2string(iface->ipv4.method);
119                 g_key_file_set_string(keyfile, iface->identifier,
120                                                         "IPv4.Method", str);
121         } else
122                 g_key_file_remove_key(keyfile, iface->identifier,
123                                                         "IPv4.Method", NULL);
124
125         if (iface->ipv4.address.s_addr != INADDR_ANY) {
126                 str = inet_ntoa(iface->ipv4.address);
127                 g_key_file_set_string(keyfile, iface->identifier,
128                                                         "IPv4.Address", str);
129         } else
130                 g_key_file_remove_key(keyfile, iface->identifier,
131                                                         "IPv4.Address", NULL);
132
133         if (iface->ipv4.netmask.s_addr != INADDR_ANY) {
134                 str = inet_ntoa(iface->ipv4.netmask);
135                 g_key_file_set_string(keyfile, iface->identifier,
136                                                         "IPv4.Netmask", str);
137         } else
138                 g_key_file_remove_key(keyfile, iface->identifier,
139                                                         "IPv4.Netmask", NULL);
140
141         if (iface->ipv4.gateway.s_addr != INADDR_ANY) {
142                 str = inet_ntoa(iface->ipv4.gateway);
143                 g_key_file_set_string(keyfile, iface->identifier,
144                                                         "IPv4.Gateway", str);
145         } else
146                 g_key_file_remove_key(keyfile, iface->identifier,
147                                                         "IPv4.Gateway", NULL);
148 }
149
150 int __connman_iface_store(struct connman_iface *iface)
151 {
152         GKeyFile *keyfile;
153         gchar *pathname, *data = NULL;
154         gsize length;
155
156         DBG("iface %p", iface);
157
158         if (iface->identifier == NULL)
159                 return -EIO;
160
161         pathname = g_strdup_printf("%s/interfaces.conf", STORAGEDIR);
162         if (pathname == NULL)
163                 return -ENOMEM;
164
165         keyfile = g_key_file_new();
166
167         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
168                 goto done;
169
170         if (length > 0) {
171                 if (g_key_file_load_from_data(keyfile, data, length,
172                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
173                         goto done;
174         }
175
176         g_free(data);
177
178         do_update(keyfile, iface);
179
180         data = g_key_file_to_data(keyfile, &length, NULL);
181
182         g_file_set_contents(pathname, data, length, NULL);
183
184 done:
185         g_free(data);
186
187         g_key_file_free(keyfile);
188
189         g_free(pathname);
190
191         return 0;
192 }