Permalink
Browse files

New network address to string function

  • Loading branch information...
mbasaglia committed Feb 19, 2016
1 parent 71e47f2 commit 0b934df1642f2c5f5c2ce30456d5b88856bc91cf
@@ -31,9 +31,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "engine/qcommon/qcommon.h"
#include "Network.h"
#ifdef _WIN32
# include <winsock2.h>
#else
# include <sys/socket.h>
# include <netinet/in.h>
#endif
// Helper functions defined in net_ip.cpp
void Sys_SockaddrToString( char *dest, int destlen, struct sockaddr *input );
void NetadrToSockadr( const netadr_t *a, struct sockaddr *s );
namespace Net {
void OutOfBandData( netsrc_t sock, netadr_t adr, byte *format, std::size_t len )
void OutOfBandData( netsrc_t sock, const netadr_t& adr, byte *format, std::size_t len )
{
if ( len == 0 )
{
@@ -53,5 +64,55 @@ void OutOfBandData( netsrc_t sock, netadr_t adr, byte *format, std::size_t len )
NET_SendPacket( sock, mbuf.cursize, mbuf.data, adr );
}
std::string AddressToString( const netadr_t& address, bool with_port )
{
if ( address.type == netadrtype_t::NA_LOOPBACK )
{
return "loopback";
}
else if ( address.type == netadrtype_t::NA_BOT )
{
return "bot";
}
else if ( address.type == netadrtype_t::NA_IP ||
address.type == netadrtype_t::NA_IP6 ||
address.type == netadrtype_t::NA_IP_DUAL )
{
char s[ NET_ADDR_STR_MAX_LEN ];
sockaddr_storage sadr = {0};
NetadrToSockadr( &address, reinterpret_cast<sockaddr*>(&sadr) );
Sys_SockaddrToString( s, sizeof(s), reinterpret_cast<sockaddr*>(&sadr) );
std::string result = s;
if ( with_port )
{
if ( NET_IS_IPv6( address.type ) )
{
result = '[' + result + ']';
}
result += ':' + std::to_string(ntohs(ExtractPort(address)));
}
return result;
}
return "";
}
unsigned short ExtractPort(const netadr_t& address)
{
if ( address.type == netadrtype_t::NA_IP_DUAL )
{
if ( NET_IS_IPv4( address.type ) )
{
return address.port4;
}
else if ( NET_IS_IPv6( address.type ) )
{
return address.port6;
}
}
return address.port;
}
} // namespace Net
@@ -39,7 +39,7 @@ inline const std::string& OOBHeader()
}
template<class... Args>
void OutOfBandPrint( netsrc_t net_socket, netadr_t adr, Str::StringRef format, Args&&... args )
void OutOfBandPrint( netsrc_t net_socket, const netadr_t& adr, Str::StringRef format, Args&&... args )
{
std::string message = OOBHeader() + Str::Format( format, std::forward<Args>(args)... );
NET_SendPacket( net_socket, message.size(), message.c_str(), adr );
@@ -48,7 +48,18 @@ void OutOfBandPrint( netsrc_t net_socket, netadr_t adr, Str::StringRef format, A
/*
* Sends huffman-compressed data
*/
void OutOfBandData( netsrc_t sock, netadr_t adr, byte* format, std::size_t len );
void OutOfBandData( netsrc_t sock, const netadr_t& adr, byte* format, std::size_t len );
/*
* Converts an address to its string representation
*/
std::string AddressToString( const netadr_t& address, bool with_port = false );
/*
* Returns port, port4 or port6 depending on the address type
*/
unsigned short ExtractPort(const netadr_t& address);
} // namespace Net
@@ -327,7 +327,7 @@ const char *NET_ErrorString()
#endif
}
static void NetadrToSockadr( netadr_t *a, struct sockaddr *s )
void NetadrToSockadr( const netadr_t *a, struct sockaddr *s )
{
memset( s, 0, sizeof( struct sockaddr ) );
if ( a->type == netadrtype_t::NA_BROADCAST )
@@ -483,7 +483,7 @@ static bool Sys_StringToSockaddr( const char *s, struct sockaddr *sadr, unsigned
Sys_SockaddrToString
=============
*/
static void Sys_SockaddrToString( char *dest, int destlen, struct sockaddr *input )
void Sys_SockaddrToString( char *dest, int destlen, struct sockaddr *input )
{
socklen_t inputlen;
@@ -204,7 +204,9 @@ void NET_SendPacket( netsrc_t sock, int length, const void *data, netadr_t
bool NET_CompareAdr( netadr_t a, netadr_t b );
bool NET_CompareBaseAdr( netadr_t a, netadr_t b );
bool NET_IsLocalAddress( netadr_t adr );
// DEPRECATED: Use Net::AddressToString
const char *NET_AdrToString( netadr_t a );
// DEPRECATED: Use Net::AddressToString
const char *NET_AdrToStringwPort( netadr_t a );
int NET_StringToAdr( const char *s, netadr_t *a, netadrtype_t family );
bool NET_GetLoopPacket( netsrc_t sock, netadr_t *net_from, msg_t *net_message );
@@ -329,7 +329,7 @@ static void SV_ResolveMasterServers()
if ( res )
{
Log::Notice( "%s resolved to %s\n", master.Cvar()->string, NET_AdrToStringwPort( master.ipv4 ) );
Log::Notice( "%s resolved to %s\n", master.Cvar()->string, Net::AddressToString(master.ipv4, true) );
}
else
{
@@ -350,7 +350,7 @@ static void SV_ResolveMasterServers()
if ( res )
{
Log::Notice( "%s resolved to %s\n", master.Cvar()->string, NET_AdrToStringwPort( master.ipv6 ) );
Log::Notice( "%s resolved to %s\n", master.Cvar()->string, Net::AddressToString(master.ipv6, true) );
}
else
{
@@ -497,7 +497,7 @@ void SV_MasterGameStat( const char *data )
}
Log::Notice( "%s resolved to %s", MASTER_SERVER_NAME,
NET_AdrToStringwPort( adr ) );
Net::AddressToString(adr, true) );
Log::Notice( "Sending gamestat to %s", MASTER_SERVER_NAME );
Net::OutOfBandPrint( netsrc_t::NS_SERVER, adr, "gamestat %s", data );
@@ -846,7 +846,7 @@ void SVC_RemoteCommand( netadr_t from, const Cmd::Args& args )
}
Log::Notice( "Bad rcon from %s:\n%s\n%s\n",
NET_AdrToString( from ),
Net::AddressToString( from ),
invalid_reason.c_str(),
args.ConcatArgs(2).c_str() );
@@ -857,7 +857,7 @@ void SVC_RemoteCommand( netadr_t from, const Cmd::Args& args )
}
else
{
Log::Notice( "Rcon from %s:\n%s\n", NET_AdrToString( from ), message.command().c_str() );
Log::Notice( "Rcon from %s:\n%s\n", Net::AddressToString( from ), message.command().c_str() );
// start redirecting all print outputs to the packet
auto env = RconEnvironment(from);
@@ -914,7 +914,7 @@ void SV_ConnectionlessPacket( netadr_t from, msg_t *msg )
return;
}
Log::Debug( "SV packet %s : %s", NET_AdrToString( from ), args.Argv(0).c_str() );
Log::Debug( "SV packet %s : %s", Net::AddressToString( from ), args.Argv(0) );
if ( args.Argv(0) == "getstatus" )
{
@@ -956,7 +956,7 @@ void SV_ConnectionlessPacket( netadr_t from, msg_t *msg )
}
else
{
Log::Debug( "bad connectionless packet from %s: %s", NET_AdrToString( from ), args.ConcatArgs(0).c_str() );
Log::Debug( "bad connectionless packet from %s: %s", Net::AddressToString( from ), args.ConcatArgs(0) );
}
}

0 comments on commit 0b934df

Please sign in to comment.