Network.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2014-12
  4. //
  5. #ifndef __NETWORK_H
  6. #define __NETWORK_H
  7. #include "IPAddress.h"
  8. #if defined(LINUX)
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <arpa/inet.h>
  12. #endif
  13. namespace tzc {
  14. struct Route {
  15. Route():
  16. ages(0), flags(0)
  17. {
  18. // Nothing
  19. }
  20. Route & operator=(const Route & right);
  21. IPAddress destination;
  22. IPAddress netmask;
  23. IPAddress gateway; // Next HOP
  24. IPAddress NICAddr;
  25. TZ_ULONG ages; // in seconds
  26. TZ_ULONG flags; // U,G,H,D...
  27. };
  28. //
  29. // inlines
  30. //
  31. inline Route & Route::operator=(const Route & right)
  32. {
  33. this->destination = right.destination;
  34. this->netmask = right.netmask;
  35. this->gateway = right.gateway;
  36. this->NICAddr = right.NICAddr;
  37. this->ages = right.ages;
  38. this->flags = right.flags;
  39. return *this;
  40. }
  41. inline bool operator==(const Route & route1, const Route & route2)
  42. {
  43. return (route1.destination == route2.destination);
  44. }
  45. inline bool operator!=(const Route & route1, const Route & route2)
  46. {
  47. return !(route1 == route2);
  48. }
  49. struct NICInfo {
  50. NICInfo(): iNICIndex(0),
  51. eNICType(NIC_TYPE_UNKNOW)
  52. {
  53. // Nothing
  54. }
  55. enum __NIC_TYPE {
  56. NIC_TYPE_LO,
  57. NIC_TYPE_VIR,
  58. NIC_TYPE_ETH,
  59. NIC_TYPE_UNKNOW
  60. };
  61. NICInfo & operator=(const NICInfo & right);
  62. IPAddress broadcast;
  63. IPAddress ipaddr;
  64. IPAddress netmask;
  65. TZ_ULONG iNICIndex;
  66. TZ_ULONG eNICType;
  67. std::string sNICName;
  68. std::string sPhyAddr;
  69. };
  70. //
  71. // inlines
  72. //
  73. inline NICInfo & NICInfo::operator=(const NICInfo & right)
  74. {
  75. this->broadcast = right.broadcast;
  76. this->ipaddr = right.ipaddr;
  77. this->netmask = right.netmask;
  78. this->iNICIndex = right.iNICIndex;
  79. this->eNICType = right.eNICType;
  80. this->sNICName = right.sNICName;
  81. this->sPhyAddr = right.sPhyAddr;
  82. return *this;
  83. }
  84. inline bool operator==(const NICInfo & nic1, const NICInfo & nic2)
  85. {
  86. return (nic1.ipaddr == nic2.ipaddr);
  87. }
  88. inline bool operator!=(const NICInfo & nic1, const NICInfo & nic2)
  89. {
  90. return !(nic1 == nic2);
  91. }
  92. class DECLDLL RouteTable {
  93. public:
  94. RouteTable();
  95. ~RouteTable();
  96. void AddRoute(const Route & route);
  97. void DelRoute(const Route & route);
  98. size_t GetTableSize() const;
  99. Route & operator[] (const size_t index);
  100. const Route & operator[] (const size_t index) const;
  101. private:
  102. std::vector<Route> m_routeTable;
  103. };
  104. class DECLDLL NICsTable {
  105. public:
  106. NICsTable();
  107. ~NICsTable();
  108. void AddNICInfo(const NICInfo & nic);
  109. void DelNICInfo(const NICInfo & nic);
  110. size_t GetTableSize() const;
  111. NICInfo & operator[] (const size_t index);
  112. const NICInfo & operator[] (const size_t index) const;
  113. private:
  114. std::vector<NICInfo> m_NICInfoTable;
  115. };
  116. } // namespace tzc
  117. #endif /* ----- #ifndef __NETWORK_H ----- */