Make it compile without IPv6

This commit is contained in:
Jimmy Hedman 2024-09-03 08:08:06 +02:00
parent b3b106f9b3
commit 16529b490d
No known key found for this signature in database
2 changed files with 13 additions and 2 deletions

View file

@ -198,7 +198,12 @@ void UDPComponent::setup() {
this->header_.push_back(0); this->header_.push_back(0);
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
for (const auto &address : this->addresses_) { for (const auto &address : this->addresses_) {
#if USE_NETWORK_IPV6
struct sockaddr_in6 saddr {}; struct sockaddr_in6 saddr {};
#else
struct sockaddr saddr {};
#endif
auto err = socket::set_sockaddr(reinterpret_cast<sockaddr *>(&saddr), sizeof(saddr), address, this->port_); auto err = socket::set_sockaddr(reinterpret_cast<sockaddr *>(&saddr), sizeof(saddr), address, this->port_);
if (err == 0) { if (err == 0) {
ESP_LOGV(TAG, "Couldn't set sockaddr %d", errno); 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) { void UDPComponent::send_packet_(void *data, size_t len) {
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
for (const auto &saddr : this->sockaddrs_) { for (const auto &saddr : this->sockaddrs_) {
#if USE_NETWORK_IPV6
if (saddr.sin6_family == AF_INET) { if (saddr.sin6_family == AF_INET) {
auto result = auto result =
this->broadcast_socket_->sendto(data, len, 0, reinterpret_cast<const sockaddr *>(&saddr), sizeof(saddr)); this->broadcast_socket_->sendto(data, len, 0, reinterpret_cast<const sockaddr *>(&saddr), sizeof(saddr));
if (result < 0) if (result < 0)
ESP_LOGW(TAG, "sendto() error %d", errno); ESP_LOGW(TAG, "sendto() error %d", errno);
} }
#if USE_NETWORK_IPV6
if (saddr.sin6_family == AF_INET6) { if (saddr.sin6_family == AF_INET6) {
auto result = auto result =
this->broadcast_socket6_->sendto(data, len, 0, reinterpret_cast<const sockaddr *>(&saddr), sizeof(saddr)); this->broadcast_socket6_->sendto(data, len, 0, reinterpret_cast<const sockaddr *>(&saddr), sizeof(saddr));
if (result < 0) if (result < 0)
ESP_LOGW(TAG, "sendto() error %d", errno); 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 #endif
} }
#else #else

View file

@ -125,9 +125,11 @@ class UDPComponent : public PollingComponent {
std::unique_ptr<socket::Socket> broadcast_socket_ = nullptr; std::unique_ptr<socket::Socket> broadcast_socket_ = nullptr;
#if USE_NETWORK_IPV6 #if USE_NETWORK_IPV6
std::unique_ptr<socket::Socket> broadcast_socket6_ = nullptr; std::unique_ptr<socket::Socket> broadcast_socket6_ = nullptr;
std::vector<struct sockaddr_in6> sockaddrs_{};
#else
std::vector<struct sockaddr> sockaddrs_{};
#endif #endif
std::unique_ptr<socket::Socket> listen_socket_ = nullptr; std::unique_ptr<socket::Socket> listen_socket_ = nullptr;
std::vector<struct sockaddr_in6> sockaddrs_{};
#else #else
std::vector<IPAddress> ipaddrs_{}; std::vector<IPAddress> ipaddrs_{};
WiFiUDP udp_client_{}; WiFiUDP udp_client_{};