diff --git a/Program.cs b/Program.cs index 0be90b8..de05c32 100644 --- a/Program.cs +++ b/Program.cs @@ -34,6 +34,8 @@ using Castle.DynamicProxy; using ln.types.odb; using System.Net.Sockets; using ln.skyscanner.entities; +using ln.types.btree; +using ln.types.test; namespace ln.skyscanner { @@ -43,8 +45,11 @@ namespace ln.skyscanner public static void Main(string[] args) { + new testODB().TestCase(); + return; + FileLogger fileLogger = new FileLogger("skyscanner.log"); - fileLogger.MaxLogLevel = LogLevel.DEBUG; + fileLogger.MaxLogLevel = LogLevel.INFO; Logger.Default.Backends.Add(fileLogger); Logger.ConsoleLogger.MaxLogLevel = LogLevel.INFO; @@ -61,41 +66,41 @@ namespace ln.skyscanner //using (ODB odb = new ODB("odb")) //{ - //ODBCollection col = odb.GetCollection("test"); + //ODBCollection col = odb.GetCollection("test"); - //foreach (ODBDocument odoc in col) - //{ - // Console.WriteLine("Stored DOC: {0}", odoc); - // if (!odoc.Contains("counter")) - // odoc["counter"] = 0; - // else - // odoc["counter"] = odoc["counter"].AsInt + 1; + //foreach (ODBDocument odoc in col) + //{ + // Console.WriteLine("Stored DOC: {0}", odoc); + // if (!odoc.Contains("counter")) + // odoc["counter"] = 0; + // else + // odoc["counter"] = odoc["counter"].AsInt + 1; - // col.Upsert(odoc); - //} + // col.Upsert(odoc); + //} - //ODBDocument doc = new ODBDocument(); + //ODBDocument doc = new ODBDocument(); - //doc["snmp.msg"] = "Hallo WelT!"; - //doc["name"] = "Harald"; - //doc["alter"] = 39; - //doc["genauer"] = 34.5; - //ODBDocument ndoc = new ODBDocument(); - //doc["keywords"] = ndoc; + //doc["snmp.msg"] = "Hallo WelT!"; + //doc["name"] = "Harald"; + //doc["alter"] = 39; + //doc["genauer"] = 34.5; + //ODBDocument ndoc = new ODBDocument(); + //doc["keywords"] = ndoc; - //ndoc["eins"] = 1; - //ndoc["zwei"] = 2; - //ndoc["drei"] = 3; + //ndoc["eins"] = 1; + //ndoc["zwei"] = 2; + //ndoc["drei"] = 3; - //Console.WriteLine(doc); + //Console.WriteLine(doc); - //col.Insert(doc); + //col.Insert(doc); - //Node node = new Node(CIDR.Parse("1.2.3.4")); - //ODBDocument nodeDoc = ODBMapper.Default.ToODBValue(node).AsDocument; - //Console.WriteLine("Node: {0}", nodeDoc); + //Node node = new Node(CIDR.Parse("1.2.3.4")); + //ODBDocument nodeDoc = ODBMapper.Default.ToODBValue(node).AsDocument; + //Console.WriteLine("Node: {0}", nodeDoc); - //Node node2 = ODBMapper.Default.ToNativeValue(nodeDoc); + //Node node2 = ODBMapper.Default.ToNativeValue(nodeDoc); //} } diff --git a/SkyEntities.cs b/SkyEntities.cs index 6019723..e0bfb2f 100644 --- a/SkyEntities.cs +++ b/SkyEntities.cs @@ -24,6 +24,7 @@ namespace ln.skyscanner public ODB odDatabase { get; private set; } public ODBCollection nodeCollection { get; private set; } public ODBCollection interfaceCollection { get; private set; } + public ODBCollection intfIPCollection { get; private set; } public ODBCollection subnetCollection { get; private set; } public SkyEntities(SkyScanner skyScanner) @@ -33,8 +34,12 @@ namespace ln.skyscanner odDatabase = new ODB(BasePath); nodeCollection = odDatabase.GetCollection(); interfaceCollection = odDatabase.GetCollection(); + intfIPCollection = odDatabase.GetCollection(); subnetCollection = odDatabase.GetCollection(); + interfaceCollection.EnsureIndex("NodeID"); + intfIPCollection.EnsureIndex("interfaceID"); + GlobalNetwork = new GlobalNetwork(this); } diff --git a/crawl/CrawledSubnet.cs b/crawl/CrawledSubnet.cs index c55eda6..391e505 100644 --- a/crawl/CrawledSubnet.cs +++ b/crawl/CrawledSubnet.cs @@ -12,7 +12,7 @@ using ln.types; using ln.types.odb; using System.Collections.Generic; using System.Linq; -namespace ln.skyscanner.entities +namespace ln.skyscanner.crawl { public class CrawledSubnet { diff --git a/entities/GlobalNetwork.cs b/entities/GlobalNetwork.cs index fb37257..50d29f0 100644 --- a/entities/GlobalNetwork.cs +++ b/entities/GlobalNetwork.cs @@ -11,6 +11,8 @@ using ln.types.odb; using ln.types.sync; using ln.logging; using System.Globalization; +using ln.types.threads; +using ln.types.odb.values; namespace ln.skyscanner.entities { @@ -61,13 +63,16 @@ namespace ln.skyscanner.entities } public IEnumerable FindHostsInSubnet(CIDR network) - { - return SkyEntities.nodeCollection.Select(node => network.Contains(node.IPAdresses)); + { + IntfIP[] intfIPs = SkyEntities.intfIPCollection.Select((ip) => ip.IP.In(network)).ToArray(); + NetworkInterface[] networkInterfaces = intfIPs.Select((iip) => iip.NetworkInterface).Distinct().ToArray(); + Node[] nodes = SkyEntities.nodeCollection.Select(networkInterfaces.Select((ni) => (ODBValue)ni.NodeID).Distinct()).ToArray(); + return nodes; } public IEnumerable FindNeighbors(Node node) { - return node.Interfaces.SelectMany((intf) => intf.IPs).SelectMany((arg) => FindHostsInSubnet(arg)).Distinct(); + return node.IPAdresses.SelectMany((ip) => Timing.Meassure("FindHostsInSubnet", () => FindHostsInSubnet(ip)).Distinct().ToArray()); } public Node FindNodeByIP(IEnumerable ips) @@ -83,7 +88,11 @@ namespace ln.skyscanner.entities public Node FindNodeByIP(CIDR ip) { ip = ip.Host; - return SkyEntities.nodeCollection.Select(node => ip.In(node.IPAdresses) || ip.In(node.PrimaryIP)).FirstOrDefault(); + + IntfIP _iip = SkyEntities.intfIPCollection.Where((iip) => iip.IP.Host.Equals(ip)).FirstOrDefault(); + if (_iip != null) + return _iip.NetworkInterface.Node; + return null; } [Unsynced] @@ -146,13 +155,38 @@ namespace ln.skyscanner.entities networkInterface = new NetworkInterface(node, fields[0]); networkInterface.HWAddress = fields[1]; - networkInterface.IPs = fields[2].Split(',').Where((sip)=> !String.Empty.Equals(sip)).Select((sip) => CIDR.Parse(sip)).ToArray(); SkyEntities.interfaceCollection.Upsert(networkInterface); - foreach (CIDR ip in networkInterface.IPs) - EnsureSubnet(ip); + CIDR[] crawledIPs = fields[2].Split(',').Where((sip) => !String.Empty.Equals(sip)).Select((sip) => CIDR.Parse(sip)).ToArray(); + IntfIP[] currentIPs = networkInterface.IPs.ToArray(); + foreach (CIDR crawledIP in crawledIPs) + { + int n; + for (n=0; n < currentIPs.Length; n++) + if (currentIPs[n].IP.Equals(crawledIP)) + break; + if (n == currentIPs.Length) + { + IntfIP miss = new IntfIP(networkInterface, crawledIP); + SkyEntities.intfIPCollection.Upsert(miss); + } + } + + foreach (IntfIP iip in currentIPs) + { + CIDR ip = iip.IP; + + if (crawledIPs.Contains(ip)) + { + EnsureSubnet(ip.Network); + } + else + { + SkyEntities.intfIPCollection.Delete(iip); + } + } } node.RemoveURI("ssh"); diff --git a/entities/HopMap.cs b/entities/HopMap.cs index 577c529..9c3def4 100644 --- a/entities/HopMap.cs +++ b/entities/HopMap.cs @@ -20,6 +20,8 @@ namespace ln.skyscanner.entities public Node Node { get; private set; } public GlobalNetwork GlobalNetwork => SkyScanner.Instance.Entities.GlobalNetwork; + public int MaxDepth { get; set; } = 2; + Dictionary hops = new Dictionary(); public HopMap() @@ -53,7 +55,8 @@ namespace ln.skyscanner.entities if (!hops.ContainsKey(node) || (hops[node] > level)) { hops[node] = level; - AddNodes(GlobalNetwork.FindNeighbors(node), level + 1); + if (level < MaxDepth) + AddNodes(GlobalNetwork.FindNeighbors(node), level + 1); } } diff --git a/entities/IntfIP.cs b/entities/IntfIP.cs new file mode 100644 index 0000000..8f02afd --- /dev/null +++ b/entities/IntfIP.cs @@ -0,0 +1,41 @@ +// /** +// * File: IntfIP.cs +// * Author: haraldwolff +// * +// * This file and it's content is copyrighted by the Author and / or copyright holder. +// * Any use wihtout proper permission is illegal and may lead to legal actions. +// * +// * +// **/ +using System; +using ln.types.odb; +using ln.types; +using Newtonsoft.Json; +namespace ln.skyscanner.entities +{ + public class IntfIP + { + [DocumentID] + public readonly Guid ID = Guid.NewGuid(); + + Guid interfaceID = Guid.Empty; + + [JsonIgnore] + public NetworkInterface NetworkInterface => SkyScanner.Instance.Entities.interfaceCollection.Select(interfaceID); + + public CIDR IP { get; private set; } + + public IntfIP() + { + } + public IntfIP(NetworkInterface networkInterface,CIDR ip) + { + interfaceID = networkInterface.ID; + IP = ip; + } + + + + + } +} diff --git a/entities/NetworkInterface.cs b/entities/NetworkInterface.cs index 24d2da5..62e6b16 100644 --- a/entities/NetworkInterface.cs +++ b/entities/NetworkInterface.cs @@ -20,7 +20,7 @@ namespace ln.skyscanner.entities { [DocumentID] public readonly Guid ID = Guid.NewGuid(); - + public Guid NodeID { get; set; } [JsonIgnoreAttribute] @@ -33,7 +33,8 @@ namespace ln.skyscanner.entities public string Name { get; private set; } = ""; public string HWAddress { get; set; } = ""; - public CIDR[] IPs { get; set; } = new CIDR[0]; + //public CIDR[] IPs { get; set; } = new CIDR[0]; + public IEnumerable IPs => SkyScanner.Instance.Entities.intfIPCollection.Select("interfaceID", ID); private NetworkInterface() { } @@ -48,9 +49,9 @@ namespace ln.skyscanner.entities { ip = ip.Host; - foreach (CIDR ifip in IPs) + foreach (IntfIP ifip in IPs) { - if (ifip.Contains(ip)) + if (ifip.IP.Host.Equals(ip)) return true; } return false; diff --git a/entities/Node.cs b/entities/Node.cs index 1918a2a..3a36c79 100644 --- a/entities/Node.cs +++ b/entities/Node.cs @@ -14,6 +14,7 @@ using System.Linq; using ln.types.odb; using Newtonsoft.Json; using LiteDB; +using ln.types.threads; namespace ln.skyscanner.entities { public class Node @@ -37,8 +38,10 @@ namespace ln.skyscanner.entities public string ProductLine { get; set; } - public IEnumerable Interfaces => SkyScanner.Instance.Entities.interfaceCollection.Select( intf => intf.NodeID.Equals(ID)); - public IEnumerable IPAdresses => Interfaces.SelectMany(intf => intf.IPs); + public IEnumerable Interfaces => Timing.Meassure("Node.Interfaces", + () => SkyScanner.Instance.Entities.interfaceCollection.Select("NodeID", ID) + ); + public IEnumerable IPAdresses => Timing.Meassure("Node.IPAddesses", () => Interfaces.SelectMany(intf => intf.IPs).Select((iip)=>iip.IP)); private HashSet uris = new HashSet(); diff --git a/entities/Subnet.cs b/entities/Subnet.cs index d453104..75233df 100644 --- a/entities/Subnet.cs +++ b/entities/Subnet.cs @@ -29,7 +29,7 @@ namespace ln.skyscanner.entities public bool AutoScan { get; set; } - public IEnumerable AttachedInterfaces => SkyEntities.interfaceCollection.Where((intf) => Network.Contains(intf.IPs)); + public IEnumerable AttachedInterfaces => SkyEntities.interfaceCollection.Where((intf) => Network.Contains(intf.IPs.Select((iip)=>iip.IP))); public IEnumerable AttachedNodes => AttachedInterfaces.Select(intf => intf.Node).Distinct(); private Subnet() diff --git a/http/NetworkApi.cs b/http/NetworkApi.cs index 9d500e3..051a996 100644 --- a/http/NetworkApi.cs +++ b/http/NetworkApi.cs @@ -48,8 +48,11 @@ namespace ln.skyscanner.http public HopMap.HopNode[] GetHopTable() { HopMap hopMap = new HopMap(); - foreach (Node node in GlobalNetwork.Nodes) - hopMap.AddNode(node, 0); + //foreach (Node node in GlobalNetwork.Nodes) + //hopMap.AddNode(node, 0); + + hopMap.AddNode(GlobalNetwork.FindNodeByIP(CIDR.Parse("10.10.10.2")),0); + return hopMap.HopNodes; } diff --git a/ln.skyscanner.csproj b/ln.skyscanner.csproj index 9282546..43b1797 100644 --- a/ln.skyscanner.csproj +++ b/ln.skyscanner.csproj @@ -74,6 +74,7 @@ +