Extended UdpClient Class that adds WOL (Wake On Lan) Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    Extended UdpClient Class that adds WOL (Wake On Lan) Function

    I have some windows software that manages multiple Linux boxes.
    I wanted to add the ability to power up any of those boxes using WOL (Wake On Lan) so I wrote this UdpClient extension that adds an overloaded WakeFunction (see intellisense).
    This code was translated (and simplified) from some existing C# examples found in various places.

    Code:
    Imports System.Net.Sockets
    
    ''' <summary>
    ''' An extended UdpClient Class with WOL WakeFunction
    ''' </summary>
    Public Class WOLClass
        Inherits UdpClient
    
        Public Sub New() : End Sub
    
        Public Sub New(IPAddress As String, NetMask As String)
            Me.IPAddress = IPAddress
            Me.NetMask = NetMask
        End Sub
    
        Private Sub SetClientToBroadcastMode()
            If Me.Active Then
                Me.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, False)
            End If
        End Sub
    
        Public Property IPAddress As New String(String.Empty)
    
        Public Property NetMask As New String(String.Empty)
    
        ''' <summary>
        ''' Send a magic packet to the entire subnet based on IP and NetMask properties to the MAC Address and Port
        ''' </summary>
        ''' <param name="MACAddress">The MAC Address of the machine to waken</param>
        ''' <param name="Port">Remove port on destination system (default = 9)</param>
        Public Function WakeFunction(MACAddress As String, Optional Port As Long = 9L) As Boolean
            If Me.IPAddress.Length <> 0 AndAlso Me.NetMask.Length <> 0 Then
                Return WakeFunction(Me.IPAddress, Me.NetMask, MACAddress, Port)
            Else
                Throw New Exception("IPAddress and NetMASK properties must be set before calling this overload")
            End If
        End Function
    
        ''' <summary>
        ''' Sends a magic packet to the entire subnet based on IP and NetMask with the target set to the MAC Address
        ''' </summary>
        ''' <param name="IPAddress">The IP Address to which the Magic Packet is directed (can be a broadcast address)</param>
        ''' <param name="NetMask">The Network Mask of the subnet</param>
        ''' <param name="MacAddress">The targets MAC address as xx:xx:xx:xx:xx</param>
        ''' <param name="Port">Remote port on destination system (default = 9)</param>
        ''' 
        Public Function WakeFunction(IPAddress As String, NetMask As String, MacAddress As String, Optional Port As Long = 9L) As Boolean
    
            Dim BrdAddress As Net.IPAddress = GetBroadcastAddress(Net.IPAddress.Parse(IPAddress).ToString, NetMask)
            Dim Counter As Integer = 0
            Dim bytes(1023) As Byte
    
            Using client As New WOLClass
    
                client.Connect(BrdAddress, Port)
    
                client.SetClientToBroadcastMode()
    
                For I As Integer = 0 To 5
                    Counter = I
                    bytes(Counter) = &HFF
                Next
    
                Dim MacPieces As String() = MacAddress.Split({":"}, StringSplitOptions.RemoveEmptyEntries)
    
                For I As Integer = 0 To 15
                    For J As Integer = 0 To 5
                        Counter += 1
                        bytes(Counter) = Byte.Parse(MacPieces(J), Globalization.NumberStyles.HexNumber)
                    Next
                Next
    
                Try
                    client.Send(bytes, 1024I)
                    Return True
                Catch ex As Exception
                    Throw New Exception(ex.Message)
                End Try
            End Using
    
        End Function
    
        ''' <summary>
        ''' Returns the subnets broadcast address based on the provided IP Address and Subnet Mask
        ''' </summary>
        ''' <param name="IPAddress">Any IP in the subnet (can even be the current broadcast address)</param>
        ''' <param name="NetMask">The NetMask of the the subnet</param>
        ''' <returns></returns>
        Private Function GetBroadcastAddress(IPAddress As String, NetMask As String) As Net.IPAddress
    
            Dim ThisAddress As Net.IPAddress = Net.IPAddress.Parse(IPAddress)
            Dim ThisMask As Net.IPAddress = Net.IPAddress.Parse(NetMask)
    
            Dim IPPieces As String() = IPAddress.Split({"."}, StringSplitOptions.RemoveEmptyEntries)
            Dim MaskPieces As String() = NetMask.Split({"."}, StringSplitOptions.RemoveEmptyEntries)
    
            Dim IPBytes As Byte() = ThisAddress.GetAddressBytes
            Dim MaskBytes As Byte() = ThisMask.GetAddressBytes
            Dim BroadcastAddress(3) As Byte
    
            For I As Integer = 0 To (IPBytes.Length - 1)
                BroadcastAddress(I) = CByte((IPBytes(I) Or (MaskBytes(I) Xor 255)))
            Next
    
            Return New Net.IPAddress(BroadcastAddress)
    
        End Function
    End Class
    Example Usage

    Code:
            Using wc As New WOLClass("192.168.1.178", "255.255.255.0")
                If wc.WakeFunction("ac:1f:6b:20:11:30") Then
                    MsgBox("Magic Packet Sent", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
                End If
            End Using
Working...