2016-09-25 17 views
1

異なるタイプのネットワークパケットを含むpcapファイルを解析しようとしています(いくつかはVLANとタグ付けされていて、いくつかは#includeを使用していません)。 ここに私のコードは、これまでのところです:libpcapとCを使用してVLANタグを取得するにはどうすればよいですか?

pcap_t *pcap; 
const unsigned char *packet; 
char errbuf[PCAP_ERRBUF_SIZE]; 
struct pcap_pkthdr header; 
pcap = pcap_open_offline(argv[0], errbuf); 
if (pcap == NULL) 
    { 
    fprintf(stderr, "error reading pcap file: %s\n", errbuf); 
    exit(1); 
} 
while ((packet = pcap_next(pcap, &header)) != NULL) 
{ 
    struct ip_header *ip; 
    unsigned int IP_header_length; 
    packet += sizeof(struct ether_header); 
    capture_len -= sizeof(struct ether_header); 
    ip = (struct ip_header*) packet; 
    IP_header_length = ip->vhl * 4; /* ip_hl is in 4-byte words */ 
    char *sinfo = strdup(inet_ntoa(ip->src)); 
    char *dinfo = strdup(inet_ntoa(ip->dst)); 
    printf ("%s<-__->%s\n", sinfo ,dinfo); 
    free (sinfo); 
    free (dinfo); 
} 

VLANをチェックして、私は非VLANからVLANパケットを区別する必要がありますcorrectly.Howそれらの上にジャンプするコードのどこかに存在する必要がありますか?

+0

が必要な場合は、あなただけでなく、フォーマットされた質問をありがとうあなたがまたはあなたは私がフェッチんか」を求めている「最高のこのコードを整理する方法を」求めているならば、私はしかし、わかりませんよ'libpcap'を使ってVLANタグを作成し、そうであれば、802.1qとISLの両方をターゲットにしていますか? –

+0

ありがとうございました。私は質問を変更しました。はい、私が本当に見たいのはちょうどIPヘッダーです。 – mazkopolo

答えて

1

(あなたは「ライブ」の環境でこれをテストしている場合、それはルータが非トランキングラインに転送する前に、802.1Qタグを削除することができますことを覚えておくことが重要です。)

あなたは、特定のプラットフォーム&のプロトコルを使用している場合

htonl(((uint32_t)(ETH_P_8021Q) << 16U) 
    | ((uint32_t)customer_tci & 0xFFFFU)) T 

しかし、libpcapcompiling a BPF filtersするための関数の形でポータブル&きれいなパケットフィルタを提供し、適用されます念頭に置いて、これを行うには最速の方法は、常に「手動」のフレームをチェックしになりますこれらのパケットをストリームに送信する(ただし、オンザフライとオフラインのフィルタリングには異なる機能セットがあることに注意してください)

このように、pcap_offline_filterを使用して、コンパイル済みBPFフィルタPCAPファイルへの指示。フィルタ式vlanをここで使用しましたが、vlan or ipのようなものが必要な場合があります。あなたは、より複雑な何か、you can consult the documentation

... 

pcap_t *pcap; 
char errbuf[PCAP_ERRBUF_SIZE]; 
const unsigned char *packet; 
struct pcap_pkthdr header; 
struct bpf_program fp; // Our filter expression 
pcap = pcap_open_offline(argv[0], errbuf); 
if (pcap == NULL) { 
    fprintf(stderr, "error reading pcap file: %s\n", errbuf); 
    exit(1); 
} 

// Compile a basic filter expression, you can exam 
if (pcap_compile(pcap, &fp, "vlan", 0, net) == -1) { 
    fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle)); 
    return 2; 
} 

while ((packet = pcap_next(pcap, &header) != NULL) 
     && pcap_offline_filter(&fp, header, packet)) { 
    struct ip_header *ip; 
    unsigned int IP_header_length; 
    packet += sizeof(struct ether_header); 
    capture_len -= sizeof(struct ether_header); 
    ip = (struct ip_header*) packet; 
    IP_header_length = ip->vhl * 4; /* ip_hl is in 4-byte words */ 
    char *sinfo = strdup(inet_ntoa(ip->src)); 
    char *dinfo = strdup(inet_ntoa(ip->dst)); 
    printf ("%s<-__->%s\n", sinfo ,dinfo); 
    free (sinfo); 
    free (dinfo); 
} 

... 
関連する問題