wlantest: Add option for writing a PCAP dump file
[mech_eap.git] / wlantest / readpcap.c
1 /*
2  * PCAP capture file reader
3  * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16 #include <pcap/pcap.h>
17
18 #include "utils/common.h"
19 #include "wlantest.h"
20
21
22 int read_cap_file(struct wlantest *wt, const char *fname)
23 {
24         char errbuf[PCAP_ERRBUF_SIZE];
25         pcap_t *pcap;
26         unsigned int count = 0;
27         struct pcap_pkthdr *hdr;
28         const u_char *data;
29         int res;
30
31         pcap = pcap_open_offline(fname, errbuf);
32         if (pcap == NULL) {
33                 wpa_printf(MSG_ERROR, "Failed to read pcap file '%s': %s",
34                            fname, errbuf);
35                 return -1;
36         }
37
38         for (;;) {
39                 res = pcap_next_ex(pcap, &hdr, &data);
40                 if (res == -2)
41                         break; /* No more packets */
42                 if (res == -1) {
43                         wpa_printf(MSG_INFO, "pcap_next_ex failure: %s",
44                                    pcap_geterr(pcap));
45                         break;
46                 }
47                 if (res != 1) {
48                         wpa_printf(MSG_INFO, "Unexpected pcap_next_ex return "
49                                    "value %d", res);
50                         break;
51                 }
52
53                 /* Packet was read without problems */
54                 wpa_printf(MSG_EXCESSIVE, "pcap hdr: ts=%d.%06d "
55                            "len=%u/%u",
56                            (int) hdr->ts.tv_sec, (int) hdr->ts.tv_usec,
57                            hdr->caplen, hdr->len);
58                 if (wt->write_pcap_dumper) {
59                         wt->write_pcap_time = hdr->ts;
60                         pcap_dump(wt->write_pcap_dumper, hdr, data);
61                 }
62                 if (hdr->caplen < hdr->len) {
63                         wpa_printf(MSG_DEBUG, "pcap: Dropped incomplete frame "
64                                    "(%u/%u captured)",
65                                    hdr->caplen, hdr->len);
66                         continue;
67                 }
68                 count++;
69                 wlantest_process(wt, data, hdr->caplen);
70         }
71
72         pcap_close(pcap);
73
74         wpa_printf(MSG_DEBUG, "Read %s: %u packets", fname, count);
75
76         return 0;
77 }
78
79
80 int read_wired_cap_file(struct wlantest *wt, const char *fname)
81 {
82         char errbuf[PCAP_ERRBUF_SIZE];
83         pcap_t *pcap;
84         unsigned int count = 0;
85         struct pcap_pkthdr *hdr;
86         const u_char *data;
87         int res;
88
89         pcap = pcap_open_offline(fname, errbuf);
90         if (pcap == NULL) {
91                 wpa_printf(MSG_ERROR, "Failed to read pcap file '%s': %s",
92                            fname, errbuf);
93                 return -1;
94         }
95
96         for (;;) {
97                 res = pcap_next_ex(pcap, &hdr, &data);
98                 if (res == -2)
99                         break; /* No more packets */
100                 if (res == -1) {
101                         wpa_printf(MSG_INFO, "pcap_next_ex failure: %s",
102                                    pcap_geterr(pcap));
103                         break;
104                 }
105                 if (res != 1) {
106                         wpa_printf(MSG_INFO, "Unexpected pcap_next_ex return "
107                                    "value %d", res);
108                         break;
109                 }
110
111                 /* Packet was read without problems */
112                 wpa_printf(MSG_EXCESSIVE, "pcap hdr: ts=%d.%06d "
113                            "len=%u/%u",
114                            (int) hdr->ts.tv_sec, (int) hdr->ts.tv_usec,
115                            hdr->caplen, hdr->len);
116                 if (hdr->caplen < hdr->len) {
117                         wpa_printf(MSG_DEBUG, "pcap: Dropped incomplete frame "
118                                    "(%u/%u captured)",
119                                    hdr->caplen, hdr->len);
120                         continue;
121                 }
122                 count++;
123                 wlantest_process_wired(wt, data, hdr->caplen);
124         }
125
126         pcap_close(pcap);
127
128         wpa_printf(MSG_DEBUG, "Read %s: %u packets", fname, count);
129
130         return 0;
131 }