OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Network2IODiscoverClient Class Referenceabstract

#include <C4Network2Discover.h>

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

Public Types

typedef EndpointAddress addr_t
 

Public Member Functions

 C4Network2IODiscoverClient ()
 
int getDiscoverCount () const
 
const C4NetIO::addr_tgetDiscover (int i)
 
void Clear ()
 
bool Init (uint16_t iPort=C4NetIO::addr_t::IPPORT_NONE) override
 
bool StartDiscovery ()
 
bool PopDiscover (C4NetIO::addr_t &Discover)
 
virtual bool InitBroadcast (addr_t *pBroadcastAddr)
 
bool Close () override
 
bool Close (const addr_t &addr) override
 
virtual bool CloseBroadcast ()
 
bool Execute (int iMaxTime=TO_INF, pollfd *=nullptr) override
 
bool Send (const C4NetIOPacket &rPacket) override
 
virtual bool Send (const class C4NetIOPacket &rPacket)=0
 
bool Broadcast (const C4NetIOPacket &rPacket) override
 
virtual bool Broadcast (const class C4NetIOPacket &rPacket)=0
 
virtual void UnBlock ()
 
void GetFDs (std::vector< struct pollfd > &FDs) override
 
bool Connect (const addr_t &addr) override
 
bool SetBroadcast (const addr_t &addr, bool fSet=true) 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
 
bool IsNotify () override
 
virtual const char * GetError () const
 
void ResetError ()
 
bool ExecuteUntil (int iTimeout=-1)
 
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

