2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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.
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.
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
18 #include "stc-iptables.h"
19 #include "stc-iptables-gdbus.h"
20 #include "stc-iptables-util.h"
22 static gboolean __stc_iptables_gdbus_manager_init(stc_iptables_s *stc_iptables)
28 StcObjectSkeleton *object = NULL;
29 StcManager *manager = NULL;
30 s = g_strdup_printf(STC_IPTABLES_DBUS_SERVICE_MANAGER_PATH);
32 /* Add interface to default object path */
33 object = stc_object_skeleton_new(s);
36 manager = stc_manager_skeleton_new();
37 stc_object_skeleton_set_manager(object, manager);
38 g_object_unref(manager);
40 g_signal_connect(manager, "handle-stop",
41 G_CALLBACK(handle_iptables_stop),
44 g_dbus_object_manager_server_export(stc_iptables->obj_mgr,
45 G_DBUS_OBJECT_SKELETON(object));
46 g_object_unref(object);
48 stc_iptables->manager_obj = (gpointer)manager;
54 static gboolean __stc_iptables_gdbus_chain_init(stc_iptables_s *stc_iptables)
60 StcObjectSkeleton *object = NULL;
61 StcChain *chain = NULL;
62 s = g_strdup_printf(STC_IPTABLES_DBUS_SERVICE_CHAIN_PATH);
64 /* Add interface to default object path */
65 object = stc_object_skeleton_new(s);
68 chain = stc_chain_skeleton_new();
69 stc_object_skeleton_set_chain(object, chain);
70 g_object_unref(chain);
72 g_signal_connect(chain, "handle-ipt-add-chain",
73 G_CALLBACK(handle_iptables_add_chain),
76 g_signal_connect(chain, "handle-ipt-remove-chain",
77 G_CALLBACK(handle_iptables_remove_chain),
80 g_signal_connect(chain, "handle-ipt-flush-chain",
81 G_CALLBACK(handle_iptables_flush_chain),
84 g_signal_connect(chain, "handle-ip6t-add-chain",
85 G_CALLBACK(handle_ip6tables_add_chain),
88 g_signal_connect(chain, "handle-ip6t-remove-chain",
89 G_CALLBACK(handle_ip6tables_remove_chain),
92 g_signal_connect(chain, "handle-ip6t-flush-chain",
93 G_CALLBACK(handle_ip6tables_flush_chain),
96 g_dbus_object_manager_server_export(stc_iptables->obj_mgr,
97 G_DBUS_OBJECT_SKELETON(object));
98 g_object_unref(object);
100 stc_iptables->chain_obj = (gpointer)chain;
106 static gboolean __stc_iptables_gdbus_rule_init(stc_iptables_s *stc_iptables)
112 StcObjectSkeleton *object = NULL;
113 StcRule *rule = NULL;
114 s = g_strdup_printf(STC_IPTABLES_DBUS_SERVICE_RULE_PATH);
116 /* Add interface to default object path */
117 object = stc_object_skeleton_new(s);
120 rule = stc_rule_skeleton_new();
121 stc_object_skeleton_set_rule(object, rule);
122 g_object_unref(rule);
124 g_signal_connect(rule, "handle-ipt-add-rule",
125 G_CALLBACK(handle_iptables_add_rule),
128 g_signal_connect(rule, "handle-ipt-insert-rule",
129 G_CALLBACK(handle_iptables_insert_rule),
132 g_signal_connect(rule, "handle-ipt-remove-rule",
133 G_CALLBACK(handle_iptables_remove_rule),
136 g_signal_connect(rule, "handle-ip6t-add-rule",
137 G_CALLBACK(handle_ip6tables_add_rule),
140 g_signal_connect(rule, "handle-ip6t-insert-rule",
141 G_CALLBACK(handle_ip6tables_add_rule),
144 g_signal_connect(rule, "handle-ip6t-remove-rule",
145 G_CALLBACK(handle_ip6tables_remove_rule),
148 g_dbus_object_manager_server_export(stc_iptables->obj_mgr,
149 G_DBUS_OBJECT_SKELETON(object));
150 g_object_unref(object);
152 stc_iptables->rule_obj = (gpointer)rule;
158 static void __stc_iptables_gdbus_on_bus_acquired(GDBusConnection *connection,
163 stc_iptables_s* stc_iptables = (stc_iptables_s*)user_data;
165 stc_iptables->obj_mgr = g_dbus_object_manager_server_new("/net/stc");
167 STC_LOGD("path : %s", name);
169 stc_iptables->connection = connection;
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 */
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 */
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 */
186 g_dbus_object_manager_server_set_connection(stc_iptables->obj_mgr,
187 stc_iptables->connection);
192 static void __stc_iptables_gdbus_on_name_acquired(GDBusConnection *connection,
196 STC_LOGD("name : %s", name);
200 static void __stc_iptables_gdbus_on_name_lost(GDBusConnection *connection,
204 STC_LOGD("name : %s", name);
208 void stc_iptables_gdbus_init(gpointer stc_data)
212 stc_iptables_s *stc_iptables = (stc_iptables_s *)stc_data;
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,
226 void stc_iptables_gdbus_deinit(gpointer stc_data)
230 stc_iptables_s *stc_iptables = (stc_iptables_s *)stc_data;
232 g_bus_unown_name(stc_iptables->gdbus_owner_id);
234 stc_iptables->manager_obj = NULL;
235 stc_iptables->rule_obj = NULL;
236 stc_iptables->chain_obj = NULL;
241 void stc_iptables_gdbus_dict_foreach(GVariantIter *iter,
242 dbus_dict_cb cb, void *user_data)
247 GVariant *value = NULL;
250 __LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
251 return; //LCOV_EXCL_LINE
254 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
255 /* STC_DEBUG_GDBUS_KEY_VALUE(key, value); */
257 cb(key, value, user_data);