OpenClonk
C4NetIO Class Referenceabstract

#include <C4NetIO.h>

Inheritance diagram for C4NetIO:
[legend]
Collaboration diagram for C4NetIO:
[legend]

Classes

class  CBClass
 
class  CBProxy
 
struct  EndpointAddress
 
struct  HostAddress
 

Public Types

typedef EndpointAddress addr_t
 

Public Member Functions

 C4NetIO ()
 
 ~C4NetIO () override
 
virtual bool Init (uint16_t iPort=addr_t::IPPORT_NONE)=0
 
virtual bool Close ()=0
 
bool Execute (int iTimeout=-1, pollfd *=nullptr) override=0
 
bool IsNotify () override
 
virtual bool Connect (const addr_t &addr)=0
 
virtual bool Close (const addr_t &addr)=0
 
virtual bool Send (const class C4NetIOPacket &rPacket)=0
 
virtual bool SetBroadcast (const addr_t &addr, bool fSet=true)=0
 
virtual bool Broadcast (const class C4NetIOPacket &rPacket)=0
 
virtual bool GetStatistic (int *pBroadcastRate)=0
 
virtual bool GetConnStatistic (const addr_t &addr, int *pIRate, int *pORate, int *pLoss)=0
 
virtual void ClearStatistic ()=0
 
virtual const char * GetError () const
 
void ResetError ()
 
virtual void SetCallback (CBClass *pnCallback)=0
 
bool ExecuteUntil (int iTimeout=-1)
 
virtual void GetFDs (std::vector< struct pollfd > &)
 
virtual C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow)
 
bool IsSignaled ()
 
virtual bool IsLowPriority ()
 
virtual uint32_t TimerInterval ()
 

Static Public Member Functions

static std::vector< HostAddressGetLocalAddresses ()
 

Static Public Attributes

static const int TO_INF = -1
 

Protected Member Functions

bool InitIPv6Socket (SOCKET socket)
 
void SetError (const char *strnError, bool fSockErr=false)
 
void Changed ()
 

Protected Attributes

StdCopyStrBuf Error
 

Detailed Description

Definition at line 59 of file C4NetIO.h.

Member Typedef Documentation

◆ addr_t

Definition at line 213 of file C4NetIO.h.

Constructor & Destructor Documentation

◆ C4NetIO()

C4NetIO::C4NetIO ( )

Definition at line 723 of file C4NetIO.cpp.

724 {
725  ResetError();
726 }
void ResetError()
Definition: C4NetIO.h:287

References ResetError().

Here is the call graph for this function:

◆ ~C4NetIO()

C4NetIO::~C4NetIO ( )
overridedefault

Member Function Documentation

◆ Broadcast()

virtual bool C4NetIO::Broadcast ( const class C4NetIOPacket rPacket)
pure virtual

◆ Changed()

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 108 of file StdScheduler.cpp.

109 {
110  auto s = scheduler;
111  if (s)
112  s->Changed(this);
113 }
#define s

References s.

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::AddConnectWait(), C4NetIOUDP::AddPeer(), C4NetIOTCP::Listen(), and CStdTimerProc::SetDelay().

Here is the caller graph for this function:

◆ ClearStatistic()

virtual void C4NetIO::ClearStatistic ( )
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IO::GenerateStatistics().

Here is the caller graph for this function:

◆ Close() [1/2]

virtual bool C4NetIO::Close ( )
pure virtual

Implemented in C4Network2IRCClient, C4AulDebug, C4Network2IRCClient, C4Network2IRCClient, C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IOConnection::Close(), and C4Network2IO::HandlePuncherPacket().

Here is the caller graph for this function:

◆ Close() [2/2]

virtual bool C4NetIO::Close ( const addr_t addr)
pure virtual

◆ Connect()

virtual bool C4NetIO::Connect ( const addr_t addr)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IOConnection::Connect(), and C4Network2IO::InitPuncher().

Here is the caller graph for this function:

◆ Execute()

bool C4NetIO::Execute ( int  iTimeout = -1,
pollfd *  = nullptr 
)
overridepure virtual

Implements StdSchedulerProc.

