PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/include/net/ipv6.h

https://github.com/dreamsxin/REACT-CPP
C Header | 234 lines | 85 code | 30 blank | 119 comment | 10 complexity | 9adeb4dc4d1065a31e670c1526d00992 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Ipv6.h
  3. *
  4. * Class representing an IPv6 address
  5. *
  6. * @copyright 2014 Copernica BV
  7. */
  8. /**
  9. * Set up namespace
  10. */
  11. namespace React { namespace Net {
  12. /**
  13. * IP address class
  14. */
  15. class Ipv6
  16. {
  17. private:
  18. /**
  19. * The address
  20. * @var struct in6_addr
  21. */
  22. struct in6_addr _addr;
  23. public:
  24. /**
  25. * Constructor to create an empty invalid address
  26. */
  27. Ipv6()
  28. {
  29. // set to zero's
  30. memset(&_addr, 0, sizeof(struct in6_addr));
  31. }
  32. /**
  33. * Copy constructor
  34. * @param ip Address to copy
  35. */
  36. Ipv6(const Ipv6 &ip)
  37. {
  38. // copy address
  39. memcpy(&_addr, &ip._addr, sizeof(struct in6_addr));
  40. }
  41. /**
  42. * Move operator
  43. * @param ip Address to copy
  44. */
  45. Ipv6(Ipv6 &&ip)
  46. {
  47. // copy address
  48. memcpy(&_addr, &ip._addr, sizeof(struct in6_addr));
  49. }
  50. /**
  51. * Construct from a string representation
  52. * @param ip String representation
  53. */
  54. Ipv6(const char *ip)
  55. {
  56. // try to parse
  57. if (inet_pton(AF_INET6, ip, &_addr) == 1) return;
  58. // failure, set to zero
  59. memset(&_addr, 0, sizeof(struct in6_addr));
  60. }
  61. /**
  62. * Construct from a string representation
  63. * @param ip String representation
  64. */
  65. Ipv6(const std::string &ip)
  66. {
  67. // try to parse
  68. if (inet_pton(AF_INET6, ip.c_str(), &_addr) == 1) return;
  69. // failure, set to zero
  70. memset(&_addr, 0, sizeof(struct in6_addr));
  71. }
  72. /**
  73. * Construct from a struct in6_addr object
  74. * @param ip in6_addr storage
  75. */
  76. Ipv6(const struct in6_addr ip)
  77. {
  78. // copy address
  79. memcpy(&_addr, &ip, sizeof(struct in6_addr));
  80. }
  81. /**
  82. * Construct from a pointer to a struct in6_addr object
  83. * @param ip in6_addr storage
  84. */
  85. Ipv6(const struct in6_addr *ip)
  86. {
  87. // copy address
  88. memcpy(&_addr, ip, sizeof(struct in6_addr));
  89. }
  90. /**
  91. * Constructor based on ares ipv6 structure
  92. * @param ip ares_in6_addr storage
  93. */
  94. Ipv6(const struct ares_in6_addr ip)
  95. {
  96. // copy address just as if it was an in6_addr
  97. memcpy(&_addr, &ip, sizeof(struct in6_addr));
  98. }
  99. /**
  100. * Constructor based on ares ipv6 structure
  101. * @param ip ares_in6_addr storage
  102. */
  103. Ipv6(const struct ares_in6_addr *ip)
  104. {
  105. // copy address just as if it was an in6_addr
  106. memcpy(&_addr, ip, sizeof(struct in6_addr));
  107. }
  108. /**
  109. * Destructor
  110. */
  111. virtual ~Ipv6() {}
  112. /**
  113. * Pointer to the internal address
  114. * @return struct in6_addr*
  115. */
  116. const struct in6_addr *internal() const
  117. {
  118. return &_addr;
  119. }
  120. /**
  121. * Is this address valid?
  122. * @return bool
  123. */
  124. bool valid() const
  125. {
  126. // construct invalid address
  127. Ipv6 invalid;
  128. // compare if equal to invalid
  129. return memcmp(&_addr, &invalid._addr, sizeof(struct in6_addr)) != 0;
  130. }
  131. /**
  132. * Assign a different IP address to this object
  133. * @param address The other address
  134. * @return Ipv6
  135. */
  136. Ipv6 &operator=(const Ipv6 &address)
  137. {
  138. // skip identicals
  139. if (&address == this) return *this;
  140. // copy address
  141. memcpy(&_addr, &address._addr, sizeof(struct in6_addr));
  142. // done
  143. return *this;
  144. }
  145. /**
  146. * Compare two IP addresses
  147. * @param address The address to compare
  148. * @return bool
  149. */
  150. bool operator==(const Ipv6 &address) const
  151. {
  152. // compare memory
  153. return memcmp(&_addr, &address._addr, sizeof(struct in6_addr)) == 0;
  154. }
  155. /**
  156. * Compare two IP addresses
  157. * @param address The address to compare
  158. * @return bool
  159. */
  160. bool operator!=(const Ipv6 &address) const
  161. {
  162. // compare memory
  163. return memcmp(&_addr, &address._addr, sizeof(struct in6_addr)) != 0;
  164. }
  165. /**
  166. * Compare two IP addresses
  167. * @param address The address to compare
  168. * @return bool
  169. */
  170. bool operator<(const Ipv6 &address) const
  171. {
  172. // compare addresses
  173. return memcmp(&_addr, &address._addr, sizeof(struct in6_addr)) < 0;
  174. }
  175. /**
  176. * Compare two IP addresses
  177. * @param address The address to compare
  178. * @return bool
  179. */
  180. bool operator>(const Ipv6 &address) const
  181. {
  182. // compare addresses
  183. return memcmp(&_addr, &address._addr, sizeof(struct in6_addr)) > 0;
  184. }
  185. /**
  186. * String representation of the address
  187. * @return string
  188. */
  189. const std::string toString() const
  190. {
  191. // not valid?
  192. if (!valid()) return std::string("::");
  193. // construct a buffer
  194. char buffer[INET6_ADDRSTRLEN];
  195. // convert
  196. inet_ntop(AF_INET6, &_addr, buffer, INET6_ADDRSTRLEN);
  197. // done
  198. return std::string(buffer);
  199. }
  200. };
  201. /**
  202. * End of namespace
  203. */
  204. }}