Revert "Remove codes to launch mdnsd"
authorCheoleun Moon <chleun.moon@samsung.com>
Tue, 16 Mar 2021 10:39:00 +0000 (19:39 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Tue, 16 Mar 2021 10:39:00 +0000 (19:39 +0900)
This reverts commit d64465cb8d80c549dbe50fe4dad643c68f4a98c3.

gtest/gdbus.h
gtest/network_state.cpp
gtest/network_state.h
gtest/unittest.cpp
include/util.h
interfaces/netconfig-iface-network-state.xml
packaging/net-config.spec
resources/etc/dbus-1/system.d/net-config.conf
src/network-state.c
src/utils/util.c

index 39c61e7..6008b70 100755 (executable)
 #define CHECK_GET_PRIVILEGE "CheckGetPrivilege"
 #define CHECK_PROFILE_PRIVILEGE "CheckProfilePrivilege"
 #define CHECK_INTERNET_Privilege "CheckInternetPrivilege"
+#define LAUNCH_MDNS "LaunchMdns"
 #define DEVICE_POLICY_SET_WIFI "DevicePolicySetWifi"
 #define DEVICE_POLICY_GET_WIFI "DevicePolicyGetWifi"
 #define DEVICE_POLICY_SET_WIFI_PROFILE "DevicePolicySetWifiProfile"
index c31ece5..4fad5c8 100755 (executable)
@@ -105,6 +105,27 @@ error_e NetworkState::EthernetCableState(int *state)
        return ERROR_NONE;
 }
 
+error_e NetworkState::LaunchMdns(const char *name)
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+
+       message = InvokeMethod(NETCONFIG_SERVICE,
+               NETCONFIG_NETWORK_PATH,
+               NETCONFIG_NETWORK_INTERFACE,
+               LAUNCH_MDNS,
+               g_variant_new("(s)", name),
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
 error_e NetworkState::DevicePolicySetWifi(int state)
 {
        GVariant *message = NULL;
index 887fb7f..7e33fb2 100755 (executable)
@@ -28,6 +28,7 @@ public:
        error_e RemoveRoute(const char *ip, const char *mask, const char *interface,
                        const char *gateway, int family, gboolean *result);
        error_e EthernetCableState(int *state);
+       error_e LaunchMdns(const char *name);
        error_e DevicePolicySetWifi(int state);
        error_e DevicePolicyGetWifi(int *state);
        error_e DevicePolicySetWifiProfile(int state);
index 356c9da..679b8a8 100755 (executable)
@@ -191,6 +191,26 @@ TEST(NetworkState, RemoveRoute_n)
        EXPECT_NE(ERROR_NONE, ret);
 }
 
+TEST(NetworkState, LaunchMdns_p)
+{
+       error_e ret = ERROR_NONE;
+       NetworkState mgr;
+       const char *name = "gtest";
+
+       ret = mgr.LaunchMdns(name);
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(NetworkState, LaunchMdns_n)
+{
+       error_e ret = ERROR_NONE;
+       NetworkState mgr;
+       const char *name = "gtest";
+
+       ret = mgr.LaunchMdns(name);
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
 TEST(NetworkState, DevicePolicySetWifi_p1)
 {
        error_e ret = ERROR_NONE;
index 73f41c5..436f49c 100755 (executable)
@@ -86,6 +86,8 @@ int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gi
 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family);
 
 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context);
+gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
+                                                       gchar *name);
 
 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * data);
 int netconfig_send_message_to_net_popup(const char *title,
index 5b8b46a..57f7938 100755 (executable)
@@ -23,6 +23,9 @@
                <method name="CheckGetPrivilege"></method>
                <method name="CheckProfilePrivilege"></method>
                <method name="CheckInternetPrivilege"></method>
+               <method name="LaunchMdns">
+                       <arg type="s" name="name" direction="in"/>
+               </method>
                <method name="DevicePolicySetWifi">
                        <arg type="i" name="state" direction="in"/>
                </method>
index a16ef7d..4f8059e 100755 (executable)
@@ -1,6 +1,6 @@
 Name:          net-config
 Summary:       TIZEN Network Configuration service
-Version:       1.2.9
+Version:       1.2.8
 Release:       1
 Group:         System/Network
 License:       Apache-2.0
index c651886..2c8fd42 100755 (executable)
@@ -18,6 +18,8 @@
        <policy context="default">
                <deny own="net.netconfig"/>
                <deny send_destination="net.netconfig"/>
+               <allow send_destination="net.netconfig" send_interface="net.netconfig.network" send_member="LaunchMdns" />
+               <allow send_destination="net.netconfig" send_interface="net.netconfig.network" send_member="UnrefMdns" />
 
                <allow send_destination="net.netconfig" send_interface="net.netconfig.network" send_member="DevicePolicyGetWifi" />
                <allow send_destination="net.netconfig" send_interface="net.netconfig.network" send_member="DevicePolicyGetWifiProfile" />
index eed90e3..75dea27 100755 (executable)
@@ -1433,6 +1433,8 @@ void state_object_create_and_init(void)
                                G_CALLBACK(handle_preferred_ipv6_address), NULL);
        g_signal_connect(netconfigstate, "handle-remove-route",
                                G_CALLBACK(handle_remove_route), NULL);
