23 set_host(
const std::string &hostname,
unsigned short port) {
25 if (hostname ==
"255.255.255.255") {
29 struct addrinfo hints, *res =
nullptr;
30 memset(&hints, 0,
sizeof(hints));
31 hints.ai_flags = AI_ADDRCONFIG;
32 hints.ai_family = support_ipv6 ? AF_UNSPEC : AF_INET;
34 if (getaddrinfo(hostname.c_str(),
nullptr, &hints, &res)) {
38 nassertr(res->ai_addrlen <=
sizeof(_storage),
false);
39 memcpy(&_storage, res->ai_addr, res->ai_addrlen);
42 _addr4.sin_port = htons(port);
52 std::string::size_type pos = hostname.rfind(
':');
53 if (pos == std::string::npos) {
57 std::string::size_type host_begin = 0;
58 std::string::size_type host_end = pos;
61 while (host_begin < host_end && isspace(hostname[host_begin])) {
65 while (host_begin < host_end && isspace(hostname[host_end - 1])) {
69 if (host_begin < host_end && hostname[host_begin] ==
'[') {
72 if (hostname[host_end - 1] ==
']') {
79 std::string host = hostname.substr(host_begin, host_end - host_begin);
80 std::string port = hostname.substr(pos + 1, 100);
82 unsigned short port_dig = (
unsigned short)atoi(port.c_str());
94 if (_storage.ss_family == AF_INET) {
95 getnameinfo(&_addr,
sizeof(sockaddr_in), buf,
sizeof(buf),
nullptr, 0, NI_NUMERICHOST);
97 }
else if (_storage.ss_family == AF_INET6) {
98 getnameinfo(&_addr,
sizeof(sockaddr_in6), buf,
sizeof(buf),
nullptr, 0, NI_NUMERICHOST);
101 nassert_raise(
"unsupported address family");
104 return std::string(buf);
116 if (_storage.ss_family == AF_INET) {
117 getnameinfo(&_addr,
sizeof(sockaddr_in), buf,
sizeof(buf),
nullptr, 0, NI_NUMERICHOST);
118 sprintf(buf + strlen(buf),
":%hu",
get_port());
120 }
else if (_storage.ss_family == AF_INET6) {
123 getnameinfo(&_addr,
sizeof(sockaddr_in6), buf + 1,
sizeof(buf) - 1,
nullptr, 0, NI_NUMERICHOST);
124 sprintf(buf + strlen(buf),
"]:%hu",
get_port());
127 nassert_raise(
"unsupported address family");
130 return std::string(buf);
139 if (_addr.sa_family == AF_INET) {
140 return _addr4.sin_addr.s_addr;
142 if (_addr.sa_family == AF_INET6) {
145 uint32_t *parts = (uint32_t *)&_addr6.sin6_addr;
146 if (parts[0] == 0 && parts[1] == 0 && (parts[2] == 0 || parts[2] == htonl(0xffff))) {
147 if (parts[2] == 0 && parts[3] == htonl(1)) {
155 nassert_raise(
"GetIPAddressRaw() can only be called on an IPv4 address");
unsigned long GetIPAddressRaw() const
Returns a raw 32-bit unsigned integer representing the IPv4 address.
std::string get_ip_port() const
Return the ip address/port in dot notation string.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::string get_ip() const
Return the IP address portion in dot notation string.
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...