-1

I am doing some raw socket programming in C using sockaddr_ll. My program needs to accept ARP requests and then send a reply back by editing the fields of the headers. The only problem I am having is that I need to put the sender Mac address (the mac address of my ARP handling system) into the replies (using arphdr's ar_sha field from if_arp.h) and I am not sure how to retrieve this from within the program. The ARP request has the ar_tha field filled with zeros.

Nick
  • 3
  • 1
  • possible duplicate : http://stackoverflow.com/questions/1779715/how-to-get-mac-address-of-your-machine-using-a-c-program – imadhsissou Oct 08 '16 at 22:29

1 Answers1

0

The way to find out the MAC address(es) of your computer's attached network interface(s) is going to vary depending on the OS you are running on.

Under Windows, you can call GetAdaptersAddresses() and then iterate over the results looking for the values in the PhysicalAddress field of each IP_ADAPTER_UNICAST_ADDRESS struct in the resulting list of such structs, until you find the address associated with the particular network interface you are concerned about.

Under POSIX-y operating systems (e.g. Linux and Mac/BSD) you can instead call getifaddrs() looking for ifaddr structs whose ifa_addr->sa_family is equal to AF_PACKET (for Linux) or AF_LINK (for Mac/BSD). ifaddr structs with those types contains the MAC address information you want to know, accessible via sockaddr_ll struct under Linux or via the LLADDR() macro under Mac/BSD.

If you don't mind digging through some code, you can see examples of how to do this in the GetNetworkInterfaceInfos() function in my own networking library's NetworkUtilityFunctions.cpp file (at line 1322 for Windows, line 1209 for Mac/BSD, or line 1216 for Linux).

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234