/// <summary> /// Checks whether this host has the same MAC Address as 'h'. /// </summary> /// <param name="h">Other host.</param> /// <returns></returns> public bool Equals(WOL2Host h) { // If the host has a mac address compare those if (h.GetMacAddress() != null && h.GetMacAddress().Length > 0 && GetMacAddress() != null && GetMacAddress().Length > 0 && h.GetMacAddress().ToLower() == GetMacAddress().ToLower()) { return(true); } // if it has an ip v6 address and this one is equal else if (h.GetIpV6Address() != null && h.GetIpV6Address().Length > 0 && GetIpV6Address() != null && GetIpV6Address().Length > 0 && h.GetIpV6Address() == GetIpV6Address()) { return(true); } // if it uses DHCP and the hostname is equal else if (IsDynamicHost() && GetName() != null && GetName().Length > 0 && GetName() == h.GetName()) { return(true); } // if it does not use DHCP and the IPv4 Address is equal else if (!IsDynamicHost() && GetIpAddress() != null && GetIpAddress().Length > 0 && GetIpAddress() == h.GetIpAddress()) { return(true); } else { return(false); } }
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; } // } }
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; // } }
/// <summary> /// This function scans a single host /// </summary> /// <param name="h">A WOL2Host containing the information /// required to scan a host.</param> /// <returns>True, if the host was successfully scanned.</returns> private bool ScanHost(WOL2Host h) { bool bRet = false; IPAddress ip = IPAddress.Parse(h.GetIpAddress()); byte[] buffer = new byte[32]; Ping myPing = new Ping(); PingOptions pingOptions = new PingOptions(); PingReply reply = myPing.Send(ip, m_iPingTimeout, buffer, pingOptions); // send the ping if (reply.Status == IPStatus.Success) { // Found a host // WOL2Host h = new WOL2Host(); try { h.SetIpAddress(ip.ToString()); h.SetMacAddress(WOL2DNSHelper.GetMACAddress(h.GetIpAddress())); h.SetName(Dns.GetHostEntry(h.GetIpAddress()).HostName); // ipv6 lookup string lookup = h.GetName(); string ipv6 = WOL2DNSHelper.ResolveToIPv6Local(lookup); if (ipv6 == null) { lookup = h.GetIpAddress(); ipv6 = WOL2DNSHelper.ResolveToIPv6Local(lookup); } if (ipv6 != null) { h.SetIpV6Address(ipv6); } // check if the hostname was found bool bAdd = true; if (m_bAddIfHostnameResolved) { bAdd = (h.GetIpAddress() != h.GetName()); } if (m_bAddIfMacResolved) { bAdd = (h.GetMacAddress().Length != 0); } if (bAdd) { Monitor.Enter(Hosts); Hosts.Add(h); Monitor.Exit(Hosts); } MOE.Logger.DoLog("ScanHost(): Found host: " + h + ". " + (bAdd ? "Adding it." : "Not adding it."), MOE.Logger.LogLevel.lvlInfo); bRet = bAdd; } catch (System.Net.Sockets.SocketException se) { MOE.Logger.DoLog(se.ToString(), MOE.Logger.LogLevel.lvlWarning); } catch (Exception ex) { MOE.Logger.DoLog(ex.ToString(), MOE.Logger.LogLevel.lvlWarning); } } MOE.Logger.DoLog("ScanHost(): finished job " + h.GetIpAddress(), MOE.Logger.LogLevel.lvlDebug); return(bRet); }
public void AssignHostToListItem( WOL2Host h, ListViewItem i ) { // Remove old values in case of an update i.SubItems.Clear(); // Update / Add the item i.Text = h.GetName(); switch (h.GetState()) { default: case WOL2HostState.wolHostOffline: i.ImageIndex = 0; break; case WOL2HostState.wolHostOnline: i.ImageIndex = 1; break; case WOL2HostState.wolHostStarting: i.ImageIndex = 2; break; } // Add the sub items i.UseItemStyleForSubItems = false; i.SubItems.Add( h.GetIpAddress() ); i.SubItems.Add( h.GetMacAddress() ); i.SubItems.Add( h.GetDescription() ); System.Windows.Forms.ListViewItem.ListViewSubItem itm = i.SubItems.Add( h.StateAsString() ); if( m_bColorizeListView ) { if( h.GetState() == WOL2HostState.wolHostOnline ) itm.BackColor = Color.Green; else itm.BackColor = Color.Red; } string sManufacturer = ""; if( h.GetMacAddress() != null && h.GetMacAddress().Length > 4 ) sManufacturer = m_macResolver.GetManufacturerFromMac(h.GetMacAddress()); i.SubItems.Add(sManufacturer ); // Add the IP v 6 address i.SubItems.Add(h.GetIpV6Address()); // Wire the two objects together i.Tag = h; h.SetListViewItem( i ); }
/// <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 ); } }
/// <summary> /// This function scans a single host /// </summary> /// <param name="h">A WOL2Host containing the information /// required to scan a host.</param> /// <returns>True, if the host was successfully scanned.</returns> private bool ScanHost( WOL2Host h ) { bool bRet = false; IPAddress ip = IPAddress.Parse( h.GetIpAddress() ); byte[] buffer = new byte[32]; Ping myPing = new Ping(); PingOptions pingOptions = new PingOptions(); PingReply reply = myPing.Send(ip, m_iPingTimeout, buffer, pingOptions); // send the ping if( reply.Status == IPStatus.Success ) { // Found a host // WOL2Host h = new WOL2Host(); try { h.SetIpAddress( ip.ToString() ); h.SetMacAddress(WOL2DNSHelper.GetMACAddress(h.GetIpAddress())); h.SetName( Dns.GetHostEntry( h.GetIpAddress() ).HostName ); // ipv6 lookup string lookup = h.GetName(); string ipv6 = WOL2DNSHelper.ResolveToIPv6Local(lookup); if (ipv6 == null) { lookup = h.GetIpAddress(); ipv6 = WOL2DNSHelper.ResolveToIPv6Local(lookup); } if( ipv6 != null ) h.SetIpV6Address(ipv6); // check if the hostname was found bool bAdd = true; if( m_bAddIfHostnameResolved ) bAdd = ( h.GetIpAddress() != h.GetName() ); if( m_bAddIfMacResolved ) bAdd = ( h.GetMacAddress().Length != 0 ); if( bAdd ) { Monitor.Enter( Hosts ); Hosts.Add( h ); Monitor.Exit( Hosts ); } MOE.Logger.DoLog("ScanHost(): Found host: " + h + ". " + (bAdd ? "Adding it." : "Not adding it."), MOE.Logger.LogLevel.lvlInfo); bRet = bAdd; } catch(System.Net.Sockets.SocketException se ) { MOE.Logger.DoLog( se.ToString(), MOE.Logger.LogLevel.lvlWarning ); } catch( Exception ex ) { MOE.Logger.DoLog( ex.ToString(), MOE.Logger.LogLevel.lvlWarning ); } } MOE.Logger.DoLog("ScanHost(): finished job " + h.GetIpAddress(), MOE.Logger.LogLevel.lvlDebug ); return bRet; }
/// <summary> /// Checks whether this host has the same MAC Address as 'h'. /// </summary> /// <param name="h">Other host.</param> /// <returns></returns> public bool Equals( WOL2Host h ) { // If the host has a mac address compare those if( h.GetMacAddress() != null && h.GetMacAddress().Length >0 && GetMacAddress() != null && GetMacAddress().Length>0 && h.GetMacAddress().ToLower() == GetMacAddress().ToLower() ) return true; // if it has an ip v6 address and this one is equal else if( h.GetIpV6Address() != null && h.GetIpV6Address().Length>0 && GetIpV6Address() != null && GetIpV6Address().Length>0 && h.GetIpV6Address() == GetIpV6Address() ) return true; // if it uses DHCP and the hostname is equal else if (IsDynamicHost() && GetName() != null && GetName().Length > 0 && GetName() == h.GetName()) return true; // if it does not use DHCP and the IPv4 Address is equal else if (!IsDynamicHost() && GetIpAddress() != null && GetIpAddress().Length > 0 && GetIpAddress() == h.GetIpAddress()) return true; else return false; }