どうも~。こんにちは。
さてさて、今C++パケットキャプチャの開発を考えています。
半分は授業(C++)の課題制作なんですが。
んで、window用のライブラリwinPcapを使ってみることにします。
お約束:
以下の手順を実行した上でのいかなる損害も補償しません。
自己責任でどうぞ。
~winPcapのサンプルが動くまで~
OS:Windows XP SP2
IDE:Visual Studio.NET 2003
Language:Visual C++
Lib:winPcap 3.1
手順1:winPcapのインストール
http://www.winpcap.org/
↑のサイトから「Get WinPcap」へ進む。
今回winPcapのバージョンは資料やサンプルが豊富なので3.1を使います。
WinPcap auto-installerをダウンロードしてインストール。
これをインストールしないとwinPcapを使用したアプリは動きません。
手順2:開発キットをダウンロードする
先ほどのページから「Development」へ進み、
「WinPcap 3.1 Developer’s Pack」
をダウンロードして解凍。
手順3:Visual Studio .NET 2003 にライブラリの追加
手順2で解凍したフォルダの中のIncludeとLibフォルダの中身を
C:¥Program Files¥Microsoft Visual Studio .NET 2003¥Vc7
のInclude,Libにそれぞれコピー。
正直これはやらなくていいかも知れないですが、ここでコピーしておくと、
間違ってヘッダファイルなどを消してしまう心配がないかと思います。
(VS自体アンインストールしてしまうといみないですが・・・)
ここでコピーしておけば
#include
のように”<>“を使ってincludeできます。
手順3:サンプルのビルドと実行
では実際にビルドしてみましょう。
Visual Studio .NET 2003を起動して、
「新しいプロジェクト」
「Visual C++ プロジェクト」
「Win32」
と進み
「Win32コンソール プロジェクト」
プロジェクト名を入力してOK。
Win32アプリケーションウィザードが表示されるので
「アプリケーションの設定」
で「空のプロジェクト」にチェックを入れて完了。
出来たら、ソースファイルに「sample.c」を追加し、
下記のサンプルコードを入力してみましょう。
/*
#include “pcap.h”
↑手順2でヘッダファイルなどコピーしなかった場合
この場合はしっかりヘッダなど追加して下さい。
(こっちでは試してません。)
*/
#include
/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the device list */
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,”Error in pcap_findalldevs: %s¥n”, errbuf);
exit(1);
}
/* Print the list */
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”);
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* Open the device */
/* Open the adapter */
if ((adhandle= pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,”¥nUnable to open the adapter. %s is not supported by WinPcap¥n”, d->name);
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
printf(“¥nlistening on %s…¥n”, d->description);
/* At this point, we don’t need any more the device list. Free it */
pcap_freealldevs(alldevs);
/* start the capture */
pcap_loop(adhandle, 0, packet_handler, NULL);
pcap_close(adhandle);
return 0;
}
/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
/* convert the timestamp to readable format */
ltime=localtime(&header->ts.tv_sec);
strftime( timestr, sizeof timestr, “%H:%M:%S”, ltime);
printf(“%s,%.6d len:%d¥n”, timestr, header->ts.tv_usec, header->len);
}
入力し終わったらビルド!
おおおおおおおおおお。
うごかねーーーーーーー。
正直なやみました。
いろいろ調べたところ、wpcap.libをリンカに関連付けしないといけない模様。
というわけで、
1.メニューバーから「プロジェクト」
2.[プロジェクト名]のプロパティ
3.構成プロパティ
4.リンカ
5.入力
6.追加の依存ファイルにwpcap.libと入力。
7.OKを押して再度ビルド
これで動くと思います。
で、このプログラムは何をしてるかというと、
PCにインストールされているネットワークカードを検出し、
そのネットワークカードに流れてきたパケットを拾っているようです。
あ、そうそう。
言い忘れてました。
僕C/C++最高に苦手なんで。
一応コンソールでそれなりのものは作れますが、
やさしいCレベルなんで(^^;;
まぁこれから勉強していきたいと思います。
参考・資料にさせていただいたサイト
WinPcapを使ったプログラミング
ひげぽん OSとか作っちゃうかMona- – WinPcapを試す
そしてwinPcapの日本語ドキュメント
WinPcap Documentation
↑3.0用ですが、かなりありがたいです。
この解説資料を元にいろいろ作っていく感じになります。
参考にさせていただいたサイト様ありがとうございました。
それとスペシャルサンクス
くだすれVisual C++(超初心者用)スレのの550さん
僕と全く同じエラー出てました。
落ちてますのでキャッシュを貼らせていただきます。
こちら