Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fe80caa

Browse files
committed
Define constants for the IPv4 index
1 parent ac541e8 commit fe80caa

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

‎api/IPAddress.cpp‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ IPAddress::IPAddress(IPType ip_type)
3333
IPAddress::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

4343
IPAddress::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;
8484
if (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 {
8888
memcpy(_address.bytes, address, sizeof(_address.bytes));
8989
}
@@ -103,6 +103,7 @@ bool IPAddress::fromString4(const char *address)
103103
int16_t acc = -1; // Accumulator
104104
uint8_t dots = 0;
105105

106+
memset(_address.bytes, 0, sizeof(_address.bytes));
106107
while (*address)
107108
{
108109
char c = *address++;
@@ -124,7 +125,7 @@ bool IPAddress::fromString4(const char *address)
124125
/* No value between dots, e.g. '1..' */
125126
return false;
126127
}
127-
_address.bytes[12 + dots++] = acc;
128+
_address.bytes[IPADDRESS_V4_BYTES_INDEX + dots++] = acc;
128129
acc = -1;
129130
}
130131
else
@@ -142,8 +143,7 @@ bool IPAddress::fromString4(const char *address)
142143
/* No value between dots, e.g. '1..' */
143144
return 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;
148148
return 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));
225225
return *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;
237235
return *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

252250
uint8_t IPAddress::operator[](int index) const {
253251
if (_type == IPv4) {
254-
return _address.bytes[index + 12];
252+
return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index];
255253
}
256254
return _address.bytes[index];
257255
};
258256

259257
uint8_t& IPAddress::operator[](int index) {
260258
if (_type == IPv4) {
261-
return _address.bytes[index + 12];
259+
return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index];
262260
}
263261
return _address.bytes[index];
264262
};
@@ -321,10 +319,10 @@ size_t IPAddress::printTo(Print& p) const
321319
// IPv4
322320
for (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);
328326
return n;
329327
}
330328

‎api/IPAddress.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "Printable.h"
2424
#include "String.h"
2525

26+
#define IPADDRESS_V4_BYTES_INDEX 12
27+
#define IPADDRESS_V4_DWORD_INDEX 3
28+
2629
// forward declarations of global name space friend classes
2730
class EthernetClass;
2831
class DhcpClass;
@@ -49,7 +52,7 @@ class IPAddress : public Printable {
4952
// to the internal structure rather than a copy of the address this function should only
5053
// be used when you know that the usage of the returned uint8_t* will be transient and not
5154
// stored.
52-
uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[12] : _address.bytes; }
55+
uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes; }
5356

5457
public:
5558
// Constructors
@@ -70,7 +73,7 @@ class IPAddress : public Printable {
7073

7174
// Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected
7275
// NOTE: IPv4 only; see implementation note
73-
operator uint32_t() const { return _type == IPv4 ? _address.dword[3] : 0; };
76+
operator uint32_t() const { return _type == IPv4 ? _address.dword[IPADDRESS_V4_DWORD_INDEX] : 0; };
7477

7578
bool operator==(const IPAddress& addr) const;
7679
bool operator!=(const IPAddress& addr) const { return !(*this == addr); };

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /