Skip to content

Commit

Permalink
network: fixed port byte order conversion
Browse files Browse the repository at this point in the history
network: fixed uninitialized address processing
network: added connection remote host set call before starting a
connection attempt

note: there's still a corner case where if a tcp connection attempt
times out while performing a dns query the remote host address will not
be available.

Signed-off-by: Leonardo Alminana <[email protected]>
  • Loading branch information
leonardo-albertovich authored and edsiper committed Sep 7, 2022
1 parent a5d5570 commit 6c117f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/fluent-bit/flb_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#define FLB_NETWORK_DEFAULT_BACKLOG_SIZE 128
#define FLB_NETWORK_UNIX_SOCKET_PEER_ADDRESS_TEMPLATE "pid_%s"
#define FLB_NETWORK_ADDRESS_UNAVAILABLE "unavailable"

/* FLB_NETWORK_MAX_UNIX_ADDRESS_LENGTH should be enough for the
* string "PID " plus the string form of a signed 32 bit integer
Expand Down
40 changes: 29 additions & 11 deletions src/flb_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,8 @@ flb_sockfd_t flb_net_tcp_connect(const char *host, unsigned long port,
u_conn->event.fd = fd;
}

flb_connection_set_remote_host(u_conn, rp->ai_addr);

/* Perform TCP connection */
if (is_async == FLB_TRUE) {
ret = net_connect_async(fd, rp->ai_addr, rp->ai_addrlen,
Expand Down Expand Up @@ -1767,15 +1769,19 @@ static int net_socket_get_peer_address(flb_sockfd_t fd,

static unsigned short int net_address_port(struct sockaddr_storage *address)
{
unsigned short int port;

if (address->ss_family == AF_INET) {
return ((struct sockaddr_in *) address)->sin_port;
port = ((struct sockaddr_in *) address)->sin_port;
}
else if (address->ss_family == AF_INET6) {
return ((struct sockaddr_in6 *) address)->sin6_port;
port = ((struct sockaddr_in6 *) address)->sin6_port;
}
else {
return 0;
port = 0;
}

return ntohs(port);
}

#ifdef FLB_HAVE_UNIX_SOCKET
Expand Down Expand Up @@ -1821,7 +1827,7 @@ static int net_address_unix_socket_peer_pid_raw(flb_sockfd_t fd,
#else
*output_data_size = snprintf(output_buffer,
output_buffer_size,
"unavailable");
FLB_NETWORK_ADDRESS_UNAVAILABLE);
#endif

return result;
Expand Down Expand Up @@ -1902,6 +1908,11 @@ static int net_address_ip_raw(flb_sockfd_t fd,

errno = 0;

if (address->ss_family == AF_UNSPEC) {
flb_debug("socket_ip_raw: uninitialized address");

return -1;
}
if (address->ss_family == AF_INET) {
address_data = ((char *) &((struct sockaddr_in *) address)->sin_addr);
address_size = sizeof(struct in_addr);
Expand All @@ -1919,7 +1930,7 @@ static int net_address_ip_raw(flb_sockfd_t fd,
&address_size);

if (result != 0) {
flb_error("socket_ip_raw: error getting client process pid");
flb_debug("socket_ip_raw: error getting client process pid");

return -1;
}
Expand All @@ -1928,14 +1939,14 @@ static int net_address_ip_raw(flb_sockfd_t fd,
}
#endif
else {
flb_error("socket_ip_raw: unsupported address type (%i)",
flb_debug("socket_ip_raw: unsupported address type (%i)",
address->ss_family);

return -1;
}

if (output_buffer_size < address_size) {
flb_error("socket_ip_raw: insufficient buffer size (%i < %zu)",
flb_debug("socket_ip_raw: insufficient buffer size (%i < %zu)",
output_buffer_size, address_size);

return -1;
Expand All @@ -1961,7 +1972,14 @@ static int net_address_ip_str(flb_sockfd_t fd,

errno = 0;

if (address->ss_family == AF_INET) {
if (address->ss_family == AF_UNSPEC) {
*output_data_size = snprintf(output_buffer,
output_buffer_size,
FLB_NETWORK_ADDRESS_UNAVAILABLE);

return 0;
}
else if (address->ss_family == AF_INET) {
address_data = (void *) &((struct sockaddr_in *) address)->sin_addr;
}
else if (address->ss_family == AF_INET6) {
Expand All @@ -1976,14 +1994,14 @@ static int net_address_ip_str(flb_sockfd_t fd,
output_data_size);

if (result != 0) {
flb_error("socket_ip_raw: error getting client process pid");
flb_debug("socket_ip_str: error getting client process pid");
}

return result;
}
#endif
else {
flb_error("socket_ip_raw: unsupported address type (%i)",
flb_debug("socket_ip_str: unsupported address type (%i)",
address->ss_family);

return -1;
Expand All @@ -1993,7 +2011,7 @@ static int net_address_ip_str(flb_sockfd_t fd,
address_data,
output_buffer,
output_buffer_size)) == NULL) {
flb_error("socket_ip_str: Can't get the IP text form (%i)", errno);
flb_debug("socket_ip_str: Can't get the IP text form (%i)", errno);

return -1;
}
Expand Down

0 comments on commit 6c117f4

Please sign in to comment.