DoFindTopology.cpp
上传用户:yfy060102
上传日期:2021-05-22
资源大小:60k
文件大小:3k
源码类别:

SNMP编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MIB.h"
  3. #include "winsock2.h"
  4. #include "SNMPAppDlg.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. #define WM_ROUTERFINISH WM_USER+777
  11. #define WM_PRINTSTR WM_USER+778
  12. CObArray routerSet;  //CRouter
  13. CString infprint;
  14. int level;
  15. CSNMPAppDlg* pMainWnd=NULL;
  16. CEvent eStop;
  17. void RecalRouter(CString& strIp,CSnmp* psnmp);
  18. void printfToStr(CString& strInf);
  19. void strPrintf(const char * pstr,int style=0,int color=0);
  20. BOOL IsIpInRouterSet(CString& strIp);
  21. enum {normal=0,bold=1,italic=2};
  22. enum {black=0,blue=0x00ff0000,green=0x0000ff00,red=0x000000ff};
  23. UINT DoFindTopology(LPVOID pParam)
  24. {
  25. CString strTemp;
  26. //Initial snmp for GET operation
  27. CSnmp snmp;
  28. if (!snmp.CreateSession())
  29. return 1;
  30. pMainWnd=(CSNMPAppDlg*)pParam;
  31. CString ip=pMainWnd->m_input;
  32. snmp.m_community=pMainWnd->m_community;
  33. level=atoi(pMainWnd->m_num);
  34. if (level<0)
  35. {
  36. strPrintf("Level value error!n",italic,black);
  37. return 1;
  38. }
  39. strPrintf("Starting topology finding!n",italic,black);
  40. RecalRouter(ip,&snmp);
  41. strPrintf("Find Router End,OK!n",italic,red);
  42. pMainWnd->SendMessage(WM_ROUTERFINISH, 0, 0);
  43. // snmp.Close();
  44. // snmp.Cleanup();
  45. return 0;
  46. }
  47. void RecalRouter(CString& strIp,CSnmp* psnmp)
  48. {
  49. CString strTemp;
  50. if (level>3)
  51. {
  52. level--;
  53. return;
  54. }
  55. if ( strIp.CollateNoCase("0.0.0.0")==0 ||
  56. strIp.CollateNoCase("127.0.0.1")==0 )
  57. {
  58. level--;
  59. return;
  60. }
  61. if (IsIpInRouterSet(strIp))
  62. {
  63. level--;
  64. return;
  65. }
  66. strTemp.Format("Trying an Ip address:%s ...n",strIp);
  67. strPrintf(strTemp,italic,black);
  68. CRouter* pRt=new CRouter();
  69. pRt->m_ipAddr=strIp;
  70. if (!pRt->GetRouterInfo(psnmp))
  71. {
  72. strPrintf("Router unkown.n",italic,red);
  73. delete pRt;
  74. level--;
  75. return;
  76. }
  77. routerSet.Add(pRt);
  78. strTemp.Format("Find a Router:%s!n",pRt->m_ipAddr);
  79. strPrintf(strTemp,italic,red);
  80. pRt->PrintRouterInfo();
  81. level++;
  82. for (int j=0;j<pRt->m_ipRouteTable.m_ipRouteTableEntries.GetSize();j++)
  83. {
  84. CipRouteTableEntry* pEntry=
  85. (CipRouteTableEntry*)(pRt->m_ipRouteTable.m_ipRouteTableEntries.GetAt(j));
  86. RecalRouter(pEntry->ipRouteNextHop,psnmp);
  87. }
  88. }
  89. void strPrintf(const char * pstr,int style,int color)
  90. {
  91. infprint=pstr;
  92. ASSERT(pMainWnd);
  93. pMainWnd->SendMessage(WM_PRINTSTR,style,color);
  94. return ;
  95. }
  96. BOOL IsIpInRouterSet(CString& strIp)
  97. {
  98. for (int i=0;i<routerSet.GetSize();i++)
  99. {
  100. CRouter* pRouter=(CRouter*)routerSet.GetAt(i);
  101. for (int j=0;
  102. j<pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetSize();j++)
  103. {
  104. CipAddrTableEntry* pEntry=
  105. (CipAddrTableEntry*)pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetAt(j);
  106. if (pEntry->ipAdEntAddr.CompareNoCase(strIp)==0)
  107. return TRUE;
  108. }
  109. }
  110. return FALSE;
  111. }