ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
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
10namespace esphome::socket {
11
18
20class 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 {
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
IPv4 peers that may connect. An empty list allows every peer.
Definition ipv4_allow.h:20
Ipv4AllowEntry entry(size_t i) const
A copy of entry i, read from flash.
Definition ipv4_allow.h:52
bool allows(uint32_t addr) const
addr is network byte order, as it sits in a sockaddr_in.
Definition ipv4_allow.h:37
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
void set(const Ipv4AllowEntry *entries, size_t count)
Definition ipv4_allow.h:22
unsigned count
Definition gif.cpp:51
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.
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
static void uint32_t
One allowed IPv4 network, network byte order, host bits cleared.
Definition ipv4_allow.h:14
uint32_t mask
Definition ipv4_allow.h:16
uint32_t addr
Definition ipv4_allow.h:15