Support 6GHz band for channels and RSSI level
[platform/core/connectivity/net-config.git] / src / ip-conflict-detect.c
index 9021c04..4e15614 100755 (executable)
@@ -34,6 +34,7 @@
 #include <unistd.h>
 #include <stdbool.h>
 
+#include "netdbus.h"
 #include "ip-conflict-detect.h"
 #include "network-state.h"
 #include "log.h"
@@ -95,8 +96,10 @@ static struct timer_data td = {
 int ioctl_sock;
 static bool initial_bursts = true;
 bool is_ip_conflict_detect_enabled = false;
+bool is_ip_conflict_detect_running = false;
 static gboolean send_arp(gpointer data);
-static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac);
+static void __netconfig_wifi_notify_ip_conflict(const char *interface_name,
+                       char *state, char *mac);
 ip_conflict_state_e conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
 
 struct sock_data *sd;
@@ -132,11 +135,12 @@ static gboolean __arp_reply_timeout_cb(gpointer data)
                        sd->iteration == CONFLICT_REMOVE_ITERATION_LIMIT) {
                sd->iteration = 0;
                conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
-               __netconfig_wifi_notify_ip_conflict("resolved", GRATUITOUS_ARP_MAC_ADDR);
+               __netconfig_wifi_notify_ip_conflict(netconfig_get_default_ifname(),
+                       "resolved", GRATUITOUS_ARP_MAC_ADDR);
                initial_bursts = true;
        }
 
-       if (sd->timer_id > 0)
+       if (sd->timer_id)
                g_source_remove(sd->timer_id);
        sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
        return G_SOURCE_REMOVE;
