/* * ktcpdump.c * libpcap を使った TCP パケット解析サンプルコード * Ctrl-C で終了。 */ #define PROG_NAME "ktcpdump" #include "mystab_pcap.h" #include "apr_getopt.h" static const apr_getopt_option_t opt_option[] = { /* long-option, short-option, has-arg flag, description */ { "port_number", 'p', TRUE, "[MUST] port_number" }, { "interface", 'i', TRUE, "[MUST] network device to use" }, { "help", 'h', FALSE, "[OPTION] show help" }, /* -h or --help */ #if defined(WIN32) || defined(WINDOWS) || defined(MSVC) { "disp_interfaces", 'D', FALSE, "[OPTION] displays avalable interfaces (Windows Only)" }, #endif { NULL, 0, 0, NULL }, /* SENTINEL */ }; void print_usage (apr_file_t *out) { apr_getopt_option_t *ptr = (apr_getopt_option_t *)opt_option; apr_file_printf(out, "Usage: %s ", PROG_NAME); while (ptr && ptr->name) { if (ptr->has_arg) { apr_file_printf(out, "[ -%c | --%s ] ", ptr->optch, ptr->name); } else { apr_file_printf(out, "[ -%c | --%s ] ", ptr->optch, ptr->name); } ptr++; } apr_file_printf(out, "\n"); ptr = (apr_getopt_option_t *)opt_option; while (ptr && ptr->name) { apr_file_printf(out, "\t-%c, --%s\t: %s\n", ptr->optch, ptr->name, ptr->description); ptr++; } } int my_pcap_init ( int ac , char **av , my_pcap_params *params , apr_file_t *astderr , apr_pool_t *pool ) { apr_status_t rv = APR_SUCCESS; apr_getopt_t *opt = NULL; int opt_ch = 0; const char *opt_arg = NULL; apr_uint16_t port_number = 0; const char * interface_str = NULL; if (ac < 2) { print_usage(astderr); return 0; } rv = apr_getopt_init(&opt, pool, ac, (const char * const *)av); if (rv != APR_SUCCESS) { apr_file_printf(astderr, "ERROR: apr_getopt_init\n"); return 0; } while ((rv = apr_getopt_long(opt, opt_option, &opt_ch, &opt_arg)) == APR_SUCCESS) { switch (opt_ch) { case 'p': port_number = atoi(opt_arg); break; case 'i': interface_str = opt_arg; break; #if defined(WIN32) || defined(WINDOWS) || defined(MSVC) case 'D': { char errbuf[80]; if (! disp_devices(astderr, errbuf, sizeof(errbuf))) { apr_file_printf(astderr, "ERROR: disp_devices: %s\n", errbuf); } } return 0; break; #endif case 'h': print_usage(astderr); return 0; } } if (rv != APR_EOF || port_number<=0 || !interface_str) { print_usage(astderr); return 0; } params->device = interface_str; params->filter = apr_psprintf(pool, "tcp port %d", port_number); return 1; } void my_pcap_main( apr_pool_t *pool , apr_file_t *out , my_tcp_pkt *d ) { if (!d || !pool || !out) { return; } apr_file_printf(out, "[%s:%d-%s:%d] seq_no=%d ack_no=%d size=%d\n", d->ip_src_addr, d->tcp_src_port, d->ip_dst_addr, d->tcp_dst_port, d->tcp_seq_no, d->tcp_ack_no, d->tcp_payload_size); apr_file_printf(out, "\turg=%d ack=%d psh=%d rst=%d syn=%d fin=%d", d->tcp_urg_bit, d->tcp_ack_bit, d->tcp_psh_bit, d->tcp_rst_bit, d->tcp_syn_bit, d->tcp_fin_bit); if (d->tcp_payload_size > 0) { int i=0; for (i=0; itcp_payload_size; i++) { if (i%32 == 0) { apr_file_printf(out, "\n"); } apr_file_printf(out, "%02X", d->tcp_payload_pos[i]); } apr_file_printf(out, "\n"); } apr_file_printf(out, "\n"); } void my_pcap_finally ( apr_file_t *out ) { apr_file_printf(out, "done.\n"); }