ネットワークをモニタするこの機能を利用するには、プログラマはアダプタを開いて統計モードにする必要があります。 pcap_setmode() を使用することによりそうすることが出来ます。特に、MODE_STATがこの関数のモード引数として使用される必要があります。
統計モードでTCPトラフィックをロードするアプリケーションを作ることは、数行のコードが関連しています。続くサンプルが、どのように出来るか を示しています。
/* * Copyright (c) 1999 - 2002 * Politecnico di Torino. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that: (1) source code distributions * retain the above copyright notice and this paragraph in its entirety, (2) * distributions including binary code include the above copyright notice and * this paragraph in its entirety in the documentation or other materials * provided with the distribution, and (3) all advertising materials mentioning * features or use of this software display the following acknowledgement: * ``This product includes software developed by the Politecnico * di Torino, and its contributors.'' Neither the name of * the University nor the names of its contributors may be used to endorse * or promote products derived from this software without specific prior * written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ #include <stdlib.h> #include <stdio.h> #include <pcap.h> void usage();
void dispatcher_handler(u_char *,
const struct pcap_pkthdr *, const u_char *);
void main(int argc, char **argv) {
pcap_t *fp;
char error[PCAP_ERRBUF_SIZE];
struct timeval st_ts;
u_int netmask;
struct bpf_program fcode;
/* Check the validity of the command line */ if (argc != 2)
{
usage();
return;
}
/* Open the output adapter */ if((fp = pcap_open_live(argv[1], 100, 1, 1000, error) ) == NULL)
{
fprintf(stderr,"\nError opening adapter: %s\n", error);
return;
}
/* Don't care about netmask, it won't be used for this filter */ netmask=0xffffff; //compile the filter if(pcap_compile(fp, &fcode, "tcp", 1, netmask) <0 ){
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* Free the device list */ return;
}
//set the filter if(pcap_setfilter(fp, &fcode)<0){
fprintf(stderr,"\nError setting the filter.\n");
/* Free the device list */ return;
}
/* Put the interface in statstics mode */ pcap_setmode(fp, MODE_STAT);
printf("TCP traffic summary:\n");
/* Start the main loop */ pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);
return;
}
void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct timeval *old_ts = (struct timeval *)state;
u_int delay;
LARGE_INTEGER Bps,Pps;
struct tm *ltime;
char timestr[16];
/* Calculate the delay in microseconds from the last sample. */ /* This value is obtained from the timestamp that the associated with the sample. */ delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec;
/* Get the number of Bits per second */ Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay)); /* ^ ^ | | | | | | converts bytes in bits -- | | delay is expressed in microseconds -- */ /* Get the number of Packets per second */ Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay)); /* Convert the timestamp to readable format */ ltime=localtime(&header->ts.tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
/* Print timestamp*/ printf("%s ", timestr);
/* Print the samples */ printf("BPS=%I64u ", Bps.QuadPart);
printf("PPS=%I64u\n", Pps.QuadPart);
//store current timestamp old_ts->tv_sec=header->ts.tv_sec;
old_ts->tv_usec=header->ts.tv_usec;
}
void usage()
{
printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n");
printf("\nUsage:\n");
printf("\t tcptop adapter\n");
printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n");
exit(0);
}
統計モードを有効にする前に、ユーザーは、モニタされるネットワークトラフィックのサブセット規定するフィルタを設定することが可能です。詳しく は「フィルタリングの表現シンタックス」の項を参照して ください。もしフィルタがセットされていないのであれば、全てのトラフィックがモニタされます。
その後、インターフェイス記述子が統計モードで作動し始めます。四番目のpcap_live()の パ ラメータ(to_ms)に注目してください。これは、統計サンプル間の間隔を規定します。コールバックはミリ秒毎に、ドライバによって算出されたサンプル を受け取ります。これらのサンプルは次の図にもある通り、コールバックの二つ目、三つ目のパラメータ内でカプセルかされます。
サンプル中では、1000ミリ秒のタイムアウトでアダプタがオープンされます。dispatcher_handler()
が一秒につき一回呼び出される、ということです。この時点で、TCPパケットだけをキープしているフィルタがコンパイルされてセットされます。そして、pcap_setmode() と pcap_loop()
が呼び出されます。timeval 構造体のポインタが pcap_loop()
にユーザーパラメータとして渡されることに注目視ください。この構造体は、二つのサンプル間の間隔を算出するためのタイムスタンプを蓄えるために使用され
ます。dispatcher_handler()
はこの間隔を、一秒後とのパケットとビットを獲得して、画面上にそれらの値を表示します。
最後にこのサンプルが、従来のパケットキャプチャ法よりもとても効率的な方法であること、ユーザーレベルで統計値を算出できることに注目してくだ さい。統計モードは最小限のデータコピーとコンテキストスイッチしか要求しないため、CPUが最適化されます。更に、ごく少量のメモリが要求されるだけな のです。
documentation. Copyright (c)2002-2003 Politecnico di Torino.
2005 translated Japanese by Telebusiness,Inc.
All rights reserved.