MibBrowserView.cpp
上传用户:hbytqc8
上传日期:2014-07-31
资源大小:527k
文件大小:36k
- // MibBrowserView.cpp : implementation of the CMibBrowserView class
- //
- #include "stdafx.h"
- #include "MibBrowser.h"
- #include "Dlg_input.h"//输入OID对话框;
- #include "SetDig.h"//SET操作对话框;
- #include "MibBrowserDoc.h"
- #include "MibBrowserView.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMibBrowserView
- IMPLEMENT_DYNCREATE(CMibBrowserView, CFormView)
- BEGIN_MESSAGE_MAP(CMibBrowserView, CFormView)
- //{{AFX_MSG_MAP(CMibBrowserView)
- ON_COMMAND(ID_GET, OnGet)
- ON_COMMAND(ID_GETNEXT, OnGetnext)
- ON_COMMAND(ID_GETSUBTREE, OnGetsubtree)
- ON_COMMAND(ID_WALK, OnWalk)
- ON_WM_DESTROY()
- ON_NOTIFY(TVN_ITEMEXPANDED, IDC_TREE1, OnItemexpandedTree1)
- ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
- ON_COMMAND(ID_LOAD, OnLoad)
- ON_COMMAND(ID_OID, OnOid)
- ON_COMMAND(ID_SAVE, OnSave)
- ON_COMMAND(ID_SET, OnSet)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMibBrowserView construction/destruction
- CMibBrowserView::CMibBrowserView()
- : CFormView(CMibBrowserView::IDD)
- {
- //{{AFX_DATA_INIT(CMibBrowserView)
- m_edit1 = _T("");
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- }
- CMibBrowserView::~CMibBrowserView()
- {
- }
- void CMibBrowserView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMibBrowserView)
- DDX_Control(pDX, IDC_EDIT3, m_oid);
- DDX_Control(pDX, IDC_EDIT2, m_community);
- DDX_Control(pDX, IDC_IPADDRESS1, m_ipadd);
- DDX_Control(pDX, IDC_TREE1, m_tree);
- DDX_Control(pDX, IDC_LIST1, m_list);
- DDX_Text(pDX, IDC_EDIT1, m_edit1);
- //}}AFX_DATA_MAP
- }
- BOOL CMibBrowserView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- //查找给定OID的叶子节点的函数,返回节点句柄;
- HTREEITEM CMibBrowserView::SearchNode(const CString &OidStr)
- {
- MibNode *pData;
- HTREEITEM hTv;
- hTv=m_tree.GetRootItem();
- //追踪节点OID,直到叶子节点;
- while (hTv!=NULL)
- {
- pData=(MibNode*)m_tree.GetItemData(hTv);
- if (pData!=NULL)
- {
- //如果OID串不以该节点的OID串开始,移到它的兄弟节点;
- if (OidStr.Find((LPCTSTR)(pData->POid+'.'))!=0)
- hTv=m_tree.GetNextItem(hTv,TVGN_NEXT);
- else
- {
- //否则,如果不是叶子节点,移到它的第一个子节点;
- if (m_tree.ItemHasChildren(hTv))
- hTv=m_tree.GetNextItem(hTv,TVGN_CHILD);
- //如果是叶子节点,返回节点句柄;
- else
- return hTv;
- }
- }//if (pData!=NULL)
- }//while (hTv!=NULL)
- if (hTv==NULL)
- return NULL;
- else
- return hTv;
- }
- //处理数据类型为枚举型整数的函数,将其转化为字符串列表中的元素,
- //以下面的形式保存:
- //1
- //up
- //2
- //down
- //3
- //testing
- void CMibBrowserView::WorkOnInt(CString &str,CStringList* p)
- {
- //截取大括号中的字符串,不包括大括号;
- int n1=str.Find('{');
- int n2=str.Find('}')-n1-1;
- str=str.Mid(n1+1,n2)+',';
- //AfxMessageBox((LPCTSTR)str);
- while (str.GetLength()>3)
- {
- CString str1=str.Left(str.Find(','));
- str=str.Mid(str.Find(',')+1);
- int n3=str1.Find('(');
- int n4=str1.Find(')')-n3-1;
- CString list=str1.Mid(n3+1,n4);
- list.TrimLeft();
- list.TrimRight();
-
- p->AddTail(list);
- list=str1.Left(n3);
- list.TrimLeft();
- list.TrimRight();
- p->AddTail(list);
-
- }
- }
- //使用递归算法,遍历整个以hparent为根节点的子树,销毁Data中的对象;
- void CMibBrowserView::travl(HTREEITEM hparent)
- {
- MibNode* pnode;
- //如果节点存在,首先判断data是否为NULL,如否,则进一步判断PInteger
- //是否为NULL,如否,释放所指向的对象,然后释放data所指向的对象;
- if (hparent!=NULL)
- {
- if ((pnode=(MibNode*)m_tree.GetItemData(hparent))!=NULL)
- {
- if((pnode->PInteger)!=NULL)
- delete pnode->PInteger;
- delete pnode;
- }
- }
- hparent=m_tree.GetChildItem(hparent);
- do
- {
- if (hparent!=NULL)
- travl(hparent);
- hparent=m_tree.GetNextSiblingItem(hparent);
- } while(hparent!=NULL);
- }
- //根据给定的父,子节点名称在树中增加新节点的函数,并设置相应的图标;
- void CMibBrowserView::AddObject(HTREEITEM ht,CString &Parent_name,
- CString &Child_name,CString &num,MibNode *p)
- {
- CString OidStr;
- Parent_name.TrimLeft();
- Parent_name.TrimRight();
- Child_name.TrimLeft();
- Child_name.TrimRight();
- //查找父节点位置;
- HTREEITEM hParent=FindNode(ht,Parent_name);
- if (hParent!=NULL)
- {
- //增加新节点;
- HCurrent=m_tree.InsertItem(LPCTSTR(Child_name),3,3,hParent);
- //获取父节点OID;
- MibNode* P_node=(MibNode*)m_tree.GetItemData(hParent);
- OidStr=P_node->POid;
- //生成子节点OID;
- OidStr=OidStr+'.'+num;
- p->POid=OidStr;
- //设置子节点data属性;
- m_tree.SetItemData(HCurrent,(DWORD)p);
- //根据不同节点设置不同发图标;
- if (IndexString.Find((LPCTSTR)Child_name)>0)
- m_tree.SetItemImage(HCurrent,4,4);
- if (Child_name.Find("Entry")>1)
- m_tree.SetItemImage(HCurrent,5,5);
- if (Child_name.Find("Table")>1)
- m_tree.SetItemImage(HCurrent,6,6);
- }
- }
- //增加辅助节点。增加前,首先要查找树中父节点是否已经存在,如存在父节点,
- //生成节点对应的结构数据,将新节点增加为父节点的子节点。否则,说明新节
- //点为顶端节点,首先从TopOid中查找对应的OID,生成节点对应的结构数据,并
- //增加父节点。然后根据父节点的OID和参数&num生成子节点的OID,生成节点对
- //应的结构数据,并增加子节点;
- void CMibBrowserView::AddPlaceHolder(HTREEITEM ht,CString &Parent_name,
- CString &Child_name,CString &num)
- {
- CString OidStr;
- Parent_name.TrimLeft();
- Parent_name.TrimRight();
- Child_name.TrimLeft();
- Child_name.TrimRight();
- //查找父节点;
- HTREEITEM hParent=FindNode(ht,Parent_name);
- if (hParent==NULL)
- {
- //父节点不存在;
- AfxMessageBox("请确认是否注册了顶端节点!");
- return;
- }
- else
- {
- //父节点存在,只增加子节点;
- HCurrent=m_tree.InsertItem(LPCTSTR(Child_name),1,1,hParent);
- MibNode* P_node=(MibNode*)m_tree.GetItemData(hParent);
- OidStr=P_node->POid;
- OidStr=OidStr+'.'+num;
- P_node=new MibNode;
- InitNodeData(P_node);
- P_node->POid=OidStr;
- m_tree.SetItemData(HCurrent,(DWORD)P_node);
- }
- }
- //装载MIB文件;
- bool CMibBrowserView::LoadMib(CString &filename)
- {
- MibNode* pNode;
- if (MibFile.Open((LPCTSTR)filename,CFile::modeRead)==false)
- {
- AfxMessageBox("打开MIB-II文件时失败");
- return false;
- }
- IndexString=' ';
- //逐行读入并处理;
- while (MibFile.ReadString(Line))
- {
- //预处理读入的行;
- //空行,直接读下一行;
- if (Line.GetLength()==0) continue;
- //注释行,直接读下一行;
- if (Line.Find("--")==0) continue;
- //行的后面包含注释部分,去掉注释部分;
- if (Line.Find("--")>0)
- Line=Line.Left(Line.Find("--"));
- //除去行两端的空白字符;
- Line.TrimLeft();
- Line.TrimRight();
- //MIB模块名定义行;
- /* if (Line.Find("DEFINITIONS ::=")>0)
- {
- Line=Line.Left(Line.Find("DEFINITIONS ::="));
- HSecond= m_tree.InsertItem(LPCTSTR(Line) ,1,2,HRoot);
- continue;
- };*/
- //辅助节点定义行;
- if (Line.Find("OBJECT IDENTIFIER ::=")>2)
- if ((Line.Find('{')>10)&&(Line.Find('}')>10))
- {
- CString ParentName,NodeName,Pos;
- NodeName=Line.Left(Line.Find("OBJECT IDENTIFIER ::="));
- int n1=Line.Find('{');
- int n2=Line.Find('}')-n1-1;
- Line=Line.Mid(n1+1,n2);
- Line.TrimLeft();
- Line.TrimRight();
- ParentName=Line.Left(Line.Find(' '));
- Pos=Line.Mid(Line.Find(' ')+1);
- AddPlaceHolder(HRoot,ParentName,NodeName,Pos);
- continue;
- }
- //辅助节点定义行;
- if ((Line.Find("MODULE-IDENTITY")>2)&&(Line.Find(',')==-1))
- {
- CString ParentName,NodeName,Pos;
- NodeName=Line.Left(Line.Find("MODULE-IDENTITY"));
- while (MibFile.ReadString(Line))
- {
- if (Line.GetLength()==0) continue;
- if (Line.Find("--")==0) continue;
- if (Line.Find("--")>0)
- Line=Line.Left(Line.Find("--"));
- Line.TrimLeft();
- Line.TrimRight();
- if ((Line.Find("::=")==0)&&(Line.Find('}')>3)&&(Line.Find('{')>1))
- {
- int n1=Line.Find('{');
- int n2=Line.Find('}')-n1-1;
- Line=Line.Mid(n1+1,n2);
- Line.TrimLeft();
- Line.TrimRight();
- ParentName=Line.Left(Line.Find(' '));
- Pos=Line.Mid(Line.Find(' ')+1);
- AddPlaceHolder(HRoot,ParentName,NodeName,Pos);
- break;
- }
- }// while (MibFile.ReadString(Line))
- continue;
- }
- //辅助节点定义行;
- if ((Line.Find("OBJECT-IDENTITY")>2)&&(Line.Find(',')==-1))
- {
- CString ParentName,NodeName,Pos;
- NodeName=Line.Left(Line.Find("OBJECT-IDENTITY"));
- int n1=Line.Find('{');
- int n2=Line.Find('}')-n1-1;
- Line=Line.Mid(n1+1,n2);
- Line.TrimLeft();
- Line.TrimRight();
- ParentName=Line.Left(Line.Find(' '));
- Pos=Line.Mid(Line.Find(' ')+1);
- AddPlaceHolder(HRoot,ParentName,NodeName,Pos);
- continue;
- }
- //被管理对象定义行;
- if ((Line.Find("OBJECT-TYPE")>2)&&(Line.Find(',')==-1))
- {
- CString ParentName,NodeName,Pos;
- NodeName=Line.Left(Line.Find("OBJECT-TYPE"));
- NodeName.TrimLeft();
- NodeName.TrimRight();
- pNode=new MibNode;
- // pNode->PInteger=new CStringList();
- // if (pNode->PInteger==NULL) AfxMessageBox(" 1 Bad pointer!!");
- InitNodeData(pNode);
- //逐行读入后续的定义行;
- while (MibFile.ReadString(Line))
- {
- //读入行的预处理;
- if (Line.GetLength()==0) continue;
- if (Line.Find("--")==0) continue;
- if (Line.Find("--")>0)
- Line=Line.Left(Line.Find("--"));
- Line.TrimLeft();
- Line.TrimRight();
- if(Line.Find("SYNTAX")==0)
- {
- //整数类型;
- if (Line.Find("INTEGER")>0)
- {
- pNode->PSnytax="INTEGER";
- //一行中完成定义的枚举整数类型;
- if ((Line.Find('{')>10)&&(Line.Find('}')>10))
- {
- pNode->PInteger=new CStringList();
- WorkOnInt(Line,pNode->PInteger);
-
-
- }
- //枚举型整数定义的后两种形式;
- if ((Line.Find('{')>10)&&(Line.Find('}')==-1))
- {
- CString Line1='{';
- while (MibFile.ReadString(Line))
- {
- if (Line.GetLength()==0) continue;
- if (Line.Find("--")==0) continue;
- if (Line.Find("--")>0)
- Line=Line.Left(Line.Find("--"));
- Line.TrimLeft();
- Line.TrimRight();
- Line1+=Line;
- if (Line.Find('}')!=-1) break;
- }
- pNode->PInteger=new CStringList();
- WorkOnInt(Line1,pNode->PInteger);
- }
- }
- else
- //非INTEGER的其他类型;
- {
- Line=Line.Mid(6);
- Line.TrimLeft();
- Line.TrimRight();
- pNode->PSnytax=Line;
- }
- continue;
- }// if(Line.Find("SYNTAX")==0)
- //枚举型整数的第一种定义形式;
- if ((Line.Find('{')==0)&&(pNode->PSnytax.Find("INTEGER")==0))
- {
- CString Line1=Line;
- while (MibFile.ReadString(Line))
- {
- if (Line.GetLength()==0) continue;
- if (Line.Find("--")==0) continue;
- if (Line.Find("--")>0)
- Line=Line.Left(Line.Find("--"));
- Line.TrimLeft();
- Line.TrimRight();
- Line1+=Line;
- if (Line.Find('}')!=-1) break;
- }
- pNode->PInteger=new CStringList();
- WorkOnInt(Line1,pNode->PInteger);
- continue;
- }
- //定义访问权限的行;
- if ((Line.Find("ACCESS")==0)||(Line.Find("MAX-ACCESS")==0))
- {
- Line=Line.Mid(Line.Find(' '));
- Line.TrimLeft();
- Line.TrimRight();
- pNode->PAccess=Line;
- continue;
- }
- //定义对象状态的行;
- if ((Line.Find("STATUS")==0))
- {
- Line=Line.Mid(Line.Find(' '));
- Line.TrimLeft();
- Line.TrimRight();
- pNode->PStatus=Line;
- continue;
- }
- //定义表索引的行;
- if ((Line.Find("INDEX")==0))
- {
- Line=Line.Mid(Line.Find(' '));
- CString Line1=Line;
- if (Line.Find('}')==-1)
- {
- while (MibFile.ReadString(Line))
- {
- if (Line.GetLength()==0) continue;
- if (Line.Find("--")==0) continue;
- if (Line.Find("--")>0)
- Line=Line.Left(Line.Find("--"));
- Line.TrimLeft();
- Line.TrimRight();
- Line1=Line1+' '+Line;
- if (Line.Find('}')!=-1) break;
- }
- }
- pNode->PIndex=Line1;
- IndexString=Line1;
- continue;
- }
- if ((Line.Find("DESCRIPTION")==0))
- {
- CString Line1;
- while (MibFile.ReadString(Line))
- {
- if (Line.GetLength()==0) continue;
- if (Line.Find("--")==0) continue;
- if (Line.Find("--")>0)
- Line=Line.Left(Line.Find("--"));
- Line.TrimLeft();
- Line.TrimRight();
- Line1+=Line;
- //第一个字符为‘“ ’,从第3个字符开始判断;
- if (Line.Find('"',2)>2) break;
- }
- pNode->PDescr=Line1;
- continue;
- }
- //最后一行,退出;
- if ((Line.Find("::=")==0)&&(Line.Find('}')>3)&&(Line.Find('{')>1))
- {
- int n1=Line.Find('{');
- int n2=Line.Find('}')-n1-1;
- Line=Line.Mid(n1+1,n2);
- Line.TrimLeft();
- Line.TrimRight();
- ParentName=Line.Left(Line.Find(' '));
- Pos=Line.Mid(Line.Find(' ')+1);
- AddObject(HRoot,ParentName,NodeName,Pos,pNode);
- break;
- }
- }// while (MibFile.ReadString(Line))
- continue;
- }
- }//while (MibFile.ReadString(Line))
- MibFile.Close();
- return true;
- }
- //使用递归算法,遍历整个以hSubRoot为根节点的子树,查找是否存在label为&text
- //的节点,如存在,返回该节点的句柄,否则返回NULL;
- HTREEITEM CMibBrowserView::FindNode(HTREEITEM hSubRoot,const CString &text)
- {
- if (hSubRoot==NULL)
-
- return NULL;
- //判断是否符合条件,如是,返回该节点的举柄;
- if (m_tree.GetItemText(hSubRoot)==text)
- return hSubRoot;
- hSubRoot=m_tree.GetChildItem(hSubRoot);
- do
- {
- HTREEITEM ht;
- //递归调用FindNode;
- if ((ht=FindNode(hSubRoot,text)) !=NULL)
- return ht;
- } while ((hSubRoot=m_tree.GetNextSiblingItem(hSubRoot)) !=NULL);
- return NULL;
- }
- //初始化一个结构数据,所有指针设置为NULL;
- void CMibBrowserView::InitNodeData(MibNode *Pnode)
- {
- Pnode->PSnytax=' ';
- Pnode->PAccess=' ';
- Pnode->PDescr=' ';
- Pnode->PIndex=' ';
- Pnode->PStatus=' ';
- Pnode->POid=' ';
- Pnode->PInteger=NULL;
- }
- void CMibBrowserView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit();
- //增加的代码;
- CurrDir=_pgmptr;
- CurrDir.MakeReverse();
- int path=CurrDir.Find('\');
- CurrDir.MakeReverse();
- CurrDir=CurrDir.Left(CurrDir.GetLength()-path);
- //初始化列表控件;
- m_ipadd.SetAddress(127,0,0,1);
- CString pass="public";
- m_community.SetWindowText(pass);
- pass="1.3.6.1.2.1.1.1.0";
- m_oid.SetWindowText(pass);
- m_list.InsertColumn(0," OID",LVCFMT_RIGHT,180,0);
- m_list.InsertColumn(1," Value",LVCFMT_LEFT,320,1);
- m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- //初始化,装载图标;
- m_iImageList.Create(16, 16, TRUE,1, 0);
- HICON hIcon = NULL;
- //0,root;
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_ROOT1), IMAGE_ICON, 32, 32, 0);
- m_iImageList.Add(hIcon);
- //1,closed box;
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_CLOSE_BOX), IMAGE_ICON, 16, 16, 0);
- m_iImageList.Add(hIcon);
- //2,opened box;
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_OPEN_BOX), IMAGE_ICON, 16, 16, 0);
- m_iImageList.Add(hIcon);
- //3,leaf node
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_LEAF), IMAGE_ICON, 16, 16, 0);
- m_iImageList.Add(hIcon);
- //4,key node
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_KEY), IMAGE_ICON, 16, 16, 0);
- m_iImageList.Add(hIcon);
- //5,entry;
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_ENTRY), IMAGE_ICON, 16, 16, 0);
- m_iImageList.Add(hIcon);
- //6,table;
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_TABLE), IMAGE_ICON, 16, 16, 0);
- m_iImageList.Add(hIcon);
- m_tree.SetImageList ( &m_iImageList,TVSIL_NORMAL );
- //增加根节点;
- MibNode* pNode;
- HRoot= m_tree.InsertItem("MIB",0,0,NULL);
- pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid="1.3.6.1";
- pNode->PAccess="not-accessable";
- //增加几个主要辅助节点;
- //mgmt
- m_tree.SetItemData(HRoot,(DWORD)pNode);
- HSecond= m_tree.InsertItem("mgmt",1,1,HRoot);
- pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid="1.3.6.1.2";
- pNode->PAccess="not-accessable";
- m_tree.SetItemData(HSecond,(DWORD)pNode);
- //private;
- HSecond= m_tree.InsertItem("private",1,1,HRoot);
- pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid="1.3.6.1.4";
- pNode->PAccess="not-accessable";
- m_tree.SetItemData(HSecond,(DWORD)pNode);
- //enterprises;
- HSecond= m_tree.InsertItem("enterprises",1,1,HSecond);
- pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid="1.3.6.1.4.1";
- pNode->PAccess="not-accessable";
- m_tree.SetItemData(HSecond,(DWORD)pNode);
- //cisco
- HSecond= m_tree.InsertItem("cisco",1,1,HSecond);
- pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid="1.3.6.1.4.1.9";
- pNode->PAccess="not-accessable";
- m_tree.SetItemData(HSecond,(DWORD)pNode);
- //ciscomgmt
- HSecond=m_tree.InsertItem("ciscoMgmt",1,1,HSecond);
- pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid="1.3.6.1.4.1.9.9";
- pNode->PAccess="not-accessable";
- m_tree.SetItemData(HSecond,(DWORD)pNode);
- //microsoft
- HSecond=m_tree.InsertItem("microsoft",
- 1,1,m_tree.GetParentItem(m_tree.GetParentItem(HSecond)));
- pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid="1.3.6.1.4.1.311";
- pNode->PAccess="not-accessable";
- m_tree.SetItemData(HSecond,(DWORD)pNode);
- //打开OID文件,将用户增加的辅助(顶端)节点增加的MIB树中;
- //每个顶端节点占3行,第一行为父节点名,第二行为辅助节点名,
- //第三行为它的OID;
- CString tempfile=CurrDir+"mib/oid.txt";
- if (MibFile.Open((LPCTSTR)tempfile,CFile::modeRead)==false)
- {
- AfxMessageBox("打开oid文件时失败");
- return;
- }
- //逐行读入并处理;
- while (MibFile.ReadString(Line))
- {
- CString n_name,o_name;
- Line.TrimLeft();
- Line.TrimRight();
- if (Line.GetLength()==0) continue;
- HTREEITEM hr;
- hr=m_tree.GetRootItem();
- HTREEITEM hp=FindNode(hr,Line);
- //读入节点名;
- MibFile.ReadString(Line);
- n_name=Line;
- //OID;
- MibFile.ReadString(Line);
- o_name=Line;
- if (hp!=NULL)
- {
- //插入新节点并设置data项;
- hp=m_tree.InsertItem((LPCTSTR)n_name,1,1,hp);
- MibNode *pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid=o_name;
- pNode->PAccess="not-accessable";
- m_tree.SetItemData(hp,(DWORD)pNode);
- }
- }
- MibFile.Close();
- //装载MIB,所有已装载MIB文件以txt格式保存在当前目录的mib子目
- //录下,搜索目录中所有MIB文件并依次装载。
- tempfile=CurrDir+"mib/*.txt";
- CFileFind fileread;
- BOOL nextfile=fileread.FindFile((LPCTSTR)tempfile);
- while (nextfile)
- {
- nextfile=fileread.FindNextFile();
- //跳过oid.txt文件;
- if (fileread.GetFileName()=="oid.txt") continue;
- tempfile=fileread.GetFilePath();
- //调用函数装载MIB文件;
- if (LoadMib(tempfile)==false)
- AfxMessageBox("装载文件出错!");
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMibBrowserView printing
- BOOL CMibBrowserView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CMibBrowserView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CMibBrowserView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- void CMibBrowserView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
- {
- // TODO: add customized printing code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMibBrowserView diagnostics
- #ifdef _DEBUG
- void CMibBrowserView::AssertValid() const
- {
- CFormView::AssertValid();
- }
- void CMibBrowserView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- CMibBrowserDoc* CMibBrowserView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMibBrowserDoc)));
- return (CMibBrowserDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMibBrowserView message handlers
- //GET操作;
- void CMibBrowserView::OnGet()
- {
- HTREEITEM hNode;
- MibNode* pNodeData;
- CString ipadd,community,oidstr;
- m_ipadd.GetWindowText(ipadd);
- if(m_ipadd.IsBlank())
- {
- AfxMessageBox("IP地址错误!");
- return ;
- }
- m_ipadd.GetWindowText(ipadd);
- m_community.GetWindowText(community);
- m_oid.GetWindowText(oidstr);
- ipadd+=":161";
- Snmp::socket_startup();
- UdpAddress address((LPCTSTR)ipadd);
- Oid oid((LPCTSTR)oidstr);
- snmp_version version=version1;
- int status;
- Snmp snmp(status, 0, false);
- Pdu pdu;
- Vb vb;
- vb.set_oid(oid);
- pdu += vb;
- CTarget ctarget(address);
- ctarget.set_version(version);
- ctarget.set_retry(1);
- ctarget.set_timeout(100);
- ctarget.set_readcommunity((LPCTSTR)community);
- SnmpTarget *target;
- target = &ctarget;
- status = snmp.get(pdu, *target);
- if (status == SNMP_CLASS_SUCCESS)
- {
- pdu.get_vb(vb,0);
- CString reply_oid=vb.get_printable_oid();
- CString reply_value=vb.get_printable_value();
- hNode=SearchNode(reply_oid);
- if (hNode!=NULL)
- {
- pNodeData=(MibNode*)m_tree.GetItemData(hNode);
- reply_oid.Replace((LPCTSTR)pNodeData->POid,
- (LPCTSTR)m_tree.GetItemText(hNode));
- if (pNodeData->PInteger!=NULL)
- {
- POSITION index=pNodeData->PInteger->Find(reply_value);
- if (index!=NULL)
- {
- pNodeData->PInteger->GetNext(index);
- reply_value=pNodeData->PInteger->GetNext(index);
- }
- }
- }
- if (m_list.GetItemCount()>0)
- m_list.DeleteAllItems();
- int row=m_list.InsertItem(1,reply_oid);
- m_list.SetItemText(row,1,reply_value);
- }
- Snmp::socket_cleanup();
- CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
- int num=m_list.GetItemCount();
- oidstr.Format("%d",num);
- oidstr="共取回 "+oidstr+" 个对象";
- pF->m_wndStatusBar.SetPaneText(0,oidstr);
- }
-
- void CMibBrowserView::OnGetnext()
- {
- HTREEITEM hNode;
- MibNode* pNodeData;
- CString ipadd,community,oidstr;
- m_ipadd.GetWindowText(ipadd);
- if(m_ipadd.IsBlank())
- {
- AfxMessageBox("IP地址错误!");
- return ;
- }
- m_ipadd.GetWindowText(ipadd);
- ipadd+=":161";
- m_community.GetWindowText(community);
- m_oid.GetWindowText(oidstr);
- Snmp::socket_startup();
- UdpAddress address((LPCTSTR)ipadd);
- Oid oid((LPCTSTR)oidstr);
- snmp_version version=version1;
- int status;
- Snmp snmp(status, 0, false);
- Pdu pdu;
- Vb vb;
- vb.set_oid(oid);
- pdu += vb;
- CTarget ctarget( address);
- ctarget.set_version( version);
- ctarget.set_retry(1);
- ctarget.set_timeout(100);
- ctarget.set_readcommunity((LPCTSTR)community);
- SnmpTarget *target;
- target = &ctarget;
- status = snmp.get_next(pdu, *target);
- if (status == SNMP_CLASS_SUCCESS)
- {
- pdu.get_vb(vb,0);
- CString reply_oid=vb.get_printable_oid();
- CString reply_value=vb.get_printable_value();
- hNode=SearchNode(reply_oid);
- if (hNode!=NULL)
- {
- pNodeData=(MibNode*)m_tree.GetItemData(hNode);
- reply_oid.Replace((LPCTSTR)pNodeData->POid,
- (LPCTSTR)m_tree.GetItemText(hNode));
- if (pNodeData->PInteger!=NULL)
- {
- POSITION index=pNodeData->PInteger->Find(reply_value);
- if (index!=NULL)
- {
- pNodeData->PInteger->GetNext(index);
- reply_value=pNodeData->PInteger->GetNext(index);
- }
- }
- }
- if (m_list.GetItemCount()>0)
- m_list.DeleteAllItems();
- int row=m_list.InsertItem(1,reply_oid);
- m_list.SetItemText(row,1,reply_value);
- }
- else
- AfxMessageBox("操作失败,请检查配置!");
- Snmp::socket_cleanup();
- CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
- int num=m_list.GetItemCount();
- oidstr.Format("%d",num);
- oidstr="共取回 "+oidstr+" 个对象";
- pF->m_wndStatusBar.SetPaneText(0,oidstr);
- }
- void CMibBrowserView::OnGetsubtree()
- {
- CString ipadd,community,oidstr;
- m_ipadd.GetWindowText(ipadd);
- if(m_ipadd.IsBlank())
- {
- AfxMessageBox("IP地址错误!");
- return ;
- }
- m_ipadd.GetWindowText(ipadd);
- ipadd+=":161";
- m_community.GetWindowText(community);
- m_oid.GetWindowText(oidstr);
- Snmp::socket_startup();
- UdpAddress address((LPCTSTR)ipadd);
- Oid Baseoid((LPCTSTR)oidstr);
- snmp_version version=version1;
- int status;
- Snmp snmp(status, 0, false);
- Pdu pdu;
- Vb vb;
- vb.set_oid(Baseoid);
- pdu += vb;
- CTarget ctarget( address);
- ctarget.set_version( version);
- ctarget.set_retry(1);
- ctarget.set_timeout(100);
- ctarget.set_readcommunity((LPCTSTR)community);
- SnmpTarget *target;
- target = &ctarget;
- Oid NextOid(Baseoid);
- NextOid+=".1";
- int number=1;
- if (m_list.GetItemCount()>0)
- m_list.DeleteAllItems();
- CString reply_oid;
- CString reply_value;
- MibNode* pNodeData;
- HTREEITEM hNode=NULL;
- //bool seek=true;
- while (Baseoid.nCompare(Baseoid.len(),NextOid)==0)
- {
- status = snmp.get_next(pdu, *target);
- if (status == SNMP_CLASS_SUCCESS)
- {
- pdu.get_vb(vb,0);
- reply_oid=vb.get_printable_oid();
- reply_value=vb.get_printable_value();
- vb.get_oid(NextOid);
- if (Baseoid.nCompare(Baseoid.len(),NextOid)==0)
- {
- hNode=SearchNode(reply_oid);
- if (hNode!=NULL)
- {
- pNodeData=(MibNode*)m_tree.GetItemData(hNode);
- if (reply_oid.Find((LPCTSTR)pNodeData->POid)==0)
- reply_oid.Replace((LPCTSTR)pNodeData->POid,
- (LPCTSTR)m_tree.GetItemText(hNode));
- if (pNodeData->PInteger!=NULL)
- {
- POSITION index=pNodeData->PInteger->Find(reply_value);
- if (index!=NULL)
- {
- pNodeData->PInteger->GetNext(index);
- reply_value=pNodeData->PInteger->GetNext(index);
- }
- }
-
- }
- //在列表中显示;
- int row=m_list.InsertItem(number,reply_oid);
- m_list.SetItemText(row,1,reply_value);
- number++;
- }
- }
- vb.set_oid(NextOid);
- vb.set_null();
- pdu.set_vb(vb,0);
-
- }
- Snmp::socket_cleanup();
- CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
- int num=m_list.GetItemCount();
- oidstr.Format("%d",num);
- oidstr="共取回 "+oidstr+" 个对象";
- pF->m_wndStatusBar.SetPaneText(0,oidstr);
- }
- //遍历MIB的函数;
- void CMibBrowserView::OnWalk()
- {
- CString ipadd,community,oidstr;
- m_ipadd.GetWindowText(ipadd);
- if(m_ipadd.IsBlank())
- {
- AfxMessageBox("IP地址错误!");
- return ;
- }
- m_ipadd.GetWindowText(ipadd);
- ipadd+=":161";
- m_community.GetWindowText(community);
- m_oid.GetWindowText(oidstr);
- Snmp::socket_startup();
- UdpAddress address((LPCTSTR)ipadd);
- Oid BaseOid((LPCTSTR)oidstr);
- snmp_version version=version1;
- int status;
- Snmp snmp(status, 0, false);
- Pdu pdu;
- Vb vb;
- vb.set_oid(BaseOid);
- pdu += vb;
- CTarget ctarget( address);
- ctarget.set_version( version);
- ctarget.set_retry(1);
- ctarget.set_timeout(100);
- ctarget.set_readcommunity((LPCTSTR)community);
- SnmpTarget *target;
- target = &ctarget;
- Oid NextOid(BaseOid);
- int number=1;
- if (m_list.GetItemCount()>0)
- m_list.DeleteAllItems();
- CString reply_oid;
- CString reply_value;
- MibNode* pNodeData;
- HTREEITEM hNode=NULL;
- // bool Seek=true;
- do
- {
- BaseOid=NextOid;
- status = snmp.get_next(pdu, *target);
- if (status == SNMP_CLASS_SUCCESS)
- {
- pdu.get_vb(vb,0);
- reply_oid=vb.get_printable_oid();
- reply_value=vb.get_printable_value();
- hNode=SearchNode(reply_oid);
- if (hNode!=NULL)
- {
- pNodeData=(MibNode*)m_tree.GetItemData(hNode);
- if (reply_oid.Find((LPCTSTR)pNodeData->POid)==0)
- reply_oid.Replace((LPCTSTR)pNodeData->POid,
- (LPCTSTR)m_tree.GetItemText(hNode));
- if (pNodeData->PInteger!=NULL)
- {
- POSITION index=pNodeData->PInteger->Find(reply_value);
- if (index!=NULL)
- {
- pNodeData->PInteger->GetNext(index);
- reply_value=pNodeData->PInteger->GetNext(index);
- }
- }
- }
- vb.get_oid(NextOid);
- int row=m_list.InsertItem(number,reply_oid);
- m_list.SetItemText(row,1,reply_value);
- number++;
- }
- vb.set_oid(NextOid);
- vb.set_null();
- pdu.set_vb(vb,0);
- } while (BaseOid!=NextOid) ;
- Snmp::socket_cleanup();
- CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
- int num=m_list.GetItemCount();
- oidstr.Format("%d",num);
- oidstr="共取回 "+oidstr+" 个对象";
- pF->m_wndStatusBar.SetPaneText(0,oidstr);
- }
- //set;
- void CMibBrowserView::OnSet()
- {
- CString ipadd,community,oidstr,n_value;
- int n_type;
- CSetDig set;
- if(set.DoModal()==IDOK)
- {
- n_value=set.m_value;
- n_type=set.m_type;
- if ((n_value.GetLength()==0)||(n_type==-1))
- {
- AfxMessageBox("未选择对象类型或值!");
- return;
- }
- }
- m_ipadd.GetWindowText(ipadd);
- if(m_ipadd.IsBlank())
- {
- AfxMessageBox("IP地址错误!");
- return ;
- }
- Pdu pdu;
- Vb vb;
- switch (n_type)
- {
- case 0:
- {
- unsigned long i;
- i = atol((LPCTSTR)n_value);
- long l;
- l = (long) i;
- vb.set_value(l);
- }
- case 1:
- {
- OctetStr octetstr((LPCTSTR)n_value);
- if ( octetstr.valid())
- vb.set_value(octetstr);
- }
- case 2:
- {
- Oid oid1((LPCTSTR)n_value);
- if ( oid1.valid())
- vb.set_value(oid1);
- }
- }
- m_ipadd.GetWindowText(ipadd);
- ipadd+=":161";
- m_community.GetWindowText(community);
- m_oid.GetWindowText(oidstr);
- Snmp::socket_startup();
- UdpAddress address((LPCTSTR)ipadd);
- Oid oid((LPCTSTR)oidstr);
- vb.set_oid(oid);
- snmp_version version=version1;
- pdu += vb;
- CTarget ctarget(address);
- ctarget.set_version(version);
- ctarget.set_retry(1);
- ctarget.set_timeout(100);
- ctarget.set_writecommunity((LPCTSTR)community);
- SnmpTarget *target;
- target = &ctarget;
- int status;
- Snmp snmp(status, 0, false);
- if ( status != SNMP_CLASS_SUCCESS)
- {
- AfxMessageBox("SNMP++ Session Create Failed");
- return ;
- }
- status=snmp.set(pdu, *target);
- if ( status == SNMP_CLASS_SUCCESS)
- AfxMessageBox("设置操作成功!");
- else
- AfxMessageBox(snmp.error_msg(status));
- Snmp::socket_cleanup(); // Shut down socket subsystem
- }
-
- //增加的事件处理函数,在程序退出时销毁动态生成的对象;
- void CMibBrowserView::OnDestroy()
- {
- CFormView::OnDestroy();
- HRoot=m_tree.GetRootItem();//从根节点开始;
- travl(HRoot);//遍历整个树;
- }
- //在节点展开与闭合时改变节点图标;
- void CMibBrowserView::OnItemexpandedTree1(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
- HTREEITEM hnode=pNMTreeView->itemNew.hItem;
- //不是叶子节点和根节点时才改变图标;
- if ((m_tree.ItemHasChildren(hnode))&&(m_tree.GetRootItem()!=hnode))
- {
- if (pNMTreeView->itemNew.iImage==1)
- m_tree.SetItemImage(hnode,2,2);
- if (pNMTreeView->itemNew.iImage==2)
- m_tree.SetItemImage(hnode,1,1);
- }//增加的代码
- *pResult = 0;
- }
- void CMibBrowserView::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
- HTREEITEM hnode=pNMTreeView->itemNew.hItem;
- //二级节点,MIB文件名,返回;
- // if (m_tree.GetParentItem(hnode)==m_tree.GetRootItem())
- // return;
- //不是根节点,显示节点详细信息;
- if (m_tree.GetRootItem()!=hnode)
- {
- MibNode *pMibNode;
- //获取节点data指针;
- pMibNode=(MibNode *)m_tree.GetItemData(hnode);
- CString str;
- //获取节点语法信息;
- str="SYNTAX: "+pMibNode->PSnytax+"rn";
- CString str1;
- //如果是枚举型整数,获取保存在PInteger中的信息;
- if (pMibNode->PInteger!=NULL)
- {
- POSITION pos;
- CString str2;
- //遍历整个列表,第一项为数字,紧随的一项是数字的意义解释;
- for(pos=pMibNode->PInteger->GetHeadPosition(); pos!=NULL;)
- {
- str2=pMibNode->PInteger->GetNext(pos)+" : ";
- str2=str2+pMibNode->PInteger->GetNext(pos)+"rn";
- str1=str1+str2;
- }
- }
- str+=str1;
- str=str+"ACCESS:"+pMibNode->PAccess+"rn";
- str=str+"STATUS:"+pMibNode->PStatus+"rn";
- //如果有INDEX信息,取出;
- if (pMibNode->PIndex!=' ')
- str=str+"INDEX:"+pMibNode->PIndex+"rn";
- str=str+"OID:"+pMibNode->POid+"rn";
- str=str+"DESCRPTION:"+pMibNode->PDescr+"rn";
- //在编辑控件中显示;
- m_edit1=((LPCTSTR)str);
- m_oid.SetWindowText(pMibNode->POid);
- UpdateData(FALSE);
- }
- //以上是增加的代码
- *pResult = 0;
- }
- void CMibBrowserView::OnLoad()
- {
- // TODO: Add your command handler code here
- CString filename,Dfilename;
- CFileDialog openfile(TRUE,NULL," *.txt",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST,
- "MIB files");
- openfile.DoModal();
- Dfilename=openfile.GetFileName();
- filename=openfile.GetPathName();
- if (LoadMib(filename)==false)
- AfxMessageBox("装载文件 "+filename+" 出错!");
- else
- {
- AfxMessageBox("装载成功!");
- Dfilename=CurrDir+"mib/"+Dfilename;
- CopyFile((LPCTSTR)filename,(LPCTSTR)Dfilename,false);
- }
- }
- //注册节点,需要输入父节点,新增加节点以及新节点OID。每次
- //注册成功的节点同时写入OID.TXT文件,下次程序启动时候读入;
- void CMibBrowserView::OnOid()
- {
- // TODO: Add your command handler code here
- CString parent;
- CString name,oidstr;
- CDlg_input inputD;
- inputD.m_ParentName="enterprises";
- if (inputD.DoModal()==IDOK)
- {
- parent=inputD.m_ParentName;
- name=inputD.m_NodeName;
- oidstr=inputD.m_NodeOid;
- oidstr="1.3.6.1.4.1."+oidstr;
- HTREEITEM hp,hr;
- hr=m_tree.GetRootItem();
- hp=FindNode(hr,parent);
- if (hp!=NULL)
- {
- hr=m_tree.InsertItem((LPCTSTR)name,1,1,hp);
- MibNode* pNode;
- pNode=new MibNode;
- InitNodeData(pNode);
- pNode->POid=oidstr;
- pNode->PAccess="not-accessable";
- m_tree.SetItemData(hr,(DWORD)pNode);
- AfxMessageBox("注册成功!");
- CStdioFile OidFile;
- if(OidFile.Open("mib/oid.txt",CFile::modeWrite))
- {
- OidFile.SeekToEnd();
- OidFile.WriteString(parent+"rn");
- OidFile.WriteString(name+"rn");
- OidFile.WriteString(oidstr+"rn");
- OidFile.Close();
- }
- else
- AfxMessageBox("打开文件oid.txt错误!");
- }
-
- }
- }
- void CMibBrowserView::OnSave()
- {
- // TODO: Add your command handler code here
- CStdioFile kvFile;
- CString line;
- CFileDialog openfile(FALSE,"*.txt","mib",
- OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
- "Text files(*.txt)|*.txt");
- if (openfile.DoModal()==IDOK)
- {
- if(kvFile.Open(openfile.GetPathName(),
- CFile::modeWrite|CFile::modeCreate))
- {
- int num=m_list.GetItemCount();
- for (int i=0;i<num;++i)
- {
- line=m_list.GetItemText(i,0);
- line=line+"="+m_list.GetItemText(i,1)+"rn";
- kvFile.WriteString(line);
- }
-
- }
- kvFile.Close();
- }
- }