Skip to content
Snippets Groups Projects
Commit 50f75532 authored by Recolic Keghart's avatar Recolic Keghart
Browse files

get peer name in sio

parent 344b3197
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netdb.h> #include <netdb.h>
#include <fcntl.h> #include <fcntl.h>
#include <arpa/inet.h>
#endif #endif
// Include winsock2.h before windows.h // Include winsock2.h before windows.h
...@@ -34,6 +35,36 @@ namespace rlib { ...@@ -34,6 +35,36 @@ namespace rlib {
throw std::runtime_error("accept failed. errno = {}"_format(strerror(errno))); throw std::runtime_error("accept failed. errno = {}"_format(strerror(errno)));
return res; return res;
} }
static inline std::pair<std::string, uint16_t> get_peer_name(sockfd_t sock) {
struct sockaddr_storage addr;
socklen_t addrlen = sizeof(addr);
auto res = getpeername(sock, (struct sockaddr *)&addr, &addrlen);
if(res == -1)
throw std::runtime_error("getpeername failed. errno = {}"_format(strerror(errno)));
std::string ipstr;
uint16_t port = 0;
if(addr.ss_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
char str[INET_ADDRSTRLEN];
auto res = inet_ntop(AF_INET, &s->sin_addr, str, INET_ADDRSTRLEN);
if(res == NULL)
throw std::runtime_error("inet_ntop failed. errno = {}"_format(strerror(errno)));
port = ntohs(s->sin_port);
ipstr = str;
}
else {
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
char str[INET6_ADDRSTRLEN];
auto res = inet_ntop(AF_INET6, &s->sin6_addr, str, INET6_ADDRSTRLEN);
if(res == NULL)
throw std::runtime_error("inet_ntop failed. errno = {}"_format(strerror(errno)));
port = ntohs(s->sin6_port);
ipstr = std::string() + '[' + str + ']';
}
return {ipstr, port};
}
#if RLIB_OS_ID != OS_WINDOWS #if RLIB_OS_ID != OS_WINDOWS
namespace impl { namespace impl {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment