You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1011 B
42 lines
1011 B
#include <esp_wifi_types.h>
|
|
#include <esp_wifi.h>
|
|
#include <esp_log.h>
|
|
#include <fcntl.h>
|
|
#include <sys/socket.h>
|
|
|
|
#include "httpd_utils/fd_to_ipv4.h"
|
|
|
|
static const char *TAG = "fd2ipv4";
|
|
|
|
/**
|
|
* Get IP address for a FD
|
|
*
|
|
* @param fd
|
|
* @param[out] ipv4
|
|
* @return success
|
|
*/
|
|
esp_err_t fd_to_ipv4(int fd, in_addr_t *ipv4)
|
|
{
|
|
struct sockaddr_in6 addr;
|
|
size_t len = sizeof(addr);
|
|
int rv = getpeername(fd, (struct sockaddr *) &addr, &len);
|
|
if (rv != 0) {
|
|
ESP_LOGE(TAG, "Failed to get IP addr for fd %d", fd);
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
uint32_t client_addr = 0;
|
|
if (addr.sin6_family == AF_INET6) {
|
|
// this would fail in a real ipv6 network
|
|
// with ipv4 the addr is simply in the last ipv6 byte
|
|
struct sockaddr_in6 *s = &addr;
|
|
client_addr = s->sin6_addr.un.u32_addr[3];
|
|
}
|
|
else {
|
|
struct sockaddr_in *s = (struct sockaddr_in *) &addr;
|
|
client_addr = s->sin_addr.s_addr;
|
|
}
|
|
|
|
*ipv4 = client_addr;
|
|
return ESP_OK;
|
|
}
|
|
|