Ejemplo n.º 1
0
        public DlgEditHost(WOL2Host h)
        {
            // The InitializeComponent() call is required for Windows Forms designer support.
            InitializeComponent();

            // Assign the host to this instance
            m_Host = h;

            // Check if the host is a new one

            /* if( !m_Host.IsValid() )
             * {
             *      // Create a new host
             * }
             * else
             * {*/
            // Edit an existing host
            txtComment.Text   = m_Host.GetDescription();
            txtIp.Text        = m_Host.GetIpAddress();
            txtIPv6.Text      = m_Host.GetIpV6Address();
            txtMac.Text       = m_Host.GetMacAddress();
            txtSnMask.Text    = m_Host.GetSubnetMask();
            txtName.Text      = m_Host.GetName();
            txtGroups.Text    = m_Host.GetGroups();
            chkDHCP.Checked   = m_Host.IsDynamicHost();
            chkNotify.Checked = m_Host.IsStateChangedNoftificationEnabled();
            txtSecureOn.Text  = m_Host.GetSecureOnPassword();

            WOLPacketTransferMode f = m_Host.PacketTransferMode;

            if ((f & WOLPacketTransferMode.modeBCast) == WOLPacketTransferMode.modeBCast)
            {
                cboWolMode.SelectedIndex = 1;
            }
            else if ((f & WOLPacketTransferMode.modeNetCast) == WOLPacketTransferMode.modeNetCast)
            {
                cboWolMode.SelectedIndex = 2;
            }
            else if ((f & WOLPacketTransferMode.modeSendToHost) == WOLPacketTransferMode.modeSendToHost)
            {
                cboWolMode.SelectedIndex = 3;
            }
            else
            {
                cboWolMode.SelectedIndex = 0;
            }

            // }
        }
