Add internal APIs for appcontrol 00/88500/6
authorSeonah Moon <seonah1.moon@samsung.com>
Mon, 19 Sep 2016 10:32:34 +0000 (19:32 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Tue, 27 Sep 2016 05:59:46 +0000 (14:59 +0900)
Change-Id: Id49f8b977e2cd3d3c449ce193077ce3c639f66e4
Signed-off-by: Seonah Moon <seonah1.moon@samsung.com>
CMakeLists.txt
include/vpn_service_internal.h
src/capi_vpn_service.c
test/vpn_service_test.c

index 6dc3c6e..3de910a 100755 (executable)
@@ -40,6 +40,7 @@ TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} -lrt -ldl)
 
 INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION ${LIBDIR})
 INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/vpn_service.h DESTINATION include)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/vpn_service_internal.h DESTINATION include)
 
 SET_TARGET_PROPERTIES(${PACKAGE_NAME}
       PROPERTIES
index 24fc6bc..466d348 100755 (executable)
@@ -34,7 +34,57 @@ extern "C" {
 
 /**
  * @internal
- * @brief Sets-up VPN interface and brings it up. Installs specified routes/DNS servers/DNS suffix.
+ * @brief Creates VPN service handle.
+ * @since_tizen 3.0
+ * @privlevel platform
+ * @param[in] handle                           The VPN interface handle
+ * @return 0 on success. otherwise, a negative error value.
+ * @retval #VPNSVC_ERROR_NONE                  Success
+ * @retval #VPNSVC_ERROR_INVALID_PARAMETER     Invalid parameter
+ * @retval #VPNSVC_ERROR_IPC_FAILED            Cannot connect to service daemon
+ * @retval #VPNSVC_ERROR_PERMISSION_DENIED     Permission Denied
+ * @retval #VPNSVC_ERROR_NOT_SUPPORTED         Not Supported
+ * @post If you want to set interface down, please call vpnsvc_down().
+ */
+int vpnsvc_create(vpnsvc_h *handle);
+
+/**
+ * @internal
+ * @brief Destroys VPN service handle.
+ * @since_tizen 3.0
+ * @privlevel platform
+ * @param[in] handle                           The VPN interface handle
+ * @return 0 on success. otherwise, a negative error value.
+ * @retval #VPNSVC_ERROR_NONE                  Success
+ * @retval #VPNSVC_ERROR_INVALID_PARAMETER     Invalid parameter
+ * @retval #VPNSVC_ERROR_IPC_FAILED            Cannot connect to service daemon
+ * @retval #VPNSVC_ERROR_PERMISSION_DENIED     Permission Denied
+ * @retval #VPNSVC_ERROR_NOT_SUPPORTED         Not Supported
+ * @pre The VPN interface should be created already.
+ */
+int vpnsvc_destroy(vpnsvc_h handle);
+
+/**
+ * @internal
+ * @brief Sets VPN interface name.
+ * @since_tizen 3.0
+ * @privlevel platform
+ * @param[in] handle                           The VPN interface handle
+ * @param[in] iface_name                       The VPN interface name
+ * @return 0 on success. otherwise, a negative error value.
+ * @retval #VPNSVC_ERROR_NONE                  Success
+ * @retval #VPNSVC_ERROR_INVALID_PARAMETER     Invalid parameter
+ * @retval #VPNSVC_ERROR_IPC_FAILED            Cannot connect to service daemon
+ * @retval #VPNSVC_ERROR_PERMISSION_DENIED     Permission Denied
+ * @retval #VPNSVC_ERROR_NOT_SUPPORTED         Not Supported
+ * @pre The VPN interface should be initialized already.
+ * @post If you want to set interface down, please call vpnsvc_down().
+ */
+int vpnsvc_set_iface_name(vpnsvc_h handle, const char *iface_name);
+
+/**
+ * @internal
+ * @brief Sets-up VPN interface and brings it up.
  * @since_tizen 3.0
  * @privlevel platform
  * @param[in] handle                           The VPN interface handle
index 96e76ae..bd57ef1 100755 (executable)
@@ -101,6 +101,7 @@ static void _vpnsvc_init_vpnsvc_tun_s(vpnsvc_tun_s **s)
        }
 
        /* Setting Default User Settings */
+       (*s)->index = 0;
        (*s)->mtu = _USER_SETTING_DEFAULT_MTU;
        strncpy((*s)->session, _USER_SETTING_DEFAULT_SESSION, VPNSVC_SESSION_STRING_LEN);
        (*s)->session[VPNSVC_SESSION_STRING_LEN-1] = '\0';
@@ -246,6 +247,47 @@ GVariant *_vpnsvc_invoke_dbus_method_with_fd(GDBusConnection *connection,
        return reply;
 }
 
+EXPORT_API int vpnsvc_create(vpnsvc_h *handle)
+{
+       CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE);
+
+       LOGD("enter vpnsvc_create");
+
+       /* parameter check */
+       if (_vpn_check_handle_validity(*handle)) {
+               LOGE("Invalid parameter"); //LCOV_EXCL_LINE
+               return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       }
+
+       vpnsvc_tun_s *tmp_s = NULL;
+       _vpnsvc_init_vpnsvc_tun_s(&tmp_s);
+       *handle = tmp_s;
+       LOGD("handle : %p", (*handle));
+
+       return VPNSVC_ERROR_NONE;
+}
+
+EXPORT_API int vpnsvc_destroy(vpnsvc_h handle)
+{
+       CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE);
+
+       int result = VPNSVC_ERROR_NONE;
+       vpnsvc_tun_s *tun_s = NULL;
+
+       /* parameter check */
+       if (handle == NULL) {
+               LOGE("handle is a NULL"); //LCOV_EXCL_LINE
+               return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       }
+       tun_s = (vpnsvc_tun_s*)handle;
+
+       LOGD("enter vpnsvc_destroy");
+
+       _vpnsvc_deinit_vpnsvc_tun_s(tun_s);
+
+       return result;
+}
+
 EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle)
 {
        CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE);
