Exemplo n.º 1
0
        /// <summary>
        /// Gets the best match route with the lowest metric to the given destination.
        /// </summary>
        /// <param name="ipa">The destination to search the route for.</param>
        /// <returns>The best route to the destination, or null if no route is found.</returns>
        public RoutingEntry GetRouteToDestination(IPAddress ipa)
        {
            int          iMetric     = int.MaxValue;
            uint         iMask       = 0;
            int          iMaskFav    = 0;
            RoutingEntry reFavourite = null;

            lock (lAllRoutes)
            {
                foreach (RoutingEntry re in lAllRoutes)
                {
                    if (ipa.AddressFamily == re.Destination.AddressFamily &&
                        IPAddressAnalysis.GetClasslessNetworkAddress(re.Destination, re.Subnetmask).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipa, re.Subnetmask)))
                    {
                        if (iMask > iMaskFav)
                        {
                            iMetric     = re.Metric;
                            reFavourite = re;
                            iMaskFav    = reFavourite.Subnetmask.PrefixLength;
                        }
                        else if (re.Metric < iMetric && iMask == iMaskFav)
                        {
                            iMetric     = re.Metric;
                            reFavourite = re;
                            iMaskFav    = reFavourite.Subnetmask.PrefixLength;
                        }
                    }
                }
            }

            return(reFavourite);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new instance of this class
        /// </summary>
        public NetMap()
        {
            ndiuUtility = new NetDiscoveryUtility();
            ndiuUtility.OnResolveFinished += new NetDiscoveryUtility.ResolveCompletedEventHandler(ndiuUtility_OnResolveFinished);
            lHosts                = new List <Host>();
            dictIPHost            = new Dictionary <IPAddress, Host>();
            dictMACHost           = new Dictionary <MACAddress, Host>();
            lDataLinkNeighbours   = new List <Host>();
            lUpperLayerNeighbours = new List <Host>();
            lDataLinkDistributors = new List <Host>();
            ResolveHostnames      = true;

            hLocalhost = CreateHost(System.Environment.MachineName);
            Host hNetwork;

            IPAddress[]  ipa;
            Subnetmask[] smMasks;
            MACAddress   mcMac;

            string[] strInterfaces = InterfaceConfiguration.GetAllInterfaceNames();

            for (int iC2 = 0; iC2 < strInterfaces.Length; iC2++)
            {
                if (strInterfaces[iC2] != null)
                {
                    ipa     = InterfaceConfiguration.GetIPAddressesForInterface(strInterfaces[iC2]);
                    smMasks = InterfaceConfiguration.GetIPSubnetsForInterface(strInterfaces[iC2]);
                    mcMac   = InterfaceConfiguration.GetMacAddressForInterface(strInterfaces[iC2]);
                    if (ipa != null)
                    {
                        for (int iC1 = 0; iC1 < ipa.Length; iC1++)
                        {
                            if (ipa[iC1].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            {
                                if (smMasks != null && smMasks.Length > iC1)
                                {
                                    AddIPToHost(hLocalhost, ipa[iC1]);
                                    hNetwork = CreateHost("Network");
                                    SetAsDistributionDevice(hNetwork);
                                    hNetwork.Properties.Add("subnetmask", smMasks[iC1]);
                                    hNetwork.Properties.Add("network", IPAddressAnalysis.GetClasslessNetworkAddress(ipa[iC1], smMasks[iC1]));
                                    AddIPToHost(hNetwork, IPAddressAnalysis.GetClasslessNetworkAddress(ipa[iC1], smMasks[iC1]));
                                    lDataLinkDistributors.Add(hNetwork);
                                    Connect(hNetwork, hLocalhost);
                                }
                            }
                        }
                    }
                    if (mcMac != null)
                    {
                        AddMACToHost(hLocalhost, mcMac);
                    }
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Adds an interface to this router.
 /// </summary>
 /// <param name="ipi">The IPInterface to add.</param>
 public override void AddInterface(IPInterface ipi)
 {
     for (int iC1 = 0; iC1 < ipi.IpAddresses.Length && iC1 < ipi.Subnetmasks.Length; iC1++)
     {
         RoutingEntry re = new RoutingEntry(ipi.IpAddresses[iC1], IPAddressAnalysis.GetClasslessNetworkAddress(ipi.IpAddresses[iC1], ipi.Subnetmasks[iC1]), 0, ipi.Subnetmasks[iC1], RoutingEntryOwner.Interface);
         re.NextHopInterface = ipi;
         this.rtRoutingtable.AddRoute(re);
     }
     ipi.AddressAdded   += new IPInterface.AddressEventHandler(ipi_AddressAdded);
     ipi.AddressRemoved += new IPInterface.AddressEventHandler(ipi_AddressRemoved);
     base.AddInterface(ipi);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Adds the given NAT address range to the external range
 /// </summary>
 /// <param name="toAdd"></param>
 public void AddToExternalRange(NATAddressRange toAdd)
 {
     lock (lExternalRange)
     {
         lExternalAddressPool.AddRange(IPAddressAnalysis.GetIPRange(
                                           IPAddressAnalysis.GetClasslessNetworkAddress(toAdd.NetworkAddress, toAdd.Subnetmask),
                                           IPAddressAnalysis.GetClasslessBroadcastAddress(toAdd.NetworkAddress, toAdd.Subnetmask))
                                       );
         lExternalRange.Add(toAdd);
     }
     InvokePropertyChanged();
 }
Exemplo n.º 5
0
 private bool IsInternalRange(IPAddress ipaAddress)
 {
     lock (lInternalRange)
     {
         foreach (NATAddressRange ipa in lInternalRange)
         {
             if (IPAddressAnalysis.GetClasslessNetworkAddress(ipa.NetworkAddress, ipa.Subnetmask).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipaAddress, ipa.Subnetmask)))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Removes the given NAT address range from the external range. Open connections will not be interrupted.
 /// </summary>
 /// <param name="toRemove">The address range to remove</param>
 public void RemoveFromExternalRange(NATAddressRange toRemove)
 {
     lock (lExternalRange)
     {
         foreach (IPAddress ipa in IPAddressAnalysis.GetIPRange(
                      IPAddressAnalysis.GetClasslessNetworkAddress(toRemove.NetworkAddress, toRemove.Subnetmask),
                      IPAddressAnalysis.GetClasslessBroadcastAddress(toRemove.NetworkAddress, toRemove.Subnetmask))
                  )
         {
             lExternalAddressPool.Remove(ipa);
         }
         lExternalRange.Remove(toRemove);
     }
     InvokePropertyChanged();
 }
        private IPAddress[] GetSourceIPsForARPRequest(IPAddress ipaQuery)
        {
            List <IPAddress> lAddresses = new List <IPAddress>();

            foreach (IPAddress ipa in this.IpAddresses)
            {
                if (ipa.AddressFamily == ipaQuery.AddressFamily)
                {
                    if (IPAddressAnalysis.GetClasslessNetworkAddress(ipa, this.GetMaskForAddress(ipa)).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipaQuery, this.GetMaskForAddress(ipa))))
                    {
                        lAddresses.Add(ipa);
                    }
                }
            }

            return(lAddresses.ToArray());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets all matching routes for a destination.
        /// </summary>
        /// <param name="ipa">The destination to get the routes for.</param>
        /// <returns>An array filled with all routes for the given destination.</returns>
        public RoutingEntry[] GetRoutes(IPAddress ipa)
        {
            List <RoutingEntry> lRe = new List <RoutingEntry>();

            lock (lAllRoutes)
            {
                foreach (RoutingEntry re in lAllRoutes)
                {
                    if (ipa.AddressFamily == re.Destination.AddressFamily &&
                        IPAddressAnalysis.GetClasslessNetworkAddress(re.Destination, re.Subnetmask).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipa, re.Subnetmask)))
                    {
                        lRe.Add(re);
                    }
                }
            }

            return(lRe.ToArray());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns all IPInterfaces connected with this DirectInterfaceIO and subnets matching the given address.
        /// </summary>
        /// <param name="ipaAddress">The address to search a match for.</param>
        /// <returns>All IPInterfaces with subnets matching the given address</returns>
        protected IPInterface[] GetInterfacesForAddress(IPAddress ipaAddress)
        {
            List <IPInterface> lReturnInterfaces = new List <IPInterface>();

            foreach (IPInterface ipi in lInterfaces)
            {
                for (int iC1 = 0; iC1 < ipi.IpAddresses.Length && iC1 < ipi.Subnetmasks.Length; iC1++)
                {
                    IPAddress[]  ipaAddresses = ipi.IpAddresses;
                    Subnetmask[] smMasks      = ipi.Subnetmasks;
                    if (ipaAddresses[iC1].AddressFamily == ipaAddress.AddressFamily &&
                        IPAddressAnalysis.GetClasslessNetworkAddress(ipaAddresses[iC1], smMasks[iC1]).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipaAddress, smMasks[iC1])))
                    {
                        lReturnInterfaces.Add(ipi);
                    }
                }
            }
            return(lReturnInterfaces.ToArray());
        }
Exemplo n.º 10
0
        private Host GetNetworkForAddress(IPAddress ipa)
        {
            int  iMask;
            int  iMaskFav   = -1;
            Host hFavourite = null;

            foreach (Host h in lDataLinkDistributors)
            {
                if (((IPAddress)h.Properties["network"]).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipa, (Subnetmask)h.Properties["subnetmask"])))
                {
                    iMask = SubnetmaskToInt((Subnetmask)h.Properties["subnetmask"]);

                    if (iMask > iMaskFav)
                    {
                        iMaskFav   = SubnetmaskToInt((Subnetmask)h.Properties["subnetmask"]);
                        hFavourite = h;
                    }
                }
            }

            return(hFavourite);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Adds ARP scan tasks for the range between the given start and the given end address and associates them with the according interfaces
 /// </summary>
 /// <param name="ipaStart">The IP address where scanning starts</param>
 /// <param name="ipaEnd">The IP address where scanning ends</param>
 public void AddARPScanTask(System.Net.IPAddress ipaStart, System.Net.IPAddress ipaEnd)
 {
     if (lInterfaces.Count < 1)
     {
         throw new Exception("There are currently no interfaces present.");
     }
     if (ipaStart.AddressFamily != ipaEnd.AddressFamily)
     {
         throw new ArgumentException("Cannot mix up diffrent types of addresses.");
     }
     foreach (EthernetInterface ipi in lInterfaces)
     {
         for (int iC1 = 0; iC1 < ipi.IpAddresses.Length && iC1 < ipi.Subnetmasks.Length; iC1++)
         {
             if (ipi.IpAddresses[iC1].AddressFamily == ipaStart.AddressFamily && ipi.Subnetmasks[iC1].AddressFamily == ipaStart.AddressFamily &&
                 (IPAddressAnalysis.GetClasslessNetworkAddress(ipi.IpAddresses[iC1], ipi.Subnetmasks[iC1]).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipaStart, ipi.Subnetmasks[iC1])) ||
                  IPAddressAnalysis.GetClasslessNetworkAddress(ipi.IpAddresses[iC1], ipi.Subnetmasks[iC1]).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipaEnd, ipi.Subnetmasks[iC1]))))
             {
                 this.AddARPScanTask(new ARPScanTask(ipaStart, ipaEnd, ipi.PrimaryMACAddress, ipi.IpAddresses[iC1], this));
             }
         }
     }
 }
