OpenClonk
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
 
size_t GetAddrLen () 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 68 of file C4NetIO.h.

Member Enumeration Documentation

◆ AddressFamily

Enumerator
IPv6 
IPv4 
UnknownFamily 

Definition at line 70 of file C4NetIO.h.

71  {
72  IPv6 = AF_INET6,
73  IPv4 = AF_INET,
74  UnknownFamily = 0
75  };

◆ SpecialAddress

Enumerator
Loopback 
Any 
AnyIPv4 

Definition at line 76 of file C4NetIO.h.

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

◆ ToStringFlags

Enumerator
TSF_SkipZoneId 
TSF_SkipPort 

Definition at line 83 of file C4NetIO.h.

84  {
85  TSF_SkipZoneId = 1,
86  TSF_SkipPort = 2
87  };

Constructor & Destructor Documentation

◆ HostAddress() [1/6]

C4NetIO::HostAddress::HostAddress ( )
inline

Definition at line 89 of file C4NetIO.h.

89 { Clear(); }

References Clear().

Here is the call graph for this function:

◆ HostAddress() [2/6]

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

Definition at line 90 of file C4NetIO.h.

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

References SetHost().

Here is the call graph for this function:

◆ HostAddress() [3/6]

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

Definition at line 91 of file C4NetIO.h.

91 { SetHost(addr); }

References SetHost().

Here is the call graph for this function:

◆ HostAddress() [4/6]

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

Definition at line 92 of file C4NetIO.h.

92 { SetHost(addr); }

References SetHost().

Here is the call graph for this function:

◆ HostAddress() [5/6]

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

Definition at line 93 of file C4NetIO.h.

93 { SetHost(addr); }

References SetHost().

Here is the call graph for this function:

◆ HostAddress() [6/6]

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

Definition at line 94 of file C4NetIO.h.

94 { SetHost(addr); }

References SetHost().

Here is the call graph for this function:

Member Function Documentation

◆ AsIPv4()

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

Definition at line 325 of file C4NetIO.cpp.

326 {
327  HostAddress nrv(*this);
328  if (gen.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&v6.sin6_addr))
329  {
330  nrv.v4.sin_family = AF_INET;
331  memcpy((char*) &nrv.v4.sin_addr, (char*) &v6.sin6_addr.s6_addr[12], sizeof(v4.sin_addr));
332  }
333  return nrv;
334 }

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

Here is the caller graph for this function:

◆ AsIPv6()

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

Definition at line 303 of file C4NetIO.cpp.

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

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

Here is the caller graph for this function:

◆ Clear()

void C4NetIO::HostAddress::Clear ( )

Definition at line 219 of file C4NetIO.cpp.

220 {
221  v6.sin6_family = AF_INET6;
222  v6.sin6_flowinfo = 0;
223  v6.sin6_scope_id = 0;
224  memset(&v6.sin6_addr, 0, sizeof(v6.sin6_addr));
225 }

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

Here is the caller graph for this function:

◆ GetAddrLen()

size_t C4NetIO::HostAddress::GetAddrLen ( ) const

Definition at line 526 of file C4NetIO.cpp.

527 {
528  return GetFamily() == IPv4 ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
529 }
AddressFamily GetFamily() const
Definition: C4NetIO.cpp:520

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::Bind(), C4NetIOTCP::Connect(), C4NetIOTCP::Listen(), and C4NetIOSimpleUDP::Send().

Here is the caller graph for this function:

◆ GetFamily()

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

Definition at line 520 of file C4NetIO.cpp.

521 {
522  return gen.sa_family == AF_INET ? IPv4 :
523  gen.sa_family == AF_INET6 ? IPv6 : UnknownFamily;
524 }

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

Here is the caller graph for this function:

◆ GetScopeId()

int C4NetIO::HostAddress::GetScopeId ( ) const

Definition at line 296 of file C4NetIO.cpp.

297 {
298  if (gen.sa_family == AF_INET6)
299  return v6.sin6_scope_id;
300  return 0;
301 }

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

Here is the caller graph for this function:

◆ IsLocal()

bool C4NetIO::HostAddress::IsLocal ( ) const

Definition at line 260 of file C4NetIO.cpp.

261 {
262  if (gen.sa_family == AF_INET6)
263  return IN6_IS_ADDR_LINKLOCAL(&v6.sin6_addr) != 0;
264  // We don't really care about local 169.256.0.0/16 addresses here as users will either have a
265  // router doing DHCP (which will prevent usage of these addresses) or have a network that
266  // doesn't care about IP and IPv6 link-local addresses will work.
267  return false;
268 }

