6aa2bde28a8dcbe7b25f4be2a3331ab800bb5f1a
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 #include "wtap.h"
2 #include "ngsniffer.h"
3
4 static
5 void pcap_callback_wrapper(u_char *user, const struct pcap_pkthdr *phdr,
6                 const u_char *buf);
7
8 wtap_handler wtap_callback = NULL;
9
10 FILE* wtap_file(wtap *wth)
11 {
12         if (wth->file_type == WTAP_FILE_PCAP) {
13                 return pcap_file(wth->pcap);
14         }
15         else
16                 return wth->fh;
17 }
18
19 int wtap_file_type(wtap *wth)
20 {
21         return wth->file_type;
22 }
23
24 int wtap_encapsulation(wtap *wth)
25 {
26         return wth->encapsulation;
27 }
28
29
30 int wtap_snapshot_length(wtap *wth)
31 {
32         if (wth->file_type == WTAP_FILE_PCAP)
33                 return pcap_snapshot(wth->pcap);
34         else
35                 return 5000;
36 }
37
38 void wtap_close(wtap *wth)
39 {
40         if (wth->file_type == WTAP_FILE_PCAP)
41                 pcap_close(wth->pcap);
42         else
43                 fclose(wth->fh);
44 }
45
46 void wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user)
47 {
48         int i = 0;
49
50         if (wth->file_type == WTAP_FILE_PCAP) {
51                 wtap_callback = callback;
52                 pcap_loop(wth->pcap, count, pcap_callback_wrapper, user);
53         }
54         else {
55                 while (ngsniffer_read(wth)) {
56                         i++;
57                         callback(user, &wth->phdr, buffer_start_ptr(&wth->frame_buffer));
58                 }
59         }
60 }
61
62 static
63 void pcap_callback_wrapper(u_char *user, const struct pcap_pkthdr *phdr,
64                 const u_char *buf)
65 {
66 /*      struct wtap_pkthdr whdr;
67         memcpy(&whdr, phdr, sizeof(struct wtap_pkthdr));*/
68         wtap_callback(user, (struct wtap_pkthdr*) phdr, buf);
69 }