/* * mystab_pcap.h * libpcap を用いた TCP パケット解析用スタブ */ #ifndef _MYSTAB_PCAP_H_ #define _MYSTAB_PCAP_H_ #include #include #include #ifndef lib_pcap_pcap_h #include #endif #ifndef _MYSTAB_H_ #include "mystab.h" #endif #ifndef APR_STRINGS_H #include "apr_strings.h" #endif #define ERR_BUF_SIZE 1024 #if defined(WIN32) || defined(WINDOWS) || defined(MSVC) #define snprintf sprintf_s /* * ネットワークデバイスのリストの出力 * (Windows のみ) * 返り値: * 成功:1 * 失敗:0 (errbuf にエラーメッセージが保存される) */ int disp_devices( apr_file_t *out /* 出力ファイルハンドラ */ , char *errbuf /* エラーメッセージの保存される領域 */ , int errbuf_size /* errbuf の領域のサイズ */ ); #endif /* defined(WIN32) || defined(WINDOWS) || defined(MSVC) */ /* libpcap API で使用するパラメータ構造体 */ typedef struct _my_pcap_parms_st { /* 監視対象のネットワークデバイス */ const char *device; /* キャプチャするパケットのサイズ */ int snaplen; /* promiscuous modeのフラグ。ON:1 OFF:0 */ int pflag; /* パケット読み出しのタイムアウト秒 */ int to_ms; /* フィルタ文字列 */ char *filter; /* フィルタ文字列の最適化フラグ。最適化あり:1 なし:0 */ int Oflag; /* パケットを取得する回数。負の時にはエラーになるまで。 */ int cnt; } my_pcap_params; typedef struct my_tcp_pkt_st { /* IP パケットの先頭 */ apr_byte_t *ip_header_pos; /* IP ヘッダのサイズ */ apr_byte_t ip_header_size; /* 送信元IPアドレス XXX.XXX.XXX.XXX */ char ip_src_addr[16]; /* 送信先IPアドレス XXX.XXX.XXX.XXX */ char ip_dst_addr[16]; /* IP データグラムのサイズ(IPヘッダ含む) */ apr_uint16_t ip_datagram_size; /* TCP パケットの先頭 */ apr_byte_t *tcp_header_pos; /* TCP ヘッダのサイズ */ apr_byte_t tcp_header_size; /* 送信元ポート */ apr_uint16_t tcp_src_port; /* 送信先ポート */ apr_uint16_t tcp_dst_port; /* TCP シーケンス番号 */ apr_uint32_t tcp_seq_no; /* TCP ACK 番号 */ apr_uint32_t tcp_ack_no; /* コードビットフラグ */ apr_byte_t tcp_urg_bit; apr_byte_t tcp_ack_bit; apr_byte_t tcp_psh_bit; apr_byte_t tcp_rst_bit; apr_byte_t tcp_syn_bit; apr_byte_t tcp_fin_bit; /* TCP ペイロードのサイズ */ apr_uint16_t tcp_payload_size; /* TCP ペイロードの先頭 */ apr_byte_t *tcp_payload_pos; } my_tcp_pkt; /* * PCAP 用パラメータの設定と独自データを初期化する関数 * 返り値: * 成功:1 * 失敗:0 * メモ: * コマンドライン引数を解析して libpcap API 用パラメータを設定する * 処理を実装する。必要に応じて、独自データの初期化も行う。 * 独自データの解放などの終期化処理は my_pcap_finally の中で実装する。 */ int my_pcap_init ( int ac /* コマンドライン引数の個数 */ , char **av /* コマンドライン引数の配列 */ , my_pcap_params *params /* libpcap API 用パラメータ構造体 */ , apr_file_t *astderr /* エラー出力ファイルポインタ */ , apr_pool_t *pool /* メモリプール */ ); /* * TCP パケットを処理する関数 * ・TCP パケットをキャプチャする度に呼ばれる関数 */ void my_pcap_main( apr_pool_t *pool /* メモリプール */ , apr_file_t *out /* 出力ファイルポインタ */ , my_tcp_pkt *d /* TCP パケット */ ); /* * 終期化関数 * メモ: * my_pcap_init で作成した独自データの解放などの処理を実装する。 */ void my_pcap_finally ( apr_file_t *out ); #endif