@@ -255,7 +297,6 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle)
        int iface_fd = 0;
 
        LOGD("enter vpnsvc_init, iface_name : %s", iface_name);
-       LOGD("handle : %p\n", handle);
 
        /* parameter check */
        if (iface_name == NULL || strlen(iface_name) <= 0) {
@@ -474,22 +515,15 @@ EXPORT_API int vpnsvc_up(vpnsvc_h handle)
 
        LOGD("enter vpnsvc_up");
 
-       if (tun_s->index <= 0) {
-               LOGE("invalid handle"); //LCOV_EXCL_LINE
-               return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
-       } else if (tun_s->connection == NULL) {
+       if (tun_s->connection == NULL) {
                LOGE("Connection Object is NULL"); //LCOV_EXCL_LINE
                return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
-       }
-
-       if (tun_s->local_ip == NULL || tun_s->remote_ip == NULL) {
-               LOGE("local and remote ip are invalid"); //LCOV_EXCL_LINE
+       } else if (tun_s->name[0] == 0) {
+               LOGE("invalid handle"); //LCOV_EXCL_LINE
                return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
-       LOGD("iface_index %d", tun_s->index);
-       LOGD("local_ip : %s, remote_ip : %s", tun_s->local_ip, tun_s->remote_ip);
-       LOGD("dns_suffix : %s", tun_s->dns_suffix);
+       LOGD("iface_index %d, iface_name %s", tun_s->index, tun_s->name);
 
        op = _vpnsvc_invoke_dbus_method(tun_s->connection,
                                                                NETCONFIG_SERVICE_NAME,
@@ -507,7 +541,7 @@ EXPORT_API int vpnsvc_up(vpnsvc_h handle)
                                                                NETCONFIG_VPNSVC_PATH,
                                                                NETCONFIG_VPNSVC_INTERFACE_NAME,
                                                                "vpn_up",
-                                                               g_variant_new("(i)", tun_s->index),
+                                                               g_variant_new("(s)", tun_s->name),
                                                                &dbus_result);
 
        if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED)
@@ -546,9 +580,6 @@ EXPORT_API int vpnsvc_down(vpnsvc_h handle)
        if (tun_s == NULL) {
                LOGE("handle is a NULL"); //LCOV_EXCL_LINE
                return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
-       } else if (tun_s->index <= 0) {
-               LOGE("invalid handle"); //LCOV_EXCL_LINE
-               return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        } else if (tun_s->connection == NULL) {
                LOGE("Connection Object is NULL"); //LCOV_EXCL_LINE
                return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
@@ -570,7 +601,7 @@ EXPORT_API int vpnsvc_down(vpnsvc_h handle)
                                                                NETCONFIG_VPNSVC_PATH,
                                                                NETCONFIG_VPNSVC_INTERFACE_NAME,
                                                                "vpn_down",
-                                                               g_variant_new("(i)", tun_s->index),
+                                                               g_variant_new("(s)", tun_s->name),
                                                                &dbus_result);
 
        if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED)
@@ -818,62 +849,11 @@ EXPORT_API int vpnsvc_update_settings(vpnsvc_h handle)
        tun_s = (vpnsvc_tun_s*)handle;
 
        op = _vpnsvc_invoke_dbus_method(tun_s->connection,
-                                                       NETCONFIG_SERVICE_NAME,
-                                                       NETCONFIG_NETWORK_PATH,
-                                                       NETCONFIG_NETWORK_INTERFACE,
-                                                       "CheckInternetPrivilege",
-                                                       NULL,
-                                                       &dbus_result);
-
-       if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED)
-               return VPNSVC_ERROR_PERMISSION_DENIED;
-
-       op = _vpnsvc_invoke_dbus_method(tun_s->connection,
-                       NETCONFIG_SERVICE_NAME,
-                       NETCONFIG_VPNSVC_PATH,
-                       NETCONFIG_VPNSVC_INTERFACE_NAME,
-                       "vpn_set_local_ip_address",
-                       g_variant_new("(is)", tun_s->index, tun_s->local_ip),
-                       &dbus_result);
-       if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED)
-               return VPNSVC_ERROR_PERMISSION_DENIED;
-
-       if (op == NULL) {
-               return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE
-       } else {
-               g_variant_get(op, "(i)", &result);
-               if (result != VPNSVC_ERROR_NONE)
-                       LOGE("vpn_set_local_ip_address() failed"); //LCOV_EXCL_LINE
-               else
-                       LOGD("vpn_set_local_ip_address() succeed");
-       }
-
-       op = _vpnsvc_invoke_dbus_method(tun_s->connection,
-                                                               NETCONFIG_SERVICE_NAME,
-                                                               NETCONFIG_VPNSVC_PATH,
-                                                               NETCONFIG_VPNSVC_INTERFACE_NAME,
-                                                               "vpn_set_remote_ip_address",
-                                                               g_variant_new("(is)", tun_s->index, tun_s->remote_ip),
-                                                               &dbus_result);
-       if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED)
-               return VPNSVC_ERROR_PERMISSION_DENIED;
-
-       if (op == NULL) {
-               return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE
-       } else {
-               g_variant_get(op, "(i)", &result);
-               if (result != VPNSVC_ERROR_NONE)
-                       LOGE("vpn_set_remote_ip_address() failed"); //LCOV_EXCL_LINE
-               else
-                       LOGD("vpn_set_remote_ip-address() succeed");
-       }
-
-       op = _vpnsvc_invoke_dbus_method(tun_s->connection,
                                                                NETCONFIG_SERVICE_NAME,
                                                                NETCONFIG_VPNSVC_PATH,
                                                                NETCONFIG_VPNSVC_INTERFACE_NAME,
-                                                               "vpn_set_mtu",
-                                                               g_variant_new("(iu)", tun_s->index, tun_s->mtu),
+                                                               "vpn_update_settings",
+                                                               g_variant_new("(issu)", tun_s->index, tun_s->local_ip, tun_s->remote_ip, tun_s->mtu),
                                                                &dbus_result);
        if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED)
                return VPNSVC_ERROR_PERMISSION_DENIED;
