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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. *
  3. * MIT License:
  4. * Copyright (c) 2011 Adrian McEwen
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. *
  23. * adrianm@mcqn.com 1/1/2011
  24. */
  25. #ifndef IPAddress_h
  26. #define IPAddress_h
  27. #include <Printable.h>
  28. // A class to make it easier to handle and pass around IP addresses
  29. class IPAddress : public Printable {
  30. private:
  31. union {
  32. uint8_t bytes[4]; // IPv4 address
  33. uint32_t dword;
  34. } _address;
  35. // Access the raw byte array containing the address. Because this returns a pointer
  36. // to the internal structure rather than a copy of the address this function should only
  37. // be used when you know that the usage of the returned uint8_t* will be transient and not
  38. // stored.
  39. uint8_t * raw_address() { return _address.bytes; };
  40. public:
  41. // Constructors
  42. IPAddress() {
  43. _address.dword = 0;
  44. }
  45. IPAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) {
  46. _address.bytes[0] = b1;
  47. _address.bytes[1] = b2;
  48. _address.bytes[2] = b3;
  49. _address.bytes[3] = b4;
  50. }
  51. IPAddress(uint32_t address) {
  52. _address.dword = address;
  53. }
  54. IPAddress(const uint8_t *address) {
  55. // TODO: use unaligned read on Cortex-M4
  56. _address.bytes[0] = *address++;
  57. _address.bytes[1] = *address++;
  58. _address.bytes[2] = *address++;
  59. _address.bytes[3] = *address++;
  60. }
  61. // Overloaded cast operator to allow IPAddress objects to be used where a pointer
  62. // to a four-byte uint8_t array is expected
  63. operator uint32_t () {
  64. return _address.dword;
  65. }
  66. bool operator==(const IPAddress& addr) {
  67. return _address.dword == addr._address.dword;
  68. }
  69. bool operator==(const uint8_t* addr) {
  70. // TODO: use unaligned read on Cortex-M4
  71. return (_address.bytes[0] == addr[0]
  72. && _address.bytes[1] == addr[1]
  73. && _address.bytes[2] == addr[2]
  74. && _address.bytes[3] == addr[3]);
  75. }
  76. // Overloaded index operator to allow getting and setting individual octets of the address
  77. uint8_t operator[](int index) const {
  78. return _address.bytes[index];
  79. };
  80. uint8_t& operator[](int index) {
  81. return _address.bytes[index];
  82. };
  83. // Overloaded copy operators to allow initialisation of IPAddress objects from other types
  84. IPAddress& operator=(const uint8_t *address) {
  85. // TODO: use unaligned read on Cortex-M4
  86. _address.bytes[0] = *address++;
  87. _address.bytes[1] = *address++;
  88. _address.bytes[2] = *address++;
  89. _address.bytes[3] = *address++;
  90. return *this;
  91. }
  92. IPAddress& operator=(uint32_t address) {
  93. _address.dword = address;
  94. return *this;
  95. }
  96. virtual size_t printTo(Print& p) const;
  97. friend class EthernetClass;
  98. friend class UDP;
  99. friend class Client;
  100. friend class Server;
  101. friend class DhcpClass;
  102. friend class DNSClient;
  103. };
  104. const IPAddress INADDR_NONE((uint32_t)0);
  105. #endif