summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2019-10-05 05:12:33 +0200
committerNeels Hofmeyr <neels@hofmeyr.de>2019-11-01 14:33:58 +0100
commit2ceb758ba4a845898fbff11bd4942751078b8ee7 (patch)
tree3ae464646ef28312ddbd6f2ced625622a43ab7da
parentab6d6cf3aa5ca75f4b110ac4282a07adcc981e59 (diff)
add osmo_sockaddr_str_is_nonzero()
Often, an IP address of 0.0.0.0 is considered an unset value (for clients requiring a server address; not for listening on "any"). osmo_sockaddr_str_is_set() does return false when the port is 0, but there is no simple way to tell whether the IP address is actually set to a server address. Add osmo_sockaddr_str_is_nonzero() to return false if: - the port is zero, or - the IP address is zero (0.0.0.0 or ::0), or - the IP address cannot be parsed. A practical use example: osmo-msc so far accepts an RTP IP address of 0.0.0.0 as valid. I noticed when trying to trigger error handling from a ttcn3 test. osmo-msc can use this function to reject invalid addresses from MGCP messages. Related: I53ddb19a70fda3deb906464e1b89c12d9b4c7cbd (osmo-msc) Change-Id: I73cbcab90cffcdc9a5f8d5281c57c1f87b2c3550
-rw-r--r--include/osmocom/core/sockaddr_str.h1
-rw-r--r--src/sockaddr_str.c29
-rw-r--r--tests/sockaddr_str/sockaddr_str_test.c3
-rw-r--r--tests/sockaddr_str/sockaddr_str_test.ok54
4 files changed, 87 insertions, 0 deletions
diff --git a/include/osmocom/core/sockaddr_str.h b/include/osmocom/core/sockaddr_str.h
index d16dc86f..d47b2a4e 100644
--- a/include/osmocom/core/sockaddr_str.h
+++ b/include/osmocom/core/sockaddr_str.h
@@ -65,6 +65,7 @@ struct osmo_sockaddr_str {
#define OSMO_SOCKADDR_STR_FMT_ARGS(R) ((R)? (R)->ip : "NULL"), ((R)? (R)->port : 0)
bool osmo_sockaddr_str_is_set(const struct osmo_sockaddr_str *sockaddr_str);
+bool osmo_sockaddr_str_is_nonzero(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);
diff --git a/src/sockaddr_str.c b/src/sockaddr_str.c
index d683c7d2..f523050c 100644
--- a/src/sockaddr_str.c
+++ b/src/sockaddr_str.c
@@ -66,6 +66,35 @@ bool osmo_sockaddr_str_is_set(const struct osmo_sockaddr_str *sockaddr_str)
&& (sockaddr_str->af == AF_INET || sockaddr_str->af == AF_INET6);
}
+/*! Return true if IP and port are valid and nonzero.
+ * \param[in] sockaddr_str The instance to examine.
+ * \return True iff ip can be converted to a nonzero IP address, and port is not 0.
+ */
+bool osmo_sockaddr_str_is_nonzero(const struct osmo_sockaddr_str *sockaddr_str)
+{
+ uint32_t ipv4;
+ struct in6_addr ipv6_zero = {};
+ struct in6_addr ipv6;
+
+ if (!osmo_sockaddr_str_is_set(sockaddr_str))
+ return false;
+
+ switch (sockaddr_str->af) {
+ case AF_INET:
+ if (osmo_sockaddr_str_to_32(sockaddr_str, &ipv4))
+ return false;
+ return ipv4 != 0;
+
+ case AF_INET6:
+ if (osmo_sockaddr_str_to_in6_addr(sockaddr_str, &ipv6))
+ return false;
+ return memcmp(&ipv6, &ipv6_zero, sizeof(ipv6)) != 0;
+
+ default:
+ return false;
+ }
+}
+
/*! 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
diff --git a/tests/sockaddr_str/sockaddr_str_test.c b/tests/sockaddr_str/sockaddr_str_test.c
index d2e7944d..4284387b 100644
--- a/tests/sockaddr_str/sockaddr_str_test.c
+++ b/tests/sockaddr_str/sockaddr_str_test.c
@@ -53,6 +53,8 @@ struct osmo_sockaddr_str oip_data[] = {
{ .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 },
+ { .af = AF_INET, .ip = "0.0.0.0", .port = 5 },
+ { .af = AF_INET6, .ip = "::", .port = 5 },
};
const char *af_name(int af)
@@ -106,6 +108,7 @@ void sockaddr_str_test_conversions()
dump_oip(x);
printf(" osmo_sockaddr_str_is_set() = %s\n", osmo_sockaddr_str_is_set(x) ? "true" : "false");
+ printf(" osmo_sockaddr_str_is_nonzero() = %s\n", osmo_sockaddr_str_is_nonzero(x) ? "true" : "false");
{
struct in_addr a = {};
diff --git a/tests/sockaddr_str/sockaddr_str_test.ok b/tests/sockaddr_str/sockaddr_str_test.ok
index d69314d4..781e9d10 100644
--- a/tests/sockaddr_str/sockaddr_str_test.ok
+++ b/tests/sockaddr_str/sockaddr_str_test.ok
@@ -2,6 +2,7 @@
{ .af = AF_INET, .ip = "1.2.3.4", .port = 5 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -19,6 +20,7 @@
{ .af = AF_INET, .ip = "0.0.0.0", .port = 0 }
osmo_sockaddr_str_is_set() = false
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -36,6 +38,7 @@
{ .af = AF_INET, .ip = "255.255.255.255", .port = 65535 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -53,6 +56,7 @@
{ .af = AF_INET, .ip = "0.0.0.256", .port = 1 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -65,6 +69,7 @@
{ .af = AF_INET, .ip = "not an ip address", .port = 1 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -77,6 +82,7 @@
{ .af = AF_INET6, .ip = "1:2:3::4", .port = 5 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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 }
@@ -92,6 +98,7 @@
{ .af = AF_INET6, .ip = "::", .port = 0 }
osmo_sockaddr_str_is_set() = false
+ osmo_sockaddr_str_is_nonzero() = 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 }
@@ -107,6 +114,7 @@
{ .af = AF_INET6, .ip = "::1", .port = 0 }
osmo_sockaddr_str_is_set() = false
+ osmo_sockaddr_str_is_nonzero() = 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 }
@@ -122,6 +130,7 @@
{ .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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 }
@@ -137,6 +146,7 @@
{ .af = AF_INET6, .ip = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", .port = 65535 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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 }
@@ -155,6 +165,7 @@
{ .af = AF_INET6, .ip = "::fffff", .port = 1 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -167,6 +178,7 @@
{ .af = AF_INET6, .ip = "not an ip address", .port = 1 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -179,6 +191,7 @@
{ .af = AF_INET6, .ip = "1.2.3.4", .port = 5 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -192,6 +205,7 @@
{ .af = AF_INET, .ip = "1:2:3::4", .port = 5 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -205,6 +219,7 @@
{ .af = AF_UNSPEC, .ip = "1.2.3.4", .port = 5 }
osmo_sockaddr_str_is_set() = false
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -218,6 +233,7 @@
{ .af = AF_INET, .ip = "", .port = 5 }
osmo_sockaddr_str_is_set() = false
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -230,6 +246,7 @@
{ .af = AF_INET6, .ip = "", .port = 5 }
osmo_sockaddr_str_is_set() = false
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -242,6 +259,7 @@
{ .af = AF_INET, .ip = "1.2.3.4", .port = 0 }
osmo_sockaddr_str_is_set() = false
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -259,6 +277,7 @@
{ .af = AF_INET, .ip = "1.2.3:4:5", .port = 0 }
osmo_sockaddr_str_is_set() = false
+ osmo_sockaddr_str_is_nonzero() = 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
@@ -272,6 +291,7 @@
{ .af = AF_INET6, .ip = "::1:10.9.8.7", .port = 1 }
osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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 }
@@ -286,3 +306,37 @@
-> 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 }
+
+
+{ .af = AF_INET, .ip = "0.0.0.0", .port = 5 }
+ osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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 = 5 }
+ 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 = 5 }
+ 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 = 5 }
+ osmo_sockaddr_str_to_sockaddr_in() rc == 0 sockaddr_in=02000005000000000000000000000000
+ -> osmo_sockaddr_str_from_sockaddr_in() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 5 }
+ 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_sockaddr() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 5 }
+ osmo_sockaddr_str_from_str() rc == 0 { .af = AF_INET, .ip = "0.0.0.0", .port = 5 }
+
+
+{ .af = AF_INET6, .ip = "::", .port = 5 }
+ osmo_sockaddr_str_is_set() = true
+ osmo_sockaddr_str_is_nonzero() = 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 = 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=0a000005000000000000000000000000000000000000000000000000
+ -> osmo_sockaddr_str_from_sockaddr_in6() rc == 0 { .af = AF_INET6, .ip = "::", .port = 5 }
+ osmo_sockaddr_str_to_sockaddr() rc == 0 sockaddr_storage=0a00000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ -> osmo_sockaddr_str_from_sockaddr() rc == 0 { .af = AF_INET6, .ip = "::", .port = 5 }
+ osmo_sockaddr_str_from_str() rc == 0 { .af = AF_INET6, .ip = "::", .port = 5 }