OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4NetIO::HostAddress Struct Reference

#include <C4NetIO.h>

Inheritance diagram for C4NetIO::HostAddress:
[legend]

Public Types

enum  AddressFamily { IPv6 = AF_INET6, IPv4 = AF_INET, UnknownFamily = 0 }
 
enum  SpecialAddress { Loopback, Any, AnyIPv4 }
 
enum  ToStringFlags { TSF_SkipZoneId = 1, TSF_SkipPort = 2 }
 

Public Member Functions

 HostAddress ()
 
 HostAddress (const HostAddress &other)
 
 HostAddress (SpecialAddress addr)
 
 HostAddress (uint32_t addr)
 
 HostAddress (const StdStrBuf &addr)
 
 HostAddress (const sockaddr *addr)
 
AddressFamily GetFamily () const
 
void SetScopeId (int scopeId)
 
int GetScopeId () const
 
void Clear ()
 
void SetHost (const sockaddr *addr)
 
void SetHost (const HostAddress &host)
 
void SetHost (SpecialAddress host)
 
void SetHost (const StdStrBuf &host, AddressFamily family=UnknownFamily)
 
void SetHost (uint32_t host)
 
C4NetIO::HostAddress AsIPv6 () const
 
C4NetIO::HostAddress AsIPv4 () const
 
bool IsNull () const
 
bool IsMulticast () const
 
bool IsLoopback () const
 
bool IsLocal () const
 
bool IsPrivate () const
 
StdStrBuf ToString (int flags=0) const
 
bool operator== (const HostAddress &rhs) const
 
bool operator!= (const HostAddress &rhs) const
 

Protected Attributes

union {
sockaddr gen
 
sockaddr_in v4
 
sockaddr_in6 v6
 
}; 
 

Detailed Description

Definition at line 69 of file C4NetIO.h.

Member Enumeration Documentation

Enumerator
IPv6 
IPv4 
UnknownFamily 

Definition at line 71 of file C4NetIO.h.

72  {
73  IPv6 = AF_INET6,
74  IPv4 = AF_INET,
75  UnknownFamily = 0
76  };
Enumerator
Loopback 
Any 
AnyIPv4 

Definition at line 77 of file C4NetIO.h.

78  {
79  Loopback, // IPv6 localhost (::1)
80  Any, // IPv6 any-address (::)
81  AnyIPv4 // IPv4 any-address (0.0.0.0)
82  };
Enumerator
TSF_SkipZoneId 
TSF_SkipPort 

Definition at line 84 of file C4NetIO.h.

Constructor & Destructor Documentation

C4NetIO::HostAddress::HostAddress ( )
inline

Definition at line 90 of file C4NetIO.h.

References Clear().

90 { Clear(); }

Here is the call graph for this function:

C4NetIO::HostAddress::HostAddress ( const HostAddress other)
inline

Definition at line 91 of file C4NetIO.h.

References SetHost().

91 { SetHost(other); }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:352

Here is the call graph for this function:

C4NetIO::HostAddress::HostAddress ( SpecialAddress  addr)
inline

Definition at line 92 of file C4NetIO.h.

References SetHost().

92 { SetHost(addr); }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:352

Here is the call graph for this function:

C4NetIO::HostAddress::HostAddress ( uint32_t  addr)
inlineexplicit

Definition at line 93 of file C4NetIO.h.

References SetHost().

93 { SetHost(addr); }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:352

Here is the call graph for this function:

C4NetIO::HostAddress::HostAddress ( const StdStrBuf addr)
inline

Definition at line 94 of file C4NetIO.h.

References SetHost().

94 { SetHost(addr); }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:352

Here is the call graph for this function:

C4NetIO::HostAddress::HostAddress ( const sockaddr *  addr)
inline

Definition at line 95 of file C4NetIO.h.

References SetHost().

95 { SetHost(addr); }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:352

Here is the call graph for this function:

Member Function Documentation

C4NetIO::HostAddress C4NetIO::HostAddress::AsIPv4 ( ) const

