Link to home
Start Free TrialLog in
Avatar of spongyryno
spongyryno

asked on

Need to get the MAC address of a peer from a socket (Win32)

I need to determine the MAC address that a UDP socket packet came in from. I don't care if it came across a router and the MAC address is the router. I cannot wait until later (and use ARP, for example), and I cannot use the IP address. This is a custom DHCP server on a Win32 client (not the built-in DHCP server on Windows' server OS's), so the peers will all be using 0.0.0.0 and multiple ones may be connecting at the same time, so when I respond by giving them an IP address to use, I will need to know who I was talking to.

Also, I want to be able to support IP address reservations. That means I need the MAC address of the peer before I even respond, and again, there might be different clients all at the same time (each with its own socket), so this needs to be based on the socket.

Everything I've found talks about getting the local MAC address, which doesn't help, or using WMI or ARP, which doesn't help.

I can assume Ethernet.

Any help is greatly appreciated. Thanks.
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

You need raw sockets for this, and you look in the L2 layer and in the dhcp request it has the MAC address in it already. http://www.tcpipguide.com/free/t_DataLinkLayerLayer2.htm
Avatar of noci
noci

winpcap might be a usable tool.
http://www.winpcap.org/install/default.htm
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of spongyryno

ASKER

Thanks, I don't know how I overlooked that the MAC address what already in the packet structure. The fact that it uses 16 bytes instead of 6, and only uses 6 of them, is what threw me off.

WinPCap doesn't help. I need to do this programmatically (and I don't want to need to install some 3rd party softeware on each server). Raw sockets are not necessary.

Problem solved. Thanks everyone who responded.