1

複数のデバイスでスニッフィングできるスニファを作成しようとしています。私のコードでは、プログラムはユーザーがスニッフィングしたいデバイスのリストを受け取ります。デバイスのリストを取得し、それを配列に格納してループスルーし、以下の関数のように pcap_t ハンドルを作成する関数に渡します。

void *startPcapProcess(char * dev){
    char errbuf[PCAP_ERRBUF_SIZE];     /* error buffer */
    pcap_t *handle;                    /* packet capture handle  */

/* filter expression [3] */
char filter_exp[] = "(dst port 53) and (udp[0xa] & 0x78 = 0x28)"; 

struct bpf_program fp;      /* compiled filter program (expression) */
bpf_u_int32 mask;           /* subnet mask */
bpf_u_int32 net;            /* ip */

printf("%s","startPacketProcess called\n");
printf("Device sent to startPacketProcess: %s\n", dev);
/* get network number and mask associated with capture device */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
    fprintf(stderr, "Couldn't get netmask for device %s: %s\n",
            dev, errbuf);
    net = 0;
    mask = 0;
}

/* open capture device */
handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
if (handle == NULL) {
    fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
    exit(EXIT_FAILURE);
}

/* make sure we're capturing on an Ethernet device [2] */
if (pcap_datalink(handle) != DLT_EN10MB) {
    fprintf(stderr, "%s is not an Ethernet\n", dev);
    exit(EXIT_FAILURE);
}

/* compile the filter expression */
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
    fprintf(stderr, "Couldn't parse filter %s: %s\n",
        filter_exp, pcap_geterr(handle));
    exit(EXIT_FAILURE);
}

/* apply the compiled filter */
if (pcap_setfilter(handle, &fp) == -1) {
    fprintf(stderr, "Couldn't install filter %s: %s\n",
        filter_exp, pcap_geterr(handle));
    exit(EXIT_FAILURE);
}

pcap_freecode(&fp);

/* now we can set our callback function */
pcap_loop(handle, -1, process_packet, NULL);
printf("%s","End startPacketProcess call\n");

}

ただし、for ループ内でこの関数を呼び出すと、pcap_loop コールバック関数でスタックしているように見えるため、1 つのデバイスでしかキャプチャできません。この結果、マルチスレッドを実行しようとしましたが、すべてのデバイスを開いてキャプチャするために使用する for ループがループを通過しましたが、pcap_loop コールバック関数は実行されていないようです。次のコードは、マルチスレッドの使用方法を示しています。

for (i = 0; i < numDevice; i++){
        printf("Device returned by getDevices call: %s\n", deviceList[i]);
        printf("%s","Entering for loop\n");
        pthread_create(&tid, thAttr, startPacketProcess,(void*)deviceList[i]);
    }

誰かが私が間違っていることを知っていますか?この問題を解決する方法について提案してもらえますか?

ありがとう、リン

4

2 に答える 2

1

process_packet問題かもしれません。スレッド コンテキストでパケットを取得してみてください。

  struct pcap_pkthdr *pkt_header;   
  u_char *pkt_data;         

  while ((retval = pcap_next_ex(mpPcap, &pkt_header, (const u_char **) &pkt_data)) >= 0)   {
    //Do Whatever
  } 
于 2011-08-23T02:19:52.277 に答える
0

またはを使用してみましたpcap_findalldevs()pcap_findalldevs_ex()

于 2011-02-03T11:59:41.583 に答える