@@ -33,11 +33,11 @@ IPAddress::IPAddress(IPType ip_type)
3333IPAddress::IPAddress (uint8_t first_octet,uint8_t second_octet,uint8_t third_octet,uint8_t fourth_octet)
3434{
3535 _type = IPv4;
36- memset (_address.bytes ,0 ,sizeof (_address.bytes ) - sizeof ( uint32_t ) );
37- _address.bytes [12 ] = first_octet;
38- _address.bytes [13 ] = second_octet;
39- _address.bytes [14 ] = third_octet;
40- _address.bytes [15 ] = fourth_octet;
36+ memset (_address.bytes ,0 ,sizeof (_address.bytes ));
37+ _address.bytes [IPADDRESS_V4_BYTES_INDEX ] = first_octet;
38+ _address.bytes [IPADDRESS_V4_BYTES_INDEX + 1 ] = second_octet;
39+ _address.bytes [IPADDRESS_V4_BYTES_INDEX + 2 ] = third_octet;
40+ _address.bytes [IPADDRESS_V4_BYTES_INDEX + 3 ] = fourth_octet;
4141}
4242
4343IPAddress::IPAddress (uint8_t o1,uint8_t o2,uint8_t o3,uint8_t o4,uint8_t o5,uint8_t o6,uint8_t o7,uint8_t o8,uint8_t o9,uint8_t o10,uint8_t o11,uint8_t o12,uint8_t o13,uint8_t o14,uint8_t o15,uint8_t o16) {
@@ -64,8 +64,8 @@ IPAddress::IPAddress(uint32_t address)
6464{
6565// IPv4 only
6666 _type = IPv4;
67- memset (_address.bytes ,0 ,sizeof (_address.bytes ) - sizeof ( uint32_t ) );
68- _address.dword [3 ] = address;
67+ memset (_address.bytes ,0 ,sizeof (_address.bytes ));
68+ _address.dword [IPADDRESS_V4_DWORD_INDEX ] = address;
6969
7070// NOTE on conversion/comparison and uint32_t:
7171// These conversions are host platform dependent.
@@ -82,8 +82,8 @@ IPAddress::IPAddress(IPType ip_type, const uint8_t *address)
8282{
8383 _type = ip_type;
8484if (ip_type == IPv4) {
85- memset (_address.bytes ,0 ,sizeof (_address.bytes ) - sizeof ( uint32_t ) );
86- memcpy (&_address.bytes [12 ], address,sizeof (uint32_t ));
85+ memset (_address.bytes ,0 ,sizeof (_address.bytes ));
86+ memcpy (&_address.bytes [IPADDRESS_V4_BYTES_INDEX ], address,sizeof (uint32_t ));
8787 }else {
8888memcpy (_address.bytes , address,sizeof (_address.bytes ));
8989 }
@@ -103,6 +103,7 @@ bool IPAddress::fromString4(const char *address)
103103int16_t acc = -1 ;// Accumulator
104104uint8_t dots =0 ;
105105
106+ memset (_address.bytes ,0 ,sizeof (_address.bytes ));
106107while (*address)
107108 {
108109char c = *address++;
@@ -124,7 +125,7 @@ bool IPAddress::fromString4(const char *address)
124125/* No value between dots, e.g. '1..'*/
125126return false ;
126127 }
127- _address.bytes [12 + dots++] = acc;
128+ _address.bytes [IPADDRESS_V4_BYTES_INDEX + dots++] = acc;
128129 acc = -1 ;
129130 }
130131else
@@ -142,8 +143,7 @@ bool IPAddress::fromString4(const char *address)
142143/* No value between dots, e.g. '1..'*/
143144return false ;
144145 }
145- memset (_address.bytes ,0 ,sizeof (_address.bytes ) -sizeof (uint32_t ));
146- _address.bytes [15 ] = acc;
146+ _address.bytes [IPADDRESS_V4_BYTES_INDEX +3 ] = acc;
147147 _type = IPv4;
148148return true ;
149149}
@@ -220,8 +220,8 @@ IPAddress& IPAddress::operator=(const uint8_t *address)
220220{
221221// IPv4 only conversion from byte pointer
222222 _type = IPv4;
223- memset (_address.bytes ,0 ,sizeof (_address.bytes ) - sizeof ( uint32_t ) );
224- memcpy (&_address.bytes [12 ], address,sizeof (uint32_t ));
223+ memset (_address.bytes ,0 ,sizeof (_address.bytes ));
224+ memcpy (&_address.bytes [IPADDRESS_V4_BYTES_INDEX ], address,sizeof (uint32_t ));
225225return *this ;
226226}
227227
@@ -230,10 +230,8 @@ IPAddress& IPAddress::operator=(uint32_t address)
230230// IPv4 conversion
231231// See note on conversion/comparison and uint32_t
232232 _type = IPv4;
233- _address.dword [0 ] =0 ;
234- _address.dword [1 ] =0 ;
235- _address.dword [2 ] =0 ;
236- _address.dword [3 ] = address;
233+ memset (_address.bytes ,0 ,sizeof (_address.bytes ));
234+ _address.dword [IPADDRESS_V4_DWORD_INDEX] = address;
237235return *this ;
238236}
239237
@@ -246,19 +244,19 @@ bool IPAddress::operator==(const uint8_t* addr) const
246244{
247245// IPv4 only comparison to byte pointer
248246// Can't support IPv6 as we know our type, but not the length of the pointer
249- return _type == IPv4 &&memcmp (addr, &_address.bytes [12 ],sizeof (uint32_t )) ==0 ;
247+ return _type == IPv4 &&memcmp (addr, &_address.bytes [IPADDRESS_V4_BYTES_INDEX ],sizeof (uint32_t )) ==0 ;
250248}
251249
252250uint8_t IPAddress::operator [](int index)const {
253251if (_type == IPv4) {
254- return _address.bytes [index +12 ];
252+ return _address.bytes [IPADDRESS_V4_BYTES_INDEX +index ];
255253 }
256254return _address.bytes [index];
257255};
258256
259257uint8_t & IPAddress::operator [](int index) {
260258if (_type == IPv4) {
261- return _address.bytes [index +12 ];
259+ return _address.bytes [IPADDRESS_V4_BYTES_INDEX +index ];
262260 }
263261return _address.bytes [index];
264262};
@@ -321,10 +319,10 @@ size_t IPAddress::printTo(Print& p) const
321319// IPv4
322320for (int i =0 ; i <3 ; i++)
323321 {
324- n += p.print (_address.bytes [12 + i], DEC);
322+ n += p.print (_address.bytes [IPADDRESS_V4_BYTES_INDEX + i], DEC);
325323 n += p.print (' .' );
326324 }
327- n += p.print (_address.bytes [15 ], DEC);
325+ n += p.print (_address.bytes [IPADDRESS_V4_BYTES_INDEX + 3 ], DEC);
328326return n;
329327}
330328