|
1 | 1 | /* |
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. |
3 | 4 |
|
4 | 5 | This library is free software; you can redistribute it and/or |
5 | 6 | modify it under the terms of the GNU Lesser General Public |
|
8 | 9 |
|
9 | 10 | This library is distributed in the hope that it will be useful, |
10 | 11 | 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. |
13 | 14 |
|
14 | 15 | You should have received a copy of the GNU Lesser General Public |
15 | 16 | License along with this library; if not, write to the Free Software |
|
21 | 22 |
|
22 | 23 | IPAddress::IPAddress() |
23 | 24 | { |
24 | | - memset(_address, 0, sizeof(_address)); |
| 25 | + _address.dword = 0; |
25 | 26 | } |
26 | 27 |
|
27 | 28 | IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) |
28 | 29 | { |
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; |
33 | 34 | } |
34 | 35 |
|
35 | 36 | IPAddress::IPAddress(uint32_t address) |
36 | 37 | { |
37 | | - memcpy(_address, &address, sizeof(_address)); |
| 38 | + _address.dword = address; |
38 | 39 | } |
39 | 40 |
|
40 | 41 | IPAddress::IPAddress(const uint8_t *address) |
41 | 42 | { |
42 | | - memcpy(_address, address, sizeof(_address)); |
| 43 | + memcpy(_address.bytes, address, sizeof(_address.bytes)); |
43 | 44 | } |
44 | 45 |
|
45 | 46 | IPAddress& IPAddress::operator=(const uint8_t *address) |
46 | 47 | { |
47 | | - memcpy(_address, address, sizeof(_address)); |
| 48 | + memcpy(_address.bytes, address, sizeof(_address.bytes)); |
48 | 49 | return *this; |
49 | 50 | } |
50 | 51 |
|
51 | 52 | IPAddress& IPAddress::operator=(uint32_t address) |
52 | 53 | { |
53 | | - memcpy(_address, (const uint8_t *)&address, sizeof(_address)); |
| 54 | + _address.dword = address; |
54 | 55 | return *this; |
55 | 56 | } |
56 | 57 |
|
57 | 58 | bool IPAddress::operator==(const uint8_t* addr) const |
58 | 59 | { |
59 | | - return memcmp(addr, _address, sizeof(_address)) == 0; |
| 60 | + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; |
60 | 61 | } |
61 | 62 |
|
62 | 63 | size_t IPAddress::printTo(Print& p) const |
63 | 64 | { |
64 | 65 | size_t n = 0; |
65 | 66 | for (int i =0; i < 3; i++) |
66 | 67 | { |
67 | | - n += p.print(_address[i], DEC); |
| 68 | + n += p.print(_address.bytes[i], DEC); |
68 | 69 | n += p.print('.'); |
69 | 70 | } |
70 | | - n += p.print(_address[3], DEC); |
| 71 | + n += p.print(_address.bytes[3], DEC); |
71 | 72 | return n; |
72 | 73 | } |
73 | 74 |
|
0 commit comments