Skip to content

Commit

Permalink
Issue #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed Feb 12, 2019
1 parent c322197 commit 0bda9e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
30 changes: 24 additions & 6 deletions Router/RIP/RIPService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using PacketDotNet;
using Router.Protocols;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Router.RIP
{
Expand Down Expand Up @@ -47,16 +49,32 @@ public void OnStopped(Interface Interface)

public void OnChanged(Interface Interface)
{
throw new NotImplementedException();

var RIPEntry = RIPTable.Instance.Find(Interface, Interface.IPNetwork);
if (RIPEntry == null)
// Poison old
var OldRIPEntry = RIPTable.Instance.FindAll(Interface).Where(Entry => Entry.Metric == 1).First();
if (OldRIPEntry == null)
{
throw new Exception("RIPEntry not found while Updating");
throw new Exception("RIPEntry not found while Stopping");
}

RIPUpdates.SendTriggered(Interface, RIPEntry);
OldRIPEntry.PossibblyDown = true;

// Remove all routes from that interface ?
RIPTable.Instance.Remove(Interface);

// Insert new
var NewRIPEntry = new RIPEntry(Interface, Interface.IPNetwork, Interface.IPAddress, 1)
{
SyncWithRT = false,
CanBeUpdated = false,
TimersEnabled = false
};
RIPTable.Instance.Add(NewRIPEntry);

RIPUpdates.SendTriggered(Interface, new List<RIPEntry> {
OldRIPEntry,
NewRIPEntry
});
RIPRequest.AskForUpdate(Interface);
RIPTable.Instance.SyncWithRT();
}

Expand Down
24 changes: 24 additions & 0 deletions Router/RIP/RIPTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,21 @@ public void GarbageCollector()
});
}

private int LastHashCode;

public void SyncWithRT()
{
GarbageCollector();

var Entries = BestEntries();

var HashCode = RIPListExtensions.GetHashCode(Entries);
if (Entries.Count == 0 || LastHashCode == HashCode)
{
return;
}
LastHashCode = HashCode;

foreach (var Entry in Entries)
{
if (!Entry.SyncWithRT)
Expand Down Expand Up @@ -121,5 +132,18 @@ public static RIPEntry Find(this List<RIPEntry> Entries, IPNetwork IPNetwork)
{
return Entries.Find(Entry => Entry.IPNetwork == IPNetwork);
}

public static int GetHashCode(List<RIPEntry> Entries)
{
unchecked
{
int hash = 19;
foreach (var Entry in Entries)
{
hash = hash * 31 + Entry.GetHashCode();
}
return hash;
}
}
}
}

0 comments on commit 0bda9e6

Please sign in to comment.