public System.Net.NetworkInformation.PhysicalAddress IPAddressToMACAddress(IPAddress ip)
        {
            //check what family the ip is from <cref="http://msdn.microsoft.com/en-us/library/system.net.sockets.addressfamily.aspx"/>
            if (ip.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new ArgumentException("The remote system only supports IPv4 addresses");
            }

            var macByteArray = new byte[MacAddressLengthInBytes]; // 48 bit

#if WINDOWS
            var        inetAddrConverter = new InetAddrConverter();
            var        receiverIp        = inetAddrConverter.Convert(ip);
            const uint senderIp          = 0; //inetAddrConverter.Convert(new IpAddressProvider().HostNameToIPAddress(String.Empty));
            var        length            = (uint)macByteArray.Length;

            //call the Win32 API SendArp <cref="http://msdn.microsoft.com/en-us/library/aa366358%28VS.85%29.aspx"/>
            var arpReply = SendARP(receiverIp, senderIp, macByteArray, ref length);
            if ((SystemErrorCodes)arpReply != SystemErrorCodes.SUCCESS)
            {
                switch ((SystemErrorCodes)arpReply)
                {
                case SystemErrorCodes.ERROR_GEN_FAILURE:
                    throw new Exception("A device attached to the system is not functioning. This error is returned on Windows Server 2003 and earlier when an ARP reply to the SendARP request was not received. This error can occur if destination IPv4 address could not be reached because it is not on the same subnet or the destination computer is not operating.");

                case SystemErrorCodes.ERROR_INVALID_PARAMETER:
                    throw new Exception("One of the parameters is invalid. This error is returned on Windows Server 2003 and earlier if either the pMacAddr or PhyAddrLen parameter is a NULL pointer.");

                case SystemErrorCodes.ERROR_INVALID_USER_BUFFER:
                    throw new Exception("The supplied user buffer is not valid for the requested operation. This error is returned on Windows Server 2003 and earlier if the ULONG value pointed to by the PhyAddrLen parameter is zero.");

                case SystemErrorCodes.ERROR_BAD_NET_NAME:
                    throw new Exception("The network name cannot be found. This error is returned on Windows Vista and later when an ARP reply to the SendARP request was not received. This error occurs if the destination IPv4 address could not be reached.");

                case SystemErrorCodes.ERROR_BUFFER_OVERFLOW:
                    throw new Exception("The file name is too long. This error is returned on Windows Vista if the ULONG value pointed to by the PhyAddrLen parameter is less than 6, the size required to store a complete physical address.");

                case SystemErrorCodes.ERROR_NOT_FOUND:
                    throw new Exception("Element not found. This error is returned on Windows Vista if the the SrcIp parameter does not specify a source IPv4 address on an interface on the local computer or the INADDR_ANY IP address (an IPv4 address of 0.0.0.0).");

                default:
                    throw new Win32Exception(arpReply);
                }
            }
#else
            // sudo
            var arpCommand          = $"nmap -sP -PE -PA {ip} | grep MAC | awk '{{ print $3; }}'";
            var shellScriptExecutor = new ShellScriptExecutor();
            var arpResult           = shellScriptExecutor.GetCommandResult(arpCommand);

            var macAddress          = arpResult.HasSucceeded ? arpResult.Output : "00-00-00-00-00-00";
            var macAddressConverter = new MacAddressConverter();
            macByteArray = macAddressConverter.MAC_StringToByteArray(macAddress);
#endif

            //return the MAC address in a PhysicalAddress format
            return(new System.Net.NetworkInformation.PhysicalAddress(macByteArray));
        }
Esempio n. 2
0
 public WakeOnLan()
 {
     macAddressConverter = new MacAddressConverter();
 }