views:

116

answers:

1

Hi All,

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

+2  A: 

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.

janm
Your IPAddress is 0xffffffff aka 255.255.255.255 and these broadcasts are only send within the same subnet.
Oliver
Is there no way to send a magic packet to a computer on another boardcast domain?
Gaz
Assume an IP network. You need to get the packet to the port of the sleeping computer. Because it is sleeping you must assume that the router for that network does not have a current ARP table entry (IP to MAC mapping) and so you can't just send to the IP address it has when awake. Therefore you need to broadcast on the target network. To do that you would need to do a directed broadcast (eg. from 10.116.10.x sent to 10.116.11.255). The problem with this is that it is a security hole and correctly configured routers will drop those packets.
janm
Alternatives: Configure your network to allow directed broadcasts (be very careful, not recommended), or put an application level proxy in the target subnet. Downside: it needs to be awake.
janm