Exemplo n.º 12
0
 /// <summary>
 /// Creates a pool filled with addresses from the given start to the given end IP address and associates the pool to the according interfaces.
 /// Items which cannot be asooicated with an interface are ignored.
 /// </summary>
 /// <param name="ipaStart">The start address of the pool range</param>
 /// <param name="ipaEnd">The end address of the pool range</param>
 public void CreatePool(IPAddress ipaStart, IPAddress ipaEnd)
 {
     if (lInterfaces.Count < 1)
     {
         throw new Exception("There are currently no interfaces present.");
     }
     if (ipaStart.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork || ipaEnd.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
     {
         throw new ArgumentException("Only IPv4 is supported at the moment");
     }
     foreach (IPInterface ipi in lInterfaces)
     {
         for (int iC1 = 0; iC1 < ipi.IpAddresses.Length && iC1 < ipi.Subnetmasks.Length; iC1++)
         {
             if (ipi.IpAddresses[iC1].AddressFamily == ipaStart.AddressFamily && ipi.IpAddresses[iC1].AddressFamily == ipaEnd.AddressFamily &&
                 (IPAddressAnalysis.GetClasslessNetworkAddress(ipi.IpAddresses[iC1], ipi.Subnetmasks[iC1]).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipaStart, ipi.Subnetmasks[iC1])) ||
                  IPAddressAnalysis.GetClasslessNetworkAddress(ipi.IpAddresses[iC1], ipi.Subnetmasks[iC1]).Equals(IPAddressAnalysis.GetClasslessNetworkAddress(ipaEnd, ipi.Subnetmasks[iC1]))))
             {
                 this.CreatePool(ipaStart, ipaEnd, ipi, iC1);
             }
         }
     }
 }