6#ifdef USE_UDP_OMR_SOURCE
14static constexpr uint32_t NETWORK_POLL_ID = 0;
15static constexpr uint32_t NETWORK_POLL_INTERVAL_MS = 1000;
18 size_t address_count = 0;
19 for (
const char *
const *it = this->
addresses_; it !=
nullptr && *it !=
nullptr; it++)
22 for (
const char *
const *it = this->
addresses_; it !=
nullptr && *it !=
nullptr; it++) {
26 ESP_LOGW(TAG,
"Invalid address %s",
address);
35#ifdef USE_UDP_IPV6_SEND
46 auto err = this->
broadcast_socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
51 err = this->
broadcast_socket_->setsockopt(SOL_SOCKET, SO_BROADCAST, &enable,
sizeof(
int));
56#ifdef USE_UDP_IPV6_SEND
70#ifdef USE_UDP_OMR_SOURCE
88 if (server.ss_family == AF_INET6)
98 this->
listen_socket_->setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, &v6only,
sizeof(v6only)) != 0) {
99 ESP_LOGW(TAG,
"Failed to clear IPV6_V6ONLY: errno %d", errno);
115 ESP_LOGE(TAG,
"Unable to set nonblocking: errno %d", errno);
121 err = this->
listen_socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(enable));
130 ESP_LOGE(TAG,
"Socket unable to bind: errno %d", errno);
139 this->
set_interval(NETWORK_POLL_ID, NETWORK_POLL_INTERVAL_MS, [
this]() {
149#ifdef USE_SOCKET_IPV6
159 this->
set_poll_warning_(POLL_JOIN, LOG_STR(
"Failed to join multicast group"), errno);
163#ifdef USE_SOCKET_IPV6
179#ifdef USE_UDP_IPV6_SEND
188 this->
set_poll_warning_(POLL_MCAST_IF, LOG_STR(
"Failed to set IPv6 multicast interface"), errno);
193#ifdef USE_UDP_OMR_SOURCE
197 auto omr = ot !=
nullptr ? ot->get_omr_address() : std::nullopt;
198 if (!omr.has_value()) {
200 this->
set_poll_warning_(POLL_SRC_BIND, LOG_STR(
"No routable IPv6 source address"), 0);
204 src.sin6_family = AF_INET6;
205 memcpy(&
src.sin6_addr, omr->mFields.m8,
sizeof(
src.sin6_addr));
210 this->
set_poll_warning_(POLL_SRC_BIND, LOG_STR(
"Unable to bind the OMR source address"), errno);
231 ESP_LOGW(TAG,
"%s. Error %d", LOG_STR_ARG(what), err);
233 ESP_LOGW(TAG,
"%s", LOG_STR_ARG(what));
248 std::array<uint8_t, MAX_PACKET_SIZE + 1> buf;
255 size_t packet_len =
static_cast<size_t>(
len);
256 if (packet_len > MAX_PACKET_SIZE) {
257 ESP_LOGV(TAG,
"Packet longer than %zu bytes dropped", MAX_PACKET_SIZE);
260 ESP_LOGV(TAG,
"Received packet of length %zu", packet_len);
269 " Broadcast Port: %u",
271 for (
const char *
const *it = this->
addresses_; it !=
nullptr && *it !=
nullptr; it++) {
273 ESP_LOGCONFIG(TAG,
" Address: %s",
address);
279 " Broadcasting: %s\n"
289#ifdef USE_UDP_IPV6_SEND
290 const bool v6 = entry.sa.sa_family == AF_INET6;
293 ESP_LOGV(TAG,
"Egress interface not set yet, multicast packet dropped");
296#ifdef USE_UDP_OMR_SOURCE
302 ESP_LOGW(TAG,
"IPv6 sends are held until a Thread border router provides an OMR address. "
303 "Put mesh-only destinations in a separate udp block to send them without waiting");
305 ESP_LOGV(TAG,
"IPv6 source not bound yet, packet dropped");
315 if (sock ==
nullptr) {
316 ESP_LOGW(TAG,
"Broadcast socket not initialized");
319 auto result = sock->
sendto(data,
size, 0, &entry.sa, sockaddr_size);
323 if (errno == ENOMEM) {
324 ESP_LOGV(TAG,
"sendto() out of memory");
326 ESP_LOGW(TAG,
"sendto() error %d", errno);
void mark_failed()
Mark this component as failed.
void status_set_warning()
bool cancel_interval(const char *name)
Cancel an interval function.
bool status_has_warning() const
void disable_loop()
Disable this component's loop.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
void status_clear_warning()
ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
std::unique_ptr< socket::UDPSendSocket > send_socket_v6_
const char * listen_address_
FixedVector< SockaddrEntry > sockaddrs_
LazyCallbackManager< void(std::span< const uint8_t >)> packet_listeners_
bool network_poll_()
One pass of the network poll; true once nothing is left to do.
std::unique_ptr< socket::UDPSendSocket > broadcast_socket_
void set_poll_warning_(PollStep step, const LogString *what, int err)
err is the errno of the failed call, or 0 when no call failed.
void dump_config() override
const char *const * addresses_
std::unique_ptr< socket::UDPSocket > listen_socket_
bool send_packet(const uint8_t *data, size_t size)
Returns false if the packet could not be sent to every destination.
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
OpenThreadComponent * global_openthread_component
std::unique_ptr< UDPSendSocket > socket_udp_send(int domain, int protocol)
Create a send-only UDP socket (socket_udp_send), a UDP socket with receive support (socket_udp),...
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port)
Set a sockaddr to the specified address and port for the IP version used by socket_ip().
bool set_ipv6_multicast_if(Socket *sock, uint32_t if_index_in=0)
Configure the outgoing IPv6 multicast interface on the given socket.
bool join_multicast_group(UDPSocket *sock, const char *ip_address, uint32_t *if_index_out=nullptr)
Join a multicast group on the given socket.
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
std::unique_ptr< UDPSocket > socket_udp_loop_monitored(int domain, int protocol)
ESPHOME_LOG_TAG(TAG, "udp.packet_interface")