Skip to content

Commit 7b80688

Browse files
committed
Updated IPAddress class to the latest version
1 parent 5c6890c commit 7b80688

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

cores/arduino/IPAddress.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
Copyright (c) 2014 Arduino. All right reserved.
2+
IPAddress.cpp - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
34
45
This library is free software; you can redistribute it and/or
56
modify it under the terms of the GNU Lesser General Public
@@ -8,8 +9,8 @@
89
910
This library is distributed in the hope that it will be useful,
1011
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12-
See the GNU Lesser General Public License for more details.
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
1314
1415
You should have received a copy of the GNU Lesser General Public
1516
License along with this library; if not, write to the Free Software
@@ -21,53 +22,53 @@
2122

2223
IPAddress::IPAddress()
2324
{
24-
memset(_address, 0, sizeof(_address));
25+
_address.dword = 0;
2526
}
2627

2728
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
2829
{
29-
_address[0] = first_octet;
30-
_address[1] = second_octet;
31-
_address[2] = third_octet;
32-
_address[3] = 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;
3334
}
3435

3536
IPAddress::IPAddress(uint32_t address)
3637
{
37-
memcpy(_address, &address, sizeof(_address));
38+
_address.dword = address;
3839
}
3940

4041
IPAddress::IPAddress(const uint8_t *address)
4142
{
42-
memcpy(_address, address, sizeof(_address));
43+
memcpy(_address.bytes, address, sizeof(_address.bytes));
4344
}
4445

4546
IPAddress& IPAddress::operator=(const uint8_t *address)
4647
{
47-
memcpy(_address, address, sizeof(_address));
48+
memcpy(_address.bytes, address, sizeof(_address.bytes));
4849
return *this;
4950
}
5051

5152
IPAddress& IPAddress::operator=(uint32_t address)
5253
{
53-
memcpy(_address, (const uint8_t *)&address, sizeof(_address));
54+
_address.dword = address;
5455
return *this;
5556
}
5657

5758
bool IPAddress::operator==(const uint8_t* addr) const
5859
{
59-
return memcmp(addr, _address, sizeof(_address)) == 0;
60+
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
6061
}
6162

6263
size_t IPAddress::printTo(Print& p) const
6364
{
6465
size_t n = 0;
6566
for (int i =0; i < 3; i++)
6667
{
67-
n += p.print(_address[i], DEC);
68+
n += p.print(_address.bytes[i], DEC);
6869
n += p.print('.');
6970
}
70-
n += p.print(_address[3], DEC);
71+
n += p.print(_address.bytes[3], DEC);
7172
return n;
7273
}
7374

cores/arduino/IPAddress.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
Copyright (c) 2014 Arduino. All right reserved.
2+
IPAddress.h - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
34
45
This library is free software; you can redistribute it and/or
56
modify it under the terms of the GNU Lesser General Public
@@ -8,8 +9,8 @@
89
910
This library is distributed in the hope that it will be useful,
1011
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12-
See the GNU Lesser General Public License for more details.
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
1314
1415
You should have received a copy of the GNU Lesser General Public
1516
License along with this library; if not, write to the Free Software
@@ -19,18 +20,23 @@
1920
#ifndef IPAddress_h
2021
#define IPAddress_h
2122

23+
#include <stdint.h>
2224
#include <Printable.h>
2325

2426
// A class to make it easier to handle and pass around IP addresses
2527

2628
class IPAddress : public Printable {
2729
private:
28-
uint8_t _address[4]; // IPv4 address
30+
union {
31+
uint8_t bytes[4]; // IPv4 address
32+
uint32_t dword;
33+
} _address;
34+
2935
// Access the raw byte array containing the address. Because this returns a pointer
3036
// to the internal structure rather than a copy of the address this function should only
3137
// be used when you know that the usage of the returned uint8_t* will be transient and not
3238
// stored.
33-
uint8_t* raw_address() { return _address; };
39+
uint8_t* raw_address() { return _address.bytes; };
3440

3541
public:
3642
// Constructors
@@ -41,13 +47,13 @@ class IPAddress : public Printable {
4147

4248
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
4349
// to a four-byte uint8_t array is expected
44-
operator uint32_t() const { return *((uint32_t*)_address+0); };
45-
bool operator==(const IPAddress& addr) const { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
50+
operator uint32_t() const { return _address.dword; };
51+
bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
4652
bool operator==(const uint8_t* addr) const;
4753

4854
// Overloaded index operator to allow getting and setting individual octets of the address
49-
uint8_t operator[](int index) const { return _address[index]; };
50-
uint8_t& operator[](int index) { return _address[index]; };
55+
uint8_t operator[](int index) const { return _address.bytes[index]; };
56+
uint8_t& operator[](int index) { return _address.bytes[index]; };
5157

5258
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
5359
IPAddress& operator=(const uint8_t *address);

0 commit comments

Comments
 (0)