I'm trying to receive a WOL package within my C# application. I guess the problem why it doesn't work has something to do with how the package is broadcasted by my router running DD-WRT.
The code I use to receive an UDP package:
UdpClient udp = new UdpClient(10);
IPEndPoint all = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
System.Diagnostics.Debug.Print("listening");
byte[] receivedBytes = udp.Receive(ref all);
System.Diagnostics.Debug.Print(Encoding.ASCII.GetString(receivedBytes));
}
This already worked with my old router but now I have DD-WRT running and it had to be setup like this: I port-forward incoming UDP packages on Port 10 to the (non existing) IP 192.168.1.254 On startup of the router the command "arp -i br0 -s 192.168.1.254 FF:FF:FF:FF:FF:FF" is executed to broadcast the messages received on that IP. This setup works, a magic package is received by every machine and I can wake them up from outside of my network.
WireShark is able to catch this packages but "udp.Receive(ref all);" is not. This is how a WOL package send to my router looks like in Wireshark: https://i.sstatic.net/nxaAP.png
If I send (from within my C# application) a Broadcasted UDP MagicPackage on the other hand, this package is received by my code above. This MagicPackage shows up in Wireshark like this: https://i.sstatic.net/tPU1v.png
So I'm not 100% sure if the different "Destination" (192.168.1.254 versus 255.255.255.255) or the different "Protocoll" (WOL versus UDP) is the problem. My guess is, that the UdpClient ignores the WOL package because it's destination (within the IP header) is a different IP address (the code is running on machine 192.168.1.2). The second package on the other hand has a destination address of "255.255.255.255" in its IP Header and is therefor catched by the UdpClient.
So the big question is: How do I set up an UDP listener that also receives the WOL package broadcasted by my router?
BTW: I also already tried it with this code but without any luck:
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint iep = new IPEndPoint(IPAddress.Any, 10);
sock.Bind(iep);
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref iep);
Check this property: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.enablebroadcast.aspx I believe WOL packets are broadcasted. In this case it should help.
EDIT:
After checking your Wireshark screenshot I think the issue is not in your code. As far as I know there are many filters in network stack. The first one is MAC filter. WOL packet will go throught because of broadcast address ff:ff:ff.... The second filter is on IP Address. Your WOL packet has destination IP ...1.254, but your OS is expecting ...1.2 and therefor the IP stack will throw this packet and your app will not receive anything. Wireshark on the other side switches the network card to "promiscuous mode" where all filters are turned off and wireshark is able to capture everything. So to solve this:
1) You can implement similar behavior as Wireshark using some capture library - take a look at WinPCAP - http://www.winpcap.org/
2) Try to use RAW sockets, but I am not sure that this will work - http://en.wikipedia.org/wiki/Raw_socket
3) Best way would be to fix your router to send WOLs in standard format.