@@ -152,6 +156,7 @@ static gboolean __netconfig_check_arp_receive(GIOChannel *source,
        char sbuf[WLAN_MAC_ADDR_MAX];
        char tbuf[WLAN_MAC_ADDR_MAX];
        const char *default_ip = NULL;
+       int mac_cmp = 0;
 
        if (g_io_channel_read_chars(source, buffer, ARP_PACKET_SIZE,
                                &bytes_read, NULL) == G_IO_STATUS_NORMAL) {
@@ -177,13 +182,13 @@ static gboolean __netconfig_check_arp_receive(GIOChannel *source,
 
                int zero_mac = strcmp(tbuf , GRATUITOUS_ARP_MAC_ADDR);
                if (zero_mac == 0) {
-                       DBG("Broadcast packet.\n");
+                       DBG("Broadcast packet.");
                        goto skip;
                }
                DBG("our mac= %s source mac= %s target mac= %s", netconfig_get_default_mac_address(), sbuf, tbuf);
-               int mac_cmp = strcmp(tbuf , netconfig_get_default_mac_address());
+               mac_cmp = strcmp(tbuf , netconfig_get_default_mac_address());
                if (mac_cmp != 0) {
-                       INFO("Packet not intended to us.\n");
+                       INFO("Packet not intended to us.");
                        goto out;
                }
 skip:
@@ -192,9 +197,11 @@ skip:
                if ((mac_cmp != 0) && (__convert_uchar_to_uint(arp_recv.s_IPaddr) == target_ip)) {
                        sd->iteration = 0;
                        if (conflict_state != NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED) {
-                               INFO("ip conflict is detected !\n");
+                               INFO("ip conflict is detected !");
                                conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED;
-                               __netconfig_wifi_notify_ip_conflict("conflict", sbuf);
+                               __netconfig_wifi_notify_ip_conflict(
+                                       netconfig_get_default_ifname(),
+                                       "conflict", sbuf);
                                sd->timeout = BURST_ARP_SEND_TIME;
                        }
 
@@ -232,7 +239,7 @@ static int __open_channel_and_sock(struct sock_data *sd)
                return -1;
 
        if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
-               INFO("socket Failed.\n");
+               INFO("socket Failed.");
                return -1;
        }
 
@@ -335,7 +342,7 @@ static gboolean send_arp(gpointer data)
        }
 
        if (ioctl(sd->chk_conflict_sd, SIOCGIFINDEX, &net_ifr) == -1) {
-               INFO("ioctl Failed. Error..... = %s\n",
+               INFO("ioctl Failed. Error..... = %s",
                                strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
                goto err;
        }
@@ -350,18 +357,20 @@ static gboolean send_arp(gpointer data)
 
        if (sendto(sd->chk_conflict_sd, &arp, sizeof(arp), 0,
                                (struct sockaddr*)&addr, sizeof(addr)) < 0) {
-               INFO("Sending ARP Packet Failed. Error. = %s\n",
+               INFO("Sending ARP Packet Failed. Error. = %s",
                                strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
                __close_channel_and_sock(sd);
                if (__open_channel_and_sock(sd) == -1)
                        INFO("__open_channel_and_sock failed");
                goto err;
        } else {
-               DBG("Sent ARP Packet \n");
+               /* DBG("Sent ARP Packet"); */
        }
 
-       g_source_remove(sd->timer_id);
-       sd->timer_id = 0;
+       if (sd->timer_id) {
+               g_source_remove(sd->timer_id);
+               sd->timer_id = 0;
+       }
 
        if (conflict_state == NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED || initial_bursts)
                sd->timeout = BURST_ARP_SEND_TIME;
@@ -381,7 +390,12 @@ err:
 
 struct sock_data * start_ip_conflict_mon(void)
 {
-       if (is_ip_conflict_detect_enabled == true) {
+       if (is_ip_conflict_detect_enabled == false) {
+               INFO("ip_conflict_detect is disabled");
+               return NULL;
+       }
+
+       if (is_ip_conflict_detect_running == true) {
                INFO("detection mode is set to true");
                return NULL;
        }
@@ -395,18 +409,17 @@ struct sock_data * start_ip_conflict_mon(void)
        }
        sd->chk_conflict_data_id = -1;
        sd->chk_conflict_sd = -1;
-       sd->timer_id = 0;
-       sd->iteration = 0;
 
        if (__open_channel_and_sock(sd) == -1) {
                INFO("__open_channel_and_sock failed");
                g_free(sd);
+               sd = NULL;
                return NULL;
        }
 
        sd->timeout = td.initial_time;
        send_arp(sd);
-       is_ip_conflict_detect_enabled = true;
+       is_ip_conflict_detect_running = true;
        conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
 
        return sd;
@@ -423,18 +436,18 @@ void stop_ip_conflict_mon()
        if (-1 < sd->chk_conflict_sd)
                __close_channel_and_sock(sd);
 
-       if (sd->timer_id > 0) {
+       if (sd->timer_id) {
                g_source_remove(sd->timer_id);
-               sd->timer_id = 0;
        }
        g_free(sd);
        sd = NULL;
-       is_ip_conflict_detect_enabled = false;
+       is_ip_conflict_detect_running = false;
        conflict_state = NETCONFIG_IP_CONFLICT_STATE_UNKNOWN;
        INFO("Monitoring stopped");
 }
 
-static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac)
+static void __netconfig_wifi_notify_ip_conflict(const char *interface_name,
+                       char *state, char *mac)
 {
        GVariantBuilder *builder = NULL;
 
@@ -442,7 +455,8 @@ static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac)
        g_variant_builder_add(builder, "{sv}", "state", g_variant_new_string(state));
        g_variant_builder_add(builder, "{sv}", "mac", g_variant_new_string(mac));
 
-       wifi_emit_ip_conflict_event((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
+       wifi_emit_ip_conflict_event((Wifi *)get_wifi_object(),
+               interface_name, g_variant_builder_end(builder));
        g_variant_builder_unref(builder);
 
        /* send notification using net-popup */
@@ -455,30 +469,33 @@ static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac)
 }
 
 gboolean handle_ip_conflict_set_enable(Wifi *wifi, GDBusMethodInvocation *context,
-                                      bool detect)
+                       const gchar *ifname, bool detect)
 {
        g_return_val_if_fail(wifi != NULL, TRUE);
 
        if (detect == false) {
-               if (sd != NULL)
+               if (is_ip_conflict_detect_enabled == true) {
                        stop_ip_conflict_mon();
-               else {
+                       is_ip_conflict_detect_enabled = false;
+               } else {
                        netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AlreadyExists");
-                       wifi_complete_ip_conflict_set_enable(wifi, context);
                        return TRUE;
                }
        } else {
-               if (sd == NULL) {
-                       if (start_ip_conflict_mon() == NULL) {
-                               INFO("Failed to start IP conflict monitoring");
-                               netconfig_error_dbus_method_return(context,
-                                               NETCONFIG_ERROR_INTERNAL, "Failed");
-                               wifi_complete_ip_conflict_set_enable(wifi, context);
-                               return TRUE;
+               if (is_ip_conflict_detect_enabled == false) {
+                       is_ip_conflict_detect_enabled = true;
+                       const char *def_profile = netconfig_get_default_profile();
+                       if (netconfig_is_wifi_profile(def_profile) ||
+                                       netconfig_is_ethernet_profile(def_profile)) {
+                               if (start_ip_conflict_mon() == NULL) {
+                                       INFO("Failed to start IP conflict monitoring");
+                                       netconfig_error_dbus_method_return(context,
+                                                       NETCONFIG_ERROR_INTERNAL, "Failed");
+                                       return TRUE;
+                               }
                        }
                } else {
                        netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AlreadyExists");
-                       wifi_complete_ip_conflict_set_enable(wifi, context);
                        return TRUE;
                }
        }
@@ -487,7 +504,8 @@ gboolean handle_ip_conflict_set_enable(Wifi *wifi, GDBusMethodInvocation *contex
        return TRUE;
 }
 
-gboolean handle_is_ip_conflict_detect_enabled(Wifi *wifi, GDBusMethodInvocation *context)
+gboolean handle_is_ip_conflict_detect_enabled(Wifi *wifi, GDBusMethodInvocation *context,
+                       const gchar *ifname)
 {
        g_return_val_if_fail(wifi != NULL, TRUE);
        GVariant *param = NULL;
@@ -496,7 +514,8 @@ gboolean handle_is_ip_conflict_detect_enabled(Wifi *wifi, GDBusMethodInvocation
        return TRUE;
 }
 
-gboolean handle_set_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *context, guint initial_time)
+gboolean handle_set_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *context,
+                       const gchar *ifname, guint initial_time)
 {
        g_return_val_if_fail(wifi != NULL, TRUE);
        INFO("%d", initial_time);
@@ -514,7 +533,8 @@ gboolean handle_set_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *contex
        return TRUE;
 }
 
-gboolean handle_get_ip_conflict_state(Wifi *wifi, GDBusMethodInvocation *context)
+gboolean handle_get_ip_conflict_state(Wifi *wifi, GDBusMethodInvocation *context,
+                       const gchar *ifname)
 {
        g_return_val_if_fail(wifi != NULL, TRUE);
        GVariant *param = NULL;
@@ -523,7 +543,8 @@ gboolean handle_get_ip_conflict_state(Wifi *wifi, GDBusMethodInvocation *context
        return TRUE;
 }
 
-gboolean handle_get_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *context)
+gboolean handle_get_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *context,
+                       const gchar *ifname)
 {
        g_return_val_if_fail(wifi != NULL, TRUE);
        GVariant *param = NULL;