mystab_pcap.h
最終更新:2010/1/12
001: /*
002: * mystab_pcap.h
003: * libpcap を用いた TCP パケット解析用スタブ
004: */
005:
006: #ifndef _MYSTAB_PCAP_H_
007: #define _MYSTAB_PCAP_H_
008:
009: #include <stdio.h>
010: #include <stdlib.h>
011: #include <string.h>
012:
013: #ifndef lib_pcap_pcap_h
014: #include <pcap.h>
015: #endif
016:
017: #ifndef _MYSTAB_H_
018: #include "mystab.h"
019: #endif
020:
021: #ifndef APR_STRINGS_H
022: #include "apr_strings.h"
023: #endif
024:
025: #define ERR_BUF_SIZE 1024
026:
027: #if defined(WIN32) || defined(WINDOWS) || defined(MSVC)
028: #define snprintf sprintf_s
029:
030: /*
031: * ネットワークデバイスのリストの出力
032: * (Windows のみ)
033: * 返り値:
034: * 成功:1
035: * 失敗:0 (errbuf にエラーメッセージが保存される)
036: */
037:
038: int disp_devices(
039: apr_file_t *out /* 出力ファイルハンドラ */
040: , char *errbuf /* エラーメッセージの保存される領域 */
041: , int errbuf_size /* errbuf の領域のサイズ */
042: );
043: #endif /* defined(WIN32) || defined(WINDOWS) || defined(MSVC) */
044:
045: /* libpcap API で使用するパラメータ構造体 */
046:
047: typedef struct _my_pcap_parms_st {
048: /* 監視対象のネットワークデバイス */
049: const char *device;
050: /* キャプチャするパケットのサイズ */
051: int snaplen;
052: /* promiscuous modeのフラグ。ON:1 OFF:0 */
053: int pflag;
054: /* パケット読み出しのタイムアウト秒 */
055: int to_ms;
056: /* フィルタ文字列 */
057: char *filter;
058: /* フィルタ文字列の最適化フラグ。最適化あり:1 なし:0 */
059: int Oflag;
060: /* パケットを取得する回数。負の時にはエラーになるまで。 */
061: int cnt;
062: } my_pcap_params;
063:
064: typedef struct my_tcp_pkt_st {
065: /* IP パケットの先頭 */
066: apr_byte_t *ip_header_pos;
067: /* IP ヘッダのサイズ */
068: apr_byte_t ip_header_size;
069: /* 送信元IPアドレス XXX.XXX.XXX.XXX */
070: char ip_src_addr[16];
071: /* 送信先IPアドレス XXX.XXX.XXX.XXX */
072: char ip_dst_addr[16];
073: /* IP データグラムのサイズ(IPヘッダ含む) */
074: apr_uint16_t ip_datagram_size;
075:
076: /* TCP パケットの先頭 */
077: apr_byte_t *tcp_header_pos;
078: /* TCP ヘッダのサイズ */
079: apr_byte_t tcp_header_size;
080: /* 送信元ポート */
081: apr_uint16_t tcp_src_port;
082: /* 送信先ポート */
083: apr_uint16_t tcp_dst_port;
084: /* TCP シーケンス番号 */
085: apr_uint32_t tcp_seq_no;
086: /* TCP ACK 番号 */
087: apr_uint32_t tcp_ack_no;
088:
089: /* コードビットフラグ */
090: apr_byte_t tcp_urg_bit;
091: apr_byte_t tcp_ack_bit;
092: apr_byte_t tcp_psh_bit;
093: apr_byte_t tcp_rst_bit;
094: apr_byte_t tcp_syn_bit;
095: apr_byte_t tcp_fin_bit;
096:
097: /* TCP ペイロードのサイズ */
098: apr_uint16_t tcp_payload_size;
099:
100: /* TCP ペイロードの先頭 */
101: apr_byte_t *tcp_payload_pos;
102:
103: } my_tcp_pkt;
104:
105:
106: /*
107: * PCAP 用パラメータの設定と独自データを初期化する関数
108: * 返り値:
109: * 成功:1
110: * 失敗:0
111: * メモ:
112: * コマンドライン引数を解析して libpcap API 用パラメータを設定する
113: * 処理を実装する。必要に応じて、独自データの初期化も行う。
114: * 独自データの解放などの終期化処理は my_pcap_finally の中で実装する。
115: */
116:
117: int my_pcap_init (
118: int ac /* コマンドライン引数の個数 */
119: , char **av /* コマンドライン引数の配列 */
120: , my_pcap_params *params /* libpcap API 用パラメータ構造体 */
121: , apr_file_t *astderr /* エラー出力ファイルポインタ */
122: , apr_pool_t *pool /* メモリプール */
123: );
124:
125: /*
126: * TCP パケットを処理する関数
127: * ・TCP パケットをキャプチャする度に呼ばれる関数
128: */
129:
130: void my_pcap_main(
131: apr_pool_t *pool /* メモリプール */
132: , apr_file_t *out /* 出力ファイルポインタ */
133: , my_tcp_pkt *d /* TCP パケット */
134: );
135:
136: /*
137: * 終期化関数
138: * メモ:
139: * my_pcap_init で作成した独自データの解放などの処理を実装する。
140: */
141:
142: void my_pcap_finally (
143: apr_file_t *out
144: );
145:
146: #endif
![]() | KAKU PROJECT (2009) |