diff --git a/SkyEntities.cs b/SkyEntities.cs index ddcd371..6019723 100644 --- a/SkyEntities.cs +++ b/SkyEntities.cs @@ -20,24 +20,20 @@ namespace ln.skyscanner public SkyScanner SkyScanner { get; } public string BasePath => Path.Combine(SkyScanner.BasePath, "entities"); - //public GlobalNetwork GlobalNetwork => DBGlobalNetwork.Root; - //public ODB DBGlobalNetwork { get; private set; } - public GlobalNetwork GlobalNetwork { get; private set; } - public LiteDatabase liteDatabase { get; private set; } - public LiteCollection nodeCollection { get; private set; } - public LiteCollection interfaceCollection { get; private set; } - public LiteCollection subnetCollection { get; private set; } + public ODB odDatabase { get; private set; } + public ODBCollection nodeCollection { get; private set; } + public ODBCollection interfaceCollection { get; private set; } + public ODBCollection subnetCollection { get; private set; } public SkyEntities(SkyScanner skyScanner) { SkyScanner = skyScanner; - //DBGlobalNetwork = new ODB(BasePath); - liteDatabase = new LiteDatabase(String.Format(BasePath, "entities.db")); - nodeCollection = liteDatabase.GetCollection("nodes"); - interfaceCollection = liteDatabase.GetCollection("interfaces"); - subnetCollection = liteDatabase.GetCollection("subnets"); + odDatabase = new ODB(BasePath); + nodeCollection = odDatabase.GetCollection(); + interfaceCollection = odDatabase.GetCollection(); + subnetCollection = odDatabase.GetCollection(); GlobalNetwork = new GlobalNetwork(this); } diff --git a/crawl/CrawledSubnet.cs b/crawl/CrawledSubnet.cs index 5ef2781..c55eda6 100644 --- a/crawl/CrawledSubnet.cs +++ b/crawl/CrawledSubnet.cs @@ -17,7 +17,7 @@ namespace ln.skyscanner.entities public class CrawledSubnet { [DocumentID] - Guid ID; + Guid ID = Guid.NewGuid(); public readonly CIDR Network; public String Name; diff --git a/crawl/Crawler.cs b/crawl/Crawler.cs index fda10a6..74486b4 100644 --- a/crawl/Crawler.cs +++ b/crawl/Crawler.cs @@ -168,6 +168,11 @@ namespace ln.skyscanner.crawl } } + public void EnsureSubnet(CIDR network) + { + FindSubnet(network); + } + public void Enqueue(JobDelegate job) { crawlThreadPool.Enqueue(job); diff --git a/crawl/service/Crawling.cs b/crawl/service/Crawling.cs deleted file mode 100644 index e73ba42..0000000 --- a/crawl/service/Crawling.cs +++ /dev/null @@ -1,64 +0,0 @@ -// /** -// * File: CrawlTest.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 System.Collections.Generic; -using System.Reflection; -using ln.types; - -namespace ln.skyscanner.crawl.tests -{ - public static class Crawling - { - - //public static bool Crawl(Crawl hostCrawl) - //{ - // hostCrawl.setState("ICMP"); - // if (!ICMP.IsReachable(hostCrawl.Host)) - // { - // return false; - // } - - // if (hostCrawl.AbortRequested) - // return false; - - // hostCrawl.setState("SSH"); - // if (SSH.CanConnect(hostCrawl.Host)) - // { - // // ToDo: Extract more details... - // } - - // if (hostCrawl.AbortRequested) - // return false; - - // hostCrawl.setState("SNMP"); - // if (SNMP.HasSNMP(hostCrawl.Host)) - // { - // if (hostCrawl.AbortRequested) - // return false; - - // hostCrawl.setState("RFC1213"); - // RFC1213.Check(hostCrawl.Host); - // } - - - - - // foreach (CIDR ip in hostCrawl.Host.IPAddresses) - // { - // if (ip.MaskWidth != 32) - // hostCrawl.Crawler.CrawlPool.GetSubnet(ip.Network); - // } - - // return true; - //} - - - } -} diff --git a/crawl/service/RFC1213.cs b/crawl/service/RFC1213.cs index 8c99b94..5c469a2 100644 --- a/crawl/service/RFC1213.cs +++ b/crawl/service/RFC1213.cs @@ -43,15 +43,13 @@ namespace ln.skyscanner.crawl.tests { try { - - ln.snmp.rfc1213.RFC1213.Interface[] interfaces = ln.snmp.rfc1213.RFC1213.GetInterfaces(snmp); if (interfaces.Length > 0) { crawl.Host.IPAddresses = interfaces.SelectMany(intf => intf.IPAddresses).ToArray(); crawl.Host.HWAddresses = interfaces.Select(intf => intf.HWAddr).ToArray(); - crawl.Host.SetHint("rfc1213.interfaces",interfaces.Select(intf => string.Format("{0};{1};{2}",intf.Name,intf.HWAddr,String.Join(",",intf.IPAddresses.Select(ip=>ip.ToString())).ToArray())).ToArray()); + crawl.Host.SetHint("rfc1213.interfaces",interfaces.Select(intf => string.Format("{0};{1};{2}",intf.Name,intf.HWAddr,String.Join(",",intf.IPAddresses.Select(ip=>ip.ToString())))).ToArray()); crawl.Host.SetHint("rfc1213", true); } } catch (TimeoutException) diff --git a/crawl/service/SNMP.cs b/crawl/service/SNMP.cs index 8189324..c2aa3aa 100644 --- a/crawl/service/SNMP.cs +++ b/crawl/service/SNMP.cs @@ -120,6 +120,7 @@ namespace ln.skyscanner.crawl.tests } } } + v3endpoint.Close(); } } return false; @@ -153,6 +154,7 @@ namespace ln.skyscanner.crawl.tests { } } + v2endpoint.Close(); } } return false; @@ -186,6 +188,7 @@ namespace ln.skyscanner.crawl.tests { } } + v1endpoint.Close(); } } return false; diff --git a/entities/GlobalNetwork.cs b/entities/GlobalNetwork.cs index 0cb15bc..fb37257 100644 --- a/entities/GlobalNetwork.cs +++ b/entities/GlobalNetwork.cs @@ -10,6 +10,7 @@ using ln.snmp.rfc1213; using ln.types.odb; using ln.types.sync; using ln.logging; +using System.Globalization; namespace ln.skyscanner.entities { @@ -20,14 +21,11 @@ namespace ln.skyscanner.entities [Unsynced] public SkyEntities SkyEntities { get; private set; } - public IEnumerable Subnets => subnets; - public IEnumerable Nodes => nodes; + public IEnumerable Subnets => SkyEntities.subnetCollection; + public IEnumerable Nodes => SkyEntities.nodeCollection; public HopMap DefaultHopMap { get; private set; } = new HopMap(); - private List nodes = new List(); - private List subnets = new List(); - private object _updateLock = new object(); public GlobalNetwork() @@ -39,52 +37,32 @@ namespace ln.skyscanner.entities SkyEntities = skyEntities; } + public void EnsureSubnet(CIDR network) + { + SkyScanner.Crawler.EnsureSubnet(network); + + Subnet subnet = SkyEntities.subnetCollection.Where(s => s.Network.Equals(network)).FirstOrDefault(); + if (subnet == null) + { + subnet = new Subnet(network); + SkyEntities.subnetCollection.Upsert(subnet); + } + + } + public Node GetNode(Guid id) { - //foreach (Node node in nodes) - //{ - // if (node.PersistenceID.Equals(persistenceID)) - // return node; - //} - //throw new KeyNotFoundException(); - - return SkyEntities.nodeCollection.FindOne(n => n.ID.Equals(id)); + return SkyEntities.nodeCollection.Select(id); } public Subnet FindSubnetForHost(CIDR host) { - //foreach (Subnet subnet in subnets) - //{ - // if (subnet.Network.Contains(host.Host)) - // return subnet; - //} - //return null; - - return SkyEntities.subnetCollection.FindOne(net => net.Network.Contains(host)); + return SkyEntities.subnetCollection.Select(net => net.Network.Contains(host)).FirstOrDefault(); } public IEnumerable FindHostsInSubnet(CIDR network) - { - //HashSet result = new HashSet(); - - //foreach (Node node in nodes.ToArray()) - //{ - // foreach (NetworkInterface networkInterface in node.Interfaces.ToArray()) - // { - // foreach (CIDR ip in networkInterface.IPs) - // { - // if (network.Contains(ip)) - // { - // result.Add(node); - // break; - // } - // } - // } - //} - - //return result; - - return SkyEntities.nodeCollection.Find(node => network.Contains(node.IPAdresses)); + { + return SkyEntities.nodeCollection.Select(node => network.Contains(node.IPAdresses)); } public IEnumerable FindNeighbors(Node node) @@ -104,7 +82,8 @@ namespace ln.skyscanner.entities } public Node FindNodeByIP(CIDR ip) { - return SkyEntities.nodeCollection.FindOne(node => node.IPAdresses.Contains(ip) || node.PrimaryIP.Equals(ip)); + ip = ip.Host; + return SkyEntities.nodeCollection.Select(node => ip.In(node.IPAdresses) || ip.In(node.PrimaryIP)).FirstOrDefault(); } [Unsynced] @@ -158,14 +137,23 @@ namespace ln.skyscanner.entities node.PrimaryMac = crawledHost.PrimaryHWAddr; } - //foreach (String si in crawledHost.GetHint("rfc1213.interfaces",) - //{ - // NetworkInterface networkInterface = node.GetInterface(intf.Name); - // if (networkInterface == null) - // networkInterface = new NetworkInterface(node, intf.Name); + foreach (String si in crawledHost.GetHint("rfc1213.interfaces",new string[0])) + { + String[] fields = si.Split(';'); - // networkInterface.IPs = intf.IPAddresses.ToArray(); - //} + NetworkInterface networkInterface = node.GetInterface(fields[0]); + if (networkInterface == null) + 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); + + } node.RemoveURI("ssh"); if (crawledHost.GetHint("ssh.port", -1) != -1) diff --git a/entities/NetworkInterface.cs b/entities/NetworkInterface.cs index a6f37b4..24d2da5 100644 --- a/entities/NetworkInterface.cs +++ b/entities/NetworkInterface.cs @@ -18,19 +18,20 @@ namespace ln.skyscanner.entities { public class NetworkInterface : Persistent { - [BsonId] - public Guid ID { get; private set; } + [DocumentID] + public readonly Guid ID = Guid.NewGuid(); public Guid NodeID { get; set; } [JsonIgnoreAttribute] public Node Node { - get => SkyScanner.Instance.Entities.nodeCollection.FindById(NodeID); + get => SkyScanner.Instance.Entities.nodeCollection[NodeID]; set => NodeID = value.ID; } public string Name { get; private set; } = ""; + public string HWAddress { get; set; } = ""; public CIDR[] IPs { get; set; } = new CIDR[0]; diff --git a/entities/Node.cs b/entities/Node.cs index 5094015..1918a2a 100644 --- a/entities/Node.cs +++ b/entities/Node.cs @@ -19,7 +19,7 @@ namespace ln.skyscanner.entities public class Node { [DocumentID] - public Guid ID { get; set; } + public Guid ID { get; set; } = Guid.NewGuid(); public CIDR PrimaryIP { get; set; } public String PrimaryMac { get; set; } @@ -37,7 +37,7 @@ namespace ln.skyscanner.entities public string ProductLine { get; set; } - public IEnumerable Interfaces => SkyScanner.Instance.Entities.interfaceCollection.Find( intf => intf.NodeID.Equals(ID)); + public IEnumerable Interfaces => SkyScanner.Instance.Entities.interfaceCollection.Select( intf => intf.NodeID.Equals(ID)); public IEnumerable IPAdresses => Interfaces.SelectMany(intf => intf.IPs); private HashSet uris = new HashSet(); @@ -83,7 +83,7 @@ namespace ln.skyscanner.entities public NetworkInterface GetInterface(String intfName) { - return SkyScanner.Instance.Entities.interfaceCollection.FindOne(intf => intf.NodeID.Equals(ID) && intf.Name.Equals(intfName)); + return SkyScanner.Instance.Entities.interfaceCollection.Where(intf => intf.NodeID.Equals(ID) && intf.Name.Equals(intfName)).FirstOrDefault(); } public bool HasInterface(string intfName) diff --git a/entities/Subnet.cs b/entities/Subnet.cs index 9e890ae..d453104 100644 --- a/entities/Subnet.cs +++ b/entities/Subnet.cs @@ -16,7 +16,10 @@ namespace ln.skyscanner.entities { public class Subnet : Persistent { - public GlobalNetwork GlobalNetwork { get; private set; } + public SkyEntities SkyEntities => SkyScanner.Instance.Entities; + + [DocumentID] + public Guid ID { get; private set; } = Guid.NewGuid(); public readonly CIDR Network; public String Name; @@ -26,32 +29,20 @@ namespace ln.skyscanner.entities public bool AutoScan { get; set; } - public NetworkInterface[] AttachedInterfaces => networkInterfaces.ToArray(); - - private HashSet networkInterfaces = new HashSet(); + public IEnumerable AttachedInterfaces => SkyEntities.interfaceCollection.Where((intf) => Network.Contains(intf.IPs)); + public IEnumerable AttachedNodes => AttachedInterfaces.Select(intf => intf.Node).Distinct(); private Subnet() { } - public Subnet(GlobalNetwork globalNetwork,CIDR cidr) + public Subnet(CIDR cidr) { - GlobalNetwork = globalNetwork; Network = cidr; Name = Network.ToString(); FirstSeen = DateTime.Now; } - public void AttachInterface(NetworkInterface networkInterface) - { - networkInterfaces.Add(networkInterface); - } - public void DetachInterface(NetworkInterface networkInterface) - { - networkInterfaces.Remove(networkInterface); - } - - public override string ToString() { return Network.ToString(); diff --git a/ln.skyscanner.csproj b/ln.skyscanner.csproj index e4faa68..9282546 100644 --- a/ln.skyscanner.csproj +++ b/ln.skyscanner.csproj @@ -63,7 +63,6 @@ - diff --git a/templates/static/network/hoptable.html b/templates/static/network/hoptable.html index fda7340..7af3bca 100644 --- a/templates/static/network/hoptable.html +++ b/templates/static/network/hoptable.html @@ -70,7 +70,7 @@ }); }); - skyapi().call("api/network","GetHopTable", [ node.PersistenceID ], function(neighbors){ + skyapi().call("api/network","GetHopTable", [ node.ID ], function(neighbors){ $("#nodeTable").DataTable().clear().rows.add( neighbors ).draw(); }); diff --git a/templates/static/network/index.html b/templates/static/network/index.html index eff0fe8..9475247 100644 --- a/templates/static/network/index.html +++ b/templates/static/network/index.html @@ -70,7 +70,7 @@ }); }); - skyapi().call("api/network","GetNeighbors", [ node.PersistenceID ], function(neighbors){ + skyapi().call("api/network","GetNeighbors", [ node.ID ], function(neighbors){ $("#nodeTable").DataTable().clear().rows.add( neighbors ).draw(); });