-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPAddress.cpp
118 lines (100 loc) · 3.19 KB
/
IPAddress.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @file IPAddress.cpp
* @author Phil Hilger ([email protected])
* @brief
* @version 0.1
* @date 2023-03-02
*
* CAN-talk. A library for microcontrollers that allows decent comms
* over a CAN bus.
*
* Copyright (C) 2023, PeerGum
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https: //www.gnu.org/licenses/>.
*
*/
#include "IPAddress.h"
#include <cstring>
#include "esp_log.h"
const char* TAG = "IPAddress";
IPAddress ipNull;
IPAddress::IPAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
ipAddress.u_addr.ip4.addr = ESP_IP4TOADDR(a, b, c, d);
ipAddress.type = ESP_IPADDR_TYPE_V4;
if (!ipAddress.u_addr.ip4.addr) {
valid = false;
}
ESP_LOGD(TAG, "%s (%lu)", toChar(), toUInt());
}
// IPAddress::IPAddress(IPAddress& src) { ipAddress = src.ipAddress; }
IPAddress::IPAddress(const uint8_t* pAddr) { ipAddress.u_addr.ip4.addr = *((uint32_t*)pAddr); }
IPAddress::IPAddress(const uint32_t addr) { ipAddress.u_addr.ip4.addr = addr; }
const char* IPAddress::toChar() {
sprintf(string, IPSTR, IP2STR(&ipAddress.u_addr.ip4));
return string;
}
IPAddress& IPAddress::fromChar(const char* str) {
if (ESP_OK == esp_netif_str_to_ip4(str, &ipAddress.u_addr.ip4)) {
ipAddress.type = ESP_IPADDR_TYPE_V4;
return *this;
} else if (ESP_OK == esp_netif_str_to_ip6(str, &ipAddress.u_addr.ip6)) {
ipAddress.type = ESP_IPADDR_TYPE_V6;
return *this;
}
ipAddress.u_addr.ip4.addr = ESP_IP4TOADDR(0, 0, 0, 0);
ipAddress.type = ESP_IPADDR_TYPE_V4;
valid = false;
return *this;
}
bool IPAddress::operator==(IPAddress address) {
return (address.ipAddress.u_addr.ip4.addr == ipAddress.u_addr.ip4.addr);
}
IPAddress& IPAddress::operator=(esp_ip4_addr_t ip4) {
if (!ip4.addr) {
valid = false;
}
ipAddress.u_addr.ip4.addr = ip4.addr;
ipAddress.type = ESP_IPADDR_TYPE_V4;
return *this;
}
IPAddress& IPAddress::operator=(esp_ip6_addr_t ip6) {
memcpy(ipAddress.u_addr.ip6.addr, ip6.addr, sizeof(ip6.addr));
ipAddress.type = ESP_IPADDR_TYPE_V6;
return *this;
}
IPAddress& IPAddress::operator=(uint8_t * pAddr) {
if (!pAddr) {
valid = false;
} else {
ipAddress.u_addr.ip4.addr = *(uint32_t *)pAddr;
ipAddress.type = ESP_IPADDR_TYPE_V4;
}
return *this;
}
IPAddress& IPAddress::operator=(uint32_t addr) {
if (!addr) {
valid = false;
}
ipAddress.u_addr.ip4.addr = addr;
ipAddress.type = ESP_IPADDR_TYPE_V4;
return *this;
}
int IPAddress::type(void) { return ipAddress.type; }
void IPAddress::type(int t) { ipAddress.type = t; }
uint32_t IPAddress::toUInt(void) {
return esp_netif_htonl(ipAddress.u_addr.ip4.addr);
}
uint32_t IPAddress::toAddr(void) {
return ipAddress.u_addr.ip4.addr;
}