-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInetAddr.h
More file actions
84 lines (68 loc) · 2.64 KB
/
Copy pathInetAddr.h
File metadata and controls
84 lines (68 loc) · 2.64 KB
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
#if !defined(MUDUO_INETADDR_H)
#define MUDUO_INETADDR_H
#include <muduo/base/allocator/Allocatable.h>
#include <arpa/inet.h>
#include <cstddef> // for offsetof
#include <string>
namespace muduo {
namespace sockets {
namespace address {
/// @brief Represents a IPv4 address OR IPv6 address
union SockAddr {
struct sockaddr_in inet4;
struct sockaddr_in6 inet6;
/// FIXME: don`t use "operator type statement", it`s not graceful And clear
explicit operator const sockaddr_in6&() const {
return inet6;
}
explicit operator sockaddr_in6&() {
return inet6;
}
};
static_assert(sizeof(SockAddr) == sizeof(struct sockaddr_in6),
"size of 'union sockets::address::SockAddr' is not equal to size of 'struct sockaddr_in6'");
static_assert(offsetof(sockaddr_in, sockaddr_in::sin_family) == 0,
"sockaddr_in::sin_family offset isn`t 0");
static_assert(offsetof(sockaddr_in6, sockaddr_in6::sin6_family) == 0,
"sockaddr_in6::sin6_family offset isn`t 0");
static_assert(offsetof(SockAddr, SockAddr::inet4) == 0 &&
offsetof(SockAddr, SockAddr::inet4) == offsetof(SockAddr, SockAddr::inet6),
"'union sockets::address::SockAddr' is not supported");
} // namespace address
} // namespace sockets
/// @brief Represents a copyable IPv4 OR IPv6 address
#ifdef MUDUO_USE_MEMPOOL
class InetAddr : public base::detail::Allocatable {
#else
class InetAddr {
#endif
using SockAddr = sockets::address::SockAddr;
public:
explicit InetAddr(const SockAddr& addr)
: addr_(addr)
{ }
/// Constructs an endpoint with given port number(By default, it`s determined by the OS).
/// Mostly used in TcpServer listening.
explicit InetAddr(uint16_t port = 0, bool loopback_only = false, bool ipv6 = false);
explicit InetAddr(const std::string& ip, uint16_t port, bool ipv6 = false);
// default copy-constructor & copy-assigned are okey.
InetAddr(const InetAddr&) = default;
InetAddr& operator=(const InetAddr&) = default;
// default destructor is okey.
sa_family_t GetAddressFamily() const { return addr_.inet4.sin_family; }
const struct sockaddr* GetNativeSockAddr() const;
void SetSockAddr(const SockAddr& a)
{ addr_ = a; }
void SetSockAddrInet4(const struct sockaddr_in& a)
{ addr_.inet4 = a; }
void SetSockAddrInet6(const struct sockaddr_in6& a)
{ addr_.inet6 = a; }
std::string GetIpPort() const;
std::string GetIp() const;
private:
SockAddr addr_;
};
static_assert(sizeof(InetAddr) == sizeof(sockets::address::SockAddr),
"muduo::InetAddr size isn`t equal to sockets::address::SockAddr size");
} // namespace muduo
#endif // MUDUO_INETADDR_H