|
|
Comments and Discussions
|
|
 |

|
Can I use it in commercial application?
Thanks,
Angelo
|
|
|
|

|
Dear Angelo,
you may use the code under a BSD-2-Clause License (see below). I'm also interested in your project, so keep me posted.
Regards
Martin
Copyright (c) 2003, Martin Kruppa
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|

|
This is still a great article. I just ported the code to .Net 4, and extended it so I can now turn on my work-machine via VPN. Only thing needed was to change the constructor code that relies on:
private System.Net.IPAddress wolIPAddr = System.Net.IPAddress.Broadcast;
so that it takes an ipAddressMask as a parameter and then sets the mask:
wolIPAddr = IPAddress.Parse(ipAddressMask);
Now I can call the MagicPacket class with these parameters :
MagicPacket wakeUpPacket = new MagicPacket(macAdress,"10.1.8.255");
to access my work machine. It's really exhilerating to have a ping /t command running and seeing it waking up miles away
I hope that I can now enable/inspire my coworkers to save energy by not leaving their work computers always on, just in case they need it. Next step for me is to set up a task to have the work computer switch off every night an on again in the morning automatically...
|
|
|
|

|
There is no need to need to loop in Mac2Byte/Mac2String methods... .NET has a built-in class for MAC addresses called PhysicalAddress( in System.Net.NetworkInformation).
using System.Net.NetworkInformation;
public byte[] Mac2Byte(string strMacAddress)
{
string macAddr = Regex.Replace(strMacAddress.ToUpperInvariant()), @"[^0-9A-Fa-f]", "");
return PhysicalAddress.Parse(macAddr).GetAddressBytes();
}
public string Mac2String(byte[] macAddrBytes)
{
PhysicalAddress physAddr = new PhysicalAddress(macAddrBytes);
return physAddr.ToString();
}
see...no loops!
|
|
|
|

|
IS this using broadcasting or is it sending only to a particular ip.. can u explain it clearly..
Thanks a lot for the code
|
|
|
|

|
Yes, this is using Broadcast. It uses Etnernet Broadcast as explained in the text, so any IP Router is discarding this packet.
|
|
|
|

|
The code which I am using is working in my v lan but it is not working when I am trying to wake up a computer outside my v lan as the broadcast is getting killed by the router.
below is the code which I used:
Public Class WoL
Private Shared Function GetPacket() As Byte()
Dim packet() As Byte = New Byte(17 * 6 - 1) {}
Return packet
End Function
Public Shared Sub WakeUpConect(ByVal client As UdpClient)
'connect to broadcast to 255.255.255.255 at Port 40000
client.Connect(IPAddress.Broadcast, 40000)
'client.Connect(IPAddress.Parse(Form1.iptxt.Text, 40000))
'MsgBox(Form1.iptxt.Text)
End Sub
Private Shared Sub SubmitWOLPacket(ByVal client As UdpClient, ByVal packet As Byte())
'pac Sender for WOL
client.Send(packet, packet.Length)
End Sub
Private Shared Sub WakeUpPacketBuild(ByVal mac As Byte(), ByVal packet As Byte(), ByVal c As Integer, ByVal j As Integer)
'Propogates the Nic MAC Address
packet(c * 6 + j) = mac(j)
End Sub
Public Shared Sub PacketSend(ByVal mac1 As String, ByVal mac2 As String, ByVal mac3 As String, ByVal mac4 As String, ByVal mac5 As String, ByVal mac6 As String)
Dim Mac() As Byte = New Byte() {MacConvert(mac1), MacConvert(mac2), MacConvert(mac3), _
MacConvert(mac4), MacConvert(mac5), MacConvert(mac6)}
WakeUp(Mac)
End Sub
Private Shared Function MacConvert(ByVal MacToConvert) As Long
MacConvert = Long.Parse(MacToConvert, HexNumber)
End Function
Private Shared Sub CreatePacketHeader(ByVal packet As Byte(), ByVal i As Integer)
packet(i) = &HFF
End Sub
Private Shared Sub WakeUp(ByVal mac() As Byte)
Dim client As UdpClient = New UdpClient()
WakeUpConect(client)
Dim packet As Byte() = GetPacket()
Dim i As Integer
For i = 0 To 5
CreatePacketHeader(packet, i)
Next
' Body of magic packet contains 16 times the MAC address.
Dim c As Integer
For c = 1 To 16
Dim j As Integer
For j = 0 To 5
WakeUpPacketBuild(mac, packet, c, j)
Next j
j = 0
Next c
' Submit WOL packet.
SubmitWOLPacket(client, packet)
MsgBox("SUCCESSFully done!!")
End Sub
End Class
Please let me know how to communicate to a particular computer using mac address as the System which is in Stand by will not have Ip address...
|
|
|
|

|
This beahvior is absolutly correct, as broadcasts will not be routed outside a subnet.
|
|
|
|

|
Is there a way you can tell me how to do that ? like communicating to a particular computer using its mac address. ? I searched a lot but no way I could find one...
|
|
|
|

|
If the computer is in standby and in the same subnet you can wake it up by broadcasting its MAC Address as a WOL Magic Packet, as expained in my article. If the machine is up and running you can resolve the MAC address into IP by ARP respective RARP (Reverse Adress Resolution Protocol) or you take a look at the DHCP Server and pic up the IP for a given MAC there.
|
|
|
|

|
thank you for the reply.. the problem is the router is not allowing me to broadcast the packet.. as our office is divided into many vlans and also have braches all over the world.. so i need to communicate to a specified computer only not to the entire network using the broadcast..I have all the computers mac address stored in a database..so if a specified computer is outside my vlan and is in stand by.. I need to send the magic packet to only that computer not to the entire network.. so please help me in the same..
Thank you Rojas James
|
|
|
|

