summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/osmocom/core/sockaddr_str.h8
-rw-r--r--tests/sockaddr_str/sockaddr_str_test.ok22
2 files changed, 17 insertions, 13 deletions
diff --git a/include/osmocom/core/sockaddr_str.h b/include/osmocom/core/sockaddr_str.h
index d47b2a4e..6dd428c7 100644
--- a/include/osmocom/core/sockaddr_str.h
+++ b/include/osmocom/core/sockaddr_str.h
@@ -61,8 +61,12 @@ struct osmo_sockaddr_str {
* 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)? (R)->ip : "NULL"), ((R)? (R)->port : 0)
+#define OSMO_SOCKADDR_STR_FMT "%s%s%s:%u"
+#define OSMO_SOCKADDR_STR_FMT_ARGS(R) \
+ ((R) && (R)->af == AF_INET6)? "[" : "", \
+ (R)? (R)->ip : "NULL", \
+ ((R) && (R)->af == AF_INET6)? "]" : "", \
+ (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);
diff --git a/tests/sockaddr_str/sockaddr_str_test.ok b/tests/sockaddr_str/sockaddr_str_test.ok
index 5ebf7be6..bc18225d 100644
--- a/tests/sockaddr_str/sockaddr_str_test.ok
+++ b/tests/sockaddr_str/sockaddr_str_test.ok
@@ -86,7 +86,7 @@
{ .af = AF_INET6, .ip = "1:2:3::4", .port = 5 }
- OSMO_SOCKADDR_STR_FMT: '1:2:3::4:5'
+ OSMO_SOCKADDR_STR_FMT: '[1:2:3::4]:5'
osmo_sockaddr_str_is_set() = true
osmo_sockaddr_str_is_nonzero() = true
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -103,7 +103,7 @@
{ .af = AF_INET6, .ip = "::", .port = 0 }
- OSMO_SOCKADDR_STR_FMT: ':::0'
+ OSMO_SOCKADDR_STR_FMT: '[::]:0'
osmo_sockaddr_str_is_set() = false
osmo_sockaddr_str_is_nonzero() = false
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -120,7 +120,7 @@
{ .af = AF_INET6, .ip = "::1", .port = 0 }
- OSMO_SOCKADDR_STR_FMT: '::1:0'
+ OSMO_SOCKADDR_STR_FMT: '[::1]:0'
osmo_sockaddr_str_is_set() = false
osmo_sockaddr_str_is_nonzero() = false
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -137,7 +137,7 @@
{ .af = AF_INET6, .ip = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", .port = 65535 }
- OSMO_SOCKADDR_STR_FMT: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:65535'
+ OSMO_SOCKADDR_STR_FMT: '[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535'
osmo_sockaddr_str_is_set() = true
osmo_sockaddr_str_is_nonzero() = true
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -154,7 +154,7 @@
{ .af = AF_INET6, .ip = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", .port = 65535 }
- OSMO_SOCKADDR_STR_FMT: 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:65535'
+ OSMO_SOCKADDR_STR_FMT: '[FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:65535'
osmo_sockaddr_str_is_set() = true
osmo_sockaddr_str_is_nonzero() = true
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -174,7 +174,7 @@
{ .af = AF_INET6, .ip = "::fffff", .port = 1 }
- OSMO_SOCKADDR_STR_FMT: '::fffff:1'
+ OSMO_SOCKADDR_STR_FMT: '[::fffff]:1'
osmo_sockaddr_str_is_set() = true
osmo_sockaddr_str_is_nonzero() = false
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -188,7 +188,7 @@
{ .af = AF_INET6, .ip = "not an ip address", .port = 1 }
- OSMO_SOCKADDR_STR_FMT: 'not an ip address:1'
+ OSMO_SOCKADDR_STR_FMT: '[not an ip address]:1'
osmo_sockaddr_str_is_set() = true
osmo_sockaddr_str_is_nonzero() = false
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -202,7 +202,7 @@
{ .af = AF_INET6, .ip = "1.2.3.4", .port = 5 }
- OSMO_SOCKADDR_STR_FMT: '1.2.3.4:5'
+ OSMO_SOCKADDR_STR_FMT: '[1.2.3.4]:5'
osmo_sockaddr_str_is_set() = true
osmo_sockaddr_str_is_nonzero() = false
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -261,7 +261,7 @@
{ .af = AF_INET6, .ip = "", .port = 5 }
- OSMO_SOCKADDR_STR_FMT: ':5'
+ OSMO_SOCKADDR_STR_FMT: '[]:5'
osmo_sockaddr_str_is_set() = false
osmo_sockaddr_str_is_nonzero() = false
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -309,7 +309,7 @@
{ .af = AF_INET6, .ip = "::1:10.9.8.7", .port = 1 }
- OSMO_SOCKADDR_STR_FMT: '::1:10.9.8.7:1'
+ OSMO_SOCKADDR_STR_FMT: '[::1:10.9.8.7]:1'
osmo_sockaddr_str_is_set() = true
osmo_sockaddr_str_is_nonzero() = true
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000
@@ -348,7 +348,7 @@
{ .af = AF_INET6, .ip = "::", .port = 5 }
- OSMO_SOCKADDR_STR_FMT: ':::5'
+ OSMO_SOCKADDR_STR_FMT: '[::]:5'
osmo_sockaddr_str_is_set() = true
osmo_sockaddr_str_is_nonzero() = false
osmo_sockaddr_str_to_in_addr() rc < 0 in_addr=00000000