+       g_signal_connect(netconfigstate, "handle-launch-mdns",
+                               G_CALLBACK(handle_launch_mdns), NULL);
        g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
                                G_CALLBACK(handle_device_policy_set_wifi), NULL);
        g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
index a6d44a5..bf7c4bc 100755 (executable)
@@ -58,6 +58,7 @@
 #define CONNMAN_WIFI_DEF_IFNAME                "DefaultWifiInterface"
 
 static gboolean netconfig_device_picker_test = FALSE;
+static int mdnsd_ref_count = 0;
 typedef struct {
        char *conn_name;
        int conn_id;
@@ -954,6 +955,96 @@ gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
        return TRUE;
 }
 
+int execute_mdnsd_script(char* op)
+{
+       const char *path = "/usr/bin/mdnsresponder-server.sh";
+       char *const args[] = { "mdnsresponder-server.sh", op, NULL };
+       char *const envs[] = { NULL };
+
+       return netconfig_execute_file(path, args, envs);
+}
+
+static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
+               const gchar *Name, const gchar *path, const gchar *interface,
+               const gchar *sig, GVariant *param, gpointer user_data)
+{
+       gchar *name = NULL;
+       gchar *old = NULL;
+       gchar *new = NULL;
+       dnssd_conn_destroy_data *data = user_data;
+       GDBusConnection *connection = NULL;
+       connection = netdbus_get_connection();
+
+       if (param == NULL)
+               return;
+
+       g_variant_get(param, "(sss)", &name, &old, &new);
+
+       if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
+               DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
+                       data->conn_id);
+               mdnsd_ref_count--;
+               g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
+               if (mdnsd_ref_count == 0) {
+                       if (execute_mdnsd_script("stop") < 0)
+                               ERR("Failed to stop mdnsresponder daemon");
+               }
+       }
+       g_free(name);
+       g_free(old);
+       g_free(new);
+       g_free(data->conn_name);
+       g_free(data);
+       return;
+}
+
+static void register_dnssd_conn_destroy_signal(gchar *name)
+{
+       dnssd_conn_destroy_data *data;
+       GDBusConnection *connection = NULL;
+       connection = netdbus_get_connection();
+
+       if (connection == NULL) {
+               ERR("Failed to get GDbus Connection");
+               return;
+       }
+
+       data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
+
+       if (data == NULL) {
+               ERR("Out of Memory!");
+               return;
+       }
+
+       data->conn_name = g_strdup(name);
+
+       data->conn_id = g_dbus_connection_signal_subscribe(connection,
+                                                       DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
+                                                       "NameOwnerChanged", NULL, name,
+                                                       G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
+                                                       data, NULL);
+       return;
+}
+
+gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
+                                                       gchar *name)
+{
+       DBG("Launch mdnsresponder daemon");
+
+       if (execute_mdnsd_script("start") < 0) {
+               ERR("Failed to launch mdnsresponder daemon");
+               netconfig_error_invalid_parameter(context);
+               return TRUE;
+       }
+
+       mdnsd_ref_count++;
+       register_dnssd_conn_destroy_signal(name);
+       DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
+
+       network_complete_launch_mdns(object, context);
+       return TRUE;
+}
+
 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
 {
        if (!netconfig_plugin_headed_enabled)