/// <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); }
/// <summary> /// Creates a DHCP pool and fills it according to the given params /// </summary> /// <param name="ipaPoolStart">The start IP address of the pool</param> /// <param name="ipaPoolEnd">The end IP address of the pool</param> /// <param name="ipaStandardgateway">The standardgateway's IP address</param> /// <param name="ipaDNSServer">The DNS server's IP address</param> /// <param name="smMask">The subnetmask</param> public DHCPPool(IPAddress ipaPoolStart, IPAddress ipaPoolEnd, IPAddress ipaStandardgateway, IPAddress ipaDNSServer, Subnetmask smMask) : this() { IPAddress[] ipRange = IPAddressAnalysis.GetIPRange(ipaPoolStart, ipaPoolEnd); foreach (IPAddress ipa in ipRange) { lDHCPPool.Add(new DHCPPoolItem(ipa, smMask, ipaStandardgateway, ipaDNSServer)); } }
/// <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); } } } }
/// <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); }
/// <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(); }
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); }
private void ScanInternal() { if (IPAddressAnalysis.Compare(byteStartIP, byteEndIP) != 1) { Scan(new IPAddress(byteStartIP)); IPAddressAnalysis.Increase(byteStartIP); ScannedCount++; } else { IsFinished = true; } }
/// <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()); }
/// <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()); }
/// <summary> /// Creates a pool from the given parameters /// </summary> /// <param name="ipaStart">The start address of the pool range</param> /// <param name="ipaEnd">The end address of the pool range</param> /// <param name="ipi">The IP idnterface to which this pool should be associated</param> /// <param name="iAddrCounter">The index of the address of the interface to use if the interface has multiple IP addresses assigned</param> protected virtual void CreatePool(IPAddress ipaStart, IPAddress ipaEnd, IPInterface ipi, int iAddrCounter) { 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"); } DHCPPool dhPool = GetPoolForInterface(ipi); IPAddress[] ipRange = IPAddressAnalysis.GetIPRange(ipaStart, ipaEnd); foreach (IPAddress ipa in ipRange) { IPInterface ipiInterface = ipi; IPAddress ipAddress = ipa; IPAddress ipGateway; Subnetmask smMask = ipi.Subnetmasks[iAddrCounter]; IPAddress ipDnsServer = IPAddress.Any; if (ipaGateway == null) { ipGateway = ipi.IpAddresses[iAddrCounter]; } else { ipGateway = ipaGateway; } if (ipaDNSServer == null) { ipDnsServer = ipi.IpAddresses[iAddrCounter]; } else { ipDnsServer = ipaDNSServer; } if (dhPool.GetItemForAddress(ipAddress) == null) { DHCPPoolItem dhPoolItem = new DHCPPoolItem(ipAddress, smMask, ipGateway, ipDnsServer); AddPoolItem(dhPoolItem, dhPool, ipi); } } }
/// <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()); }
private void SendICMPNeighborSolicitation(IPAddress ipaQuery, IPAddress ipaSource) { if (ipaQuery.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { throw new InvalidOperationException("Cannot send an ICMP Neighbor Solicitation for a non IPv6 address."); } ICMPv6Frame icmpFrame = new ICMPv6Frame(); icmpFrame.ICMPv6Type = ICMPv6Type.NeighborSolicitation; icmpFrame.ICMPCode = 0; NeighborSolicitation icmpDiscoveryMessage = new NeighborSolicitation(); icmpDiscoveryMessage.TargetAddress = ipaQuery; NeighborDiscoveryOption icmpDiscoveryOption = new NeighborDiscoveryOption(); icmpDiscoveryOption.OptionType = ICMP.V6.NeighborDiscoveryOptionType.SourceLinkLayerAddress; icmpDiscoveryOption.OptionData = this.PrimaryMACAddress.AddressBytes; IPv6Frame ipFrame = new IPv6Frame(); ipFrame.SourceAddress = ipaSource; ipFrame.DestinationAddress = IPAddressAnalysis.GetSolicitedNodeMulticastAddress(ipaQuery); ipFrame.NextHeader = IPProtocol.IPv6_ICMP; EthernetFrame ethFrame = new EthernetFrame(); ethFrame.Destination = MACAddress.MulticastFromIPv6Address(ipaQuery); ethFrame.Source = PrimaryMACAddress; ethFrame.EtherType = EtherType.IPv6; ethFrame.EncapsulatedFrame = ipFrame; ipFrame.EncapsulatedFrame = icmpFrame; icmpFrame.EncapsulatedFrame = icmpDiscoveryMessage; icmpDiscoveryMessage.EncapsulatedFrame = icmpDiscoveryOption; icmpFrame.Checksum = icmpFrame.CalculateChecksum(ipFrame.GetPseudoHeader()); this.Send(ethFrame); }
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); }
/// <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)); } } } }
/// <summary> /// Creates a new instance of this class with the given params /// </summary> /// <param name="ipaStart">The start IP address of the range to scan</param> /// <param name="ipaEnd">The end IP address of the range to scan</param> /// <param name="ipLocal">The IP address which should be spoofed during scanning</param> /// <param name="thOut">The traffic handler to which the generated ARP frames should be forwarded. It is wise to assign an ARP net scanner here</param> protected ScanTask(IPAddress ipaStart, IPAddress ipaEnd, IPAddress ipLocal, TrafficHandler thOut) { if (IPAddressAnalysis.Compare(ipaStart, ipaEnd) == -1) { this.StartAddress = ipaStart; this.EndAddress = ipaEnd; } else { this.StartAddress = ipaEnd; this.EndAddress = ipaStart; } this.SourceAddress = ipLocal; this.OutputHandler = thOut; this.bIsFinished = false; this.byteStartIP = this.StartAddress.GetAddressBytes(); this.byteEndIP = this.EndAddress.GetAddressBytes(); this.ScannedCount = 0; ScanCount = IPAddressAnalysis.GetIpCount(ipaStart, ipaEnd); }
/// <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); } } } }
/// <summary> /// Handles RIPv1 updates /// </summary> /// <param name="udpFrame"></param> /// <param name="ipFrame"></param> /// <returns>Bool indicating if something changed</returns> private bool HandleRIPV1(UDP.UDPFrame udpFrame, IP.IPFrame ipFrame) { RIPFrame fRIPFrame; bool bChanged = false; if (ipFrame.SourceAddress.Equals(IPAddress.Broadcast)) // Check Addr { if (udpFrame.DestinationPort == iRIPPort) // Check Port { fRIPFrame = new RIPFrame(udpFrame.EncapsulatedFrame.FrameBytes); if (fRIPFrame.Version == 1) { foreach (RIPUpdate ru in fRIPFrame.GetUpdates()) { if (ru.AddressFamilyIdentifier == RIPEntryAddressFamily.IPv4) { bChanged |= UpdateEntry(ipFrame.SourceAddress, ru.Address, IPAddressAnalysis.GetClassfullSubnetMask(ru.Address), (int)ru.Metric); } } } } } return(bChanged); }