Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Commit 4f7ca62

Browse files
committed
Work around socket::set_sockaddr() bug with Legacy IP syslog
Using a Legacy IP syslog server in an IPv6-enabled build fails due to a socket::set_sockaddr() bug. Work around it in older versions of esphome.
1 parent a97eaef commit 4f7ca62

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

components/syslog/syslog_component.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
#include "esphome/core/log.h"
44
#include "esphome/core/application.h"
5+
#include "esphome/core/version.h"
56

67
#ifdef USE_LOGGER
78
#include "esphome/components/logger/logger.h"
89
#endif
910
/*
1011
#include "esphome/core/helpers.h"
1112
#include "esphome/core/defines.h"
12-
#include "esphome/core/version.h"
1313
*/
1414

1515
namespace esphome {
@@ -27,8 +27,37 @@ SyslogComponent::SyslogComponent() {
2727

2828
void SyslogComponent::setup() {
2929

30-
this->server_socklen = socket::set_sockaddr((struct sockaddr *)&this->server, sizeof(this->server),
31-
this->settings_.address, this->settings_.port);
30+
/*
31+
* Older versions of socket::set_sockaddr() return bogus results
32+
* if trying to log to a Legacy IP address when IPv6 is enabled.
33+
* Fixed by https://github.com/esphome/esphome/pull/7196
34+
*/
35+
this->server_socklen = 0;
36+
if (ESPHOME_VERSION_CODE >= VERSION_CODE(2024, 8, 0)) {
37+
this->server_socklen = socket::set_sockaddr((struct sockaddr *)&this->server, sizeof(this->server),
38+
this->settings_.address, this->settings_.port);
39+
}
40+
#if USE_NETWORK_IPV6
41+
else if (this->settings_.address.find(':') != std::string::npos) {
42+
auto *server6 = reinterpret_cast<sockaddr_in6 *>(&this->server);
43+
memset(server6, 0, sizeof(*server6));
44+
server6->sin6_family = AF_INET6;
45+
server6->sin6_port = htons(this->settings_.port);
46+
47+
ip6_addr_t ip6;
48+
inet6_aton(this->settings_.address.c_str(), &ip6);
49+
memcpy(server6->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
50+
this->server_socklen = sizeof(*server6);
51+
}
52+
#endif /* USE_NETWORK_IPV6 */
53+
else {
54+
auto *server4 = reinterpret_cast<sockaddr_in *>(&this->server);
55+
memset(server4, 0, sizeof(*server4));
56+
server4->sin_family = AF_INET;
57+
server4->sin_addr.s_addr = inet_addr(this->settings_.address.c_str());
58+
server4->sin_port = htons(this->settings_.port);
59+
this->server_socklen = sizeof(*server4);
60+
}
3261
if (!this->server_socklen) {
3362
ESP_LOGW(TAG, "Failed to parse server IP address '%s'", this->settings_.address.c_str());
3463
return;

0 commit comments

Comments
 (0)