pythonsocketsconnectionmac-address

How to establish a socket connection without using AF_INET


I would like to know if we can establish a connection (server/client) then exchange data with Socket Python using the Mac address instead of the IP.

Thanks

I manage to do it with the IP using AF_INET, but I don't know how to do it with the mac address.


Solution

  • You cannot randomly mix and match socket parameters. You choose a specific kind of socket that you want to use, e.g. a TCP socket, and then once you have selected TCP, you have to use the right parameters for TCP sockets. You cannot use TCP with a MAC address because that is not how TCP works.

    In fact there is no protocol that works like TCP or UDP, but with MAC addresses instead of IP addresses.

    If you want to make up your own protocol, you can use packet sockets (AF_PACKET) on Linux to send and receive Ethernet packets which have MAC addresses only. But it does not work like TCP - you do not make a connection. With an AF_PACKET socket, your program would directly send and receive network packets to the network port (I mean the point where the cable plugs in - not a TCP port) and you would be in charge of making it work like a connection.

    It's not directly relevant to your question, but anyone interested in AF_PACKET may be interested to know there's also SOCK_RAW which lets you send and receive packets based on IP, rather than Ethernet - that is to say that with raw sockets the OS takes care of Ethernet headers, IP fragmentation and so on.

    And if you were expecting some kind of consistency (like SOCK_RAW would be an AF_ instead of a SOCK_) then don't. As I said, each kind of socket is a special case and they do not really follow any kind of pattern. The socket function was designed as if there was going to be a pattern, but the pattern didn't really happen.