webserver - how to get incoming http request IP address

J00003
Posts: 13
Joined: Sun Mar 04, 2018 10:14 pm

webserver - how to get incoming http request IP address

Postby J00003 » Tue Jan 01, 2019 11:48 pm

I am using the webserver component as documented here: https://docs.espressif.com/projects/esp ... erver.html, and have the basics working.
But am wondering how to retrieve the IP address of the far-end or client for an incoming http request. Is it available some how thru the session or transport context?
The docs and example code don't give any clues...
Thanks,

ESP_Anurag
Posts: 19
Joined: Fri Aug 31, 2018 5:37 am

Re: webserver - how to get incoming http request IP address

Postby ESP_Anurag » Wed Jan 02, 2019 9:56 am

Hi,

You need to use the httpd_req_to_sockfd() function to obtain the sockfd of a client inside a request handler, then use getpeername() function (include <sys/socket.h>) to get the IP address from the sockfd.

Here is an example function that you can try invoking inside your request handler for printing the IP address:

Code: Select all

void print_client_ip(httpd_req_t *req)
{
    int sockfd = httpd_req_to_sockfd(req);
    char ipstr[INET6_ADDRSTRLEN];
    struct sockaddr_in6 addr;   // esp_http_server uses IPv6 addressing
    socklen_t addr_size = sizeof(addr);
    
    if (getpeername(sockfd, (struct sockaddr *)&addr, &addr_size) < 0) {
        ESP_LOGE(TAG, "Error getting client IP");
        return;
    }
    
    // Convert to IPv6 string
    inet_ntop(AF_INET, &addr.sin6_addr, ipstr, sizeof(ipstr));
    ESP_LOGI(TAG, "Client IP => %s", ipstr);
}
Now, you may prefer to print the IPv4 part of the address, in which case you can do (as described in this post http://bbs.esp32.com/viewtopic.php?t=8317):

Code: Select all

    // Convert to IPv4 string
    inet_ntop(AF_INET, &addr.sin6_addr.un.u32_addr[3], ipstr, sizeof(ipstr));
    ESP_LOGI(TAG, "Client IP => %s", ipstr);

J00003
Posts: 13
Joined: Sun Mar 04, 2018 10:14 pm

Re: webserver - how to get incoming http request IP address

Postby J00003 » Thu Jan 03, 2019 12:42 am

Thank you @ESP_Anurag, I'll give that a try.

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: webserver - how to get incoming http request IP address

Postby Craige Hales » Tue Sep 07, 2021 12:25 pm

Thanks!
Pretty sure the IPv6 call should use AF_INET6:

Code: Select all

inet_ntop(AF_INET6, &addr.sin6_addr, ipstr, sizeof(ipstr));
I wanted to understand the [3] for the IPv4 call, here's some info:

https://datatracker.ietf.org/doc/html/r ... on-2.5.5.2 :
IPv4-Mapped IPv6 Address
A second type of IPv6 address that holds an embedded IPv4 address is
defined. This address type is used to represent the addresses of
IPv4 nodes as IPv6 addresses. The format of the "IPv4-mapped IPv6
address" is as follows:

Code: Select all

   |                80 bits               | 16 |      32 bits        |
   +--------------------------------------+--------------------------+
   |0000..............................0000|FFFF|    IPv4 address     |
   +--------------------------------------+----+---------------------+
which is what I see with AF_INET6:

Code: Select all

I (183749) file_server: Client IP => ::FFFF:192.168.4.121
Craige

sinmosh
Posts: 6
Joined: Mon Oct 17, 2022 3:06 am

Re: webserver - how to get incoming http request IP address

Postby sinmosh » Thu Sep 07, 2023 10:46 pm

@ESP_Anurag Thank you for the great hint!

Who is online

Users browsing this forum: Baidu [Spider] and 77 guests