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

Commit b448444

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. Just do it ourselves for now, until it's fixed by esphome/esphome#7196
1 parent a97eaef commit b448444

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

components/syslog/syslog_component.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,36 @@ 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+
* Do not use socket::set_sockaddr() for now, because it returns bogus results
32+
* if trying to log to a Legacy IP address when IPv6 is enabled.
33+
*/
34+
this->server_socklen = 0;
35+
if (0) {
36+
this->server_socklen = socket::set_sockaddr((struct sockaddr *)&this->server, sizeof(this->server),
37+
this->settings_.address, this->settings_.port);
38+
}
39+
#if USE_NETWORK_IPV6
40+
else if (this->settings_.address.find(':') != std::string::npos) {
41+
auto *server6 = reinterpret_cast<sockaddr_in6 *>(&this->server);
42+
memset(server6, 0, sizeof(*server6));
43+
server6->sin6_family = AF_INET6;
44+
server6->sin6_port = htons(this->settings_.port);
45+
46+
ip6_addr_t ip6;
47+
inet6_aton(this->settings_.address.c_str(), &ip6);
48+
memcpy(server6->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
49+
this->server_socklen = sizeof(*server6);
50+
}
51+
#endif /* USE_NETWORK_IPV6 */
52+
else {
53+
auto *server4 = reinterpret_cast<sockaddr_in *>(&this->server);
54+
memset(server4, 0, sizeof(*server4));
55+
server4->sin_family = AF_INET;
56+
server4->sin_addr.s_addr = inet_addr(this->settings_.address.c_str());
57+
server4->sin_port = htons(this->settings_.port);
58+
this->server_socklen = sizeof(*server4);
59+
}
3260
if (!this->server_socklen) {
3361
ESP_LOGW(TAG, "Failed to parse server IP address '%s'", this->settings_.address.c_str());
3462
return;

0 commit comments

Comments
 (0)