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 68 of file C4NetIO.h.

Member Enumeration Documentation

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  };
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  };
Enumerator
TSF_SkipZoneId 
TSF_SkipPort 

Definition at line 83 of file C4NetIO.h.

Constructor & Destructor Documentation

C4NetIO::HostAddress::HostAddress ( )
inline

Definition at line 89 of file C4NetIO.h.

References Clear().

89 { Clear(); }

Here is the call graph for this function:

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

Definition at line 90 of file C4NetIO.h.

References SetHost().

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

Here is the call graph for this function:

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

Definition at line 91 of file C4NetIO.h.

References SetHost().

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

Here is the call graph for this function:

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

Definition at line 92 of file C4NetIO.h.

References SetHost().

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

Here is the call graph for this function:

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

Definition at line 93 of file C4NetIO.h.

References SetHost().

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

Here is the call graph for this function:

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

Definition at line 94 of file C4NetIO.h.

References SetHost().

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

Here is the call graph for this function:

Member Function Documentation

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

Definition at line 325 of file C4NetIO.cpp.

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

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 }

Here is the caller graph for this function:

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

Definition at line 303 of file C4NetIO.cpp.

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

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 }

Here is the caller graph for this function:

void C4NetIO::HostAddress::Clear ( )

Definition at line 219 of file C4NetIO.cpp.

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

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 }

Here is the caller graph for this function:

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

Definition at line 520 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().

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

Here is the caller graph for this function:

int C4NetIO::HostAddress::GetScopeId ( ) const

Definition at line 296 of file C4NetIO.cpp.

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

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

Here is the caller graph for this function:

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 }
bool C4NetIO::HostAddress::IsLoopback ( ) const

Definition at line 251 of file C4NetIO.cpp.

Referenced by C4NetIO::GetLocalAddresses().

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 }

Here is the caller graph for this function:

bool C4NetIO::HostAddress::IsMulticast ( ) const

Definition at line 242 of file C4NetIO.cpp.

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

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 }

Here is the caller graph for this function:

bool C4NetIO::HostAddress::IsNull ( ) const

Definition at line 508 of file C4NetIO.cpp.

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

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 }

Here is the caller graph for this function:

bool C4NetIO::HostAddress::IsPrivate ( ) const

Definition at line 270 of file C4NetIO.cpp.

References s.

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
bool C4NetIO::HostAddress::operator!= ( const HostAddress rhs) const
inline

Definition at line 122 of file C4NetIO.h.

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

Definition at line 553 of file C4NetIO.cpp.

References AsIPv6().

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

Here is the call graph for this function:

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

Definition at line 346 of file C4NetIO.cpp.

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

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 }

Here is the caller graph for this function:

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

Definition at line 237 of file C4NetIO.cpp.

238 {
239  SetHost(&other.gen);
240 }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:346
void C4NetIO::HostAddress::SetHost ( SpecialAddress  host)

Definition at line 376 of file C4NetIO.cpp.

References ControllerKeys::Any().

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 }

Here is the call graph for this function:

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

Definition at line 407 of file C4NetIO.cpp.

References StdStrBuf::getData().

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 }
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:346

Here is the call graph for this function:

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 }
void C4NetIO::HostAddress::SetScopeId ( int  scopeId)

Definition at line 289 of file C4NetIO.cpp.

Referenced by C4Network2Client::SendAddresses().

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 }

Here is the caller graph for this function:

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

Definition at line 583 of file C4NetIO.cpp.

References ToString().

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

584 {
585  if (gen.sa_family == AF_INET6 && v6.sin6_scope_id != 0 && (flags & TSF_SkipZoneId))
586  {
587  HostAddress addr = *this;
588  addr.v6.sin6_scope_id = 0;
589  return addr.ToString(flags);
590  }
591 
592  char buf[INET6_ADDRSTRLEN];
593  if (getnameinfo(&gen, sizeof(v6), buf, sizeof(buf), nullptr, 0, NI_NUMERICHOST) != 0)
594  return StdStrBuf();
595 
596  return StdStrBuf(buf, true);
597 }

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: