DP: Add set/get remote port, get local ip 71/258771/5 submit/tizen/20210531.043146
authorSeonah Moon <seonah1.moon@samsung.com>
Tue, 25 May 2021 10:50:19 +0000 (19:50 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Mon, 31 May 2021 04:29:19 +0000 (13:29 +0900)
- Fix vine_dp_[set/get]_port
- Add new API vine_dp_[set/get]_remote_port
- Add new API vine_dp_get_ip

Change-Id: Ib1e1e4a5cfd72fcf3a33392921a5366225fc0275

13 files changed:
include/vine.h
packaging/capi-network-vine.spec
plugins/libwebsockets/libwebsockets-plugin.cpp
src/include/vine-data-path-plugin.h
src/include/vine-data-path.h
src/include/vine-dp.h
src/vine-data-path.cpp
src/vine-dp.cpp
src/vine.cpp
tests/unittest/vine-unittest-dp.cpp
tests/verifier/vine-verifier.cpp
tests/vine-test/vine-test.cpp
tool/tool_run.cpp

index e8e87dd..2fbca0a 100755 (executable)
@@ -879,16 +879,66 @@ int vine_dp_set_remote_ip(vine_dp_h dp, vine_address_family_e addr_family, const
 int vine_dp_get_remote_ip(vine_dp_h dp, vine_address_family_e *addr_family, char **ip);
 
 /**
- * @brief Sets the port.
- * @remarks @a port is a remote server port if @a dp's type is VINE_DP_TYPE_CLIENT. \
- *          Otherwise, @a port is a listen port. \
- *          If it is a listen port, you can set it to 0 so that a random port is picked by the kernel.
+ * @brief Sets the port of a remote server.
+ * @remarks @a dp's type can be VINE_DP_TYPE_CLIENT only.
  * @since_tizen 6.5
  * @param[in] dp The data path handle
  * @param[in] port The port
  * @return 0 on success, otherwise a negative error value
  * @retval #VINE_ERROR_NONE    Successful
  * @retval #VINE_ERROR_INVALID_PARAMETER       Invalid parameter
+ * @retval #VINE_ERROR_INVALID_OPERATION       Invalid operation
+ * @retval #VINE_ERROR_NOT_SUPPORTED Not supported
+ * @see vine_dp_create()
+ * @see vine_dp_get_remote_port()
+ */
+int vine_dp_set_remote_port(vine_dp_h dp, int port);
+
+/**
+ * @brief Gets the port of a remote server.
+ * @remarks @a dp's type can be VINE_DP_TYPE_CLIENT only.
+ * @since_tizen 6.5
+ * @param[in] dp The data path handle
+ * @param[out] port The port
+ * @return 0 on success, otherwise a negative error value
+ * @retval #VINE_ERROR_NONE    Successful
+ * @retval #VINE_ERROR_INVALID_PARAMETER       Invalid parameter
+ * @retval #VINE_ERROR_INVALID_OPERATION       Invalid operation
+ * @retval #VINE_ERROR_NOT_SUPPORTED Not supported
+ * @see vine_dp_create()
+ * @see vine_dp_set_remote_port()
+ */
+int vine_dp_get_remote_port(vine_dp_h dp, int *port);
+
+/**
+ * @brief Gets the local IP address.
+ * @remarks @a ip must be released using free().
+ *          If VINE_DP_TYPE_SERVER are listening to multiple interfaces,
+ *          @a ip would be returned empty without error.
+ *          For the same reason, VINE_DP_TYPE_PUBSUB doesn't support this API.
+ * @since_tizen 6.5
+ * @param[in] dp The data path handle
+ * @param[out] addr_family The address family
+ * @param[out] ip The local IP address
+ * @return 0 on success, otherwise a negative error value
+ * @retval #VINE_ERROR_NONE    Successful
+ * @retval #VINE_ERROR_INVALID_PARAMETER       Invalid parameter
+ * @retval #VINE_ERROR_NOT_SUPPORTED Not supported
+ * @see vine_dp_create()
+ */
+int vine_dp_get_ip(vine_dp_h dp, vine_address_family_e *addr_family, char **ip);
+
+/**
+ * @brief Sets the listen port.
+ * @remarks @a dp's type can be VINE_DP_TYPE_SERVER or VINE_DP_TYPE_PUBSUB. \
+ *          You can set it to 0 so that a random port is picked by the kernel.
+ * @since_tizen 6.5
+ * @param[in] dp The data path handle
+ * @param[in] port The listen port
+ * @return 0 on success, otherwise a negative error value
+ * @retval #VINE_ERROR_NONE    Successful
+ * @retval #VINE_ERROR_INVALID_PARAMETER       Invalid parameter
+ * @retval #VINE_ERROR_INVALID_OPERATION       Invalid operation
  * @retval #VINE_ERROR_NOT_SUPPORTED Not supported
  * @see vine_dp_create()
  */
@@ -896,6 +946,9 @@ int vine_dp_set_port(vine_dp_h dp, int port);
 
 /**
  * @brief Gets the port.
+ * @remarks @a port is the local port number used for the connection. \
+ *          If dp is VINE_DP_TYPE_SERVER or VINE_DP_TYPE_PUBSUB, \
+ *          @a port is the same with a listen port.
  * @since_tizen 6.5
  * @param[in] dp The data path handle
  * @param[out] port The port
@@ -904,6 +957,7 @@ int vine_dp_set_port(vine_dp_h dp, int port);
  * @retval #VINE_ERROR_INVALID_PARAMETER       Invalid parameter
  * @retval #VINE_ERROR_NOT_SUPPORTED Not supported
  * @see vine_dp_create()
+ * @see vine_dp_set_port()
  */
 int vine_dp_get_port(vine_dp_h dp, int *port);
 
index 3336ebe..5260543 100755 (executable)
@@ -2,7 +2,7 @@
 %bcond_without lws_static_prebuilt
 Name:    capi-network-vine
 Summary: An service discovery framework
-Version: 1.0.1
+Version: 1.0.2
 Release: 0
 Group:   Network & Connectivity/API
 License: Apache-2.0
index fed2d1b..a16c942 100755 (executable)
@@ -22,6 +22,7 @@
 #include <pthread.h>
 #include <stdbool.h>
 #include <sys/eventfd.h>
+#include <sys/socket.h>
 #include <unistd.h>
 #include <arpa/inet.h>
 
@@ -131,33 +132,19 @@ static bool __is_ipv4_mapped(const char *ip)
        return strncmp(IPV4_MAPPED_IPV6_PREFIX, ip, IPV4_MAPPED_IPv6_PREFIX_LEN) == 0;
 }
 
-static void _get_peer_network_info(struct lws *wsi,
-       vine_dp_addr_family_e *family, char ip[], int *port)
+static void __get_network_info(struct sockaddr_storage *addr,
+               vine_dp_addr_family_e *family, char ip[], int *port)
 {
-       struct sockaddr_storage addr;
-       socklen_t len = sizeof(addr);
-       int fd;
-       int ret;
-
-       fd = lws_get_socket_fd(wsi);
-       if (fd < 0) {
-               VINE_LOGE("lws_get_socket_fd fails[%d]", fd);
-               return;
-       }
-
-       ret = getpeername(fd, (struct sockaddr*)&addr, &len);
-       if (ret < 0) {
-               VINE_LOGE("Cannot get name of connected peer. errno[%d]", errno);
+       if (!addr)
                return;
-       }
 
-       if (addr.ss_family == AF_INET) {
-               struct sockaddr_in *s = (struct sockaddr_in*)&addr;
+       if (addr->ss_family == AF_INET) {
+               struct sockaddr_in *s = (struct sockaddr_in*)addr;
                *port = ntohs(s->sin_port);
                *family = VINE_DP_IPV4;
                inet_ntop(AF_INET, &s->sin_addr, ip, INET6_ADDRSTRLEN);
        } else {
-               struct sockaddr_in6 *s = (struct sockaddr_in6*)&addr;
+               struct sockaddr_in6 *s = (struct sockaddr_in6*)addr;
                *port = ntohs(s->sin6_port);
                inet_ntop(AF_INET6, &s->sin6_addr, ip, INET6_ADDRSTRLEN);
                if (__is_ipv4_mapped(ip)) {
@@ -173,7 +160,53 @@ static void _get_peer_network_info(struct lws *wsi,
                        *family = VINE_DP_IPV6;
                }
        }
+}
+
+static int _get_local_network_info(struct lws *wsi,
+               vine_dp_addr_family_e *family, char ip[], int *port)
+{
+       struct sockaddr_storage addr;
+       socklen_t len = sizeof(struct sockaddr_storage);
+       int fd, ret;
+
+       fd = lws_get_socket_fd(wsi);
+       if (fd < 0) {
+               VINE_LOGE("lws_get_socket_fd fails[%d]", fd);
+               return VINE_DATA_PATH_ERROR_OPERATION_FAILED;
+       }
+
+       ret = getsockname(fd, (struct sockaddr *)&addr, &len);
+       if (ret < 0) {
+               VINE_LOGE("Cannot get sockname. errno[%d]", errno);
+               return VINE_DATA_PATH_ERROR_OPERATION_FAILED;
+       }
+
+       __get_network_info(&addr, family, ip, port);
+       VINE_LOGD("address family[%d] ip[%s], port[%d]\n", addr.ss_family, ip, *port);
+       return VINE_DATA_PATH_ERROR_NONE;
+}
+
+static void _get_peer_network_info(struct lws *wsi,
+       vine_dp_addr_family_e *family, char ip[], int *port)
+{
+       struct sockaddr_storage addr;
+       socklen_t len = sizeof(addr);
+       int fd;
+       int ret;
+
+       fd = lws_get_socket_fd(wsi);
+       if (fd < 0) {
+               VINE_LOGE("lws_get_socket_fd fails[%d]", fd);
+               return;
+       }
+
+       ret = getpeername(fd, (struct sockaddr*)&addr, &len);
+       if (ret < 0) {
+               VINE_LOGE("Cannot get name of connected peer. errno[%d]", errno);
+               return;
+       }
 
+       __get_network_info(&addr, family, ip, port);
        VINE_LOGD("address family[%d] peer ip[%s], peer port[%d]\n", addr.ss_family, ip, *port);
 }
 
@@ -332,6 +365,16 @@ static void _change_websocket_poll_fd(struct lws_pollargs *args)
                g_callbacks.pollfd_cb(VINE_DATA_PATH_POLLFD_MOD, pollfd->fd, pollfd->events);
 }
 
+static void _invoke_connected_cb(struct lws *wsi, bool connected, void *user_data)
+{
+       if (!connected) {
+               g_callbacks.connected_cb(-1, user_data);
+               return;
+       }
+
+       g_callbacks.connected_cb(0, user_data);
+}
+
 static int _websocket_protocol_cb(struct lws *wsi,
                enum lws_callback_reasons reason, void *user, void *in, size_t len)
 {
@@ -422,7 +465,6 @@ static int _websocket_protocol_cb(struct lws *wsi,
 
        case LWS_CALLBACK_ESTABLISHED: {
                VINE_LOGI("Websocket connection is established.");
-
                websocket_s *client_ws = (websocket_s *)lws_wsi_user(wsi);
                vhd->curr_conn++;
                if (g_callbacks.accepted_cb) {
@@ -484,7 +526,7 @@ static int _websocket_protocol_cb(struct lws *wsi,
        case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
                VINE_LOGI("Failed connection request to server.");
                if (ws && g_callbacks.connected_cb)
-                       g_callbacks.connected_cb(-1, ws->user);
+                       _invoke_connected_cb(wsi, false, ws->user);
                break;
 
        case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: {
@@ -505,7 +547,7 @@ static int _websocket_protocol_cb(struct lws *wsi,
        case LWS_CALLBACK_CLIENT_ESTABLISHED:
                VINE_LOGI("Connected with server.");
                if (ws && g_callbacks.connected_cb)
-                       g_callbacks.connected_cb(0, ws->user);
+                       _invoke_connected_cb(wsi, true, ws->user);
                break;
 
        case LWS_CALLBACK_CLIENT_RECEIVE:
@@ -974,6 +1016,19 @@ static void websocket_register_callbacks(vine_dp_plugin_callbacks callbacks)
        g_callbacks = callbacks;
 }
 
+static int websocket_get_local_address_info(vine_dp_plugin_h handle,
+               int *address_family, char local_ip[], int *port)
+{
+       RET_VAL_IF(handle == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "handle is NULL");
+       RET_VAL_IF(address_family == NULL,
+                       VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "address_family is NULL");
+       RET_VAL_IF(local_ip == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "local_ip is NULL");
+       RET_VAL_IF(port == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "port is NULL");
+
+       websocket_s *ws = (websocket_s *)handle;
+       return _get_local_network_info(ws->wsi, (vine_dp_addr_family_e *)address_family, local_ip, port);
+}
+
 static int websocket_set_token(vine_dp_plugin_h handle, const char *token)
 {
        RET_VAL_IF(handle == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "handle is NULL");
@@ -1019,6 +1074,8 @@ void vine_data_path_plugin_init(vine_dp_plugin_fn *fn)
        fn->write = websocket_write;
        fn->close = websocket_close;
 
+       fn->get_local_address_info = websocket_get_local_address_info;
+
        fn->set_token = websocket_set_token;
        fn->get_token = websocket_get_token;
 }
index a71bc8b..3f8a4c9 100755 (executable)
@@ -101,6 +101,9 @@ typedef struct {
        int (*write)(vine_dp_plugin_h handle, unsigned char *buf, size_t len);
        int (*close)(vine_dp_plugin_h handle);
 
+       int (*get_local_address_info)(vine_dp_plugin_h handle,
+                       int *addr_family, char local_ip[], int *port);
+
        // The token means an additional information which is used during connection establishing.
        // The way to send it can be different based on a plugin.
        // In case of libwebsockets, send it through a HTTP custom header.
index 0cd2c97..15421ff 100755 (executable)
@@ -52,6 +52,8 @@ const char *vine_data_path_get_ip(vine_data_path_h datapath);
 vine_address_family_e vine_data_path_get_addr_family(vine_data_path_h datapath);
 int vine_data_path_get_port(vine_data_path_h datapath);
 vine_security_h vine_data_path_get_security(vine_data_path_h datapath);
+int vine_data_path_get_local_address_info(vine_data_path_h datapath, int *address_family,
+        char **ip, int *port);
 
 int vine_data_path_set_received_cb(vine_data_path_h datapath,
                vine_data_path_received_cb callback, void *user_data);
index 778534d..f070950 100644 (file)
@@ -50,8 +50,12 @@ public:
        virtual int set_addr_family(vine_address_family_e addr_family) = 0;
        virtual int set_remote_ip(vine_address_family_e, const std::string &ip) = 0;
        virtual int get_remote_ip(vine_address_family_e *addr_family, char **ip) = 0;
+       virtual int set_remote_port(int port) = 0;
+       virtual int get_remote_port() = 0;
+       virtual int get_ip(vine_address_family_e *addr_family, char **ip) = 0;
        virtual int set_port(int port) = 0;
        virtual int get_port() = 0;
+       virtual int update_local_address_info() = 0;
        virtual int set_topic(std::string topic) = 0;
        virtual int set_max_connections(int max_conn) = 0;
        virtual int set_accepted_cb(vine_dp_accepted_cb callback, void *user_data) = 0;
@@ -103,8 +107,12 @@ public:
        virtual int set_addr_family(vine_address_family_e addr_family);
        virtual int set_remote_ip(vine_address_family_e, const std::string &ip);
        virtual int get_remote_ip(vine_address_family_e *addr_family, char **ip);
+       virtual int set_remote_port(int port);
+       virtual int get_remote_port();
+       virtual int get_ip(vine_address_family_e *addr_family, char **ip);
        virtual int set_port(int port);
        virtual int get_port(){ return mListenPort; };
+       virtual int update_local_address_info();
        virtual int set_topic(std::string topic);
        virtual int set_max_connections(int max_conn);
        virtual int set_accepted_cb(vine_dp_accepted_cb callback, void *user_data);
@@ -121,6 +129,7 @@ private:
        vine_data_path_h mDataPath;
        vine_address_family_e mAddrFamily;
        int mListenPort;
+       std::string mLocalIp;
        int mMaxConnNum;
        vine_dp_accepted_cb mAcceptedCb;
        void *mAcceptedCbData;
@@ -141,8 +150,12 @@ public:
        virtual int set_addr_family(vine_address_family_e addr_family);
        virtual int set_remote_ip(vine_address_family_e, const std::string &ip);
        virtual int get_remote_ip(vine_address_family_e *addr_family, char **ip);
+       virtual int set_remote_port(int port);
+       virtual int get_remote_port(){ return mPeerPort; };
+       virtual int get_ip(vine_address_family_e *addr_family, char **ip);
        virtual int set_port(int port);
-       virtual int get_port(){ return mPeerPort; };
+       virtual int get_port(){ return mPort; };
+       virtual int update_local_address_info();
        virtual int set_topic(std::string topic);
        virtual int set_max_connections(int max_conn){ return VINE_ERROR_NONE; };
        virtual int set_accepted_cb(vine_dp_accepted_cb callback, void *user_data);
@@ -157,6 +170,8 @@ private:
        vine_address_family_e mAddrFamily;
        std::string mPeerIp;
        int mPeerPort;
+       std::string mLocalIp;
+       int mPort;
 
        bool isCreatedByServerDp;
 };
@@ -188,9 +203,12 @@ public:
        virtual int set_addr_family(vine_address_family_e addr_family);
        virtual int set_remote_ip(vine_address_family_e, const std::string &ip);
        virtual int get_remote_ip(vine_address_family_e *addr_family, char **ip);
+       virtual int set_remote_port(int port);
+       virtual int get_remote_port();
+       virtual int get_ip(vine_address_family_e *addr_family, char **ip);
        virtual int set_port(int port);
        virtual int get_port(){ return mListenPort; };
-
+       virtual int update_local_address_info();
        virtual int set_topic(std::string topic);
        virtual int set_max_connections(int max_conn);
        virtual int set_accepted_cb(vine_dp_accepted_cb callback, void *user_data);
@@ -231,6 +249,7 @@ private:
        vine_address_family_e mAddrFamily;
        std::set<std::string> mIpList;
        int mListenPort;
+       std::string mLocalIp;
        int mMaxConnNum;
 
        vine_dp_peer_joined_cb mPeerJoinedCb;
@@ -255,6 +274,9 @@ int _vine_dp_set_iface_name(vine_dp_h dp, const char *iface_name);
 int _vine_dp_set_addr_family(vine_dp_h dp, vine_address_family_e addr_family);
 int _vine_dp_set_remote_ip(vine_dp_h dp, vine_address_family_e addr_family, const char *ip);
 int _vine_dp_get_remote_ip(vine_dp_h dp, vine_address_family_e *addr_family, char **ip);
+int _vine_dp_set_remote_port(vine_dp_h dp, int port);
+int _vine_dp_get_remote_port(vine_dp_h dp, int *port);
+int _vine_dp_get_ip(vine_dp_h dp, vine_address_family_e *addr_family, char **ip);
 int _vine_dp_set_port(vine_dp_h dp, int port);
 int _vine_dp_get_port(vine_dp_h dp, int *port);
 int _vine_dp_set_topic(vine_dp_h dp, const char *topic);
index c51137b..d20890f 100755 (executable)
@@ -782,3 +782,29 @@ vine_security_h vine_data_path_get_security(vine_data_path_h datapath)
        vine_data_path_s *dp = (vine_data_path_s *)datapath;
        return dp->security;
 }
+
+int vine_data_path_get_local_address_info(vine_data_path_h datapath, int *address_family,
+               char **ip, int *port)
+{
+       RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
+       RET_VAL_IF(address_family == NULL, VINE_ERROR_INVALID_PARAMETER, "address_family is NULL");
+       RET_VAL_IF(ip == NULL, VINE_ERROR_INVALID_PARAMETER, "ip is NULL");
+       RET_VAL_IF(port == NULL, VINE_ERROR_INVALID_PARAMETER, "port is NULL");
+
+       vine_data_path_s *dp = (vine_data_path_s *)datapath;
+       vine_error_e error;
+       int addr_family, local_port;
+       char local_ip[VINE_MAX_IP_LEN + 1] = {0, };
+       int ret = g_dp_plugin_fn.get_local_address_info(dp->plugin_handle,
+                       &addr_family, local_ip, &local_port);
+
+       error = __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
+       if (error != VINE_ERROR_NONE)
+               return error;
+
+       *address_family = addr_family;
+       *port = local_port;
+       *ip = STRDUP(local_ip);
+
+       return VINE_ERROR_NONE;
+}
index f6f4f68..014bc95 100644 (file)
@@ -90,6 +90,7 @@ static void _opened_cb(vine_data_path_h datapath, int result, int port, void *us
 {
        DataPath *dp = static_cast<DataPath *>(userdata);
        dp->set_port(port);
+       dp->update_local_address_info();
 
        VINE_LOGI("port[%d] result[%d]", port, result);
        dp->invoke_opened_cb((vine_error_e)result);
@@ -109,14 +110,21 @@ static void _accepted_cb(vine_data_path_h datapath, void *user_data)
                        _received_cb, static_cast<void *>(connected_client_dp));
        vine_data_path_set_terminated_cb(datapath,
                        _terminated_cb, static_cast<void *>(connected_client_dp));
+
+       connected_client_dp->update_local_address_info();
        static_cast<DPServer *>(user_data)->invoke_accepted_cb(connected_client_dp);
 }
 
 static void _connected_cb(vine_data_path_h datapath, int result, void *user_data)
 {
-       vine_data_path_set_received_cb(datapath, _received_cb, user_data);
-       vine_data_path_set_terminated_cb(datapath, _terminated_cb, user_data);
-       static_cast<DataPath *>(user_data)->invoke_opened_cb(result);
+       if (result == 0) {
+               vine_data_path_set_received_cb(datapath, _received_cb, user_data);
+               vine_data_path_set_terminated_cb(datapath, _terminated_cb, user_data);
+       }
+
+       DataPath *dp = static_cast<DataPath *>(user_data);
+       dp->update_local_address_info();
+       dp->invoke_opened_cb(result);
 }
 
 static void _pubsub_received_cb(vine_data_path_h datapath, size_t received_len, void *user_data)
@@ -485,6 +493,16 @@ int DPServer::get_remote_ip(vine_address_family_e *addr_family, char **ip)
        return VINE_ERROR_INVALID_OPERATION;
 }
 
+int DPServer::set_remote_port(int port)
+{
+       return VINE_ERROR_INVALID_OPERATION;
+}
+
+int DPServer::get_remote_port()
+{
+       return -1;
+}
+
 int DPServer::set_port(int port)
 {
        if (port < 0 || port > 65535)
@@ -495,6 +513,28 @@ int DPServer::set_port(int port)
        return VINE_ERROR_NONE;
 }
 
+int DPServer::get_ip(vine_address_family_e *addr_family, char **ip)
+{
+       *addr_family = mAddrFamily;
+       *ip = STRDUP(mLocalIp.c_str());
+       return VINE_ERROR_NONE;
+}
+
+int DPServer::update_local_address_info()
+{
+       char *ip;
+       int addr_family;
+       int port;
+       int ret = vine_data_path_get_local_address_info(mDataPath, &addr_family, &ip, &port);
+       if (ret != VINE_ERROR_NONE)
+               return ret;
+
+       mLocalIp = string(ip);
+       free(ip);
+
+       return VINE_ERROR_NONE;
+}
+
 int DPServer::set_topic(std::string topic)
 {
        return VINE_ERROR_INVALID_OPERATION;
@@ -595,6 +635,7 @@ DPClient::DPClient(void *event_fd)
        mDataPath = NULL;
        mPeerIp = "";
        mPeerPort = 0;
+       mPort = 0;
        mReceivedCb = NULL;
        mReceivedCbData = NULL;
        mOpenedCb = NULL;
@@ -615,6 +656,7 @@ DPClient::DPClient(void *event_fd, void *datapath)
        mAddrFamily = vine_data_path_get_addr_family(mDataPath);
        mPeerIp = vine_data_path_get_ip(mDataPath);
        mPeerPort = vine_data_path_get_port(mDataPath);
+       mPort = 0;
        mReceivedCb = NULL;
        mReceivedCbData = NULL;
        mOpenedCb = NULL;
@@ -656,7 +698,7 @@ int DPClient::get_remote_ip(vine_address_family_e *addr_family, char **ip)
        return VINE_ERROR_NONE;
 }
 
-int DPClient::set_port(int port)
+int DPClient::set_remote_port(int port)
 {
        RET_VAL_IF(isCreatedByServerDp, VINE_ERROR_INVALID_OPERATION, "cannot set port");
        if (port <= 0 || port > 65535) // Do not allow 0
@@ -667,6 +709,35 @@ int DPClient::set_port(int port)
        return VINE_ERROR_NONE;
 }
 
+int DPClient::set_port(int port)
+{
+       return VINE_ERROR_INVALID_OPERATION;
+}
+
+int DPClient::get_ip(vine_address_family_e *addr_family, char **ip)
+{
+       *addr_family = mAddrFamily;
+       *ip = STRDUP(mLocalIp.c_str());
+       return VINE_ERROR_NONE;
+}
+
+int DPClient::update_local_address_info()
+{
+       char *ip;
+       int addr_family;
+       int port;
+       int ret = vine_data_path_get_local_address_info(mDataPath, &addr_family, &ip, &port);
+       if (ret != VINE_ERROR_NONE)
+               return ret;
+
+       mLocalIp = string(ip);
+       mPort  = port;
+
+       free(ip);
+
+       return VINE_ERROR_NONE;
+}
+
 int DPClient::set_topic(std::string topic)
 {
        return VINE_ERROR_INVALID_OPERATION;
@@ -791,6 +862,16 @@ int DPPubSub::get_remote_ip(vine_address_family_e *addr_family, char **ip)
        return VINE_ERROR_INVALID_OPERATION;
 }
 
+int DPPubSub::set_remote_port(int port)
+{
+       return VINE_ERROR_INVALID_OPERATION;
+}
+
+int DPPubSub::get_remote_port()
+{
+       return -1;
+}
+
 int DPPubSub::set_port(int port)
 {
        if (port < 0 || port > 65535)
@@ -801,6 +882,26 @@ int DPPubSub::set_port(int port)
        return VINE_ERROR_NONE;
 }
 
+int DPPubSub::get_ip(vine_address_family_e *addr_family, char **ip)
+{
+       return VINE_ERROR_INVALID_OPERATION;
+}
+
+int DPPubSub::update_local_address_info()
+{
+       char *ip;
+       int addr_family;
+       int port;
+       int ret = vine_data_path_get_local_address_info(mServerDataPath, &addr_family, &ip, &port);
+       if (ret != VINE_ERROR_NONE)
+               return ret;
+
+       mLocalIp = string(ip);
+       free(ip);
+
+       return VINE_ERROR_NONE;
+}
+
 int DPPubSub::set_topic(std::string topic)
 {
        mTopic = topic;
@@ -1264,6 +1365,36 @@ int _vine_dp_get_remote_ip(vine_dp_h dp, vine_address_family_e *addr_family, cha
        return _dp->get_remote_ip(addr_family, ip);
 }
 
+int _vine_dp_set_remote_port(vine_dp_h dp, int port)
+{
+       RET_VAL_IF(dp == NULL, VINE_ERROR_INVALID_PARAMETER, "dp is null.");
+
+       DataPath *_dp = static_cast<DataPath *>(dp);
+       return _dp->set_remote_port(port);
+}
+
+int _vine_dp_get_remote_port(vine_dp_h dp, int *port)
+{
+       RET_VAL_IF(dp == NULL, VINE_ERROR_INVALID_PARAMETER, "dp is null.");
+       RET_VAL_IF(port == NULL, VINE_ERROR_INVALID_PARAMETER, "port is null.");
+
+       DataPath *_dp = static_cast<DataPath *>(dp);
+       int remote_port = _dp->get_remote_port();
+       if (remote_port < 0)
+               return VINE_ERROR_INVALID_OPERATION;
+       *port = remote_port;
+       return VINE_ERROR_NONE;
+}
+int _vine_dp_get_ip(vine_dp_h dp, vine_address_family_e *addr_family, char **ip)
+{
+       RET_VAL_IF(dp == NULL, VINE_ERROR_INVALID_PARAMETER, "dp is null.");
+       RET_VAL_IF(addr_family == NULL, VINE_ERROR_INVALID_PARAMETER, "addr_family is null.");
+       RET_VAL_IF(ip == NULL, VINE_ERROR_INVALID_PARAMETER, "ip is null.");
+
+       DataPath *_dp = static_cast<DataPath *>(dp);
+       return _dp->get_ip(addr_family, ip);
+}
+
 int _vine_dp_set_port(vine_dp_h dp, int port)
 {
        RET_VAL_IF(dp == NULL, VINE_ERROR_INVALID_PARAMETER, "dp is null.");
index 8ddfd82..bd16968 100755 (executable)
@@ -357,6 +357,30 @@ API int vine_dp_get_remote_ip(vine_dp_h dp, vine_address_family_e *addr_family,
        return _vine_dp_get_remote_ip(dp, addr_family, ip);
 }
 
+API int vine_dp_set_remote_port(vine_dp_h dp, int port)
+{
+       __VINE_FUNC_ENTER__;
+       CHECK_FEATURE_SUPPORTED;
+
+       return _vine_dp_set_remote_port(dp, port);
+}
+
+API int vine_dp_get_remote_port(vine_dp_h dp, int *port)
+{
+       __VINE_FUNC_ENTER__;
+       CHECK_FEATURE_SUPPORTED;
+
+       return _vine_dp_get_remote_port(dp, port);
+}
+
+API int vine_dp_get_ip(vine_dp_h dp, vine_address_family_e *addr_family, char **ip)
+{
+       __VINE_FUNC_ENTER__;
+       CHECK_FEATURE_SUPPORTED;
+
+       return _vine_dp_get_ip(dp, addr_family, ip);
+}
+
 API int vine_dp_set_port(vine_dp_h dp, int port)
 {
        __VINE_FUNC_ENTER__;
index 10fd0b8..209bbad 100755 (executable)
@@ -122,7 +122,7 @@ TEST_F(VineDpTest, SetRemoteIpN)
                        vine_dp_set_remote_ip(server_dp, VINE_ADDRESS_FAMILY_IPV4, TEST_IP));
 }
 
-TEST_F(VineDpTest, GetRemoteN)
+TEST_F(VineDpTest, GetRemoteIpN)
 {
        vine_address_family_e family;
        char *ip;
@@ -151,18 +151,80 @@ TEST_F(VineDpTest, SetGetRemoteIpP)
        free(ip);
 }
 
-TEST_F(VineDpTest, SetPortN)
+TEST_F(VineDpTest, SetRemotePortN)
 {
        EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER,
-                       vine_dp_set_port(client_dp, -1));
+                       vine_dp_set_remote_port(client_dp, -1));
+       EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER,
+                       vine_dp_set_remote_port(NULL, 12345));
+       EXPECT_EQ(VINE_ERROR_INVALID_OPERATION,
+                       vine_dp_set_remote_port(server_dp, 12345));
+       EXPECT_EQ(VINE_ERROR_INVALID_OPERATION,
+                       vine_dp_set_remote_port(pubsub_dp, 12345));
+}
+
+TEST_F(VineDpTest, SetRemotePortP)
+{
+       EXPECT_EQ(VINE_ERROR_NONE,
+                       vine_dp_set_remote_port(client_dp, 12345));
+}
+
+TEST_F(VineDpTest, GetRemotePortN)
+{
+       int port;
+       EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER,
+                       vine_dp_get_remote_port(client_dp, NULL));
+       EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER,
+                       vine_dp_get_remote_port(NULL, &port));
+       EXPECT_EQ(VINE_ERROR_INVALID_OPERATION,
+                       vine_dp_get_remote_port(server_dp, &port));
+       EXPECT_EQ(VINE_ERROR_INVALID_OPERATION,
+                       vine_dp_get_remote_port(pubsub_dp, &port));
+}
+
+TEST_F(VineDpTest, GetRemotePortP)
+{
+       int port;
+       EXPECT_EQ(VINE_ERROR_NONE,
+                       vine_dp_get_remote_port(client_dp, &port));
+}
+
+TEST_F(VineDpTest, GetIpN)
+{
+       vine_address_family_e family;
+       char *ip;
+       EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER,
+               vine_dp_get_ip(client_dp, NULL, &ip));
+       EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER,
+               vine_dp_get_ip(client_dp, &family, NULL));
+
+       EXPECT_EQ(VINE_ERROR_INVALID_OPERATION,
+               vine_dp_get_ip(pubsub_dp, &family, &ip));
+}
+
+TEST_F(VineDpTest, GetIpP)
+{
+       vine_address_family_e family;
+       char *ip;
+       EXPECT_EQ(VINE_ERROR_NONE,
+                       vine_dp_get_ip(client_dp, &family, &ip));
+       free(ip);
+}
+
+TEST_F(VineDpTest, SetPortN)
+{
        EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER,
                        vine_dp_set_port(NULL, 12345));
