IPAddress.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. IPAddress.h - Base class that provides IPAddress
  3. Copyright (c) 2011 Adrian McEwen. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #ifndef IPAddress_h
  17. #define IPAddress_h
  18. #include <stdint.h>
  19. #include <Printable.h>
  20. // A class to make it easier to handle and pass around IP addresses
  21. class IPAddress : public Printable {
  22. private:
  23. union {
  24. uint8_t bytes[4]; // IPv4 address
  25. uint32_t dword;
  26. } _address;
  27. // Access the raw byte array containing the address. Because this returns a pointer
  28. // to the internal structure rather than a copy of the address this function should only
  29. // be used when you know that the usage of the returned uint8_t* will be transient and not
  30. // stored.
  31. uint8_t* raw_address() { return _address.bytes; };
  32. public:
  33. // Constructors
  34. IPAddress();
  35. IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
  36. IPAddress(uint32_t address);
  37. IPAddress(const uint8_t *address);
  38. // Overloaded cast operator to allow IPAddress objects to be used where a pointer
  39. // to a four-byte uint8_t array is expected
  40. operator uint32_t() const { return _address.dword; };
  41. bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
  42. bool operator==(const uint8_t* addr) const;
  43. // Overloaded index operator to allow getting and setting individual octets of the address
  44. uint8_t operator[](int index) const { return _address.bytes[index]; };
  45. uint8_t& operator[](int index) { return _address.bytes[index]; };
  46. // Overloaded copy operators to allow initialisation of IPAddress objects from other types
  47. IPAddress& operator=(const uint8_t *address);
  48. IPAddress& operator=(uint32_t address);
  49. virtual size_t printTo(Print& p) const;
  50. friend class EthernetClass;
  51. friend class UDP;
  52. friend class Client;
  53. friend class Server;
  54. friend class DhcpClass;
  55. friend class DNSClient;
  56. };
  57. const IPAddress INADDR_NONE(0,0,0,0);
  58. #endif