|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /*
- * IPAddress.h - Base class that provides IPAddress
- * Copyright (c) 2011 Adrian McEwen. All right reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- *
- * Modified by Marcelo Aquino <marceloaqno@gmail.org> for MySensors use
- */
-
- #ifndef IPAddress_h
- #define IPAddress_h
-
- #include <stdint.h>
- #include <string>
-
- /**
- * @brief A class to make it easier to handle and pass around IP addresses
- */
- class IPAddress
- {
- private:
- union {
- uint8_t bytes[4]; //!< IPv4 address as an array
- uint32_t dword; //!< IPv4 address in 32 bits format
- } _address;
-
- /**
- * @brief Access the raw byte array containing the address.
- *
- * Because this returns a pointer to the internal structure rather than a copy of the address
- * this function should only be used when you know that the usage of the returned uint8_t* will
- * be transient and not stored.
- *
- * @return pointer to the internal structure.
- */
- uint8_t* raw_address()
- {
- return _address.bytes;
- }
-
- public:
- /**
- * @brief IPAddress constructor.
- */
- IPAddress();
- /**
- * @brief IPAddress constructor.
- *
- * @param first_octet first octet of the IPv4 address.
- * @param second_octet second octet of the IPv4 address.
- * @param third_octet third octet of the IPv4 address.
- * @param fourth_octet fourth octet of the IPv4 address.
- */
- IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
- /**
- * @brief IPAddress constructor.
- *
- * @param address to be set from a 32 bits integer.
- */
- explicit IPAddress(uint32_t address);
- /**
- * @brief IPAddress constructor.
- *
- * @param address to be set from a byte array.
- */
- explicit IPAddress(const uint8_t *address);
- /**
- * @brief Set the IP from a array of characters.
- *
- * @param address to be set.
- */
- bool fromString(const char *address);
- /**
- * @brief Set the IP from a string class type.
- *
- * @param address to be set.
- */
- bool fromString(const std::string &address)
- {
- return fromString(address.c_str());
- }
- /**
- * @brief Overloaded cast operator
- *
- * Allow IPAddress objects to be used where a pointer to a four-byte uint8_t array is expected
- */
- operator uint32_t() const
- {
- return _address.dword;
- }
- /**
- * @brief Overloaded cast operator
- *
- */
- bool operator==(const IPAddress& addr) const
- {
- return _address.dword == addr._address.dword;
- }
- /**
- * @brief Overloaded cast operator
- *
- */
- bool operator==(uint32_t addr) const
- {
- return _address.dword == addr;
- }
- /**
- * @brief Overloaded cast operator
- *
- */
- bool operator==(const uint8_t* addr) const;
- /**
- * @brief Overloaded index operator.
- *
- * Allow getting and setting individual octets of the address.
- *
- */
- uint8_t operator[](int index) const
- {
- return _address.bytes[index];
- }
- /**
- * @brief Overloaded index operator
- *
- */
- uint8_t& operator[](int index)
- {
- return _address.bytes[index];
- }
- /**
- * @brief Overloaded copy operators.
- *
- * Allow initialisation of IPAddress objects from byte array.
- */
- IPAddress& operator=(const uint8_t *address);
- /**
- * @brief Overloaded copy operator.
- *
- * Allow initialisation of IPAddress objects from a 32 bits integer.
- */
- IPAddress& operator=(uint32_t address);
- /**
- * @brief Convert the IP address to a string.
- *
- * @return A stringified IP address
- */
- std::string toString();
-
- friend class Client;
- };
-
- #endif
|