فهرست منبع

httpd: print the source of the request

Print the network peer name of the incoming request.
H. Peter Anvin 1 سال پیش
والد
کامیت
0c6fc27810
5فایلهای تغییر یافته به همراه44 افزوده شده و 2 حذف شده
  1. 44 2
      esp32/max80/httpd.c
  2. BIN
      esp32/output/max80.ino.bin
  3. BIN
      fpga/output/max80.fw
  4. BIN
      fpga/output/v1.fw
  5. BIN
      fpga/output/v2.fw

+ 44 - 2
esp32/max80/httpd.c

@@ -5,6 +5,8 @@
 #include "httpd.h"
 #include "config.h"
 #include "boardinfo_esp.h"
+#include "lwip/sockets.h"
+#include "lwip/inet.h"
 
 #include <incbin.h>
 #include <unzipLIB.h>
@@ -142,9 +144,49 @@ static const char *http_dos_date(uint32_t dos_date)
 
 static const char text_plain[] = "text/plain; charset=\"UTF-8\"";
 
-static void httpd_print_request(const httpd_req_t *req)
+static void httpd_print_request(httpd_req_t *req)
 {
-    printf("[HTTP] %s %s\n", http_method_str(req->method), req->uri);
+    /* Get the client address */
+    union {
+	struct sockaddr sa;
+	struct sockaddr_in sin;
+	struct sockaddr_in6 sin6;
+	struct sockaddr_storage ss;
+    } sa = { };
+    char addrbuf[64];
+    char *p = addrbuf;
+    const char * const endp = addrbuf + sizeof addrbuf;
+    int sock = httpd_req_to_sockfd(req);
+    socklen_t sa_len = sizeof sa;
+
+    if (getpeername(sock, &sa.sa, &sa_len))
+	sa.sa.sa_family = AF_UNSPEC;
+
+    /* lwip lacks getnameinfo() */
+
+    switch (sa.sa.sa_family) {
+    case AF_INET:
+	inet_ntop(AF_INET, &sa.sin.sin_addr, p, endp - p);
+	p = strchr(p, '\0');
+	p += sprintf(p, ":%u", ntohs(sa.sin.sin_port));
+	break;
+    case AF_INET6:
+	*p++ = '[';
+	inet_ntop(AF_INET6, &sa.sin6.sin6_addr, p, endp - p);
+	p = strchr(p, '\0');
+	p += sprintf(p, "]:%u", ntohs(sa.sin6.sin6_port));
+	break;
+    case AF_UNSPEC:
+	strcpy(p, "[not a socket?]");
+	break;
+    default:
+	p += sprintf(p, "[AF %u?]", sa.sa.sa_family);
+	break;
+    }
+
+    /* sa.sin.sin_port == sa.sin6.sin6_port */
+    printf("[HTTP] %s %s %s\n",
+	   addrbuf, http_method_str(req->method), req->uri);
 }
 
 static char *httpd_req_get_hdr(httpd_req_t *req, const char *field, size_t *lenp)

BIN
esp32/output/max80.ino.bin


BIN
fpga/output/max80.fw


BIN
fpga/output/v1.fw


BIN
fpga/output/v2.fw