◆ IsLoopback()

bool C4NetIO::HostAddress::IsLoopback ( ) const

Definition at line 251 of file C4NetIO.cpp.

252 {
253  if (gen.sa_family == AF_INET6)
254  return IN6_IS_ADDR_LOOPBACK(&v6.sin6_addr) != 0;
255  if (gen.sa_family == AF_INET)
256  return (ntohl(v4.sin_addr.s_addr) >> 24) == 127;
257  return false;
258 }

Referenced by C4NetIO::GetLocalAddresses().

Here is the caller graph for this function:

◆ IsMulticast()

bool C4NetIO::HostAddress::IsMulticast ( ) const

Definition at line 242 of file C4NetIO.cpp.

243 {
244  if (gen.sa_family == AF_INET6)
245  return IN6_IS_ADDR_MULTICAST(&v6.sin6_addr) != 0;
246  if (gen.sa_family == AF_INET)
247  return (ntohl(v4.sin_addr.s_addr) >> 24) == 239;
248  return false;
249 }

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

Here is the caller graph for this function:

◆ IsNull()

bool C4NetIO::HostAddress::IsNull ( ) const

Definition at line 508 of file C4NetIO.cpp.

509 {
510  switch (gen.sa_family)
511  {
512  case AF_INET: return v4.sin_addr.s_addr == 0;
513  case AF_INET6:
514  return !!IN6_IS_ADDR_UNSPECIFIED(&v6.sin6_addr);
515  }
516  assert(!"Shouldn't reach this");
517  return false;
518 }

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

Here is the caller graph for this function:

◆ IsPrivate()

bool C4NetIO::HostAddress::IsPrivate ( ) const

Definition at line 270 of file C4NetIO.cpp.

271 {
272  // IPv6 unique local address
273  if (gen.sa_family == AF_INET6)
274  return (v6.sin6_addr.s6_addr[0] & 0xfe) == 0xfc;
275  if (gen.sa_family == AF_INET)
276  {
277  uint32_t addr = ntohl(v4.sin_addr.s_addr);
278  uint32_t s = (addr >> 16) & 0xff;
279  switch (addr >> 24)
280  {
281  case 10: return true;
282  case 172: return s >= 16 && s <= 31;
283  case 192: return s == 168;
284  }
285  }
286  return false;
287 }
#define s

References s.

◆ operator!=()

bool C4NetIO::HostAddress::operator!= ( const HostAddress rhs) const
inline

Definition at line 123 of file C4NetIO.h.

123 { return !(*this == rhs); }

◆ operator==()

bool C4NetIO::HostAddress::operator== ( const HostAddress rhs) const

Definition at line 558 of file C4NetIO.cpp.

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

References AsIPv6().

Here is the call graph for this function:

◆ SetHost() [1/5]

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

Definition at line 237 of file C4NetIO.cpp.

238 {
239  SetHost(&other.gen);
240 }

◆ SetHost() [2/5]

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

Definition at line 346 of file C4NetIO.cpp.

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

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

Here is the caller graph for this function:

◆ SetHost() [3/5]

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

Definition at line 407 of file C4NetIO.cpp.

408 {
409  addrinfo hints = addrinfo();
410  hints.ai_family = family;
411  addrinfo *addresses = nullptr;
412  if (getaddrinfo(addr.getData(), nullptr, &hints, &addresses) != 0)
413  // GAI failed
414  return;
415  SetHost(addresses->ai_addr);
416  freeaddrinfo(addresses);
417 }

References StdStrBuf::getData().

Here is the call graph for this function:

◆ SetHost() [4/5]

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

Definition at line 376 of file C4NetIO.cpp.

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

References ControllerKeys::Any().

Here is the call graph for this function:

◆ SetHost() [5/5]

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

Definition at line 400 of file C4NetIO.cpp.

401 {
402  v4.sin_family = AF_INET;
403  v4.sin_addr.s_addr = v4addr;
404  memset(&v4.sin_zero, 0, sizeof(v4.sin_zero));
405 }

◆ SetScopeId()

void C4NetIO::HostAddress::SetScopeId ( int  scopeId)

Definition at line 289 of file C4NetIO.cpp.

290 {
291  if (gen.sa_family != AF_INET6) return;
292  if (IN6_IS_ADDR_LINKLOCAL(&v6.sin6_addr) != 0)
293  v6.sin6_scope_id = scopeId;
294 }

Referenced by C4Network2Client::SendAddresses().

Here is the caller graph for this function:

◆ ToString()

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

Definition at line 588 of file C4NetIO.cpp.

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

References ToString().

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

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: