forked from Arduino-CI/arduino_ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPAddress.h
59 lines (49 loc) · 1.49 KB
/
IPAddress.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include <stdint.h>
class IPAddress {
private:
union {
uint8_t bytes[4];
uint32_t dword;
operator uint8_t *() const { return (uint8_t *)bytes; }
} _address;
public:
// Constructors
IPAddress() : IPAddress(0, 0, 0, 0) {}
IPAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) {
_address.bytes[0] = octet1;
_address.bytes[1] = octet2;
_address.bytes[2] = octet3;
_address.bytes[3] = octet4;
}
IPAddress(uint32_t dword) { _address.dword = dword; }
IPAddress(const uint8_t bytes[]) {
_address.bytes[0] = bytes[0];
_address.bytes[1] = bytes[1];
_address.bytes[2] = bytes[2];
_address.bytes[3] = bytes[3];
}
IPAddress(unsigned long dword) { _address.dword = (uint32_t)dword; }
// Accessors
uint32_t asWord() const { return _address.dword; }
uint8_t *raw_address() { return _address.bytes; }
// Comparisons
bool operator==(const IPAddress &rhs) const {
return _address.dword == rhs.asWord();
}
bool operator!=(const IPAddress &rhs) const {
return _address.dword != rhs.asWord();
}
// Indexing
uint8_t operator[](int index) const { return _address.bytes[index]; }
uint8_t &operator[](int index) { return _address.bytes[index]; }
// Conversions
operator uint32_t() const { return _address.dword; };
friend class EthernetClass;
friend class UDP;
friend class Client;
friend class Server;
friend class DhcpClass;
friend class DNSClient;
};
const IPAddress INADDR_NONE(0, 0, 0, 0);