Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

IPAddress.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * IPAddress.h - Base class that provides IPAddress
  3. * Copyright (c) 2011 Adrian McEwen. All right reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. *
  20. * Modified by Marcelo Aquino <marceloaqno@gmail.org> for MySensors use
  21. */
  22. #ifndef IPAddress_h
  23. #define IPAddress_h
  24. #include <stdint.h>
  25. #include <string>
  26. /**
  27. * @brief A class to make it easier to handle and pass around IP addresses
  28. */
  29. class IPAddress
  30. {
  31. private:
  32. union {
  33. uint8_t bytes[4]; //!< IPv4 address as an array
  34. uint32_t dword; //!< IPv4 address in 32 bits format
  35. } _address;
  36. /**
  37. * @brief Access the raw byte array containing the address.
  38. *
  39. * Because this returns a pointer to the internal structure rather than a copy of the address
  40. * this function should only be used when you know that the usage of the returned uint8_t* will
  41. * be transient and not stored.
  42. *
  43. * @return pointer to the internal structure.
  44. */
  45. uint8_t* raw_address()
  46. {
  47. return _address.bytes;
  48. }
  49. public:
  50. /**
  51. * @brief IPAddress constructor.
  52. */
  53. IPAddress();
  54. /**
  55. * @brief IPAddress constructor.
  56. *
  57. * @param first_octet first octet of the IPv4 address.
  58. * @param second_octet second octet of the IPv4 address.
  59. * @param third_octet third octet of the IPv4 address.
  60. * @param fourth_octet fourth octet of the IPv4 address.
  61. */
  62. IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
  63. /**
  64. * @brief IPAddress constructor.
  65. *
  66. * @param address to be set from a 32 bits integer.
  67. */
  68. explicit IPAddress(uint32_t address);
  69. /**
  70. * @brief IPAddress constructor.
  71. *
  72. * @param address to be set from a byte array.
  73. */
  74. explicit IPAddress(const uint8_t *address);
  75. /**
  76. * @brief Set the IP from a array of characters.
  77. *
  78. * @param address to be set.
  79. */
  80. bool fromString(const char *address);
  81. /**
  82. * @brief Set the IP from a string class type.
  83. *
  84. * @param address to be set.
  85. */
  86. bool fromString(const std::string &address)
  87. {
  88. return fromString(address.c_str());
  89. }
  90. /**
  91. * @brief Overloaded cast operator
  92. *
  93. * Allow IPAddress objects to be used where a pointer to a four-byte uint8_t array is expected
  94. */
  95. operator uint32_t() const
  96. {
  97. return _address.dword;
  98. }
  99. /**
  100. * @brief Overloaded cast operator
  101. *
  102. */
  103. bool operator==(const IPAddress& addr) const
  104. {
  105. return _address.dword == addr._address.dword;
  106. }
  107. /**
  108. * @brief Overloaded cast operator
  109. *
  110. */
  111. bool operator==(uint32_t addr) const
  112. {
  113. return _address.dword == addr;
  114. }
  115. /**
  116. * @brief Overloaded cast operator
  117. *
  118. */
  119. bool operator==(const uint8_t* addr) const;
  120. /**
  121. * @brief Overloaded index operator.
  122. *
  123. * Allow getting and setting individual octets of the address.
  124. *
  125. */
  126. uint8_t operator[](int index) const
  127. {
  128. return _address.bytes[index];
  129. }
  130. /**
  131. * @brief Overloaded index operator
  132. *
  133. */
  134. uint8_t& operator[](int index)
  135. {
  136. return _address.bytes[index];
  137. }
  138. /**
  139. * @brief Overloaded copy operators.
  140. *
  141. * Allow initialisation of IPAddress objects from byte array.
  142. */
  143. IPAddress& operator=(const uint8_t *address);
  144. /**
  145. * @brief Overloaded copy operator.
  146. *
  147. * Allow initialisation of IPAddress objects from a 32 bits integer.
  148. */
  149. IPAddress& operator=(uint32_t address);
  150. /**
  151. * @brief Convert the IP address to a string.
  152. *
  153. * @return A stringified IP address
  154. */
  155. std::string toString();
  156. friend class Client;
  157. };
  158. #endif