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

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_SUBNETFINISH WM_USER+779
  12. extern CObArray routerSet;
  13. CStringArray subnetIpSet;
  14. BOOL g_Stop;
  15. BOOL InitPing();
  16. BOOL PingSubnet(CString& host,CString& netmask);
  17. BOOL Ping(char* sHost);
  18. void strPrintf(const char * pstr,int style=0,int color=0);
  19. enum {normal=0,bold=1,italic=2};
  20. enum {black=0,blue=0x00ff0000,green=0x0000ff00,red=0x000000ff};
  21. UINT DoFindSubnet(LPVOID pParam)
  22. {
  23. g_Stop=FALSE;
  24. CString strTemp;
  25. if (!InitPing())
  26. return 0;
  27. CSNMPAppDlg* pMainWnd=(CSNMPAppDlg*)pParam;
  28. strPrintf("nnStart subnet finding...n",italic,black);
  29. for (int i=0;i<routerSet.GetSize();i++)
  30. {
  31. CRouter* pRouter=(CRouter*)routerSet.GetAt(i);
  32. strTemp.Format("nFinding subnet of router %s:n",
  33. pRouter->m_sys.sysName);
  34. strPrintf(strTemp,italic,black);
  35. for (int j=0;j<pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetSize();j++)
  36. {
  37. CipAddrTableEntry* pEntry=
  38. (CipAddrTableEntry*)pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetAt(j);
  39. strTemp.Format("find subnet in interface %sn",pEntry->ipAdEntAddr);
  40. strPrintf(strTemp,italic,black);
  41. PingSubnet(pEntry->ipAdEntAddr,pEntry->ipAdEntNetMask);
  42. }
  43. strTemp.Format("The End of finding subnet of router %s:n",
  44. pRouter->m_sys.sysName);
  45. strPrintf(strTemp,italic,red);
  46. }
  47. strPrintf("Find subnet OK!n",italic,red);
  48. pMainWnd->SendMessage(WM_SUBNETFINISH, 0, 0);
  49. return 1;
  50. }
  51. BOOL PingSubnet(CString& host,CString& netmask)
  52. {
  53. CString temp;
  54. unsigned long subnetIp,subnetMask,currIp;
  55. if ( host.CompareNoCase("127.0.0.1")==0 ||
  56. host.CompareNoCase("0.0.0.0")==0 )
  57. {
  58. strPrintf("No need to find subnet.n",italic,red);
  59. return FALSE;
  60. }
  61. subnetMask=inet_addr(netmask);
  62. subnetIp=inet_addr(host);
  63. currIp=subnetIp;
  64. subnetIp &= subnetMask;
  65. temp=inet_ntoa(*((struct in_addr*)(&subnetIp)));
  66. unsigned long count=(~subnetMask)>>24;
  67. for (unsigned long i=1;i<count;i++)
  68. {
  69. currIp=subnetIp+(i<<24);
  70. CString strCurrIp=inet_ntoa(*((struct in_addr*)(&currIp)));
  71. if (g_Stop)
  72. {
  73. strPrintf("nThread stop.n",italic,red);
  74. return FALSE;
  75. }
  76. if (Ping(strCurrIp.GetBuffer(15)))
  77. {
  78. subnetIpSet.Add(strCurrIp);
  79. temp.Format("  %s",strCurrIp);
  80. strPrintf(temp,bold,blue);
  81. }
  82. }
  83. strPrintf("nn");
  84. return TRUE;
  85. }