Change in libosmocore[master]: add osmo_ip_port API

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Mon Mar 4 22:10:18 UTC 2019


Neels Hofmeyr has uploaded this change for review. ( https://gerrit.osmocom.org/13123


Change subject: add osmo_ip_port API
......................................................................

add osmo_ip_port API

For handling RTP IP addresses and ports, osmo-mgw, osmo-bsc and osmo-msc so far
have their own separate shims and code duplication around inet_ntoa(), htons(),
sockaddr conversions etc. Unify and standardize with this common API.

In the MGW endpoint FSM that was introduced in osmo-bsc and which I would like
to re-use for osmo-msc (upcoming patch moving that to osmo-mgw), it has turned
out that using char* IP address and uint16_t port number types are a convenient
common denominator for logging, MGCP message composition and GSM48. Ongoing
osmo-msc work also uses this for MNCC.

This is of course potentially useful for any other IP+port combinations besides
RTP stream handling.

Needless to say that most current implementations will probably stay with their
current own conversion code for a long time; for current osmo-{bsc,msc,mgw}
work (MGW endpoint FSM) though, I would like to move to this API here.

Change-Id: Id617265337f09dfb6ddfe111ef5e578cd3dc9f63
---
M include/Makefile.am
A include/osmocom/core/ip_port.h
M src/Makefile.am
A src/ip_port.c
M tests/Makefile.am
A tests/ip_port/ip_port_test.c
A tests/ip_port/ip_port_test.ok
M tests/testsuite.at
8 files changed, 995 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/23/13123/1

diff --git a/include/Makefile.am b/include/Makefile.am
index 6f64fa6..1f0f94b 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -53,6 +53,7 @@
                        osmocom/core/utils.h \
                        osmocom/core/write_queue.h \
                        osmocom/core/use_count.h \
+                       osmocom/core/ip_port.h \
                        osmocom/crypt/auth.h \
                        osmocom/crypt/gprs_cipher.h \
 		       osmocom/ctrl/control_cmd.h \
diff --git a/include/osmocom/core/ip_port.h b/include/osmocom/core/ip_port.h
new file mode 100644
index 0000000..2dad411
--- /dev/null
+++ b/include/osmocom/core/ip_port.h
@@ -0,0 +1,81 @@
+/*! \file ip_port.h
+ * Common API to store an IP address and port.
+ */
+/*
+ * (C) 2019 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ *
+ * Author: neels at hofmeyr.de
+ *
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <netinet/in.h>
+
+/*! \defgroup ip_port  IP address/port utilities.
+ * @{
+ * \file ip_port.h
+ */
+
+int osmo_ip_str_type(const char *ip);
+
+struct osmo_ip_port {
+	/*! AF_INET for IPv4 address, or AF_INET6 for IPv6 address. */
+	int af;
+	/*! NUL terminated string of the IPv4 or IPv6 address. */
+	char ip[INET6_ADDRSTRLEN];
+	/*! Port number */
+	uint16_t port;
+};
+
+/*! Format string to print an osmo_ip_port.
+ *
+ * For example:
+ *
+ *     struct osmo_ip_port *my_ip_port = ...;
+ *     printf("got " OSMO_IP_PORT_FMT, OSMO_IP_PORT_ARGS(my_ip_port));
+ */
+#define OSMO_IP_PORT_FMT "%s:%u"
+#define OSMO_IP_PORT_ARGS(R) ((R)->ip ? : ""), (R)->port
+
+bool osmo_ip_port_is_set(const struct osmo_ip_port *ip_port);
+
+int osmo_ip_port_from_str(struct osmo_ip_port *ip_port, const char *ip, uint16_t port);
+
+int osmo_ip_port_from_in_addr(struct osmo_ip_port *ip_port, const struct in_addr *addr, uint16_t port);
+int osmo_ip_port_from_in6_addr(struct osmo_ip_port *ip_port, const struct in6_addr *addr, uint16_t port);
+int osmo_ip_port_from_32(struct osmo_ip_port *ip_port, uint32_t ip, uint16_t port);
+int osmo_ip_port_from_32n(struct osmo_ip_port *ip_port, uint32_t ip, uint16_t port);
+int osmo_ip_port_from_sockaddr_in(struct osmo_ip_port *ip_port, const struct sockaddr_in *src);
+int osmo_ip_port_from_sockaddr_in6(struct osmo_ip_port *ip_port, const struct sockaddr_in6 *src);
+int osmo_ip_port_from_sockaddr(struct osmo_ip_port *ip_port, const struct sockaddr_storage *dst);
+
+int osmo_ip_port_to_in_addr(const struct osmo_ip_port *ip_port, struct in_addr *dst);
+int osmo_ip_port_to_in6_addr(const struct osmo_ip_port *ip_port, struct in6_addr *dst);
+int osmo_ip_port_to_32(const struct osmo_ip_port *ip_port, uint32_t *ip);
+int osmo_ip_port_to_32n(const struct osmo_ip_port *ip_port, uint32_t *ip);
+int osmo_ip_port_to_sockaddr_in(const struct osmo_ip_port *ip_port, struct sockaddr_in *dst);
+int osmo_ip_port_to_sockaddr_in6(const struct osmo_ip_port *ip_port, struct sockaddr_in6 *dst);
+int osmo_ip_port_to_sockaddr(const struct osmo_ip_port *ip_port, struct sockaddr_storage *dst);
+
+/*! @} */
diff --git a/src/Makefile.am b/src/Makefile.am
index 726ad15..cb1d76c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -26,6 +26,7 @@
 			 isdnhdlc.c \
 			 tdef.c \
 			 use_count.c \
+			 ip_port.c \
 			 $(NULL)
 
 if HAVE_SSSE3
diff --git a/src/ip_port.c b/src/ip_port.c
new file mode 100644
index 0000000..4ab6f79
--- /dev/null
+++ b/src/ip_port.c
@@ -0,0 +1,375 @@
+/*! \file ip_port.c
+ * Common implementation to store an IP address and port.
+ */
+/*
+ * (C) 2019 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ *
+ * Author: neels at hofmeyr.de
+ *
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <osmocom/core/ip_port.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/byteswap.h>
+
+/*! \addtogroup ip_port
+ *
+ * Common operations to store IP address as a char string along with a uint16_t port number.
+ *
+ * Convert IP address string to/from in_addr and in6_addr, with bounds checking and basic housekeeping.
+ *
+ * The initial purpose is to store and translate IP address info between GSM CC and MGCP protocols -- GSM mostly using
+ * 32-bit IPv4 addresses, and MGCP forwarding addresses as ASCII character strings.
+ *
+ * (At the time of writing, there are no immediate IPv6 users that come to mind, but it seemed appropriate to
+ * accommodate both address families from the start.)
+ *
+ * @{
+ * \file ip_port.c
+ */
+
+
+/*! Return true if all elements of the osmo_ip_port instance are set.
+ * \param[in] ip_port  The instance to examine.
+ * \return True iff ip is nonempty, port is not 0 and af is set to either AF_INET or AF_INET6.
+ */
+bool osmo_ip_port_is_set(const struct osmo_ip_port *ip_port)
+{
+	return *ip_port->ip
+		&& ip_port->port
+		&& (ip_port->af == AF_INET || ip_port->af == AF_INET6);
+}
+
+/*! Distinguish between valid IPv4 and IPv6 strings.
+ * This does not verify whether the string is a valid IP address; it assumes that the input is a valid IP address, and
+ * on that premise returns whether it is an IPv4 or IPv6 string, by looking for '.' and ':' characters.  It is safe to
+ * feed invalid address strings, but the return value is only guaranteed to be meaningful if the input was valid.
+ * \param[in] ip  Valid IP address string.
+ * \return AF_INET or AF_INET6, or AF_UNSPEC if neither '.' nor ':' are found in the string.
+ */
+int osmo_ip_str_type(const char *ip)
+{
+	if (!ip)
+		return AF_UNSPEC;
+	/* Could also be IPv4-mapped IPv6 format with both colons and dots: x:x:x:x:x:x:d.d.d.d */
+	if (strchr(ip, ':'))
+		return AF_INET6;
+	if (strchr(ip, '.'))
+		return AF_INET;
+	return AF_UNSPEC;
+}
+
+/*! Safely copy the given ip string to ip_port, classify to AF_INET or AF_INET6, and set the port.
+ * Data will be written to ip_port even if an error is returned.
+ * \param[out] ip_port  The instance to copy to.
+ * \param[in] ip  Valid IP address string.
+ * \param[in] port  Port number.
+ * \return 0 on success, negative if copying the address string failed (e.g. too long), if the address family could
+ *         not be detected (i.e. if osmo_ip_str_type() returned AF_UNSPEC), or if ip_port is NULL.
+ */
+int osmo_ip_port_from_str(struct osmo_ip_port *ip_port, const char *ip, uint16_t port)
+{
+	int rc;
+	if (!ip_port)
+		return -ENOSPC;
+	if (!ip)
+		ip = "";
+	*ip_port = (struct osmo_ip_port){
+		.af = osmo_ip_str_type(ip),
+		.port = port,
+	};
+	rc = osmo_strlcpy(ip_port->ip, ip, sizeof(ip_port->ip));
+	if (rc <= 0)
+		return -EIO;
+	if (rc >= sizeof(ip_port->ip))
+		return -ENOSPC;
+	if (ip_port->af == AF_UNSPEC)
+		return -EINVAL;
+	return 0;
+}
+
+/*! Convert IPv4 address to osmo_ip_port, and set port.
+ * \param[out] ip_port  The instance to copy to.
+ * \param[in] addr  IPv4 address data.
+ * \param[in] port  Port number.
+ * \return 0 on success, negative on error.
+ */
+int osmo_ip_port_from_in_addr(struct osmo_ip_port *ip_port, const struct in_addr *addr, uint16_t port)
+{
+	if (!ip_port)
+		return -ENOSPC;
+	*ip_port = (struct osmo_ip_port){
+		.af = AF_INET,
+		.port = port,
+	};
+	if (!inet_ntop(AF_INET, addr, ip_port->ip, sizeof(ip_port->ip)))
+		return -ENOSPC;
+	return 0;
+}
+
+/*! Convert IPv6 address to osmo_ip_port, and set port.
+ * \param[out] ip_port  The instance to copy to.
+ * \param[in] addr  IPv6 address data.
+ * \param[in] port  Port number.
+ * \return 0 on success, negative on error.
+ */
+int osmo_ip_port_from_in6_addr(struct osmo_ip_port *ip_port, const struct in6_addr *addr, uint16_t port)
+{
+	if (!ip_port)
+		return -ENOSPC;
+	*ip_port = (struct osmo_ip_port){
+		.af = AF_INET6,
+		.port = port,
+	};
+	if (!inet_ntop(AF_INET6, addr, ip_port->ip, sizeof(ip_port->ip)))
+		return -ENOSPC;
+	return 0;
+}
+
+/*! Convert IPv4 address from 32bit host-byte-order to osmo_ip_port, and set port.
+ * \param[out] ip_port  The instance to copy to.
+ * \param[in] addr  32bit IPv4 address data.
+ * \param[in] port  Port number.
+ * \return 0 on success, negative on error.
+ */
+int osmo_ip_port_from_32(struct osmo_ip_port *ip_port, uint32_t ip, uint16_t port)
+{
+	struct in_addr addr;
+	if (!ip_port)
+		return -ENOSPC;
+	addr.s_addr = ip;
+	return osmo_ip_port_from_in_addr(ip_port, &addr, port);
+}
+
+/*! Convert IPv4 address from 32bit network-byte-order to osmo_ip_port, and set port.
+ * \param[out] ip_port  The instance to copy to.
+ * \param[in] addr  32bit IPv4 address data.
+ * \param[in] port  Port number.
+ * \return 0 on success, negative on error.
+ */
+int osmo_ip_port_from_32n(struct osmo_ip_port *ip_port, uint32_t ip, uint16_t port)
+{
+	if (!ip_port)
+		return -ENOSPC;
+	return osmo_ip_port_from_32(ip_port, osmo_ntohl(ip), port);
+}
+
+/*! Convert IPv4 address and port to osmo_ip_port.
+ * \param[out] ip_port  The instance to copy to.
+ * \param[in] src  IPv4 address and port data.
+ * \return 0 on success, negative on error.
+ */
+int osmo_ip_port_from_sockaddr_in(struct osmo_ip_port *ip_port, const struct sockaddr_in *src)
+{
+	if (!ip_port)
+		return -ENOSPC;
+	if (!src)
+		return -EINVAL;
+	if (src->sin_family != AF_INET)
+		return -EINVAL;
+	return osmo_ip_port_from_in_addr(ip_port, &src->sin_addr, osmo_ntohs(src->sin_port));
+}
+
+/*! Convert IPv6 address and port to osmo_ip_port.
+ * \param[out] ip_port  The instance to copy to.
+ * \param[in] src  IPv6 address and port data.
+ * \return 0 on success, negative on error.
+ */
+int osmo_ip_port_from_sockaddr_in6(struct osmo_ip_port *ip_port, const struct sockaddr_in6 *src)
+{
+	if (!ip_port)
+		return -ENOSPC;
+	if (!src)
+		return -EINVAL;
+	if (src->sin6_family != AF_INET6)
+		return -EINVAL;
+	return osmo_ip_port_from_in6_addr(ip_port, &src->sin6_addr, osmo_ntohs(src->sin6_port));
+}
+
+/*! Convert IPv4 or IPv6 address and port to osmo_ip_port.
+ * \param[out] ip_port  The instance to copy to.
+ * \param[in] src  IPv4 or IPv6 address and port data.
+ * \return 0 on success, negative if src does not indicate AF_INET nor AF_INET6 (or if the conversion fails, which
+ *         should not be possible in practice).
+ */
+int osmo_ip_port_from_sockaddr(struct osmo_ip_port *ip_port, const struct sockaddr_storage *src)
+{
+	const struct sockaddr_in *sin = (void*)src;
+	const struct sockaddr_in6 *sin6 = (void*)src;
+	if (!ip_port)
+		return -ENOSPC;
+	if (!src)
+		return -EINVAL;
+	if (sin->sin_family == AF_INET)
+		return osmo_ip_port_from_sockaddr_in(ip_port, sin);
+	if (sin6->sin6_family == AF_INET6)
+		return osmo_ip_port_from_sockaddr_in6(ip_port, sin6);
+	return -EINVAL;
+}
+
+/*! Convert osmo_ip_port address string to IPv4 address data.
+ * \param[in] ip_port  The instance to convert the IP of.
+ * \param[out] dst  IPv4 address data to write to.
+ * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
+ */
+int osmo_ip_port_to_in_addr(const struct osmo_ip_port *ip_port, struct in_addr *dst)
+{
+	int rc;
+	if (!ip_port)
+		return -EINVAL;
+	if (!dst)
+		return -ENOSPC;
+	if (ip_port->af != AF_INET)
+		return -EAFNOSUPPORT;
+	rc = inet_pton(AF_INET, ip_port->ip, dst);
+	if (rc != 1)
+		return -EINVAL;
+	return 0;
+}
+
+/*! Convert osmo_ip_port address string to IPv6 address data.
+ * \param[in] ip_port  The instance to convert the IP of.
+ * \param[out] dst  IPv6 address data to write to.
+ * \return 0 on success, negative on error (e.g. invalid IPv6 address string).
+ */
+int osmo_ip_port_to_in6_addr(const struct osmo_ip_port *ip_port, struct in6_addr *dst)
+{
+	int rc;
+	if (!ip_port)
+		return -EINVAL;
+	if (!dst)
+		return -ENOSPC;
+	if (ip_port->af != AF_INET6)
+		return -EINVAL;
+	rc = inet_pton(AF_INET6, ip_port->ip, dst);
+	if (rc != 1)
+		return -EINVAL;
+	return 0;
+}
+
+/*! Convert osmo_ip_port address string to IPv4 address data in host-byte-order.
+ * \param[in] ip_port  The instance to convert the IP of.
+ * \param[out] dst  IPv4 address data in 32bit host-byte-order format to write to.
+ * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
+ */
+int osmo_ip_port_to_32(const struct osmo_ip_port *ip_port, uint32_t *ip)
+{
+	int rc;
+	struct in_addr addr;
+	if (!ip_port)
+		return -EINVAL;
+	if (!ip)
+		return -ENOSPC;
+	rc = osmo_ip_port_to_in_addr(ip_port, &addr);
+	if (rc)
+		return rc;
+	*ip = addr.s_addr;
+	return 0;
+}
+
+/*! Convert osmo_ip_port address string to IPv4 address data in network-byte-order.
+ * \param[in] ip_port  The instance to convert the IP of.
+ * \param[out] dst  IPv4 address data in 32bit network-byte-order format to write to.
+ * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
+ */
+int osmo_ip_port_to_32n(const struct osmo_ip_port *ip_port, uint32_t *ip)
+{
+	int rc;
+	uint32_t ip_h;
+	if (!ip_port)
+		return -EINVAL;
+	if (!ip)
+		return -ENOSPC;
+	rc = osmo_ip_port_to_32(ip_port, &ip_h);
+	if (rc)
+		return rc;
+	*ip = osmo_htonl(ip_h);
+	return 0;
+}
+
+/*! Convert osmo_ip_port address string and port to IPv4 address and port data.
+ * \param[in] ip_port  The instance to convert the IP and port of.
+ * \param[out] dst  IPv4 address and port data to write to.
+ * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
+ */
+int osmo_ip_port_to_sockaddr_in(const struct osmo_ip_port *ip_port, struct sockaddr_in *dst)
+{
+	if (!ip_port)
+		return -EINVAL;
+	if (!dst)
+		return -ENOSPC;
+	if (ip_port->af != AF_INET)
+		return -EINVAL;
+	*dst = (struct sockaddr_in){
+		.sin_family = ip_port->af,
+		.sin_port = osmo_htons(ip_port->port),
+	};
+	return osmo_ip_port_to_in_addr(ip_port, &dst->sin_addr);
+}
+
+/*! Convert osmo_ip_port address string and port to IPv6 address and port data.
+ * \param[in] ip_port  The instance to convert the IP and port of.
+ * \param[out] dst  IPv6 address and port data to write to.
+ * \return 0 on success, negative on error (e.g. invalid IPv6 address string).
+ */
+int osmo_ip_port_to_sockaddr_in6(const struct osmo_ip_port *ip_port, struct sockaddr_in6 *dst)
+{
+	if (!ip_port)
+		return -EINVAL;
+	if (!dst)
+		return -ENOSPC;
+	if (ip_port->af != AF_INET6)
+		return -EINVAL;
+	*dst = (struct sockaddr_in6){
+		.sin6_family = ip_port->af,
+		.sin6_port = osmo_htons(ip_port->port),
+	};
+	return osmo_ip_port_to_in6_addr(ip_port, &dst->sin6_addr);
+}
+
+/*! Convert osmo_ip_port address string and port to IPv4 or IPv6 address and port data.
+ * Depending on ip_port->af, dst will be handled as struct sockaddr_in or struct sockaddr_in6.
+ * \param[in] ip_port  The instance to convert the IP and port of.
+ * \param[out] dst  IPv4/IPv6 address and port data to write to.
+ * \return 0 on success, negative on error (e.g. invalid IP address string for the family indicated by ip_port->af).
+ */
+int osmo_ip_port_to_sockaddr(const struct osmo_ip_port *ip_port, struct sockaddr_storage *dst)
+{
+	if (!ip_port)
+		return -EINVAL;
+	if (!dst)
+		return -ENOSPC;
+	switch (ip_port->af) {
+	case AF_INET:
+		return osmo_ip_port_to_sockaddr_in(ip_port, (void*)dst);
+	case AF_INET6:
+		return osmo_ip_port_to_sockaddr_in6(ip_port, (void*)dst);
+	default:
+		return -EINVAL;
+	}
+}
+
+/*! @} */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0584834..afed74d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -31,6 +31,7 @@
 		 tdef/tdef_vty_test_config_subnode			\
 		 tdef/tdef_vty_test_dynamic				\
 		 use_count/use_count_test				\
+		 ip_port/ip_port_test					\
 		 $(NULL)
 
 if ENABLE_MSGFILE
@@ -240,6 +241,9 @@
 use_count_use_count_test_SOURCES = use_count/use_count_test.c
 use_count_use_count_test_LDADD = $(LDADD)
 
+ip_port_ip_port_test_SOURCES = ip_port/ip_port_test.c
+ip_port_ip_port_test_LDADD = $(LDADD)
+
 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 	:;{ \
@@ -309,6 +313,7 @@
 	     tdef/tdef_vty_test_config_subnode.vty \
 	     tdef/tdef_vty_test_dynamic.vty \
 	     use_count/use_count_test.ok use_count/use_count_test.err \
+	     ip_port/ip_port_test.ok \
 	     $(NULL)
 
 DISTCLEANFILES = atconfig atlocal conv/gsm0503_test_vectors.c
diff --git a/tests/ip_port/ip_port_test.c b/tests/ip_port/ip_port_test.c
new file mode 100644
index 0000000..da1150b
--- /dev/null
+++ b/tests/ip_port/ip_port_test.c
@@ -0,0 +1,238 @@
+/* tests for osmo_ip_port API of libmsomcore */
+/*
+ * (C) 2019 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ *
+ * Author: neels at hofmeyr.de
+ *
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <osmocom/core/ip_port.h>
+#include <osmocom/core/utils.h>
+
+struct osmo_ip_port oip_data[] = {
+	{ .af = AF_INET, .ip = "1.2.3.4", .port = 5 },
+	{ .af = AF_INET, .ip = "0.0.0.0", .port = 0 },
+	{ .af = AF_INET, .ip = "255.255.255.255", .port = 65535 },
+	{ .af = AF_INET, .ip = "0.0.0.256", .port = 1 },
+	{ .af = AF_INET, .ip = "not an ip address", .port = 1 },
+	{ .af = AF_INET6, .ip = "1:2:3::4", .port = 5 },
+	{ .af = AF_INET6, .ip = "::", .port = 0 },
+	{ .af = AF_INET6, .ip = "::1", .port = 0 },
+	{ .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 },
+	{ .af = AF_INET6, .ip = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", .port = 65535 },
+	{ .af = AF_INET6, .ip = "::fffff", .port = 1 },
+	{ .af = AF_INET6, .ip = "not an ip address", .port = 1 },
+
+	{ .af = AF_INET6, .ip = "1.2.3.4", .port = 5 },
+	{ .af = AF_INET, .ip = "1:2:3::4", .port = 5 },
+	{ .af = AF_UNSPEC, .ip = "1.2.3.4", .port = 5 },
+	{ .af = AF_INET, .ip = "", .port = 5 },
+	{ .af = AF_INET6, .ip = "", .port = 5 },
+	{ .af = AF_INET, .ip = "1.2.3.4", .port = 0 },
+	{ .af = AF_INET, .ip = "1.2.3:4:5", .port = 0 },
+	{ .af = AF_INET6, .ip = "::1:10.9.8.7", .port = 1 },
+};
+
+const char *af_name(int af)
+{
+	switch (af) {
+	case AF_INET:
+		return "AF_INET";
+	case AF_INET6:
+		return "AF_INET6";
+	case AF_UNSPEC:
+		return "AF_UNSPEC";
+	default:
+		return "?";
+	}
+}
+
+static const struct value_string err_names[] = {
+	{ -EINVAL, "-EINVAL" },
+	{}
+};
+
+static inline const char *err_name(int err)
+{ return get_value_string(err_names, err); }
+
+static inline const char *rc_name(int rc)
+{
+	if (!rc)
+		return "rc == 0";
+	if (rc < 0)
+		return "rc < 0";
+	return "rc > 0";
+}
+
+void dump_oip(const struct osmo_ip_port *oip)
+{
+	printf("{ .af = %s, .ip = %s, .port = %u }\n", af_name(oip->af), osmo_quote_str(oip->ip, -1), oip->port);
+}
+
+void ip_port_test_conversions()
+{
+	int i;
+	char buf[1024];
+
+#define hexdump(what) \
+	osmo_hexdump_buf(buf, sizeof(buf), (void*)(&what), sizeof(what), "", false)
+
+	for (i = 0; i < ARRAY_SIZE(oip_data); i++) {
+		struct osmo_ip_port *x = &oip_data[i];
+		int rc;
+		printf("\n\n");
+		dump_oip(x);
+
+		printf("  osmo_ip_port_is_set() = %s\n", osmo_ip_port_is_set(x) ? "true" : "false");
+
+		{
+			struct in_addr a = {};
+
+			rc = osmo_ip_port_to_in_addr(x, &a);
+			printf("  osmo_ip_port_to_in_addr() %s in_addr=%s\n", rc_name(rc), hexdump(a));
+
+			if (rc == 0) {
+				struct osmo_ip_port back;
+				rc = osmo_ip_port_from_in_addr(&back, &a, x->port);
+				printf("   -> osmo_ip_port_from_in_addr() %s ", rc_name(rc));
+				dump_oip(&back);
+				if (memcmp(x, &back, sizeof(back)))
+					printf("      DIFFERS!\n");
+			}
+		}
+
+		{
+			struct in6_addr a = {};
+
+			rc = osmo_ip_port_to_in6_addr(x, &a);
+			printf("  osmo_ip_port_to_in6_addr() %s in6_addr=%s\n", rc_name(rc), hexdump(a));
+
+			if (rc == 0) {
+				struct osmo_ip_port back;
+				rc = osmo_ip_port_from_in6_addr(&back, &a, x->port);
+				printf("   -> osmo_ip_port_from_in6_addr() %s ", rc_name(rc));
+				dump_oip(&back);
+				if (memcmp(x, &back, sizeof(back)))
+					printf("      DIFFERS!\n");
+			}
+		}
+
+		{
+			uint32_t a = 0;
+
+			rc = osmo_ip_port_to_32(x, &a);
+			printf("  osmo_ip_port_to_32() %s uint32_t=0x%x\n", rc_name(rc), a);
+
+			if (rc == 0) {
+				struct osmo_ip_port back;
+				rc = osmo_ip_port_from_32(&back, a, x->port);
+				printf("   -> osmo_ip_port_from_32() %s ", rc_name(rc));
+				dump_oip(&back);
+				if (memcmp(x, &back, sizeof(back)))
+					printf("      DIFFERS!\n");
+			}
+		}
+
+		{
+			uint32_t a = 0;
+
+			rc = osmo_ip_port_to_32n(x, &a);
+			printf("  osmo_ip_port_to_32n() %s uint32_t=0x%x\n", rc_name(rc), a);
+
+			if (rc == 0) {
+				struct osmo_ip_port back;
+				rc = osmo_ip_port_from_32n(&back, a, x->port);
+				printf("   -> osmo_ip_port_from_32n() %s ", rc_name(rc));
+				dump_oip(&back);
+				if (memcmp(x, &back, sizeof(back)))
+					printf("      DIFFERS!\n");
+			}
+		}
+
+		{
+			struct sockaddr_in a = {};
+
+			rc = osmo_ip_port_to_sockaddr_in(x, &a);
+			printf("  osmo_ip_port_to_sockaddr_in() %s sockaddr_in=%s\n", rc_name(rc), hexdump(a));
+
+			if (rc == 0) {
+				struct osmo_ip_port back;
+				rc = osmo_ip_port_from_sockaddr_in(&back, &a);
+				printf("   -> osmo_ip_port_from_sockaddr_in() %s ", rc_name(rc));
+				dump_oip(&back);
+				if (memcmp(x, &back, sizeof(back)))
+					printf("      DIFFERS!\n");
+			}
+		}
+
+		{
+			struct sockaddr_in6 a = {};
+
+			rc = osmo_ip_port_to_sockaddr_in6(x, &a);
+			printf("  osmo_ip_port_to_sockaddr_in6() %s sockaddr_in6=%s\n", rc_name(rc), hexdump(a));
+
+			if (rc == 0) {
+				struct osmo_ip_port back;
+				rc = osmo_ip_port_from_sockaddr_in6(&back, &a);
+				printf("   -> osmo_ip_port_from_sockaddr_in6() %s ", rc_name(rc));
+				dump_oip(&back);
+				if (memcmp(x, &back, sizeof(back)))
+					printf("      DIFFERS!\n");
+			}
+		}
+
+		{
+			struct sockaddr_storage a = {};
+
+			rc = osmo_ip_port_to_sockaddr(x, &a);
+			printf("  osmo_ip_port_to_sockaddr() %s sockaddr_storage=%s\n", rc_name(rc), hexdump(a));
+
+			if (rc == 0) {
+				struct osmo_ip_port back;
+				rc = osmo_ip_port_from_sockaddr(&back, &a);
+				printf("   -> osmo_ip_port_from_sockaddr() %s ", rc_name(rc));
+				dump_oip(&back);
+				if (memcmp(x, &back, sizeof(back)))
+					printf("      DIFFERS!\n");
+			}
+		}
+
+		{
+			struct osmo_ip_port from_str;
+			rc = osmo_ip_port_from_str(&from_str, x->ip, x->port);
+			printf("  osmo_ip_port_from_str() %s ", rc_name(rc));
+			dump_oip(&from_str);
+			if (rc == 0 && memcmp(x, &from_str, sizeof(from_str)))
+				printf("      DIFFERS!\n");
+		}
+	}
+
+}
+
+int main(int argc, char **argv)
+{
+	ip_port_test_conversions();
+	return 0;
+}
+
diff --git a/tests/ip_port/ip_port_test.ok b/tests/ip_port/ip_port_test.ok
new file mode 100644
index 0000000..7ab7b01
--- /dev/null
+++ b/tests/ip_port/ip_port_test.ok
@@ -0,0 +1,288 @@
+
+
+{ .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc == 0 in_addr=01020304
+   -> osmo_ip_port_from_in_addr() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc == 0 uint32_t=0x4030201
+   -> osmo_ip_port_from_32() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+  osmo_ip_port_to_32n() rc == 0 uint32_t=0x1020304
+   -> osmo_ip_port_from_32n() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+  osmo_ip_port_to_sockaddr_in() rc == 0 sockaddr_in=02000005010203040000000000000000
+   -> osmo_ip_port_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0200000501020304000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+
+
+{ .af = AF_INET, .ip = "0.0.0.0", .port = 0 }
+  osmo_ip_port_is_set() = false
+  osmo_ip_port_to_in_addr() rc == 0 in_addr=00000000
+   -> osmo_ip_port_from_in_addr() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 }
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc == 0 uint32_t=0x0
+   -> osmo_ip_port_from_32() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 }
+  osmo_ip_port_to_32n() rc == 0 uint32_t=0x0
+   -> osmo_ip_port_from_32n() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 }
+  osmo_ip_port_to_sockaddr_in() rc == 0 sockaddr_in=02000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 }
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 }
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 }
+
+
+{ .af = AF_INET, .ip = "255.255.255.255", .port = 65535 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc == 0 in_addr=ffffffff
+   -> osmo_ip_port_from_in_addr() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 }
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc == 0 uint32_t=0xffffffff
+   -> osmo_ip_port_from_32() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 }
+  osmo_ip_port_to_32n() rc == 0 uint32_t=0xffffffff
+   -> osmo_ip_port_from_32n() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 }
+  osmo_ip_port_to_sockaddr_in() rc == 0 sockaddr_in=0200ffffffffffff0000000000000000
+   -> osmo_ip_port_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 }
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0200ffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 }
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 }
+
+
+{ .af = AF_INET, .ip = "0.0.0.256", .port = 1 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=02000001000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0200000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET, .ip = "0.0.0.256", .port = 1 }
+
+
+{ .af = AF_INET, .ip = "not an ip address", .port = 1 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=02000001000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0200000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc < 0 { .af = AF_UNSPEC, .ip = "not an ip address", .port = 1 }
+
+
+{ .af = AF_INET6, .ip = "1:2:3::4", .port = 5 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc == 0 in6_addr=00010002000300000000000000000004
+   -> osmo_ip_port_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 }
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc == 0 sockaddr_in6=0a000005000000000001000200030000000000000000000400000000
+   -> osmo_ip_port_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 }
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0a00000500000000000100020003000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 }
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 }
+
+
+{ .af = AF_INET6, .ip = "::", .port = 0 }
+  osmo_ip_port_is_set() = false
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc == 0 in6_addr=00000000000000000000000000000000
+   -> osmo_ip_port_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "::", .port = 0 }
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc == 0 sockaddr_in6=0a000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "::", .port = 0 }
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "::", .port = 0 }
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "::", .port = 0 }
+
+
+{ .af = AF_INET6, .ip = "::1", .port = 0 }
+  osmo_ip_port_is_set() = false
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc == 0 in6_addr=00000000000000000000000000000001
+   -> osmo_ip_port_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "::1", .port = 0 }
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc == 0 sockaddr_in6=0a000000000000000000000000000000000000000000000100000000
+   -> osmo_ip_port_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "::1", .port = 0 }
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0a00000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "::1", .port = 0 }
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "::1", .port = 0 }
+
+
+{ .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc == 0 in6_addr=ffffffffffffffffffffffffffffffff
+   -> osmo_ip_port_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc == 0 sockaddr_in6=0a00ffff00000000ffffffffffffffffffffffffffffffff00000000
+   -> osmo_ip_port_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0a00ffff00000000ffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
+
+
+{ .af = AF_INET6, .ip = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", .port = 65535 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc == 0 in6_addr=ffffffffffffffffffffffffffffffff
+   -> osmo_ip_port_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
+      DIFFERS!
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc == 0 sockaddr_in6=0a00ffff00000000ffffffffffffffffffffffffffffffff00000000
+   -> osmo_ip_port_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
+      DIFFERS!
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0a00ffff00000000ffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
+      DIFFERS!
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", .port = 65535 }
+
+
+{ .af = AF_INET6, .ip = "::fffff", .port = 1 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=0a000001000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0a00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "::fffff", .port = 1 }
+
+
+{ .af = AF_INET6, .ip = "not an ip address", .port = 1 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=0a000001000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0a00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc < 0 { .af = AF_UNSPEC, .ip = "not an ip address", .port = 1 }
+
+
+{ .af = AF_INET6, .ip = "1.2.3.4", .port = 5 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=0a000005000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0a00000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+      DIFFERS!
+
+
+{ .af = AF_INET, .ip = "1:2:3::4", .port = 5 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=02000005000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0200000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 }
+      DIFFERS!
+
+
+{ .af = AF_UNSPEC, .ip = "1.2.3.4", .port = 5 }
+  osmo_ip_port_is_set() = false
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
+      DIFFERS!
+
+
+{ .af = AF_INET, .ip = "", .port = 5 }
+  osmo_ip_port_is_set() = false
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=02000005000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0200000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc < 0 { .af = AF_UNSPEC, .ip = "", .port = 5 }
+
+
+{ .af = AF_INET6, .ip = "", .port = 5 }
+  osmo_ip_port_is_set() = false
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=0a000005000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0a00000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc < 0 { .af = AF_UNSPEC, .ip = "", .port = 5 }
+
+
+{ .af = AF_INET, .ip = "1.2.3.4", .port = 0 }
+  osmo_ip_port_is_set() = false
+  osmo_ip_port_to_in_addr() rc == 0 in_addr=01020304
+   -> osmo_ip_port_from_in_addr() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 }
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc == 0 uint32_t=0x4030201
+   -> osmo_ip_port_from_32() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 }
+  osmo_ip_port_to_32n() rc == 0 uint32_t=0x1020304
+   -> osmo_ip_port_from_32n() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 }
+  osmo_ip_port_to_sockaddr_in() rc == 0 sockaddr_in=02000000010203040000000000000000
+   -> osmo_ip_port_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 }
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0200000001020304000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 }
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 }
+
+
+{ .af = AF_INET, .ip = "1.2.3:4:5", .port = 0 }
+  osmo_ip_port_is_set() = false
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=02000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_to_sockaddr() rc < 0 sockaddr_storage=0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "1.2.3:4:5", .port = 0 }
+      DIFFERS!
+
+
+{ .af = AF_INET6, .ip = "::1:10.9.8.7", .port = 1 }
+  osmo_ip_port_is_set() = true
+  osmo_ip_port_to_in_addr() rc < 0 in_addr=00000000
+  osmo_ip_port_to_in6_addr() rc == 0 in6_addr=0000000000000000000000010a090807
+   -> osmo_ip_port_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "::1:a09:807", .port = 1 }
+      DIFFERS!
+  osmo_ip_port_to_32() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_32n() rc < 0 uint32_t=0x0
+  osmo_ip_port_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000
+  osmo_ip_port_to_sockaddr_in6() rc == 0 sockaddr_in6=0a000001000000000000000000000000000000010a09080700000000
+   -> osmo_ip_port_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "::1:a09:807", .port = 1 }
+      DIFFERS!
+  osmo_ip_port_to_sockaddr() rc == 0 sockaddr_storage=0a000001000000000000000000000000000000010a0908070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+   -> osmo_ip_port_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "::1:a09:807", .port = 1 }
+      DIFFERS!
+  osmo_ip_port_from_str() rc == 0 { .af = AF_INET6, .ip = "::1:10.9.8.7", .port = 1 }
diff --git a/tests/testsuite.at b/tests/testsuite.at
index edf127d..5d74af1 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -338,3 +338,9 @@
 cat $abs_srcdir/use_count/use_count_test.err > experr
 AT_CHECK([$abs_top_builddir/tests/use_count/use_count_test], [0], [expout], [experr])
 AT_CLEANUP
+
+AT_SETUP([ip_port])
+AT_KEYWORDS([ip_port])
+cat $abs_srcdir/ip_port/ip_port_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/ip_port/ip_port_test], [0], [expout], [ignore])
+AT_CLEANUP

-- 
To view, visit https://gerrit.osmocom.org/13123
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id617265337f09dfb6ddfe111ef5e578cd3dc9f63
Gerrit-Change-Number: 13123
Gerrit-PatchSet: 1
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190304/f0f03767/attachment-0001.html>


More information about the gerrit-log mailing list