From: hyunuktak Date: Wed, 5 Dec 2018 04:49:46 +0000 (+0900) Subject: Add a function to get all pcap loop X-Git-Tag: accepted/tizen/unified/20181212.062514~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F94%2F194494%2F2;p=platform%2Fcore%2Fconnectivity%2Fstc-manager.git Add a function to get all pcap loop Change-Id: I92fa7de5a45e758a96f3ae98a31683d81aabe66d Signed-off-by: hyunuktak --- diff --git a/include/stc-manager-plugin-pcap.h b/include/stc-manager-plugin-pcap.h index 728004f..620f8d7 100755 --- a/include/stc-manager-plugin-pcap.h +++ b/include/stc-manager-plugin-pcap.h @@ -27,9 +27,10 @@ int stc_plugin_pcap_deinit(void); int stc_plugin_pcap_lookup_dev(void); int stc_plugin_pcap_lookup_net(void); int stc_plugin_pcap_find_alldevs(void); -int stc_plugin_pcap_register_loop_pcap(const char *ifname, - int group); -int stc_plugin_pcap_unregister_loop_pcap(const char *ifname, - int group); +int stc_plugin_pcap_register_loop(const char *ifname, + int nflog_group); +int stc_plugin_pcap_unregister_loop(const char *ifname, + int nflog_group); +int stc_plugin_pcap_get_all_loop(GVariantBuilder *builder); #endif /* __STC_MANAGER_PLUGIN_PROCFS_H__ */ diff --git a/include/stc-pcap.h b/include/stc-pcap.h index d99d450..49c310a 100755 --- a/include/stc-pcap.h +++ b/include/stc-pcap.h @@ -47,4 +47,8 @@ gboolean handle_pcap_stop(StcPcap *object, GVariant *parameters, void *user_data); +gboolean handle_pcap_get_all(StcPcap *object, + GDBusMethodInvocation *invocation, + void *user_data); + #endif /* __STC_PCAP_H__ */ diff --git a/interfaces/stcmanager-iface-pcap.xml b/interfaces/stcmanager-iface-pcap.xml index 4320ab6..01c10b6 100644 --- a/interfaces/stcmanager-iface-pcap.xml +++ b/interfaces/stcmanager-iface-pcap.xml @@ -8,5 +8,8 @@ + + + diff --git a/plugin/pcap/include/stc-plugin-pcap.h b/plugin/pcap/include/stc-plugin-pcap.h index a2b8c34..f545445 100755 --- a/plugin/pcap/include/stc-plugin-pcap.h +++ b/plugin/pcap/include/stc-plugin-pcap.h @@ -123,8 +123,8 @@ #define DNS_QCLASS_ANY 255 typedef struct { - char *name; - int group; + char *ifname; + int nflog_group; GThread *thread; pcap_t *handle; } stc_pcap_data_s; @@ -218,8 +218,9 @@ typedef struct { int (*lookup_dev) (void); int (*lookup_net) (void); int (*find_alldevs) (void); - int (*register_loop_pcap) (const char *ifname, int group); - int (*unregister_loop_pcap) (const char *ifname, int group); + int (*register_loop) (const char *ifname, int group); + int (*unregister_loop) (const char *ifname, int group); + int (*get_all_loop) (GVariantBuilder *builder); } stc_plugin_pcap_s; #endif /* __STC_PLUGIN_PCAP_H__ */ diff --git a/plugin/pcap/stc-plugin-pcap.c b/plugin/pcap/stc-plugin-pcap.c index fd03c4a..15f9ed8 100755 --- a/plugin/pcap/stc-plugin-pcap.c +++ b/plugin/pcap/stc-plugin-pcap.c @@ -16,6 +16,9 @@ #include "stc-plugin-pcap.h" +#define PCAP_IFNAME "ifname" +#define PCAP_NFLOG_GROUP "nflog_group" + //LCOV_EXCL_START static GHashTable *g_pcap_tables = NULL; static bool g_pcap_start_fm = false; @@ -27,7 +30,7 @@ static void __pcap_data_free(gpointer value) g_thread_unref(data->thread); - FREE(data->name); + FREE(data->ifname); FREE(data); } @@ -1072,7 +1075,7 @@ static gboolean __pcap_thread_source_func(gpointer data) return false; snprintf(buf, sizeof(buf), "%s_%d", - pcap_data->name, pcap_data->group); + pcap_data->ifname, pcap_data->nflog_group); lookup = g_hash_table_lookup(g_pcap_tables, buf); if (!lookup) { @@ -1105,7 +1108,7 @@ static gpointer __pcap_thread_func(gpointer data) } for (dev = alldevs; dev; dev = dev->next) { - if (g_strcmp0(dev->name, pcap_data->name) == 0) { + if (g_strcmp0(dev->name, pcap_data->ifname) == 0) { name = g_strdup(dev->name); break; } @@ -1114,7 +1117,7 @@ static gpointer __pcap_thread_func(gpointer data) pcap_freealldevs(alldevs); if (name == NULL) { - STC_LOGE("Failed to find dev [%s]", pcap_data->name); + STC_LOGE("Failed to find dev [%s]", pcap_data->ifname); goto thread_exit; } @@ -1126,9 +1129,12 @@ static gpointer __pcap_thread_func(gpointer data) goto thread_exit; } + STC_LOGD("Pcap loop start [%s]", name); pcap_loop(pcap_data->handle, 0, __pcap_handler, NULL); + STC_LOGD("Pcap loop end [%s]", name); pcap_close(pcap_data->handle); + STC_LOGD("Pcap closed [%s]", name); thread_exit: FREE(name); @@ -1147,6 +1153,24 @@ thread_exit: return NULL; } +static void __pcap_make_params(gpointer key, gpointer value, + gpointer user_data) +{ + stc_pcap_data_s *data = (stc_pcap_data_s *)value; + GVariantBuilder *builder = (GVariantBuilder *)user_data; + GVariantBuilder sub_builder; + + g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}")); + + g_variant_builder_add(&sub_builder, "{sv}", PCAP_IFNAME, + g_variant_new_string(data->ifname)); + + g_variant_builder_add(&sub_builder, "{sv}", PCAP_NFLOG_GROUP, + g_variant_new_uint32(data->nflog_group)); + + g_variant_builder_add_value(builder, g_variant_builder_end(&sub_builder)); +} + int stc_plugin_pcap_initialize(void) { __STC_LOG_FUNC_ENTER__; @@ -1259,8 +1283,8 @@ int stc_plugin_pcap_find_alldevs(void) return STC_ERROR_NONE; } -int stc_plugin_pcap_register_loop_pcap(const char *ifname, - int group) +int stc_plugin_pcap_register_loop(const char *ifname, + int nflog_group) { stc_pcap_data_s *data; stc_pcap_data_s *lookup; @@ -1274,7 +1298,7 @@ int stc_plugin_pcap_register_loop_pcap(const char *ifname, STC_ERROR_FAIL, "Invalid parameter [ifname]"); - snprintf(buf, sizeof(buf), "%s_%d", ifname, group); + snprintf(buf, sizeof(buf), "%s_%d", ifname, nflog_group); lookup = g_hash_table_lookup(g_pcap_tables, buf); if (lookup) { @@ -1288,8 +1312,8 @@ int stc_plugin_pcap_register_loop_pcap(const char *ifname, return STC_ERROR_OUT_OF_MEMORY; } - data->name = g_strdup(ifname); - data->group = group; + data->ifname = g_strdup(ifname); + data->nflog_group = nflog_group; data->thread = g_thread_new(buf, __pcap_thread_func, data); g_hash_table_insert(g_pcap_tables, g_strdup(buf), data); @@ -1298,8 +1322,8 @@ int stc_plugin_pcap_register_loop_pcap(const char *ifname, return STC_ERROR_NONE; } -int stc_plugin_pcap_unregister_loop_pcap(const char *ifname, - int group) +int stc_plugin_pcap_unregister_loop(const char *ifname, + int nflog_group) { stc_pcap_data_s *lookup; char buf[MAX_IFACE_LENGTH]; @@ -1312,7 +1336,7 @@ int stc_plugin_pcap_unregister_loop_pcap(const char *ifname, STC_ERROR_FAIL, "Invalid parameter [ifname]"); - snprintf(buf, sizeof(buf), "%s_%d", ifname, group); + snprintf(buf, sizeof(buf), "%s_%d", ifname, nflog_group); lookup = g_hash_table_lookup(g_pcap_tables, buf); if (!lookup) { @@ -1322,6 +1346,18 @@ int stc_plugin_pcap_unregister_loop_pcap(const char *ifname, } pcap_breakloop(lookup->handle); + STC_LOGD("Successfully removed pcap loop [%s]", buf); + + return STC_ERROR_NONE; +} + +int stc_plugin_pcap_get_all_loop(GVariantBuilder *builder) +{ + ret_value_msg_if(g_pcap_tables == NULL, + STC_ERROR_FAIL, + "pcap tables is not initialized!"); + + g_hash_table_foreach(g_pcap_tables, __pcap_make_params, builder); return STC_ERROR_NONE; } @@ -1337,9 +1373,11 @@ API stc_plugin_pcap_s stc_plugin_pcap = { stc_plugin_pcap_lookup_net, .find_alldevs = stc_plugin_pcap_find_alldevs, - .register_loop_pcap = - stc_plugin_pcap_register_loop_pcap, - .unregister_loop_pcap = - stc_plugin_pcap_unregister_loop_pcap + .register_loop = + stc_plugin_pcap_register_loop, + .unregister_loop = + stc_plugin_pcap_unregister_loop, + .get_all_loop = + stc_plugin_pcap_get_all_loop }; //LCOV_EXCL_STOP diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 0268eef..14cf127 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -262,6 +262,10 @@ static gboolean __stc_manager_gdbus_pcap_init(stc_s *stc) G_CALLBACK(handle_pcap_stop), stc); + g_signal_connect(pcap, "handle-get-all", + G_CALLBACK(handle_pcap_get_all), + stc); + /* Export the object (@manager takes its own reference to @object) */ g_dbus_object_manager_server_export(stc->obj_mgr, G_DBUS_OBJECT_SKELETON(object)); diff --git a/src/stc-manager-plugin-pcap.c b/src/stc-manager-plugin-pcap.c index 395031c..dc96424 100755 --- a/src/stc-manager-plugin-pcap.c +++ b/src/stc-manager-plugin-pcap.c @@ -118,8 +118,8 @@ int stc_plugin_pcap_find_alldevs(void) return stc_plugin->find_alldevs(); } -int stc_plugin_pcap_register_loop_pcap(const char *ifname, - int group) +int stc_plugin_pcap_register_loop(const char *ifname, + int nflog_group) { if (!stc_plugin_enabled) { if (STC_DEBUG_LOG) @@ -133,11 +133,11 @@ int stc_plugin_pcap_register_loop_pcap(const char *ifname, return STC_ERROR_UNINITIALIZED; } - return stc_plugin->register_loop_pcap(ifname, group); + return stc_plugin->register_loop(ifname, nflog_group); } -int stc_plugin_pcap_unregister_loop_pcap(const char *ifname, - int group) +int stc_plugin_pcap_unregister_loop(const char *ifname, + int nflog_group) { if (!stc_plugin_enabled) { if (STC_DEBUG_LOG) @@ -151,6 +151,23 @@ int stc_plugin_pcap_unregister_loop_pcap(const char *ifname, return STC_ERROR_UNINITIALIZED; } - return stc_plugin->unregister_loop_pcap(ifname, group); + return stc_plugin->unregister_loop(ifname, nflog_group); +} + +int stc_plugin_pcap_get_all_loop(GVariantBuilder *builder) +{ + if (!stc_plugin_enabled) { + if (STC_DEBUG_LOG) + STC_LOGE("Plugin wasn't enabled"); + return STC_ERROR_UNINITIALIZED; + } + + if (!stc_plugin) { + if (STC_DEBUG_LOG) + STC_LOGE("Plugin wasn't loaded"); + return STC_ERROR_UNINITIALIZED; + } + + return stc_plugin->get_all_loop(builder); } //LCOV_EXCL_STOP diff --git a/src/stc-pcap.c b/src/stc-pcap.c index 368971e..c24e34b 100755 --- a/src/stc-pcap.c +++ b/src/stc-pcap.c @@ -108,7 +108,7 @@ gboolean handle_pcap_start(StcPcap *object, return TRUE; } - ret = stc_plugin_pcap_register_loop_pcap(pcap.ifname, pcap.nflog_group); + ret = stc_plugin_pcap_register_loop(pcap.ifname, pcap.nflog_group); if (ret != STC_ERROR_NONE) { STC_PCAP_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE @@ -147,7 +147,7 @@ gboolean handle_pcap_stop(StcPcap *object, return TRUE; } - ret = stc_plugin_pcap_unregister_loop_pcap(pcap.ifname, pcap.nflog_group); + ret = stc_plugin_pcap_unregister_loop(pcap.ifname, pcap.nflog_group); if (ret != STC_ERROR_NONE) { STC_PCAP_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE @@ -158,3 +158,24 @@ gboolean handle_pcap_stop(StcPcap *object, __STC_LOG_FUNC_EXIT__; return TRUE; } + +gboolean handle_pcap_get_all(StcPcap *object, + GDBusMethodInvocation *invocation, + void *user_data) +{ + __STC_LOG_FUNC_ENTER__; + GVariantBuilder *builder = NULL; + GVariant *return_parameters = NULL; + + builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); + + stc_plugin_pcap_get_all_loop(builder); + + return_parameters = g_variant_new("(aa{sv})", builder); + g_variant_builder_unref(builder); + + DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters); + STC_DBUS_REPLY(invocation, return_parameters); + __STC_LOG_FUNC_EXIT__; + return TRUE; +}