Implemented in C4NetIOTCP, C4NetIOUDP, and C4NetIOSimpleUDP.

◆ ExecuteUntil()

bool StdSchedulerProc::ExecuteUntil ( int  iTimeout = -1)
inherited

Definition at line 33 of file StdScheduler.cpp.

34 {
35  // Infinite?
36  if (iTimeout < 0)
37  for (;;)
38  if (!Execute())
39  return false;
40  // Calculate endpoint
41  C4TimeMilliseconds tStopTime = C4TimeMilliseconds::Now() + iTimeout;
42  for (;;)
43  {
44  // Call execute with given timeout
45  if (!Execute(std::max(iTimeout, 0)))
46  return false;
47  // Calculate timeout
49  if (tTime >= tStopTime)
50  break;
51  iTimeout = tStopTime - tTime;
52  }
53  // All ok.
54  return true;
55 }
static C4TimeMilliseconds Now()
virtual bool Execute(int iTimeout=-1, pollfd *readyfds=nullptr)=0

References StdSchedulerProc::Execute(), and C4TimeMilliseconds::Now().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetConnStatistic()

virtual bool C4NetIO::GetConnStatistic ( const addr_t addr,
int *  pIRate,
int *  pORate,
int *  pLoss 
)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IOConnection::DoStatistics().

Here is the caller graph for this function:

◆ GetError()

virtual const char* C4NetIO::GetError ( ) const
inlinevirtual

Definition at line 286 of file C4NetIO.h.

286 { return Error.getData(); }
StdCopyStrBuf Error
Definition: C4NetIO.h:283
const char * getData() const
Definition: StdBuf.h:442

References Error, and StdStrBuf::getData().

Referenced by C4Network2IO::ConnectWithSocket(), C4NetIOUDP::DoLoopbackTest(), C4StartupNetDlg::DoRefresh(), C4Network2IO::Init(), C4NetIOUDP::InitBroadcast(), main(), MyCBClass::OnConn(), C4ChatControl::OnConnectBtn(), MyCBClass::OnPacket(), C4ChatControl::ProcessInput(), and C4Network2IOConnection::Send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFDs()

virtual void StdSchedulerProc::GetFDs ( std::vector< struct pollfd > &  )
inlinevirtualinherited

Reimplemented in C4FileMonitor, C4NetIOSimpleUDP, C4NetIOTCP, CStdNotifyProc, C4StdInProc, and C4HTTPClient.

Definition at line 72 of file StdScheduler.h.

72 { }

◆ GetLocalAddresses()

std::vector< C4NetIO::HostAddress > C4NetIO::GetLocalAddresses ( )
static

Definition at line 631 of file C4NetIO.cpp.

