20#ifndef MUDUO_NET_INETADDRESS_H
21#define MUDUO_NET_INETADDRESS_H
24#include <trantor/exports.h>
28using sa_family_t =
unsigned short;
29using in_addr_t = uint32_t;
30using uint16_t =
unsigned short;
32#include <netinet/in.h>
34#include <sys/socket.h>
37#include <unordered_map>
57 bool loopbackOnly =
false,
67 InetAddress(
const std::string &ip, uint16_t port,
bool ipv6 =
false);
76 : addr_(addr), isUnspecified_(false)
87 : addr6_(addr), isIpV6_(true), isUnspecified_(false)
98 return addr_.sin_family;
171 return static_cast<const struct sockaddr *
>((
void *)(&addr6_));
182 isIpV6_ = (addr6_.sin6_family == AF_INET6);
183 isUnspecified_ =
false;
208 return addr_.sin_port;
218 addr_.sin_port = port;
226 return isUnspecified_;
232 struct sockaddr_in addr_;
233 struct sockaddr_in6 addr6_;
236 bool isUnspecified_{
true};
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