-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddress_impl.h
112 lines (84 loc) · 2.7 KB
/
address_impl.h
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
#ifndef SOCKPUPPET_ADDRESS_IMPL_H
#define SOCKPUPPET_ADDRESS_IMPL_H
#include "sockpuppet/address.h" // for Address
#include "winsock_guard.h" // for WinSockGuard
#ifdef _WIN32
# include <ws2tcpip.h> // for sockaddr_storage
#else
# include <netdb.h> // for sockaddr_storage
# include <sys/socket.h> // for socklen_t
#endif // _WIN32
#include <cstdint> // for uint16_t
#include <memory> // for std::unique_ptr
#include <string> // for std::string
#include <vector> // for std::vector
namespace sockpuppet {
struct SockAddrView
{
sockaddr const *addr;
socklen_t addrLen;
bool operator<(SockAddrView const &other) const;
bool operator==(SockAddrView const &other) const;
bool operator!=(SockAddrView const &other) const;
};
struct Address::AddressImpl
{
WinSockGuard guard; ///< Guard to initialize socket subsystem on windows
AddressImpl();
virtual ~AddressImpl();
virtual SockAddrView ForTcp() const = 0;
virtual SockAddrView ForUdp() const = 0;
virtual SockAddrView ForAny() const = 0;
virtual int Family() const = 0;
std::string Host() const;
std::string Service() const;
uint16_t Port() const;
bool IsV6() const;
bool operator<(Address::AddressImpl const &other) const;
bool operator==(Address::AddressImpl const &other) const;
bool operator!=(Address::AddressImpl const &other) const;
static std::vector<Address> LocalAddresses();
};
struct SockAddrInfo : public Address::AddressImpl
{
struct AddrInfoDeleter
{
void operator()(addrinfo const *ptr) const noexcept;
};
using AddrInfoPtr = std::unique_ptr<addrinfo const, AddrInfoDeleter>;
AddrInfoPtr info;
SockAddrInfo(std::string const &uri);
SockAddrInfo(std::string const &host, std::string const &serv);
SockAddrInfo(uint16_t port);
~SockAddrInfo() override;
addrinfo const *Find(int type, int protocol) const;
SockAddrView ForTcp() const override;
SockAddrView ForUdp() const override;
SockAddrView ForAny() const override;
int Family() const override;
};
struct SockAddrStorage : public Address::AddressImpl
{
sockaddr_storage storage;
socklen_t size;
SockAddrStorage();
SockAddrStorage(sockaddr const *addr, size_t addrLen);
~SockAddrStorage() override;
sockaddr *Addr();
socklen_t *AddrLen();
SockAddrView ForTcp() const override;
SockAddrView ForUdp() const override;
SockAddrView ForAny() const override;
int Family() const override;
};
std::string to_string(Address::AddressImpl const &sockAddr);
std::string to_string(SockAddrView const &sockAddr);
} // namespace sockpuppet
namespace std {
template<>
struct hash<sockpuppet::Address::AddressImpl>
{
size_t operator()(sockpuppet::Address::AddressImpl const &addr) const;
};
} // namespace std
#endif // SOCKPUPPET_ADDRESS_IMPL_H