diff --git a/esphome/components/udp/udp_component.cpp b/esphome/components/udp/udp_component.cpp index 3ba8fda12b..dfc5a319de 100644 --- a/esphome/components/udp/udp_component.cpp +++ b/esphome/components/udp/udp_component.cpp @@ -198,7 +198,12 @@ void UDPComponent::setup() { this->header_.push_back(0); #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) for (const auto &address : this->addresses_) { +#if USE_NETWORK_IPV6 struct sockaddr_in6 saddr {}; +#else + struct sockaddr saddr {}; +#endif + auto err = socket::set_sockaddr(reinterpret_cast(&saddr), sizeof(saddr), address, this->port_); if (err == 0) { ESP_LOGV(TAG, "Couldn't set sockaddr %d", errno); @@ -589,19 +594,23 @@ void UDPComponent::increment_code_() { void UDPComponent::send_packet_(void *data, size_t len) { #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) for (const auto &saddr : this->sockaddrs_) { +#if USE_NETWORK_IPV6 if (saddr.sin6_family == AF_INET) { auto result = this->broadcast_socket_->sendto(data, len, 0, reinterpret_cast(&saddr), sizeof(saddr)); if (result < 0) ESP_LOGW(TAG, "sendto() error %d", errno); } -#if USE_NETWORK_IPV6 if (saddr.sin6_family == AF_INET6) { auto result = this->broadcast_socket6_->sendto(data, len, 0, reinterpret_cast(&saddr), sizeof(saddr)); if (result < 0) ESP_LOGW(TAG, "sendto() error %d", errno); } +#else + auto result = this->broadcast_socket_->sendto(data, len, 0, &saddr, sizeof(saddr)); + if (result < 0) + ESP_LOGW(TAG, "sendto() error %d", errno); #endif } #else diff --git a/esphome/components/udp/udp_component.h b/esphome/components/udp/udp_component.h index cfb558fa48..6844843565 100644 --- a/esphome/components/udp/udp_component.h +++ b/esphome/components/udp/udp_component.h @@ -125,9 +125,11 @@ class UDPComponent : public PollingComponent { std::unique_ptr broadcast_socket_ = nullptr; #if USE_NETWORK_IPV6 std::unique_ptr broadcast_socket6_ = nullptr; + std::vector sockaddrs_{}; +#else + std::vector sockaddrs_{}; #endif std::unique_ptr listen_socket_ = nullptr; - std::vector sockaddrs_{}; #else std::vector ipaddrs_{}; WiFiUDP udp_client_{};