aboutsummaryrefslogtreecommitdiffhomepage
path: root/node_modules/uws/src/Socket.cpp
blob: c35bbf8d9c23572d3d93edd08c3520ec010b79b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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"};
    }
}

}