mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 14:43:51 +00:00
@@ -434,7 +434,8 @@ static bool process_rolling_code(Provider &provider, uint8_t *&buf, const uint8_
|
||||
void UDPComponent::process_(uint8_t *buf, const size_t len) {
|
||||
auto ping_key_seen = !this->ping_pong_enable_;
|
||||
if (len < 8) {
|
||||
return ESP_LOGV(TAG, "Bad length %zu", len);
|
||||
ESP_LOGV(TAG, "Bad length %zu", len);
|
||||
return;
|
||||
}
|
||||
char namebuf[256]{};
|
||||
uint8_t byte;
|
||||
@@ -442,31 +443,40 @@ void UDPComponent::process_(uint8_t *buf, const size_t len) {
|
||||
const uint8_t *end = buf + len;
|
||||
FuData rdata{};
|
||||
auto magic = get_uint16(buf);
|
||||
if (magic != MAGIC_NUMBER && magic != MAGIC_PING)
|
||||
return ESP_LOGV(TAG, "Bad magic %X", magic);
|
||||
if (magic != MAGIC_NUMBER && magic != MAGIC_PING) {
|
||||
ESP_LOGV(TAG, "Bad magic %X", magic);
|
||||
return;
|
||||
}
|
||||
|
||||
auto hlen = *buf++;
|
||||
if (hlen > len - 3) {
|
||||
return ESP_LOGV(TAG, "Bad hostname length %u > %zu", hlen, len - 3);
|
||||
ESP_LOGV(TAG, "Bad hostname length %u > %zu", hlen, len - 3);
|
||||
return;
|
||||
}
|
||||
memcpy(namebuf, buf, hlen);
|
||||
if (strcmp(this->name_, namebuf) == 0) {
|
||||
return ESP_LOGV(TAG, "Ignoring our own data");
|
||||
ESP_LOGV(TAG, "Ignoring our own data");
|
||||
return;
|
||||
}
|
||||
buf += hlen;
|
||||
if (magic == MAGIC_PING)
|
||||
return this->process_ping_request_(namebuf, buf, end - buf);
|
||||
if (magic == MAGIC_PING) {
|
||||
this->process_ping_request_(namebuf, buf, end - buf);
|
||||
return;
|
||||
}
|
||||
if (round4(len) != len) {
|
||||
return ESP_LOGW(TAG, "Bad length %zu", len);
|
||||
ESP_LOGW(TAG, "Bad length %zu", len);
|
||||
return;
|
||||
}
|
||||
hlen = round4(hlen + 3);
|
||||
buf = start_ptr + hlen;
|
||||
if (buf == end) {
|
||||
return ESP_LOGV(TAG, "No data after header");
|
||||
ESP_LOGV(TAG, "No data after header");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->providers_.count(namebuf) == 0) {
|
||||
return ESP_LOGVV(TAG, "Unknown hostname %s", namebuf);
|
||||
ESP_LOGVV(TAG, "Unknown hostname %s", namebuf);
|
||||
return;
|
||||
}
|
||||
auto &provider = this->providers_[namebuf];
|
||||
// if encryption not used with this host, ping check is pointless since it would be easily spoofed.
|
||||
@@ -489,7 +499,8 @@ void UDPComponent::process_(uint8_t *buf, const size_t len) {
|
||||
if (!process_rolling_code(provider, buf, end))
|
||||
return;
|
||||
} else if (byte != DATA_KEY) {
|
||||
return ESP_LOGV(TAG, "Expected rolling_key or data_key, got %X", byte);
|
||||
ESP_LOGV(TAG, "Expected rolling_key or data_key, got %X", byte);
|
||||
return;
|
||||
}
|
||||
while (buf < end) {
|
||||
byte = *buf++;
|
||||
@@ -497,7 +508,8 @@ void UDPComponent::process_(uint8_t *buf, const size_t len) {
|
||||
continue;
|
||||
if (byte == PING_KEY) {
|
||||
if (end - buf < 4) {
|
||||
return ESP_LOGV(TAG, "PING_KEY requires 4 more bytes");
|
||||
ESP_LOGV(TAG, "PING_KEY requires 4 more bytes");
|
||||
return;
|
||||
}
|
||||
auto key = get_uint32(buf);
|
||||
if (key == this->ping_key_) {
|
||||
@@ -515,21 +527,25 @@ void UDPComponent::process_(uint8_t *buf, const size_t len) {
|
||||
}
|
||||
if (byte == BINARY_SENSOR_KEY) {
|
||||
if (end - buf < 3) {
|
||||
return ESP_LOGV(TAG, "Binary sensor key requires at least 3 more bytes");
|
||||
ESP_LOGV(TAG, "Binary sensor key requires at least 3 more bytes");
|
||||
return;
|
||||
}
|
||||
rdata.u32 = *buf++;
|
||||
} else if (byte == SENSOR_KEY) {
|
||||
if (end - buf < 6) {
|
||||
return ESP_LOGV(TAG, "Sensor key requires at least 6 more bytes");
|
||||
ESP_LOGV(TAG, "Sensor key requires at least 6 more bytes");
|
||||
return;
|
||||
}
|
||||
rdata.u32 = get_uint32(buf);
|
||||
} else {
|
||||
return ESP_LOGW(TAG, "Unknown key byte %X", byte);
|
||||
ESP_LOGW(TAG, "Unknown key byte %X", byte);
|
||||
return;
|
||||
}
|
||||
|
||||
hlen = *buf++;
|
||||
if (end - buf < hlen) {
|
||||
return ESP_LOGV(TAG, "Name length of %u not available", hlen);
|
||||
ESP_LOGV(TAG, "Name length of %u not available", hlen);
|
||||
return;
|
||||
}
|
||||
memset(namebuf, 0, sizeof namebuf);
|
||||
memcpy(namebuf, buf, hlen);
|
||||
|
||||
Reference in New Issue
Block a user