trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
InetAddress.h
1// Copyright 2010, Shuo Chen. All rights reserved.
2// http://code.google.com/p/muduo/
3//
4// Use of this source code is governed by a BSD-style license
5// that can be found in the License file.
6
7// Author: Shuo Chen (chenshuo at chenshuo dot com)
8//
9// This is a public header file, it must only include public header files.
10
11// Taken from Muduo and modified
12// Copyright 2016, Tao An. All rights reserved.
13// https://github.com/an-tao/trantor
14//
15// Use of this source code is governed by a BSD-style license
16// that can be found in the License file.
17
18// Author: Tao An
19
20#ifndef MUDUO_NET_INETADDRESS_H
21#define MUDUO_NET_INETADDRESS_H
22
23#include <trantor/utils/Date.h>
24#include <trantor/exports.h>
25
26#ifdef _WIN32
27#include <ws2tcpip.h>
28using sa_family_t = unsigned short;
29using in_addr_t = uint32_t;
30using uint16_t = unsigned short;
31#else
32#include <netinet/in.h>
33#include <arpa/inet.h>
34#include <sys/socket.h>
35#endif
36#include <string>
37#include <unordered_map>
38#include <mutex>
39namespace trantor
40{
45class TRANTOR_EXPORT InetAddress
46{
47 public:
56 InetAddress(uint16_t port = 0,
57 bool loopbackOnly = false,
58 bool ipv6 = false);
59
67 InetAddress(const std::string &ip, uint16_t port, bool ipv6 = false);
68
75 explicit InetAddress(const struct sockaddr_in &addr)
76 : addr_(addr), isUnspecified_(false)
77 {
78 }
79
86 explicit InetAddress(const struct sockaddr_in6 &addr)
87 : addr6_(addr), isIpV6_(true), isUnspecified_(false)
88 {
89 }
90
96 sa_family_t family() const
97 {
98 return addr_.sin_family;
99 }
100
106 std::string toIp() const;
107
113 std::string toIpPort() const;
114
120 std::string toIpNetEndian() const;
121
128 std::string toIpPortNetEndian() const;
129
135 uint16_t toPort() const;
136
143 bool isIpV6() const
144 {
145 return isIpV6_;
146 }
147
154 bool isIntranetIp() const;
155
162 bool isLoopbackIp() const;
163
169 const struct sockaddr *getSockAddr() const
170 {
171 return static_cast<const struct sockaddr *>((void *)(&addr6_));
172 }
173
179 void setSockAddrInet6(const struct sockaddr_in6 &addr6)
180 {
181 addr6_ = addr6;
182 isIpV6_ = (addr6_.sin6_family == AF_INET6);
183 isUnspecified_ = false;
184 }
185
191 uint32_t ipNetEndian() const;
192
199 const uint32_t *ip6NetEndian() const;
200
206 uint16_t portNetEndian() const
207 {
208 return addr_.sin_port;
209 }
210
216 void setPortNetEndian(uint16_t port)
217 {
218 addr_.sin_port = port;
219 }
220
224 inline bool isUnspecified() const
225 {
226 return isUnspecified_;
227 }
228
229 private:
230 union
231 {
232 struct sockaddr_in addr_;
233 struct sockaddr_in6 addr6_;
234 };
235 bool isIpV6_{false};
236 bool isUnspecified_{true};
237};
238
239} // namespace trantor
240
241#endif // MUDUO_NET_INETADDRESS_H
uint16_t portNetEndian() const
Return the port number in net endian byte order.
Definition InetAddress.h:206
std::string toIpNetEndian() const
Return the IP bytes of the endpoint in net endian byte order.
void setSockAddrInet6(const struct sockaddr_in6 &addr6)
Set the sockaddr_in6 struct in the endpoint.
Definition InetAddress.h:179
sa_family_t family() const
Return the sin_family of the endpoint.
Definition InetAddress.h:96
std::string toIp() const
Return the IP string of the endpoint.
bool isUnspecified() const
Return true if the address is not initialized.
Definition InetAddress.h:224
bool isIpV6() const
Check if the endpoint is IPv4 or IPv6.
Definition InetAddress.h:143
const uint32_t * ip6NetEndian() const
Return the pointer to the integer value of the IP(v6) in net endian byte order.
const struct sockaddr * getSockAddr() const
Get the pointer to the sockaddr struct.
Definition InetAddress.h:169
void setPortNetEndian(uint16_t port)
Set the port number in net endian byte order.
Definition InetAddress.h:216
uint32_t ipNetEndian() const
Return the integer value of the IP(v4) in net endian byte order.
InetAddress(uint16_t port=0, bool loopbackOnly=false, bool ipv6=false)
Constructs an endpoint with given port number. Mostly used in TcpServer listening.
InetAddress(const struct sockaddr_in6 &addr)
Constructs an IPv6 endpoint with given struct sockaddr_in6. Mostly used when accepting new connection...
Definition InetAddress.h:86
bool isLoopbackIp() const
Return true if the endpoint is a loopback endpoint.
std::string toIpPortNetEndian() const
Return the IP and port bytes of the endpoint in net endian byte order.
InetAddress(const std::string &ip, uint16_t port, bool ipv6=false)
Constructs an endpoint with given ip and port.
bool isIntranetIp() const
Return true if the endpoint is an intranet endpoint.
InetAddress(const struct sockaddr_in &addr)
Constructs an endpoint with given struct sockaddr_in. Mostly used when accepting new connections.
Definition InetAddress.h:75
uint16_t toPort() const
Return the port number of the endpoint.
std::string toIpPort() const
Return the IP and port string of the endpoint.
Definition EventLoop.h:34