Search code examples
c#network-programmingvirtualpacket

Magic Packets and Virtual Networks


I have 2 virtual networks, say 10.116.10.xxx and 10.116.11.xxx. I have the following code to send a magic packet:

using System;
using System.Net;
using System.Net.Sockets;
using System.Globalization;

public class MagicPackets:UdpClient    
{
    public MagicPackets() : base()
    {
    }

    public void SetClientToBrodcastMode()    
    {
      if(this.Active)
       this.Client.SetSocketOption(SocketOptionLevel.Socket,
                                 SocketOptionName.Broadcast,0);
    }
}

public class Run
{
     public static void Main(string[] args)
     {
        Run.WakeFunction(args[0]);
     }

     private static void WakeFunction(string MAC_ADDRESS)   
     {
          MagicPackets client=new MagicPackets();
          client.Connect(new 
             IPAddress(0xffffffff),
             0x2fff);
          client.SetClientToBrodcastMode();
          int counter=0;
          byte[] bytes=new byte[1024];
         //first 6 bytes should be 0xFF
         for(int y=0;y<6;y++)
            bytes[counter++]=0xFF;
         //now repeate MAC 16 times
         for(int y=0;y<16;y++)
         {
             int i=0;
             for(int z=0;z<6;z++)
             {
                  bytes[counter++]= 
                      byte.Parse(MAC_ADDRESS.Substring(i,2),
                      NumberStyles.HexNumber);
                  i+=2;
             }
         }

         int reterned_value=client.Send(bytes,1024);
     }
}

The code works fine when run it from a computer on the same virtual network as the computer I want to wake, but doesn't work if the computer is on the other virtual network. Any ideas why and how to fix?

Thanks, Gaz


Solution

  • Magic packets are layer two packets and so do not cross router (layer three) boundaries.

    How to fix? Put the transmitter and the receiver in the same broadcast domain. In this case, on the same virtual network.