forked from Mirrors/openclonk
Remove ResolveAddress()
As setting a default port is a common operation, add a helper function for this.ipv6
parent
60560125dc
commit
c94b4cc3b0
|
@ -372,9 +372,10 @@ void C4NetIO::HostAddress::SetHost(uint32_t v4addr)
|
|||
memset(&v4.sin_zero, 0, sizeof(v4.sin_zero));
|
||||
}
|
||||
|
||||
void C4NetIO::HostAddress::SetHost(const StdStrBuf &addr)
|
||||
void C4NetIO::HostAddress::SetHost(const StdStrBuf &addr, AddressFamily family)
|
||||
{
|
||||
addrinfo hints = addrinfo();
|
||||
hints.ai_family = family;
|
||||
addrinfo *addresses = nullptr;
|
||||
if (getaddrinfo(addr.getData(), nullptr, &hints, &addresses) != 0)
|
||||
// GAI failed
|
||||
|
@ -383,7 +384,7 @@ void C4NetIO::HostAddress::SetHost(const StdStrBuf &addr)
|
|||
freeaddrinfo(addresses);
|
||||
}
|
||||
|
||||
void C4NetIO::EndpointAddress::SetAddress(const StdStrBuf &addr)
|
||||
void C4NetIO::EndpointAddress::SetAddress(const StdStrBuf &addr, AddressFamily family)
|
||||
{
|
||||
Clear();
|
||||
|
||||
|
@ -439,6 +440,7 @@ void C4NetIO::EndpointAddress::SetAddress(const StdStrBuf &addr)
|
|||
}
|
||||
|
||||
addrinfo hints = addrinfo();
|
||||
hints.ai_family = family;
|
||||
//hints.ai_flags = AI_NUMERICHOST;
|
||||
addrinfo *addresses = nullptr;
|
||||
if (getaddrinfo(std::string(ab, ae).c_str(), pb != end ? std::string(pb, pe).c_str() : nullptr, &hints, &addresses) != 0)
|
||||
|
@ -499,6 +501,12 @@ void C4NetIO::EndpointAddress::SetPort(uint16_t port)
|
|||
}
|
||||
}
|
||||
|
||||
void C4NetIO::EndpointAddress::SetDefaultPort(uint16_t port)
|
||||
{
|
||||
if (GetPort() == IPPORT_NONE)
|
||||
SetPort(port);
|
||||
}
|
||||
|
||||
uint16_t C4NetIO::EndpointAddress::GetPort() const
|
||||
{
|
||||
switch (gen.sa_family)
|
||||
|
@ -3731,50 +3739,3 @@ void C4NetIOMan::EnlargeIO(int iBy)
|
|||
delete[] ppNetIO;
|
||||
ppNetIO = ppnNetIO;
|
||||
}
|
||||
|
||||
// *** helpers
|
||||
|
||||
bool ResolveAddress(const char *szAddress, C4NetIO::addr_t *paddr, uint16_t iPort)
|
||||
{
|
||||
assert(szAddress && paddr);
|
||||
// port?
|
||||
StdStrBuf Buf;
|
||||
const char *pColon = strchr(szAddress, ':');
|
||||
if (pColon)
|
||||
{
|
||||
// get port
|
||||
iPort = atoi(pColon + 1);
|
||||
// copy address
|
||||
Buf.CopyUntil(szAddress, ':');
|
||||
szAddress = Buf.getData();
|
||||
}
|
||||
// set up address
|
||||
sockaddr_in raddr; ZeroMem(&raddr, sizeof raddr);
|
||||
raddr.sin_family = AF_INET;
|
||||
raddr.sin_port = htons(iPort);
|
||||
// no plain IP address?
|
||||
if ((raddr.sin_addr.s_addr = inet_addr(szAddress)) == INADDR_NONE)
|
||||
{
|
||||
#ifdef HAVE_WINSOCK
|
||||
if (!AcquireWinSock()) return false;
|
||||
#endif
|
||||
// resolve
|
||||
hostent *pHost;
|
||||
if (!(pHost = gethostbyname(szAddress)))
|
||||
#ifdef HAVE_WINSOCK
|
||||
{ ReleaseWinSock(); return false; }
|
||||
ReleaseWinSock();
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
// correct type?
|
||||
if (pHost->h_addrtype != AF_INET || pHost->h_length != sizeof(in_addr))
|
||||
return false;
|
||||
// get address
|
||||
raddr.sin_addr = *reinterpret_cast<in_addr *>(pHost->h_addr_list[0]);
|
||||
}
|
||||
// ok
|
||||
paddr->SetAddress(reinterpret_cast<sockaddr*>(&raddr));
|
||||
paddr->SetPort(iPort);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -102,7 +102,7 @@ public:
|
|||
void SetHost(const sockaddr *addr);
|
||||
void SetHost(const HostAddress &host);
|
||||
void SetHost(SpecialAddress host);
|
||||
void SetHost(const StdStrBuf &host);
|
||||
void SetHost(const StdStrBuf &host, AddressFamily family = UnknownFamily);
|
||||
void SetHost(uint32_t host);
|
||||
|
||||
C4NetIO::HostAddress AsIPv6() const; // convert an IPv4 address to an IPv6-mapped IPv4 address
|
||||
|
@ -148,13 +148,14 @@ public:
|
|||
void SetAddress(const EndpointAddress &other);
|
||||
void SetAddress(HostAddress::SpecialAddress addr, uint16_t port = IPPORT_NONE);
|
||||
void SetAddress(const HostAddress &host, uint16_t port = IPPORT_NONE);
|
||||
void SetAddress(const StdStrBuf &addr);
|
||||
void SetAddress(const StdStrBuf &addr, AddressFamily family = UnknownFamily);
|
||||
|
||||
HostAddress GetHost() const { return *this; } // HostAddress copy ctor slices off port information
|
||||
EndpointAddress AsIPv6() const; // convert an IPv4 address to an IPv6-mapped IPv4 address
|
||||
EndpointAddress AsIPv4() const; // try to convert an IPv6-mapped IPv4 address to an IPv4 address (returns unchanged address if not possible)
|
||||
|
||||
void SetPort(uint16_t port);
|
||||
void SetDefaultPort(uint16_t port); // set a port only if there is none
|
||||
uint16_t GetPort() const;
|
||||
|
||||
bool IsNull() const;
|
||||
|
@ -950,6 +951,5 @@ private:
|
|||
bool AcquireWinSock();
|
||||
void ReleaseWinSock();
|
||||
#endif
|
||||
bool ResolveAddress(const char *szAddress, C4NetIO::addr_t *paddr, uint16_t iPort);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -946,8 +946,12 @@ void C4Network2::InitPuncher()
|
|||
{
|
||||
// We have an internet connection, so let's punch the puncher server here in order to open an udp port
|
||||
C4NetIO::addr_t PuncherAddr;
|
||||
if (ResolveAddress(getNetpuncherAddr().getData(), &PuncherAddr, C4NetStdPortPuncher))
|
||||
NetIO.InitPuncher(PuncherAddr);
|
||||
PuncherAddr.SetAddress(getNetpuncherAddr(), C4NetIO::HostAddress::IPv4);
|
||||
if (!PuncherAddr.IsNull())
|
||||
{
|
||||
PuncherAddr.SetDefaultPort(C4NetStdPortPuncher);
|
||||
NetIO.InitPuncher(PuncherAddr);
|
||||
}
|
||||
}
|
||||
|
||||
void C4Network2::OnGameSynchronized()
|
||||
|
|
|
@ -318,8 +318,10 @@ bool C4Network2IRCClient::Connect(const char *szServer, const char *szNick, cons
|
|||
if (!Init())
|
||||
return false;
|
||||
// Resolve address
|
||||
if (!ResolveAddress(szServer, &ServerAddr, 6666))
|
||||
ServerAddr.SetAddress(StdStrBuf(szServer));
|
||||
if (ServerAddr.IsNull())
|
||||
{ SetError("Could no resolve server address!"); return false; }
|
||||
ServerAddr.SetDefaultPort(6666);
|
||||
// Set connection data
|
||||
Nick = szNick; RealName = szRealName;
|
||||
Password = szPassword; AutoJoin = szAutoJoin;
|
||||
|
|
|
@ -566,10 +566,7 @@ bool C4Network2HTTPClient::SetServer(const char *szServerAddress)
|
|||
SetError(FormatString("Could not resolve server address %s!", Server.getData()).getData());
|
||||
return false;
|
||||
}
|
||||
if (ServerAddr.GetPort() == C4NetIO::EndpointAddress::IPPORT_NONE)
|
||||
{
|
||||
ServerAddr.SetPort(GetDefaultPort());
|
||||
}
|
||||
ServerAddr.SetDefaultPort(GetDefaultPort());
|
||||
// Remove port
|
||||
const char *firstColon = strchr(Server.getData(), ':');
|
||||
const char *lastColon = strrchr(Server.getData(), ':');
|
||||
|
|
|
@ -158,7 +158,8 @@ bool C4AulDebug::SetAllowed(const char *szHost)
|
|||
// No host?
|
||||
if (!szHost || !*szHost) return true;
|
||||
// Resolve the address
|
||||
return ResolveAddress(szHost, &AllowedAddr, 0);
|
||||
AllowedAddr.SetAddress(StdStrBuf(szHost));
|
||||
return !AllowedAddr.IsNull();
|
||||
}
|
||||
|
||||
bool C4AulDebug::Init(uint16_t iPort)
|
||||
|
|
Loading…
Reference in New Issue