1 /*
2   IPAddress.cpp - 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 #include <Arduino.h>
21 #include <IPAddress.h>
22 
IPAddress()23 IPAddress::IPAddress()
24 {
25     _address.dword = 0;
26 }
27 
IPAddress(uint8_t first_octet,uint8_t second_octet,uint8_t third_octet,uint8_t fourth_octet)28 IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
29 {
30     _address.bytes[0] = first_octet;
31     _address.bytes[1] = second_octet;
32     _address.bytes[2] = third_octet;
33     _address.bytes[3] = fourth_octet;
34 }
35 
IPAddress(uint32_t address)36 IPAddress::IPAddress(uint32_t address)
37 {
38     _address.dword = address;
39 }
40 
IPAddress(const uint8_t * address)41 IPAddress::IPAddress(const uint8_t *address)
42 {
43     memcpy(_address.bytes, address, sizeof(_address.bytes));
44 }
45 
fromString(const char * address)46 bool IPAddress::fromString(const char *address)
47 {
48     uint16_t acc = 0; // Accumulator
49     uint8_t dots = 0;
50 
51     while (*address)
52     {
53         char c = *address++;
54         if (c >= '0' && c <= '9')
55         {
56             acc = acc * 10 + (c - '0');
57             if (acc > 255) {
58                 // Value out of [0..255] range
59                 return false;
60             }
61         }
62         else if (c == '.')
63         {
64             if (dots == 3) {
65                 // Too much dots (there must be 3 dots)
66                 return false;
67             }
68             _address.bytes[dots++] = acc;
69             acc = 0;
70         }
71         else
72         {
73             // Invalid char
74             return false;
75         }
76     }
77 
78     if (dots != 3) {
79         // Too few dots (there must be 3 dots)
80         return false;
81     }
82     _address.bytes[3] = acc;
83     return true;
84 }
85 
operator =(const uint8_t * address)86 IPAddress& IPAddress::operator=(const uint8_t *address)
87 {
88     memcpy(_address.bytes, address, sizeof(_address.bytes));
89     return *this;
90 }
91 
operator =(uint32_t address)92 IPAddress& IPAddress::operator=(uint32_t address)
93 {
94     _address.dword = address;
95     return *this;
96 }
97 
operator ==(const uint8_t * addr) const98 bool IPAddress::operator==(const uint8_t* addr) const
99 {
100     return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
101 }
102 
printTo(Print & p) const103 size_t IPAddress::printTo(Print& p) const
104 {
105     size_t n = 0;
106     for (int i =0; i < 3; i++)
107     {
108         n += p.print(_address.bytes[i], DEC);
109         n += p.print('.');
110     }
111     n += p.print(_address.bytes[3], DEC);
112     return n;
113 }
114 
115