From 67fdec20726e48ba3a934cb25bb30d47ec4a4f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20De=20La=20Pe=C3=B1a=20Smirnov?= Date: Wed, 29 Nov 2017 11:44:34 +0300 Subject: Initial commit, version 0.5.3 --- node_modules/uws/src/Socket.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 node_modules/uws/src/Socket.cpp (limited to 'node_modules/uws/src/Socket.cpp') diff --git a/node_modules/uws/src/Socket.cpp b/node_modules/uws/src/Socket.cpp new file mode 100644 index 0000000..c35bbf8 --- /dev/null +++ b/node_modules/uws/src/Socket.cpp @@ -0,0 +1,28 @@ +#include "Socket.h" + +namespace uS { + +Socket::Address Socket::getAddress() +{ + uv_os_sock_t fd = getFd(); + + sockaddr_storage addr; + socklen_t addrLength = sizeof(addr); + if (getpeername(fd, (sockaddr *) &addr, &addrLength) == -1) { + return {0, "", ""}; + } + + static __thread char buf[INET6_ADDRSTRLEN]; + + if (addr.ss_family == AF_INET) { + sockaddr_in *ipv4 = (sockaddr_in *) &addr; + inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)); + return {ntohs(ipv4->sin_port), buf, "IPv4"}; + } else { + sockaddr_in6 *ipv6 = (sockaddr_in6 *) &addr; + inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)); + return {ntohs(ipv6->sin6_port), buf, "IPv6"}; + } +} + +} -- cgit v1.2.3