ESPHome
2026.11.0-dev
Loading...
Searching...
No Matches
esphome
components
socket
ipv4_allow.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
headers.h
"
4
#include "
socket.h
"
5
#include "
esphome/core/hal.h
"
6
7
#include <cstddef>
8
#include <cstdint>
9
10
namespace
esphome::socket
{
11
14
struct
Ipv4AllowEntry
{
15
uint32_t
addr
;
16
uint32_t
mask
;
17
};
18
20
class
Ipv4Allow
{
21
public
:
22
void
set
(
const
Ipv4AllowEntry
*entries,
size_t
count
) {
23
this->entries_ = entries;
24
this->count_ =
count
;
25
}
26
28
bool
allows
(
const
struct
sockaddr
*peer)
const
{
29
if
(this->count_ == 0) {
30
return
true
;
31
}
32
uint32_t
addr;
33
return
sockaddr_to_ipv4
(peer, &addr) && this->
allows
(addr);
34
}
35
37
bool
allows
(
uint32_t
addr)
const
{
38
if
(this->count_ == 0) {
39
return
true
;
40
}
41
for
(
size_t
i = 0; i != this->count_; i++) {
42
Ipv4AllowEntry
e = this->
entry
(i);
43
if
((addr & e.
mask
) == e.
addr
) {
44
return
true
;
45
}
46
}
47
return
false
;
48
}
49
50
size_t
size
()
const
{
return
this->count_; }
52
Ipv4AllowEntry
entry
(
size_t
i)
const
{
53
Ipv4AllowEntry
e;
54
progmem_memcpy
(&e, &this->entries_[i],
sizeof
(e));
55
return
e;
56
}
57
58
private
:
59
const
Ipv4AllowEntry
*entries_{
nullptr
};
60
size_t
count_{0};
61
};
62
63
}
// namespace esphome::socket
esphome::socket::Ipv4Allow
IPv4 peers that may connect. An empty list allows every peer.
Definition
ipv4_allow.h:20
esphome::socket::Ipv4Allow::entry
Ipv4AllowEntry entry(size_t i) const
A copy of entry i, read from flash.
Definition
ipv4_allow.h:52
esphome::socket::Ipv4Allow::allows
bool allows(uint32_t addr) const
addr is network byte order, as it sits in a sockaddr_in.
Definition
ipv4_allow.h:37
esphome::socket::Ipv4Allow::size
size_t size() const
Definition
ipv4_allow.h:50
esphome::socket::Ipv4Allow::allows
bool allows(const struct sockaddr *peer) const
A v4 mapped IPv6 peer is unwrapped; any other family fails a non empty list.
Definition
ipv4_allow.h:28
esphome::socket::Ipv4Allow::set
void set(const Ipv4AllowEntry *entries, size_t count)
Definition
ipv4_allow.h:22
hal.h
count
unsigned count
Definition
gif.cpp:51
headers.h
esphome::socket
Definition
bsd_sockets_impl.cpp:13
esphome::socket::sockaddr_to_ipv4
bool sockaddr_to_ipv4(const struct sockaddr *addr, uint32_t *out)
Network order IPv4 of an AF_INET or v4 mapped AF_INET6 peer; false for anything else.
esphome::progmem_memcpy
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition
hal.h:56
uint32_t
static void uint32_t
Definition
crash_handler.cpp:153
socket.h
esphome::socket::Ipv4AllowEntry
One allowed IPv4 network, network byte order, host bits cleared.
Definition
ipv4_allow.h:14
esphome::socket::Ipv4AllowEntry::mask
uint32_t mask
Definition
ipv4_allow.h:16
esphome::socket::Ipv4AllowEntry::addr
uint32_t addr
Definition
ipv4_allow.h:15
sockaddr
Definition
headers.h:95
Generated by
1.12.0