From 0c7826e9bd8026b239d320dba49416fd603f17fd Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Mon, 25 Feb 2019 02:45:06 +0100 Subject: add osmo_sockaddr_str 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 --- configure.ac | 2 +- include/Makefile.am | 1 + include/osmocom/core/sockaddr_str.h | 87 ++++++++ src/Makefile.am | 4 +- src/sockaddr_str.c | 378 ++++++++++++++++++++++++++++++++ tests/Makefile.am | 5 + tests/sockaddr_str/sockaddr_str_test.c | 239 ++++++++++++++++++++ tests/sockaddr_str/sockaddr_str_test.ok | 288 ++++++++++++++++++++++++ tests/testsuite.at | 6 + 9 files changed, 1008 insertions(+), 2 deletions(-) create mode 100644 include/osmocom/core/sockaddr_str.h create mode 100644 src/sockaddr_str.c create mode 100644 tests/sockaddr_str/sockaddr_str_test.c create mode 100644 tests/sockaddr_str/sockaddr_str_test.ok diff --git a/configure.ac b/configure.ac index 4a00e695..d717a0bb 100644 --- a/configure.ac +++ b/configure.ac @@ -57,7 +57,7 @@ AC_SUBST(LTLDFLAGS_OSMOCTRL) dnl checks for header files AC_HEADER_STDC -AC_CHECK_HEADERS(execinfo.h sys/select.h sys/socket.h sys/timerfd.h syslog.h ctype.h netinet/tcp.h) +AC_CHECK_HEADERS(execinfo.h sys/select.h sys/socket.h sys/timerfd.h syslog.h ctype.h netinet/tcp.h netinet/in.h) # for src/conv.c AC_FUNC_ALLOCA AC_SEARCH_LIBS([dlopen], [dl dld], [LIBRARY_DLOPEN="$LIBS";LIBS=""]) diff --git a/include/Makefile.am b/include/Makefile.am index 17f7d1ce..6ed7fe67 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -52,6 +52,7 @@ nobase_include_HEADERS = \ osmocom/core/timer_compat.h \ osmocom/core/utils.h \ osmocom/core/write_queue.h \ + osmocom/core/sockaddr_str.h \ osmocom/crypt/auth.h \ osmocom/crypt/gprs_cipher.h \ osmocom/ctrl/control_cmd.h \ diff --git a/include/osmocom/core/sockaddr_str.h b/include/osmocom/core/sockaddr_str.h new file mode 100644 index 00000000..253b755f --- /dev/null +++ b/include/osmocom/core/sockaddr_str.h @@ -0,0 +1,87 @@ +/*! \file sockaddr_str.h + * Common API to store an IP address and port. + */ +/* + * (C) 2019 by sysmocom - s.f.m.c. GmbH + * + * Author: neels@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 +#include +#include + +struct in_addr; +struct in6_addr; +struct sockaddr_storage; +struct sockaddr_in; +struct sockaddr_in6; + +/*! \defgroup sockaddr_str IP address/port utilities. + * @{ + * \file sockaddr_str.h + */ + +int osmo_ip_str_type(const char *ip); + +struct osmo_sockaddr_str { + /*! 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_sockaddr_str. + * + * For example: + * + * struct osmo_sockaddr_str *my_sockaddr_str = ...; + * printf("got " OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(my_sockaddr_str)); + */ +#define OSMO_SOCKADDR_STR_FMT "%s:%u" +#define OSMO_SOCKADDR_STR_FMT_ARGS(R) ((R)->ip ? : ""), (R)->port + +bool osmo_sockaddr_str_is_set(const struct osmo_sockaddr_str *sockaddr_str); + +int osmo_sockaddr_str_from_str(struct osmo_sockaddr_str *sockaddr_str, const char *ip, uint16_t port); + +int osmo_sockaddr_str_from_in_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in_addr *addr, uint16_t port); +int osmo_sockaddr_str_from_in6_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in6_addr *addr, uint16_t port); +int osmo_sockaddr_str_from_32(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port); +int osmo_sockaddr_str_from_32n(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port); +int osmo_sockaddr_str_from_sockaddr_in(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in *src); +int osmo_sockaddr_str_from_sockaddr_in6(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in6 *src); +int osmo_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_storage *src); + +int osmo_sockaddr_str_to_in_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in_addr *dst); +int osmo_sockaddr_str_to_in6_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in6_addr *dst); +int osmo_sockaddr_str_to_32(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip); +int osmo_sockaddr_str_to_32n(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip); +int osmo_sockaddr_str_to_sockaddr_in(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in *dst); +int osmo_sockaddr_str_to_sockaddr_in6(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in6 *dst); +int osmo_sockaddr_str_to_sockaddr(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_storage *dst); + +/*! @} */ diff --git a/src/Makefile.am b/src/Makefile.am index 27ab7026..1fae8b0a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,7 +24,9 @@ libosmocore_la_SOURCES = timer.c timer_gettimeofday.c timer_clockgettime.c \ macaddr.c stat_item.c stats.c stats_statsd.c prim.c \ conv_acc.c conv_acc_generic.c sercomm.c prbs.c \ isdnhdlc.c \ - tdef.c + tdef.c \ + sockaddr_str.c \ + $(NULL) if HAVE_SSSE3 libosmocore_la_SOURCES += conv_acc_sse.c diff --git a/src/sockaddr_str.c b/src/sockaddr_str.c new file mode 100644 index 00000000..c9d9a946 --- /dev/null +++ b/src/sockaddr_str.c @@ -0,0 +1,378 @@ +/*! \file sockaddr_str.c + * Common implementation to store an IP address and port. + */ +/* + * (C) 2019 by sysmocom - s.f.m.c. GmbH + * + * Author: neels@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 "config.h" + +#ifdef HAVE_NETINET_IN_H +#include +#include +#include +#include + +#include +#include +#include + +/*! \addtogroup sockaddr_str + * + * 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 sockaddr_str.c + */ + +/*! Return true if all elements of the osmo_sockaddr_str instance are set. + * \param[in] sockaddr_str 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_sockaddr_str_is_set(const struct osmo_sockaddr_str *sockaddr_str) +{ + return *sockaddr_str->ip + && sockaddr_str->port + && (sockaddr_str->af == AF_INET || sockaddr_str->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 sockaddr_str, classify to AF_INET or AF_INET6, and set the port. + * Data will be written to sockaddr_str even if an error is returned. + * \param[out] sockaddr_str 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 sockaddr_str is NULL. + */ +int osmo_sockaddr_str_from_str(struct osmo_sockaddr_str *sockaddr_str, const char *ip, uint16_t port) +{ + int rc; + if (!sockaddr_str) + return -ENOSPC; + if (!ip) + ip = ""; + *sockaddr_str = (struct osmo_sockaddr_str){ + .af = osmo_ip_str_type(ip), + .port = port, + }; + rc = osmo_strlcpy(sockaddr_str->ip, ip, sizeof(sockaddr_str->ip)); + if (rc <= 0) + return -EIO; + if (rc >= sizeof(sockaddr_str->ip)) + return -ENOSPC; + if (sockaddr_str->af == AF_UNSPEC) + return -EINVAL; + return 0; +} + +/*! Convert IPv4 address to osmo_sockaddr_str, and set port. + * \param[out] sockaddr_str 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_sockaddr_str_from_in_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in_addr *addr, uint16_t port) +{ + if (!sockaddr_str) + return -ENOSPC; + *sockaddr_str = (struct osmo_sockaddr_str){ + .af = AF_INET, + .port = port, + }; + if (!inet_ntop(AF_INET, addr, sockaddr_str->ip, sizeof(sockaddr_str->ip))) + return -ENOSPC; + return 0; +} + +/*! Convert IPv6 address to osmo_sockaddr_str, and set port. + * \param[out] sockaddr_str 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_sockaddr_str_from_in6_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in6_addr *addr, uint16_t port) +{ + if (!sockaddr_str) + return -ENOSPC; + *sockaddr_str = (struct osmo_sockaddr_str){ + .af = AF_INET6, + .port = port, + }; + if (!inet_ntop(AF_INET6, addr, sockaddr_str->ip, sizeof(sockaddr_str->ip))) + return -ENOSPC; + return 0; +} + +/*! Convert IPv4 address from 32bit host-byte-order to osmo_sockaddr_str, and set port. + * \param[out] sockaddr_str 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_sockaddr_str_from_32(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port) +{ + struct in_addr addr; + if (!sockaddr_str) + return -ENOSPC; + addr.s_addr = ip; + return osmo_sockaddr_str_from_in_addr(sockaddr_str, &addr, port); +} + +/*! Convert IPv4 address from 32bit network-byte-order to osmo_sockaddr_str, and set port. + * \param[out] sockaddr_str 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_sockaddr_str_from_32n(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port) +{ + if (!sockaddr_str) + return -ENOSPC; + return osmo_sockaddr_str_from_32(sockaddr_str, osmo_ntohl(ip), port); +} + +/*! Convert IPv4 address and port to osmo_sockaddr_str. + * \param[out] sockaddr_str The instance to copy to. + * \param[in] src IPv4 address and port data. + * \return 0 on success, negative on error. + */ +int osmo_sockaddr_str_from_sockaddr_in(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in *src) +{ + if (!sockaddr_str) + return -ENOSPC; + if (!src) + return -EINVAL; + if (src->sin_family != AF_INET) + return -EINVAL; + return osmo_sockaddr_str_from_in_addr(sockaddr_str, &src->sin_addr, osmo_ntohs(src->sin_port)); +} + +/*! Convert IPv6 address and port to osmo_sockaddr_str. + * \param[out] sockaddr_str The instance to copy to. + * \param[in] src IPv6 address and port data. + * \return 0 on success, negative on error. + */ +int osmo_sockaddr_str_from_sockaddr_in6(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in6 *src) +{ + if (!sockaddr_str) + return -ENOSPC; + if (!src) + return -EINVAL; + if (src->sin6_family != AF_INET6) + return -EINVAL; + return osmo_sockaddr_str_from_in6_addr(sockaddr_str, &src->sin6_addr, osmo_ntohs(src->sin6_port)); +} + +/*! Convert IPv4 or IPv6 address and port to osmo_sockaddr_str. + * \param[out] sockaddr_str 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_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_storage *src) +{ + const struct sockaddr_in *sin = (void*)src; + const struct sockaddr_in6 *sin6 = (void*)src; + if (!sockaddr_str) + return -ENOSPC; + if (!src) + return -EINVAL; + if (sin->sin_family == AF_INET) + return osmo_sockaddr_str_from_sockaddr_in(sockaddr_str, sin); + if (sin6->sin6_family == AF_INET6) + return osmo_sockaddr_str_from_sockaddr_in6(sockaddr_str, sin6); + return -EINVAL; +} + +/*! Convert osmo_sockaddr_str address string to IPv4 address data. + * \param[in] sockaddr_str 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_sockaddr_str_to_in_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in_addr *dst) +{ + int rc; + if (!sockaddr_str) + return -EINVAL; + if (!dst) + return -ENOSPC; + if (sockaddr_str->af != AF_INET) + return -EAFNOSUPPORT; + rc = inet_pton(AF_INET, sockaddr_str->ip, dst); + if (rc != 1) + return -EINVAL; + return 0; +} + +/*! Convert osmo_sockaddr_str address string to IPv6 address data. + * \param[in] sockaddr_str 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_sockaddr_str_to_in6_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in6_addr *dst) +{ + int rc; + if (!sockaddr_str) + return -EINVAL; + if (!dst) + return -ENOSPC; + if (sockaddr_str->af != AF_INET6) + return -EINVAL; + rc = inet_pton(AF_INET6, sockaddr_str->ip, dst); + if (rc != 1) + return -EINVAL; + return 0; +} + +/*! Convert osmo_sockaddr_str address string to IPv4 address data in host-byte-order. + * \param[in] sockaddr_str 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_sockaddr_str_to_32(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip) +{ + int rc; + struct in_addr addr; + if (!sockaddr_str) + return -EINVAL; + if (!ip) + return -ENOSPC; + rc = osmo_sockaddr_str_to_in_addr(sockaddr_str, &addr); + if (rc) + return rc; + *ip = addr.s_addr; + return 0; +} + +/*! Convert osmo_sockaddr_str address string to IPv4 address data in network-byte-order. + * \param[in] sockaddr_str 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_sockaddr_str_to_32n(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip) +{ + int rc; + uint32_t ip_h; + if (!sockaddr_str) + return -EINVAL; + if (!ip) + return -ENOSPC; + rc = osmo_sockaddr_str_to_32(sockaddr_str, &ip_h); + if (rc) + return rc; + *ip = osmo_htonl(ip_h); + return 0; +} + +/*! Convert osmo_sockaddr_str address string and port to IPv4 address and port data. + * \param[in] sockaddr_str 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_sockaddr_str_to_sockaddr_in(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in *dst) +{ + if (!sockaddr_str) + return -EINVAL; + if (!dst) + return -ENOSPC; + if (sockaddr_str->af != AF_INET) + return -EINVAL; + *dst = (struct sockaddr_in){ + .sin_family = sockaddr_str->af, + .sin_port = osmo_htons(sockaddr_str->port), + }; + return osmo_sockaddr_str_to_in_addr(sockaddr_str, &dst->sin_addr); +} + +/*! Convert osmo_sockaddr_str address string and port to IPv6 address and port data. + * \param[in] sockaddr_str 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_sockaddr_str_to_sockaddr_in6(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in6 *dst) +{ + if (!sockaddr_str) + return -EINVAL; + if (!dst) + return -ENOSPC; + if (sockaddr_str->af != AF_INET6) + return -EINVAL; + *dst = (struct sockaddr_in6){ + .sin6_family = sockaddr_str->af, + .sin6_port = osmo_htons(sockaddr_str->port), + }; + return osmo_sockaddr_str_to_in6_addr(sockaddr_str, &dst->sin6_addr); +} + +/*! Convert osmo_sockaddr_str address string and port to IPv4 or IPv6 address and port data. + * Depending on sockaddr_str->af, dst will be handled as struct sockaddr_in or struct sockaddr_in6. + * \param[in] sockaddr_str 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 sockaddr_str->af). + */ +int osmo_sockaddr_str_to_sockaddr(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_storage *dst) +{ + if (!sockaddr_str) + return -EINVAL; + if (!dst) + return -ENOSPC; + switch (sockaddr_str->af) { + case AF_INET: + return osmo_sockaddr_str_to_sockaddr_in(sockaddr_str, (void*)dst); + case AF_INET6: + return osmo_sockaddr_str_to_sockaddr_in6(sockaddr_str, (void*)dst); + default: + return -EINVAL; + } +} + +/*! @} */ +#endif // HAVE_NETINET_IN_H diff --git a/tests/Makefile.am b/tests/Makefile.am index ab3728f0..88bcd7e9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -30,6 +30,7 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \ tdef/tdef_test tdef/tdef_vty_test_config_root \ tdef/tdef_vty_test_config_subnode \ tdef/tdef_vty_test_dynamic \ + sockaddr_str/sockaddr_str_test \ $(NULL) if ENABLE_MSGFILE @@ -236,6 +237,9 @@ tdef_tdef_vty_test_config_subnode_LDADD = $(LDADD) $(top_builddir)/src/vty/libos tdef_tdef_vty_test_dynamic_SOURCES = tdef/tdef_vty_test_dynamic.c tdef_tdef_vty_test_dynamic_LDADD = $(LDADD) $(top_builddir)/src/vty/libosmovty.la +sockaddr_str_sockaddr_str_test_SOURCES = sockaddr_str/sockaddr_str_test.c +sockaddr_str_sockaddr_str_test_LDADD = $(LDADD) + # The `:;' works around a Bash 3.2 bug when the output is not writeable. $(srcdir)/package.m4: $(top_srcdir)/configure.ac :;{ \ @@ -304,6 +308,7 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \ tdef/tdef_vty_test_config_root.vty \ tdef/tdef_vty_test_config_subnode.vty \ tdef/tdef_vty_test_dynamic.vty \ + sockaddr_str/sockaddr_str_test.ok \ $(NULL) DISTCLEANFILES = atconfig atlocal conv/gsm0503_test_vectors.c diff --git a/tests/sockaddr_str/sockaddr_str_test.c b/tests/sockaddr_str/sockaddr_str_test.c new file mode 100644 index 00000000..d2e7944d --- /dev/null +++ b/tests/sockaddr_str/sockaddr_str_test.c @@ -0,0 +1,239 @@ +/* tests for osmo_sockaddr_str API of libmsomcore */ +/* + * (C) 2019 by sysmocom - s.f.m.c. GmbH + * + * Author: neels@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 +#include +#include +#include +#include +#include + +struct osmo_sockaddr_str 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_sockaddr_str *oip) +{ + printf("{ .af = %s, .ip = %s, .port = %u }\n", af_name(oip->af), osmo_quote_str(oip->ip, -1), oip->port); +} + +void sockaddr_str_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_sockaddr_str *x = &oip_data[i]; + int rc; + printf("\n\n"); + dump_oip(x); + + printf(" osmo_sockaddr_str_is_set() = %s\n", osmo_sockaddr_str_is_set(x) ? "true" : "false"); + + { + struct in_addr a = {}; + + rc = osmo_sockaddr_str_to_in_addr(x, &a); + printf(" osmo_sockaddr_str_to_in_addr() %s in_addr=%s\n", rc_name(rc), hexdump(a)); + + if (rc == 0) { + struct osmo_sockaddr_str back; + rc = osmo_sockaddr_str_from_in_addr(&back, &a, x->port); + printf(" -> osmo_sockaddr_str_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_sockaddr_str_to_in6_addr(x, &a); + printf(" osmo_sockaddr_str_to_in6_addr() %s in6_addr=%s\n", rc_name(rc), hexdump(a)); + + if (rc == 0) { + struct osmo_sockaddr_str back; + rc = osmo_sockaddr_str_from_in6_addr(&back, &a, x->port); + printf(" -> osmo_sockaddr_str_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_sockaddr_str_to_32(x, &a); + printf(" osmo_sockaddr_str_to_32() %s uint32_t=0x%x\n", rc_name(rc), a); + + if (rc == 0) { + struct osmo_sockaddr_str back; + rc = osmo_sockaddr_str_from_32(&back, a, x->port); + printf(" -> osmo_sockaddr_str_from_32() %s ", rc_name(rc)); + dump_oip(&back); + if (memcmp(x, &back, sizeof(back))) + printf(" DIFFERS!\n"); + } + } + + { + uint32_t a = 0; + + rc = osmo_sockaddr_str_to_32n(x, &a); + printf(" osmo_sockaddr_str_to_32n() %s uint32_t=0x%x\n", rc_name(rc), a); + + if (rc == 0) { + struct osmo_sockaddr_str back; + rc = osmo_sockaddr_str_from_32n(&back, a, x->port); + printf(" -> osmo_sockaddr_str_from_32n() %s ", rc_name(rc)); + dump_oip(&back); + if (memcmp(x, &back, sizeof(back))) + printf(" DIFFERS!\n"); + } + } + + { + struct sockaddr_in a = {}; + + rc = osmo_sockaddr_str_to_sockaddr_in(x, &a); + printf(" osmo_sockaddr_str_to_sockaddr_in() %s sockaddr_in=%s\n", rc_name(rc), hexdump(a)); + + if (rc == 0) { + struct osmo_sockaddr_str back; + rc = osmo_sockaddr_str_from_sockaddr_in(&back, &a); + printf(" -> osmo_sockaddr_str_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_sockaddr_str_to_sockaddr_in6(x, &a); + printf(" osmo_sockaddr_str_to_sockaddr_in6() %s sockaddr_in6=%s\n", rc_name(rc), hexdump(a)); + + if (rc == 0) { + struct osmo_sockaddr_str back; + rc = osmo_sockaddr_str_from_sockaddr_in6(&back, &a); + printf(" -> osmo_sockaddr_str_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_sockaddr_str_to_sockaddr(x, &a); + printf(" osmo_sockaddr_str_to_sockaddr() %s sockaddr_storage=%s\n", rc_name(rc), hexdump(a)); + + if (rc == 0) { + struct osmo_sockaddr_str back; + rc = osmo_sockaddr_str_from_sockaddr(&back, &a); + printf(" -> osmo_sockaddr_str_from_sockaddr() %s ", rc_name(rc)); + dump_oip(&back); + if (memcmp(x, &back, sizeof(back))) + printf(" DIFFERS!\n"); + } + } + + { + struct osmo_sockaddr_str from_str; + rc = osmo_sockaddr_str_from_str(&from_str, x->ip, x->port); + printf(" osmo_sockaddr_str_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) +{ + sockaddr_str_test_conversions(); + return 0; +} + diff --git a/tests/sockaddr_str/sockaddr_str_test.ok b/tests/sockaddr_str/sockaddr_str_test.ok new file mode 100644 index 00000000..d69314d4 --- /dev/null +++ b/tests/sockaddr_str/sockaddr_str_test.ok @@ -0,0 +1,288 @@ + + +{ .af = AF_INET, .ip = "1.2.3.4", .port = 5 } + osmo_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc == 0 in_addr=01020304 + -> osmo_sockaddr_str_from_in_addr() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 } + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc == 0 uint32_t=0x4030201 + -> osmo_sockaddr_str_from_32() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 } + osmo_sockaddr_str_to_32n() rc == 0 uint32_t=0x1020304 + -> osmo_sockaddr_str_from_32n() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 } + osmo_sockaddr_str_to_sockaddr_in() rc == 0 sockaddr_in=02000005010203040000000000000000 + -> osmo_sockaddr_str_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 } + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0200000501020304000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 } + osmo_sockaddr_str_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_sockaddr_str_is_set() = false + osmo_sockaddr_str_to_in_addr() rc == 0 in_addr=00000000 + -> osmo_sockaddr_str_from_in_addr() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 } + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc == 0 uint32_t=0x0 + -> osmo_sockaddr_str_from_32() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 } + osmo_sockaddr_str_to_32n() rc == 0 uint32_t=0x0 + -> osmo_sockaddr_str_from_32n() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 } + osmo_sockaddr_str_to_sockaddr_in() rc == 0 sockaddr_in=02000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 } + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 0 } + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc == 0 in_addr=ffffffff + -> osmo_sockaddr_str_from_in_addr() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 } + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc == 0 uint32_t=0xffffffff + -> osmo_sockaddr_str_from_32() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 } + osmo_sockaddr_str_to_32n() rc == 0 uint32_t=0xffffffff + -> osmo_sockaddr_str_from_32n() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 } + osmo_sockaddr_str_to_sockaddr_in() rc == 0 sockaddr_in=0200ffffffffffff0000000000000000 + -> osmo_sockaddr_str_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 } + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0200ffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET, .ip = "255.255.255.255", .port = 65535 } + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=02000001000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0200000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=02000001000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0200000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc == 0 in6_addr=00010002000300000000000000000004 + -> osmo_sockaddr_str_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 } + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc == 0 sockaddr_in6=0a000005000000000001000200030000000000000000000400000000 + -> osmo_sockaddr_str_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 } + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0a00000500000000000100020003000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 } + osmo_sockaddr_str_from_str() rc == 0 { .af = AF_INET6, .ip = "1:2:3::4", .port = 5 } + + +{ .af = AF_INET6, .ip = "::", .port = 0 } + osmo_sockaddr_str_is_set() = false + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc == 0 in6_addr=00000000000000000000000000000000 + -> osmo_sockaddr_str_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "::", .port = 0 } + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc == 0 sockaddr_in6=0a000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "::", .port = 0 } + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "::", .port = 0 } + osmo_sockaddr_str_from_str() rc == 0 { .af = AF_INET6, .ip = "::", .port = 0 } + + +{ .af = AF_INET6, .ip = "::1", .port = 0 } + osmo_sockaddr_str_is_set() = false + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc == 0 in6_addr=00000000000000000000000000000001 + -> osmo_sockaddr_str_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "::1", .port = 0 } + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc == 0 sockaddr_in6=0a000000000000000000000000000000000000000000000100000000 + -> osmo_sockaddr_str_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "::1", .port = 0 } + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0a00000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "::1", .port = 0 } + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc == 0 in6_addr=ffffffffffffffffffffffffffffffff + -> osmo_sockaddr_str_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 } + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc == 0 sockaddr_in6=0a00ffff00000000ffffffffffffffffffffffffffffffff00000000 + -> osmo_sockaddr_str_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 } + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0a00ffff00000000ffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 } + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc == 0 in6_addr=ffffffffffffffffffffffffffffffff + -> osmo_sockaddr_str_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 } + DIFFERS! + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc == 0 sockaddr_in6=0a00ffff00000000ffffffffffffffffffffffffffffffff00000000 + -> osmo_sockaddr_str_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 } + DIFFERS! + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0a00ffff00000000ffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 } + DIFFERS! + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=0a000001000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0a00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_from_str() rc == 0 { .af = AF_INET6, .ip = "::fffff", .port = 1 } + + +{ .af = AF_INET6, .ip = "not an ip address", .port = 1 } + osmo_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=0a000001000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0a00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=0a000005000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0a00000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=02000005000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0200000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_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_sockaddr_str_is_set() = false + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_from_str() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 5 } + DIFFERS! + + +{ .af = AF_INET, .ip = "", .port = 5 } + osmo_sockaddr_str_is_set() = false + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=02000005000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0200000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_from_str() rc < 0 { .af = AF_UNSPEC, .ip = "", .port = 5 } + + +{ .af = AF_INET6, .ip = "", .port = 5 } + osmo_sockaddr_str_is_set() = false + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=0a000005000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0a00000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_from_str() rc < 0 { .af = AF_UNSPEC, .ip = "", .port = 5 } + + +{ .af = AF_INET, .ip = "1.2.3.4", .port = 0 } + osmo_sockaddr_str_is_set() = false + osmo_sockaddr_str_to_in_addr() rc == 0 in_addr=01020304 + -> osmo_sockaddr_str_from_in_addr() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 } + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc == 0 uint32_t=0x4030201 + -> osmo_sockaddr_str_from_32() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 } + osmo_sockaddr_str_to_32n() rc == 0 uint32_t=0x1020304 + -> osmo_sockaddr_str_from_32n() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 } + osmo_sockaddr_str_to_sockaddr_in() rc == 0 sockaddr_in=02000000010203040000000000000000 + -> osmo_sockaddr_str_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 } + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0200000001020304000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET, .ip = "1.2.3.4", .port = 0 } + osmo_sockaddr_str_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_sockaddr_str_is_set() = false + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc < 0 in6_addr=00000000000000000000000000000000 + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=02000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc < 0 sockaddr_in6=00000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr() rc < 0 sockaddr_storage=0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + osmo_sockaddr_str_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_sockaddr_str_is_set() = true + osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000 + osmo_sockaddr_str_to_in6_addr() rc == 0 in6_addr=0000000000000000000000010a090807 + -> osmo_sockaddr_str_from_in6_addr() rc == 0 { .af = AF_INET6, .ip = "::1:a09:807", .port = 1 } + DIFFERS! + osmo_sockaddr_str_to_32() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_32n() rc < 0 uint32_t=0x0 + osmo_sockaddr_str_to_sockaddr_in() rc < 0 sockaddr_in=00000000000000000000000000000000 + osmo_sockaddr_str_to_sockaddr_in6() rc == 0 sockaddr_in6=0a000001000000000000000000000000000000010a09080700000000 + -> osmo_sockaddr_str_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "::1:a09:807", .port = 1 } + DIFFERS! + osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0a000001000000000000000000000000000000010a0908070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "::1:a09:807", .port = 1 } + DIFFERS! + osmo_sockaddr_str_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 0093403a..db2003f4 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -331,3 +331,9 @@ AT_KEYWORDS([tdef]) cat $abs_srcdir/tdef/tdef_test.ok > expout AT_CHECK([$abs_top_builddir/tests/tdef/tdef_test], [0], [expout], [ignore]) AT_CLEANUP + +AT_SETUP([sockaddr_str]) +AT_KEYWORDS([sockaddr_str]) +cat $abs_srcdir/sockaddr_str/sockaddr_str_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/sockaddr_str/sockaddr_str_test], [0], [expout], [ignore]) +AT_CLEANUP -- cgit v1.2.3