Function getpeername() doesn't work, trying to determine remote IP Address

mikea101
Posts: 2
Joined: Wed Nov 21, 2018 6:53 pm

Function getpeername() doesn't work, trying to determine remote IP Address

Postby mikea101 » Tue Dec 04, 2018 7:56 pm

Hi,

I'm trying to determine the remote IP of a browser connected to the HTTP server (httpd).

When I use the function getpeername() to get the IP address of the remote client browser it is always "0.0.0.0" (which is wrong).

Code: Select all

esp_err_t MyHttpHandler(httpd_req_t *req)
{
	int len = 0;
	char buf[200];

	int socket = httpd_req_to_sockfd(req); // This is the socket for the request

	struct sockaddr_in saddr; // expecting an IPv4 address
	memset(&saddr, 0, sizeof(saddr)); // Clear it to be sure
	socklen_t saddr_len = sizeof(saddr);

	if(getpeername(socket, (struct sockaddr *)&saddr, &saddr_len) == 0)
	{
		len += sprintf(&buf[len], "\n\t\"remip\":\"%s\"", inet_ntoa(saddr.sin_addr));
		len += sprintf(&buf[len], "\n\t\"remport\":%d",       htons(saddr.sin_port));
	}

	httpd_resp_send(req, buf, len);
	return ESP_OK;
}
Results in this:

Code: Select all

	"remip":"0.0.0.0"
	"remport":65198
The port seems to be OK but the ip is not.

Please advise: either a solution for getpeername or another solution that will get me the remote IP.

Thanks.

Who is online

Users browsing this forum: No registered users and 4 guests