01c1537bc0f16f1704ed3074a89896d9ccb5258d
[platform/core/connectivity/stc-iptables.git] / src / stc-iptables-gdbus.c
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License version 2 as
6  *  published by the Free Software Foundation.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17
18 #include "stc-iptables.h"
19 #include "stc-iptables-gdbus.h"
20 #include "stc-iptables-util.h"
21
22 static gboolean __stc_iptables_gdbus_manager_init(stc_iptables_s *stc_iptables)
23 {
24         __LOG_FUNC_ENTER__;
25         gboolean ret = TRUE;
26         gchar *s = NULL;
27
28         StcObjectSkeleton *object = NULL;
29         StcManager *manager = NULL;
30         s = g_strdup_printf(STC_IPTABLES_DBUS_SERVICE_MANAGER_PATH);
31
32         /* Add interface to default object path */
33         object = stc_object_skeleton_new(s);
34         g_free(s);
35
36         manager = stc_manager_skeleton_new();
37         stc_object_skeleton_set_manager(object, manager);
38         g_object_unref(manager);
39
40         g_signal_connect(manager, "handle-stop",
41                          G_CALLBACK(handle_iptables_stop),
42                          stc_iptables);
43
44         g_dbus_object_manager_server_export(stc_iptables->obj_mgr,
45                                             G_DBUS_OBJECT_SKELETON(object));
46         g_object_unref(object);
47
48         stc_iptables->manager_obj = (gpointer)manager;
49
50         __LOG_FUNC_EXIT__;
51         return ret;
52 }
53
54 static gboolean __stc_iptables_gdbus_chain_init(stc_iptables_s *stc_iptables)
55 {
56         __LOG_FUNC_ENTER__;
57         gboolean ret = TRUE;
58         gchar *s = NULL;
59
60         StcObjectSkeleton *object = NULL;
61         StcChain *chain = NULL;
62         s = g_strdup_printf(STC_IPTABLES_DBUS_SERVICE_CHAIN_PATH);
63
64         /* Add interface to default object path */
65         object = stc_object_skeleton_new(s);
66         g_free(s);
67
68         chain = stc_chain_skeleton_new();
69         stc_object_skeleton_set_chain(object, chain);
70         g_object_unref(chain);
71
72         g_signal_connect(chain, "handle-ipt-add-chain",
73                          G_CALLBACK(handle_iptables_add_chain),
74                          stc_iptables);
75
76         g_signal_connect(chain, "handle-ipt-remove-chain",
77                          G_CALLBACK(handle_iptables_remove_chain),
78                          stc_iptables);
79
80         g_signal_connect(chain, "handle-ipt-flush-chain",
81                          G_CALLBACK(handle_iptables_flush_chain),
82                          stc_iptables);
83
84         g_signal_connect(chain, "handle-ip6t-add-chain",
85                          G_CALLBACK(handle_ip6tables_add_chain),
86                          stc_iptables);
87
88         g_signal_connect(chain, "handle-ip6t-remove-chain",
89                          G_CALLBACK(handle_ip6tables_remove_chain),
90                          stc_iptables);
91
92         g_signal_connect(chain, "handle-ip6t-flush-chain",
93                          G_CALLBACK(handle_ip6tables_flush_chain),
94                          stc_iptables);
95
96         g_dbus_object_manager_server_export(stc_iptables->obj_mgr,
97                                             G_DBUS_OBJECT_SKELETON(object));
98         g_object_unref(object);
99
100         stc_iptables->chain_obj = (gpointer)chain;
101
102         __LOG_FUNC_EXIT__;
103         return ret;
104 }
105
106 static gboolean __stc_iptables_gdbus_rule_init(stc_iptables_s *stc_iptables)
107 {
108         __LOG_FUNC_ENTER__;
109         gboolean ret = TRUE;
110         gchar *s = NULL;
111
112         StcObjectSkeleton *object = NULL;
113         StcRule *rule = NULL;
114         s = g_strdup_printf(STC_IPTABLES_DBUS_SERVICE_RULE_PATH);
115
116         /* Add interface to default object path */
117         object = stc_object_skeleton_new(s);
118         g_free(s);
119
120         rule = stc_rule_skeleton_new();
121         stc_object_skeleton_set_rule(object, rule);
122         g_object_unref(rule);
123
124         g_signal_connect(rule, "handle-ipt-add-rule",
125                          G_CALLBACK(handle_iptables_add_rule),
126                          stc_iptables);
127
128         g_signal_connect(rule, "handle-ipt-insert-rule",
129                          G_CALLBACK(handle_iptables_insert_rule),
130                          stc_iptables);
131
132         g_signal_connect(rule, "handle-ipt-remove-rule",
133                          G_CALLBACK(handle_iptables_remove_rule),
134                          stc_iptables);
135
136         g_signal_connect(rule, "handle-ip6t-add-rule",
137                          G_CALLBACK(handle_ip6tables_add_rule),
138                          stc_iptables);
139
140         g_signal_connect(rule, "handle-ip6t-insert-rule",
141                          G_CALLBACK(handle_ip6tables_add_rule),
142                          stc_iptables);
143
144         g_signal_connect(rule, "handle-ip6t-remove-rule",
145                          G_CALLBACK(handle_ip6tables_remove_rule),
146                          stc_iptables);
147
148         g_dbus_object_manager_server_export(stc_iptables->obj_mgr,
149                                             G_DBUS_OBJECT_SKELETON(object));
150         g_object_unref(object);
151
152         stc_iptables->rule_obj = (gpointer)rule;
153
154         __LOG_FUNC_EXIT__;
155         return ret;
156 }
157
158 static void __stc_iptables_gdbus_on_bus_acquired(GDBusConnection *connection,
159                                                 const gchar *name,
160                                                 gpointer user_data)
161 {
162         __LOG_FUNC_ENTER__;
163         stc_iptables_s* stc_iptables = (stc_iptables_s*)user_data;
164
165         stc_iptables->obj_mgr = g_dbus_object_manager_server_new("/net/stc");
166
167         STC_LOGD("path : %s", name);
168
169         stc_iptables->connection = connection;
170
171         if (__stc_iptables_gdbus_manager_init(stc_iptables) == FALSE) {
172                 STC_LOGE("Can not signal connect to manager"); //LCOV_EXCL_LINE
173                 /* Deinitialize and quit iptables */
174         }
175
176         if (__stc_iptables_gdbus_chain_init(stc_iptables) == FALSE) {
177                 STC_LOGE("Can not signal connect to chain"); //LCOV_EXCL_LINE
178                 /* Deinitialize and quit iptables */
179         }
180
181         if (__stc_iptables_gdbus_rule_init(stc_iptables) == FALSE) {
182                 STC_LOGE("Can not signal connect to rule"); //LCOV_EXCL_LINE
183                 /* Deinitialize and quit iptables */
184         }
185
186         g_dbus_object_manager_server_set_connection(stc_iptables->obj_mgr,
187                                                     stc_iptables->connection);
188
189         __LOG_FUNC_EXIT__;
190 }
191
192 static void __stc_iptables_gdbus_on_name_acquired(GDBusConnection *connection,
193                                                  const gchar *name,
194                                                  gpointer user_data)
195 {
196         STC_LOGD("name : %s", name);
197 }
198
199 //LCOV_EXCL_START
200 static void __stc_iptables_gdbus_on_name_lost(GDBusConnection *connection,
201                                              const gchar *name,
202                                              gpointer user_data)
203 {
204         STC_LOGD("name : %s", name);
205 }
206 //LCOV_EXCL_STOP
207
208 void stc_iptables_gdbus_init(gpointer stc_data)
209 {
210         __LOG_FUNC_ENTER__;
211
212         stc_iptables_s *stc_iptables = (stc_iptables_s *)stc_data;
213
214         stc_iptables->gdbus_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
215                                              STC_IPTABLES_DBUS_SERVICE,
216                                              G_BUS_NAME_OWNER_FLAGS_NONE,
217                                              __stc_iptables_gdbus_on_bus_acquired,
218                                              __stc_iptables_gdbus_on_name_acquired,
219                                              __stc_iptables_gdbus_on_name_lost,
220                                              stc_iptables,
221                                              NULL);
222
223         __LOG_FUNC_EXIT__;
224 }
225
226 void stc_iptables_gdbus_deinit(gpointer stc_data)
227 {
228         __LOG_FUNC_ENTER__;
229
230         stc_iptables_s *stc_iptables = (stc_iptables_s *)stc_data;
231
232         g_bus_unown_name(stc_iptables->gdbus_owner_id);
233
234         stc_iptables->manager_obj = NULL;
235         stc_iptables->rule_obj = NULL;
236         stc_iptables->chain_obj = NULL;
237
238         __LOG_FUNC_EXIT__;
239 }
240
241 void stc_iptables_gdbus_dict_foreach(GVariantIter *iter,
242                                      dbus_dict_cb cb, void *user_data)
243 {
244         __LOG_FUNC_ENTER__;
245
246         gchar *key = NULL;
247         GVariant *value = NULL;
248
249         if (!cb) {
250                 __LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
251                 return; //LCOV_EXCL_LINE
252         }
253
254         while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
255                 /* STC_DEBUG_GDBUS_KEY_VALUE(key, value); */
256                 if (key)
257                         cb(key, value, user_data);
258         }
259
260         __LOG_FUNC_EXIT__;
261 }