void OnPacket (const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override
 
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

StdCopyStrBuf Error
 

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 48 of file C4Network2Discover.h.

Member Typedef Documentation

typedef EndpointAddress C4NetIO::addr_t
inherited

Definition at line 211 of file C4NetIO.h.

Constructor & Destructor Documentation

C4Network2IODiscoverClient::C4Network2IODiscoverClient ( )
inline

Definition at line 51 of file C4Network2Discover.h.

References C4NetIOSimpleUDP::SetCallback().

void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:571

Here is the call graph for this function:

Member Function Documentation

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

Definition at line 2138 of file C4NetIO.cpp.

References C4NetIOPacket::getRef(), and C4NetIOSimpleUDP::Send().

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

2139 {
2140  // just set broadcast address and send
2141  return C4NetIOSimpleUDP::Send(C4NetIOPacket(rPacket.getRef(), MCAddr));
2142 }
C4NetIOPacket getRef() const
Definition: C4NetIO.h:323
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2118

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 C4Network2IODiscoverClient::Clear ( )
inline

Definition at line 63 of file C4Network2Discover.h.

63 { iDiscoverCount = 0; }
void C4NetIOSimpleUDP::ClearStatistic ( )
inlineoverridevirtualinherited

Implements C4NetIO.

Reimplemented in C4NetIOUDP.

Definition at line 528 of file C4NetIO.h.

528 { assert(false); }
bool C4NetIOSimpleUDP::Close ( )
overridevirtualinherited

Implements C4NetIO.

Reimplemented in C4NetIOUDP.

Definition at line 1979 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::CloseBroadcast(), closesocket, INVALID_SOCKET, and C4NetIO::ResetError().

Referenced by C4NetIOUDP::Close(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::~C4NetIOSimpleUDP(), and C4StartupNetDlg::~C4StartupNetDlg().

1980 {
1981  // should be initialized
1982  if (!fInit) return true;
1983 
1984  ResetError();
1985 
1986  // deactivate multicast
1987  if (fMultiCast)
1988  CloseBroadcast();
1989 
1990  // close sockets
1991  if (sock != INVALID_SOCKET)
1992  {
1993  closesocket(sock);
1994  sock = INVALID_SOCKET;
1995  }
1996 
1997 #ifdef STDSCHEDULER_USE_EVENTS
1998  // close event
1999  if (hEvent != nullptr)
2000  {
2001  WSACloseEvent(hEvent);
2002  hEvent = nullptr;
2003  }
2004 #else
2005  // close pipes
2006  close(Pipe[0]);
2007  close(Pipe[1]);
2008 #endif
2009 
2010 #ifdef HAVE_WINSOCK
2011  // release winsock
2012  ReleaseWinSock();
2013 #endif
2014 
2015  // ok
2016  fInit = false;
2017  return false;
2018 }
void ResetError()
Definition: C4NetIO.h:285
#define INVALID_SOCKET
Definition: C4NetIO.h:36
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2020
#define closesocket
Definition: C4NetIO.cpp:46

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::Close ( const addr_t addr)
inlineoverridevirtualinherited

Implements C4NetIO.

Reimplemented in C4NetIOUDP.

Definition at line 521 of file C4NetIO.h.

521 { assert(false); return false; }
bool C4NetIOSimpleUDP::CloseBroadcast ( )
virtualinherited

Reimplemented in C4NetIOUDP.

Definition at line 2020 of file C4NetIO.cpp.

References C4NetIO::SetError(), and SOCKET_ERROR.

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

2021 {
2022  // multicast not active?
2023  if (!fMultiCast) return true;
2024 
2025  // leave multicast group
2026  if (setsockopt(sock, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP,
2027  reinterpret_cast<const char *>(&MCGrpInfo), sizeof(MCGrpInfo)) == SOCKET_ERROR)
2028  {
2029  SetError("could not leave multicast group"); // to do: more error information
2030  return false;
2031  }
2032 
2033  // ok
2034  fMultiCast = false;
2035  return true;
2036 }
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:

bool C4NetIOSimpleUDP::Connect ( const addr_t addr)
inlineoverridevirtualinherited

Implements C4NetIO.

Reimplemented in C4NetIOUDP.

Definition at line 520 of file C4NetIO.h.

520 { assert(false); return false; }
bool C4NetIOSimpleUDP::Execute ( int  iMaxTime = TO_INF,
pollfd *  = nullptr 
)
overridevirtualinherited

Implements C4NetIO.

Reimplemented in C4NetIOUDP.

Definition at line 2038 of file C4NetIO.cpp.

References C4NetIO::HostAddress::GetFamily(), GetSocketErrorMsg(), HaveConnResetError(), ioctlsocket, StdBuf::New(), C4NetIO::CBClass::OnDisconn(), C4NetIO::CBClass::OnPacket(), C4NetIO::ResetError(), C4NetIOPacket::SetAddr(), C4NetIO::SetError(), StdBuf::SetSize(), SOCKET_ERROR, and C4NetIO::HostAddress::UnknownFamily.

Referenced by C4NetIOUDP::DoLoopbackTest(), C4NetIOUDP::Execute(), C4NetIOUDP::InitBroadcast(), and C4StartupNetDlg::OnSec1Timer().

2039 {
2040  if (!fInit) { SetError("not yet initialized"); return false; }
2041  ResetError();
2042 
2043 #ifdef __APPLE__
2044  iMaxTime = fix_poll_timeout(iMaxTime);
2045 #endif
2046 
2047  // wait for socket / timeout
2048  WaitResult eWR = WaitForSocket(iMaxTime);
2049  if (eWR == WR_Error) return false;
2050 
2051  // cancelled / timeout?
2052  if (eWR == WR_Cancelled || eWR == WR_Timeout) return true;
2053  assert(eWR == WR_Readable);
2054 
2055  // read packets from socket
2056  for (;;)
2057  {
2058  // how much can be read?
2059 #ifdef _WIN32
2060  u_long iMaxMsgSize;
2061 #else
2062  // The FIONREAD ioctl call takes an int on unix
2063  int iMaxMsgSize;
2064 #endif
2065  if (::ioctlsocket(sock, FIONREAD, &iMaxMsgSize) == SOCKET_ERROR)
2066  {
2067  SetError("Could not determine the amount of data that can be read from socket", true);
2068  return false;
2069  }
2070 
2071  // nothing?
2072  if (!iMaxMsgSize)
2073  break;
2074  // alloc buffer
2075  C4NetIOPacket Pkt; Pkt.New(iMaxMsgSize);
2076  // read data (note: it is _not_ garantueed that iMaxMsgSize bytes are available)
2077  addr_t SrcAddr; socklen_t iSrcAddrLen = sizeof(sockaddr_in6);
2078  int iMsgSize = ::recvfrom(sock, getMBufPtr<char>(Pkt), iMaxMsgSize, 0, &SrcAddr, &iSrcAddrLen);
2079  // error?
2080  if (iMsgSize == SOCKET_ERROR)
2081  {
2082  if (HaveConnResetError())
2083  {
2084  // this is actually some kind of notification: an ICMP msg (unreachable)
2085  // came back, so callback and continue reading
2086  if (pCB) pCB->OnDisconn(SrcAddr, this, GetSocketErrorMsg());
2087  continue;
2088  }
2089  else
2090  {
2091  // this is the real thing, though
2092  SetError("could not receive data from socket", true);
2093  return false;
2094  }
2095  }
2096  // invalid address?
2097  if ((iSrcAddrLen != sizeof(sockaddr_in) && iSrcAddrLen != sizeof(sockaddr_in6)) || SrcAddr.GetFamily() == addr_t::UnknownFamily)
2098  {
2099  SetError("recvfrom returned an invalid address");
2100  return false;
2101  }
2102  // again: nothing?
2103  if (!iMsgSize)
2104  // docs say that the connection has been closed (whatever that means for a connectionless socket...)
2105  // let's just pretend it didn't happen, but stop reading.
2106  break;
2107  // fill in packet information
2108  Pkt.SetSize(iMsgSize);
2109  Pkt.SetAddr(SrcAddr);
2110  // callback
2111  if (pCB) pCB->OnPacket(Pkt, this);
2112  }
2113 
2114  // ok
2115  return true;
2116 }
void ResetError()
Definition: C4NetIO.h:285
const char * GetSocketErrorMsg(int iError)
Definition: C4NetIO.cpp:190
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
#define ioctlsocket
Definition: C4NetIO.cpp:45
bool HaveConnResetError()
Definition: C4NetIO.cpp:207
void SetSize(size_t inSize)
Definition: StdBuf.h:204
EndpointAddress addr_t
Definition: C4NetIO.h:211
void New(size_t inSize)
Definition: StdBuf.h:146
virtual void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason)
Definition: C4NetIO.h:220
virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)=0
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:326
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the call graph for this function:

