From f275de2b3f112d9cdbdbed81d6193916982b22b9 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 6 Dec 2018 14:19:23 +0900 Subject: [PATCH] Add a function to find all devices Change-Id: I7c1a8fa26b18f36955fc89ace5bb6b1d3f5b470c Signed-off-by: hyunuktak --- include/stc-manager-plugin-pcap.h | 2 +- include/stc-pcap.h | 4 ++++ interfaces/stcmanager-iface-pcap.xml | 3 +++ plugin/pcap/include/stc-plugin-pcap.h | 2 +- plugin/pcap/stc-plugin-pcap.c | 28 ++++++++++++++++++++++++---- src/stc-manager-gdbus.c | 4 ++++ src/stc-manager-plugin-pcap.c | 4 ++-- src/stc-pcap.c | 21 +++++++++++++++++++++ 8 files changed, 60 insertions(+), 8 deletions(-) diff --git a/include/stc-manager-plugin-pcap.h b/include/stc-manager-plugin-pcap.h index 620f8d7..7c8fe7e 100755 --- a/include/stc-manager-plugin-pcap.h +++ b/include/stc-manager-plugin-pcap.h @@ -26,7 +26,7 @@ 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_find_all_devs(GVariantBuilder *builder); int stc_plugin_pcap_register_loop(const char *ifname, int nflog_group); int stc_plugin_pcap_unregister_loop(const char *ifname, diff --git a/include/stc-pcap.h b/include/stc-pcap.h index 49c310a..1bd3d7e 100755 --- a/include/stc-pcap.h +++ b/include/stc-pcap.h @@ -51,4 +51,8 @@ gboolean handle_pcap_get_all(StcPcap *object, GDBusMethodInvocation *invocation, void *user_data); +gboolean handle_pcap_find_all_devs(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 01c10b6..035e57f 100644 --- a/interfaces/stcmanager-iface-pcap.xml +++ b/interfaces/stcmanager-iface-pcap.xml @@ -11,5 +11,8 @@ + + + diff --git a/plugin/pcap/include/stc-plugin-pcap.h b/plugin/pcap/include/stc-plugin-pcap.h index f545445..d1747b2 100755 --- a/plugin/pcap/include/stc-plugin-pcap.h +++ b/plugin/pcap/include/stc-plugin-pcap.h @@ -217,7 +217,7 @@ typedef struct { int (*deinitialize_plugin) (void); int (*lookup_dev) (void); int (*lookup_net) (void); - int (*find_alldevs) (void); + int (*find_all_devs) (GVariantBuilder *builder); int (*register_loop) (const char *ifname, int group); int (*unregister_loop) (const char *ifname, int group); int (*get_all_loop) (GVariantBuilder *builder); diff --git a/plugin/pcap/stc-plugin-pcap.c b/plugin/pcap/stc-plugin-pcap.c index 15f9ed8..8785368 100755 --- a/plugin/pcap/stc-plugin-pcap.c +++ b/plugin/pcap/stc-plugin-pcap.c @@ -18,6 +18,10 @@ #define PCAP_IFNAME "ifname" #define PCAP_NFLOG_GROUP "nflog_group" +#define PCAP_DEV_NAME "dev" +#define PCAP_DEV_DECS "decs" +#define PCAP_DEV_NET "net" +#define PCAP_DEV_MASK "mask" //LCOV_EXCL_START static GHashTable *g_pcap_tables = NULL; @@ -1244,7 +1248,7 @@ int stc_plugin_pcap_lookup_net(void) return STC_ERROR_NONE; } -int stc_plugin_pcap_find_alldevs(void) +int stc_plugin_pcap_find_all_devs(GVariantBuilder *builder) { char net[BUFF_SIZE_IP]; char mask[BUFF_SIZE_IP]; @@ -1261,21 +1265,37 @@ int stc_plugin_pcap_find_alldevs(void) } for (dev = alldevs; dev; dev = dev->next) { + GVariantBuilder sub_builder; + + g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}")); + STC_LOGD("Dev [%s]", dev->name); + g_variant_builder_add(&sub_builder, "{sv}", PCAP_DEV_NAME, + g_variant_new_string(dev->name)); - if (dev->description) + if (dev->description) { STC_LOGD("Decs [%s]", dev->description); + g_variant_builder_add(&sub_builder, "{sv}", PCAP_DEV_DECS, + g_variant_new_string(dev->description)); + } if (pcap_lookupnet(dev->name, &netp, &maskp, errbuf) == -1) { STC_LOGE("Failed to look up net [%s]", errbuf); + g_variant_builder_add_value(builder, g_variant_builder_end(&sub_builder)); continue; } inet_ntop(AF_INET, &netp, net, BUFF_SIZE_IP); STC_LOGD("Net [%s]", net); + g_variant_builder_add(&sub_builder, "{sv}", PCAP_DEV_NET, + g_variant_new_string(net)); inet_ntop(AF_INET, &maskp, mask, BUFF_SIZE_IP); STC_LOGD("Mask [%s]", mask); + g_variant_builder_add(&sub_builder, "{sv}", PCAP_DEV_MASK, + g_variant_new_string(mask)); + + g_variant_builder_add_value(builder, g_variant_builder_end(&sub_builder)); } pcap_freealldevs(alldevs); @@ -1371,8 +1391,8 @@ API stc_plugin_pcap_s stc_plugin_pcap = { stc_plugin_pcap_lookup_dev, .lookup_net = stc_plugin_pcap_lookup_net, - .find_alldevs = - stc_plugin_pcap_find_alldevs, + .find_all_devs = + stc_plugin_pcap_find_all_devs, .register_loop = stc_plugin_pcap_register_loop, .unregister_loop = diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 14cf127..61a81b4 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -266,6 +266,10 @@ static gboolean __stc_manager_gdbus_pcap_init(stc_s *stc) G_CALLBACK(handle_pcap_get_all), stc); + g_signal_connect(pcap, "handle-find-all-devs", + G_CALLBACK(handle_pcap_find_all_devs), + 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 dc96424..8134391 100755 --- a/src/stc-manager-plugin-pcap.c +++ b/src/stc-manager-plugin-pcap.c @@ -101,7 +101,7 @@ int stc_plugin_pcap_lookup_net(void) return stc_plugin->lookup_net(); } -int stc_plugin_pcap_find_alldevs(void) +int stc_plugin_pcap_find_all_devs(GVariantBuilder *builder) { if (!stc_plugin_enabled) { if (STC_DEBUG_LOG) @@ -115,7 +115,7 @@ int stc_plugin_pcap_find_alldevs(void) return STC_ERROR_UNINITIALIZED; } - return stc_plugin->find_alldevs(); + return stc_plugin->find_all_devs(builder); } int stc_plugin_pcap_register_loop(const char *ifname, diff --git a/src/stc-pcap.c b/src/stc-pcap.c index c24e34b..cd961e6 100755 --- a/src/stc-pcap.c +++ b/src/stc-pcap.c @@ -179,3 +179,24 @@ gboolean handle_pcap_get_all(StcPcap *object, __STC_LOG_FUNC_EXIT__; return TRUE; } + +gboolean handle_pcap_find_all_devs(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_find_all_devs(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; +} -- 2.7.4