Skip to content

Commit 9bca73d

Browse files
tieliaomichalvasko
authored andcommitted
fix of a crash when a connection is made in background
1 parent 3341d1d commit 9bca73d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/session_client.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,8 @@ nc_sock_connect(const char *host, uint16_t port, int timeout_ms, struct nc_keepa
16341634
struct addrinfo hints, *res_list = NULL, *res;
16351635
char *buf, port_s[6]; /* length of string representation of short int */
16361636
void *addr;
1637+
struct sockaddr saddr;
1638+
socklen_t addr_len;
16371639

16381640
DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout_ms, sock);
16391641

@@ -1697,6 +1699,31 @@ nc_sock_connect(const char *host, uint16_t port, int timeout_ms, struct nc_keepa
16971699
/* try to get a connection with the pending socket */
16981700
assert(sock_pending);
16991701
sock = sock_connect(timeout_ms, sock_pending, NULL, ka);
1702+
1703+
if (sock > 0) {
1704+
if (getsockname(sock, (struct sockaddr *)&saddr, &addr_len)) {
1705+
ERR(NULL, "getsockname failed (%s).", strerror(errno));
1706+
goto error;
1707+
}
1708+
1709+
buf = malloc(INET6_ADDRSTRLEN);
1710+
if (!buf) {
1711+
ERRMEM;
1712+
goto error;
1713+
}
1714+
if (saddr.sa_family == AF_INET) {
1715+
addr = &((struct sockaddr_in *)&saddr)->sin_addr;
1716+
} else {
1717+
addr = &((struct sockaddr_in6 *)&saddr)->sin6_addr;
1718+
}
1719+
if (!inet_ntop(saddr.sa_family, addr, buf, INET6_ADDRSTRLEN)) {
1720+
ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno));
1721+
free(buf);
1722+
goto error;
1723+
}
1724+
1725+
*ip_host = buf;
1726+
}
17001727
}
17011728

17021729
return sock;

0 commit comments

Comments
 (0)