From b71f5cc2795bfb1a869badfaa62addf431e89310 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Wed, 3 Apr 2019 09:19:37 +0200 Subject: [PATCH] WIP --- Program.cs | 22 ++++++++--- SkyEntities.cs | 12 +++--- crawl/Crawler.cs | 2 +- entities/{IntfIP.cs => ConfiguredIP.cs} | 12 ++---- entities/GlobalNetwork.cs | 52 +++++++++++-------------- entities/NetworkInterface.cs | 27 +++---------- entities/Node.cs | 8 +++- entities/Subnet.cs | 7 +++- ln.skyscanner.csproj | 2 +- 9 files changed, 66 insertions(+), 78 deletions(-) rename entities/{IntfIP.cs => ConfiguredIP.cs} (69%) diff --git a/Program.cs b/Program.cs index 8231b83..5f0ec87 100644 --- a/Program.cs +++ b/Program.cs @@ -15,6 +15,7 @@ using ln.skyscanner.entities; using ln.types.threads; using ln.types.net; using ln.types.odb; +using ln.types.odb.index; namespace ln.skyscanner { @@ -38,16 +39,25 @@ namespace ln.skyscanner skyScanner.Start(); return; - foreach (IntfIP iip in SkyScanner.Instance.Entities.intfIPCollection.Select("Network", Network4.Parse("10.255.9.16/29"))) + Node coreNode = skyScanner.Entities.GlobalNetwork.FindNodeByIP(IPv4.Parse("10.10.10.2")); + Console.WriteLine("Core Node: {0}", coreNode); + + Node coreNode2 = skyScanner.Entities.GlobalNetwork.FindNodeByIP(IPv4.Parse("10.255.7.129")); + Console.WriteLine("Core Node (alt): {0}", coreNode2); + + Query nodeByIpQuery = Query.Equals("Interfaces[].ConfiguredIPs[].IP", IPv4.Parse("10.255.7.129")); + IEnumerable qnodes = skyScanner.Entities.nodeCollection.Select(nodeByIpQuery); + + IEnumerable neighbors = skyScanner.Entities.GlobalNetwork.FindNeighbors(coreNode); + + foreach (Node neigh in neighbors) { - Console.WriteLine("INTF-IP ():{0}", iip); + Console.WriteLine("Neighbor: {0}", neigh); } - foreach (IntfIP iip in SkyScanner.Instance.Entities.intfIPCollection) - { - Console.WriteLine("INTF-IP: {0}", iip); - } + SkyScanner.Instance.Stop(); + return; IEnumerable nodes = skyScanner.Entities.nodeCollection; foreach (Node node in nodes) diff --git a/SkyEntities.cs b/SkyEntities.cs index 530a275..dd72840 100644 --- a/SkyEntities.cs +++ b/SkyEntities.cs @@ -23,8 +23,8 @@ namespace ln.skyscanner public GlobalNetwork GlobalNetwork { get; private set; } 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 interfaceCollection { get; private set; } + //public ODBCollection intfIPCollection { get; private set; } public ODBCollection subnetCollection { get; private set; } public SkyEntities(SkyScanner skyScanner) @@ -33,15 +33,13 @@ namespace ln.skyscanner odDatabase = new ODB(BasePath); nodeCollection = odDatabase.GetCollection(); - //interfaceCollection = odDatabase.GetCollection(); - //intfIPCollection = odDatabase.GetCollection(); subnetCollection = odDatabase.GetCollection(); nodeCollection.EnsureIndex("PrimaryIP"); + nodeCollection.EnsureIndex("Interfaces[].ConfiguredIPs[].IP"); + nodeCollection.EnsureIndex("Interfaces[].ConfiguredIPs[].Network"); - //interfaceCollection.EnsureIndex("NodeID"); - //intfIPCollection.EnsureIndex("interfaceID"); - //intfIPCollection.EnsureIndex("Network"); + subnetCollection.EnsureIndex("Network"); GlobalNetwork = new GlobalNetwork(this); } diff --git a/crawl/Crawler.cs b/crawl/Crawler.cs index c14d189..4d91a4e 100644 --- a/crawl/Crawler.cs +++ b/crawl/Crawler.cs @@ -213,7 +213,7 @@ namespace ln.skyscanner.crawl } public CrawledSubnet FindSubnet(Network4 network) { - CrawledSubnet sn = CrawledSubnets.Where(subnet => subnet.Network.Equals(network)).FirstOrDefault(); + CrawledSubnet sn = CrawledSubnets.Select("Network", network).FirstOrDefault(); if (sn == null) { Logging.Log(LogLevel.INFO, "Crawler adds new subnet: {0}",network); diff --git a/entities/IntfIP.cs b/entities/ConfiguredIP.cs similarity index 69% rename from entities/IntfIP.cs rename to entities/ConfiguredIP.cs index 56cb725..4ea1ace 100644 --- a/entities/IntfIP.cs +++ b/entities/ConfiguredIP.cs @@ -14,25 +14,19 @@ using Newtonsoft.Json; using ln.types.net; namespace ln.skyscanner.entities { - public class IntfIP + public class ConfiguredIP { [DocumentID] public readonly Guid ID = Guid.NewGuid(); - Guid interfaceID = Guid.Empty; - - [JsonIgnore] - public NetworkInterface NetworkInterface => SkyScanner.Instance.Entities.interfaceCollection.Select(interfaceID); - public IPv4 IP { get; private set; } public Network4 Network { get; private set; } - public IntfIP() + public ConfiguredIP() { } - public IntfIP(NetworkInterface networkInterface,IPv4 ip,Network4 network) + public ConfiguredIP(IPv4 ip,Network4 network) { - interfaceID = networkInterface.ID; IP = ip; Network = network; } diff --git a/entities/GlobalNetwork.cs b/entities/GlobalNetwork.cs index a032cf0..1bcdcef 100644 --- a/entities/GlobalNetwork.cs +++ b/entities/GlobalNetwork.cs @@ -65,25 +65,29 @@ namespace ln.skyscanner.entities public IEnumerable FindHostsInSubnet(Network4 network) { - IntfIP[] intfIPs = SkyEntities.intfIPCollection.Select("Network",network).ToArray(); - NetworkInterface[] networkInterfaces = intfIPs.Select((iip) => iip.NetworkInterface).ToArray(); - - Node[] nodes = SkyEntities.nodeCollection.Select(networkInterfaces.Select((ni) => new ODBGuid(ni.NodeID))).ToArray(); - - return nodes; + Query nodeByIpQuery = Query.Equals("Interfaces[].ConfiguredIPs[].Network", network); + return SkyEntities.nodeCollection.Select(nodeByIpQuery); } public IEnumerable FindNeighbors(Node node) { HashSet nodes = new HashSet(); - //foreach (Network4 network in node.Networks) - //{ - // foreach (Node neighbor in FindHostsInSubnet(network)) - // { - // nodes.Add(neighbor); - // } - //} + if (node.Interfaces.Count > 0) + { + foreach (Network4 network in node.Networks) + { + foreach (Node neighbor in FindHostsInSubnet(network)) + nodes.Add(neighbor); + } + } + else + { + Subnet subnet = FindSubnetForIP(node.PrimaryIP); + IEnumerable nodelist = FindHostsInSubnet(subnet.Network); + foreach (Node n in nodelist) + nodes.Add(n); + } return nodes; } @@ -98,24 +102,14 @@ namespace ln.skyscanner.entities } return null; } - //public Node FindNodeByIP(CIDR ip) - //{ - // ip = ip.Host; - // /* - // * ToDo: Alternative Implementation - // * a) higher performance (use indexing) - // * b) fix bug: Node not found if no interface ips exist... - // */ - // IntfIP _iip = SkyEntities.intfIPCollection.Where((iip) => iip.IP.Host.Equals(ip)).FirstOrDefault(); - // return _iip?.NetworkInterface?.Node; - //} public Node FindNodeByIP(IPv4 ip) { Node node = SkyEntities.nodeCollection.SelectOne("PrimaryIP", ip); if (node == null) { - + Query nodeByIpQuery = Query.Equals("Interfaces[].ConfiguredIPs[].IP", ip); + node = SkyEntities.nodeCollection.Select(nodeByIpQuery).FirstOrDefault(); } return node; } @@ -193,7 +187,7 @@ namespace ln.skyscanner.entities NetworkInterface networkInterface = node.GetInterface(fields[0]); if (networkInterface == null) { - networkInterface = new NetworkInterface(node, fields[0]); + networkInterface = new NetworkInterface(fields[0]); node.Interfaces.Add(networkInterface); } @@ -202,7 +196,7 @@ namespace ln.skyscanner.entities IPv4[][] crawledIPs = fields[2].Split(',').Where((sip) => !String.Empty.Equals(sip)) .Select((sip) => sip.Split('/').Select(ip => IPv4.Parse(ip)).ToArray()).ToArray(); - IntfIP[] currentIPs = networkInterface.ConfiguredIPs.ToArray(); + ConfiguredIP[] currentIPs = networkInterface.ConfiguredIPs.ToArray(); foreach (IPv4[] crawledIP in crawledIPs) { @@ -212,12 +206,12 @@ namespace ln.skyscanner.entities break; if (n == currentIPs.Length) { - IntfIP miss = new IntfIP(networkInterface, crawledIP[0], new Network4(crawledIP[0], crawledIP[1])); + ConfiguredIP miss = new ConfiguredIP(crawledIP[0], new Network4(crawledIP[0], crawledIP[1])); networkInterface.ConfiguredIPs.Add(miss); } } - foreach (IntfIP iip in currentIPs) + foreach (ConfiguredIP iip in currentIPs) { IPv4 ip = iip.IP; diff --git a/entities/NetworkInterface.cs b/entities/NetworkInterface.cs index 5e68cd8..6fd7407 100644 --- a/entities/NetworkInterface.cs +++ b/entities/NetworkInterface.cs @@ -22,34 +22,23 @@ namespace ln.skyscanner.entities { [DocumentID] public readonly Guid ID = Guid.NewGuid(); - - public Guid NodeID { get; set; } - - [JsonIgnoreAttribute] - public Node Node - { - get => SkyScanner.Instance.Entities.nodeCollection[NodeID]; - set => NodeID = value.ID; - } public string Name { get; private set; } = ""; public string HWAddress { get; set; } = ""; - public List ConfiguredIPs { get; private set; } = new List(); - //public IEnumerable ConfiguredIPs => SkyScanner.Instance.Entities.intfIPCollection.Select("interfaceID", ID); + public List ConfiguredIPs { get; private set; } = new List(); private NetworkInterface() { } - public NetworkInterface(Node node,String name) + public NetworkInterface(String name) { - Node = node; Name = name; } public bool HasIP(IPv4 ip) { - foreach (IntfIP ifip in ConfiguredIPs) + foreach (ConfiguredIP ifip in ConfiguredIPs) { if (ifip.IP.Equals(ip)) return true; @@ -60,11 +49,7 @@ namespace ln.skyscanner.entities public override int GetHashCode() { - if (Node == null) - Logging.Log(LogLevel.WARNING, "NetworkInterface without Node detected. ID={2} NodeID={0} Name={1}",NodeID,Name,ID); - else - return Node.GetHashCode() ^ Name.GetHashCode(); - return 0; + return Name.GetHashCode(); } public override bool Equals(object obj) @@ -72,14 +57,14 @@ namespace ln.skyscanner.entities if (obj is NetworkInterface) { NetworkInterface networkInterface = obj as NetworkInterface; - return object.Equals(Node,networkInterface.Node) && object.Equals(Name,networkInterface.Name); + return object.Equals(Name,networkInterface.Name); } return false; } public override string ToString() { - return string.Format("[NetworkInterface ID={0} NodeID={1} Name={2}]",ID,NodeID,Name); + return string.Format("[NetworkInterface ID={0} Name={1}]",ID,Name); } } diff --git a/entities/Node.cs b/entities/Node.cs index 6ed4db0..b5eb1e0 100644 --- a/entities/Node.cs +++ b/entities/Node.cs @@ -41,8 +41,12 @@ namespace ln.skyscanner.entities public List Interfaces { get; private set; } = new List(); + [JsonIgnore] public IEnumerable IPAdresses => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.IP)); - public IEnumerable Networks => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.Network)); + [JsonIgnore] + public IEnumerable Networks => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.Network)).Distinct(); + [JsonIgnore] + public IEnumerable Subnets => SkyScanner.Instance.Entities.subnetCollection.Select("Network", Networks.Select(net => net.Network)); private HashSet uris = new HashSet(); @@ -87,7 +91,7 @@ namespace ln.skyscanner.entities public NetworkInterface GetInterface(String intfName) { - return SkyScanner.Instance.Entities.interfaceCollection.Where(intf => intf.NodeID.Equals(ID) && intf.Name.Equals(intfName)).FirstOrDefault(); + return Interfaces.Where(intf => intf.Name.Equals(intfName)).FirstOrDefault(); } public bool HasInterface(string intfName) diff --git a/entities/Subnet.cs b/entities/Subnet.cs index 99e0057..c14e8b6 100644 --- a/entities/Subnet.cs +++ b/entities/Subnet.cs @@ -13,6 +13,7 @@ using ln.types.odb; using System.Collections.Generic; using System.Linq; using ln.types.net; +using Newtonsoft.Json; namespace ln.skyscanner.entities { public class Subnet : Persistent @@ -30,8 +31,10 @@ namespace ln.skyscanner.entities public bool AutoScan { get; set; } - public IEnumerable AttachedInterfaces => SkyEntities.interfaceCollection.Where((intf) => Network.Contains(intf.ConfiguredIPs.Select((iip)=>iip.IP))); - public IEnumerable AttachedNodes => AttachedInterfaces.Select(intf => intf.Node).Distinct(); + [JsonIgnore] + public IEnumerable AttachedInterfaces => throw new NotImplementedException(); + [JsonIgnore] + public IEnumerable AttachedNodes => throw new NotImplementedException(); private Subnet() { diff --git a/ln.skyscanner.csproj b/ln.skyscanner.csproj index 282ff0c..805e177 100644 --- a/ln.skyscanner.csproj +++ b/ln.skyscanner.csproj @@ -75,7 +75,7 @@ - +