aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/interconnect/interconnect_address.cpp
blob: 290aee1b55f0b4ae51ec658b3077f7e466b91ac8 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "interconnect_address.h"

#include <util/string/cast.h>
#include <util/system/file.h>

#if defined(_linux_)
#include <sys/un.h>
#include <sys/stat.h>
#endif

namespace NInterconnect {
    TAddress::TAddress() {
        memset(&Addr, 0, sizeof(Addr));
    }

    TAddress::TAddress(NAddr::IRemoteAddr& addr) {
        socklen_t len = addr.Len();
        Y_VERIFY(len <= sizeof(Addr));
        memcpy(&Addr.Generic, addr.Addr(), len);
    }

    int TAddress::GetFamily() const {
        return Addr.Generic.sa_family;
    }

    socklen_t TAddress::Size() const {
        switch (Addr.Generic.sa_family) {
            case AF_INET6:
                return sizeof(sockaddr_in6);
            case AF_INET:
                return sizeof(sockaddr_in);
            default:
                return 0;
        }
    }

    sockaddr* TAddress::SockAddr() {
        return &Addr.Generic;
    }

    const sockaddr* TAddress::SockAddr() const {
        return &Addr.Generic;
    }

    ui16 TAddress::GetPort() const {
        switch (Addr.Generic.sa_family) {
            case AF_INET6:
                return ntohs(Addr.Ipv6.sin6_port);
            case AF_INET:
                return ntohs(Addr.Ipv4.sin_port);
            default:
                return 0;
        }
    }

    TString TAddress::ToString() const {
        return GetAddress() + ":" + ::ToString(GetPort());
    }

    TAddress::TAddress(const char* addr, ui16 port) {
        memset(&Addr, 0, sizeof(Addr));
        if (inet_pton(Addr.Ipv6.sin6_family = AF_INET6, addr, &Addr.Ipv6.sin6_addr) > 0) {
            Addr.Ipv6.sin6_port = htons(port);
        } else if (inet_pton(Addr.Ipv4.sin_family = AF_INET, addr, &Addr.Ipv4.sin_addr) > 0) {
            Addr.Ipv4.sin_port = htons(port);
        }
    }

    TAddress::TAddress(const TString& addr, ui16 port)
        : TAddress(addr.data(), port)
    {}

    TString TAddress::GetAddress() const {
        const void *src;
        socklen_t size;

        switch (Addr.Generic.sa_family) {
            case AF_INET6:
                std::tie(src, size) = std::make_tuple(&Addr.Ipv6.sin6_addr, INET6_ADDRSTRLEN);
                break;

            case AF_INET:
                std::tie(src, size) = std::make_tuple(&Addr.Ipv4.sin_addr, INET_ADDRSTRLEN);
                break;

            default:
                return TString();
        }

        char *buffer = static_cast<char*>(alloca(size));
        const char *p = inet_ntop(Addr.Generic.sa_family, const_cast<void*>(src), buffer, size);
        return p ? TString(p) : TString();
    }
}