Here is the caller 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 C4NetIOSimpleUDP::GetConnStatistic ( const addr_t addr,
int *  pIRate,
int *  pORate,
int *  pLoss 
)
inlineoverridevirtualinherited

Implements C4NetIO.

Reimplemented in C4NetIOUDP.

Definition at line 526 of file C4NetIO.h.

527  { assert(false); return false; }
const C4NetIO::addr_t& C4Network2IODiscoverClient::getDiscover ( int  i)
inline

Definition at line 61 of file C4Network2Discover.h.

61 { return Discovers[i]; }
int C4Network2IODiscoverClient::getDiscoverCount ( ) const
inline

Definition at line 60 of file C4Network2Discover.h.

Referenced by PopDiscover().

60 { return iDiscoverCount; }

Here is the caller 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 StdSchedulerProc::GetNextTick ( C4TimeMilliseconds  tNow)
virtualinherited

Reimplemented in C4NetIOUDP, C4Network2HTTPClient, C4Network2IO, and CStdTimerProc.

Definition at line 115 of file StdScheduler.cpp.

References C4TimeMilliseconds::PositiveInfinity.

Referenced by C4Network2HTTPClient::GetNextTick().

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::GetStatistic ( int *  pBroadcastRate)
inlineoverridevirtualinherited

Implements C4NetIO.

Reimplemented in C4NetIOUDP.

Definition at line 525 of file C4NetIO.h.

525 { assert(false); return false; }
bool C4Network2IODiscoverClient::Init ( uint16_t  iPort = C4NetIO::addr_t::IPPORT_NONE)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 89 of file C4Network2Discover.cpp.