632 {
633  std::vector<HostAddress> result;
634 
635 #ifdef HAVE_WINSOCK
636  HostAddress addr;
637  const size_t BUFFER_SIZE = 16000;
638  PIP_ADAPTER_ADDRESSES addresses = nullptr;
639  for (int i = 0; i < 3; ++i)
640  {
641  addresses = (PIP_ADAPTER_ADDRESSES) realloc(addresses, BUFFER_SIZE * (i+1));
642  if (!addresses)
643  // allocation failed
644  return result;
645  ULONG bufsz = BUFFER_SIZE * (i+1);
646  DWORD rv = GetAdaptersAddresses(AF_UNSPEC,
647  GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER|GAA_FLAG_SKIP_FRIENDLY_NAME,
648  nullptr, addresses, &bufsz);
649  if (rv == ERROR_BUFFER_OVERFLOW)
650  // too little space, try again
651  continue;
652  if (rv != NO_ERROR)
653  {
654  // Something else happened
655  free(addresses);
656  return result;
657  }
658  // All okay, add addresses
659  for (PIP_ADAPTER_ADDRESSES address = addresses; address; address = address->Next)
660  {
661  for (PIP_ADAPTER_UNICAST_ADDRESS unicast = address->FirstUnicastAddress; unicast; unicast = unicast->Next)
662  {
663  addr.SetHost(unicast->Address.lpSockaddr);
664  if (addr.IsLoopback())
665  continue;
666  result.push_back(addr);
667  }
668  }
669  }
670  free(addresses);
671 #else
672  bool have_ipv6 = false;
673 
674 #ifdef __linux__
675  // Get IPv6 addresses on Linux from procfs which allows filtering deprecated privacy addresses.
676  FILE *f = fopen("/proc/net/if_inet6", "r");
677  if (f)
678  {
679  sockaddr_in6 sa6 = sockaddr_in6();
680  sa6.sin6_family = AF_INET6;
681  auto a6 = sa6.sin6_addr.s6_addr;
682  uint8_t if_idx, plen, scope, flags;
683  char devname[20];
684  while (fscanf(f, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx %02hhx %02hhx %02hhx %02hhx %20s\n",
685  &a6[0], &a6[1], &a6[2], &a6[3], &a6[4], &a6[5], &a6[6], &a6[7],
686  &a6[8], &a6[9], &a6[10], &a6[11], &a6[12], &a6[13], &a6[14], &a6[15],
687  &if_idx, &plen, &scope, &flags, devname) != EOF)
688  {
689  // Skip loopback and deprecated addresses.
690  if (scope == IPV6_ADDR_LOOPBACK || flags & IFA_F_DEPRECATED)
691  continue;
692  sa6.sin6_scope_id = scope == IPV6_ADDR_LINKLOCAL ? if_idx : 0;
693  result.emplace_back((sockaddr*) &sa6);
694  }
695  have_ipv6 = result.size() > 0;
696  fclose(f);
697  }
698 #endif
699 
700  struct ifaddrs* addrs;
701  if (getifaddrs(&addrs) < 0)
702  return result;
703  for (struct ifaddrs* ifaddr = addrs; ifaddr != nullptr; ifaddr = ifaddr->ifa_next)
704  {
705  struct sockaddr* ad = ifaddr->ifa_addr;
706  if (ad == nullptr) continue;
707 
708  if ((ad->sa_family == AF_INET || (!have_ipv6 && ad->sa_family == AF_INET6)) && (~ifaddr->ifa_flags & IFF_LOOPBACK)) // Choose only non-loopback IPv4/6 devices
709  {
710  result.emplace_back(ad);
711  }
712  }
713  freeifaddrs(addrs);
714 #endif
715 
716  return result;
717 }
uint32_t DWORD

References C4NetIO::HostAddress::IsLoopback(), and C4NetIO::HostAddress::SetHost().

Referenced by C4Network2Client::AddLocalAddrs(), and C4NetIOUDP::InitBroadcast().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetNextTick()

C4TimeMilliseconds StdSchedulerProc::GetNextTick ( C4TimeMilliseconds  tNow)
virtualinherited

Reimplemented in CStdTimerProc, C4Network2IO, C4NetIOUDP, and C4HTTPClient.

Definition at line 115 of file StdScheduler.cpp.

References C4TimeMilliseconds::PositiveInfinity.

◆ GetStatistic()

virtual bool C4NetIO::GetStatistic ( int *  pBroadcastRate)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IO::GenerateStatistics().

Here is the caller graph for this function:

◆ Init()

virtual bool C4NetIO::Init ( uint16_t  iPort = addr_t::IPPORT_NONE)
pure virtual

Implemented in C4Network2IODiscoverClient, C4Network2IODiscover, C4NetIOUDP, C4NetIOSimpleUDP, C4NetIOTCP, and C4AulDebug.

Referenced by C4Network2IO::Init().

Here is the caller graph for this function:

◆ InitIPv6Socket()

bool C4NetIO::InitIPv6Socket ( SOCKET  socket)
protected

Definition at line 730 of file C4NetIO.cpp.

731 {
732  int opt = 0;
733  if (setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char*>(&opt), sizeof(opt)) == SOCKET_ERROR)
734  {
735  SetError("could not enable dual-stack socket", true);
736  return false;
737  }
738 
739 #ifdef IPV6_ADDR_PREFERENCES
740  // Prefer stable addresses. This should prevent issues with address
741  // deprecation while a match is running. No error handling - if the call
742  // fails, we just take any address.
743  opt = IPV6_PREFER_SRC_PUBLIC;
744  setsockopt(socket, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES, reinterpret_cast<char*>(&opt), sizeof(opt));
745 #endif
746 
747  return true;
748 }
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750

