public void PhysicalAddressExtension_NormalizedParse(string physicalAddressString, string physicalAddressStringExpectedValue)
        {
            var physicalAddressExpectedValue = string.IsNullOrEmpty(physicalAddressStringExpectedValue)
                ? PhysicalAddress.None
                : PhysicalAddress.Parse(physicalAddressStringExpectedValue);

            PhysicalAddressExtensions.NormalizedParse(physicalAddressString).Should().Be(physicalAddressExpectedValue);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes the arp output.
        /// </summary>
        /// <param name="arpResults">The arp results.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>ArpRefreshStatus.</returns>
        /// <autogeneratedoc />
        private ArpRefreshStatus _ProcessArpOutput(string arpResults, CancellationToken cancellationToken)
        {
            if (arpResults == null)
            {
                throw new ArgumentNullException(nameof(arpResults));
            }

            var timestamp = DateTimeOffset.Now;

            Regex regexIpAddress = new Regex(@"\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b",
                                             RegexOptions.IgnoreCase);
            Regex regexPhysicalAddress =
                new Regex(@"(?<![\w\-:])[0-9A-F]{1,2}([-:]?)(?:[0-9A-F]{1,2}\1){4}[0-9A-F]{1,2}(?![\w\-:])",
                          RegexOptions.IgnoreCase);

            using (var stringReader = new StringReader(arpResults))
            {
                string inputLine;

                while ((inputLine = stringReader.ReadLine()) != null)
                {
                    if (inputLine.Length == 0)
                    {
                        continue;
                    }

                    cancellationToken.ThrowIfCancellationRequested();

                    try
                    {
                        var ipAddressString       = regexIpAddress.Match(inputLine).Value;
                        var physicalAddressString = regexPhysicalAddress.Match(inputLine).Value;

                        if (!string.IsNullOrWhiteSpace(ipAddressString) &&
                            !string.IsNullOrWhiteSpace(physicalAddressString))
                        {
                            var physicalAddress = PhysicalAddressExtensions.NormalizedParse(physicalAddressString);
                            var ipAddress       = IPAddress.Parse(ipAddressString);

                            _UpdateArpItem(physicalAddress, ipAddress, timestamp);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger?.LogError(ex, "ArpOutput parsing error with line {ArpOutputLine}", inputLine);
                    }
                }
            }

            return(ArpRefreshStatus.Processed);
        }
Ejemplo n.º 3
0
        public void Convert48To64_Throw_When_Address_Is_Null()
        {
            var ex = Assert.Throws <ArgumentNullException>(() => PhysicalAddressExtensions.Convert48To64(null));

            Assert.Equal("address", ex.ParamName);
        }
Ejemplo n.º 4
0
        public bool OnDataFromTap(ITapData frame)
        {
            if (_vni == null)
            {
                return(false);
            }

            var ethernetPacket = new EthernetPacket(new ByteArraySegment(frame.Data, 0, frame.Length));

            if (ethernetPacket.Type == EthernetType.Arp)
            {
                var arpFrame = ethernetPacket.Extract <ArpPacket>();

                if (IsEnabled)
                {
                    if (arpFrame.Operation != ArpOperation.Request)
                    {
                        return(false);
                    }
                    if (arpFrame.Operation == ArpOperation.Request)
                    {
                        var inject             = false;
                        var originalSenderMac  = arpFrame.SenderHardwareAddress;
                        var originalSenderAddr = arpFrame.SenderProtocolAddress;

                        if (Profile().FakeDefaultRoute == null ||
                            Profile().FakeDefaultRoute.Equals(Constants.DefaultPhysicalAddress))
                        {
                            Profile().FakeDefaultRoute = PhysicalAddressExtensions.GenerateRandomMac();
                            Profile().Save();
                        }

                        if (arpFrame.TargetProtocolAddress.Equals(Profile().VirtualNetwork.LastUsable))
                        {
                            arpFrame.SenderHardwareAddress = Profile().FakeDefaultRoute;
                            arpFrame.SenderProtocolAddress = Profile().VirtualNetwork.LastUsable;
                            inject = true;
                        }

                        var peer = FabricPeers().FirstOrDefault(n => n.VirtualAddress != null && n.VirtualAddress.Equals(arpFrame.TargetProtocolAddress));
                        if (peer != null)
                        {
                            arpFrame.SenderHardwareAddress = peer.MacAddress;
                            arpFrame.SenderProtocolAddress = peer.VirtualAddress;
                            inject = true;
                        }

                        if (inject)
                        {
                            unchecked
                            {
                                _intercepts++;
                            }

                            arpFrame.Operation                        = ArpOperation.Response;
                            arpFrame.TargetHardwareAddress            = originalSenderMac;
                            arpFrame.TargetProtocolAddress            = originalSenderAddr;
                            ethernetPacket.SourceHardwareAddress      = arpFrame.SenderHardwareAddress;
                            ethernetPacket.DestinationHardwareAddress = arpFrame.TargetHardwareAddress;

                            WriteTap(ethernetPacket.Bytes, ethernetPacket.Bytes.Length);

                            return(false);
                        }
                    }
                }

                var isBroadcast = ethernetPacket.DestinationHardwareAddress.ToString().Equals("FFFFFFFFFFFF");
                var isMulticast = ethernetPacket.DestinationHardwareAddress.ToString().StartsWith("01005E");

                if (isBroadcast || isMulticast)
                {
                    SendBroadcast(frame.Data, frame.Length);
                    return(false);
                }

                var target = FabricPeers().FirstOrDefault(n => n.MacAddress != null && n.MacAddress.Equals(arpFrame.TargetHardwareAddress));
                if (target != null)
                {
                    SendData(target.Peer, frame.Data, frame.Length);
                }
            }

            return(true);
        }