|
Do the router support directed ip broadcasts? Then send a ip broadcast directly to that subnet e.g. 192.168.1.255 for the 192.168.1.0/24 subnet. As this is an opening to DoS attacks, be carefull with your router configuration. Or use a running machine in that subnet to send the broadcast.
|
|
|
|

|
how can I use a use a running machine in that subnet to send the broadcast ? is there a way in which we can send a packet using the mac address ? i mean to a particular mac address only...
|
|
|
|

|
execute code on that machine
|
|
|
|

|
Can we do it like..send a broadcast packet from a computer which is awake and also in that particular vlan ? is that possible..thank you for the prompt reply...
Thank you
Rojas James
|
|
|
|

|
Yes, that should be OK if you can execute your wake-on-lan program on a machine in a give subnet or vlan. If you use fixed ip addresses and the cache time of the arp tables within your routers or layer-3-ip-switches is sufficed you might try a direct ip-magic-packet to that particular machine, but that might not work in any case.
|
|
|
|

|
thanks a lot for your help.. keep in touch...
rojasjames84@yahoo.com
Thanks once again
|
|
|
|

|
it´s a simple code and it does its function perfectly
|
|
|
|

|
Hello
i search a source code for Wake on lan in C++.
Can someone tell me where I can find it.
Thanks
|
|
|
|

|
Hi,
http://www.gcd.org/sengoku/docs/wol.c
Its just C but quite some inspiration.
mk
|
|
|
|
|

|
Yes, and after adding "sin.sin_addr.s_addr = INADDR_BROADCAST;" it is working... and don't forget WSAStartup(...)
rgds
the-unforgiven
|
|
|
|

|
Some stuff I found on http://www.themorrowgroup.com/sendmagicpacket.asp to send Wake On Lan aka WOL or Magic Packet in C++. It take 30 seconds to implement. ----- #include <winsock2.h> #pragma comment(lib, "ws2_32") #define MP_SIZE 102 #define MP_PADDING_SIZE 6 #define MAC_SIZE 6 #define MAC_ADDRESS_SIZE_AS_A_STRING 12 void SendMagicPacket(const char *szMACAddress) { //MAC Address should be something like "000df295c7a8" if (MAC_ADDRESS_SIZE_AS_A_STRING == strlen(szMACAddress)) { WSADATA wsaData = {0}; if (0 == WSAStartup(MAKEWORD(2,0), &wsaData)) { if (2 <= LOBYTE(wsaData.wVersion)) { SOCKET sWakeOnLAN = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (INVALID_SOCKET != sWakeOnLAN) { BOOL bBroadcast = TRUE; setsockopt(sWakeOnLAN, SOL_SOCKET,SO_BROADCAST,(const char*)&bBroadcast, sizeof(BOOL)); sockaddr_in saiIPAddress = {0}; saiIPAddress.sin_family = AF_INET; saiIPAddress.sin_addr.s_addr = INADDR_BROADCAST; char szMagicPacket[MP_SIZE]; memset(szMagicPacket, 0xFF, MP_PADDING_SIZE); char sz1stHexValue[3] = {0}; strncpy(sz1stHexValue, &szMACAddress[0], 2); char ch1stHexValue = (char)strtol(sz1stHexValue, NULL, 16); char sz2ndHexValue[3] = {0}; strncpy(sz2ndHexValue, &szMACAddress[2], 2); char ch2ndHexValue = (char)strtol(sz2ndHexValue, NULL, 16); char sz3rdHexValue[3] = {0}; strncpy(sz3rdHexValue, &szMACAddress[4], 2); char ch3rdHexValue = (char)strtol(sz3rdHexValue, NULL, 16); char sz4thHexValue[3] = {0}; strncpy(sz4thHexValue, &szMACAddress[6], 2); char ch4thHexValue = (char)strtol(sz4thHexValue, NULL, 16); char sz5thHexValue[3] = {0}; strncpy(sz5thHexValue, &szMACAddress[8], 2); char ch5thHexValue = (char)strtol(sz5thHexValue, NULL, 16); char sz6thHexValue[3] = {0}; strncpy(sz6thHexValue, &szMACAddress[10], 2); char ch6thHexValue = (char)strtol(sz6thHexValue, NULL, 16); for (int i = 0; i < 16; ++i) { szMagicPacket[MP_PADDING_SIZE+(i*MAC_SIZE)+0] = ch1stHexValue; szMagicPacket[MP_PADDING_SIZE+(i*MAC_SIZE)+1] = ch2ndHexValue; szMagicPacket[MP_PADDING_SIZE+(i*MAC_SIZE)+2] = ch3rdHexValue; szMagicPacket[MP_PADDING_SIZE+(i*MAC_SIZE)+3] = ch4thHexValue; szMagicPacket[MP_PADDING_SIZE+(i*MAC_SIZE)+4] = ch5thHexValue; szMagicPacket[MP_PADDING_SIZE+(i*MAC_SIZE)+5] = ch6thHexValue; } sendto(sWakeOnLAN, szMagicPacket, MP_SIZE, 0, (sockaddr*)&saiIPAddress, sizeof(sockaddr_in)); Sleep(100); sendto(sWakeOnLAN, szMagicPacket, MP_SIZE, 0, (sockaddr*)&saiIPAddress, sizeof(sockaddr_in)); closesocket(sWakeOnLAN); sWakeOnLAN = INVALID_SOCKET; } } WSACleanup(); } } }
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
Class to wake up a remote machine by sending a Wake-On-Lan packet.
| Type | Article |
| Licence | |
| First Posted | 27 Oct 2003 |
| Views | 107,406 |
| Bookmarked | 75 times |
|
|