IP层抓包

LabWindows/CVI是National Instruments公司推出的一套面向测控领域的软件开发平台。它以ANSI C为核心,将功能强大,使用灵活的C语言平台与数据采集,分析和表达的测控专业工具有机地接和起来。它的集成化开发平台,交互式编程方法,丰富的控件和库函数大大增强了C语言的功能,为熟悉C语言的开发人员建立检测系统,自动测量环境,数据采集系统,过程监控系统等提供了一个理想的软件开发环境。

LabWindows同时也支持Win32 SDK;能进行Win32应用程式开发.下例是以Socket API写的IP层抓包程序,功能非常简单,封包内容并没有详细分析,有兴趣的朋友可以试试做些协议分析;

在主Panel上添加两个Ring控件,3个Command Button,和一个Table,在Table创建5列;

  1. #include      
  2. #include 
  3. #include 
  4. #include 
  5. #include 
  6. #include "main.h"
  7. #include "iphead.h"
  8.                                      
  9. static int panelHandle;
  10. SOCKET socket_id;
  11. HANDLE hThread;
  12. DWORD WINAPI CaptureThread(void*);
  13. int InitSocket(void){
  14.     u_short wVersionRequested = 0x0202;
  15.     WSADATA wsaData;
  16.     
  17.     if(SOCKET_ERROR == WSAStartup( wVersionRequested, &wsaData)){
  18.         return 0;   
  19.     }
  20.     return -1;
  21. }
  22. int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  23.                        LPSTR lpszCmdLine, int nCmdShow)
  24. {
  25.     if (InitSocket() == 0){
  26.         return -1;  
  27.     }
  28.     if (InitCVIRTE (hInstance, 0, 0) == 0)
  29.         return -1;  /* out of memory */
  30.     if ((panelHandle = LoadPanel (0, "main.uir", PANEL)) < 0)
  31.         return -1;
  32.     DisplayPanel (panelHandle);
  33.     RunUserInterface ();
  34.     DiscardPanel (panelHandle);
  35.     return 0;
  36. }
  37. int CVICALLBACK BUTTON_EXIT (int panel, int control, int event,
  38.         void *callbackData, int eventData1, int eventData2)
  39. {
  40.     switch (event)
  41.     {
  42.         case EVENT_COMMIT:
  43.             QuitUserInterface (0);
  44.             break;
  45.     }
  46.     return 0;
  47. }
  48. int CVICALLBACK BUTTON_BEGIN_CAPTURE (int panel, int control, int event,
  49.         void *callbackData, int eventData1, int eventData2)
  50. {
  51.     switch (event)
  52.     {
  53.         case EVENT_COMMIT:{
  54.                 char FAR name[128];
  55.                 struct hostent FAR * pHostent;
  56.                 struct sockaddr_in addr;
  57.                 DWORD wt        = 0;
  58.                 u_long sioarg   = 1;
  59.                 u_long bioarg =  0;
  60.                 SetCtrlVal (panelHandle, PANEL_NUMERICMETER, 0);
  61.                 socket_id      = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
  62.                 if(socket_id    == INVALID_SOCKET){
  63.                     return 0;   
  64.                 }
  65.                 
  66.                 if(gethostname(name, sizeof(name)) == -1){
  67.                     closesocket(socket_id);
  68.                     return 0;
  69.                 }
  70.                 
  71.                 pHostent        = gethostbyname(name);
  72.                 addr.sin_family = AF_INET;
  73.                 addr.sin_addr   = *(struct in_addr*)pHostent->h_addr;
  74.                 addr.sin_port   = 0; 
  75.                 
  76.                 //SetCtrlVal (panelHandle, PANEL_STRING,pHostent->h_name);
  77.                 
  78.                 if( SOCKET_ERROR == bind(socket_id,(struct sockaddr *)&addr,sizeof(addr) )){
  79.                     closesocket(socket_id);
  80.                     return 0;
  81.                 }
  82.                 if(SOCKET_ERROR == WSAIoctl(socket_id, SIO_RCVALL, &sioarg,sizeof(sioarg),NULL,0,&wt,NULL,NULL)){
  83.                     closesocket(socket_id);
  84.                     return 0;
  85.                 }
  86.                 
  87.                 if(SOCKET_ERROR == ioctlsocket(socket_id, FIONBIO , &bioarg)){
  88.                     closesocket(socket_id);
  89.                     return 0;
  90.                 }
  91.                 
  92.                 DeleteTableRows (panelHandle,PANEL_TABLE ,1,-1);
  93.                 
  94.                 if((hThread=CreateThread(NULL,0,CaptureThread,NULL,0,NULL)) == NULL){
  95.                     closesocket(socket_id);
  96.                     return 0;
  97.                 }
  98.                 
  99.                 SetCtrlAttribute (panelHandle, PANEL_COMMANDBUTTON, ATTR_DIMMED, 1);
  100.                 SetCtrlAttribute (panelHandle, PANEL_COMMANDBUTTON_2, ATTR_DIMMED, 1);
  101.                 SetCtrlAttribute (panelHandle, PANEL_COMMANDBUTTON_3, ATTR_DIMMED, 0);
  102.                 break;
  103.             }
  104.     }
  105.     return 0;
  106. }
  107. int CVICALLBACK BUTTON_STOP_CAPTURE (int panel, int control, int event,
  108.         void *callbackData, int eventData1, int eventData2)
  109. {
  110.     switch (event)
  111.     {
  112.         case EVENT_COMMIT:
  113.             closesocket(socket_id);
  114.             WaitForSingleObject(hThread,INFINITE);
  115.             CloseHandle(hThread);
  116.             
  117.             SetCtrlAttribute (panelHandle, PANEL_COMMANDBUTTON, ATTR_DIMMED, 0);
  118.             SetCtrlAttribute (panelHandle, PANEL_COMMANDBUTTON_2, ATTR_DIMMED, 0);
  119.             SetCtrlAttribute (panelHandle, PANEL_COMMANDBUTTON_3, ATTR_DIMMED, 1);
  120.             break;
  121.     }
  122.     return 0;
  123. }
  124. DWORD WINAPI  CaptureThread(void* param){
  125.     char buf[65536];
  126.     int len = 0;
  127.             
  128.     do{
  129.         len = recv(socket_id, buf, sizeof(buf),0);
  130.         if(len > 0){
  131.             if(len > sizeof(IP_PK_HEAD)){
  132.                 IP_PK_HEAD iphead;
  133.                 struct in_addr src,dst;
  134.                 int insert_row_index;
  135.                 char strsrc[20];
  136.                 char strdst[20];
  137.                 char str_ver[8];
  138.                 char str_len[12];
  139.                 char str_ProtocolType[8];
  140.                 char str_PacketLen[12];
  141.                 char str_Sequence[12];
  142.                 
  143.                 memcpy( &iphead, buf, sizeof(iphead));
  144.                 iphead.wPacketLen = ntohs(iphead.wPacketLen);
  145.                 iphead.wSequence = ntohs(iphead.wSequence);
  146.                 iphead.wHeadCheckSum = ntohs(iphead.wHeadCheckSum);
  147.                 
  148.                 src.S_un.S_addr = iphead.dwIPSrc;
  149.                 dst.S_un.S_addr = iphead.dwIPDes;
  150.                 
  151.                 strcpy(strsrc, inet_ntoa(src));
  152.                 strcpy(strdst, inet_ntoa(dst));
  153.                 
  154.                 sprintf(str_ver,"%d",iphead.ver_hlen >> 4);
  155.                 sprintf(str_len,"%d",(iphead.ver_hlen & 0x0F) << 2);     
  156.                 sprintf(str_PacketLen,"%d",iphead.wPacketLen);
  157.                 sprintf(str_Sequence,"%d",iphead.wSequence);
  158.                 
  159.                 GetNumTableRows(panelHandle,PANEL_TABLE,&insert_row_index);
  160.                 insert_row_index++;
  161.                 InsertTableRows (panelHandle, PANEL_TABLE, insert_row_index, 1, VAL_CELL_STRING);
  162.                 SetTableCellVal (panelHandle, PANEL_TABLE, MakePoint(1,insert_row_index), str_ver);
  163.                 SetTableCellVal (panelHandle, PANEL_TABLE, MakePoint(2,insert_row_index), str_len);
  164.                 SetTableCellVal (panelHandle, PANEL_TABLE, MakePoint(3,insert_row_index), getProtocolName(str_ProtocolType,iphead.byProtocolType));
  165.                 SetTableCellVal (panelHandle, PANEL_TABLE, MakePoint(4,insert_row_index), str_PacketLen);
  166.                 SetTableCellVal (panelHandle, PANEL_TABLE, MakePoint(5,insert_row_index), str_Sequence);
  167.                 SetTableCellVal (panelHandle, PANEL_TABLE, MakePoint(6,insert_row_index), strsrc);
  168.                 SetTableCellVal (panelHandle, PANEL_TABLE, MakePoint(7,insert_row_index), strdst);
  169.             }
  170.             SetCtrlVal (panelHandle, PANEL_NUMERICMETER, len / 20);
  171.         }
  172.     }while(len > 0);
  173.     
  174.     return 0;
  175. }
  176. char * getProtocolName(char *namebuffer,BYTE byProtocolType){
  177.     switch(byProtocolType){
  178.         case PROTOCOL_TCP:
  179.             strcpy(namebuffer,"TCP");
  180.             return namebuffer;
  181.         case PROTOCOL_UDP:
  182.             strcpy(namebuffer,"UDP");
  183.             return namebuffer;
  184.         case PROTOCOL_ICMP:
  185.             strcpy(namebuffer,"ICMP");
  186.             return namebuffer;
  187.         case PROTOCOL_IGMP:
  188.             strcpy(namebuffer,"IGMP");
  189.             return namebuffer;
  190.         default:
  191.             strcpy(namebuffer,"UNKNOW");
  192.             return namebuffer;
  193.     }
  194. }
  195. int CVICALLBACK PANEL_FORM (int panel, int event, void *callbackData,
  196.         int eventData1, int eventData2)
  197. {
  198.     switch (event)
  199.     {
  200.         case EVENT_GOT_FOCUS:
  201.             break;
  202.         case EVENT_LOST_FOCUS:
  203.             break;
  204.         case EVENT_CLOSE:
  205.             closesocket(socket_id);
  206.             WSACleanup();
  207.             WaitForSingleObject(hThread,INFINITE);
  208.             CloseHandle(hThread);
  209.             break;
  210.     }
  211.     return 0;
  212. }

你可能感兴趣的:(测试开发CVI,LabView,Teststand)