References C4NetDiscoveryAddress, C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIO::EndpointAddress::SetAddress(), C4NetIOSimpleUDP::SetCallback(), C4NetIOSimpleUDP::SetMCLoopback(), C4NetIO::EndpointAddress::SetPort(), and C4NetIOSimpleUDP::SetReUseAddress().

Referenced by C4StartupNetDlg::C4StartupNetDlg().

90 {
91  // Reuse address
93  // Bind to port
94  if (!C4NetIOSimpleUDP::Init(iPort))
95  return false;
96  // Set callback
98  // Build broadcast address
99  DiscoveryAddr.SetAddress(C4NetDiscoveryAddress);
100  DiscoveryAddr.SetPort(iPort);
101  // Initialize broadcast
102  if (!C4NetIOSimpleUDP::InitBroadcast(&DiscoveryAddr))
103  return false;
104  // Enable multicast loopback
105  return C4NetIOSimpleUDP::SetMCLoopback(true);
106 }
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2228
virtual bool InitBroadcast(addr_t *pBroadcastAddr)
Definition: C4NetIO.cpp:1924
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:571
const StdStrBuf C4NetDiscoveryAddress
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
void SetPort(uint16_t port)
Definition: C4NetIO.cpp:526
void SetReUseAddress(bool fAllow)
Definition: C4NetIO.cpp:2240
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:1836

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::InitBroadcast ( addr_t pBroadcastAddr)
virtualinherited

Reimplemented in C4NetIOUDP.

Definition at line 1924 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::CloseBroadcast(), C4NetIO::HostAddress::GetFamily(), C4NetIO::EndpointAddress::GetPort(), C4NetIO::HostAddress::IPv6, C4NetIO::HostAddress::IsMulticast(), C4NetIO::ResetError(), C4NetIO::SetError(), C4NetIOSimpleUDP::SetMCLoopback(), and SOCKET_ERROR.

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

1925 {
1926  // no error... yet
1927  ResetError();
1928 
1929  // security
1930  if (!pBroadcastAddr) return false;
1931 
1932  // Init() has to be called first
1933  if (!fInit) return false;
1934  // already activated?
1935  if (fMultiCast) CloseBroadcast();
1936 
1937  // broadcast addr valid?
1938  if (!pBroadcastAddr->IsMulticast() || pBroadcastAddr->GetFamily() != HostAddress::IPv6)
1939  {
1940  SetError("invalid broadcast address (only IPv6 multicast addresses are supported)");
1941  return false;
1942  }
1943  if (pBroadcastAddr->GetPort() != iPort)
1944  {
1945  SetError("invalid broadcast address (different port)");
1946  return false;
1947  }
1948 
1949  // set mc ttl to somewhat about "same net"
1950  int TTL = 16;
1951  if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, reinterpret_cast<char*>(&TTL), sizeof(TTL)) == SOCKET_ERROR)
1952  {
1953  SetError("could not set mc ttl", true);
1954  return false;
1955  }
1956 
1957  // set up multicast group information
1958  this->MCAddr = *pBroadcastAddr;
1959  MCGrpInfo.ipv6mr_multiaddr = static_cast<sockaddr_in6>(MCAddr).sin6_addr;
1960  // TODO: do multicast on all interfaces?
1961  MCGrpInfo.ipv6mr_interface = 0; // default interface
1962 
1963  // join multicast group
1964  if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1965  reinterpret_cast<const char *>(&MCGrpInfo), sizeof(MCGrpInfo)) == SOCKET_ERROR)
1966  {
1967  SetError("could not join multicast group"); // to do: more error information
1968  return false;
1969  }
1970 
1971  // (try to) disable loopback (will set fLoopback accordingly)
1972  SetMCLoopback(false);
1973 
1974  // ok
1975  fMultiCast = true;
1976  return true;
1977 }
void ResetError()
Definition: C4NetIO.h:285
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2228
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2020
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the call graph for this function:

Here is the caller 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 C4Network2IODiscoverClient::OnPacket ( const class C4NetIOPacket rPacket,
C4NetIO pNetIO 
)
overrideprotectedvirtual

Implements C4NetIO::CBClass.

