From 1c43cecb722a8a6fa81beaa34d592af27e5f855b Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Mon, 22 Apr 2024 17:21:37 +0200 Subject: [PATCH] Show remote ip address when somebody requests and invalid path to ease debugging --- components/esp_http_server/src/httpd_uri.c | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/components/esp_http_server/src/httpd_uri.c b/components/esp_http_server/src/httpd_uri.c index 5746febc74..99aa413e30 100644 --- a/components/esp_http_server/src/httpd_uri.c +++ b/components/esp_http_server/src/httpd_uri.c @@ -294,10 +294,31 @@ esp_err_t httpd_uri(struct httpd_data *hd) /* If URI with method not found, respond with error code */ if (uri == NULL) { + switch (err) { case HTTPD_404_NOT_FOUND: - ESP_LOGW(TAG, LOG_FMT("URI '%s' not found"), req->uri); + { + char ipstr[INET6_ADDRSTRLEN] = "UNKNOWN"; + + const int sockfd = httpd_req_to_sockfd(req); + + if (sockfd < 0) + ESP_LOGW(TAG, "httpd_req_to_sockfd() failed with %i", sockfd); + else + { + struct sockaddr_in6 addr; // esp_http_server uses IPv6 addressing + socklen_t addr_size = sizeof(addr); + const int result = getpeername(sockfd, (struct sockaddr *)&addr, &addr_size); + + if (result < 0) + ESP_LOGW(TAG, "getpeername() failed with %i", result); + else + inet_ntop(AF_INET, &addr.sin6_addr.un.u32_addr[3], ipstr, sizeof(ipstr)); + } + + ESP_LOGW(TAG, LOG_FMT("URI '%s' not found for %s"), req->uri, ipstr); return httpd_req_handle_err(req, HTTPD_404_NOT_FOUND); + } case HTTPD_405_METHOD_NOT_ALLOWED: ESP_LOGW(TAG, LOG_FMT("Method '%d' not allowed for URI '%s'"), req->method, req->uri);