Skip to content

Commit

Permalink
[fix] socket::addr
Browse files Browse the repository at this point in the history
  • Loading branch information
olegator77 committed Aug 5, 2019
1 parent 12fb0c9 commit 232c3d3
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cpp_src/net/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ int socket::create(string_view addr, struct addrinfo **presults) {
}

std::string socket::addr() const {
struct sockaddr saddr;
struct sockaddr_storage saddr;
struct sockaddr *paddr = reinterpret_cast<sockaddr *>(&saddr);
socklen_t len = sizeof(saddr);
if (::getpeername(fd_, &saddr, &len) != 0) {
if (::getpeername(fd_, paddr, &len) != 0) {
perror("getpeername");
return {};
}
char buf[INET_ADDRSTRLEN] = {};
getnameinfo(&saddr, len, buf, INET_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
getnameinfo(paddr, len, buf, INET_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
return buf;
}

Expand Down

0 comments on commit 232c3d3

Please sign in to comment.