19 INLINE Socket_Address::
20 Socket_Address(
unsigned short port) {
21 _addr4.sin_family = AF_INET;
22 _addr4.sin_addr.s_addr = INADDR_ANY;
23 _addr4.sin_port = htons(port);
29 INLINE Socket_Address::
31 _storage(inaddr._storage) {
37 INLINE Socket_Address::
38 Socket_Address(
const struct sockaddr &inaddr) {
39 if (inaddr.sa_family == AF_INET) {
40 _addr4 = (
const sockaddr_in &)inaddr;
42 }
else if (inaddr.sa_family == AF_INET6) {
43 _addr6 = (
const sockaddr_in6 &)inaddr;
46 nassert_raise(
"unsupported address family");
54 INLINE Socket_Address::
55 Socket_Address(
const struct sockaddr_in &inaddr) :
62 INLINE Socket_Address::
63 Socket_Address(
const struct sockaddr_in6 &inaddr) :
66 if (IN6_IS_ADDR_V4MAPPED(&_addr6.sin6_addr) != 0) {
68 _addr4.sin_family = AF_INET;
69 _addr4.sin_addr.s_addr = ((uint32_t *)&_addr6.sin6_addr)[3];
76 INLINE Socket_Address::
77 Socket_Address(
const struct sockaddr_storage &inaddr) :
91 INLINE
bool Socket_Address::
93 if (_storage.ss_family != in._storage.ss_family) {
97 if (_storage.ss_family == AF_INET) {
98 return _addr4.sin_port == in._addr4.sin_port &&
99 _addr4.sin_addr.s_addr == in._addr4.sin_addr.s_addr;
101 }
else if (_storage.ss_family == AF_INET6) {
102 return _addr6.sin6_port != in._addr6.sin6_port &&
103 memcmp((
char *) &_addr6.sin6_addr,
104 (
char *) &in._addr6.sin6_addr,
105 sizeof(_addr6.sin6_addr)) == 0;
109 nassert_raise(
"unsupported address family");
116 INLINE
bool Socket_Address::
118 return !operator ==(in);
126 _addr4.sin_family = AF_INET;
127 _addr4.sin_addr.s_addr = 0xffffffff;
128 _addr4.sin_port = htons(port);
137 _addr4.sin_family = AF_INET;
138 _addr4.sin_addr.s_addr = INADDR_ANY;
139 _addr4.sin_port = htons(port);
148 _addr6.sin6_family = AF_INET6;
149 _addr6.sin6_addr = in6addr_any;
150 _addr6.sin6_port = htons(port);
151 _addr6.sin6_scope_id = 0;
160 _addr4.sin_port = htons(port);
169 _addr4.sin_family = AF_INET;
170 _addr4.sin_addr.s_addr = INADDR_ANY;
171 _addr4.sin_port = htons(0);
180 return _storage.ss_family;
188 return ntohs(_addr4.sin_port);
195 set_host(uint32_t in_hostname,
unsigned short port) {
196 memcpy(&_addr4.sin_addr, &in_hostname,
sizeof(in_hostname));
197 _addr4.sin_port = htons(port);
198 _addr4.sin_family = AF_INET;
205 INLINE
bool Socket_Address::
207 if (_storage.ss_family != in._storage.ss_family) {
208 return _storage.ss_family < in._storage.ss_family;
211 if (_storage.ss_family == AF_INET) {
212 if (_addr4.sin_port != in._addr4.sin_port) {
213 return _addr4.sin_port < in._addr4.sin_port;
216 return _addr4.sin_addr.s_addr < in._addr4.sin_addr.s_addr;
218 }
else if (_storage.ss_family == AF_INET6) {
219 if (_addr6.sin6_port != in._addr6.sin6_port) {
220 return _addr6.sin6_port < in._addr6.sin6_port;
223 return IN6_ARE_ADDR_EQUAL(&_addr6.sin6_addr, &in._addr6.sin6_addr) != 0;
227 nassert_raise(
"unsupported address family");
236 if (_storage.ss_family == AF_INET) {
237 return (_addr4.sin_addr.s_addr == 0);
239 }
else if (_storage.ss_family == AF_INET6) {
240 return IN6_IS_ADDR_UNSPECIFIED(&_addr6.sin6_addr) != 0;
252 if (_storage.ss_family == AF_INET) {
253 uint32_t address = ntohl(_addr4.sin_addr.s_addr);
255 return (address >= 0xe0000000 && address < 0xefffffff);
257 }
else if (_storage.ss_family == AF_INET6) {
259 return IN6_IS_ADDR_MULTICAST(&_addr6.sin6_addr) != 0;
virtual ~Socket_Address()
Normal Destructor.
bool is_any() const
True if the address is zero.
bool set_port(unsigned short port)
Set to a specified port.
bool set_any_IP(unsigned short port)
Set to any address and a specified port.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool set_broadcast(unsigned short port)
Set to the broadcast address and a specified port.
unsigned short get_port() const
Get the port portion as an integer.
bool set_any_IPv6(unsigned short port)
Set to any IPv6 address and a specified port.
sa_family_t get_family() const
Returns AF_INET if this is an IPv4 address, or AF_INET6 if this is an IPv6 address.
void clear()
Set the internal values to a suitable known value.
A simple place to store and manipulate tcp and port address for communication layer.
bool is_mcast_range() const
True if the address is in the multicast range.
bool set_host(const std::string &hostname, unsigned short port)
This function will take a port and string-based TCP address and initialize the address with this info...