Definition at line 73 of file C4Network2Discover.cpp.

References C4NetMaxDiscover, C4NetIOPacket::getAddr(), StdBuf::getData(), StdBuf::getSize(), C4NetIOPacket::getStatus(), C4Network2IODiscoverReply::Port, and C4NetIO::EndpointAddress::SetPort().

74 {
75  // discovery?
76  if (rPacket.getSize() == sizeof(C4Network2IODiscoverReply) && rPacket.getStatus() == 4)
77  {
78  // save discovered address
79  if (iDiscoverCount < C4NetMaxDiscover)
80  {
81  const C4Network2IODiscoverReply *pReply = reinterpret_cast<const C4Network2IODiscoverReply *>(rPacket.getData());
82  Discovers[iDiscoverCount] = rPacket.getAddr();
83  Discovers[iDiscoverCount].SetPort(pReply->Port);
84  iDiscoverCount++;
85  }
86  }
87 }
void SetPort(uint16_t port)
Definition: C4NetIO.cpp:526
const int C4NetMaxDiscover

Here is the call graph for this function:

bool C4Network2IODiscoverClient::PopDiscover ( C4NetIO::addr_t Discover)

Definition at line 115 of file C4Network2Discover.cpp.

References getDiscoverCount().

116 {
117  // Discovers left?
118  if (!getDiscoverCount())
119  return false;
120  // Return one
121  Discover = Discovers[--iDiscoverCount];
122  return true;
123 }

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 C4NetIOSimpleUDP::Send ( const C4NetIOPacket rPacket)
overrideinherited

Definition at line 2118 of file C4NetIO.cpp.

References C4NetIOPacket::getAddr(), StdBuf::getSize(), HaveWouldBlockError(), C4NetIO::ResetError(), and C4NetIO::SetError().

Referenced by C4Network2IODiscover::Announce(), C4NetIOSimpleUDP::Broadcast(), C4NetIOUDP::SendDirect(), and StartDiscovery().

2119 {
2120  if (!fInit) { SetError("not yet initialized"); return false; }
2121 
2122  // send it
2123  C4NetIO::addr_t addr = rPacket.getAddr();
2124  if (::sendto(sock, getBufPtr<char>(rPacket), rPacket.getSize(), 0,
2125  &addr, sizeof(addr))
2126  != int(rPacket.getSize()) &&
2128  {
2129  SetError("socket sendto failed", true);
2130  return false;
2131  }
2132 
2133  // ok
2134  ResetError();
2135  return true;
2136 }
void ResetError()
Definition: C4NetIO.h:285
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
size_t getSize() const
Definition: StdBuf.h:101
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
bool HaveWouldBlockError()
Definition: C4NetIO.cpp:203

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::SetBroadcast ( const addr_t addr,
bool  fSet = true 
)
inlineoverridevirtualinherited

Implements C4NetIO.

Reimplemented in C4NetIOUDP.

Definition at line 523 of file C4NetIO.h.

523 { assert(false); return false; }
virtual void C4NetIO::SetCallback ( CBClass pnCallback)
pure virtualinherited

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

Here is the caller graph for this function:

void C4NetIOSimpleUDP::SetCallback ( CBClass *  pnCallback)
inlineoverrideinherited

Definition at line 571 of file C4NetIO.h.

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

571 { 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(), 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 Init().

2241 {
2242  fAllowReUse = fAllow;
2243 }

Here is the caller graph for this function:

bool C4Network2IODiscoverClient::StartDiscovery ( )

Definition at line 108 of file C4Network2Discover.cpp.

References C4NetIOSimpleUDP::Send().

Referenced by C4StartupNetDlg::C4StartupNetDlg(), C4StartupNetDlg::DoRefresh(), and C4StartupNetDlg::OnSec1Timer().

109 {
110  // Multicast discovery byte
111  char c = 3;
112  return Send(C4NetIOPacket(&c, sizeof(c), false, DiscoveryAddr));
113 }
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2118

Here is the call graph for this function:

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

const int C4NetIO::TO_INF = -1
staticinherited

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