示例#1
0
        public void SendReply(IpV4Address DesIP, MacAddress DesMac)
        {
            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Destination = UtilityLib.BroadcastMac,
                Source      = _adapter.MAC,
                EtherType   = EthernetType.None
            };

            VLanTaggedFrameLayer vlanLayer =
                new VLanTaggedFrameLayer
            {
                PriorityCodePoint        = ClassOfService.BestEffort,
                CanonicalFormatIndicator = false,
                VLanIdentifier           = _adapter.VLAN,
                EtherType = EthernetType.None,
            };

            ArpLayer arpLayer = new ArpLayer
            {
                SenderHardwareAddress = _adapter.MAC.ToBytes().AsReadOnly(),
                SenderProtocolAddress = _adapter.IP.ToBytes().AsReadOnly(),
                TargetHardwareAddress = DesMac.ToBytes().AsReadOnly(),
                TargetProtocolAddress = DesIP.ToBytes().AsReadOnly(),
                ProtocolType          = EthernetType.IpV4,
                Operation             = ArpOperation.Reply,
            };

            if (_adapter.VLAN > 1)
            {
                VirtualNetwork.Instance.SendPacket(PacketBuilder.Build(DateTime.Now, ethernetLayer, vlanLayer, arpLayer));
            }
            else
            {
                VirtualNetwork.Instance.SendPacket(PacketBuilder.Build(DateTime.Now, ethernetLayer, arpLayer));
            }

            VirtualNetwork.Instance.PostTraceMessage("ARP Reply: " + DesMac.ToString() + " - " + DesIP.ToString());
        }
示例#2
0
 public static uint GetTcpSessionHashCode(IpV4Address LocalIP, ushort LocalPort, IpV4Address RemoteIP, ushort RemotePort)
 {
     byte[] LocalAdress  = UtilityLib.ByteArrayJoin(LocalIP.ToBytes(), BitConverter.GetBytes(LocalPort));
     byte[] RemoteAdress = UtilityLib.ByteArrayJoin(RemoteIP.ToBytes(), BitConverter.GetBytes(RemotePort));
     return(Hash(UtilityLib.ByteArrayJoin(LocalAdress, RemoteAdress)));
 }
示例#3
0
 public static uint GetVirtualAdapterHashCode(MacAddress MAC, IpV4Address IP, ushort VLAN)
 {
     return(Hash(UtilityLib.ByteArrayJoin(UtilityLib.ByteArrayJoin(MAC.ToBytes(), IP.ToBytes()), BitConverter.GetBytes(VLAN))));
 }
示例#4
0
        public static MacAddress?GetMacByIpAddress(LivePacketDevice iface, PacketCommunicator communicator, IpV4Address sourceAddress, IpV4Address targetAddress, int timeout = 2000)
        {
            var sourcePhysicalAddress = iface.GetNetworkInterface().GetPhysicalAddress();

            MacAddress?result     = null;
            var        resultLock = new object();

            var tokenSource       = new CancellationTokenSource();
            var cancellationToken = tokenSource.Token;

            var packet = PacketBuilder.Build(
                DateTime.Now,
                new EthernetLayer()
            {
                EtherType   = EthernetType.None,
                Source      = PhysicalAddressToMacAddress(sourcePhysicalAddress),
                Destination = BroadcastMac,
            },
                new ArpLayer()
            {
                ProtocolType          = EthernetType.IpV4,
                Operation             = ArpOperation.Request,
                SenderProtocolAddress = sourceAddress.ToBytes(),
                SenderHardwareAddress = sourcePhysicalAddress.GetAddressBytes().AsReadOnly(),
                TargetProtocolAddress = targetAddress.ToBytes(),
                TargetHardwareAddress = MacAddress.Zero.ToBytes(),
            }
                );

            communicator.SendPacket(packet);

            Task.Run(() =>
            {
                while (true)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    communicator.ReceivePacket(out Packet p);

                    if (p != null &&
                        p.IsValid &&
                        p.Ethernet.IsValid &&
                        p.Ethernet.EtherType == EthernetType.Arp &&
                        p.Ethernet.Arp.IsValid &&
                        p.Ethernet.Arp.Operation == ArpOperation.Reply &&
                        p.Ethernet.Arp.SenderProtocolIpV4Address == targetAddress &&
                        p.Ethernet.Arp.TargetProtocolIpV4Address == sourceAddress)
                    {
                        lock (resultLock)
                            result = new MacAddress(BitConverter.ToString(p.Ethernet.Arp.SenderHardwareAddress.ToArray()).Replace('-', ':'));

                        break;
                    }
                }
            });

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            while (true)
            {
                lock (resultLock)
                {
                    if (result != null)
                    {
                        return(result);
                    }
                }

                if (stopwatch.ElapsedMilliseconds >= timeout)
                {
                    tokenSource.Cancel();
                    communicator.Break();
                    break;
                }
                else
                {
                    Thread.Sleep(100);
                }
            }

            return(result);
        }