From 3dc2973161d71060cbea62063d219077f531af8a Mon Sep 17 00:00:00 2001 From: Jordan Augé Date: Tue, 8 Oct 2019 15:11:40 +0200 Subject: [HICN-306] Wrong representation format in ip_prefix_ntop causes parsing issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I16c8059ce537f338c15434acc87ac0e4baf87049 Signed-off-by: Jordan Augé --- hicn-light/src/hicn/socket/api.c | 4 ++-- lib/includes/hicn/util/ip_address.h | 1 + lib/src/util/ip_address.c | 14 +++++++++++++- libtransport/src/hicn/transport/core/prefix.cc | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/hicn-light/src/hicn/socket/api.c b/hicn-light/src/hicn/socket/api.c index eca02ec38..a3d5a3cfe 100644 --- a/hicn-light/src/hicn/socket/api.c +++ b/hicn-light/src/hicn/socket/api.c @@ -268,7 +268,7 @@ int hicn_get_local_address(const ip_prefix_t *remote_address, uint32_t interface_id; char remote_address_str[INET_MAX_ADDRSTRLEN + 4 ]; - rc = ip_prefix_ntop(remote_address, remote_address_str, + rc = ip_prefix_ntop_short(remote_address, remote_address_str, sizeof(remote_address_str)); if (rc < 0) { rc = HICN_SOCKET_ERROR_BIND_REMOTE_REPR; @@ -338,7 +338,7 @@ int hicn_set_remote_endpoint(hicn_socket_t *socket, ///// /* Convert to representation format */ - rc = ip_prefix_ntop(&addr, local_ip_address, sizeof(local_ip_address)); + rc = ip_prefix_ntop_short(&addr, local_ip_address, sizeof(local_ip_address)); if (rc < 0) { rc = HICN_SOCKET_ERROR_BIND_REMOTE_REPR; goto ERR; diff --git a/lib/includes/hicn/util/ip_address.h b/lib/includes/hicn/util/ip_address.h index 728a9da90..542e6e4c6 100644 --- a/lib/includes/hicn/util/ip_address.h +++ b/lib/includes/hicn/util/ip_address.h @@ -135,6 +135,7 @@ int ip_address_empty(const ip_address_t * ip); /* Prefix */ int ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix); +int ip_prefix_ntop_short (const ip_prefix_t * ip_prefix, char *dst, size_t size); int ip_prefix_ntop (const ip_prefix_t * ip_prefix, char *dst, size_t size); int ip_prefix_len (const ip_prefix_t * prefix); bool ip_prefix_empty (const ip_prefix_t * prefix); diff --git a/lib/src/util/ip_address.c b/lib/src/util/ip_address.c index ea238167f..2cf2aaef3 100644 --- a/lib/src/util/ip_address.c +++ b/lib/src/util/ip_address.c @@ -227,7 +227,19 @@ ERR: } int -ip_prefix_ntop (const ip_prefix_t * ip_prefix, char *dst, size_t size) +ip_prefix_ntop_short(const ip_prefix_t * ip_prefix, char *dst, size_t size) +{ + char ip_s[MAXSZ_IP_ADDRESS]; + const char * s = inet_ntop (ip_prefix->family, ip_prefix->address.buffer, ip_s, MAXSZ_IP_ADDRESS); + if (!s) + return -1; + size_t n = snprintf(dst, size, "%s", ip_s); + + return (n > 0 ? 1 : -1); +} + +int +ip_prefix_ntop(const ip_prefix_t * ip_prefix, char *dst, size_t size) { char ip_s[MAXSZ_IP_ADDRESS]; const char * s = inet_ntop (ip_prefix->family, ip_prefix->address.buffer, ip_s, MAXSZ_IP_ADDRESS); diff --git a/libtransport/src/hicn/transport/core/prefix.cc b/libtransport/src/hicn/transport/core/prefix.cc index badbf3b3b..6b87ccd1f 100644 --- a/libtransport/src/hicn/transport/core/prefix.cc +++ b/libtransport/src/hicn/transport/core/prefix.cc @@ -133,7 +133,7 @@ std::string Prefix::getNetwork() const { std::string network(size, 0); - if (ip_prefix_ntop(&ip_address_, (char *)network.c_str(), size) < 0) { + if (ip_prefix_ntop_short(&ip_address_, (char *)network.c_str(), size) < 0) { throw errors::RuntimeException( "Impossible to retrieve network from ip address."); } -- cgit 1.2.3-korg