THRIFT-3947 use sockaddr_storage with getsockname for future transport compatibility (ipv6)

Client: Lua

This closes #1127
This commit is contained in:
James E. King, III 2016-11-12 15:12:33 -05:00
parent fd832242bb
commit ea5ea8b4c8

View File

@ -131,20 +131,27 @@ T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len) {
} }
T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len) { T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len) {
struct sockaddr_in sa; struct sockaddr_storage sa;
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
socklen_t addrlen = sizeof(sa); socklen_t addrlen = sizeof(sa);
int rc = getsockname(*sock, (struct sockaddr*)&sa, &addrlen); int rc = getsockname(*sock, (struct sockaddr*)&sa, &addrlen);
if (!rc) { if (!rc) {
char *addr = inet_ntoa(sa.sin_addr); if (sa.ss_family == AF_INET6) {
*port = ntohs(sa.sin_port); struct sockaddr_in6* sin = (struct sockaddr_in6*)(&sa);
if (strlen(addr) < len) { if (!inet_ntop(AF_INET6, &sin->sin6_addr, buf, len)) {
len = strlen(addr); return errno;
}
*port = ntohs(sin->sin6_port);
} else {
struct sockaddr_in* sin = (struct sockaddr_in*)(&sa);
if (!inet_ntop(AF_INET, &sin->sin_addr, buf, len)) {
return errno;
}
*port = ntohs(sin->sin_port);
} }
memcpy(buf, addr, len);
return SUCCESS; return SUCCESS;
} }
return rc; return errno;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////