aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/socket_addr.h
blob: e66735c4587dcd86de8dd6cb5db1f0f143544964 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once

#include "hash.h"

#include <util/generic/hash.h>
#include <util/generic/utility.h>
#include <util/network/address.h>
#include <util/network/init.h>

#include <string.h>

namespace NBus {
    class TNetAddr;
}

namespace NBus { 
    namespace NPrivate { 
        enum EAddrFamily { 
            ADDR_UNSPEC = AF_UNSPEC, 
            ADDR_IPV4 = AF_INET, 
            ADDR_IPV6 = AF_INET6, 
        }; 

        class TBusIpAddr { 
        private: 
            EAddrFamily Af; 

            union { 
                in_addr In4; 
                in6_addr In6; 
            }; 

        public: 
            TBusIpAddr() { 
                Clear(); 
            } 

            EAddrFamily GetAddrFamily() const { 
                return Af; 
            } 

            void Clear() { 
                Zero(*this); 
            } 

            in_addr GetInAddr() const { 
                Y_ASSERT(Af == ADDR_IPV4); 
                return In4; 
            } 

            void SetInAddr(const in_addr& in4) { 
                Clear(); 
                Af = ADDR_IPV4; 
                In4 = in4; 
            } 

            in6_addr GetIn6Addr() const { 
                Y_ASSERT(Af == ADDR_IPV6); 
                return In6; 
            } 

            void SetIn6Addr(const in6_addr& in6) { 
                Clear(); 
                Af = ADDR_IPV6; 
                In6 = in6; 
            } 

            bool operator==(const TBusIpAddr& that) const { 
                return memcmp(this, &that, sizeof(that)) == 0; 
            } 
        }; 

        class TBusSocketAddr { 
        public: 
            TBusIpAddr IpAddr; 
            ui16 Port; 

            //Only makes sense for IPv6 link-local addresses 
            ui32 IPv6ScopeID; 

            TBusSocketAddr() 
                : Port(0) 
                , IPv6ScopeID(0) 
            { 
            } 

            TBusSocketAddr(const NAddr::IRemoteAddr*); 
            TBusSocketAddr(const TNetAddr&); 
            TBusSocketAddr(TStringBuf host, unsigned port); 

            TNetAddr ToNetAddr() const; 

            bool operator==(const TBusSocketAddr& that) const { 
                return IpAddr == that.IpAddr && Port == that.Port; 
            } 
        }; 

    } 
} 

template <>
struct THash<NBus::NPrivate::TBusIpAddr> {
    inline size_t operator()(const NBus::NPrivate::TBusIpAddr& a) const {
        return ComputeHash(TStringBuf((const char*)&a, sizeof(a)));
    }
};

template <>
struct THash<NBus::NPrivate::TBusSocketAddr> {
    inline size_t operator()(const NBus::NPrivate::TBusSocketAddr& a) const {
        return HashValues(a.IpAddr, a.Port);
    }
};