How to get client address as a server using sockets

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Thorin
Posts: 2
Joined: Thu May 06, 2021 11:18 am

How to get client address as a server using sockets

Post by Thorin » Thu May 06, 2021 4:01 pm

A small background:

I'm developing a minimal resource server that is meant to respond to simple pings and send some JSON messages and so on. I want to be able to track the IP address of every client that has pinged the server, but I can't find any solution to this on the internet except for one person asking the exact question with no answer.

To connect to a client I approximately do this:

Code: Select all

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind(addr_info[0][-1])
serverSocket.listen(1)
(conn, addr) = self.serverSocket.accept()
In normal python, then addr would have ended up as a list where you easily could retrieve an IP address, and port. But micropython returns a bytearray, which I don't know how to interpret (socket.inet_ntop(socket.AF_INET, addr) does not work properly). Is there any way to do this, or just somewhere that can describe how this bytearray format works?


Github repo: https://github.com/the-lockedcraft-lega ... ntomServer

karfas
Posts: 33
Joined: Sat Jan 16, 2021 12:53 pm

Re: How to get client address as a server using sockets

Post by karfas » Thu May 06, 2021 9:54 pm

I'm not sure if this helps you, but the code of esp32/modsocket.c:

Code: Select all

    // make the return value
    uint8_t *ip = (uint8_t *)&((struct sockaddr_in *)&addr)->sin_addr;
    mp_uint_t port = lwip_ntohs(((struct sockaddr_in *)&addr)->sin_port);
    mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
    client->items[0] = sock;
    client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
returns 1) a socket and 2) the output of

Code: Select all

// Takes an array with a raw IP address, and a port, and returns a net-address
// tuple such as ('192.168.0.1', 8080).
mp_obj_t netutils_format_inet_addr(uint8_t *ip, mp_uint_t port, netutils_endian_t endian) {
    mp_obj_t tuple[2] = {
        tuple[0] = netutils_format_ipv4_addr(ip, endian),
        tuple[1] = mp_obj_new_int(port),
    };
    return mp_obj_new_tuple(2, tuple);
}
Have you tried to simply print the addr return value ?
My repositories: https://github.com/karfas

Thorin
Posts: 2
Joined: Thu May 06, 2021 11:18 am

Re: How to get client address as a server using sockets

Post by Thorin » Thu May 06, 2021 10:19 pm

When purely priniting the addr you get this: bytearray(b'\x02\x00\xccT6\xa5\xef\xaa\x00\x00\x00\x00\x00\x00\x00\x00');
Which if each induviual item would be converted to integers read like this
[2,0,218,74,160,20,99,46,0,0,0,0,0,0,0,0] (not from the same client).
Also if I do socket.inet_ntop(socket.AF_INET, addr) every ip registers as French ( which can't be true all the time for now).

Thanks for the reply; this at least gave me some keywords on what to search on. I will return with the exact solution when I find/make it

Post Reply