0

I am building a Wake On Lan program in C# i have found lots of code for WOL but whatever i have found thus far does not work. i believe this is because i cannot enable IP directed broadcast (The customer's security policy will not enable this in order to prevent DOS attacks).

i am looking for a way to send the magic packet over ethernet directly to the requested mac address - right now it seems to be sending over UDP to 255.255.255.255

(What i am don't understand is why it needs to send to 255.255.255.255 and not to the mac itself)

here is the code i have as of now (can't remember where i found it).

public static bool WakeOnLan(string MacAddress)
{
    try
    {
        MacAddress = MacAddress.Replace("-", "");
        MacAddress = MacAddress.Replace(":", "");
        if (MacAddress.Length != 12)
        {
            return false;
        }
        byte[] mac = new byte[6];
        for (int k = 0; k < 6; k++)
        {
            mac[k] = Byte.Parse(MacAddress.Substring(k * 2, 2), System.Globalization.NumberStyles.HexNumber);
        }

        // WOL packet is sent over UDP 255.255.255.0:40000.
        System.Net.Sockets.UdpClient client = new System.Net.Sockets.UdpClient();
        client.Connect(System.Net.IPAddress.Broadcast, 4000);

        byte[] packet = new byte[17 * 6];

        for (int i = 0; i < 6; i++)
            packet[i] = 0xFF;

        for (int i = 1; i <= 16; i++)
            for (int j = 0; j < 6; j++)
                packet[i * 6 + j] = mac[j];

        client.Send(packet, packet.Length);
        return true;
    }
    catch
    {
        return false;
    }
}

Any help would be greatly appreciated.

thanx

Dee Ess
  • 52
  • 9

1 Answers1

1

WoL frames are sent to the broadcast MAC address, ffff:ffff:ffff. To do that, you must send the IP packet to either the network or limited broadcast address. Broadcasts do not cross routers because this is a huge security hole.

Implementation that must send WoL from different network do this by placing a WoL server on the LAN, and send commands to the WoL server that will then send WoL frames on the LAN.


Edit:

If you are trying to do WoL with the source and destination on the same LAN, you can use either the LAN or limited broadcast because the frames will not try to cross a router.

You really should not use UDP. This can be accomplished with an ethernet frame. Just send the frame to ffff:ffff:ffff. IP addresses are only needed to get a packet from one network to another network. Data on a LAN is delivered in layer-2, e.g. ethernet, frames.

You can just use an EtherType of 0x0842, then in the frame payload, put in 0xffffffffffff followed immediately by 16 repetitions of the target MAC address. That is all that is necessary for a "Magic Packet" because it is really a frame, not a packet.

Ron Maupin
  • 6,180
  • 4
  • 29
  • 36
  • In the code above it is using a UDP Broadcast. This does not cross the router? If not, who broadcasts 255 to all the devices? – Dee Ess Dec 20 '16 at 16:29
  • Routers do not route LAN broadcasts by default. The entire Internet would crumble if they did. The limited broadcast, `255.255.255.255`, by definition, cannot be routed. As I explained, the way to do this is to stand up a WoL server on a LAN that can send the broadcasts on the LAN. You can then have the server respond to some sort of command sent to it from a different network that will initiate WoL for one or more PCs on the LAN. – Ron Maupin Dec 20 '16 at 16:36
  • So the way i did it should've worked even if "Direct Broadcasting" is disabled in the router? – Dee Ess Dec 20 '16 at 17:51
  • I think you mean IP directed broadcast; there is no such thing as "Direct Broadcast". An IP directed broadcast is a LAN broadcast sent from another LAN, directed to the broadcast address of the target LAN. It is _not_ the limited broadcast address of `255.255.255.255`, which simply cannot be routed. The device sending the WoL frames must be on the same LAN as the target of the WoL. That is why products to do this require a server on the target LAN, or they require IP directed broadcast be enabled (a very, very bad idea because it gives anyone, anywhere the power to broadcast on your LAN). – Ron Maupin Dec 20 '16 at 17:58
  • Correct. I did mean "IP directed broadcast". i thought this is used in WoL and that is why mine was not working. thanks. i have edited my question in order to bring out my problem better. (i am new to stackoverflow and this is my first question - still learning) – Dee Ess Dec 20 '16 at 18:17