2 IPAddress.cpp - Base class that provides IPAddress
3 Copyright (c) 2011 Adrian McEwen. All right reserved.
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.
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.
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
21 #include <IPAddress.h>
23 IPAddress::IPAddress()
28 IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
30 _address.bytes[0] = first_octet;
31 _address.bytes[1] = second_octet;
32 _address.bytes[2] = third_octet;
33 _address.bytes[3] = fourth_octet;
36 IPAddress::IPAddress(uint32_t address)
38 _address.dword = address;
41 IPAddress::IPAddress(const uint8_t *address)
43 memcpy(_address.bytes, address, sizeof(_address.bytes));
46 bool IPAddress::fromString(const char *address)
48 uint16_t acc = 0; // Accumulator
54 if (c >= '0' && c <= '9')
56 acc = acc * 10 + (c - '0');
58 // Value out of [0..255] range
65 // Too much dots (there must be 3 dots)
68 _address.bytes[dots++] = acc;
79 // Too few dots (there must be 3 dots)
82 _address.bytes[3] = acc;
86 IPAddress& IPAddress::operator=(const uint8_t *address)
88 memcpy(_address.bytes, address, sizeof(_address.bytes));
92 IPAddress& IPAddress::operator=(uint32_t address)
94 _address.dword = address;
98 bool IPAddress::operator==(const uint8_t* addr) const
100 return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
103 size_t IPAddress::printTo(Print& p) const
106 for (int i =0; i < 3; i++)
108 n += p.print(_address.bytes[i], DEC);
111 n += p.print(_address.bytes[3], DEC);