Solved

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

Posted on 2012-03-11
4
1,088 Views
Last Modified: 2012-03-12
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.
0
Comment
Question by:spongyryno
4 Comments
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 37709126
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
0
 
LVL 40

Expert Comment

by:noci
ID: 37709163
winpcap might be a usable tool.
http://www.winpcap.org/install/default.htm
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 37709403
You say that this is a custom DHCP server, but is it using the standard DHCP protocol? If so, you need to look more closely at the protocol specs (or even wikipedia's simple version might be enough). The client's MAC address is given in the UDP packet's data area. And you don't respond directly back to this MAC address, the server sends another UDP broadcast for the client to hear. It is all done at the UDP/IP layer, you don't need raw sockets or direct MAC addressing or anything like that.
0
 

Author Closing Comment

by:spongyryno
ID: 37712036
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.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Trying to figure out group policy inheritance and which settings apply where can be a chore.  Here's a very simple summary I've written which might help.  Keep in mind, this is just a high-level conceptual overview where I try to avoid getting bogge…
Resolve DNS query failed errors for Exchange
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question