/// <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> /// Returns a bool indicating whether this routing table contains a specific entry /// </summary> /// <param name="reEntry">The entry to search for</param> /// <returns>A bool indicating whether this routing table contains a specific entry</returns> public bool ContainsEntry(RoutingEntry reEntry) { lock (lAllRoutes) { return(lAllRoutes.Contains(reEntry)); } }
void ipi_AddressAdded(object sender, AddressEventArgs args) { lLocalAdresses.Add(args.IP); RoutingEntry reEntry = new RoutingEntry(args.IP, args.IP, 0, args.Netmask, RoutingEntryOwner.Interface); reEntry.NextHopInterface = args.Interface; rtRoutingtable.AddRoute(reEntry); }
/// <summary> /// Removes a routing entry from this routing table. /// </summary> /// <param name="reToRemove">The routing entry to remove</param> public void RemoveRoute(RoutingEntry reToRemove) { lock (lAllRoutes) { lAllRoutes.Remove(reToRemove); } Invoke(RouteRemoved, new RoutingTableEventArgs(reToRemove, this)); }
/// <summary> /// Adds a routing entry to this routing table. /// </summary> /// <param name="reToAdd">The routing entry to add</param> public void AddRoute(RoutingEntry reToAdd) { lock (lAllRoutes) { lAllRoutes.Add(reToAdd); } Invoke(RouteAdded, new RoutingTableEventArgs(reToAdd, this)); }
/// <summary> /// Invokes the routing entry updated event for a specific routing entry. /// </summary> /// <param name="re">The routing entry which has been updated.</param> protected void InvokeEntryUpdated(RoutingEntry re) { IRouter rRouter = RouterToManage; if (RouterToManage != null) { RouterToManage.RoutingTable.InvokeRouteUpdated(re); } }
private IPInterface GetInterfaceForIPSubnet(IPAddress ipaDestination) { RoutingEntry re = rtRoutingtable.GetRouteToDestination(ipaDestination); if (re != null && re.Owner == RoutingEntryOwner.Interface) { return(re.NextHopInterface); } return(null); }
/// <summary> /// Removes an interface from this router. /// </summary> /// <param name="ipi">The IPInterface to remove.</param> public override void RemoveInterface(IPInterface ipi) { for (int iC1 = 0; iC1 < ipi.IpAddresses.Length && iC1 < ipi.Subnetmasks.Length; iC1++) { RoutingEntry re = new RoutingEntry(ipi.IpAddresses[0], ipi.IpAddresses[0], 0, ipi.Subnetmasks[0], RoutingEntryOwner.Interface); re.NextHopInterface = ipi; this.rtRoutingtable.RemoveRoute(re); } ipi.AddressAdded -= new IPInterface.AddressEventHandler(ipi_AddressAdded); ipi.AddressRemoved -= new IPInterface.AddressEventHandler(ipi_AddressRemoved); base.RemoveInterface(ipi); }
/// <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 a routing entry to this instance and the router to manage. /// </summary> /// <param name="re">The routing entry to add</param> protected void AddRoutingEntry(RoutingEntry re) { lEntries.Add(re); IRouter rRouter = RouterToManage; lock (oRouteLock) { if (RouterToManage != null) { RouterToManage.RoutingTable.AddRoute(re); } } }
/// <summary> /// Compares whether two routing entries are equal or not. /// </summary> /// <param name="obj">The routing entry to compare to this instance.</param> /// <returns>A bool indicating whether the two routing entries are equal or not</returns> public override bool Equals(object obj) { if (typeof(RoutingEntry) != obj.GetType()) { return(false); } RoutingEntry re = (RoutingEntry)obj; if (re.Metric == this.Metric && re.Subnetmask.Equals(this.Subnetmask) && re.NextHop.Equals(this.NextHop) && re.Destination.Equals(this.Destination) && this.Owner == re.Owner) { return(true); } return(false); }
/// <summary> /// Removes a routing entry from this instance and the router to manage. /// </summary> /// <param name="re">The routing entry to remove.</param> protected void RemoveEntry(RoutingEntry re) { lEntries.Remove(re); RouterToManage.RoutingTable.RemoveRoute(re); }
private bool IsInLocalSubnet(IPAddress ipaDestination) { RoutingEntry re = rtRoutingtable.GetRouteToDestination(ipaDestination); return(re != null && re.Owner == RoutingEntryOwner.Interface); }
private void RouteFrame(Frame fInputFrame) { IPFrame ipFrame = GetIPFrame(fInputFrame); IPInterface ipintOutInt = null; IPAddress ipaDestination = null; IPAddress ipaNextHop = null; if (ipFrame != null) // If it is an IP frame { ipaDestination = ipFrame.DestinationAddress; if (IsInLocalSubnet(ipaDestination)) // If destination is in the local subnet { #region local subnet routing ipintOutInt = GetInterfaceForIPSubnet(ipaDestination); if (ipintOutInt != null) { ipintOutInt.Send(fInputFrame, ipaDestination); iRoutedPackets++; InvokeInterfaceFramePushed(); PushRoutedFrame(fInputFrame); } else { //*FRAMEDROP/Interface error* iDroppedPackets++; PushDroppedFrame(fInputFrame); throw new RoutingException("The destination is known to be in a local subnet, but no valid interface was available for routing (" + ipaDestination + ")."); } #endregion } else // Routing { #region routing RoutingEntry reEntry = rtRoutingtable.GetRouteToDestination(ipaDestination); //Lookup if (reEntry != null) { ipintOutInt = reEntry.NextHopInterface; ipaNextHop = reEntry.NextHop; } while (reEntry != null && ipintOutInt == null) //Iteriere durch tabelle { reEntry = rtRoutingtable.GetRouteToDestination(reEntry.NextHop); if (reEntry.Owner == RoutingEntryOwner.Interface) { ipintOutInt = reEntry.NextHopInterface; } else { ipaNextHop = reEntry.NextHop; } } if (ipintOutInt != null && ipaNextHop != null) { ipintOutInt.Send(fInputFrame, ipaNextHop); iRoutedPackets++; InvokeInterfaceFramePushed(); PushRoutedFrame(fInputFrame); } else { //*FRAMEDROP/Route error* iDroppedPackets++; PushDroppedFrame(fInputFrame); throw new RoutingException("No route is known for the given destination (" + ipaDestination + ")."); } #endregion } } }
/// <summary> /// Creates a new instance of this class. /// </summary> /// <param name="reEntry">The routing entry</param> /// <param name="rtOwner">The routing table owning the routing entry</param> public RoutingTableEventArgs(RoutingEntry reEntry, RoutingTable rtOwner) { this.reEntry = reEntry; this.rtOwner = rtOwner; }
/// <summary> /// Rises the RouteUpdated event. If a class changes a routing entry, it has to rise this event immediatly after changing the routing entry. /// </summary> /// <param name="re">The changed routing entry.</param> public void InvokeRouteUpdated(RoutingEntry re) { this.Invoke(RouteUpdated, new RoutingTableEventArgs(re, this)); }