Change variables inside the network structure
[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         str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Network", NULL);
65         if (str != NULL) {
66                 g_free(iface->network.identifier);
67                 iface->network.identifier = str;
68
69                 str = g_key_file_get_string(keyfile,
70                                 iface->network.identifier, "PSK", NULL);
71                 if (str != NULL) {
72                         g_free(iface->network.passphrase);
73                         iface->network.passphrase = str;
74                 }
75         }
76
77 done:
78         g_key_file_free(keyfile);
79
80         g_free(pathname);
81
82         return 0;
83 }
84
85 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
86 {
87         const char *str;
88
89         DBG("iface %p", iface);
90
91         str = __connman_iface_policy2string(iface->policy);
92         g_key_file_set_string(keyfile, GROUP_CONFIG, "Policy", str);
93
94         if (iface->network.identifier != NULL) {
95                 g_key_file_set_string(keyfile, GROUP_CONFIG,
96                                 "Network", iface->network.identifier);
97         } else
98                 g_key_file_remove_key(keyfile, GROUP_CONFIG, "Network", NULL);
99
100         if (iface->network.identifier != NULL)
101                 g_key_file_set_string(keyfile, iface->network.identifier,
102                                         "PSK", iface->network.passphrase);
103 }
104
105 int __connman_iface_store(struct connman_iface *iface)
106 {
107         GKeyFile *keyfile;
108         gchar *pathname, *data = NULL;
109         gsize length;
110
111         DBG("iface %p", iface);
112
113         if (iface->identifier == NULL)
114                 return -EIO;
115
116         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
117                                                         iface->identifier);
118         if (pathname == NULL)
119                 return -ENOMEM;
120
121         keyfile = g_key_file_new();
122
123         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
124                 goto update;
125
126         if (length > 0) {
127                 if (g_key_file_load_from_data(keyfile, data, length,
128                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
129                         goto done;
130         }
131
132         g_free(data);
133
134 update:
135         do_update(keyfile, iface);
136
137         data = g_key_file_to_data(keyfile, &length, NULL);
138
139         g_file_set_contents(pathname, data, length, NULL);
140
141 done:
142         g_free(data);
143
144         g_key_file_free(keyfile);
145
146         g_free(pathname);
147
148         return 0;
149 }