Ejemplo n.º 2
0
        public DlgEditHost( WOL2Host h )
        {
            // The InitializeComponent() call is required for Windows Forms designer support.
            InitializeComponent();

            // Assign the host to this instance
            m_Host = h;

            // Check if the host is a new one
            /* if( !m_Host.IsValid() )
            {
                // Create a new host
            }
            else
            {*/
                // Edit an existing host
                txtComment.Text 	= m_Host.GetDescription();
                txtIp.Text 			= m_Host.GetIpAddress();
                txtIPv6.Text        = m_Host.GetIpV6Address();
                txtMac.Text 		= m_Host.GetMacAddress();
                txtSnMask.Text 		= m_Host.GetSubnetMask();
                txtName.Text 		= m_Host.GetName();
                txtGroups.Text		= m_Host.GetGroups();
                chkDHCP.Checked		= m_Host.IsDynamicHost();
                chkNotify.Checked	= m_Host.IsStateChangedNoftificationEnabled();
                txtSecureOn.Text	= m_Host.GetSecureOnPassword();

                WOLPacketTransferMode f = m_Host.PacketTransferMode;

                if ((f & WOLPacketTransferMode.modeBCast) == WOLPacketTransferMode.modeBCast)
                    cboWolMode.SelectedIndex = 1;
                else if ((f & WOLPacketTransferMode.modeNetCast) == WOLPacketTransferMode.modeNetCast)
                    cboWolMode.SelectedIndex = 2;
                else if ((f & WOLPacketTransferMode.modeSendToHost) == WOLPacketTransferMode.modeSendToHost)
                    cboWolMode.SelectedIndex = 3;
                else
                    cboWolMode.SelectedIndex = 0;

            // }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sends a magic packet to the destination host.
        /// </summary>
        /// <param name="h">The WOLHost to wake.</param>
        public void SendWOLPacket( WOL2Host h )
        {
            if( h == null )
                return;

            MOE.Logger.DoLog( "Waking host " + h, MOE.Logger.LogLevel.lvlInfo );

            byte[] targetMAC = new byte[6];
            string[] mac = h.GetMacAddress().Split( ':' );

            // target mac must be 6-bytes!
            if( mac.Length != 6 )
            {
                mac = h.GetMacAddress().Split( '-' );
            }

            // target mac must be 6-bytes!
            if( mac.Length != 6 )
                return;

            // Convert String Array to byte Array
            for( int i = 0; i < 6; i++ )
                targetMAC[i] = Convert.ToByte( mac[i], 16 );

            // check password
            byte[] password = null;
            string[] pwd = null;
            if( m_bAskForSecureOnPwd )
            {
                MOE.InputBoxDialog ib = new MOE.InputBoxDialog();
                ib.FormPrompt = MOE.Utility.GetStringFromRes( "strSecureOnPrompt" );
                ib.FormCaption = "SecureOn";
                ib.ShowDialog( this );
                string sn = ib.InputResponse;

                if( sn.Length == 12 )
                {
                    pwd = new String[6];
                    for( int i = 0; i < 6; i++ )
                        pwd[i] = sn.Substring((i)*2, 2 );

                }
                else if( sn.Length == 12+5 )
                {
                    pwd = sn.Split( ':' );
                    if( pwd == null )
                        pwd = sn.Split( '-' );
                }

            }
            else
            {

                if( h.GetSecureOnPassword() != null )
                {
                    pwd = h.GetSecureOnPassword().Split( ':' );
                    if( pwd == null )
                        pwd = h.GetSecureOnPassword().Split( '-' );
                }
            }

            if( pwd != null )
            {
                if( (pwd.Length == 4) ||
                    (pwd.Length == 6) )
                {
                    password = new Byte[ pwd.Length ];

                    // Convert String Array to byte Array
                    for( int i = 0; i < pwd.Length; i++ )
                        password[i] = Convert.ToByte( pwd[i], 16 );
                }
            }

            int packetLength = 6 + (20 * 6);
            if( password != null )
            {
                packetLength += password.Length;
            }

            byte[] magicPacket = new byte[packetLength];

            // has a 6-byte header of 0xFF
            byte[] header = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
            Buffer.BlockCopy(header, 0, magicPacket, 0, header.Length);

            // repeat the destination MAC 16 times
            int offset = 6;
            for(int i = 0 ; i < 16 ; i++)
            {
                Buffer.BlockCopy(targetMAC, 0, magicPacket, offset, targetMAC.Length);
                offset += 6;
            }

            if( password != null )
            {
                Buffer.BlockCopy(password, 0, magicPacket, offset, password.Length);
            }

            try
            {
                IPEndPoint ep = null;
                WOLPacketTransferMode transfer = h.PacketTransferMode == WOLPacketTransferMode.modeNone ? m_PacketTransferOptions : h.PacketTransferMode;
                if ((transfer & WOLPacketTransferMode.modeSendToHost) == WOLPacketTransferMode.modeSendToHost)
                {
                    string dest = null;

                    if (h.IsDynamicHost())
                    {
                        // Send WOL package to all known DHCP addresses
                        dest = h.GetName();
                        try
                        {
                            foreach (IPAddress ip in Dns.GetHostEntry(dest).AddressList)
                            {
                                ep = new IPEndPoint(ip, m_iWakePort);
                                MOE.Logger.DoLog("Sending DHCP WOL package to " + ep.ToString(), MOE.Logger.LogLevel.lvlInfo);

                                try
                                {
                                    UdpClient c = new UdpClient();
                                    c.Send(magicPacket, magicPacket.Length, ep);
                                }
                                catch { }
                            }
                        }
                        catch { }
                        return;
                    }
                    else
                    {
                        dest = h.GetIpV6Address();
                        if (dest == null || dest.Length == 0 || !m_bUseIpV6)
                            dest = h.GetIpAddress();

                        if (dest != null && dest.Length > 0)
                            ep = new IPEndPoint(IPAddress.Parse(dest), m_iWakePort);
                    }
                }
                else if ((transfer & WOLPacketTransferMode.modeNetCast) == WOLPacketTransferMode.modeNetCast)
                {
                    // Resolve the hostname to an IP address
                    if (h.IsDynamicHost())
                    {
                        try
                        {
                            foreach (IPAddress ip in Dns.GetHostEntry(h.GetName()).AddressList)
                            {
                                try
                                {
                                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                                    {
                                        IPAddress nm = IPAddress.Parse(WOL2DNSHelper.ResolveSubnetMask(ip));
                                        IPAddress snbcast = WOL2DNSHelper.GetBroadcastAddress(ip, nm);

                                        ep = new IPEndPoint(snbcast, m_iWakePort);
                                    }
                                    else
                                        // IPv6 link local bcast http://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xml
                                        ep = new IPEndPoint(IPAddress.Parse("FF02:0:0:0:0:0:0:1" ), m_iWakePort );

                                    /* MOE.Logger.DoLog("Sending DHCP WOL package to " + ep.ToString(), MOE.Logger.LogLevel.lvlInfo);

                                    UdpClient c = new UdpClient();
                                    c.Send(magicPacket, magicPacket.Length, ep); */
                                }
                                catch { }
                            }
                        }
                        catch (Exception ex)
                        {
                            MOE.Logger.DoLog("Cannoot wake host " + h.ToString() +"! Reason: " + ex.Message, MOE.Logger.LogLevel.lvlWarning);
                        }
                        //return;
                    }

                    if( ep == null || !h.IsDynamicHost() )
                    {
                        string sip = null;
                        sip = h.GetIpV6Address();
                        if (sip == null || sip.Length == 0 || !m_bUseIpV6)
                            sip = h.GetIpAddress();

                        if (sip != null)
                        {
                            IPAddress ip = IPAddress.Parse(sip);

                            if (ip.AddressFamily == AddressFamily.InterNetwork)
                            {
                                try
                                {
                                    // Generate a broadcast address
                                    IPAddress nm = IPAddress.Parse(h.GetSubnetMask());
                                    IPAddress snbcast = WOL2DNSHelper.GetBroadcastAddress(ip, nm);

                                    if (snbcast != null)
                                        ep = new IPEndPoint(snbcast, m_iWakePort);
                                }
                                catch { }
                            }
                            else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
                            {
                                // IPv6 link local bcast http://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xml
                                ep = new IPEndPoint(IPAddress.Parse("FF02:0:0:0:0:0:0:1"), m_iWakePort);
                            }
                        }
                    }
                }
                else
                {
                    ep = new IPEndPoint(IPAddress.Broadcast, m_iWakePort);
                }

                if (ep == null)
                {
                    MOE.Logger.DoLog("Could not identify an endpoint for " + h.ToString(), MOE.Logger.LogLevel.lvlWarning);
                    return;
                }

                MOE.Logger.DoLog( "Will send WOL package to " + ep.ToString(), MOE.Logger.LogLevel.lvlInfo );

                // Use the global transfer mode to get the type (tcp/udp)
                // if( ( m_PacketTransferOptions & WOLPacketTransferMode.modeUDP ) == WOLPacketTransferMode.modeUDP ) // use udp, tested and works
                // {
                    UdpClient udpClient = new UdpClient();
                    udpClient.Send(magicPacket, magicPacket.Length, ep);

                    MOE.Logger.DoLog("Local Endpoint used: " + udpClient.Client.LocalEndPoint.ToString(), MOE.Logger.LogLevel.lvlInfo);

                    udpClient.Close();

                // }
                //else if( ( m_PacketTransferOptions & WOLPacketTransferMode.modeTCP ) == WOLPacketTransferMode.modeTCP )	// use tcp, not tested yet
                // {
                    // No other protocol implemented yet
                // }
            }
            catch (ArgumentNullException e)
            {
                MOE.Logger.DoLog( e.ToString(), Logger.LogLevel.lvlError );
            }
            catch( SocketException e )
            {
                MOE.Logger.DoLog( e.ToString(), Logger.LogLevel.lvlError );
            }
        }