Skip to content

Commit b94a025

Browse files
committed
connection: ensure IPv6 literals are surrounded by []
Fixes: #11558 Signed-off-by: Per von Zweigbergk <pvz@pvz.pp.se>
1 parent acad02a commit b94a025

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

include/fluent-bit/flb_connection.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636

3737
/* FLB_CONNECTION_MAX_LABEL_LENGTH is the maximum length of
3838
* any of the following variants plus an optional colon if
39-
* the spec includes a port number :
39+
* the spec includes a port number, as well as enclosing
40+
* square brackets for IPv6 addresses :
4041
*
41-
* udp://
42-
* tcp://
43-
* unix://
42+
* udp:// (6 for prefix + 1 for colon + 2 for brackets = 9 total)
43+
* tcp:// (6 for prefix + 1 for colon + 2 for brackets = 9 total)
44+
* unix:// (7 for prefix)
4445
*/
45-
#define FLB_CONNECTION_MAX_LABEL_LENGTH 7
46+
#define FLB_CONNECTION_MAX_LABEL_LENGTH 9
4647

4748
#define FLB_CONNECTION_MAX_IPV4_ADDRESS_LENGTH 15
4849
#define FLB_CONNECTION_MAX_IPV6_ADDRESS_LENGTH 39

src/flb_connection.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <assert.h>
2+
#include <string.h>
23

34
#include <fluent-bit/flb_connection.h>
45
#include <fluent-bit/flb_upstream.h>
@@ -89,17 +90,23 @@ static void compose_user_friendly_remote_host(struct flb_connection *connection)
8990

9091
connection_type = connection->stream->transport;
9192

92-
if (connection_type == FLB_TRANSPORT_TCP) {
93-
snprintf(connection->user_friendly_remote_host,
94-
sizeof(connection->user_friendly_remote_host),
95-
"tcp://%s:%u",
96-
connection->remote_host,
97-
connection->remote_port);
98-
}
99-
else if (connection_type == FLB_TRANSPORT_UDP) {
93+
if (connection_type == FLB_TRANSPORT_TCP ||
94+
connection_type == FLB_TRANSPORT_UDP) {
95+
const char *fmt;
96+
97+
/* If the address is an IPv6 literal, we need to
98+
* enclose the address in square brackets.
99+
*/
100+
if (strchr(connection->remote_host, ':') != NULL) {
101+
fmt = "%s://[%s]:%u";
102+
}
103+
else {
104+
fmt = "%s://%s:%u";
105+
}
100106
snprintf(connection->user_friendly_remote_host,
101107
sizeof(connection->user_friendly_remote_host),
102-
"udp://%s:%u",
108+
fmt,
109+
connection_type == FLB_TRANSPORT_TCP ? "tcp" : "udp",
103110
connection->remote_host,
104111
connection->remote_port);
105112
}

0 commit comments

Comments
 (0)