summaryrefslogtreecommitdiffstats
path: root/lib/src/util/ip_address.c
diff options
context:
space:
mode:
authorJordan Augé <jordan.auge+fdio@cisco.com>2019-11-12 00:03:08 +0100
committerJordan Augé <jordan.auge+fdio@cisco.com>2019-11-14 17:16:56 +0100
commitfdb523a02680f5aa0727b862f0616ba5f8cb24cf (patch)
tree01fdf30eafde2a6f74edba52890715f55fdd6554 /lib/src/util/ip_address.c
parent4f57ca72e8131e5cfb023b26417b924e774d5e73 (diff)
[HICN-386] Improve API error management in libhicnctrl
Change-Id: I332e74ebcd89798c93de50ae7a20f7af8f59f54c Signed-off-by: Jordan Augé <jordan.auge+fdio@cisco.com>
Diffstat (limited to 'lib/src/util/ip_address.c')
-rw-r--r--lib/src/util/ip_address.c70
1 files changed, 30 insertions, 40 deletions
diff --git a/lib/src/util/ip_address.c b/lib/src/util/ip_address.c
index 062ff8c75..2b7c69bb5 100644
--- a/lib/src/util/ip_address.c
+++ b/lib/src/util/ip_address.c
@@ -19,6 +19,7 @@
*/
#include <hicn/util/ip_address.h>
+#include <hicn/util/log.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#ifdef __ANDROID__
@@ -110,11 +111,13 @@ ip_address_pton (const char *ip_address_str, ip_address_t * ip_address)
int pton_fd;
int family;
-
family = ip_address_get_family (ip_address_str);
switch (family) {
case AF_INET:
+ ip_address->pad[0] = 0;
+ ip_address->pad[1] = 0;
+ ip_address->pad[2] = 0;
pton_fd = inet_pton (AF_INET, ip_address_str, &ip_address->v4.buffer);
break;
case AF_INET6:
@@ -135,18 +138,26 @@ ip_address_pton (const char *ip_address_str, ip_address_t * ip_address)
int
ip_address_snprintf(char * s, size_t size, const ip_address_t * ip_address, int family)
{
+
const char * rc;
switch(family) {
case AF_INET:
+ if (size <= INET_ADDRSTRLEN)
+ return -1;
rc = inet_ntop (AF_INET, ip_address->v4.buffer, s, INET_ADDRSTRLEN);
- break;
+ if (!rc)
+ return -1;
+ return INET_ADDRSTRLEN;
case AF_INET6:
+ if (size <= INET6_ADDRSTRLEN)
+ return -1;
rc = inet_ntop (AF_INET6, ip_address->v6.buffer, s, INET6_ADDRSTRLEN);
- break;
+ if (!rc)
+ return -1;
+ return INET6_ADDRSTRLEN;
default:
return -1;
}
- return rc ? strlen(rc) : -1;
}
int
@@ -269,9 +280,10 @@ ip_prefix_ntop_short(const ip_prefix_t * ip_prefix, char *dst, size_t size)
}
if (!s)
return -1;
- size_t n = snprintf(dst, size, "%s", ip_s);
-
- return (n > 0 ? 1 : -1);
+ int rc = snprintf(dst, size, "%s", ip_s);
+ if (rc >= size)
+ return size;
+ return rc;
}
int
@@ -291,9 +303,10 @@ ip_prefix_ntop(const ip_prefix_t * ip_prefix, char *dst, size_t size)
}
if (!s)
return -1;
- size_t n = snprintf(dst, size, "%s/%d", ip_s, ip_prefix->len);
-
- return (n > 0 ? 1 : -1);
+ int rc = snprintf(dst, size, "%s/%d", ip_s, ip_prefix->len);
+ if (rc >= size)
+ return size;
+ return rc;
}
int
@@ -361,42 +374,19 @@ int
url_snprintf(char * s, size_t size, int family,
const ip_address_t * ip_address, u16 port)
{
- char * cur = s;
+ char ip_address_s[MAXSZ_IP_ADDRESS];
int rc;
/* Other address are currently not supported */
- if (!IS_VALID_FAMILY(family)) {
+ if (!IS_VALID_FAMILY(family))
return -1;
- }
-
- rc = snprintf(cur, s + size - cur, "inet%c://",
- (family == AF_INET) ? '4' : '6');
- if (rc < 0)
- return rc;
- cur += rc;
- if (size != 0 && cur >= s + size)
- return cur - s;
-
- rc = ip_address_snprintf(cur, s + size - cur, ip_address, family);
- if (rc < 0)
- return rc;
- cur += rc;
- if (size != 0 && cur >= s + size)
- return cur - s;
-
- rc = snprintf(cur, s + size - cur, ":");
- if (rc < 0)
- return rc;
- cur += rc;
- if (size != 0 && cur >= s + size)
- return cur - s;
- rc = snprintf(cur, s + size - cur, "%d", port);
+ rc = ip_address_snprintf(ip_address_s, MAXSZ_IP_ADDRESS, ip_address, family);
+ if (rc >= MAXSZ_IP_ADDRESS)
+ WARN("[url_snprintf] Unexpected ip_address truncation");
if (rc < 0)
return rc;
- cur += rc;
- if (size != 0 && cur >= s + size)
- return cur - s;
- return cur - s;
+ return snprintf(s, size, "inet%c://%s:%d", (family == AF_INET) ? '4' : '6',
+ ip_address_s, port);
}