OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4PuncherServer Class Referenceabstract
Inheritance diagram for C4PuncherServer:
[legend]
Collaboration diagram for C4PuncherServer:
[legend]

Public Types

typedef C4NetpuncherID::value CID
 
typedef EndpointAddress addr_t
 

Public Member Functions

 C4PuncherServer ()
 
bool Init (uint16_t iPort=addr_t::IPPORT_NONE) override
 
bool InitBroadcast (addr_t *pBroadcastAddr) override
 
bool Close () override
 
bool Close (const addr_t &addr) override
 
bool CloseBroadcast () override
 
bool Execute (int iMaxTime=TO_INF, pollfd *=nullptr) override
 
bool Connect (const addr_t &addr) override
 
bool Send (const C4NetIOPacket &rPacket) override
 
virtual bool Send (const class C4NetIOPacket &rPacket)=0
 
bool SendDirect (C4NetIOPacket &&rPacket)
 
bool Broadcast (const C4NetIOPacket &rPacket) override
 
virtual bool Broadcast (const class C4NetIOPacket &rPacket)=0
 
bool SetBroadcast (const addr_t &addr, bool fSet=true) override
 
C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow) override
 
bool GetStatistic (int *pBroadcastRate) override
 
bool GetConnStatistic (const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override
 
void ClearStatistic () override
 
void SetCallback (CBClass *pnCallback) override
 
virtual void SetCallback (CBClass *pnCallback)=0
 
virtual void UnBlock ()
 
void GetFDs (std::vector< struct pollfd > &FDs) override
 
bool IsNotify () override
 
virtual const char * GetError () const
 
void ResetError ()
 
bool ExecuteUntil (int iTimeout=-1)
 
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 Types

enum  IPTypeID {
  IPID_Ping = 0, IPID_Test = 1, IPID_Conn = 2, IPID_ConnOK = 3,
  IPID_AddAddr = 7, IPID_Data = 4, IPID_Check = 5, IPID_Close = 6
}
 

Protected Member Functions

bool BroadcastDirect (const Packet &rPacket, unsigned int iNr=~0u)
 
bool DoLoopbackTest ()
 
void ClearMCPackets ()
 
void AddPeer (Peer *pPeer)
 
PeerGetPeer (const addr_t &addr)
 
PeerConnectPeer (const addr_t &PeerAddr, bool fFailCallback)
 
void OnShareFree (CStdCSecEx *pCSec) override
 
void DoCheck ()
 
const addr_tgetMCAddr () const
 
bool SetMCLoopback (int fLoopback)
 
bool getMCLoopback () const
 
void SetReUseAddress (bool fAllow)
 
bool InitIPv6Socket (SOCKET socket)
 
void SetError (const char *strnError, bool fSockErr=false)
 
void Changed ()
 

Protected Attributes

CStdCSecEx PeerListCSec
 
CStdCSec PeerListAddCSec
 
CStdCSec OutCSec
 
bool fInit {false}
 
bool fMultiCast {false}
 
uint16_t iPort
 
PeerpPeerList {nullptr}
 
bool fSavePacket {false}
 
C4NetIOPacket LastPacket
 
addr_t MCLoopbackAddr
 
bool fDelayedLoopbackTest {false}
 
C4TimeMilliseconds tNextCheck
 
PacketList OPackets
 
unsigned int iOPacketCounter {0}
 
int iBroadcastRate {0}
 
CStdCSec StatCSec
 
CStdCSec ExecuteCSec
 
StdCopyStrBuf Error
 

Static Protected Attributes

static const unsigned int iVersion = 2
 
static const unsigned int iStdTimeout = 1000
 
static const unsigned int iCheckInterval = 1000
 
static const unsigned int iMaxOPacketBacklog = 10000
 
static const unsigned int iUDPHeaderSize = 8 + 24
 

Private Member Functions

virtual bool OnConn (const addr_t &AddrPeer, const addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO)
 
virtual void OnDisconn (const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason)
 

Detailed Description

Definition at line 25 of file main.cpp.

Member Typedef Documentation

typedef EndpointAddress C4NetIO::addr_t
inherited

Definition at line 211 of file C4NetIO.h.

Definition at line 28 of file main.cpp.

Member Enumeration Documentation

enum C4NetIOUDP::IPTypeID
protectedinherited
Enumerator
IPID_Ping 
IPID_Test 
IPID_Conn 
IPID_ConnOK 
IPID_AddAddr 
IPID_Data 
IPID_Check 
IPID_Close 

Definition at line 616 of file C4NetIO.h.

Constructor & Destructor Documentation

C4PuncherServer::C4PuncherServer ( )
inline

Definition at line 29 of file main.cpp.

References C4NetIOUDP::SetCallback().

29  {
31  rng = std::bind(std::uniform_int_distribution<CID>(1/*, max*/), std::ref(random_device));
32  }
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:918

Here is the call graph for this function:

Member Function Documentation

void C4NetIOUDP::AddPeer ( Peer pPeer)
protectedinherited

Definition at line 3691 of file C4NetIO.cpp.

References StdSchedulerProc::Changed(), C4NetIOUDP::Peer::Next, C4NetIOUDP::PeerListAddCSec, C4NetIOUDP::PeerListCSec, and C4NetIOUDP::pPeerList.

Referenced by C4NetIOUDP::ConnectPeer().

3692 {
3693  // get locks
3694  CStdShareLock PeerListLock(&PeerListCSec);
3695  CStdLock PeerListAddLock(&PeerListAddCSec);
3696  // add
3697  pPeer->Next = pPeerList;
3698  pPeerList = pPeer;
3699  Changed();
3700 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:854

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 2683 of file C4NetIO.cpp.

References C4NetIOUDP::PacketList::AddPacket(), C4NetIOUDP::BroadcastDirect(), C4NetIOUDP::Peer::doBroadcast(), C4NetIOPacket::Duplicate(), C4NetIOUDP::Packet::FragmentCnt(), C4NetIOUDP::iOPacketCounter, C4NetIOUDP::Peer::MultiCast(), C4NetIOUDP::Peer::Next, C4NetIOUDP::OPackets, C4NetIOUDP::Peer::Open(), C4NetIOUDP::OutCSec, C4NetIOUDP::Packet, C4NetIOUDP::PeerListCSec, C4NetIOUDP::pPeerList, and C4NetIOUDP::Peer::Send().

2684 {
2685  CStdShareLock PeerListLock(&PeerListCSec);
2686  // search: any client reachable via multicast?
2687  Peer *pPeer;
2688  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2689  if (pPeer->Open() && pPeer->MultiCast() && pPeer->doBroadcast())
2690  break;
2691  bool fSuccess = true;
2692  if (pPeer)
2693  {
2694  CStdLock OutLock(&OutCSec);
2695  // send it via multicast: encapsulate packet
2696  Packet *pPkt = new Packet(rPacket.Duplicate(), iOPacketCounter);
2697  iOPacketCounter += pPkt->FragmentCnt();
2698  // add to list
2699  OPackets.AddPacket(pPkt);
2700  // send it
2701  fSuccess &= BroadcastDirect(*pPkt);
2702  }
2703  // send to all clients connected via du, too
2704  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2705  if (pPeer->Open() && !pPeer->MultiCast() && pPeer->doBroadcast())
2706  pPeer->Send(rPacket);
2707  return true;
2708 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
friend class Packet
Definition: C4NetIO.h:687
PacketList OPackets
Definition: C4NetIO.h:877
CStdCSec OutCSec
Definition: C4NetIO.h:855
bool AddPacket(Packet *pPacket)
Definition: C4NetIO.cpp:3032
Peer * pPeerList
Definition: C4NetIO.h:863
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
bool BroadcastDirect(const Packet &rPacket, unsigned int iNr=~0u)
Definition: C4NetIO.cpp:3595
C4NetIOPacket Duplicate() const
Definition: C4NetIO.h:324
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

bool C4NetIOUDP::BroadcastDirect ( const Packet rPacket,
unsigned int  iNr = ~0u 
)
protectedinherited

Definition at line 3595 of file C4NetIO.cpp.

References C4NetIOUDP::Packet::FragmentCnt(), C4NetIOUDP::Packet::GetFragment(), C4NetIOUDP::Packet::GetNr(), and C4NetIOUDP::SendDirect().

Referenced by C4NetIOUDP::Broadcast().

3596 {
3597  // only one fragment?
3598  if (iNr + 1)
3599  return SendDirect(rPacket.GetFragment(iNr - rPacket.GetNr(), true));
3600  // send all fragments
3601  bool fSuccess = true;
3602  for (unsigned int iFrgm = 0; iFrgm < rPacket.FragmentCnt(); iFrgm++)
3603  fSuccess &= SendDirect(rPacket.GetFragment(iFrgm, true));
3604  return fSuccess;
3605 }
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3607

Here is the call graph for this function:

Here is the caller graph for this function:

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 108 of file StdScheduler.cpp.

References s.

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

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

Here is the caller graph for this function:

void C4NetIOUDP::ClearMCPackets ( )
protectedinherited

Definition at line 3673 of file C4NetIO.cpp.

References C4NetIOUDP::PacketList::Clear(), C4NetIOUDP::PacketList::ClearPackets(), C4NetIOUDP::Peer::GetMCAckPacketCounter(), C4NetIOUDP::Peer::Next, C4NetIOUDP::OPackets, C4NetIOUDP::OutCSec, C4NetIOUDP::PeerListCSec, and C4NetIOUDP::pPeerList.

3674 {
3675  CStdShareLock PeerListLock(&PeerListCSec);
3676  CStdLock OutLock(&OutCSec);
3677  // clear packets if no client is present
3678  if (!pPeerList)
3679  OPackets.Clear();
3680  else
3681  {
3682  // find minimum acknowledged packet number
3683  unsigned int iAckNr = pPeerList->GetMCAckPacketCounter();
3684  for (Peer *pPeer = pPeerList->Next; pPeer; pPeer = pPeer->Next)
3685  iAckNr = std::min(iAckNr, pPeerList->GetMCAckPacketCounter());
3686  // clear packets
3687  OPackets.ClearPackets(iAckNr);
3688  }
3689 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
PacketList OPackets
Definition: C4NetIO.h:877
unsigned int GetMCAckPacketCounter() const
Definition: C4NetIO.h:805
CStdCSec OutCSec
Definition: C4NetIO.h:855
Peer * pPeerList
Definition: C4NetIO.h:863
void ClearPackets(unsigned int iUntil)
Definition: C4NetIO.cpp:3076
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

void C4NetIOUDP::ClearStatistic ( )
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2756 of file C4NetIO.cpp.

References C4NetIOUDP::iBroadcastRate, C4NetIOUDP::Peer::Next, C4NetIOUDP::PeerListCSec, C4NetIOUDP::pPeerList, and C4NetIOUDP::StatCSec.

2757 {
2758  CStdShareLock PeerListLock(&PeerListCSec);
2759  // clear all peer statistics
2760  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2761  pPeer->ClearStatistics();
2762  // broadcast statistics
2763  CStdLock StatLock(&StatCSec);
2764  iBroadcastRate = 0;
2765 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
int iBroadcastRate
Definition: C4NetIO.h:881
Peer * pPeerList
Definition: C4NetIO.h:863
CStdCSec StatCSec
Definition: C4NetIO.h:882
friend class Peer
Definition: C4NetIO.h:850
bool C4NetIOUDP::Close ( )
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2577 of file C4NetIO.cpp.

References CStdShareLock::Clear(), C4NetIOSimpleUDP::Close(), C4NetIOUDP::CloseBroadcast(), C4NetIOUDP::fInit, C4NetIOUDP::fMultiCast, C4NetIOUDP::Peer::Next, C4NetIOUDP::PeerListCSec, and C4NetIOUDP::pPeerList.

Referenced by C4NetIOUDP::Init(), C4NetIOUDP::Peer::OnConn(), C4NetIOUDP::Peer::OnRecv(), C4NetIOUDP::Peer::OnTimeout(), C4NetIOUDP::Peer::Send(), C4NetIOUDP::~C4NetIOUDP(), and C4NetIOUDP::Peer::~Peer().

2578 {
2579  // should be initialized
2580  if (!fInit) return false;
2581 
2582  // close all peers
2583  CStdShareLock PeerListLock(&PeerListCSec);
2584  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2585  pPeer->Close("owner class closed");
2586  PeerListLock.Clear();
2587 
2588  // deactivate multicast
2589  if (fMultiCast)
2590  CloseBroadcast();
2591 
2592  // close UDP
2593  bool fSuccess = C4NetIOSimpleUDP::Close();
2594 
2595 #ifdef C4NETIO_DEBUG
2596  // close log
2597  CloseDebugLog();
2598 #endif
2599 
2600  // ok
2601  fInit = false;
2602  return fSuccess;
2603 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
bool Close() override
Definition: C4NetIO.cpp:1979
Peer * pPeerList
Definition: C4NetIO.h:863
bool CloseBroadcast() override
Definition: C4NetIO.cpp:2605
bool fInit
Definition: C4NetIO.h:858
bool fMultiCast
Definition: C4NetIO.h:859
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Close ( const addr_t addr)
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2661 of file C4NetIO.cpp.

References C4NetIOUDP::Peer::Close(), C4NetIOUDP::GetPeer(), and C4NetIOUDP::PeerListCSec.

2662 {
2663  CStdShareLock PeerListLock(&PeerListCSec);
2664  // find peer
2665  Peer *pPeer = GetPeer(addr);
2666  if (!pPeer) return false;
2667  // close
2668  pPeer->Close("closed");
2669  return true;
2670 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

bool C4NetIOUDP::CloseBroadcast ( )
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2605 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::CloseBroadcast(), C4NetIOUDP::fMultiCast, and C4NetIO::ResetError().

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

2606 {
2607  ResetError();
2608 
2609  // multicast not active?
2610  if (!fMultiCast) return true;
2611 
2612  // ok
2613  fMultiCast = false;
2615 }
void ResetError()
Definition: C4NetIO.h:285
bool fMultiCast
Definition: C4NetIO.h:859
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2020

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Connect ( const addr_t addr)
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2655 of file C4NetIO.cpp.

References C4NetIOUDP::ConnectPeer().

Referenced by C4NetIOUDP::Peer::OnRecv().

2656 {
2657  // connect
2658  return !! ConnectPeer(addr, true);
2659 }
Peer * ConnectPeer(const addr_t &PeerAddr, bool fFailCallback)
Definition: C4NetIO.cpp:3738

Here is the call graph for this function:

Here is the caller graph for this function:

C4NetIOUDP::Peer * C4NetIOUDP::ConnectPeer ( const addr_t PeerAddr,
bool  fFailCallback 
)
protectedinherited

Definition at line 3738 of file C4NetIO.cpp.

References C4NetIOUDP::AddPeer(), CStdLock::Clear(), C4NetIOUDP::Peer::Close(), C4NetIOUDP::Peer::Connect(), C4NetIOUDP::GetPeer(), C4NetIOUDP::Peer, C4NetIOUDP::PeerListAddCSec, and C4NetIOUDP::PeerListCSec.

Referenced by C4NetIOUDP::Connect().

3739 {
3740  CStdShareLock PeerListLock(&PeerListCSec);
3741  // lock so no new peer can be added after this point
3742  CStdLock PeerListAddLock(&PeerListAddCSec);
3743  // recheck: address already known?
3744  Peer *pnPeer = GetPeer(PeerAddr);
3745  if (pnPeer) return pnPeer;
3746  // create new Peer class
3747  pnPeer = new Peer(PeerAddr, this);
3748  if (!pnPeer) return nullptr;
3749  // add peer to list
3750  AddPeer(pnPeer);
3751  PeerListAddLock.Clear();
3752  // send connection request
3753  if (!pnPeer->Connect(fFailCallback)) { pnPeer->Close("connect failed"); return nullptr; }
3754  // ok (do not wait for peer)
3755  return pnPeer;
3756 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
void AddPeer(Peer *pPeer)
Definition: C4NetIO.cpp:3691
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:854
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOUDP::DoCheck ( )
protectedinherited

Definition at line 3758 of file C4NetIO.cpp.

References C4NetIOUDP::CheckPacketHdr::AskCount, C4NetIOUDP::fMultiCast, C4NetIOUDP::iCheckInterval, C4NetIOUDP::iOPacketCounter, C4NetIOUDP::IPID_Check, C4NetIOUDP::CheckPacketHdr::MCAskCount, C4NetIOUDP::Peer::MultiCast(), C4NetIOUDP::Peer::Next, C4TimeMilliseconds::Now(), C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::Peer::Open(), C4NetIOUDP::PeerListCSec, C4NetIOUDP::pPeerList, C4NetIOUDP::SendDirect(), C4NetIOUDP::PacketHdr::StatusByte, and C4NetIOUDP::tNextCheck.

Referenced by C4NetIOUDP::Peer::Check(), and C4NetIOUDP::Execute().

3759 {
3760  CStdShareLock PeerListLock(&PeerListCSec);
3761  // mc connection check?
3762  if (fMultiCast)
3763  {
3764  // only if a peer is connected via multicast
3765  Peer *pPeer;
3766  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3767  if (pPeer->Open() && pPeer->MultiCast())
3768  break;
3769  if (pPeer)
3770  {
3771  // set up packet
3772  CheckPacketHdr Pkt;
3773  Pkt.StatusByte = uint8_t(IPID_Check | 0x80);
3774  Pkt.Nr = iOPacketCounter;
3775  Pkt.AskCount = Pkt.MCAskCount = 0;
3776  // send it
3777  SendDirect(C4NetIOPacket(&Pkt, sizeof(Pkt)));
3778  }
3779  }
3780  // peer connection checks
3781  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3782  if (pPeer->Open())
3783  pPeer->Check();
3784 
3785  // set time for next check
3787 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
bool fMultiCast
Definition: C4NetIO.h:859
static const unsigned int iCheckInterval
Definition: C4NetIO.h:636
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3607
static C4TimeMilliseconds Now()
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::DoLoopbackTest ( )
protectedinherited

Definition at line 3634 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::Broadcast(), StdBuf::Compare(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::fSavePacket, C4NetIOPacket::getAddr(), C4NetIO::GetError(), C4NetIOSimpleUDP::getMCLoopback(), StdBuf::getSize(), C4NetIOUDP::IPID_Test, C4NetIOUDP::iStdTimeout, C4NetIOUDP::LastPacket, C4NetIOUDP::MCLoopbackAddr, C4NetIO::SetError(), C4NetIOSimpleUDP::SetMCLoopback(), and UnsyncedRandom().

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

3635 {
3636  // (try to) enable loopback
3638  // ensure loopback is activate
3639  if (!C4NetIOSimpleUDP::getMCLoopback()) return false;
3640 
3641  // send test packet
3642  const PacketHdr TestPacket = { uint8_t(IPID_Test | 0x80), UnsyncedRandom(UINT32_MAX) };
3643  if (!C4NetIOSimpleUDP::Broadcast(C4NetIOPacket(&TestPacket, sizeof(TestPacket))))
3644  return false;
3645 
3646  // wait for socket to become readable (should happen immediatly, do not expect packet loss)
3647  fSavePacket = true;
3649  {
3650  fSavePacket = false;
3651  if (!GetError()) SetError("Multicast disabled: loopback test failed");
3652  return false;
3653  }
3654  fSavePacket = false;
3655 
3656  // compare it to the packet that was sent
3657  if (LastPacket.getSize() != sizeof(TestPacket) ||
3658  LastPacket.Compare(&TestPacket, sizeof(TestPacket)))
3659  {
3660  SetError("Multicast disabled: loopback test failed");
3661  return false;
3662  }
3663 
3664  // save the loopback addr back
3666 
3667  // disable loopback
3669  // ok
3670  return true;
3671 }
bool getMCLoopback() const
Definition: C4NetIO.h:558
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2228
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
virtual const char * GetError() const
Definition: C4NetIO.h:284
bool fSavePacket
Definition: C4NetIO.h:866
bool Execute(int iMaxTime=TO_INF, pollfd *=nullptr) override
Definition: C4NetIO.cpp:2038
size_t getSize() const
Definition: StdBuf.h:101
C4NetIOPacket LastPacket
Definition: C4NetIO.h:867
int Compare(const void *pCData, size_t iCSize, size_t iAt=0) const
Definition: StdBuf.h:165
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
bool Broadcast(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2138
addr_t MCLoopbackAddr
Definition: C4NetIO.h:870
static const unsigned int iStdTimeout
Definition: C4NetIO.h:636

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Execute ( int  iMaxTime = TO_INF,
pollfd *  = nullptr 
)
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2617 of file C4NetIO.cpp.

References C4NetIOUDP::DoCheck(), C4NetIOUDP::DoLoopbackTest(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::ExecuteCSec, C4NetIOUDP::fDelayedLoopbackTest, C4NetIOUDP::fInit, C4NetIOUDP::fMultiCast, C4NetIOUDP::GetNextTick(), C4NetIOUDP::Peer::Next, C4TimeMilliseconds::Now(), C4NetIOUDP::PeerListCSec, C4NetIOUDP::pPeerList, C4NetIO::ResetError(), C4NetIO::SetError(), C4NetIOUDP::tNextCheck, and C4NetIO::TO_INF.

2618 {
2619  if (!fInit) { SetError("not yet initialized"); return false; }
2620 
2621  CStdLock ExecuteLock(&ExecuteCSec);
2622  CStdShareLock PeerListLock(&PeerListCSec);
2623 
2624  ResetError();
2625 
2626  // adjust maximum block time
2628  uint32_t iMaxBlock = std::max(tNow, GetNextTick(tNow)) - tNow;
2629  if (iMaxTime == TO_INF || iMaxTime > (int) iMaxBlock) iMaxTime = iMaxBlock;
2630 
2631  // execute subclass
2632  if (!C4NetIOSimpleUDP::Execute(iMaxTime))
2633  return false;
2634 
2635  // connection check needed?
2637  DoCheck();
2638  // client timeout?
2639  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2640  if (!pPeer->Closed())
2641  pPeer->CheckTimeout();
2642 
2643  // do a delayed loopback test once the incoming buffer is empty
2645  {
2646  if (fMultiCast)
2648  fDelayedLoopbackTest = false;
2649  }
2650 
2651  // ok
2652  return true;
2653 }
void ResetError()
Definition: C4NetIO.h:285
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow) override
Definition: C4NetIO.cpp:2721
bool DoLoopbackTest()
Definition: C4NetIO.cpp:3634
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
Peer * pPeerList
Definition: C4NetIO.h:863
bool Execute(int iMaxTime=TO_INF, pollfd *=nullptr) override
Definition: C4NetIO.cpp:2038
bool fDelayedLoopbackTest
Definition: C4NetIO.h:871
bool fInit
Definition: C4NetIO.h:858
bool fMultiCast
Definition: C4NetIO.h:859
static const int TO_INF
Definition: C4NetIO.h:66
void DoCheck()
Definition: C4NetIO.cpp:3758
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
CStdCSec ExecuteCSec
Definition: C4NetIO.h:906
static C4TimeMilliseconds Now()
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

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

Definition at line 33 of file StdScheduler.cpp.

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

Referenced by main().

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 }
virtual bool Execute(int iTimeout=-1, pollfd *readyfds=nullptr)=0
static C4TimeMilliseconds Now()

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::GetConnStatistic ( const addr_t addr,
int *  pIRate,
int *  pORate,
int *  pLoss 
)
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2743 of file C4NetIO.cpp.

References C4NetIOUDP::Peer::GetIRate(), C4NetIOUDP::Peer::GetORate(), C4NetIOUDP::GetPeer(), C4NetIOUDP::Peer::Open(), and C4NetIOUDP::PeerListCSec.

2744 {
2745  CStdShareLock PeerListLock(&PeerListCSec);
2746  // find peer
2747  Peer *pPeer = GetPeer(addr);
2748  if (!pPeer || !pPeer->Open()) return false;
2749  // return statistics
2750  if (pIRate) *pIRate = pPeer->GetIRate();
2751  if (pORate) *pORate = pPeer->GetORate();
2752  if (pLoss) *pLoss = 0;
2753  return true;
2754 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

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

Definition at line 284 of file C4NetIO.h.

References StdStrBuf::getData().

Referenced by C4Network2IO::Connect(), C4NetIOUDP::DoLoopbackTest(), C4StartupNetDlg::DoRefresh(), C4DownloadDlg::GetError(), C4Network2IO::Init(), C4NetIOUDP::InitBroadcast(), C4Network2::InitLeague(), C4Game::InitNetworkFromAddress(), C4Network2::LeagueEnd(), C4Network2::LeaguePlrAuthCheck(), C4Network2::LeagueStart(), C4Network2::LeagueUpdate(), C4Network2::LeagueUpdateProcessReply(), main(), MyCBClass::OnConn(), C4ChatControl::OnConnectBtn(), MyCBClass::OnPacket(), C4StartupNetListEntry::OnReference(), C4ChatControl::ProcessInput(), C4Network2IOConnection::Send(), and C4StartupNetListEntry::SetRefQuery().

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOSimpleUDP::GetFDs ( std::vector< struct pollfd > &  FDs)
overridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 2190 of file C4NetIO.cpp.

References INVALID_SOCKET.

2191 {
2192  // add pipe
2193  pollfd pfd = { Pipe[0], POLLIN, 0 };
2194  fds.push_back(pfd);
2195  // add socket
2196  if (sock != INVALID_SOCKET)
2197  {
2198  pollfd pfd = { sock, POLLIN, 0 };
2199  fds.push_back(pfd);
2200  }
2201 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
std::vector< C4NetIO::HostAddress > C4NetIO::GetLocalAddresses ( )
staticinherited

Definition at line 626 of file C4NetIO.cpp.

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

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

const addr_t& C4NetIOSimpleUDP::getMCAddr ( ) const
inlineprotectedinherited

Definition at line 554 of file C4NetIO.h.

Referenced by C4NetIOUDP::SendDirect().

554 { return MCAddr; }

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::getMCLoopback ( ) const
inlineprotectedinherited

Definition at line 558 of file C4NetIO.h.

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

558 { return fMCLoopback; }

Here is the caller graph for this function:

C4TimeMilliseconds C4NetIOUDP::GetNextTick ( C4TimeMilliseconds  tNow)
overridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 2721 of file C4NetIO.cpp.

References C4TimeMilliseconds::IsInfinite(), C4NetIOUDP::Peer::Next, C4NetIOUDP::PeerListCSec, C4NetIOUDP::pPeerList, and C4NetIOUDP::tNextCheck.

Referenced by C4NetIOUDP::Execute().

2722 {
2723  // maximum time: check interval
2724  C4TimeMilliseconds tTiming = tNextCheck.IsInfinite() ? tNow : std::max(tNow, tNextCheck);
2725 
2726  // client timeouts (e.g. connection timeout)
2727  CStdShareLock PeerListLock(&PeerListCSec);
2728  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2729  if (!pPeer->Closed())
2730  if (!pPeer->GetTimeout().IsInfinite())
2731  tTiming = std::min(tTiming, pPeer->GetTimeout());
2732  // return timing value
2733  return tTiming;
2734 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
bool IsInfinite() const
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

Here is the caller graph for this function:

C4NetIOUDP::Peer * C4NetIOUDP::GetPeer ( const addr_t addr)
protectedinherited

Definition at line 3728 of file C4NetIO.cpp.

References C4NetIOUDP::Peer::Next, C4NetIOUDP::PeerListCSec, and C4NetIOUDP::pPeerList.

Referenced by C4NetIOUDP::Close(), C4NetIOUDP::ConnectPeer(), C4NetIOUDP::GetConnStatistic(), C4NetIOUDP::Send(), and C4NetIOUDP::SetBroadcast().

3729 {
3730  CStdShareLock PeerListLock(&PeerListCSec);
3731  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3732  if (!pPeer->Closed())
3733  if (pPeer->GetAddr() == addr || pPeer->GetAltAddr() == addr)
3734  return pPeer;
3735  return nullptr;
3736 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
friend class Peer
Definition: C4NetIO.h:850

Here is the caller graph for this function:

bool C4NetIOUDP::GetStatistic ( int *  pBroadcastRate)
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2736 of file C4NetIO.cpp.

References C4NetIOUDP::iBroadcastRate, and C4NetIOUDP::StatCSec.

2737 {
2738  CStdLock StatLock(&StatCSec);
2739  if (pBroadcastRate) *pBroadcastRate = iBroadcastRate;
2740  return true;
2741 }
int iBroadcastRate
Definition: C4NetIO.h:881
CStdCSec StatCSec
Definition: C4NetIO.h:882
bool C4NetIOUDP::Init ( uint16_t  iPort = addr_t::IPPORT_NONE)
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2410 of file C4NetIO.cpp.

References C4NetIOUDP::Close(), C4NetIOUDP::fInit, C4NetIOUDP::fMultiCast, C4NetIOUDP::iCheckInterval, C4NetIOSimpleUDP::Init(), C4NetIOUDP::iPort, C4TimeMilliseconds::Now(), C4NetIOSimpleUDP::SetCallback(), and C4NetIOUDP::tNextCheck.

Referenced by main().

2411 {
2412  // already initialized? close first
2413  if (fInit) Close();
2414 
2415 #ifdef C4NETIO_DEBUG
2416  // open log
2417  OpenDebugLog();
2418 #endif
2419 
2420  // Initialize UDP
2421  if (!C4NetIOSimpleUDP::Init(inPort))
2422  return false;
2423  iPort = inPort;
2424 
2425  // set callback
2426  C4NetIOSimpleUDP::SetCallback(CBProxy(this));
2427 
2428  // set flags
2429  fInit = true;
2430  fMultiCast = false;
2431 
2433 
2434  // ok, that's all for now.
2435  // call InitBroadcast for more initialization fun
2436  return true;
2437 }
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:571
bool Close() override
Definition: C4NetIO.cpp:2577
uint16_t iPort
Definition: C4NetIO.h:860
bool fInit
Definition: C4NetIO.h:858
bool fMultiCast
Definition: C4NetIO.h:859
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:1836
static const unsigned int iCheckInterval
Definition: C4NetIO.h:636
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
static C4TimeMilliseconds Now()

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::InitBroadcast ( addr_t pBroadcastAddr)
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2439 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::Broadcast(), C4NetIOPacket::Clear(), C4NetIOSimpleUDP::CloseBroadcast(), C4NetIOUDP::CloseBroadcast(), C4NetIOUDP::DoLoopbackTest(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::fDelayedLoopbackTest, C4NetIOUDP::fInit, C4NetIOUDP::fMultiCast, C4NetIOUDP::fSavePacket, C4NetIOPacket::getAddr(), C4NetIO::GetError(), C4NetIO::GetLocalAddresses(), C4NetIOSimpleUDP::getMCLoopback(), C4NetIO::EndpointAddress::GetPort(), C4NetIOUDP::iBroadcastRate, C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::iOPacketCounter, C4NetIOUDP::IPID_Ping, C4NetIOUDP::iPort, C4NetIO::HostAddress::IPv6, C4NetIO::HostAddress::IsMulticast(), StdBuf::isNull(), C4NetIO::EndpointAddress::IsNull(), C4NetIOUDP::iStdTimeout, C4NetIOUDP::LastPacket, C4NetIOUDP::MCLoopbackAddr, C4NetIO::ResetError(), C4NetIO::EndpointAddress::SetAddress(), C4NetIO::SetError(), C4NetIOSimpleUDP::SetMCLoopback(), C4NetIO::EndpointAddress::SetPort(), and UnsyncedRandom().

2440 {
2441  // no error... yet
2442  ResetError();
2443 
2444  // security
2445  if (!pBroadcastAddr) return false;
2446 
2447  // Init() has to be called first
2448  if (!fInit) return false;
2449  // already activated?
2450  if (fMultiCast) CloseBroadcast();
2451 
2452  // set up multicast group information
2453  C4NetIO::addr_t MCAddr = *pBroadcastAddr;
2454 
2455  // broadcast addr valid?
2456  if (!MCAddr.IsMulticast())
2457  {
2458  // port is needed in order to search a mc address automatically
2459  if (!iPort)
2460  {
2461  SetError("broadcast address is not valid");
2462  return false;
2463  }
2464  // Set up address as unicast-prefix-based IPv6 multicast address (RFC 3306).
2465  sockaddr_in6 saddrgen = sockaddr_in6();
2466  saddrgen.sin6_family = AF_INET6;
2467  uint8_t *addrgen = saddrgen.sin6_addr.s6_addr;
2468  // ff3e ("global multicast based on network prefix") : 64 (length of network prefix)
2469  static const uint8_t mcast_prefix[4] = { 0xff, 0x3e, 0, 64};
2470  memcpy(addrgen, mcast_prefix, sizeof(mcast_prefix));
2471  addrgen += sizeof(mcast_prefix);
2472  // 64 bit network prefix
2473  addr_t prefixAddr;
2474  for (auto& addr : GetLocalAddresses())
2475  if (addr.GetFamily() == HostAddress::IPv6 && !addr.IsLocal())
2476  {
2477  prefixAddr.SetAddress(addr);
2478  break;
2479  }
2480  if (prefixAddr.IsNull())
2481  {
2482  SetError("no IPv6 unicast address available");
2483  return false;
2484  }
2485  static const size_t network_prefix_size = 8;
2486  memcpy(addrgen, &static_cast<sockaddr_in6*>(&prefixAddr)->sin6_addr, network_prefix_size);
2487  addrgen += network_prefix_size;
2488  // 32 bit group id: search for a free one
2489  for (int iRetries = 1000; iRetries; iRetries--)
2490  {
2491  uint32_t rnd = UnsyncedRandom();
2492  memcpy(addrgen, &rnd, sizeof(rnd));
2493  // "high-order bit of the Group ID will be the same value as the T flag"
2494  addrgen[0] |= 0x80;
2495  // create new - random - address
2496  MCAddr.SetAddress((sockaddr*) &saddrgen);
2497  MCAddr.SetPort(iPort);
2498  // init broadcast
2499  if (!C4NetIOSimpleUDP::InitBroadcast(&MCAddr))
2500  return false;
2501  // do the loopback test
2502  if (!DoLoopbackTest())
2503  {
2505  if (!GetError()) SetError("multicast loopback test failed");
2506  return false;
2507  }
2508  // send a ping packet
2509  const PacketHdr PingPacket = { IPID_Ping | static_cast<uint8_t>(0x80u), 0 };
2510  if (!C4NetIOSimpleUDP::Broadcast(C4NetIOPacket(&PingPacket, sizeof(PingPacket))))
2511  {
2513  return false;
2514  }
2515  bool fSuccess = false;
2516  for (;;)
2517  {
2518  fSavePacket = true; LastPacket.Clear();
2519  // wait for something to happen
2521  {
2522  fSavePacket = false;
2524  return false;
2525  }
2526  fSavePacket = false;
2527  // Timeout? So expect this address to be unused
2528  if (LastPacket.isNull()) { fSuccess = true; break; }
2529  // looped back?
2531  // ignore this one
2532  continue;
2533  // otherwise: there must be someone else in this MC group
2535  break;
2536  }
2537  if (fSuccess) break;
2538  // no success? try again...
2539  }
2540 
2541  // return found address
2542  *pBroadcastAddr = MCAddr;
2543  }
2544  else
2545  {
2546  // check: must be same port
2547  if (MCAddr.GetPort() == iPort)
2548  {
2549  SetError("invalid multicast address: wrong port");
2550  return false;
2551  }
2552  // init
2553  if (!C4NetIOSimpleUDP::InitBroadcast(&MCAddr))
2554  return false;
2555  // do loopback test (if not delayed)
2556  if (!fDelayedLoopbackTest)
2557  if (!DoLoopbackTest())
2558  {
2560  if (!GetError()) SetError("multicast loopback test failed");
2561  return false;
2562  }
2563  }
2564 
2565  // (try to) disable multicast loopback
2567 
2568  // set flags
2569  fMultiCast = true;
2570  iOPacketCounter = 0;
2571  iBroadcastRate = 0;
2572 
2573  // ok
2574  return true;
2575 }
void ResetError()
Definition: C4NetIO.h:285
bool IsMulticast() const
Definition: C4NetIO.cpp:242
bool getMCLoopback() const
Definition: C4NetIO.h:558
bool DoLoopbackTest()
Definition: C4NetIO.cpp:3634
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2228
virtual bool InitBroadcast(addr_t *pBroadcastAddr)
Definition: C4NetIO.cpp:1924
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
int iBroadcastRate
Definition: C4NetIO.h:881
virtual const char * GetError() const
Definition: C4NetIO.h:284
bool fSavePacket
Definition: C4NetIO.h:866
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
bool isNull() const
Definition: StdBuf.h:98
bool Execute(int iMaxTime=TO_INF, pollfd *=nullptr) override
Definition: C4NetIO.cpp:2038
C4NetIOPacket LastPacket
Definition: C4NetIO.h:867
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
bool CloseBroadcast() override
Definition: C4NetIO.cpp:2605
static std::vector< HostAddress > GetLocalAddresses()
Definition: C4NetIO.cpp:626
bool fDelayedLoopbackTest
Definition: C4NetIO.h:871
uint16_t iPort
Definition: C4NetIO.h:860
void SetPort(uint16_t port)
Definition: C4NetIO.cpp:526
bool Broadcast(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2138
addr_t MCLoopbackAddr
Definition: C4NetIO.h:870
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
bool fInit
Definition: C4NetIO.h:858
EndpointAddress addr_t
Definition: C4NetIO.h:211
bool fMultiCast
Definition: C4NetIO.h:859
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2020
void Clear()
Definition: C4NetIO.cpp:784
static const unsigned int iStdTimeout
Definition: C4NetIO.h:636
uint16_t GetPort() const
Definition: C4NetIO.cpp:542

Here is the call graph for this function:

bool C4NetIO::InitIPv6Socket ( SOCKET  socket)
protectedinherited

Definition at line 725 of file C4NetIO.cpp.

References C4NetIO::SetError(), and SOCKET_ERROR.

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool StdSchedulerProc::IsLowPriority ( )
inlinevirtualinherited

Definition at line 82 of file StdScheduler.h.

82 { return false; }
bool C4NetIO::IsNotify ( )
inlineoverridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 258 of file C4NetIO.h.

258 { return true; }
bool StdSchedulerProc::IsSignaled ( )
inherited
void C4NetIOUDP::OnShareFree ( CStdCSecEx pCSec)
overrideprotectedvirtualinherited

Implements CStdCSecExCallback.

Definition at line 3702 of file C4NetIO.cpp.

References C4NetIOUDP::Peer::Closed(), C4NetIOUDP::Peer::Next, C4NetIOUDP::PeerListCSec, and C4NetIOUDP::pPeerList.

3703 {
3704  if (pCSec == &PeerListCSec)
3705  {
3706  Peer *pPeer = pPeerList, *pLast = nullptr;
3707  while (pPeer)
3708  {
3709  // delete?
3710  if (pPeer->Closed())
3711  {
3712  // unlink
3713  Peer *pDelete = pPeer;
3714  (pLast ? pLast->Next : pPeerList) = pPeer = pPeer->Next;
3715  // delete
3716  delete pDelete;
3717  }
3718  else
3719  {
3720  // next peer
3721  pLast = pPeer;
3722  pPeer = pPeer->Next;
3723  }
3724  }
3725  }
3726 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

void C4NetIO::ResetError ( )
inlineinherited

Definition at line 285 of file C4NetIO.h.

References StdStrBuf::Clear().

Referenced by C4NetIO::C4NetIO(), C4NetIOTCP::Close(), C4NetIOSimpleUDP::Close(), C4NetIOUDP::CloseBroadcast(), C4Network2IO::Connect(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::Execute(), C4Network2RefClient::GetReferences(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::InitBroadcast(), C4Network2HTTPClient::Query(), C4Network2IOConnection::Send(), C4NetIOSimpleUDP::Send(), and C4Network2HTTPClient::SetServer().

285 { Error.Clear(); }
void Clear()
Definition: StdBuf.h:466
StdCopyStrBuf Error
Definition: C4NetIO.h:281

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Here is the caller graph for this function:

bool C4NetIOUDP::Send ( const C4NetIOPacket rPacket)
overrideinherited

Definition at line 2672 of file C4NetIO.cpp.

References C4NetIOPacket::getAddr(), C4NetIOUDP::GetPeer(), C4NetIOUDP::PeerListCSec, and C4NetIOUDP::Peer::Send().

2673 {
2674  // find Peer class for given address
2675  CStdShareLock PeerListLock(&PeerListCSec);
2676  Peer *pPeer = GetPeer(rPacket.getAddr());
2677  // not found?
2678  if (!pPeer) return false;
2679  // send the packet
2680  return pPeer->Send(rPacket);
2681 }
bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:3126
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

bool C4NetIOUDP::SendDirect ( C4NetIOPacket &&  rPacket)
inherited

Definition at line 3607 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::getMCAddr(), C4NetIOUDP::iBroadcastRate, C4NetIOUDP::IPID_Test, C4NetIOUDP::iUDPHeaderSize, C4NetIOSimpleUDP::Send(), C4NetIOPacket::SetAddr(), C4NetIOUDP::StatCSec, and UnsyncedRandom().

Referenced by C4NetIOUDP::BroadcastDirect(), C4NetIOUDP::Peer::DoCheck(), C4NetIOUDP::DoCheck(), C4NetIOUDP::Peer::DoConn(), C4NetIOUDP::Peer::OnRecv(), C4NetIOUDP::Peer::Send(), and C4NetIOUDP::Peer::SendDirect().

3608 {
3609  addr_t toaddr = rPacket.getAddr();
3610  // packet meant to be broadcasted?
3611  if (rPacket.getStatus() & 0x80)
3612  {
3613  // set addr
3614  toaddr = C4NetIOSimpleUDP::getMCAddr();
3615  // statistics
3616  CStdLock StatLock(&StatCSec);
3617  iBroadcastRate += rPacket.getSize() + iUDPHeaderSize;
3618  }
3619 
3620  // debug
3621 #ifdef C4NETIO_DEBUG
3622  { C4NetIOPacket Pkt2 = rPacket; Pkt2.SetAddr(toaddr); DebugLogPkt(true, Pkt2); }
3623 #endif
3624 
3625 #ifdef C4NETIO_SIMULATE_PACKETLOSS
3626  if ((rPacket.getStatus() & 0x7F) != IPID_Test)
3627  if (UnsyncedRandom(100) < C4NETIO_SIMULATE_PACKETLOSS) return true;
3628 #endif
3629 
3630  // send it
3631  return C4NetIOSimpleUDP::Send(C4NetIOPacket(rPacket.getRef(), toaddr));
3632 }
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
int iBroadcastRate
Definition: C4NetIO.h:881
C4NetIOPacket getRef() const
Definition: C4NetIO.h:323
static const unsigned int iUDPHeaderSize
Definition: C4NetIO.h:641
const addr_t & getMCAddr() const
Definition: C4NetIO.h:554
size_t getSize() const
Definition: StdBuf.h:101
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
EndpointAddress addr_t
Definition: C4NetIO.h:211
uint8_t getStatus() const
Definition: C4NetIO.h:317
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2118
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:326
CStdCSec StatCSec
Definition: C4NetIO.h:882

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::SetBroadcast ( const addr_t addr,
bool  fSet = true 
)
overridevirtualinherited

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2710 of file C4NetIO.cpp.

References C4NetIOUDP::GetPeer(), C4NetIOUDP::PeerListCSec, and C4NetIOUDP::Peer::SetBroadcast().

2711 {
2712  CStdShareLock PeerListLock(&PeerListCSec);
2713  // find peer
2714  Peer *pPeer = GetPeer(addr);
2715  if (!pPeer) return false;
2716  // set flag
2717  pPeer->SetBroadcast(fSet);
2718  return true;
2719 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

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

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

Here is the caller graph for this function:

void C4NetIOUDP::SetCallback ( CBClass *  pnCallback)
inlineoverrideinherited

Definition at line 918 of file C4NetIO.h.

Referenced by C4PuncherServer().

918 { pCB = pnCallback; };

Here is the caller graph for this function:

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

Definition at line 745 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::Accept(), C4NetIOSimpleUDP::CloseBroadcast(), C4Network2IRCClient::Connect(), C4NetIOTCP::Connect(), C4NetIOUDP::DoLoopbackTest(), C4NetIOTCP::Execute(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::Execute(), C4Network2RefClient::GetReferences(), C4Network2UpdateClient::GetUpdateURL(), C4Network2UpdateClient::GetVersion(), C4NetIOTCP::Init(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::InitBroadcast(), C4NetIO::InitIPv6Socket(), C4Network2IRCClient::Join(), C4NetIOTCP::Listen(), C4Network2IRCClient::Send(), C4NetIOSimpleUDP::Send(), C4Network2HTTPClient::SetServer(), C4NetIOTCP::UnBlock(), and C4NetIOSimpleUDP::UnBlock().

746 {
747  fSockErr &= HaveSocketError();
748  if (fSockErr)
749  Error.Format("%s (%s)", strnError, GetSocketErrorMsg());
750  else
751  Error.Copy(strnError);
752 }
const char * GetSocketErrorMsg(int iError)
Definition: C4NetIO.cpp:190
StdCopyStrBuf Error
Definition: C4NetIO.h:281
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
bool HaveSocketError()
Definition: C4NetIO.cpp:199
void Copy()
Definition: StdBuf.h:467

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::SetMCLoopback ( int  fLoopback)
protectedinherited

Definition at line 2228 of file C4NetIO.cpp.

References iSize, and SOCKET_ERROR.

Referenced by C4NetIOUDP::DoLoopbackTest(), C4Network2IODiscover::Init(), C4Network2IODiscoverClient::Init(), C4NetIOSimpleUDP::InitBroadcast(), and C4NetIOUDP::InitBroadcast().

2229 {
2230  // enable/disable MC loopback
2231  setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, reinterpret_cast<char *>(&fLoopback), sizeof fLoopback);
2232  // read result
2233  socklen_t iSize = sizeof(fLoopback);
2234  if (getsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, reinterpret_cast<char *>(&fLoopback), &iSize) == SOCKET_ERROR)
2235  return false;
2236  fMCLoopback = !! fLoopback;
2237  return true;
2238 }
int iSize
Definition: TstC4NetIO.cpp:32
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the caller graph for this function:

void C4NetIOSimpleUDP::SetReUseAddress ( bool  fAllow)
protectedinherited

Definition at line 2240 of file C4NetIO.cpp.

Referenced by C4Network2IODiscover::Init(), and C4Network2IODiscoverClient::Init().

2241 {
2242  fAllowReUse = fAllow;
2243 }

Here is the caller graph for this function:

virtual uint32_t StdSchedulerProc::TimerInterval ( )
inlinevirtualinherited

Reimplemented in CStdTimerProc.

Definition at line 84 of file StdScheduler.h.

Referenced by SCHAdditions::assignAdditionForProc:.

84 { return 0; }

Here is the caller graph for this function:

void C4NetIOSimpleUDP::UnBlock ( )
virtualinherited

Definition at line 2181 of file C4NetIO.cpp.

References C4NetIO::SetError().

2182 {
2183  // write one character to the pipe, this will unblock everything that
2184  // waits for the FD set returned by GetFDs.
2185  char c = 42;
2186  if (write(Pipe[1], &c, 1) == -1)
2187  SetError("write failed");
2188 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745

Here is the call graph for this function:

Member Data Documentation

CStdCSec C4NetIOUDP::ExecuteCSec
protectedinherited

Definition at line 906 of file C4NetIO.h.

Referenced by C4NetIOUDP::Execute().

bool C4NetIOUDP::fDelayedLoopbackTest {false}
protectedinherited

Definition at line 871 of file C4NetIO.h.

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

bool C4NetIOUDP::fInit {false}
protectedinherited
bool C4NetIOUDP::fSavePacket {false}
protectedinherited

Definition at line 866 of file C4NetIO.h.

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

int C4NetIOUDP::iBroadcastRate {0}
protectedinherited
const unsigned int C4NetIOUDP::iCheckInterval = 1000
staticprotectedinherited

Definition at line 636 of file C4NetIO.h.

Referenced by C4NetIOUDP::DoCheck(), and C4NetIOUDP::Init().

const unsigned int C4NetIOUDP::iMaxOPacketBacklog = 10000
staticprotectedinherited

Definition at line 639 of file C4NetIO.h.

unsigned int C4NetIOUDP::iOPacketCounter {0}
protectedinherited
uint16_t C4NetIOUDP::iPort
protectedinherited

Definition at line 860 of file C4NetIO.h.

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

const unsigned int C4NetIOUDP::iStdTimeout = 1000
staticprotectedinherited
const unsigned int C4NetIOUDP::iUDPHeaderSize = 8 + 24
staticprotectedinherited
const unsigned int C4NetIOUDP::iVersion = 2
staticprotectedinherited

Definition at line 631 of file C4NetIO.h.

C4NetIOPacket C4NetIOUDP::LastPacket
protectedinherited

Definition at line 867 of file C4NetIO.h.

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

addr_t C4NetIOUDP::MCLoopbackAddr
protectedinherited

Definition at line 870 of file C4NetIO.h.

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

PacketList C4NetIOUDP::OPackets
protectedinherited
CStdCSec C4NetIOUDP::OutCSec
protectedinherited
CStdCSec C4NetIOUDP::PeerListAddCSec
protectedinherited

Definition at line 854 of file C4NetIO.h.

Referenced by C4NetIOUDP::AddPeer(), and C4NetIOUDP::ConnectPeer().

C4TimeMilliseconds C4NetIOUDP::tNextCheck
protectedinherited
const int C4NetIO::TO_INF = -1
staticinherited

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