@@ -883,9 +863,9 @@ EXPORT_API int vpnsvc_update_settings(vpnsvc_h handle)
        } else {
                g_variant_get(op, "(i)", &result);
                if (result != VPNSVC_ERROR_NONE)
-                       LOGE("vpn_set_mtu() failed"); //LCOV_EXCL_LINE
+                       LOGE("vpn_update_settings() failed"); //LCOV_EXCL_LINE
                else
-                       LOGD("vpn_set_mtu() succeed");
+                       LOGD("vpn_update_settings() succeed");
        }
 
        return result;
@@ -968,12 +948,35 @@ EXPORT_API int vpnsvc_get_iface_name(vpnsvc_h handle, char** iface_name)
        return VPNSVC_ERROR_NONE;
 }
 
+EXPORT_API int vpnsvc_set_iface_name(vpnsvc_h handle, const char *iface_name)
+{
+       CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE);
+
+       vpnsvc_tun_s *tun_s = NULL;
+
+       /* parameter check */
+       if (handle == NULL) {
+               LOGE("handle is a NULL"); //LCOV_EXCL_LINE
+               return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       }
+       tun_s = (vpnsvc_tun_s*)handle;
+
+       if (iface_name == NULL) {
+               LOGE("Remote IP address is NULL"); //LCOV_EXCL_LINE
+               return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       }
+
+       strncpy(tun_s->name, iface_name, VPNSVC_VPN_IFACE_NAME_LEN);
+
+       return VPNSVC_ERROR_NONE;
+}
+
+
 EXPORT_API int vpnsvc_set_mtu(vpnsvc_h handle, int mtu)
 {
        CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE);
 
        int result = VPNSVC_ERROR_NONE;
-       int dbus_result;
        vpnsvc_tun_s *tun_s = NULL;
 
        /* parameter check */
index 010791d..baa3a88 100755 (executable)
@@ -336,7 +336,7 @@ int test_vpnsvc_set_local_ip_address()
        char local_ip[VPNSVC_IP4_STRING_LEN];
 
        printf("Input local IP address: ");
-       rv = scanf("%s", local_ip);
+       rv = scanf("%15s", local_ip);
 
        rv = vpnsvc_set_local_ip_address(handle, local_ip);
        if (rv != VPNSVC_ERROR_NONE) {
@@ -353,7 +353,7 @@ int test_vpnsvc_set_remote_ip_address()
        char remote_ip[VPNSVC_IP4_STRING_LEN];
 
        printf("Input remote IP address: ");
-       rv = scanf("%s", remote_ip);
+       rv = scanf("%15s", remote_ip);
 
        rv = vpnsvc_set_remote_ip_address(handle, remote_ip);
        if (rv != VPNSVC_ERROR_NONE) {
@@ -364,6 +364,19 @@ int        test_vpnsvc_set_remote_ip_address()
        return 1;
 }
 
+int    test_vpnsvc_update_settings()
+{
+       int rv;
+
+       rv = vpnsvc_update_settings(handle);
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc update settings [%s]\n", test_print_error(rv));
+               return -1;
+       }
+
+       return 1;
+}
+
 int test_vpnsvc_add_route()
 {
        int rv;
@@ -371,7 +384,7 @@ int test_vpnsvc_add_route()
        char addr[VPNSVC_IP4_STRING_LEN];
 
        printf("Input route address: ");
-       rv = scanf("%s", addr);
+       rv = scanf("%15s", addr);
        printf("Input prefix: ");
        rv = scanf("%d", &prefix);
 
@@ -391,7 +404,7 @@ int test_vpnsvc_remove_route()
        char addr[VPNSVC_IP4_STRING_LEN];
 
        printf("Input route address: ");
-       rv = scanf("%s", addr);
+       rv = scanf("%15s", addr);
        printf("Input prefix: ");
        rv = scanf("%d", &prefix);
 
@@ -410,7 +423,7 @@ int test_vpnsvc_add_dns_server()
        char addr[VPNSVC_IP4_STRING_LEN];
 
        printf("Input dns server: ");
-       rv = scanf("%s", addr);
+       rv = scanf("%15s", addr);
 
        rv = vpnsvc_add_dns_server(handle, addr);
        if (rv != VPNSVC_ERROR_NONE) {
@@ -427,7 +440,7 @@ int test_vpnsvc_remove_dns_server()
        char addr[VPNSVC_IP4_STRING_LEN];
 
        printf("Input dns server address: ");
-       rv = scanf("%s", addr);
+       rv = scanf("%15s", addr);
 
        rv = vpnsvc_remove_dns_server(handle, addr);
        if (rv != VPNSVC_ERROR_NONE) {
@@ -438,6 +451,61 @@ int test_vpnsvc_remove_dns_server()
        return 1;
 }
 
+int test_vpnsvc_create()
+{
+       int rv = VPNSVC_ERROR_NONE;
+       int int_value;
+
+       rv = vpnsvc_create(&handle);
+
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc create fail [%s]\n", test_print_error(rv));
+               return -1;
+       } else {
+               printf("vpnsvc create success(%p)\n", handle);
+               return 1;
+       }
+}
+
+int test_vpnsvc_destroy()
+{
+       int rv = VPNSVC_ERROR_NONE;
+       int int_value;
+
+       rv = vpnsvc_destroy(handle);
+
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc destroy fail [%s]\n", test_print_error(rv));
+               return -1;
+       } else {
+               printf("vpnsvc destroy success(%p)\n", handle);
+               return 1;
+       }
+}
+
+int test_vpnsvc_set_iface_name()
+{
+       int rv;
+       char iface_name[VPNSVC_VPN_IFACE_NAME_LEN];
+
+       if (!handle) {
+               printf("invalid handle - handle is NULL\n");
+               return -1;
+       }
+
+       printf("Interface name: ");
+       rv = scanf("%15s", iface_name);
+
+       rv = vpnsvc_set_iface_name(handle, iface_name);
+
+       if (rv != VPNSVC_ERROR_NONE)
+               printf("vpnsvc set iface name fail [%s]\n", test_print_error(rv));
+       else
+               printf("vpnsvc set iface name success\n");
+
+       return 1;
+}
+
 int test_exit()
 {
        exit(0);
@@ -459,10 +527,14 @@ int (*test_function_table[])(void) = {
        test_vpnsvc_set_session,
        test_vpnsvc_set_local_ip_address,
        test_vpnsvc_set_remote_ip_address,
+       test_vpnsvc_update_settings,
        test_vpnsvc_add_route,
        test_vpnsvc_remove_route,
        test_vpnsvc_add_dns_server,
        test_vpnsvc_remove_dns_server,
+       test_vpnsvc_create,
+       test_vpnsvc_destroy,
+       test_vpnsvc_set_iface_name,
        test_exit,
 };
 
@@ -476,8 +548,8 @@ int main()
                printf("1  : vpnsvc_init\n");
                printf("2  : vpnsvc_deinit\n");
                printf("3  : vpnsvc_protect\n");
-               printf("4  : vpnsvc_up(1->13->14)\n");
-               printf("5  : vpnsvc_down\n");
+               printf("4  : vpnsvc_up(internal)\n");
+               printf("5  : vpnsvc_down(internal)\n");
                printf("6  : vpnsvc_read\n");
                printf("7  : vpnsvc_write\n");
                printf("8  : vpnsvc_block_networks\n");
@@ -487,10 +559,14 @@ int main()
                printf("12 : vpnsvc_set_session\n");
                printf("13 : vpnsvc_set_local_ip_address\n");
                printf("14 : vpnsvc_set_remote_ip_address\n");
-               printf("15 : vpnsvc_add_route\n");
-               printf("16 : vpnsvc_remove_route\n");
-               printf("17 : vpnsvc_add_dns_server\n");
-               printf("18 : vpnsvc_remove_dns_server\n");
+               printf("15 : vpnsvc_update_settings\n");
+               printf("16 : vpnsvc_add_route\n");
+               printf("17 : vpnsvc_remove_route\n");
+               printf("18 : vpnsvc_add_dns_server\n");
+               printf("19 : vpnsvc_remove_dns_server\n");
+               printf("20 : vpnsvc_create(internal)\n");
+               printf("21 : vpnsvc_destroy(internal)\n");
+               printf("22 : vpnsvc_set_iface_name(internal)\n");
                printf("q  : quit\n");
 
                TEST_CONSOLE_INPUT(input, 3);