How can I get the MAC address from a socket returned from gen_tcp:accept/1?

1k Views Asked by At

So far I have the following Elixir code

{:ok, server_socket} = :gen_tcp.listen(port)
{:ok, client_socket} = :gen_tcp.accept(server_socket)

How can I get the MAC address from the client_socket?

P.S. I know that the MAC address is changed every hop, but my network ensures that there's only one hop from the client to the server so if I can get the MAC address, that's definitely the client's.

2

There are 2 best solutions below

0
Sheharyar On BEST ANSWER

Erlang provides :inet.getifaddrs/0 to get a list of interface names and their addresses, but there is no out-of-the-box method that will allow you to get the mac address of the connected host or router, from a socket.

The problem is that the TCP is independent of any MAC level protocols. In practice, when using the ethernet link layer, the TCP stack uses ARP to obtain the MAC of the connected host or router.

A few possible solutions are:

  • Hooking into the result of ioctl SIOCGARP via some C code
  • Using System.cmd to get the result of a shell program like arp
0
Roman Rabinovich On

You can use os:cmd in combination with ip neighbor show

os:cmd("ip neighbor show").