1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// netadr.h
#ifndef NETADR_H
#define NETADR_H
#ifdef _WIN32
#pragma once
#endif
#include "tier0/platform.h"
#undef SetPort
class bf_read;
class bf_write;
typedef enum
{
NA_NULL = 0,
NA_LOOPBACK,
NA_BROADCAST,
NA_IP,
} netadrtype_t;
typedef struct netadr_s
{
public:
netadr_s() { SetIP( 0 ); SetPort( 0 ); SetType( NA_IP ); }
netadr_s( uint unIP, uint16 usPort ) { SetIP( unIP ); SetPort( usPort ); SetType( NA_IP ); }
netadr_s( const char *pch ) { SetFromString( pch ); }
void Clear(); // invalids Address
void SetType( netadrtype_t type );
void SetPort( unsigned short port );
bool SetFromSockadr(const struct sockaddr *s);
void SetIP(uint8 b1, uint8 b2, uint8 b3, uint8 b4);
void SetIP(uint unIP); // Sets IP. unIP is in host order (little-endian)
void SetIPAndPort( uint unIP, unsigned short usPort ) { SetIP( unIP ); SetPort( usPort ); }
bool SetFromString(const char *pch, bool bUseDNS = false ); // if bUseDNS is true then do a DNS lookup if needed
bool CompareAdr (const netadr_s &a, bool onlyBase = false) const;
bool CompareClassBAdr (const netadr_s &a) const;
bool CompareClassCAdr (const netadr_s &a) const;
netadrtype_t GetType() const;
unsigned short GetPort() const;
// DON'T CALL THIS
const char* ToString( bool onlyBase = false ) const; // returns xxx.xxx.xxx.xxx:ppppp
void ToString( char *pchBuffer, uint32 unBufferSize, bool onlyBase = false ) const; // returns xxx.xxx.xxx.xxx:ppppp
template< size_t maxLenInChars >
void ToString_safe( char (&pDest)[maxLenInChars], bool onlyBase = false ) const
{
ToString( &pDest[0], maxLenInChars, onlyBase );
}
void ToSockadr(struct sockaddr *s) const;
// Returns 0xAABBCCDD for AA.BB.CC.DD on all platforms, which is the same format used by SetIP().
// (So why isn't it just named GetIP()? Because previously there was a fucntion named GetIP(), and
// it did NOT return back what you put into SetIP(). So we nuked that guy.)
unsigned int GetIPHostByteOrder() const;
// Returns a number that depends on the platform. In most cases, this probably should not be used.
unsigned int GetIPNetworkByteOrder() const;
bool IsLocalhost() const; // true, if this is the localhost IP
bool IsLoopback() const; // true if engine loopback buffers are used
bool IsReservedAdr() const; // true, if this is a private LAN IP
bool IsValid() const; // ip & port != 0
bool IsBaseAdrValid() const; // ip != 0
void SetFromSocket( int hSocket );
bool Unserialize( bf_read &readBuf );
bool Serialize( bf_write &writeBuf );
bool operator==(const netadr_s &netadr) const {return ( CompareAdr( netadr ) );}
bool operator!=(const netadr_s &netadr) const {return !( CompareAdr( netadr ) );}
bool operator<(const netadr_s &netadr) const;
public: // members are public to avoid to much changes
netadrtype_t type;
unsigned char ip[4];
unsigned short port;
} netadr_t;
/// Helper class to render a netadr_t. Use this when formatting a net address
/// in a printf. Don't use adr.ToString()!
class CUtlNetAdrRender
{
public:
CUtlNetAdrRender( const netadr_t &obj, bool bBaseOnly = false )
{
obj.ToString( m_rgchString, sizeof(m_rgchString), bBaseOnly );
}
CUtlNetAdrRender( uint32 unIP )
{
netadr_t addr( unIP, 0 );
addr.ToString( m_rgchString, sizeof(m_rgchString), true );
}
CUtlNetAdrRender( uint32 unIP, uint16 unPort )
{
netadr_t addr( unIP, unPort );
addr.ToString( m_rgchString, sizeof(m_rgchString), false );
}
CUtlNetAdrRender( const struct sockaddr &s )
{
netadr_t addr;
if ( addr.SetFromSockadr( &s ) )
addr.ToString( m_rgchString, sizeof(m_rgchString), false );
else
m_rgchString[0] = '\0';
}
const char * String() const
{
return m_rgchString;
}
private:
char m_rgchString[32];
};
#endif // NETADR_H
|