References SetError(), and SOCKET_ERROR.

Referenced by C4NetIOTCP::CreateSocket(), C4NetIOSimpleUDP::Init(), and C4NetIOTCP::Listen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsLowPriority()

virtual bool StdSchedulerProc::IsLowPriority ( )
inlinevirtualinherited

Definition at line 82 of file StdScheduler.h.

82 { return false; }

◆ IsNotify()

bool C4NetIO::IsNotify ( )
inlineoverridevirtual

Reimplemented from StdSchedulerProc.

Definition at line 260 of file C4NetIO.h.

260 { return true; }

◆ IsSignaled()

bool StdSchedulerProc::IsSignaled ( )
inherited

◆ ResetError()

void C4NetIO::ResetError ( )
inline

Definition at line 287 of file C4NetIO.h.

287 { Error.Clear(); }
void Clear()
Definition: StdBuf.h:466

References StdStrBuf::Clear(), and Error.

Referenced by C4NetIO(), C4NetIOTCP::Close(), C4NetIOSimpleUDP::Close(), C4NetIOUDP::CloseBroadcast(), C4Network2IO::ConnectWithSocket(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::Execute(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::InitBroadcast(), C4NetIOSimpleUDP::Send(), and C4Network2IOConnection::Send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Send()

virtual bool C4NetIO::Send ( const class C4NetIOPacket rPacket)
pure virtual

Referenced by main(), MyCBClass::OnConn(), MyCBClass::OnPacket(), C4Network2IOConnection::Send(), and C4Network2IO::SendPuncherPacket().

Here is the caller graph for this function:

◆ SetBroadcast()

virtual bool C4NetIO::SetBroadcast ( const addr_t addr,
bool  fSet = true 
)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IOConnection::SetBroadcastTarget().

Here is the caller graph for this function:

◆ SetCallback()

virtual void C4NetIO::SetCallback ( CBClass pnCallback)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4NetIOMan::AddIO(), and C4Network2IO::Init().

Here is the caller graph for this function:

◆ SetError()

void C4NetIO::SetError ( const char *  strnError,
bool  fSockErr = false 
)
protected

Definition at line 750 of file C4NetIO.cpp.

751 {
752  fSockErr &= HaveSocketError();
753  if (fSockErr)
754  Error.Format("%s (%s)", strnError, GetSocketErrorMsg());
755  else
756  Error.Copy(strnError);
757 }
bool HaveSocketError()
Definition: C4NetIO.cpp:199
const char * GetSocketErrorMsg(int iError)
Definition: C4NetIO.cpp:190
void Copy()
Definition: StdBuf.h:467
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174

References StdStrBuf::Copy(), Error, StdStrBuf::Format(), GetSocketErrorMsg(), and HaveSocketError().

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::Bind(), C4NetIOSimpleUDP::CloseBroadcast(), C4NetIOTCP::Connect(), C4Network2IRCClient::Connect(), C4NetIOTCP::CreateSocket(), C4NetIOUDP::DoLoopbackTest(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::Execute(), C4NetIOTCP::Execute(), C4NetIOTCP::Init(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::InitBroadcast(), InitIPv6Socket(), C4Network2IRCClient::Join(), C4NetIOTCP::Listen(), C4NetIOSimpleUDP::Send(), C4Network2IRCClient::Send(), C4NetIOTCP::UnBlock(), and C4NetIOSimpleUDP::UnBlock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TimerInterval()

virtual uint32_t StdSchedulerProc::TimerInterval ( )
inlinevirtualinherited

Reimplemented in CStdTimerProc.

Definition at line 84 of file StdScheduler.h.

84 { return 0; }

Member Data Documentation

◆ Error

StdCopyStrBuf C4NetIO::Error
protected

Definition at line 283 of file C4NetIO.h.

Referenced by GetError(), ResetError(), and SetError().

◆ TO_INF

const int C4NetIO::TO_INF = -1
static

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