Added CAPIs for C# thread handling
[platform/core/api/connection.git] / src / libnetwork.c
index 7839ab5..c5d2433 100755 (executable)
@@ -45,6 +45,9 @@ struct _libnet_s {
        connection_set_default_cb set_default_cb;
        connection_reset_cb reset_profile_cb;
        libnet_ethernet_cable_state_changed_cb ethernet_cable_state_changed_cb;
+       libnet_type_changed_cb type_changed_cb;
+       libnet_ip_changed_cb ip_changed_cb;
+       libnet_proxy_changed_cb proxy_changed_cb;
        void *opened_user_data;
        void *closed_user_data;
        void *set_default_user_data;
@@ -65,7 +68,8 @@ struct managed_idle_data {
 };
 
 static __thread struct _profile_list_s profile_iterator = {0, 0, NULL};
-static __thread struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, NULL, false};
+static __thread struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, NULL,
+                                       NULL, NULL, NULL, NULL, NULL, NULL, false};
 static __thread GSList *managed_idler_list = NULL;
 static __thread bool connection_is_feature_checked[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
 static __thread bool connection_feature_supported[CONNECTION_SUPPORTED_FEATURE_MAX] = {0, };
@@ -312,6 +316,25 @@ static void __libnet_ethernet_cable_state_changed_cb(
                libnet.ethernet_cable_state_changed_cb(state);
 }
 
+static void __libnet_type_changed_cb(int type)
+{
+       if (libnet.type_changed_cb)
+               libnet.type_changed_cb(type);
+}
+
+static void __libnet_ip_changed_cb(connection_address_family_e addr_family,
+                                                                  char *ip_addr)
+{
+       if (libnet.ip_changed_cb)
+               libnet.ip_changed_cb(addr_family, ip_addr);
+}
+
+static void __libnet_proxy_changed_cb(char *proxy_addr)
+{
+       if (libnet.proxy_changed_cb)
+               libnet.proxy_changed_cb(proxy_addr);
+}
+
 static gboolean __libnet_state_changed_cb_idle(gpointer data)
 {
        struct _state_notify *notify = (struct _state_notify *)data;
@@ -469,6 +492,26 @@ static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
                CONNECTION_LOG(CONNECTION_INFO, "Got Ethernet cable detached Indication\n");
                __libnet_ethernet_cable_state_changed_cb(CONNECTION_ETHERNET_CABLE_DETACHED);
                break;
+       case NET_EVENT_NETWORK_TYPE_CHANGED:
+               CONNECTION_LOG(CONNECTION_INFO, "Got Network Type Changed Indication");
+               int *state = (int *) event_cb->Data;
+               __libnet_type_changed_cb(*state);
+               break;
+       case NET_EVENT_IPV4_ADDRESS_CHANGED:
+               CONNECTION_LOG(CONNECTION_INFO, "Got IPv4 Address Changed Indication");
+               char *ipv4_addr = (char *)event_cb->Data;
+               __libnet_ip_changed_cb(CONNECTION_ADDRESS_FAMILY_IPV4, ipv4_addr);
+               break;
+       case NET_EVENT_IPV6_ADDRESS_CHANGED:
+               CONNECTION_LOG(CONNECTION_INFO, "Got IPv6 Address Changed Indication");
+               char *ipv6_addr = (char *)event_cb->Data;
+               __libnet_ip_changed_cb(CONNECTION_ADDRESS_FAMILY_IPV6, ipv6_addr);
+               break;
+       case NET_EVENT_PROXY_ADDRESS_CHANGED:
+               CONNECTION_LOG(CONNECTION_INFO, "Got Proxy Changed Indication");
+               char *proxy_addr = (char *)event_cb->Data;
+               __libnet_proxy_changed_cb(proxy_addr);
+               break;
 
        default:
                break;
@@ -476,18 +519,6 @@ static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
 }
 //LCOV_EXCL_STOP
 
-static int __libnet_check_address_type(int address_family, const char *address)
-{
-       struct in6_addr buf;
-       int err = 0;
-
-       err = inet_pton(address_family, address, &buf);
-       if (err > 0)
-               return 1;
-
-       return 0;
-}
-
 int __libnet_get_connected_count(struct _profile_list_s *profile_list)
 {
        int count = 0;
@@ -584,6 +615,16 @@ bool _connection_libnet_deinit(void)
        return true;
 }
 
+void _connection_set_cs_tid(int tid)
+{
+       net_set_cs_tid(tid);
+}
+
+void _connection_unset_cs_tid(int tid)
+{
+       net_unset_cs_tid(tid);
+}
+
 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
 {
        GSList *list;
@@ -618,6 +659,27 @@ bool _connection_libnet_check_profile_cb_validity(connection_profile_h profile)
 }
 //LCOV_EXCL_STOP
 
+int _connection_libnet_get_metered_state(bool* is_metered)
+{
+       int rv = 0;
+       int status = 0;
+
+       rv = net_get_metered_state(&status);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Failed to get metered state[%d]", rv);
+               return CONNECTION_ERROR_OPERATION_FAILED;
+       }
+
+       if (status == 1)
+               *is_metered = true;
+       else
+               *is_metered = false;
+       return CONNECTION_ERROR_NONE;
+}
+
 int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
 {
        int rv;
@@ -653,8 +715,23 @@ int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
        return CONNECTION_ERROR_NONE;
 }
 
+void _connection_libnet_set_type_changed_cb(libnet_type_changed_cb callback)
+{
+       libnet.type_changed_cb = callback;
+}
+
+void _connection_libnet_set_ip_changed_cb(libnet_ip_changed_cb callback)
+{
+       libnet.ip_changed_cb = callback;
+}
+
+void _connection_libnet_set_proxy_changed_cb(libnet_proxy_changed_cb callback)
+{
+       libnet.proxy_changed_cb = callback;
+}
+
 //LCOV_EXCL_START
-int _connection_libnet_get_ethernet_state(connection_ethernet_state_estate)
+int _connection_libnet_get_ethernet_state(connection_ethernet_state_e *state)
 {
        int rv;
        struct _profile_list_s ethernet_profiles = {0, 0, NULL};
@@ -721,7 +798,7 @@ int _connection_libnet_set_ethernet_cable_state_changed_cb(
 }
 //LCOV_EXCL_STOP
 
-int _connection_libnet_get_bluetooth_state(connection_bt_state_estate)
+int _connection_libnet_get_bluetooth_state(connection_bt_state_e *state)
 {
        int i = 0;
        int rv = 0;
@@ -1206,24 +1283,15 @@ int _connection_libnet_add_route(const char *interface_name, const char *host_ad
        char *endstr = NULL;
        int address_family = 0;
 
-       if (__libnet_check_address_type(AF_INET, host_address))
-               address_family = AF_INET;
-       else
-               return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       address_family = AF_INET;
 
-       switch (address_family) {
-       case AF_INET:
-               endstr = strrchr(host_address, '.');
-               if (endstr == NULL ||
-                               strcmp(endstr, ".0") == 0 ||
-                               strncmp(host_address, "0.", 2) == 0 ||
-                               strstr(host_address, "255") != NULL) {
-                       CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
-                       return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
-               }
-               break;
-       default:
-               return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+       endstr = strrchr(host_address, '.');
+       if (endstr == NULL ||
+                       strcmp(endstr, ".0") == 0 ||
+                       strncmp(host_address, "0.", 2) == 0 ||
+                       strstr(host_address, "255") != NULL) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
+               return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
        rv = net_add_route(host_address, interface_name, address_family);
@@ -1242,24 +1310,15 @@ int _connection_libnet_remove_route(const char *interface_name, const char *host
        char *endstr = strrchr(host_address, '.');
        int address_family = 0;
 
-       if (__libnet_check_address_type(AF_INET, host_address))
-               address_family = AF_INET;
-       else
-               return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       address_family = AF_INET;
 
-       switch (address_family) {
-       case AF_INET:
-               endstr = strrchr(host_address, '.');
-               if (endstr == NULL ||
-                       strcmp(endstr, ".0") == 0 ||
-                       strncmp(host_address, "0.", 2) == 0 ||
-                       strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
-                       CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
-                       return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
-               }
-               break;
-       default:
-               return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+       endstr = strrchr(host_address, '.');
+       if (endstr == NULL ||
+               strcmp(endstr, ".0") == 0 ||
+               strncmp(host_address, "0.", 2) == 0 ||
+               strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
+               return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
        rv = net_remove_route(host_address, interface_name, address_family);
@@ -1278,22 +1337,12 @@ int _connection_libnet_add_route_ipv6(const char *interface_name, const char *ho
        int address_family = 0;
 
        address_family = AF_INET6;
-/*     if(__libnet_check_address_type(AF_INET6, host_address))
-               address_family = AF_INET6;
-       else
-               return CONNECTION_ERROR_INVALID_PARAMETER;*/
 
-       switch (address_family) {
-       case AF_INET6:
-               if (strncmp(host_address, "fe80:", 5) == 0 ||
-                       strncmp(host_address, "ff00:", 5) == 0 ||
-                       strncmp(host_address, "::", 2) == 0) {
-                       CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
-                       return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
-               }
-               break;
-       default:
-               return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+       if (strncmp(host_address, "fe80:", 5) == 0 ||
+               strncmp(host_address, "ff00:", 5) == 0 ||
+               strncmp(host_address, "::", 2) == 0) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
+               return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
        rv = net_add_route_ipv6(host_address, interface_name, address_family, gateway);
@@ -1312,30 +1361,119 @@ int _connection_libnet_remove_route_ipv6(const char *interface_name, const char
        int address_family = 0;
 
        address_family = AF_INET6;
-/*     if (__libnet_check_address_type(AF_INET6, host_address))
-               address_family = AF_INET6;
+
+       if (strncmp(host_address, "fe80:", 5) == 0 ||
+               strncmp(host_address, "ff00:", 5) == 0 ||
+               strncmp(host_address, "::", 2) == 0) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
+               return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       }
+
+       rv = net_remove_route_ipv6(host_address, interface_name, address_family, gateway);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
+               return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
+       } else if (rv != NET_ERR_NONE)
+               return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+
+       return CONNECTION_ERROR_NONE;
+}
+
+int _connection_libnet_add_route_entry(connection_address_family_e address_family,
+               const char *interface_name, const char *host_address, const char *gateway)
+{
+       int rv;
+       char *endstr = NULL;
+       int address_family_type = 0;
+
+       if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
+               address_family_type = AF_INET;
        else
-               return CONNECTION_ERROR_INVALID_PARAMETER;*/
+               address_family_type = AF_INET6;
+
+       if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
+
+               endstr = strrchr(host_address, '.');
+               if (endstr == NULL ||
+                               strcmp(endstr, ".0") == 0 ||
+                               strncmp(host_address, "0.", 2) == 0 ||
+                               strstr(host_address, "255") != NULL) {
+                       CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
+                       return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+               }
+
+               rv = net_add_route_entry(host_address, interface_name, address_family_type, gateway);
+               if (rv == NET_ERR_ACCESS_DENIED) {
+                       CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
+                       return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
+               } else if (rv != NET_ERR_NONE)
+                       return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+
+       } else {
 
-       switch (address_family) {
-       case AF_INET6:
                if (strncmp(host_address, "fe80:", 5) == 0 ||
                        strncmp(host_address, "ff00:", 5) == 0 ||
                        strncmp(host_address, "::", 2) == 0) {
                        CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
                        return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
                }
-               break;
-       default:
-               return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+
+               rv = net_add_route_ipv6(host_address, interface_name, address_family_type, gateway);
+               if (rv == NET_ERR_ACCESS_DENIED) {
+                       CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
+                       return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
+               } else if (rv != NET_ERR_NONE)
+                       return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
        }
 
-       rv = net_remove_route_ipv6(host_address, interface_name, address_family, gateway);
-       if (rv == NET_ERR_ACCESS_DENIED) {
-               CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
-               return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
-       } else if (rv != NET_ERR_NONE)
-               return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+       return CONNECTION_ERROR_NONE;
+}
+
+int _connection_libnet_remove_route_entry(connection_address_family_e address_family,
+               const char *interface_name, const char *host_address, const char *gateway)
+{
+       int rv;
+       char *endstr = strrchr(host_address, '.');
+       int address_family_type = 0;
+
+       if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
+               address_family_type = AF_INET;
+       else
+               address_family_type = AF_INET6;
+
+       if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
+               endstr = strrchr(host_address, '.');
+               if (endstr == NULL ||
+                       strcmp(endstr, ".0") == 0 ||
+                       strncmp(host_address, "0.", 2) == 0 ||
+                       strstr(host_address, ".0.") != NULL || strstr(host_address, "255") != NULL) {
+                       CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed"); //LCOV_EXCL_LINE
+                       return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+               }
+
+               rv = net_remove_route_entry(host_address, interface_name, address_family_type, gateway);
+               if (rv == NET_ERR_ACCESS_DENIED) {
+                       CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
+                       return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
+               } else if (rv != NET_ERR_NONE)
+                       return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+
+       } else {
+
+               if (strncmp(host_address, "fe80:", 5) == 0 ||
+                       strncmp(host_address, "ff00:", 5) == 0 ||
+                       strncmp(host_address, "::", 2) == 0) {
+                       CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n"); //LCOV_EXCL_LINE
+                       return CONNECTION_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+               }
+
+               rv = net_remove_route_ipv6(host_address, interface_name, address_family_type, gateway);
+               if (rv == NET_ERR_ACCESS_DENIED) {
+                       CONNECTION_LOG(CONNECTION_ERROR, "Access denied"); //LCOV_EXCL_LINE
+                       return CONNECTION_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
+               } else if (rv != NET_ERR_NONE)
+                       return CONNECTION_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+       }
 
        return CONNECTION_ERROR_NONE;
 }
@@ -1584,3 +1722,36 @@ int _connection_check_feature_supported(const char *feature_name, ...)
        set_last_result(CONNECTION_ERROR_NONE);
        return CONNECTION_ERROR_NONE;
 }
+
+int _connection_libnet_start_tcpdump(void)
+{
+       connection_error_e result = CONNECTION_ERROR_NONE;
+       net_err_t ret = NET_ERR_NONE;
+
+       ret = net_start_tcpdump();
+       result = __libnet_convert_to_cp_error_type(ret);
+
+       return result;
+}
+
+int _connection_libnet_stop_tcpdump(void)
+{
+       connection_error_e result = CONNECTION_ERROR_NONE;
+       net_err_t ret = NET_ERR_NONE;
+
+       ret = net_stop_tcpdump();
+       result = __libnet_convert_to_cp_error_type(ret);
+
+       return result;
+}
+
+int _connection_libnet_get_tcpdump_state(gboolean *tcpdump_state)
+{
+       connection_error_e result = CONNECTION_ERROR_NONE;
+       net_err_t ret = NET_ERR_NONE;
+
+       ret = net_get_tcpdump_state(tcpdump_state);
+       result = __libnet_convert_to_cp_error_type(ret);
+
+       return result;
+}