Definition at line 331 of file C4NetIO.cpp.

Referenced by C4NetIO::EndpointAddress::AsIPv4().

332 {
333  HostAddress nrv(*this);
334  if (gen.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&v6.sin6_addr))
335  {
336  nrv.v4.sin_family = AF_INET;
337  memcpy((char*) &nrv.v4.sin_addr, (char*) &v6.sin6_addr.s6_addr[12], sizeof(v4.sin_addr));
338  }
339  return nrv;
340 }

Here is the caller graph for this function:

C4NetIO::HostAddress C4NetIO::HostAddress::AsIPv6 ( ) const

Definition at line 309 of file C4NetIO.cpp.

Referenced by C4NetIO::EndpointAddress::AsIPv6(), and operator==().

310 {
311  static const uint8_t v6_mapped_v4_prefix[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
312 
313  HostAddress nrv(*this);
314  switch (gen.sa_family)
315  {
316  case AF_INET6:
317  // That was easy
318  break;
319  case AF_INET:
320  memmove(((char*)&nrv.v6.sin6_addr) + sizeof(v6_mapped_v4_prefix), &v4.sin_addr, sizeof(v4.sin_addr));
321  nrv.v6.sin6_family = AF_INET6;
322  memcpy(&nrv.v6.sin6_addr, v6_mapped_v4_prefix, sizeof(v6_mapped_v4_prefix));
323  nrv.v6.sin6_flowinfo = 0;
324  nrv.v6.sin6_scope_id = 0;
325  break;
326  default: assert(!"Shouldn't reach this"); break;
327  }
328  return nrv;
329 }

Here is the caller graph for this function:

void C4NetIO::HostAddress::Clear ( )

Definition at line 225 of file C4NetIO.cpp.

Referenced by C4NetIO::EndpointAddress::Clear(), and HostAddress().

226 {
227  v6.sin6_family = AF_INET6;
228  v6.sin6_flowinfo = 0;
229  v6.sin6_scope_id = 0;
230  memset(&v6.sin6_addr, 0, sizeof(v6.sin6_addr));
231 }

Here is the caller graph for this function:

C4NetIO::HostAddress::AddressFamily C4NetIO::HostAddress::GetFamily ( ) const

Definition at line 526 of file C4NetIO.cpp.

Referenced by C4NetIOTCP::Accept(), C4NetIOUDP::BinAddr::BinAddr(), C4NetIOTCP::Connect(), C4NetIOSimpleUDP::Execute(), C4Network2IO::HandlePuncherPacket(), C4NetIOSimpleUDP::InitBroadcast(), C4Network2IO::InitPuncher(), std::hash< C4NetIO::addr_t >::operator()(), and C4Network2HTTPClient::SetServer().

527 {
528  return gen.sa_family == AF_INET ? IPv4 :
529  gen.sa_family == AF_INET6 ? IPv6 : UnknownFamily;
530 }

Here is the caller graph for this function:

int C4NetIO::HostAddress::GetScopeId ( ) const

Definition at line 302 of file C4NetIO.cpp.

Referenced by C4Network2Client::AddLocalAddrs(), and C4Network2Client::SendAddresses().

303 {
304  if (gen.sa_family == AF_INET6)
305  return v6.sin6_scope_id;
306  return 0;
307 }

Here is the caller graph for this function:

bool C4NetIO::HostAddress::IsLocal ( ) const

Definition at line 266 of file C4NetIO.cpp.

267 {
268  if (gen.sa_family == AF_INET6)
269  return IN6_IS_ADDR_LINKLOCAL(&v6.sin6_addr) != 0;
270  // We don't really care about local 169.256.0.0/16 addresses here as users will either have a
271  // router doing DHCP (which will prevent usage of these addresses) or have a network that
272  // doesn't care about IP and IPv6 link-local addresses will work.
273  return false;
274 }
bool C4NetIO::HostAddress::IsLoopback ( ) const

Definition at line 257 of file C4NetIO.cpp.

Referenced by C4NetIO::GetLocalAddresses().

258 {
259  if (gen.sa_family == AF_INET6)
260  return IN6_IS_ADDR_LOOPBACK(&v6.sin6_addr) != 0;
261  if (gen.sa_family == AF_INET)
262  return (ntohl(v4.sin_addr.s_addr) >> 24) == 127;
263  return false;
264 }

Here is the caller graph for this function:

bool C4NetIO::HostAddress::IsMulticast ( ) const

Definition at line 248 of file C4NetIO.cpp.

Referenced by C4NetIOSimpleUDP::InitBroadcast(), and C4NetIOUDP::InitBroadcast().

249 {
250  if (gen.sa_family == AF_INET6)
251  return IN6_IS_ADDR_MULTICAST(&v6.sin6_addr) != 0;
252  if (gen.sa_family == AF_INET)
253  return (ntohl(v4.sin_addr.s_addr) >> 24) == 239;
254  return false;
255 }

Here is the caller graph for this function:

bool C4NetIO::HostAddress::IsNull ( ) const

Definition at line 514 of file C4NetIO.cpp.

Referenced by C4NetIO::EndpointAddress::IsNullHost().

515 {
516  switch (gen.sa_family)
517  {
518  case AF_INET: return v4.sin_addr.s_addr == 0;
519  case AF_INET6:
520  return !!IN6_IS_ADDR_UNSPECIFIED(&v6.sin6_addr);
521  }
522  assert(!"Shouldn't reach this");
523  return false;
524 }

Here is the caller graph for this function:

bool C4NetIO::HostAddress::IsPrivate ( ) const

Definition at line 276 of file C4NetIO.cpp.

References s.

277 {
278  // IPv6 unique local address
279  if (gen.sa_family == AF_INET6)
280  return (v6.sin6_addr.s6_addr[0] & 0xfe) == 0xfc;
281  if (gen.sa_family == AF_INET)
282  {
283  uint32_t addr = ntohl(v4.sin_addr.s_addr);
284  uint32_t s = (addr >> 16) & 0xff;
285  switch (addr >> 24)
286  {
287  case 10: return true;
288  case 172: return s >= 16 && s <= 31;
289  case 192: return s == 168;
290  }
291  }
292  return false;
293 }
#define s
bool C4NetIO::HostAddress::operator!= ( const HostAddress rhs) const
inline

Definition at line 123 of file C4NetIO.h.

123 { return !(*this == rhs); }
bool C4NetIO::HostAddress::operator== ( const HostAddress rhs) const

Definition at line 559 of file C4NetIO.cpp.

References AsIPv6().

560 {
561  // Check for IPv4-mapped IPv6 addresses.
562  if (gen.sa_family != rhs.gen.sa_family)
563  return AsIPv6() == rhs.AsIPv6();
564  if (gen.sa_family == AF_INET)
565  return v4.sin_addr.s_addr == rhs.v4.sin_addr.s_addr;
566  if (gen.sa_family == AF_INET6)
567  return memcmp(&v6.sin6_addr, &rhs.v6.sin6_addr, sizeof(v6.sin6_addr)) == 0 &&
568  v6.sin6_scope_id == rhs.v6.sin6_scope_id;
569  assert(!"Shouldn't reach this");
570  return false;
571 }
C4NetIO::HostAddress AsIPv6() const
Definition: C4NetIO.cpp:309

Here is the call graph for this function:

void C4NetIO::HostAddress::SetHost ( const sockaddr *  addr)

Definition at line 352 of file C4NetIO.cpp.

Referenced by C4NetIO::GetLocalAddresses(), and HostAddress().

353 {
354  // Copy all but port number
355  if (addr->sa_family == AF_INET6)
356  {
357  v6.sin6_family = ((const sockaddr_in6*)addr)->sin6_family;
358  v6.sin6_flowinfo = ((const sockaddr_in6*)addr)->sin6_flowinfo;
359  memcpy(&v6.sin6_addr, &((const sockaddr_in6*)addr)->sin6_addr, sizeof(v6.sin6_addr));
360  v6.sin6_scope_id = ((const sockaddr_in6*)addr)->sin6_scope_id;
361  }
362  else if (addr->sa_family == AF_INET)
363  {
364  v4.sin_family = ((const sockaddr_in*)addr)->sin_family;
365  v4.sin_addr.s_addr = ((const sockaddr_in*)addr)->sin_addr.s_addr;
366  memset(&v4.sin_zero, 0, sizeof(v4.sin_zero));
367  }
368 }

Here is the caller graph for this function:

void C4NetIO::HostAddress::SetHost ( const HostAddress host)

Definition at line 243 of file C4NetIO.cpp.

244 {
245  SetHost(&other.gen);
246 }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:352
void C4NetIO::HostAddress::SetHost ( SpecialAddress  host)

Definition at line 382 of file C4NetIO.cpp.

References ControllerKeys::Any().

383 {
384  switch (addr)
385  {
386  case Any:
387  v6.sin6_family = AF_INET6;
388  memset(&v6.sin6_addr, 0, sizeof(v6.sin6_addr));
389  v6.sin6_flowinfo = 0;
390  v6.sin6_scope_id = 0;
391  break;
392  case AnyIPv4:
393  v4.sin_family = AF_INET;
394  v4.sin_addr.s_addr = 0;
395  memset(&v4.sin_zero, 0, sizeof(v4.sin_zero));
396  break;
397  case Loopback:
398  v6.sin6_family = AF_INET6;
399  memset(&v6.sin6_addr, 0, sizeof(v6.sin6_addr)); v6.sin6_addr.s6_addr[15] = 1;
400  v6.sin6_flowinfo = 0;
401  v6.sin6_scope_id = 0;
402  break;
403  }
404 }

Here is the call graph for this function:

void C4NetIO::HostAddress::SetHost ( const StdStrBuf host,
AddressFamily  family = UnknownFamily 
)

Definition at line 413 of file C4NetIO.cpp.

References StdStrBuf::getData().

414 {
415  addrinfo hints = addrinfo();
416  hints.ai_family = family;
417  addrinfo *addresses = nullptr;
418  if (getaddrinfo(addr.getData(), nullptr, &hints, &addresses) != 0)
419  // GAI failed
420  return;
421  SetHost(addresses->ai_addr);
422  freeaddrinfo(addresses);
423 }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:352

Here is the call graph for this function:

void C4NetIO::HostAddress::SetHost ( uint32_t  host)

Definition at line 406 of file C4NetIO.cpp.

407 {
408  v4.sin_family = AF_INET;
409  v4.sin_addr.s_addr = v4addr;
410  memset(&v4.sin_zero, 0, sizeof(v4.sin_zero));
411 }
void C4NetIO::HostAddress::SetScopeId ( int  scopeId)

Definition at line 295 of file C4NetIO.cpp.

Referenced by C4Network2Client::SendAddresses().

296 {
297  if (gen.sa_family != AF_INET6) return;
298  if (IN6_IS_ADDR_LINKLOCAL(&v6.sin6_addr) != 0)
299  v6.sin6_scope_id = scopeId;
300 }

Here is the caller graph for this function:

StdStrBuf C4NetIO::HostAddress::ToString ( int  flags = 0) const

Definition at line 589 of file C4NetIO.cpp.

References ToString().

Referenced by ToString(), and C4NetIO::EndpointAddress::ToString().

590 {
591  if (gen.sa_family == AF_INET6 && v6.sin6_scope_id != 0 && (flags & TSF_SkipZoneId))
592  {
593  HostAddress addr = *this;
594  addr.v6.sin6_scope_id = 0;
595  return addr.ToString(flags);
596  }
597 
598  char buf[INET6_ADDRSTRLEN];
599  if (getnameinfo(&gen, sizeof(v6), buf, sizeof(buf), 0, 0, NI_NUMERICHOST) != 0)
600  return StdStrBuf();
601 
602  return StdStrBuf(buf, true);
603 }

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

union { ... }

The documentation for this struct was generated from the following files: