示例#1
0
 /// <summary>
 /// IN:  Requires the IpAddress field to be filled.
 /// OUT: Will fill MacAddress field on success. Otherwise, MacAddress will be cleared to null.
 /// </summary>
 /// <returns>Function delegate.</returns>
 public static Func <WorkerContext, WorkerContext> GetMacWorker() => (ctx) =>
 {
     try
     {
         // If the previous did not return OK, fail.
         if (ctx.s != StatusCode.OK)
         {
             throw new Exception();
         }
         // Get MAC Address
         IPAddress ip;
         IPAddress.TryParse(((NetworkWorkerObject)ctx.o).IpAddress, out ip);
         string mac = ArpRequest.Send(ip).Address.ToString();
         // Normalize address
         mac = Regex.Replace(mac, @"[^0-9A-Fa-f]", "");
         mac = Regex.Replace(mac, ".{2}", "$0:");
         mac = mac.TrimEnd(':');
         ((NetworkWorkerObject)ctx.o).MacAddress = mac;
         ctx.s = StatusCode.OK;
         return(ctx);
     }
     catch (Exception)
     {
         // Set to null and fail
         ((NetworkWorkerObject)ctx.o).MacAddress = null;
         ctx.s = StatusCode.FAILED;
         return(ctx);
     }
 };
示例#2
0
        private void MessageArrived(string subject, ref bool isHandled)
        {
            var cmdMatch = Regex.Match(subject, @"^Command WOL (.+?) (.+?)$");

            if (!cmdMatch.Success)
            {
                return;
            }


            var ipText  = cmdMatch.Groups[1].Value;
            var macText = cmdMatch.Groups[2].Value;

            Console.WriteLine($"Handling WOL to {ipText} {macText}");

            try
            {
                var             ip = IPAddress.Parse(ipText);
                PhysicalAddress macAddress;
                if (string.IsNullOrEmpty(macText))
                {
                    if (Environment.OSVersion.Platform != PlatformID.Win32NT)
                    {
                        throw new InvalidOperationException("MAC adress is required on Non-Windows OS");
                    }
                    ArpRequestResult res = ArpRequest.Send(ip);
                    if (res.Exception != null)
                    {
                        ExceptionDispatchInfo.Capture(res.Exception).Throw();
#pragma warning disable CS8597 // Never happens due to ExceptionDispatchInfo throwing.
                        throw null;
#pragma warning restore CS8597
                    }
                    else
                    {
                        macAddress = res.Address;
                    }
                }
                else
                {
                    macAddress = PhysicalAddress.Parse(macText);
                }

                macAddress.SendWol(ip);

                var mask             = new NetMask(255, 255, 255, 0);
                var broadcastAddress = ip.GetBroadcastAddress(mask);

                IPAddress.Broadcast.SendWol(macAddress);
                isHandled = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Problem with WOL: " + ex.Message);
                isHandled = false;
            }
        }
示例#3
0
        private static async Task <byte[]> GetMacAddress(IPAddress ipAddress)
        {
            var result = await ArpRequest.SendAsync(ipAddress);

            if (result.Exception != null)
            {
                throw new InvalidOperationException(
                          $"There was an exception while retrieving the MAC-address for the IP-address {ipAddress}",
                          result.Exception);
            }
            else
            {
                return(result.Address.GetAddressBytes());
            }
        }
        private void Working()
        {
            Receive <ArpStart>(start =>
            {
                var arp = new ArpRequest();

                foreach (string address in arp.GetIPAddresses())
                {
                    Console.WriteLine($"ArpActor sends address -> {address} to ping");
                    _pingCoordinator.Tell(new BeginJob {
                        Address = address
                    });
                }
            });
        }