Team Fortress 2 Source Code as on 22/4/2020
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.

73 lines
2.3 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. typedef enum
  17. {
  18. NA_NULL = 0,
  19. NA_LOOPBACK,
  20. NA_BROADCAST,
  21. NA_IP,
  22. } netadrtype_t;
  23. struct netadr_t
  24. {
  25. public:
  26. netadr_t() { SetIP( 0 ); SetPort( 0 ); SetType( NA_IP ); }
  27. netadr_t( uint unIP, uint16 usPort ) { SetIP( unIP ); SetPort( usPort ); SetType( NA_IP ); }
  28. netadr_t( const char *pch ) { SetFromString( pch ); }
  29. void Clear(); // invalids Address
  30. void SetType( netadrtype_t type );
  31. void SetPort( unsigned short port );
  32. bool SetFromSockadr(const struct sockaddr *s);
  33. void SetIP(uint8 b1, uint8 b2, uint8 b3, uint8 b4);
  34. void SetIP(uint unIP); // Sets IP. unIP is in host order (little-endian)
  35. void SetIPAndPort( uint unIP, unsigned short usPort ) { SetIP( unIP ); SetPort( usPort ); }
  36. void SetFromString(const char *pch, bool bUseDNS = false ); // if bUseDNS is true then do a DNS lookup if needed
  37. bool CompareAdr (const netadr_t &a, bool onlyBase = false) const;
  38. bool CompareClassBAdr (const netadr_t &a) const;
  39. bool CompareClassCAdr (const netadr_t &a) const;
  40. netadrtype_t GetType() const;
  41. unsigned short GetPort() const;
  42. const char* ToString( bool onlyBase = false ) const; // returns xxx.xxx.xxx.xxx:ppppp
  43. void ToSockadr(struct sockaddr *s) const;
  44. unsigned int GetIP() const;
  45. bool IsLocalhost() const; // true, if this is the localhost IP
  46. bool IsLoopback() const; // true if engine loopback buffers are used
  47. bool IsReservedAdr() const; // true, if this is a private LAN IP
  48. bool IsValid() const; // ip & port != 0
  49. bool IsBaseAdrValid() const; // ip != 0
  50. void SetFromSocket( int hSocket );
  51. // These function names are decorated because the Xbox360 defines macros for ntohl and htonl
  52. unsigned long addr_ntohl() const;
  53. unsigned long addr_htonl() const;
  54. bool operator==(const netadr_t &netadr) const {return ( CompareAdr( netadr ) );}
  55. bool operator<(const netadr_t &netadr) const;
  56. public: // members are public to avoid to much changes
  57. netadrtype_t type;
  58. unsigned char ip[4];
  59. unsigned short port;
  60. };
  61. #endif // NETADR_H