Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.9 KiB

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