Beispiel #1
0
 private void TryFireArpReplyReceied(Common.IPv4Address ip, Common.MacAddress mac)
 {
     try
     {
         ArpReplyReceived(ip, mac);
     }
     catch
     {
     }
 }
Beispiel #2
0
        public Common.MacAddress ResolveIP(Common.IPv4Address ip)
        {
            // Do we already know?
            if (ArpCache.ContainsKey(ip.AsString) && (ArpCache[ip.AsString].Expiration > DateTime.Now || ArpCache[ip.AsString].Type == ArpEntryType.Static))
            {
                Common.MacAddress mac = ArpCache[ip.AsString].Mac;
                if (DisableCache)
                {
                    ArpCache.Clear();
                }
                return(mac);
            }

            // Is this the broadcast IP for our network?
            if (ip.AsString == _client.Configuration.BroadcastAddress.AsString)
            {
                return(Common.MacAddress.Broadcast);
            }

            // Is this me?
            if (ip.AsString == _client.Configuration.IpAddress.AsString)
            {
                return(_client.Configuration.MacAddress);
            }

            // Is this IP in our subnet?
            if (!Common.IPv4Address.IsInSameSubnet(ip, _client.Configuration.IpAddress, _client.Configuration.SubnetMask))
            {
                return(ResolveIP(_client.Configuration.DefaultGateway));
            }

            // We don't know. Send packet and wait for response.
            SendArpResolutionPacket(ip);

            DateTime now = DateTime.Now;

            while ((DateTime.Now - now).TotalSeconds < TIMEOUT)
            {
                if (ArpCache.ContainsKey(ip.AsString) && (ArpCache[ip.AsString].Expiration > DateTime.Now || ArpCache[ip.AsString].Type == ArpEntryType.Static))
                {
                    Common.MacAddress mac = ArpCache[ip.AsString].Mac;
                    if (DisableCache)
                    {
                        ArpCache.Clear();
                    }
                }
                System.Threading.Thread.Sleep(100);
            }
            if (ArpCache.ContainsKey(ip.AsString))
            {
                return(ArpCache[ip.AsString].Mac);
            }
            throw new IpResolutionFailed(ip);
        }
Beispiel #3
0
        public void AddStaticEntry(Common.IPv4Address ip, Common.MacAddress mac)
        {
            ArpCacheEntry entry = new ArpCacheEntry()
            {
                Ip         = ip,
                Mac        = mac,
                Expiration = DateTime.Now,
                Type       = ArpEntryType.Static,
            };

            if (ArpCache.ContainsKey(ip.AsString))
            {
                ArpCache[ip.AsString] = entry;
            }
            else
            {
                ArpCache.AddOrUpdate(ip.AsString, entry, null);
            }
            TryFireArpCacheChanged();
        }
Beispiel #4
0
        public override void OnReceivePacket(Common.PacketData pdata)
        {
            var packet    = pdata.Packet;
            var arp       = packet.Ethernet.Arp;
            var senderMac = new Common.MacAddress(arp.SenderHardwareAddress.ToArray());
            var destMac   = new Common.MacAddress(arp.TargetHardwareAddress.ToArray());
            var senderIp  = new Common.IPv4Address(arp.SenderProtocolAddress.ToArray());
            var destIp    = new Common.IPv4Address(arp.TargetProtocolAddress.ToArray());

            // We can cache their MAC and IP
            if (AcceptGratuitousReplies || destMac.AsString != Common.MacAddress.Broadcast.AsString)
            {
                ArpCache.AddOrUpdate(senderIp.AsString, new ArpCacheEntry()
                {
                    Mac        = senderMac,
                    Ip         = senderIp,
                    Type       = ArpEntryType.Dynamic,
                    Expiration = GetNextExpirationTime(),
                }, (str, myarp) => myarp);

                if (arp.Operation == ArpOperation.Reply)
                {
                    TryFireArpReplyReceied(senderIp, senderMac);
                }

                // Fire the Cache Changed event
                TryFireArpCacheChanged();
            }

            // If it is a request for our IP, respond
            if (arp.Operation == ArpOperation.Request && destIp.AsString == _client.Configuration.IpAddress.AsString)
            {
                // Send Reply Packet
                SendArpReplyPacket(senderIp);
            }
        }