資源簡(jiǎn)介
用winpcap在局域網(wǎng)實(shí)現(xiàn)文件傳輸 自己定義包結(jié)構(gòu)
代碼片段和文件信息
#include??
#include??
#include??
#include??
#include??
#include?“packet.h“
#pragma?comment?(lib“wpcap“)
unsigned?char?*source_mac=NULL; //本機(jī)MAC地址//一個(gè)pcap實(shí)例
int?main()
{
pcap_if_t?*alldevs;
pcap_if_t?*d;
int?inum;
int?i=0;
pcap_t?*adhandle;
int?res;
char?errbuf[PCAP_ERRBUF_SIZE];
struct?pcap_pkthdr?*header;
const?u_char?*pkt_data;
????
????
????/*?獲取本機(jī)設(shè)備列表?*/
????if?(pcap_findalldevs_ex(PCAP_SRC_IF_STRING?NULL?&alldevs?errbuf)?==?-1)
????{
????????fprintf(stderr“Error?in?pcap_findalldevs:?%s\n“?errbuf);
????????exit(1);
????}
????
????/*?打印列表?*/
????for(d=alldevs;?d;?d=d->next)
????{
????????printf(“%d.?%s“?++i?d->name);
????????if?(d->description)
????????????printf(“?(%s)\n“?d->description);
????????else
????????????printf(“?(No?description?available)\n“);
????}
????
????if(i==0)
????{
????????printf(“\nNo?interfaces?found!?Make?sure?WinPcap?is?installed.\n“);
????????return?-1;
????}
????
????printf(“Enter?the?interface?number?(1-%d):“i);
????scanf(“%d“?&inum);
?????
????if(inum?1?||?inum?>?i)
????{
????????printf(“\nInterface?number?out?of?range.\n“);
????????/*?釋放設(shè)備列表?*/
????????pcap_freealldevs(alldevs);
????????return?-1;
????}
????
????/*?跳轉(zhuǎn)到已選中的適配器?*/
????for(d=alldevs?i=0;?inext?i++);
????
????/*?打開(kāi)設(shè)備?*/
????if?(?(adhandle=?pcap_open(d->name??????????//?設(shè)備名
??????????????????????????????65536????????????//?要捕捉的數(shù)據(jù)包的部分?
????????????????????????????????????????????????//?65535保證能捕獲到不同數(shù)據(jù)鏈路層上的每個(gè)數(shù)據(jù)包的全部?jī)?nèi)容
??????????????????????????????0????//?模式
??????????????????????????????-1?????????????//?讀取超時(shí)時(shí)間
??????????????????????????????NULL?????????????//?遠(yuǎn)程機(jī)器驗(yàn)證
??????????????????????????????errbuf????????????//?錯(cuò)誤緩沖池
??????????????????????????????)?)?==?NULL)
????{
????????fprintf(stderr“\nUnable?to?open?the?adapter.?%s?is?not?supported?by?WinPcap\n“?d->name);
????????/*?釋放設(shè)列表?*/
????????pcap_freealldevs(alldevs);
????????return?-1;
????}
????
????printf(“\nlistening?on?%s...\n“?d->description);
????
????/*?釋放設(shè)備列表?*/
????pcap_freealldevs(alldevs);
//獲取本機(jī)MAC
source_mac?=?GetSelfMac(d->name+8);?//+8以去掉“rpcap://“?
????
//獲取文件數(shù)據(jù)并存儲(chǔ)
HANDLE?hfile;
int?num=0;
????/*?獲取數(shù)據(jù)包?*/
????while((res?=?pcap_next_ex(?adhandle?&header?&pkt_data))?>=?0)
{
????????
????????if(res?==?0)
????????????/*?超時(shí)時(shí)間到?*/
????????????continue;
packet?pack;
memcpy(&packpkt_data22);
bool?bflag=0;
for(int?i=0;i<6;i++)??????????????????//判斷是否是自己發(fā)出的包
{
if(??pack.eth.source_mac[i]!=source_mac[i]??)
{
bflag=1;
break;
}
}
if(??bflag?&&?ntohs(pack.eth.eh_type)==0xffff??)?
{
//構(gòu)造回復(fù)包
packet?ackpack;
BYTE?ackpackbuf[22];
memcpy(ackpack.eth.dest_macpack.eth.source_macsizeof(ackpack.eth.dest_mac));
memcpy(ackpack.eth.source_macpack.eth.dest_macsizeof(ackpack.eth.source_mac));
memcpy(&ackpack.et
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????32768??2010-07-14?00:26??文件傳輸\receivefile\Debug\receivefile.exe
?????文件?????384428??2010-07-14?00:26??文件傳輸\receivefile\Debug\receivefile.ilk
?????文件?????551936??2010-07-14?00:26??文件傳輸\receivefile\Debug\receivefile.pdb
?????文件???????7574??2010-07-14?00:26??文件傳輸\receivefile\receivefile\Debug\BuildLog.htm
?????文件?????????65??2010-07-14?00:26??文件傳輸\receivefile\receivefile\Debug\mt.dep
?????文件????????663??2010-06-11?11:04??文件傳輸\receivefile\receivefile\Debug\receivefile.exe.em
?????文件????????728??2010-06-11?11:04??文件傳輸\receivefile\receivefile\Debug\receivefile.exe.em
?????文件????????621??2010-07-14?00:26??文件傳輸\receivefile\receivefile\Debug\receivefile.exe.intermediate.manifest
?????文件??????43371??2010-07-14?00:26??文件傳輸\receivefile\receivefile\Debug\receivefile.obj
?????文件?????723968??2010-07-14?00:26??文件傳輸\receivefile\receivefile\Debug\vc90.idb
?????文件?????151552??2010-07-14?00:26??文件傳輸\receivefile\receivefile\Debug\vc90.pdb
?????文件??????????0??2010-07-13?21:13??文件傳輸\receivefile\receivefile\file.txt
?????文件????????692??2010-07-13?22:47??文件傳輸\receivefile\receivefile\packet.h
?????文件???????5565??2010-07-14?00:26??文件傳輸\receivefile\receivefile\receivefile.cpp
?????文件???????4095??2010-06-11?11:02??文件傳輸\receivefile\receivefile\receivefile.vcproj
?????文件???????1421??2010-07-14?00:40??文件傳輸\receivefile\receivefile\receivefile.vcproj.liyanbing-PC.liyanbing.user
?????文件???12766208??2010-07-14?00:40??文件傳輸\receivefile\receivefile.ncb
?????文件????????899??2010-06-09?23:20??文件傳輸\receivefile\receivefile.sln
????..A..H.?????12800??2010-07-14?00:40??文件傳輸\receivefile\receivefile.suo
?????文件??????34304??2010-07-15?16:05??文件傳輸\sendfile\Debug\sendfile.exe
?????文件?????377692??2010-07-15?16:05??文件傳輸\sendfile\Debug\sendfile.ilk
?????文件?????535552??2010-07-15?16:05??文件傳輸\sendfile\Debug\sendfile.pdb
?????文件???????9426??2010-07-15?16:05??文件傳輸\sendfile\sendfile\Debug\BuildLog.htm
?????文件?????????65??2010-07-15?16:05??文件傳輸\sendfile\sendfile\Debug\mt.dep
?????文件????????663??2010-07-15?15:02??文件傳輸\sendfile\sendfile\Debug\sendfile.exe.em
?????文件????????728??2010-07-15?15:02??文件傳輸\sendfile\sendfile\Debug\sendfile.exe.em
?????文件????????621??2010-07-15?16:05??文件傳輸\sendfile\sendfile\Debug\sendfile.exe.intermediate.manifest
?????文件??????48334??2010-07-15?16:05??文件傳輸\sendfile\sendfile\Debug\sendfile.obj
?????文件?????445440??2010-07-15?16:05??文件傳輸\sendfile\sendfile\Debug\vc90.idb
?????文件?????151552??2010-07-15?16:05??文件傳輸\sendfile\sendfile\Debug\vc90.pdb
............此處省略20個(gè)文件信息
評(píng)論
共有 條評(píng)論