Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix esp-idf and IPv6 support by using plain UDP socket, drop Syslog library #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dwmw2
Copy link

@dwmw2 dwmw2 commented Aug 5, 2024

It turns out to be fairly simple to just send the UDP packets directly, and the UDP socket handling can then be a whole lot more generic.

…ibrary

It turns out to be fairly simple to just send the UDP packets directly,
and the UDP socket handling can then be a whole lot more generic.
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.
@chatziko
Copy link

Thanks for this PR, it works great with esp-idf!

However with arduino I get the following error:

src/esphome/components/syslog/syslog_component.cpp: In member function 'virtual void esphome::syslog::SyslogComponent::setup()':
src/esphome/components/syslog/syslog_component.cpp:65:72: error: 'IPPROTO_UDP' was not declared in this scope; did you mean 'IPPROTO_TCP'?
   65 |     this->socket_ = socket::socket(this->server.ss_family, SOCK_DGRAM, IPPROTO_UDP);
      |                                                                        ^~~~~~~~~~~
      |                                                                        IPPROTO_TCP

I'm using esphome 2024.9.0 with the following config:

external_components:
  - source: github://dwmw2/esphome_syslog@esp-ipv6-support
    components: [syslog]

syslog:
    ip_address: <redacted>
    port: 514

Any chance of making the code compatible with both arduino and esp-idf?

@dwmw2
Copy link
Author

dwmw2 commented Sep 26, 2024

Hm, which platform is that on? I thought I'd tested it with Arduino for ESP32. I think there are some platforms where UDP isn't available directly in LwIP and we have to use the Arduino UDP as before, as I mentioned above. I'll have a look at making the change conditional.

@chatziko
Copy link

Oh, just realized that this was on a esp8266 device (not esp32). But there are still plenty of those out there :D

this->server_socklen = sizeof(*server4);
}
if (!this->server_socklen) {
ESP_LOGW(TAG, "Failed to parse server IP address '%s'", this->settings_.address.c_str());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO i would log error, mark component failed and then return from setup.

}
this->socket_ = socket::socket(this->server.ss_family, SOCK_DGRAM, IPPROTO_UDP);
if (!this->socket_) {
ESP_LOGW(TAG, "Failed to create UDP socket");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, loge, mark failed.

@@ -53,21 +92,16 @@ void SyslogComponent::loop() {
void SyslogComponent::log(uint8_t level, const std::string &tag, const std::string &payload) {
level = level > 7 ? 7 : level;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do something like:

if (this->is_failed()) {
  // maybe log with ESP_LOGV or ESP_LOGVV to reduce spam since component is marked failed 
  return;
}

ESP_LOGW(TAG, "Failed to parse server IP address '%s'", this->settings_.address.c_str());
return;
}
this->socket_ = socket::socket(this->server.ss_family, SOCK_DGRAM, IPPROTO_UDP);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPPROTO_UDP is not defined on arduino for esp8266.
I managed to fix this by adding

#ifndef IPPROTO_UDP
#include <lwip/ip.h>
#define IPPROTO_UDP IP_PROTO_UDP
#endif

Maybe there is a better way but this works for me. (Also tested with esp-idf.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants