⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 networkdiscovery.java

📁 监控大型网络的软件。能够自动发现拓扑结构
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
																	 HashMap swportidMap,																	 HashSet spanTreeBlocked,																	 List trunkVlan,																	 Map dataStructs,																	 List foundGwports,																	 Set visitedNodeSet,																	 int level,																	 boolean DEBUG_OUT,																	 HashSet boksGwSet,																	 Map swportGwVlanMap,																	 HashMap boksName)	{		if (level > 60) {			outl("<font color=\"red\">ERROR! Level is way too big...</font>");			return false;		}		String pad = "";		for (int i=0; i<level; i++) pad+="        ";		if (DEBUG_OUT) outl(pad+"><font color=\"green\">[ENTER]</font> Now at node("+boksid+"): <b>" + boksName.get(boksid) + "</b>, came from("+fromid+"): " + boksName.get(fromid) + ", vlan: " + vlan + " cameFromTrunk: <b>"+cameFromTrunk+"</b> level: <b>" + level + "</b>");		// Check for loops, we only need to traverse the same unit once		if (visitedNodeSet.contains(boksid)) {			if (DEBUG_OUT) outl(pad+"><font color=\"red\">[RETURN]</font> NOTICE: Found loop, from("+fromid+"): " + boksName.get(fromid) + ", boksid("+boksid+"): " + boksName.get(boksid) + ", vlan: " + vlan + ", level: " + level + "");			return false;		}		visitedNodeSet.add(boksid);		// Now we now this VLAN is active on this box, the first thing we do is to traverse each 		// non-trunk port and mark the direction of the link.		boolean isActiveVlan = false;		if (nontrunkVlan.containsKey(boksid+":"+vlan)) {			String key = boksid+":"+vlan;			HashMap nontrunkMap = (HashMap)nontrunkVlan.get(key);						Iterator iter = nontrunkMap.values().iterator();			while (iter.hasNext()) {				HashMap hm = (HashMap)iter.next();				String toid = (String)hm.get("to_netboxid");				String swportid = (String)hm.get("swportid");				String swportidBack = null;								// We're not going to follow the link back anyway				if (toid.equals(fromid)) continue;								if (boksGwSet.contains(toid)) {					// Link to GW, we will not traverse, check if this VLAN is active on this swport					if (swportGwVlanMap.containsKey(swportid+":"+vlan)) {						// OK, the link now becomes 'o' (opp = up)						String[] rVlan = {							swportid,							String.valueOf(vlanid),							(setDirection)?"o":"u"						};						if (DEBUG_OUT) outl(pad+"--><b>[NON-TRUNK-GW]</b> Running on non-trunk, vlan: <b>" + vlan + "</b>, boksid("+boksid+"): <b>" + boksName.get(boksid) + "</b>, to("+toid+"): <b>" + boksName.get(toid) + "</b> level: <b>" + level + "</b> (<b>"+rVlan[0]+"</b>)");						trunkVlan.add(rVlan);						isActiveVlan = true;						foundGwports.add(swportGwVlanMap.get(swportid+":"+vlan));					}					continue;				}				// Before we traverse the link down, we check which VLAN is on				// the port on the other end.  If the VLAN on the other end is				// not identical to the one we are traversing, this VLAN value				// must be rewritten to the VLAN we are traversing at the				// moment.  This goes for all ports we find below this level.				String vlanBack = null;				Map nbvlanMap = (Map)dataStructs.get("nbvlanMap"); // Maps id:toid -> vlan				if (swportidMap.containsKey(swportid)) {					// Get the swport record and extract the to_swportid field from it					Map mySwrec = (Map)swportidMap.get(swportid);					swportidBack = (String)mySwrec.get("to_swportid");					Map swrecBack = (Map)swportidMap.get(swportidBack);					if (swrecBack != null) {						vlanBack = (String)swrecBack.get("vlan");					}				}				if (vlanBack == null) {					// Just use ids					vlanBack = (String)nbvlanMap.get(toid+":"+boksid);				}				// It could be that the other end has a different VLAN on the port that leads back to this unit; then we will rewrite the VLAN value.				// But only if we come from above, traversing downwards, i.e. if setDirection is true.				if (setDirection && vlan != 1 && nontrunkVlan.containsKey(toid+":"+vlanBack)) {					HashMap nontrunkMapBack = (HashMap)nontrunkVlan.get(toid+":"+vlanBack);					String idBack = (nontrunkMapBack != null && nontrunkMapBack.containsKey(swportid)) ? swportid : boksid;					if (nontrunkMapBack != null && nontrunkMapBack.containsKey(idBack)) {						// We've found link back on VLAN (1 or vlanBack), then we just switch it						nontrunkVlan.remove(toid+":"+vlanBack);						nontrunkVlan.put(toid+":"+vlan, nontrunkMapBack);						// Also switch it in activeVlan						Map map = (Map)activeVlan.get(toid);						if (map != null && map.containsKey(new Integer(vlanBack))) {							Collection c;							if ((c = (Collection)map.get(new Integer(vlan))) == null) map.put(new Integer(vlan), c = new HashSet());							c.addAll((Collection)map.remove(new Integer(vlanBack)));						}						if (DEBUG_OUT) outl(pad+"--><b>[REPLACE]</b> Replaced vlan: <b>1</b> with vlan: <b>" + vlan + "</b>, for boks("+toid+"): <b>" + boksName.get(toid) + "</b> level: <b>" + level + "</b>");					}				}				// Now we can at least add that the direction is down here.				String[] rVlan = {					swportid,					String.valueOf(vlanid),					(setDirection)?"n":"u" // n = ned = down				};				trunkVlan.add(rVlan);				isActiveVlan = true;				if (DEBUG_OUT) outl(pad+"--><b>[NON-TRUNK]</b> Running on non-trunk, vlan: <b>" + vlan + "</b>, boksid("+boksid+"): <b>" + boksName.get(boksid) + "</b>, to("+toid+"): <b>" + boksName.get(toid) + "</b> level: <b>" + level + "</b> (<b>"+rVlan[0]+"</b>)");				// Then we traverse the link, the return value is of no importance here				vlanTraverseLink(vlan, vlanid, boksid, toid, false, setDirection, nontrunkVlan, allowedVlan, activeVlan, swportidMap, spanTreeBlocked, trunkVlan, dataStructs, foundGwports, visitedNodeSet, level+1, DEBUG_OUT, boksGwSet, swportGwVlanMap, boksName);				// Then check if we can find the link back, and if so it is marked with direction 'o' (up)				if (swportidBack == null) {					String keyBack = toid+":"+vlan;					HashMap nontrunkMapBack = (HashMap)nontrunkVlan.get(keyBack);					if (nontrunkMapBack == null) {						// The box we are looking at has no non-trunk links, therefore we can move on						if (DEBUG_OUT) outl(pad+"---->ERROR! No non-trunk links found for vlan: " + vlan + ", toid("+toid+"): " + boksName.get(toid) + ", level: " + level + "");						continue;					}										HashMap hmBack = (HashMap)nontrunkMapBack.get(boksid);					if (hmBack == null) {						// The link back is missing						if (DEBUG_OUT) outl(pad+"---->ERROR! Link back not found for vlan: " + vlan + ", toid("+toid+"): " + boksName.get(toid) + ", level: " + level + "");						continue;					}										swportidBack = (String)hmBack.get("swportid");				}								// Now we can mark the VLAN as active also on the link back				String[] rVlanBack = {					swportidBack,					String.valueOf(vlanid),					(setDirection)?"o":"u"				};				trunkVlan.add(rVlanBack);				if (DEBUG_OUT) outl(pad+"--><b>[NON-TRUNK]</b> Link back running on non-trunk OK (<b>"+rVlanBack[0]+"</b>)");			}		}		// Check if the VLAN is active on any non-trunk ports; we must add them		// without to_netboxid		{			Map map = (Map)activeVlan.get(boksid);			if (map != null && map.containsKey(new Integer(vlan)) ) {				isActiveVlan = true;				// Create trunkVlan records for all ports				for (Iterator it = ((Collection)map.get(new Integer(vlan))).iterator(); it.hasNext();) {					String swportid = (String)it.next();					String[] rVlan = {						swportid,						String.valueOf(vlanid),						(setDirection)?"n":"u"					};					trunkVlan.add(rVlan);				}			}		}		// Check if there are any trunks on this unit that allow this VLAN		HashMap boksAllowedMap = (HashMap)allowedVlan.get(boksid);		if (boksAllowedMap == null) {			if (cameFromTrunk) {				if (fromid == null) {					// This is the first unit, then this can actually happen					if (DEBUG_OUT) outl(pad+">ERROR! AllowedVlan not found for vlan: " + vlan + ", boksid("+boksid+"): " + boksName.get(boksid) + ", level: " + level + "");				} else {					if (DEBUG_OUT) outl(pad+"><font color=\"red\">ERROR! Should not happen, AllowedVlan not found for vlan: " + vlan + ", boksid("+boksid+"): " + boksName.get(boksid) + ", level: " + level + "</font>");				}			}			if (DEBUG_OUT) outl(pad+"><font color=\"red\">[RETURN]</font> from node("+boksid+"): " + boksName.get(boksid) + ", isActiveVlan: <b>" + isActiveVlan+"</b>, no trunks to traverse.");			// Return true if there are non-trunks on this box			// This should only matter if there is a non-trunk link up to the GW			return isActiveVlan;		}		boolean isActiveVlanTrunk = false;		Iterator iter = boksAllowedMap.values().iterator();		while (iter.hasNext()) {			HashMap hm = (HashMap)iter.next();			String hexstr = (String)hm.get("hexstring");			String toid = (String)hm.get("to_netboxid");			String swportid = (String)hm.get("swportid");			String swportidBack;			// We're not following the link back anyway			if (toid.equals(fromid)) continue;			if (boksGwSet.contains(toid)) {				if (swportGwVlanMap.containsKey(swportid+":"+vlan)) {					if (!isAllowedVlan(hexstr, vlan)) {						if (DEBUG_OUT) outl(pad+"--><font color=\"red\">ERROR, running on trunk to GW, but isAllowedVlan is false, vlan: <b>" + vlan + "</b>, boksid("+boksid+"): <b>" + boksName.get(boksid) + "</b>, to("+toid+"): <b>" + boksName.get(toid) + "</b> level: <b>" + level + "</b> (<b>"+swportid+"</b>)");						continue;					}					// This is a link to a GW, it then becomes direction 'o' (up), and we don't traverse					String[] tvlan = {						swportid,						String.valueOf(vlanid),						"o"					};					if (DEBUG_OUT) outl(pad+"--><b>[TRUNK-GW]</b> Running on trunk, vlan: <b>" + vlan + "</b>, boksid("+boksid+"): <b>" + boksName.get(boksid) + "</b>, to("+toid+"): <b>" + boksName.get(toid) + "</b> level: <b>" + level + "</b> (<b>"+tvlan[0]+"</b>)");					trunkVlan.add(tvlan);					isActiveVlanTrunk = true;					foundGwports.add(swportGwVlanMap.get(swportid+":"+vlan));				}				continue;			}			// Now we need the record for the link back			{				HashMap boksAllowedMapBack = (HashMap)allowedVlan.get(toid);				if (boksAllowedMapBack == null) {					if (DEBUG_OUT) outl(pad+">ERROR! AllowedVlan not found for vlan: " + vlan + ", toid("+toid+"): " + boksName.get(toid) + ", level: " + level + "");					continue;				}				HashMap hmBack = (HashMap)boksAllowedMapBack.get(boksid);				if (hmBack == null) {					// Linken tilbake mangler					if (DEBUG_OUT) outl(pad+"---->ERROR! Link back not found for vlan: " + vlan + ", toid("+toid+"): " + boksName.get(toid) + ", level: " + level + "");					continue;				}				swportidBack = (String)hmBack.get("swportid");				String hexstrBack = (String)hmBack.get("hexstring");				if (hexstrBack == null) {					// The link back is missing					if (DEBUG_OUT) outl(pad+"---->ERROR! hexstring back not found for vlan: " + vlan + ", toid("+toid+"): " + boksName.get(toid) + ", level: " + level + "");					continue;				}				if (hexstr == null) {					// The link back is missing					if (DEBUG_OUT) outl(pad+"---->ERROR! hexstring not found for vlan: " + vlan + ", toid("+toid+"): " + boksName.get(toid) + ", level: " + level + "");					continue;				}				// If one of the link partners do not allow this VLAN, we don't follow this link				if (!isAllowedVlan(hexstr, vlan) || !isAllowedVlan(hexstrBack, vlan)) {					if (DEBUG_OUT) outl(pad+"----><b>NOT</b> allowed to("+toid+"): " + boksName.get(toid) + "");					continue;				}			}			if (DEBUG_OUT) outl(pad+"----><b>Allowed</b> to("+toid+"): " + boksName.get(toid) + ", visiting...");			// Check whether the link has been blocked by spanning tree			if (spanTreeBlocked.contains(swportid+":"+vlan) || spanTreeBlocked.contains(swportidBack+":"+vlan)) {				// Yep, add the VLAN with blocking in both ends				String[] tvlan = {					swportid,					String.valueOf(vlanid),					"b"				};				String[] tvlanBack = {					swportidBack,					String.valueOf(vlanid),					"b"				};				trunkVlan.add(tvlan);				trunkVlan.add(tvlanBack);				isActiveVlanTrunk = true;				if (DEBUG_OUT) outl(pad+"------><font color=\"purple\">Link blocked by spanning tree, boksid("+boksid+"): <b>"+boksName.get(boksid)+"</b> toid:("+toid+"): <b>"+ boksName.get(toid) + "</b>, vlan: <b>" + vlan + "</b>, level: <b>" + level + "</b></font>");				continue;			}			//if (DEBUG_OUT) outl(pad+"---->Visiting("+toid+"): " + boksName.get(toid) + "");			// Used to avoid dupes			//visitNode.add(boksid);			if (vlanTraverseLink(vlan, vlanid, boksid, toid, true, setDirection, nontrunkVlan, allowedVlan, activeVlan, swportidMap, spanTreeBlocked, trunkVlan, dataStructs, foundGwports, visitedNodeSet, level+1, DEBUG_OUT, boksGwSet, swportGwVlanMap, boksName)) {				// We now know that the VLAN runs on this trunk				String[] tvlan = {					swportid,					String.valueOf(vlanid),					(setDirection)?"n":"u"				};				String[] tvlanBack = {					swportidBack,					String.valueOf(vlanid),					(setDirection)?"o":"u"				};				trunkVlan.add(tvlan);				trunkVlan.add(tvlanBack);				isActiveVlanTrunk = true;				if (DEBUG_OUT) outl(pad+"---->Returned active on trunk, vlan: <b>" + vlan + "</b>, boksid("+boksid+"): <b>" + boksName.get(boksid) + "</b>, to("+toid+"): <b>" + boksName.get(toid) + "</b> level: <b>" + level + "</b> (<b>"+tvlan[0]+" '"+tvlan[2]+"' / "+tvlanBack[0]+" '"+tvlanBack[2]+"'</b>)");			} else {				if (DEBUG_OUT) outl(pad+"---->Returned NOT active on trunk, vlan: <b>" + vlan + "</b>, boksid("+boksid+"): <b>" + boksName.get(boksid) + "</b>, to("+toid+"): <b>" + boksName.get(toid) + "</b> level: <b>" + level + "</b>");			}			//visitNode.remove(boksid);		}		// We will return if the VLAN is active on this box		// First we check if the VLAN is active on any of the trunks		if (isActiveVlanTrunk) {			if (DEBUG_OUT) outl(pad+"><font color=\"red\">[RETURN]</font> from node("+boksid+"): " + boksName.get(boksid) + ", <b>ActiveVlan on trunk</b>");			return tr

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -