aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/endpoint.cpp
blob: b24d0baa217febd27367b0f12a286991c3e2c46f (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "endpoint.h"
#include "sock.h"

TEndpoint::TEndpoint(const TEndpoint::TAddrRef& addr)
    : Addr_(addr)
{
    const sockaddr* sa = Addr_->Addr();

    if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6 && sa->sa_family != AF_UNIX) { 
        ythrow yexception() << TStringBuf("endpoint can contain only ipv4, ipv6 or unix address");
    }
}

TEndpoint::TEndpoint()
    : Addr_(new NAddr::TIPv4Addr(TIpAddress(TIpHost(0), TIpPort(0))))
{
}

void TEndpoint::SetPort(ui16 port) {
    if (Port() == port || Addr_->Addr()->sa_family == AF_UNIX) { 
        return;
    }

    NAddr::TOpaqueAddr* oa = new NAddr::TOpaqueAddr(Addr_.Get());
    Addr_.Reset(oa);
    sockaddr* sa = oa->MutableAddr();

    if (sa->sa_family == AF_INET) {
        ((sockaddr_in*)sa)->sin_port = HostToInet(port);
    } else {
        ((sockaddr_in6*)sa)->sin6_port = HostToInet(port);
    }
}

ui16 TEndpoint::Port() const noexcept {
    if (Addr_->Addr()->sa_family == AF_UNIX) { 
        return 0; 
    } 
 
    const sockaddr* sa = Addr_->Addr();

    if (sa->sa_family == AF_INET) {
        return InetToHost(((const sockaddr_in*)sa)->sin_port);
    } else {
        return InetToHost(((const sockaddr_in6*)sa)->sin6_port);
    }
}

size_t TEndpoint::Hash() const {
    const sockaddr* sa = Addr_->Addr();

    if (sa->sa_family == AF_INET) {
        const sockaddr_in* sa4 = (const sockaddr_in*)sa;

        return IntHash((((ui64)sa4->sin_addr.s_addr) << 16) ^ sa4->sin_port);
    } else if (sa->sa_family == AF_INET6) { 
        const sockaddr_in6* sa6 = (const sockaddr_in6*)sa;
        const ui64* ptr = (const ui64*)&sa6->sin6_addr;

        return IntHash(ptr[0] ^ ptr[1] ^ sa6->sin6_port);
    } else { 
        const sockaddr_un* un = (const sockaddr_un*)sa; 
        THash<TString> strHash; 
 
        return strHash(un->sun_path); 
    }
}