2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "stc-manager-plugin-pcap.h"
21 #define PCAP_DBUS_ERROR_NAME "net.stc.pcap.Error.Failed"
23 #define STC_PCAP_DBUS_REPLY_ERROR(invocation, err_num) \
24 g_dbus_method_invocation_return_dbus_error((invocation), \
25 PCAP_DBUS_ERROR_NAME, \
26 stc_err_strs[-(err_num)])
28 static const gchar *stc_err_strs[] = {
41 gboolean __validate_pcap(stc_pcap_s *pcap)
43 __STC_LOG_FUNC_ENTER__;
46 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
47 return FALSE; //LCOV_EXCL_LINE
50 if (pcap->ifname == NULL ||
51 pcap->ifname[0] == '\0') {
52 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
53 return FALSE; //LCOV_EXCL_LINE
56 __STC_LOG_FUNC_EXIT__;
60 static void __stc_extract_pcap(const char *key, GVariant *value,
63 stc_pcap_s *pcap = (stc_pcap_s *) user_data;
65 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
66 return; //LCOV_EXCL_LINE
69 if (!g_strcmp0(key, STC_PCAP_IFNAME)) {
71 const gchar *str = g_variant_get_string(value, &str_length);
72 pcap->ifname = g_strdup(str);
73 STC_LOGD("ifname: [%s]", pcap->ifname);
75 } else if (!g_strcmp0(key, STC_PCAP_NFLOG_GROUP)) {
76 pcap->nflog_group = g_variant_get_uint32(value);
77 STC_LOGD("nflog group: [%d]", pcap->nflog_group);
80 STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE
84 gboolean handle_pcap_start(StcPcap *object,
85 GDBusMethodInvocation *invocation,
89 __STC_LOG_FUNC_ENTER__;
90 GVariantIter *iter = NULL;
92 int ret = STC_ERROR_NONE;
94 stc_set_keep_alive(TRUE);
96 memset(&pcap, 0, sizeof(stc_pcap_s));
98 g_variant_get(parameters, "a{sv}", &iter);
100 stc_manager_gdbus_dict_foreach(iter,
103 g_variant_iter_free(iter);
106 if (__validate_pcap(&pcap) == FALSE) {
107 STC_PCAP_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
108 STC_ERROR_INVALID_PARAMETER);
109 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
113 ret = stc_plugin_pcap_register_loop(pcap.ifname, pcap.nflog_group);
114 if (ret != STC_ERROR_NONE) {
115 STC_PCAP_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
116 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
120 STC_DBUS_REPLY_ERROR_NONE(invocation);
121 __STC_LOG_FUNC_EXIT__;
125 gboolean handle_pcap_stop(StcPcap *object,
126 GDBusMethodInvocation *invocation,
127 GVariant *parameters,
130 __STC_LOG_FUNC_ENTER__;
131 GVariantIter *iter = NULL;
133 int ret = STC_ERROR_NONE;
135 stc_set_keep_alive(TRUE);
137 memset(&pcap, 0, sizeof(stc_pcap_s));
139 g_variant_get(parameters, "a{sv}", &iter);
141 stc_manager_gdbus_dict_foreach(iter,
144 g_variant_iter_free(iter);
147 if (__validate_pcap(&pcap) == FALSE) {
148 STC_PCAP_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
149 STC_ERROR_INVALID_PARAMETER);
150 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
154 ret = stc_plugin_pcap_unregister_loop(pcap.ifname, pcap.nflog_group);
155 if (ret != STC_ERROR_NONE) {
156 STC_PCAP_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
157 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
161 STC_DBUS_REPLY_ERROR_NONE(invocation);
162 __STC_LOG_FUNC_EXIT__;
166 gboolean handle_pcap_get_all(StcPcap *object,
167 GDBusMethodInvocation *invocation,
170 __STC_LOG_FUNC_ENTER__;
171 GVariantBuilder *builder = NULL;
172 GVariant *return_parameters = NULL;
174 stc_set_keep_alive(TRUE);
176 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
178 stc_plugin_pcap_get_all_loop(builder);
180 return_parameters = g_variant_new("(aa{sv})", builder);
181 g_variant_builder_unref(builder);
183 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
184 STC_DBUS_REPLY(invocation, return_parameters);
185 __STC_LOG_FUNC_EXIT__;
189 gboolean handle_pcap_find_all_devs(StcPcap *object,
190 GDBusMethodInvocation *invocation,
193 __STC_LOG_FUNC_ENTER__;
194 GVariantBuilder *builder = NULL;
195 GVariant *return_parameters = NULL;
197 stc_set_keep_alive(TRUE);
199 builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
201 stc_plugin_pcap_find_all_devs(builder);
203 return_parameters = g_variant_new("(aa{sv})", builder);
204 g_variant_builder_unref(builder);
206 DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
207 STC_DBUS_REPLY(invocation, return_parameters);
208 __STC_LOG_FUNC_EXIT__;