Packet开发范例.doc_第1页
Packet开发范例.doc_第2页
Packet开发范例.doc_第3页
Packet开发范例.doc_第4页
Packet开发范例.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第一个程序:Testapp 这是一个非常简单的捕获程序,用来测试packet.dll。它是一个控制台应用程序,执行时,testapp给用户选择一个在本机器上安装好的适配器,然后它就可以从指定的网络适配器上面捕获包知道某个键按下,同时把每个包的内容打印在屏幕上。 00001 /*00002 * Copyright (c) 1999 - 200200003 * Politecnico di Torino. All rights reserved.00004 *00005 * Redistribution and use in source and binary forms, with or without00006 * modification, are permitted provided that: (1) source code distributions00007 * retain the above copyright notice and this paragraph in its entirety, (2)00008 * distributions including binary code include the above copyright notice and00009 * this paragraph in its entirety in the documentation or other materials00010 * provided with the distribution, and (3) all advertising materials mentioning00011 * features or use of this software display the following acknowledgement:00012 * This product includes software developed by the Politecnico00013 * di Torino, and its contributors. Neither the name of00014 * the University nor the names of its contributors may be used to endorse00015 * or promote products derived from this software without specific prior00016 * written permission.00017 * THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR IMPLIED00018 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF00019 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.00020 */00021 00022 #include 00023 #include 00024 00025 00026 #include .Includepacket32.h00027 #include .Includentddndis.h00028 00029 #define Max_Num_Adapter 1000030 00031 / Prototypes00032 00033 void PrintPackets(LPPACKET lpPacket);00034 00035 char AdapterListMax_Num_Adapter1024;00036 00037 int main()00038 00039 00040 /define a pointer to an ADAPTER structure00041 00042 LPADAPTER lpAdapter = 0;00043 00044 /define a pointer to a PACKET structure00045 00046 LPPACKET lpPacket;00047 00048 int i;00049 DWORD dwErrorCode;00050 00051 DWORD dwVersion;00052 DWORD dwWindowsMajorVersion;00053 00054 /unicode strings (winnt)00055 WCHAR AdapterName8192; / string that contains a list of the network adapters00056 WCHAR *temp,*temp1;00057 00058 /ascii strings (win95)00059 char AdapterNamea8192; / string that contains a list of the network adapters00060 char *tempa,*temp1a;00061 00062 00063 int AdapterNum=0,Open;00064 ULONG AdapterLength;00065 00066 char buffer256000; / buffer to hold the data coming from the driver00067 00068 struct bpf_stat stat;00069 00070 /00071 / Obtain the name of the adapters installed on this machine00072 /00073 printf(Packet.dll test application. Library version:%sn, PacketGetVersion();00074 00075 printf(Adapters installed:n);00076 i=0; 00077 00078 / the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.00079 / We have to check the os on which we are running00080 dwVersion=GetVersion();00081 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion);00082 if (!(dwVersion = 0x80000000 & dwWindowsMajorVersion = 4)00083 / Windows NT00084 AdapterLength = sizeof(AdapterName);00085 00086 if(PacketGetAdapterNames(AdapterName,&AdapterLength)=FALSE)00087 printf(Unable to retrieve the list of the adapters!n);00088 return -1;00089 00090 temp=AdapterName;00091 temp1=AdapterName;00092 while (*temp!=0)|(*(temp-1)!=0)00093 00094 if (*temp=0) 00095 00096 memcpy(AdapterListi,temp1,(temp-temp1)*2);00097 temp1=temp+1;00098 i+;00099 00100 00101 temp+;00102 00103 00104 AdapterNum=i;00105 for (i=0;iAdapterNum;i+)00106 wprintf(Ln%d- %sn,i+1,AdapterListi);00107 printf(n);00108 00109 00110 00111 else /windows 9500112 00113 AdapterLength = sizeof(AdapterNamea);00114 00115 if(PacketGetAdapterNames(AdapterNamea,&AdapterLength)=FALSE)00116 printf(Unable to retrieve the list of the adapters!n);00117 return -1;00118 00119 tempa=AdapterNamea;00120 temp1a=AdapterNamea;00121 00122 while (*tempa!=0)|(*(tempa-1)!=0)00123 00124 if (*tempa=0) 00125 00126 memcpy(AdapterListi,temp1a,tempa-temp1a);00127 temp1a=tempa+1;00128 i+;00129 00130 tempa+;00131 00132 00133 AdapterNum=i;00134 for (i=0;iAdapterNum) printf(nThe number must be smaller than %d,AdapterNum); 00145 while (OpenAdapterNum);00146 00147 00148 00149 00150 lpAdapter = PacketOpenAdapter(AdapterListOpen-1);00151 00152 if (!lpAdapter | (lpAdapter-hFile = INVALID_HANDLE_VALUE)00153 00154 dwErrorCode=GetLastError();00155 printf(Unable to open the adapter, Error Code : %lxn,dwErrorCode); 00156 00157 return -1;00158 00159 00160 / set the network adapter in promiscuous mode00161 00162 if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)=FALSE)00163 printf(Warning: unable to set promiscuous mode!n);00164 00165 00166 / set a 512K buffer in the driver00167 if(PacketSetBuff(lpAdapter,512000)=FALSE)00168 printf(Unable to set the kernel buffer!n);00169 return -1;00170 00171 00172 / set a 1 second read timeout00173 if(PacketSetReadTimeout(lpAdapter,1000)=FALSE)00174 printf(Warning: unable to set the read tiemout!n);00175 00176 00177 /allocate and initialize a packet structure that will be used to00178 /receive the packets.00179 if(lpPacket = PacketAllocatePacket()=NULL)00180 printf(nError: failed to allocate the LPPACKET structure.);00181 return (-1);00182 00183 PacketInitPacket(lpPacket,(char*)buffer,256000);00184 00185 /main capture loop00186 while(!kbhit()00187 00188 / capture the packets00189 if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)=FALSE)00190 printf(Error: PacketReceivePacket failed);00191 return (-1);00192 00193 00194 PrintPackets(lpPacket);00195 00196 00197 00198 /print the capture statistics00199 if(PacketGetStats(lpAdapter,&stat)=FALSE)00200 printf(Warning: unable to get stats from the kernel!n);00201 00202 else00203 printf(nn%d packets received.n%d Packets lost,stat.bs_recv,stat.bs_drop);00204 00205 PacketFreePacket(lpPacket);00206 00207 / close the adapter and exit00208 00209 PacketCloseAdapter(lpAdapter);00210 return (0);00211 00212 00213 / this function prints the content of a block of packets received from the driver00214 00215 void PrintPackets(LPPACKET lpPacket)00216 00217 00218 ULONG i, j, ulLines, ulen, ulBytesReceived;00219 char *pChar, *pLine, *base;00220 char *buf;00221 u_int off=0;00222 u_int tlen,tlen1;00223 struct bpf_hdr *hdr;00224 00225 ulBytesReceived = lpPacket-ulBytesReceived;00226 00227 00228 buf = lpPacket-Buffer;0

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论