+       EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER,
+                       vine_dp_set_port(server_dp, -1));
+       EXPECT_EQ(VINE_ERROR_INVALID_OPERATION,
+                       vine_dp_set_port(client_dp, 12345));
 }
 
 TEST_F(VineDpTest, SetPortP)
 {
        EXPECT_EQ(VINE_ERROR_NONE,
-                       vine_dp_set_port(client_dp, 12345));
+                       vine_dp_set_port(server_dp, 12345));
 }
 
 TEST_F(VineDpTest, GetPortN)
index 6ce82b4..92d2c94 100644 (file)
@@ -320,7 +320,7 @@ static void __connect_to_server(vine_session_h session, bool use_tls, bool use_p
        vine_dp_h dp;
        vine_dp_create(session, VINE_DP_TYPE_CLIENT, &dp);
        vine_dp_set_remote_ip(dp, address_family, ip);
-       vine_dp_set_port(dp, port);
+       vine_dp_set_remote_port(dp, port);
        vine_dp_set_security(dp, security);
 
        vine_dp_set_received_cb(dp, __client_received_cb, NULL);
index 4a692c5..c9aa697 100644 (file)
@@ -606,7 +606,7 @@ static void __connect_server()
        vine_dp_create(g_session, VINE_DP_TYPE_CLIENT, &g_client_dp);
        vine_dp_set_remote_ip(g_client_dp,
                        addr_type ? VINE_ADDRESS_FAMILY_IPV6 : VINE_ADDRESS_FAMILY_IPV4, ip);
-       PRINT_RESULT(vine_dp_set_port(g_client_dp, port), "vine_dp_set_port");
+       PRINT_RESULT(vine_dp_set_remote_port(g_client_dp, port), "vine_dp_set_remote_port");
        vine_security_h security = __create_security_handle(security_type, true);
        vine_dp_set_security(g_client_dp, security);
        vine_security_destroy(security);
index 83c25a5..4bb1635 100644 (file)
@@ -93,6 +93,22 @@ static void __terminated_cb(vine_dp_h dp, void *user_data)
 static void __opened_cb(vine_dp_h dp, vine_error_e result, void *user_data)
 {
        printf("DP %s.\n", result == VINE_ERROR_NONE ? "is opened" : "open failure");
+       if (result != VINE_ERROR_NONE)
+               return;
+
+       vine_address_family_e addr_family;
+       char *ip = NULL;
+       int port = 0;
+
+       vine_dp_get_port(dp, &port);
+       printf("\t> %s port: %d\n",
+               vine_configs.dp_type == VINE_DP_TYPE_CLIENT ? "connected" : "listen",
+               port);
+
+               vine_dp_get_ip(dp, &addr_family, &ip);
+       printf("\t> local IP: %s, address family: %d \n", ip, addr_family);
+       free(ip);
+
        vine_dp_set_received_cb(dp, __received_cb, NULL);
        vine_dp_set_terminated_cb(dp, __terminated_cb, NULL);
 
@@ -104,7 +120,18 @@ static void __opened_cb(vine_dp_h dp, vine_error_e result, void *user_data)
 
 static void __accepted_cb(vine_dp_h dp, vine_dp_h accepted_dp, void *user_data)
 {
+       vine_address_family_e addr_family;
+       int port = 0;
+       char *ip = NULL;
+
+       vine_dp_get_remote_port(accepted_dp, &port);
        printf("client is accepted.\n");
+       printf("\t> client port: %d\n", port);
+
+       vine_dp_get_ip(accepted_dp, &addr_family, &ip);
+       printf("\t> used local IP: %s, address family: %d\n", ip, addr_family);
+       free(ip);
+
        vine_dp_set_received_cb(accepted_dp, __received_cb, NULL);
        vine_dp_set_terminated_cb(accepted_dp, __terminated_cb, NULL);
 
@@ -451,16 +478,17 @@ static void _set_discovery_info()
 static void _set_dp_info(vine_dp_type_e type)
 {
        vine_dp_create(vine_configs.session, type, &vine_configs.dp);
-       vine_dp_set_port(vine_configs.dp, tool_config_get_port());
-
        vine_address_family_e addr_family = _convert_addr_family(tool_config_get_address_family());
        vine_dp_set_address_family(vine_configs.dp, addr_family);
        vine_dp_set_max_connections(vine_configs.dp, tool_config_get_max_conn());
 
        if (type == VINE_DP_TYPE_CLIENT) {
+               vine_dp_set_remote_port(vine_configs.dp, tool_config_get_port());
                vine_dp_set_remote_ip(vine_configs.dp, addr_family,
                                tool_config_get_remote_address());
                vine_dp_set_received_cb(vine_configs.dp, __received_cb, NULL);
+       } else {
+               vine_dp_set_port(vine_configs.dp, tool_config_get_port());
        }
 
        if (type == VINE_DP_TYPE_SERVER)