MainFrm.cpp
上传用户:hbytqc8
上传日期:2014-07-31
资源大小:527k
文件大小:30k
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "NetMap.h"
- #include "NetMapDoc.h"
- #include "MainFrm.h"
- #include "snmp_pp.h"
- #include "Show.h"
- #include "Modify.h"
- #include "Input.h"
- #include "AddVlan.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_SCAN, OnScan)
- ON_COMMAND(ID_GRAPH, OnGraph)
- ON_COMMAND(ID_SHOWINFORMATION, OnShowinformation)
- ON_COMMAND(ID_NODE_MODIFY, OnNodeModify)
- ON_COMMAND(ID_VLAN, OnVlan)
- ON_COMMAND(ID_PORT, OnPort)
- ON_COMMAND(ID_ADD_MAP, OnAddMap)
- ON_COMMAND(ID_VALN_TOPO, OnValnTopo)
- ON_COMMAND(ID_ADD_VLAN, OnAddVlan)
- ON_COMMAND(ID_DELETE_VLAN, OnDeleteVlan)
- ON_COMMAND(ID_PORT_MORE, OnPortMore)
- ON_COMMAND(ID_TO_VLAN, OnToVlan)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
-
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- //获得当前目录;
- /* CurrDir=_pgmptr;
- CurrDir.MakeReverse();
- int path=CurrDir.Find('\');
- CurrDir.MakeReverse();
- CurrDir=CurrDir.Left(CurrDir.GetLength()-path);*/
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- cs.x=1;
- cs.y=1;
- cs.cx=800;
- cs.cy=750;
- return TRUE;
- }
- BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
- {
- BOOL Result=v.CreateStatic(this,1,2,WS_CHILD | WS_VISIBLE);
- ASSERT(Result);
- h.CreateStatic(&v,2,1,WS_CHILD | WS_VISIBLE,v.IdFromRowCol(0,1));
- v.CreateView(0,0,RUNTIME_CLASS(CMapView),CSize(210,600),pContext);
- h.CreateView(0,0,RUNTIME_CLASS(CNetMapView),CSize(310,300),pContext);
- h.CreateView(1,0,RUNTIME_CLASS(CVlanView),CSize(310,300),pContext);
- //v.SetActivePane(0,0,NULL);
- return Result;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- CString CMainFrame::FindVlan(int id)
- {
- char ch[4];
- CString str;
- itoa(id,ch,10);
- str=ch;
- HTREEITEM Temp=pListView->m_vlan.GetRootItem();
- if (Temp!=NULL)
- {
- Temp=pListView->m_vlan.GetNextItem(Temp,TVGN_CHILD);
- Vlan * p=(Vlan *)pListView->m_vlan.GetItemData(Temp);
- if (p->Id==str)
- return p->Name;
- Temp=pListView->m_vlan.GetNextItem(Temp,TVGN_NEXT);
- while (Temp!=NULL)
- {
- p=(Vlan *)pListView->m_vlan.GetItemData(Temp);
- if (p->Id==str)
- return p->Name;
- Temp=pListView->m_vlan.GetNextItem(Temp,TVGN_NEXT);
- }
-
- }
- return "";
- }
- CString CMainFrame::HexTo01(CString &s)
- {
- CString str="";
- CString key,value;
- s.Replace(" ","");
- TempMap.SetAt("F","1111");
- TempMap.SetAt("E","1110");
- TempMap.SetAt("D","1101");
- TempMap.SetAt("C","1100");
- TempMap.SetAt("B","1011");
- TempMap.SetAt("A","1010");
- TempMap.SetAt("9","1001");
- TempMap.SetAt("8","1000");
- TempMap.SetAt("7","0111");
- TempMap.SetAt("6","0110");
- TempMap.SetAt("5","0101");
- TempMap.SetAt("4","0100");
- TempMap.SetAt("3","0011");
- TempMap.SetAt("2","0010");
- TempMap.SetAt("1","0001");
- TempMap.SetAt("0","0000");
- for(int i=0;i<s.GetLength();i++)
- {
- key=s[i];
- TempMap.Lookup(key,value);
- str+=value;
- }
- return str;
- }
- HTREEITEM CMainFrame::FindNode(HTREEITEM hSubRoot,const CString &text)
- {
- if (hSubRoot==NULL)
- return NULL;
- //判断是否符合条件,如是,返回该节点的句柄;
- if (pListView->m_map.GetItemText(hSubRoot)==text)
- return hSubRoot;
- hSubRoot=pListView->m_map.GetChildItem(hSubRoot);
- do
- {
- HTREEITEM ht;
- //递归调用FindNode;
- if ((ht=FindNode(hSubRoot,text)) !=NULL)
- return ht;
- } while ((hSubRoot=pListView->m_map.GetNextSiblingItem(hSubRoot)) !=NULL);
- return NULL;
- }
- bool CMainFrame::Findport(CString &port,NodeData *p)
- {
- for (int i=0;i<(p->m_neighbor.GetSize());i++)
- {
- // (p->m_neighbor).GetAt[i];
- if (p->m_neighbor.GetAt(i).localport==port)
- return true;
- }
- return false;
- }
- //CDP表项中的IP地址,需要转换;
- CString CMainFrame::HtoIP(CString Hex)
- {
-
-
- char ch[4];
- CString ip;
- if (Hex.GetLength()<11)
- {
- ip="NULL";
- return ip;
- }
- CString Hex1;
- Hex.Replace(" ","");
- for (int i=0;i<4;i++)
- {
- Hex1=Hex.Mid(i*2,2);
- int data=0;
- if((Hex1[0]>='A') && (Hex1[0]<='F'))
- data=data+Hex1[0]-'A'+10;
- if((Hex1[0]>='0') && (Hex1[0]<='9'))
- data=data+Hex1[0]-'0';
- data=data*16;
- int data1=0;
- if((Hex1[1]>='A') && (Hex1[1]<='F'))
- data1=data1+Hex1[1]-'A'+10;
- if((Hex1[1]>='0') && (Hex1[1]<='9'))
- data1=data1+Hex1[1]-'0';
- data=data+data1;
- itoa(data,ch,10);
- ip=ip+ch+'.';
- }
- ip=ip.Left(ip.GetLength()-1);
- return ip;
- }
- void CMainFrame::ScanNode(CString &add,CString &community,HTREEITEM hparent)
- {
- char ch[3];
- neighbor n1;
- CString BaseOid;
- NodeData *p1;
- m_snmp.clear();
- //清除原来的数据;
- for (int i=4;i<10;i++)
- {
- BaseOid="1.3.6.1.4.1.9.9.23.1.2.1.1.";
- itoa(i,ch,10);
- BaseOid=BaseOid+ch;
- if (m_snmp.GetTree(add,community,BaseOid)==false)
- {
- MessageBox("获取数据失败,请检查网络和SNMP配置参数!");
- return;
- }
-
- }
- //(pNodeData *)(pListView->m_map.GetItemData(hparent))->scan=true;
- //将CDP条目表中第4~9六个列对象的实例值取回,并存放在一个MyStringArray
- //对象中。关于这些列对象的具体含义请参考本章前面的内容;
- p1=(NodeData *)(pListView->m_map.GetItemData(hparent));
- p1->scan=true;
- //设置已经扫描标志;
- int row=m_snmp.GetCount()/6;
- CString h;
- int j;
- for (j=0; j<row;j++)
- {
- h=m_snmp.GetValue(j);
- h.TrimLeft();
- h.TrimRight();
- h=h.Left(11);
- m_snmp.SetValue(j,HtoIP(h));
- }
- //将得到的IP地址格式转换为正常的格式;
- for (j=0; j<row;j++)
- {
- CString remote="NULL";
- CString Temp_oid=m_snmp.GetOid(j);
- Temp_oid=Temp_oid.Mid(BaseOid.GetLength()+1,8);
- Temp_oid=Temp_oid.Left(Temp_oid.Find('.'));
- Temp_oid.TrimLeft();
- Temp_oid.TrimRight();
- Temp_oid="1.3.6.1.2.1.2.2.1.2."+Temp_oid;
- m_snmp.GetOne(add,community,Temp_oid,remote);
- m_snmp.Add(Temp_oid,remote);
- }
- //获得本地端口名,既通过哪个本地端口获得的该条CDP信息;
- int pos=0;
- //MessageBox("scaned: "+p1->ip);
- for (j=0; j<row;j++)
- //对获得的每一份邻居节点信息进行处理。正常情况下,每个CDP条目就是
- //一台新发现的设备,都应该在设备树增加一个节点,并在新增的节点中
- //保存到本节点的连接信息。但下列情况要做特殊处理:
- // 1:和父节点连接的CDP条目;
- // 2:和同一节点有多条连接;
- // 3:环路;
- {
- HTREEITEM h;
- if (p1->m_neighbor.GetSize()>0)
- if (Findport(m_snmp.GetValue(j+6*row),p1))
-
- continue;
-
- //和父节点连接的CDP条目,在扫描父节点时这些信息已经保存在本地
- //节点中,跳过;
- h=FindNode(pListView->m_map.GetRootItem(),m_snmp.GetValue(j));
- if (h!=NULL)
- {
- neighbor n2;
- NodeData * p2=(NodeData *)(pListView->m_map.GetItemData(h));
- n2.Id=hparent;
- n2.localport=m_snmp.GetValue(j+3*row);
- n2.remoteport=m_snmp.GetValue(j+6*row);
- p2->m_neighbor.Add(n2);
- continue;
- }
- //已经存在的节点,只增加连接信息,不增加新的节点;
- pNodeData=new NodeData;
- pNodeData->ip=m_snmp.GetValue(j);
- pNodeData->sysDescr=m_snmp.GetValue(j+row);
- pNodeData->scan=false;
- pNodeData->router=false;
- pNodeData->level=p1->level+1;
- pNodeData->pImage=NULL;
- pNodeData->position=p1->position+pos;
- pos+=1;
- //对任一子节点,层次数等于父节点层次数加1;横向位置以父节点
- //为基础,依次累加;
- CString rs=m_snmp.GetValue(j+5*row);
- rs=rs.Mid(9,2);
- if ((atoi(rs)%2)==1)
- {
- pNodeData->scan=true;
- pNodeData->router=true;
- }
- if (atoi(rs)>=8)
- pNodeData->scan=false;
- n1.Id=hparent;
- n1.localport=m_snmp.GetValue(j+3*row);
- n1.remoteport=m_snmp.GetValue(j+6*row);
- pNodeData->m_neighbor.Add(n1);
- h=pListView->m_map.InsertItem(m_snmp.GetValue(j),hparent);
- pListView->m_map.SetItemData(h,(DWORD)pNodeData);
- NodeArray.Add(h);
- }
- }
- void CMainFrame::OnScan()
- {
- if (NodeArray.GetSize()>0)
- NodeArray.RemoveAll();
- if (pListView->m_map.GetCount()>0)
- pListView->m_map.DeleteAllItems();
- //清除上次扫描数据;
- NodeData *p1;
- CNetMapDoc *pDoc=(CNetMapDoc *)this->GetActiveDocument();
- if (pDoc->ip.GetLength()==0||pDoc->community.GetLength()==0)
- {
- MessageBox("设备地址,Community信息错误!");
- return;
- }
- CString sysoid="1.3.6.1.2.1.1.1.0";
- CString reply;
- m_snmp.GetOne(pDoc->ip,pDoc->community,sysoid,reply);
- pNodeData=new NodeData;
- pNodeData->ip=pDoc->ip;
- pNodeData->sysDescr=reply;
- pNodeData->scan=false;
- pNodeData->pImage=NULL;
- pNodeData->level=0;
- pNodeData->position=0;
- HRoot=pListView->m_map.InsertItem(pDoc->ip);
- NodeArray.Add(HRoot);
- pListView->m_map.SetItemData(HRoot,(DWORD)pNodeData);
- HCurrent=HRoot;
- int NodeNumber;
- int i;
- while (1)
- {
- NodeNumber=NodeArray.GetSize();
- for(i=0;i<NodeNumber;i++)
- {
- p1=(NodeData *)(pListView->m_map.GetItemData(NodeArray[i]));
- if (p1->scan==true)
- continue;
- ScanNode(p1->ip,pDoc->community,NodeArray[i]);
- //MessageBox(pListView->m_map.GetItemText(NodeArray[i]));
- }
- if (NodeNumber==NodeArray.GetSize())
- break;
- }
- }
- void CMainFrame::OnGraph()
- {
- NodeData *p1;
- int NodeNumber=NodeArray.GetSize();
- if (NodeNumber==0) return;
- //没有图形数据,退出;
- p1=(NodeData *)(pListView->m_map.GetItemData(NodeArray[0]));
- if (p1->pImage!=NULL) return;
- //已经生成图形,退出;
- int x=80;
- int y=50;
- //CRect cr;
- CBitmap bmpLoad;
- for(int i=0;i<NodeNumber;i++)
- {
- p1=(NodeData *)(pListView->m_map.GetItemData(NodeArray[i]));
- CRect cr(x+100*p1->position,y+80*p1->level,55,25);
- p1->pImage=new CMyImage();
- p1->pImage->Create(NULL,WS_CHILD|SS_BITMAP|WS_VISIBLE|SS_NOTIFY,
- cr,pNetMapView,IDC_MYIMAGE+i);
- HBITMAP hBitmap = ::LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_S));
- if (p1->router==true)
- hBitmap = ::LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_R));
- p1->pImage->SetBitmap(hBitmap);
- p1->pImage->MyPos.x=cr.left+23;
- p1->pImage->MyPos.y=cr.top+8;
- //保存生成连接线时的点坐标;
- p1->pImage->HSelf=NodeArray[i];
- //保存在树形控件中的句柄;
- }
- pNetMapView->paint=true;
- pNetMapView->RedrawWindow();
- }
- void CMainFrame::OnShowinformation()
- {
- NodeData * p1=(NodeData *)(pListView->m_map.GetItemData(HSelect));
- CShow dlg;
- dlg.m_descr=p1->sysDescr;
- CString local="";
- CString remote="";
- CString ip="";
- for (int i=0;i<p1->m_neighbor.GetSize();i++)
- {
- ip+=pListView->m_map.GetItemText(p1->m_neighbor[i].Id)+" ";
- local+=p1->m_neighbor[i].localport+" ";
- remote+=p1->m_neighbor[i].remoteport+" ";
- }
- dlg.m_localport=local;
- dlg.m_remotelocal=remote;
- dlg.m_ip=ip;
- dlg.DoModal();
- }
- void CMainFrame::OnNodeModify()
- {
- HTREEITEM h;
- NodeData *p1;
- NodeData *p2;
- CString Lip,Lport,Rip,Rport;
- CModify dlg;
- if(dlg.DoModal()==IDOK)
- {
- Lip=dlg.m_LocalIp;
- Lport=dlg.m_LocalPort;
- Rip=dlg.m_RemoteIp;
- Rport=dlg.m_RemotePort;
- if (dlg.add==true)
- {
- h=FindNode(pListView->m_map.GetRootItem(),Lip);
- if (h!=NULL)
- {
- MessageBox("要增加的节点已经存在!");
- return;
- }
- h=FindNode(pListView->m_map.GetRootItem(),Rip);
- if (h==NULL)
- {
- MessageBox("上一级节点不存在!");
- return;
- }
- p2=(NodeData *)(pListView->m_map.GetItemData(h));
- p1=new NodeData;
- p1->ip=Lip;
- p1->level=p2->level+1;
- p1->position=p2->position;
- p1->sysDescr="增加的节点,IP:"+Lip;
- if (dlg.m_router==1)
- p1->router=true;;
- p1->scan=true;
- neighbor n1;
- // NodeData * p2=(NodeData *)(pListView->m_map.GetItemData(h));
- n1.Id=h;
- n1.localport=Lport;
- n1.remoteport=Rport;
- p1->m_neighbor.Add(n1);
- //设置邻居节点信息;
- HCurrent=pListView->m_map.InsertItem(Lip,h);
- NodeArray.Add(HCurrent);
- //在tree中增加节点,父节点为制定节点;
- h=pListView->m_map.GetPrevSiblingItem(HCurrent);
- if (h!=NULL)
- {
- p2=(NodeData *)(pListView->m_map.GetItemData(h));
- p1->position=p2->position+1;
- }
-
- //根据前面的兄弟节点的位置信息,获取新增加节点的位置信息;
- pListView->m_map.SetItemData(HCurrent,(DWORD)p1);
- //设置data属性;
- p1=(NodeData *)(pListView->m_map.GetItemData(pListView->m_map.GetRootItem()));
- p2=(NodeData *)pListView->m_map.GetItemData(HCurrent);
- if (p1->pImage!=NULL)
- {
- CRect cr(80+100*p2->position,50+80*p2->level,65,35);
- p2->pImage=new CMyImage();
- p2->pImage->Create(NULL,WS_CHILD|SS_BITMAP|WS_VISIBLE|SS_NOTIFY,
- cr,pNetMapView,IDC_MYIMAGE+NodeArray.GetSize()+1);
- HBITMAP hBitmap = ::LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP2));
- if (p2->router==true)
- hBitmap = ::LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
- p2->pImage->SetBitmap(hBitmap);
- p2->pImage->MyPos.x=cr.left+33;
- p2->pImage->MyPos.y=cr.top+12;
- //保存生成连接线时的点坐标;
- p2->pImage->HSelf=HCurrent;
- }
- pNetMapView->RedrawWindow();
- pListView->RedrawWindow();
- }
- else
- {
- h=FindNode(pListView->m_map.GetRootItem(),Lip);
- if (h==NULL)
- {
- MessageBox("要合并的节点不存在!");
- return;
- }
- HCurrent=FindNode(pListView->m_map.GetRootItem(),Rip);
- if (h==NULL)
- {
- MessageBox("上一级节点不存在!");
- return;
- }
- if ((pListView->m_map.GetChildItem(h)) ||(pListView->m_map.GetChildItem(HCurrent)))
- {
- MessageBox("只有叶子节点才能被合并!");
- return;
- }
- p1=(NodeData *)(pListView->m_map.GetItemData(h));
- p2=(NodeData *)(pListView->m_map.GetItemData(HCurrent));
- if ((p1->router!=true) ||(p2->router!=true))
- {
- MessageBox("要合并的设备必须是路由设备!");
- return;
- }
- p2->m_neighbor.Append(p1->m_neighbor);
- //合并连结信息;
- for(int i=0;i<NodeArray.GetSize();i++)
- {
- if (NodeArray[i]==h)
- {
- NodeArray.RemoveAt(i);
- break;
- }
- }
- if (p1->pImage!=NULL) delete p1->pImage;
- delete p1;
- pListView->m_map.DeleteItem(h);
- pNetMapView->RedrawWindow();
- pListView->RedrawWindow();
- }
- }
- }
- void CMainFrame::OnVlan()
- {
- Name2Id.RemoveAll();
- NodeData * p1=(NodeData *)(pListView->m_map.GetItemData(HSelect));
- if (p1->router==true)
- {
- MessageBox("该设备中不包含Vlan信息!");
- return;
- }
- //如果是路由器,退出;
- HTREEITEM Temp=pListView->m_vlan.GetRootItem();
- if (Temp!=NULL)
- {
- Temp=pListView->m_vlan.GetNextItem(Temp,TVGN_CHILD);
- Vlan * p2=(Vlan *)pListView->m_vlan.GetItemData(Temp);
- if (p2!=NULL) delete p2;
- Temp=pListView->m_vlan.GetNextItem(Temp,TVGN_NEXT);
- while (Temp!=NULL)
- {
- p2=(Vlan *)pListView->m_vlan.GetItemData(Temp);
- if (p2!=NULL) delete p2;
- Temp=pListView->m_vlan.GetNextItem(Temp,TVGN_NEXT);
- }
- pListView->m_vlan.DeleteAllItems();
- pListView->RedrawWindow();
- }
- //判断是否已经存在 VLAN信息,如存在,清除原有节点并释放
- //资源,重新采集信息;
- HRoot=pListView->m_vlan.InsertItem(p1->ip,0,0,NULL);
- //插入根节点;
- m_snmp.clear();
- CString community="public";
- CString TempOid="1.3.6.1.4.1.9.9.46.1.3.1.1.2";
- m_snmp.GetTree(p1->ip,community,TempOid);
- //取回VLAN运行状态信息;
- TempOid="1.3.6.1.4.1.9.9.46.1.3.1.1.4";
- m_snmp.GetTree(p1->ip,community,TempOid);
- //取回VLAN名字信息;
- m_temp.clear();
- TempOid="1.3.6.1.4.1.9.9.68.1.2.2.1.2";
- m_temp.GetTree(p1->ip,community,TempOid);
- //取回交换机中所有接口(非TRUNK)所属VLAN信息;
- int row=m_snmp.GetCount()/2;
- CString h,s,r,NameOid;
- for (int i=0;i<row;i++)
- {
- //处理每个VLAN信息;
- pVlanData=new Vlan;
- pVlanData->State=m_snmp.GetValue(i);
- pVlanData->Name=m_snmp.GetValue(i+row);
- //保存信息;
- h=m_snmp.GetOid(i);
- h.TrimLeft();
- h.TrimRight();
- h=h.Mid(h.ReverseFind('.')+1,4);
- pVlanData->Id=h;
- //处理OID获得并保存VLAN-id;
- Name2Id.SetAt(pVlanData->Name,pVlanData->Id);
- for(int j=0;j<m_temp.GetCount();j++)
- {
- //检查每一个非 TRUNK接口所属的vlan,并将该接口
- //的索引,名字保存在相应的VLAN节点data属性结构中;
- if (m_temp.GetValue(j)==h)
- //m_temp中第j个接口属于VLAN-id为h的VLAN,即正在
- //处理的VLAN;
- {
- s=m_temp.GetOid(j);
- s=s.Mid(s.ReverseFind('.')+1,4);
- //获取该接口的IfIndex;
- NameOid="1.3.6.1.2.1.2.2.1.2."+s;
- m_temp.GetOne(p1->ip,community,NameOid,r);
- //获取该接口的名字;
- port p;
- p.PortIndex=s;
- p.PortName=r;
- pVlanData->PortArray.Add(p);
- //保存接口信息;
- }
- }
- HCurrent=pListView->m_vlan.InsertItem(pVlanData->Name,1,1,HRoot);
- //为每一个VLAN在树形控件中增加节点;
- if (pVlanData->State!="1") pListView->m_vlan.SetItemImage(HCurrent,2,2);
- if (pVlanData->PortArray.GetSize()>0)
- for (j=0;j<pVlanData->PortArray.GetSize();j++)
- pListView->m_vlan.InsertItem(pVlanData->PortArray[j].PortName,3,3,HCurrent);
- //为每一个VLAN所拥有的接口增加节点;
- pListView->m_vlan.SetItemData(HCurrent,(DWORD)pVlanData);
- }
- pListView->m_vlan.Expand(HRoot,TVE_EXPAND);
- if (pListView->m_tab.GetCurSel()==0)
- {
- pListView->m_tab.SetCurSel(1);
- pListView->m_vlan.ShowWindow(SW_SHOW);
- pListView->m_map.ShowWindow(SW_HIDE);
- }
- //更新TAB的状态;
- }
- void CMainFrame::OnPort()
- {
- if (pListView->m_vlan.GetRootItem()==NULL)
- {
- MessageBox("先获取vlan信息!");
- return;
- }
- NodeData * p1=(NodeData *)(pListView->m_map.GetItemData(HSelect));
- if (p1->router==true)
- {
- MessageBox("该设备不是交换机!");
- return;
- }
- //如果是路由器,退出;
- m_snmp.clear();
- CString community="public";
- CString TempOid="1.3.6.1.4.1.9.9.68.1.2.2.1.2";
- m_snmp.GetTree(p1->ip,community,TempOid);
- //获得所有非TRUNK端口信息;
- CString s,r;
- for (int i=0;i<SwitchPortArray.GetSize();i++)
- {
- if (SwitchPortArray[i]->pPortImage!=NULL)
- delete SwitchPortArray[i]->pPortImage;
- if (SwitchPortArray[i]->pHostImage!=NULL)
- delete SwitchPortArray[i]->pHostImage;
- delete SwitchPortArray[i];
- }
- SwitchPortArray.RemoveAll();
- pVlanView->RedrawWindow();
- for (i=0;i<m_snmp.GetCount();i++)
- {
- pSP=new SwitchPort;
- pSP->trunk=false;
- pSP->SwitchIP=p1->ip;
- // pSP->ArrayIndex=i;
- pSP->PortVlan=m_snmp.GetValue(i);
- //所属VLAN;
- s=m_snmp.GetOid(i);
- s.TrimLeft();
- s.TrimRight();
- s=s.Mid(s.ReverseFind('.')+1,4);
- pSP->PortIndex=s;
- //该端口的IfIndex;
- TempOid="1.3.6.1.2.1.2.2.1.2."+s;
- m_snmp.GetOne(p1->ip,community,TempOid,r);
- pSP->PortName=r;
- //端口名字;
- TempOid="1.3.6.1.2.1.2.2.1.8."+s;
- m_snmp.GetOne(p1->ip,community,TempOid,r);
- if (r=="1")
- pSP->PortStatus="UP";
- else
- pSP->PortStatus="Down";
- //端口状态;
- pSP->pPortImage=NULL;
- pSP->pHostImage=NULL;
- SwitchPortArray.Add(pSP);
- }
- m_snmp.clear();
- TempOid="1.3.6.1.2.1.17.4.3.1.3";
- m_snmp.GetTree(p1->ip,community,TempOid);
- //该地址的获取方式;
- TempOid="1.3.6.1.2.1.17.4.3.1.2";
- m_snmp.GetTree(p1->ip,community,TempOid);
- //该地址的来源端口;
- TempOid="1.3.6.1.2.1.17.4.3.1.1";
- m_snmp.GetTree(p1->ip,community,TempOid);
- //MAC地址;
- int row=m_snmp.GetCount()/3;
- AddMap add;
- for (i=0;i<row;i++)
- {
- if (m_snmp.GetValue(i)=="3")
- {
- TempOid="1.3.6.1.2.1.17.1.4.1.2."+m_snmp.GetValue(i+row);
- m_snmp.GetOne(p1->ip,community,TempOid,r);
- for (int j=0;j<SwitchPortArray.GetSize();j++)
- {
- if (SwitchPortArray[j]->PortIndex==r)
- {
- add.hAdd=m_snmp.GetValue(i+2*row);
- add.hAdd=add.hAdd.Left(17);
- add.hIp="";
- if (M2I.IsEmpty()==0)
- M2I.Lookup(add.hAdd,add.hIp);
- if (add.hIp.GetLength()==0)
- add.hIp="*.*.*.*";
- SwitchPortArray[j]->HostAdd.Add(add);
- }
- }
- // TempMap.SetAt(r,m_snmp.GetValue(i+2*row));
- }
- }
- m_snmp.clear();
- TempOid="1.3.6.1.4.1.9.9.46.1.6.1.1.3";
- m_snmp.GetTree(p1->ip,community,TempOid);
- CString temp;
- //获取TRUNK端口的封装类型;
- TempOid="1.3.6.1.4.1.9.9.46.1.6.1.1.4";
- m_snmp.GetTree(p1->ip,community,TempOid);
- //该Trunk端口允许的VLAN;
- row=m_snmp.GetCount()/2;
- for (i=0;i<row;i++)
- {
- pSP=new SwitchPort;
- pSP->trunk=true;
- pSP->pHostImage=NULL;
- pSP->pPortImage=NULL;
- s=m_snmp.GetOid(i);
- s=s.Mid(s.ReverseFind('.')+1,4);
- pSP->PortIndex=s;
- TempOid="1.3.6.1.2.1.2.2.1.2."+s;
- m_snmp.GetOne(p1->ip,community,TempOid,r);
- pSP->PortName=r;
- if (m_snmp.GetValue(i)=="1") pSP->PortStatus="ISL";
- if (m_snmp.GetValue(i)=="2") pSP->PortStatus="Dot10";
- if (m_snmp.GetValue(i)=="3") pSP->PortStatus="Lane";
- if (m_snmp.GetValue(i)=="4") pSP->PortStatus="Dot1Q";
- if (m_snmp.GetValue(i)=="5") pSP->PortStatus="Negotiate";
- temp=m_snmp.GetValue(i+row);
- temp.Replace(".","");
- temp.Replace(" ","");
- temp=temp.Left(32);
- pSP->Vlans=HexTo01(temp);
- SwitchPortArray.Add(pSP);
- }
- int index,x,y;
- HICON hComputer,hNet,hIcon_gu,hIcon_gd,hIcon_dd,hIcon_du;
- hComputer= (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_COMPUTER), IMAGE_ICON, 32, 32, 0);
- hNet= (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_NET), IMAGE_ICON, 32, 32, 0);
- hIcon_gu= (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_RUNPORT), IMAGE_ICON, 32, 32, 0);
- hIcon_gd= (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_DRUNPORT), IMAGE_ICON, 32, 32, 0);
- hIcon_dd= (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_DDEADPORT), IMAGE_ICON, 32, 32, 0);
- hIcon_du= (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_UDEADPORT), IMAGE_ICON, 32, 32, 0);
- for (i=0;i<SwitchPortArray.GetSize();i++)
- {
- index=atoi(SwitchPortArray[i]->PortIndex);
- y=100+((index+1) % 2)*34;
- if ((index % 2)==0)
- x=(index-1)*16+50;
-
- else
- x=index*16+50;
- CRect cr(x,y,16,16);
- SwitchPortArray[i]->pPortImage=new CMyImage();
- SwitchPortArray[i]->pPortImage->ArrayIndex=i;
-
- SwitchPortArray[i]->pPortImage->Create(NULL,WS_CHILD|SS_ICON|WS_VISIBLE|SS_NOTIFY,
- cr,pVlanView,IDC_MYIMAGE+1000+i);
- if ((index % 2)==0)
- {
- if (SwitchPortArray[i]->PortStatus=="UP")
- SwitchPortArray[i]->pPortImage->SetIcon(hIcon_gd);
- else
- SwitchPortArray[i]->pPortImage->SetIcon(hIcon_dd);
- if (SwitchPortArray[i]->trunk==true)
- SwitchPortArray[i]->pPortImage->SetIcon(hIcon_gd);
- }
- else
- {
- if (SwitchPortArray[i]->PortStatus=="UP")
- SwitchPortArray[i]->pPortImage->SetIcon(hIcon_gu);
- else
- SwitchPortArray[i]->pPortImage->SetIcon(hIcon_du);
- if (SwitchPortArray[i]->trunk==true)
- SwitchPortArray[i]->pPortImage->SetIcon(hIcon_gu);
- }
- //SwitchPortArray[i]->pPortImage->SetIcon(hIcon);
- SwitchPortArray[i]->pPortImage->port=true;
- SwitchPortArray[i]->pPortImage->MyPos.x=x+16;
- SwitchPortArray[i]->pPortImage->MyPos.y=y+16;
- if (SwitchPortArray[i]->PortStatus=="UP")
- {
- if (SwitchPortArray[i]->HostAdd.GetSize()>=1)
- {
- if ((index % 2)==0)
- y+=60;
- else
- y-=60;
- CRect cr1(x,y,16,16);
- SwitchPortArray[i]->pHostImage=new CMyImage();
- SwitchPortArray[i]->pHostImage->Create(NULL,WS_CHILD|SS_ICON|WS_VISIBLE|SS_NOTIFY,
- cr1,pVlanView,IDC_MYIMAGE+2000+i);
- SwitchPortArray[i]->pHostImage->port=true;
- SwitchPortArray[i]->pHostImage->MyPos.x=x+16;
- SwitchPortArray[i]->pHostImage->MyPos.y=y+16;
- if (SwitchPortArray[i]->HostAdd.GetSize()==1)
- SwitchPortArray[i]->pHostImage->SetIcon(hComputer);
- else
- SwitchPortArray[i]->pHostImage->SetIcon(hNet);
- }
- }
- }
- pVlanView->paint=true;
- pVlanView->RedrawWindow();
- }
- void CMainFrame::OnAddMap()
- {
- CInput input;
- CString ip,community;
- if(input.DoModal()==IDOK)
- {
- ip=input.m_ip;
- community=input.m_community;
- }
- m_snmp.clear();
- CString TempOid="1.3.6.1.2.1.3.1.1.2";
- m_snmp.GetTree(ip,community,TempOid);
- //物理地址;
- TempOid="1.3.6.1.2.1.3.1.1.3";
- m_snmp.GetTree(ip,community,TempOid);
- //网络地址;
- int row=m_snmp.GetCount()/2;
- for (int i=0;i<row;i++)
- {
- ip=m_snmp.GetValue(i);
- ip=ip.Left(17);
- ip.TrimLeft();
- ip.TrimRight();
- M2I.SetAt(ip,m_snmp.GetValue(i+row));
- }
- if (row!=0)
- MessageBox("操作成功!");
- }
- void CMainFrame::OnValnTopo()
- {
- HRoot=pListView->m_vlan.GetRootItem();
- if (HRoot==NULL) return;
- HCurrent=pListView->m_vlan.GetSelectedItem();
- if ((HCurrent==NULL)||(HRoot!=pListView->m_vlan.GetParentItem(HCurrent)))
- {
- MessageBox("请在Tree中选择一个VLAN!");
- return;
- }
- pVlanData=(Vlan *)pListView->m_vlan.GetItemData(HCurrent);
- pNetMapView->RedrawWindow();
- CString VlanId=pVlanData->Id;
- CString r;
- NodeData *p1;
- CClientDC dc(pNetMapView);
- CBrush MyBrush;
- CBrush * pOldBrush;
- MyBrush.CreateSolidBrush(RGB(0,255,0));
- pOldBrush=dc.SelectObject(&MyBrush);
- CString TempOid="1.3.6.1.4.1.9.9.68.1.2.2.1.2";
- CString community="public";
-
- for (int i=0;i<NodeArray.GetSize();i++)
- {
- p1=(NodeData *)(pListView->m_map.GetItemData(NodeArray[i]));
- if (p1->router==true)
- continue;
- m_temp.clear();
- TempMap.RemoveAll();
- m_temp.GetTree(p1->ip,community,TempOid);
- for (int j=0;j<m_temp.GetCount();j++)
- TempMap.SetAt(m_temp.GetValue(j),m_temp.GetOid(j));
-
- if (TempMap.Lookup(VlanId,r)!=0)
- {
- int x1=p1->pImage->MyPos.x-22;
- int y1=p1->pImage->MyPos.y-22;
- CRect rc(x1,y1,x1+20,y1+20);
-
- dc.Ellipse(rc);
-
- }
-
- }
- dc.SelectObject(pOldBrush);
- }
- void CMainFrame::OnAddVlan()
- {
- NodeData * p1=(NodeData *)(pListView->m_map.GetItemData(HSelect));
- if (p1->router==true)
- {
- MessageBox("该设备不是交换机!");
- return;
- }
- CString community="private";
- CString id,name;
- CString Oid;
- CAddVlan dlg;
- if (dlg.DoModal()==IDOK)
- {
- id=dlg.m_vid;
- name=dlg.m_vname;
- }
- int said=100000+atoi((LPCTSTR)id);
- CString S;
- S.Format("%d",said);
- //根据vlan ID产生vlan的SAID;
- Oid="1.3.6.1.4.1.9.9.46.1.4.1.1.1.1";
- m_snmp.Set(p1->ip,community,Oid,"2",0);
- //1设置vtpVlanEditOperation,将当前vlan信息拷贝到编辑缓冲区;
- Oid="1.3.6.1.4.1.9.9.46.1.4.1.1.3.1";
- m_snmp.Set(p1->ip,community,Oid,"me",1);
- //2设置"owner"字符串;
- Oid="1.3.6.1.4.1.9.9.46.1.4.2.1.11.1."+id;
- m_snmp.Set(p1->ip,community,Oid,"4",0);
- //3设置vtpVlanEditRowStatus值为CreatandGo,创建行;
- Oid="1.3.6.1.4.1.9.9.46.1.4.2.1.3.1."+id;
- m_snmp.Set(p1->ip,community,Oid,"1",0);
- //4设置vlan类型;
- Oid="1.3.6.1.4.1.9.9.46.1.4.2.1.4.1."+id;
- m_snmp.Set(p1->ip,community,Oid,name,1);
- //5设置vlan名字;
- Oid="1.3.6.1.4.1.9.9.46.1.4.2.1.6.1."+id;
- m_snmp.Set(p1->ip,community,Oid,S,1);
- //6设置vlan SAID串;
- Oid="1.3.6.1.4.1.9.9.46.1.4.1.1.1.1";
- m_snmp.Set(p1->ip,community,Oid,"3",0);
- //7设置vtpVlanEditOperation,将当前编辑缓冲区的信息应用到系统中;
- Oid="1.3.6.1.4.1.9.9.46.1.4.1.1.1.1";
- m_snmp.Set(p1->ip,community,Oid,"4",0);
- //释放编辑缓冲区;
- MessageBox("OK");
- }
- void CMainFrame::OnDeleteVlan()
- {
- NodeData * p1=(NodeData *)(pListView->m_map.GetItemData(HSelect));
- CString Oid,id,name;
- CString community="private";
- Oid="1.3.6.1.4.1.9.9.46.1.4.1.1.1.1";
- CAddVlan dlg;
- if (dlg.DoModal()==IDOK)
- name=dlg.m_vname;
- Name2Id.Lookup(name,id);
- m_snmp.Set(p1->ip,community,Oid,"2",0);
- //1设置vtpVlanEditOperation,将当前vlan信息拷贝到编辑缓冲区;
- Oid="1.3.6.1.4.1.9.9.46.1.4.1.1.3.1";
- m_snmp.Set(p1->ip,community,Oid,"me",1);
- //2设置"owner"字符串;
- Oid="1.3.6.1.4.1.9.9.46.1.4.2.1.11.1."+id;
- m_snmp.Set(p1->ip,community,Oid,"6",0);
- //3设置vtpVlanEditRowStatus值为Destory,删除行;
- Oid="1.3.6.1.4.1.9.9.46.1.4.1.1.1.1";
- m_snmp.Set(p1->ip,community,Oid,"3",0);
- //7设置vtpVlanEditOperation,将当前编辑缓冲区的信息应用到系统中;
- Oid="1.3.6.1.4.1.9.9.46.1.4.1.1.1.1";
- m_snmp.Set(p1->ip,community,Oid,"4",0);
- //释放编辑缓冲区;
- MessageBox("OK");
- }
- void CMainFrame::OnPortMore()
- {
- if (pListView->m_vlan.GetRootItem()==NULL)
- return;
- CString str="";
- CString Temp="";
- SwitchPort *pPort;
- pPort=SwitchPortArray[SeletedIndex];
- str+="端口名称:"+pPort->PortName+"rn";
- if (pPort->trunk==true)
- {
- for (int i=0;i<pPort->Vlans.GetLength();i++)
- {
- if (pPort->Vlans[i]=='0')
- Temp+=FindVlan(i)+" ";
- }
- str+="端口类型:TRUNK端口rn";
- str+="封装类型:"+pPort->PortStatus+"rn";
- str+="不允许的VLAN:"+Temp;
- }
- else
- {
- str+="端口类型:一般端口rn";
- str+="端口状态:"+pPort->PortStatus+"rn";
- for(int j=0;j<pPort->HostAdd.GetSize();j++)
- Temp+=pPort->HostAdd[j].hIp+" ";
- str+="主机IP:"+Temp+"rn";
- Temp="";
- for(j=0;j<pPort->HostAdd.GetSize();j++)
- Temp+=pPort->HostAdd[j].hAdd+" ";
- str+="主机MAC:"+Temp+"rn";
- }
- MessageBox(str,"端口信息");
- }
- void CMainFrame::OnToVlan()
- {
- SwitchPort *pPort;
- pPort=SwitchPortArray[SeletedIndex];
- if (pPort->trunk)
- {
- MessageBox("该端口不能赋给一个VLAN!");
- return;
- }
- CString community="private";
- CString name,id;
- CString Oid;
-
- CAddVlan dlg;
- if (dlg.DoModal()==IDOK)
- name=dlg.m_vname;
- Name2Id.Lookup(name,id);
- if (id.GetLength()==0) return;
- Oid="1.3.6.1.4.1.9.9.68.1.2.2.1.2."+pPort->PortIndex;
- m_snmp.Set(pPort->SwitchIP,community,Oid,id,0);
- CString r;
- m_snmp.GetOne(pPort->SwitchIP,community,Oid,r);
- if (r==id)
- MessageBox("操作成功!");
- }