From 1695270f78fe22b99fb256eb0a822897dce1657a Mon Sep 17 00:00:00 2001 From: "taesub.kim" Date: Mon, 11 Jan 2016 09:32:58 +0900 Subject: [PATCH] [ACR-472] Modified comments of ACR #2 Change-Id: I346d6c7325741f9ac44ba452e56a8a9ad55af869 Signed-off-by: Taesub Kim Signed-off-by: Deepak Kumar Sahu --- daemon/include/vpn_service_daemon.h | 10 ++-- daemon/src/vpn_service_daemon.c | 23 ++++---- daemon/src/vpnsvc.c | 75 ++++++++++------------- framework/src/capi_vpn_service.c | 82 ++++++++++++++----------- include/vpn_service.h | 115 ++++++++++++++++++------------------ test/vpn_service_test.c | 80 ++++++++++++++++--------- 6 files changed, 206 insertions(+), 179 deletions(-) diff --git a/daemon/include/vpn_service_daemon.h b/daemon/include/vpn_service_daemon.h index 9237184..755ce3e 100755 --- a/daemon/include/vpn_service_daemon.h +++ b/daemon/include/vpn_service_daemon.h @@ -27,12 +27,12 @@ int vpn_daemon_init(const char* tun_name, size_t tun_name_len, int fd, vpnsvc_tu int vpn_daemon_deinit(const char* dev_name); int vpn_daemon_protect(int socket, const char* dev_name); int vpn_daemon_up(int tun_index, const char* local_ip, const char* remote_ip, - const struct vpnsvc_route* routes, size_t nr_routes, - char** dns_servers, size_t nr_dns, size_t total_dns_string_cnt, - const char* dns_suffix, const unsigned int mtu); + const char* routes[], int prefix[], size_t nr_routes, + char** dns_servers, size_t nr_dns, size_t total_dns_string_cnt, + const char* dns_suffix, const unsigned int mtu); int vpn_daemon_down(int tun_index); -int vpn_daemon_block_networks(const struct vpnsvc_route* nets_vpn, size_t nr_nets_vpn, - const struct vpnsvc_route* nets_orig, size_t nr_nets_orig); +int vpn_daemon_block_networks(const char* nets_vpn[], int prefix_vpn[], size_t nr_nets_vpn, + const char* nets_orig[], int prefix_orig[], size_t nr_nets_orig); int vpn_daemon_unblock_networks(void); #endif /* __TIZEN_CAPI_VPN_SERVICE_DAEMON_H__ */ diff --git a/daemon/src/vpn_service_daemon.c b/daemon/src/vpn_service_daemon.c index 977426e..e664124 100755 --- a/daemon/src/vpn_service_daemon.c +++ b/daemon/src/vpn_service_daemon.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "vpn_service_daemon.h" @@ -81,7 +82,7 @@ static in_addr_t host2net(ipv4 host) return net; } -static int add_routes(char* if_name, const struct vpnsvc_route* routes, size_t nr_routes) +static int add_routes(char* if_name, const char* routes[], int prefix[], size_t nr_routes) { struct rtentry rt; struct sockaddr_in addr; @@ -102,7 +103,7 @@ static int add_routes(char* if_name, const struct vpnsvc_route* routes, size_t n memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(routes[i].dest); + addr.sin_addr.s_addr = inet_addr(routes[i]); memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst)); memset(&addr, 0, sizeof(addr)); @@ -114,7 +115,7 @@ static int add_routes(char* if_name, const struct vpnsvc_route* routes, size_t n memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_addr.s_addr = host2net(make_mask(routes[i].prefix)); + addr.sin_addr.s_addr = host2net(make_mask(prefix[i])); memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask)); rt.rt_dev = if_name; @@ -724,7 +725,7 @@ int vpn_daemon_protect(int socket_fd, const char* dev_name) } int vpn_daemon_up(int tun_index, const char* local_ip, const char* remote_ip, - const struct vpnsvc_route* routes, size_t nr_routes, + const char* routes[], int prefix[], size_t nr_routes, char** dns_servers, size_t nr_dns, size_t total_dns_string_cnt, const char* dns_suffix, const unsigned int mtu) { @@ -816,7 +817,7 @@ int vpn_daemon_up(int tun_index, const char* local_ip, const char* remote_ip, /* add routes */ if (nr_routes > 0) { - ret = add_routes(ifr_tun.ifr_name, routes, nr_routes); + ret = add_routes(ifr_tun.ifr_name, routes, prefix, nr_routes); if (ret != VPNSVC_ERROR_NONE) { LOGE("add_routes failed"); return ret; @@ -903,21 +904,21 @@ int vpn_daemon_down(int tun_index) return VPNSVC_ERROR_NONE; } -int vpn_daemon_block_networks(const struct vpnsvc_route* nets_vpn, size_t nr_nets_vpn, - const struct vpnsvc_route* nets_orig, size_t nr_nets_orig) { +int vpn_daemon_block_networks(const char* nets_vpn[], int prefix_vpn[], size_t nr_nets_vpn, + const char* nets_orig[], int prefix_orig[], size_t nr_nets_orig) { unsigned int i; /* iptable chain regist */ iptables_register(); for (i = 0; i < nr_nets_vpn; i++) { - LOGD("block[%d] ip/mask : %s/%d", i, nets_vpn[i].dest, nets_vpn[i].prefix); - iptables_add(nets_vpn[i].dest, nets_vpn[i].prefix); + LOGD("block[%d] ip/mask : %s/%d", i, nets_vpn[i], prefix_vpn[i]); + iptables_add(nets_vpn[i], prefix_vpn[i]); } for (i = 0; i < nr_nets_orig; i++) { - LOGD("allow[%d] ip/mask : %s/%d", i, nets_orig[i].dest, nets_orig[i].prefix); - iptables_add_orig(nets_orig[i].dest, nets_orig[i].prefix); + LOGD("allow[%d] ip/mask : %s/%d", i, nets_orig[i], prefix_orig[i]); + iptables_add_orig(nets_orig[i], prefix_orig[i]); } return VPNSVC_ERROR_NONE; diff --git a/daemon/src/vpnsvc.c b/daemon/src/vpnsvc.c index d2a55fa..ce858da 100755 --- a/daemon/src/vpnsvc.c +++ b/daemon/src/vpnsvc.c @@ -132,7 +132,8 @@ gboolean handle_vpn_up(Vpnsvc *object, LOGD("handle_vpn_up"); - struct vpnsvc_route* routes = NULL; + char* routes[arg_nr_routes]; + int prefix[arg_nr_routes]; char **dns_servers = NULL; unsigned int i = 0; @@ -157,20 +158,17 @@ gboolean handle_vpn_up(Vpnsvc *object, if (arg_nr_routes > 0) { if (arg_routes != NULL) { GVariant *dict = g_variant_get_variant(arg_routes); - routes = (struct vpnsvc_route*)malloc(sizeof(struct vpnsvc_route)*arg_nr_routes); - if (routes == NULL) { - LOGE("malloc failed."); - result = VPNSVC_ERROR_OUT_OF_MEMORY; - goto done; - } g_variant_iter_init(&iter, dict); i = 0; while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { int temp_dest_str_len = strlen(route_dest); - strncpy(routes[i].dest, route_dest, temp_dest_str_len); - routes[i].dest[temp_dest_str_len] = '\0'; - routes[i].prefix = route_prefix; - LOGD("routes[%d] : %s/%d", i, (routes[i].dest == NULL) ? "" : routes[i].dest, routes[i].prefix); + routes[i] = malloc((sizeof(char) * temp_dest_str_len)+1); + memset(routes[i], 0, sizeof(char) * temp_dest_str_len); + strncpy(routes[i], route_dest, temp_dest_str_len); + routes[i][temp_dest_str_len] = '\0'; + prefix[i] = route_prefix; + LOGD("routes[%d] = %s \t", i, (routes[i] == NULL) ? "" : routes[i]); + LOGD("prefix[%d] = %d ", i, prefix[i]); i++; } } @@ -202,13 +200,10 @@ gboolean handle_vpn_up(Vpnsvc *object, } result = vpn_daemon_up(arg_tun_index, arg_local_ip, arg_remote_ip, - routes, arg_nr_routes, dns_servers, arg_nr_dns, + routes, prefix, arg_nr_routes, dns_servers, arg_nr_dns, total_dns_string_cnt, arg_dns_suffix, arg_mtu); done: /* free pointers */ - if (routes) - free(routes); - if (dns_servers) { for (i = 0; i < arg_nr_dns; i++) { if (dns_servers[i]) @@ -248,8 +243,11 @@ gboolean handle_vpn_block_networks(Vpnsvc *object, LOGD("handle_vpn_block_networks"); int result = VPNSVC_ERROR_NONE; - struct vpnsvc_route* nets_vpn = NULL; - struct vpnsvc_route* nets_orig = NULL; + char *nets_vpn[arg_nr_nets_vpn]; + int prefix_vpn[arg_nr_nets_vpn]; + + char *nets_orig[arg_nr_nets_vpn]; + int prefix_orig[arg_nr_nets_vpn]; int i = 0; GVariantIter iter; @@ -262,20 +260,17 @@ gboolean handle_vpn_block_networks(Vpnsvc *object, if (arg_nr_nets_vpn > 0) { if (arg_nets_vpn != NULL) { GVariant *dict_nets_vpn = g_variant_get_variant(arg_nets_vpn); - nets_vpn = (struct vpnsvc_route*)malloc(sizeof(struct vpnsvc_route)*arg_nr_nets_vpn); - if (nets_vpn == NULL) { - LOGE("malloc failed."); - result = VPNSVC_ERROR_OUT_OF_MEMORY; - goto done; - } g_variant_iter_init(&iter, dict_nets_vpn); i = 0; while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { int tmp_route_len = strlen(route_dest); - strncpy(nets_vpn[i].dest, route_dest, tmp_route_len); - nets_vpn[i].dest[tmp_route_len] = '\0'; - nets_vpn[i].prefix = route_prefix; - LOGD("nets_vpn[%d] : %s/%d", i, (nets_vpn[i].dest == NULL) ? "" : nets_vpn[i].dest, nets_vpn[i].prefix); + nets_vpn[i] = malloc(sizeof(char) * tmp_route_len + 1); + memset(nets_vpn[i], 0, sizeof(char) * tmp_route_len); + strncpy(nets_vpn[i], route_dest, tmp_route_len); + nets_vpn[i][tmp_route_len] = '\0'; + prefix_vpn[i] = route_prefix; + LOGD("nets_vpn[%d] = %s \t", i, (nets_vpn[i] == NULL) ? "" : nets_vpn[i]); + LOGD("prefix_vpn[%d] = %d ", i, prefix_vpn[i]); i++; } } @@ -285,34 +280,24 @@ gboolean handle_vpn_block_networks(Vpnsvc *object, if (arg_nr_nets_orig > 0) { if (arg_nets_orig != NULL) { GVariant *dict_nets_orig = g_variant_get_variant(arg_nets_orig); - nets_orig = (struct vpnsvc_route*)malloc(sizeof(struct vpnsvc_route)*arg_nr_nets_orig); - if (nets_orig == NULL) { - LOGE("malloc failed."); - result = VPNSVC_ERROR_OUT_OF_MEMORY; - goto done; - } g_variant_iter_init(&iter, dict_nets_orig); i = 0; while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { int tmp_route_len = strlen(route_dest); - strncpy(nets_orig[i].dest, route_dest, tmp_route_len); - nets_orig[i].dest[tmp_route_len] = '\0'; - nets_orig[i].prefix = route_prefix; - LOGD("nets_orig[%d] : %s/%d", i, (nets_orig[i].dest == NULL) ? "" : nets_orig[i].dest, nets_orig[i].prefix); + nets_orig[i] = malloc(sizeof(char) * tmp_route_len + 1); + memset(nets_orig[i], 0, sizeof(char) * tmp_route_len); + strncpy(nets_orig[i], route_dest, tmp_route_len); + nets_orig[i][tmp_route_len] = '\0'; + prefix_orig[i] = route_prefix; + LOGD("nets_orig[%d] = %s \t", i, (nets_orig[i] == NULL) ? "" : nets_orig[i]); + LOGD("prefix_orig[%d] = %d ", i, prefix_orig[i]); i++; } } } /* call function */ - result = vpn_daemon_block_networks(nets_vpn, arg_nr_nets_vpn, nets_orig, arg_nr_nets_orig); - -done: - if (nets_vpn) - free(nets_vpn); - - if (nets_orig) - free(nets_orig); + result = vpn_daemon_block_networks(nets_vpn, prefix_vpn, arg_nr_nets_vpn, nets_orig, prefix_orig, arg_nr_nets_orig); vpnsvc_complete_vpn_block_networks(object, invocation, result); diff --git a/framework/src/capi_vpn_service.c b/framework/src/capi_vpn_service.c index 9f59ade..bca72e6 100755 --- a/framework/src/capi_vpn_service.c +++ b/framework/src/capi_vpn_service.c @@ -418,9 +418,9 @@ int vpnsvc_protect(vpnsvc_tun_h handle, int socket_fd, const char* dev_name) } int vpnsvc_up(vpnsvc_tun_h handle, const char* local_ip, const char* remote_ip, - const struct vpnsvc_route* routes, size_t nr_routes, - const char** dns_servers, size_t nr_dns_servers, - const char* dns_suffix) + const char* dest[], int prefix[], size_t nr_routes, + const char** dns_servers, size_t nr_dns_servers, + const char* dns_suffix) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); @@ -460,13 +460,13 @@ int vpnsvc_up(vpnsvc_tun_h handle, const char* local_ip, const char* remote_ip, /* make a route parameter */ g_variant_builder_init(&route_builder, G_VARIANT_TYPE("a{si}")); for (i = 0 ; i < nr_routes ; i++) { - if (strlen(routes[i].dest) <= 0) { - LOGE("invalid routes[%d].dest", i); + if (strlen(dest[i]) <= 0) { + LOGE("invalid dest[%d]", i); return VPNSVC_ERROR_INVALID_PARAMETER; } - g_variant_builder_add(&route_builder, "{si}", routes[i].dest, routes[i].prefix); - LOGD("routes[%d].dest : %s", i, routes[i].dest); - LOGD("routes[%d].prefix : %d", i, routes[i].prefix); + g_variant_builder_add(&route_builder, "{si}", dest[i], prefix[i]); + LOGD("dest[%d] : %s", i, dest[i]); + LOGD("prefix[i] : %d", i, prefix[i]); } route_param = g_variant_builder_end(&route_builder); @@ -621,11 +621,15 @@ int vpnsvc_write(vpnsvc_tun_h handle, const char* data, size_t size) return write(tun_s->fd, data, size); } -API int vpnsvc_block_networks(vpnsvc_tun_h handle, - const struct vpnsvc_route* allow_routes_vpn, - size_t nr_allow_routes_vpn, - const struct vpnsvc_route* allow_routes_orig, - size_t nr_allow_routes_orig) + +int vpnsvc_block_networks(vpnsvc_tun_h handle, + const char* dest_vpn[], + int prefix_vpn[], + size_t nr_allow_routes_vpn, + const char* dest_orig[], + int prefix_orig[], + size_t nr_allow_routes_orig) + { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); @@ -653,18 +657,18 @@ API int vpnsvc_block_networks(vpnsvc_tun_h handle, /* make a route parameter for allowed VPN interface routes */ g_variant_builder_init(&nets_builder, G_VARIANT_TYPE("a{si}")); for (i = 0 ; i < nr_allow_routes_vpn ; i++) { - g_variant_builder_add(&nets_builder, "{si}", allow_routes_vpn[i].dest, allow_routes_vpn[i].prefix); - LOGD("routes[%d].dest : %s", i, allow_routes_vpn[i].dest); - LOGD("routes[%d].prefix : %d", i, allow_routes_vpn[i].prefix); + g_variant_builder_add(&nets_builder, "{si}", dest_vpn[i], prefix_vpn[i]); + LOGD("dest_vpn[%d] : %s", i, dest_vpn[i]); + LOGD("prefix_vpn[%d] : %d", i, prefix_vpn[i]); } nets_param_vpn = g_variant_builder_end(&nets_builder); /* make a route parameter for allowed Original interface Routes */ g_variant_builder_init(&nets_builder, G_VARIANT_TYPE("a{si}")); for (i = 0 ; i < nr_allow_routes_orig ; i++) { - g_variant_builder_add(&nets_builder, "{si}", allow_routes_orig[i].dest, allow_routes_orig[i].prefix); - LOGD("routes[%d].dest : %s", i, allow_routes_orig[i].dest); - LOGD("routes[%d].prefix : %d", i, allow_routes_orig[i].prefix); + g_variant_builder_add(&nets_builder, "{si}", dest_orig[i], prefix_orig[i]); + LOGD("dest_orig[%d] : %s", i, dest_orig[i]); + LOGD("prefix_orig[%d] : %d", i, prefix_orig[i]); } nets_param_orig = g_variant_builder_end(&nets_builder); @@ -736,15 +740,15 @@ int vpnsvc_unblock_networks(vpnsvc_tun_h handle) return result; } -int vpnsvc_get_tun_fd(vpnsvc_tun_h handle) +int vpnsvc_get_tun_fd(vpnsvc_tun_h handle, int* tun_fd) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); vpnsvc_tun_s *tun_s = NULL; /* parameter check */ - if (handle == NULL) { - LOGE("handle is a NULL"); + if (handle == NULL || tun_fd == NULL) { + LOGE("Invalid parameter"); return VPNSVC_ERROR_INVALID_PARAMETER; } tun_s = (vpnsvc_tun_s*)handle; @@ -754,20 +758,23 @@ int vpnsvc_get_tun_fd(vpnsvc_tun_h handle) return VPNSVC_ERROR_INVALID_PARAMETER; } - return tun_s->fd; + *tun_fd = (int)(tun_s->fd); + + return VPNSVC_ERROR_NONE; } -int vpnsvc_get_tun_index(vpnsvc_tun_h handle) +int vpnsvc_get_tun_index(vpnsvc_tun_h handle, int* tun_index) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); vpnsvc_tun_s *tun_s = NULL; /* parameter check */ - if (handle == NULL) { - LOGE("handle is a NULL"); + if (handle == NULL || tun_index == NULL) { + LOGE("Invalid parameter"); return VPNSVC_ERROR_INVALID_PARAMETER; } + tun_s = (vpnsvc_tun_s*)handle; if (tun_s->index <= 0) { @@ -775,14 +782,17 @@ int vpnsvc_get_tun_index(vpnsvc_tun_h handle) return VPNSVC_ERROR_INVALID_PARAMETER; } - return tun_s->index; + *tun_index = (int)(tun_s->index); + + return VPNSVC_ERROR_NONE; } -int vpnsvc_get_tun_name(vpnsvc_tun_h handle, char* tun_name) +int vpnsvc_get_tun_name(vpnsvc_tun_h handle, char** tun_name) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); vpnsvc_tun_s *tun_s = NULL; + char la_tun_name[VPNSVC_TUN_IF_NAME_LEN + 1] = { 0, }; /* parameter check */ if (handle == NULL) { @@ -796,8 +806,13 @@ int vpnsvc_get_tun_name(vpnsvc_tun_h handle, char* tun_name) return VPNSVC_ERROR_INVALID_PARAMETER; } - strncpy(tun_name, tun_s->name, VPNSVC_TUN_IF_NAME_LEN); - tun_name[VPNSVC_TUN_IF_NAME_LEN-1] = '\0'; + if (tun_name == NULL) { + LOGE("tun name string is NULL"); + return VPNSVC_ERROR_INVALID_PARAMETER; + } + + g_strlcpy(la_tun_name, tun_s->name, VPNSVC_TUN_IF_NAME_LEN + 1); + *tun_name = g_strdup(la_tun_name); return VPNSVC_ERROR_NONE; } @@ -886,11 +901,12 @@ int vpnsvc_set_session(vpnsvc_tun_h handle, const char* session) return VPNSVC_ERROR_NONE; } -int vpnsvc_get_session(vpnsvc_tun_h handle, char* session) +int vpnsvc_get_session(vpnsvc_tun_h handle, char** session) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); vpnsvc_tun_s *tun_s = NULL; + char la_session[VPNSVC_SESSION_STRING_LEN + 1] = { 0, }; /* parameter check */ if (handle == NULL) { @@ -904,8 +920,8 @@ int vpnsvc_get_session(vpnsvc_tun_h handle, char* session) return VPNSVC_ERROR_INVALID_PARAMETER; } - strncpy(session, tun_s->session, VPNSVC_SESSION_STRING_LEN); - session[VPNSVC_SESSION_STRING_LEN-1] = '\0'; + g_strlcpy(la_session, tun_s->session, VPNSVC_SESSION_STRING_LEN + 1); + *session = g_strdup(la_session); return VPNSVC_ERROR_NONE; } diff --git a/include/vpn_service.h b/include/vpn_service.h index 0487dcc..5374e51 100755 --- a/include/vpn_service.h +++ b/include/vpn_service.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved +* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,40 +38,46 @@ */ #include -#include #include #ifdef __cplusplus extern "C" { #endif // __cplusplus +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "CAPI_VPNSVC" + #ifndef API #define API __attribute__ ((visibility("default"))) #endif /** - * @brief IPv4 address string length (includes end null character) + * @brief IPv4 address string length (includes end null character). * @since_tizen 3.0 */ #define VPNSVC_IP4_STRING_LEN 16 /** - * @brief TUN interface name length + * @brief TUN interface name length. * @since_tizen 3.0 */ #define VPNSVC_TUN_IF_NAME_LEN 16 /** - * @brief Session name string length (includes end null character) + * @brief Session name string length (includes end null character). * @since_tizen 3.0 */ #define VPNSVC_SESSION_STRING_LEN 32 +#ifndef TIZEN_ERROR_VPNSVC +#define TIZEN_ERROR_VPNSVC -0x03200000 +#endif /** - * @brief Enumeration for VPN service error types + * @brief Enumeration for VPN service error types. * @details Indicate formats of error type field - * @ingroup VPNSVC_FRAMEWORK */ typedef enum { @@ -88,34 +94,21 @@ typedef enum /** - * @brief The structure containing the route information - * @details This structure can be used for both vpnsvc_up() and vpnsvc_block_networks() functions. - * @since_tizen 3.0 - * @see vpnsvc_up() - * @see vpnsvc_block_networks() - */ -struct vpnsvc_route { - char dest[VPNSVC_IP4_STRING_LEN]; /**< Destination address of the route */ - int prefix; /**< The prefix of route */ -}; - -/** - * @brief The VPN tun interface handle - * @details This handle can be obtained by calling vpnsvc_init() and destroyed() by calling vpnsvc_deinit(). + * @brief The VPN tun interface handle. + * @details This handle can be obtained by calling vpnsvc_init() and destroyed by calling vpnsvc_deinit(). * @since_tizen 3.0 * @see vpnsvc_init() * @see vpnsvc_deinit() */ typedef void* vpnsvc_tun_h; - /** - * @brief Initializes TUN interface + * @brief Initializes TUN interface. * @detail You should call vpnsvc_get_tun_name() for checking the actual initialized TUN interface name. (In case of duplicated interface name) * @since_tizen 3.0 * @privlevel public * @privilege %http://tizen.org/privilege/vpnservice - * @remarks The @a handle should be released using vpnsvc_deinit(). + * @remarks The @a handle should be released using vpnsvc_deinit(). * @param[in] tun_name The interface name * @param[out] handle The VPN tun interface handle * @return 0 on success. otherwise, a negative error value. @@ -127,8 +120,8 @@ typedef void* vpnsvc_tun_h; * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported * @post Please call vpnsvc_deinit() if you want to de-initialize VPN tun interface. * @post Please call vpnsvc_get_tun_fd() if you want to know the fd of tun interface. - * @post Please call vpnsvc_get_tun_index() if you want to know the fd of tun interface index(ifr.ifr_ifindex). - * @post Please call vpnsvc_get_tun_name() if you want to know the name of tun interface(ifr.ifr_name). + * @post Please call vpnsvc_get_tun_index() if you want to know the fd of tun interface index. + * @post Please call vpnsvc_get_tun_name() if you want to know the name of tun interface. * @see vpnsvc_deinit() * @see vpnsvc_get_tun_fd() * @see vpnsvc_get_tun_index() @@ -137,7 +130,7 @@ typedef void* vpnsvc_tun_h; API int vpnsvc_init(const char* tun_name, vpnsvc_tun_h *handle); /** - * @brief De-Initializes TUN interface + * @brief De-Initializes TUN interface. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle * @return 0 on success. otherwise, a negative error value. @@ -151,8 +144,8 @@ API int vpnsvc_init(const char* tun_name, vpnsvc_tun_h *handle); API int vpnsvc_deinit(vpnsvc_tun_h handle); /** - * @brief Prevents the underlying VPN traffic to be routed to the VPN itself - * @details The specific socket will be bound to the network interface using by this function. + * @brief Protect a socket from VPN connections. + * @details After protecting, data sent through this socket will go directly to the underlying network. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle * @param[in] socket_fd The opened socket file descriptor @@ -167,15 +160,16 @@ API int vpnsvc_deinit(vpnsvc_tun_h handle); API int vpnsvc_protect(vpnsvc_tun_h handle, int socket_fd, const char* dev_name); /** - * @brief Sets-up TUN interface and brings it up. Installs specified routes/DNS servers/DNS suffix + * @brief Sets-up TUN interface and brings it up. Installs specified routes/DNS servers/DNS suffix. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle * @param[in] local_ip The local IP address * @param[in] remote_ip The remote IP address - * @param[in] routes The list of routes for applying to routing table (see vpnsvc_route struct) - Optional - * @param[in] nr_routes The number of routes - Optional + * @param[in] dest Destination address of the route + * @param[in] prefix The prefix of route + * @param[in] nr_routes The number of routes * @param[in] dns_servers The list of DNS server names - Optional - * @param[in] nr_dns_servers The number of DNS server names - Optional + * @param[in] nr_dns_servers The number of DNS server names - Optionl * @param[in] dns_suffix The DNS suffix - Optional * @return 0 on success. otherwise, a negative error value. * @retval #VPNSVC_ERROR_NONE Success @@ -184,17 +178,16 @@ API int vpnsvc_protect(vpnsvc_tun_h handle, int socket_fd, const char* dev_name) * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported * @pre The VPN tun interface should be initialized already. * @post If you want to set interface down, please call vpnsvc_down(). - * @see #vpnsvc_route * @see vpnsvc_init() * @see vpnsvc_down() */ API int vpnsvc_up(vpnsvc_tun_h handle, const char* local_ip, const char* remote_ip, - const struct vpnsvc_route* routes, size_t nr_routes, + const char *dest[], int prefix[], size_t nr_routes, const char** dns_servers, size_t nr_dns_servers, const char* dns_suffix); /** - * @brief Brings the TUN interface down and restores original DNS servers/domains + * @brief Brings the TUN interface down and restores original DNS servers/domains. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle * @return 0 on success. otherwise, a negative error value. @@ -210,7 +203,7 @@ API int vpnsvc_up(vpnsvc_tun_h handle, const char* local_ip, const char* remote_ API int vpnsvc_down(vpnsvc_tun_h handle); /** - * @brief Waits for the read event on TUN descriptor, but no more than the indicated timeout in milliseconds + * @brief Reads the data event on TUN descriptor. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle * @param[in] timeout_ms The value of timeout (milliseconds) @@ -227,7 +220,7 @@ API int vpnsvc_down(vpnsvc_tun_h handle); API int vpnsvc_read(vpnsvc_tun_h handle, int timeout_ms); /** - * @brief Writes the data supplied into the TUN interface + * @brief Writes the data supplied into the TUN interface. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle * @param[in] data Data writing to tun interface @@ -244,52 +237,60 @@ API int vpnsvc_read(vpnsvc_tun_h handle, int timeout_ms); API int vpnsvc_write(vpnsvc_tun_h handle, const char* data, size_t size); /** - * @brief Blocks all traffics except specified allowing networks + * @brief Blocks all traffics except specified allowing networks. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle - * @param[in] allow_routes_vpn The list of allowing networks over VPN interface (Please see vpnsvc_route structure). + * @param[in] dest_vpn Allowing networks over VPN interface. + * @param[in] prefix_vpn The prefix of VPN interface * @param[in] nr_allow_routes_vpn The number of allowing networks over VPN interface - * @param[in] allow_routes_orig The list of allowing networks over the original interface (Please see vpnsvc_route structure). + * @param[in] dest_orig Allowing networks over the original interface. + * @param[in] prefix_orig The prefix of Original interface. * @param[in] nr_allow_routes_orig The number of allowing networks over the original interface * @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_NOT_SUPPORTED Not Supported * @post Please call vpnsvc_unblock_networks() if you want to allow all traffics. * @see vpnsvc_unblock_networks() */ API int vpnsvc_block_networks(vpnsvc_tun_h handle, - const struct vpnsvc_route* allow_routes_vpn, + const char *dest_vpn[], + int prefix_vpn[], size_t nr_allow_routes_vpn, - const struct vpnsvc_route* allow_routes_orig, + const char *dest_orig[], + int prefix_orig[], size_t nr_allow_routes_orig); /** - * @brief Removes any restrictions imposed by vpnsvc_block_networks() + * @brief Removes any restrictions imposed by vpnsvc_block_networks(). * @since_tizen 3.0 * @param[in] handle The VPN tun 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_NOT_SUPPORTED Not Supported */ API int vpnsvc_unblock_networks(vpnsvc_tun_h handle); /** - * @brief Gets the fd of the VPN tun interface + * @brief Gets the fd of the VPN tun interface. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle + * @param[out] tun_fd The tun fd * @return The fd value of VPN tun interface. Otherwise, a negative error value. * @retval #VPNSVC_ERROR_NONE Success * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported */ -API int vpnsvc_get_tun_fd(vpnsvc_tun_h handle); +API int vpnsvc_get_tun_fd(vpnsvc_tun_h handle, int* tun_fd); /** - * @brief Gets the index of VPN tun interface + * @brief Gets the index of VPN tun interface. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle + * @param[out] tun_index The tun index * @return The index of the VPN tun interface. otherwise, a negative error value. * @retval #VPNSVC_ERROR_NONE Success * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter @@ -297,10 +298,10 @@ API int vpnsvc_get_tun_fd(vpnsvc_tun_h handle); * @pre Before calling this function, VPN tun interface should be initialized already. * @see vpnsvc_init() */ -API int vpnsvc_get_tun_index(vpnsvc_tun_h handle); +API int vpnsvc_get_tun_index(vpnsvc_tun_h handle, int* tun_index); /** - * @brief Gets the name of VPN tun interface + * @brief Gets the name of VPN tun interface. * @since_tizen 3.0 * @remarks The @a tun_name should be released using free() * @param[in] handle The VPN tun interface handle @@ -312,10 +313,10 @@ API int vpnsvc_get_tun_index(vpnsvc_tun_h handle); * @pre Before calling this function, VPN tun interface should be initialized already. * @see vpnsvc_init() */ -API int vpnsvc_get_tun_name(vpnsvc_tun_h handle, char* tun_name); +API int vpnsvc_get_tun_name(vpnsvc_tun_h handle, char** tun_name); /** - * @brief Sets the MTU of the VPN tun interface + * @brief Sets the MTU of the VPN tun interface. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle * @param[in] mtu The MTU (Maximum Transmission Unit) value to be set for VPN tun interface. Default MTU size is 1500. @@ -329,7 +330,7 @@ API int vpnsvc_get_tun_name(vpnsvc_tun_h handle, char* tun_name); API int vpnsvc_set_mtu(vpnsvc_tun_h handle, int mtu); /** - * @brief Sets blocking mode of the file descriptor of VPN tun interface + * @brief Sets blocking mode of the file descriptor of VPN tun interface. * @since_tizen 3.0 * @param[in] handle The VPN tun interface handle * @param[in] blocking The blocking mode flag; True = BLOCKING, False = NON_BLOCKING @@ -344,9 +345,8 @@ API int vpnsvc_set_mtu(vpnsvc_tun_h handle, int mtu); API int vpnsvc_set_blocking(vpnsvc_tun_h handle, bool blocking); /** - * @brief Sets the session name for the VPN + * @brief Sets the session name for the VPN. * @since_tizen 3.0 - * @remarks a tun_name should be released using free() * @param[in] handle The VPN tun interface handle * @param[in] session The Session Name * @return 0 on success. Otherwise, a negative error value. @@ -356,11 +356,12 @@ API int vpnsvc_set_blocking(vpnsvc_tun_h handle, bool blocking); * @pre Before calling this function, VPN tun interface should be initialized already. * @see vpnsvc_init() */ -API int vpnsvc_set_session(vpnsvc_tun_h handle, const char* session_name); +API int vpnsvc_set_session(vpnsvc_tun_h handle, const char* session); /** - * @brief Gets the session name for the VPN + * @brief Gets the session name for the VPN. * @since_tizen 3.0 + * @remarks The @a session should be released using free() * @param[in] handle The VPN tun interface handle * @param[out] session The Session Name returned * @return 0 on success. Otherwise, a negative error value. @@ -370,7 +371,7 @@ API int vpnsvc_set_session(vpnsvc_tun_h handle, const char* session_name); * @pre Before calling this function, VPN tun interface should be initialized already. * @see vpnsvc_init() */ -API int vpnsvc_get_session(vpnsvc_tun_h handle, char* session_name); +API int vpnsvc_get_session(vpnsvc_tun_h handle, char** session); #ifdef __cplusplus } diff --git a/test/vpn_service_test.c b/test/vpn_service_test.c index bf768ac..10354cb 100755 --- a/test/vpn_service_test.c +++ b/test/vpn_service_test.c @@ -44,6 +44,7 @@ int test_vpnsvc_init() { char *name = TEST_VPN_IF_NAME; int ret = VPNSVC_ERROR_NONE; + int int_value; printf("test vpnsvc_init\n"); @@ -52,12 +53,20 @@ int test_vpnsvc_init() if (ret != VPNSVC_ERROR_NONE) { printf("vpnsvc_init failed : %d\n", ret); } else { - char result_name[VPNSVC_TUN_IF_NAME_LEN] = {0, }; + char* result_name = NULL; printf("vpnsvc_init Succeed : %d\n", ret); - printf("tun_fd : %d\n", vpnsvc_get_tun_fd(handle)); - printf("tun_index : %d\n", vpnsvc_get_tun_index(handle)); - ret = vpnsvc_get_tun_name(handle, result_name); + if (vpnsvc_get_tun_fd(handle, &int_value) == VPNSVC_ERROR_NONE) + printf("tun_fd : %d\n", int_value); + else + printf("Fail to get tun_fd\n"); + + if (vpnsvc_get_tun_index(handle, &int_value) == VPNSVC_ERROR_NONE) + printf("tun_index : %d\n", int_value); + else + printf("Fail to get tun_index\n"); + + ret = vpnsvc_get_tun_name(handle, &result_name); if (ret == VPNSVC_ERROR_NONE) printf("tun_name : %s\n", result_name); } @@ -110,7 +119,8 @@ int test_vpnsvc_up() int ret; char local[VPNSVC_IP4_STRING_LEN] = {'\0',}; char remote[VPNSVC_IP4_STRING_LEN] = {'\0',}; - struct vpnsvc_route routes[2]; + char *routes[2]; + int prefix[2]; int nr_routes = 2; const char *dns_server[2]; int nr_dns = 2; @@ -124,11 +134,17 @@ int test_vpnsvc_up() strncpy(local, "192.168.0.82", VPNSVC_IP4_STRING_LEN); strncpy(remote, "192.168.0.1", VPNSVC_IP4_STRING_LEN); - memset(routes, 0, sizeof(routes)); - strncpy(routes[0].dest, "192.168.0.10", VPNSVC_IP4_STRING_LEN); - routes[0].prefix = 32; - strncpy(routes[1].dest, "192.168.0.11", VPNSVC_IP4_STRING_LEN); - routes[1].prefix = 32; + routes[0] = malloc(sizeof(char) * VPNSVC_IP4_STRING_LEN); + routes[1] = malloc(sizeof(char) * VPNSVC_IP4_STRING_LEN); + + memset(routes[0], 0, sizeof(char) * VPNSVC_IP4_STRING_LEN); + memset(routes[1], 0, sizeof(char) * VPNSVC_IP4_STRING_LEN); + + strncpy(routes[0], "192.168.0.10", VPNSVC_IP4_STRING_LEN); + prefix[0] = 32; + + strncpy(routes[1], "192.168.0.11", VPNSVC_IP4_STRING_LEN); + prefix[1] = 32; char *dns1 = "1.1.1.1"; char *dns2 = "2.2.2.2"; @@ -136,7 +152,7 @@ int test_vpnsvc_up() dns_server[0] = dns1; dns_server[1] = dns2; - ret = vpnsvc_up(handle, local, remote, routes, nr_routes, dns_server, nr_dns, dns_suffix); + ret = vpnsvc_up(handle, local, remote, routes, prefix, nr_routes, dns_server, nr_dns, dns_suffix); if (ret != VPNSVC_ERROR_NONE) printf("vpnsvc_up failed!\n"); else @@ -177,9 +193,11 @@ int test_vpnsvc_write() int test_vpnsvc_block_networks() { - struct vpnsvc_route block_nets[2]; + char* block_nets[2]; + int block_prefix[2]; int block_nr_nets = 2; - struct vpnsvc_route allow_nets[2]; + char* allow_nets[2]; + int allow_prefix[2]; int allow_nr_nets = 2; int ret; @@ -188,19 +206,25 @@ int test_vpnsvc_block_networks() return -1; } - memset(block_nets, 0, sizeof(block_nets)); - strncpy(block_nets[0].dest, "125.209.222.141", VPNSVC_IP4_STRING_LEN); - block_nets[0].prefix = 32; - strncpy(block_nets[1].dest, "180.70.134.19", VPNSVC_IP4_STRING_LEN); - block_nets[1].prefix = 32; - - memset(allow_nets, 0, sizeof(allow_nets)); - strncpy(allow_nets[0].dest, "216.58.221.142", VPNSVC_IP4_STRING_LEN); /* google.com */ - allow_nets[0].prefix = 32; - strncpy(allow_nets[1].dest, "206.190.36.45", VPNSVC_IP4_STRING_LEN); /* yahoo.com */ - allow_nets[1].prefix = 32; - - ret = vpnsvc_block_networks(handle, block_nets, block_nr_nets, allow_nets, allow_nr_nets); + block_nets[0] = malloc(sizeof(char) * VPNSVC_IP4_STRING_LEN); + block_nets[1] = malloc(sizeof(char) * VPNSVC_IP4_STRING_LEN); + memset(block_nets[0], 0, sizeof(char) * VPNSVC_IP4_STRING_LEN); + memset(block_nets[1], 0, sizeof(char) * VPNSVC_IP4_STRING_LEN); + strncpy(block_nets[0], "125.209.222.141", VPNSVC_IP4_STRING_LEN); + block_prefix[0] = 32; + strncpy(block_nets[1], "180.70.134.19", VPNSVC_IP4_STRING_LEN); + block_prefix[1] = 32; + + allow_nets[0] = malloc(sizeof(char) * VPNSVC_IP4_STRING_LEN); + allow_nets[1] = malloc(sizeof(char) * VPNSVC_IP4_STRING_LEN); + memset(allow_nets[0], 0, sizeof(char) * VPNSVC_IP4_STRING_LEN); + memset(allow_nets[1], 0, sizeof(char) * VPNSVC_IP4_STRING_LEN); + strncpy(allow_nets[0], "216.58.221.142", VPNSVC_IP4_STRING_LEN); + allow_prefix[0] = 32; + strncpy(allow_nets[1], "206.190.36.45", VPNSVC_IP4_STRING_LEN); + allow_prefix[1] = 32; + + ret = vpnsvc_block_networks(handle, block_nets, block_prefix, block_nr_nets, allow_nets, allow_prefix, allow_nr_nets); if (ret != VPNSVC_ERROR_NONE) printf("vpnsvc_block_networks failed!\n"); @@ -266,14 +290,14 @@ int test_vpnsvc_set_session() { int ret; char *set_session = "vpnsvc_test VPN Session"; - char get_session[VPNSVC_SESSION_STRING_LEN]; + char *get_session = NULL; ret = vpnsvc_set_session(handle, set_session); if (ret != VPNSVC_ERROR_NONE) { printf("vpnsvc_set_session failed!\n"); } else { - ret = vpnsvc_get_session(handle, get_session); + ret = vpnsvc_get_session(handle, &get_session); printf("Session Name = %s\n", get_session); printf("vpnsvc_set_session Succeed!\n"); } -- 2.7.4