wlantest: Replace pcap header directory
[mech_eap.git] / wlantest / writepcap.c
1 /*
2  * PCAP capture file writer
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.h>
17 #include <pcap-bpf.h>
18
19 #include "utils/common.h"
20 #include "wlantest.h"
21
22
23 int write_pcap_init(struct wlantest *wt, const char *fname)
24 {
25         wt->write_pcap = pcap_open_dead(DLT_IEEE802_11_RADIO, 4000);
26         if (wt->write_pcap == NULL)
27                 return -1;
28         wt->write_pcap_dumper = pcap_dump_open(wt->write_pcap, fname);
29         if (wt->write_pcap_dumper == NULL) {
30                 pcap_close(wt->write_pcap);
31                 wt->write_pcap = NULL;
32                 return -1;
33         }
34
35         wpa_printf(MSG_DEBUG, "Writing PCAP dump to '%s'", fname);
36
37         return 0;
38 }
39
40
41 void write_pcap_deinit(struct wlantest *wt)
42 {
43         if (wt->write_pcap_dumper) {
44                 pcap_dump_close(wt->write_pcap_dumper);
45                 wt->write_pcap_dumper = NULL;
46         }
47         if (wt->write_pcap) {
48                 pcap_close(wt->write_pcap);
49                 wt->write_pcap = NULL;
50         }
51 }
52
53
54 void write_pcap_captured(struct wlantest *wt, const u8 *buf, size_t len)
55 {
56         struct pcap_pkthdr h;
57
58         if (!wt->write_pcap_dumper)
59                 return;
60
61         os_memset(&h, 0, sizeof(h));
62         gettimeofday(&wt->write_pcap_time, NULL);
63         h.ts = wt->write_pcap_time;
64         h.caplen = len;
65         h.len = len;
66         pcap_dump(wt->write_pcap_dumper, &h, buf);
67 }
68
69
70 void write_pcap_decrypted(struct wlantest *wt, const u8 *buf1, size_t len1,
71                           const u8 *buf2, size_t len2)
72 {
73         struct pcap_pkthdr h;
74         u8 rtap[] = {
75                 0x00 /* rev */,
76                 0x00 /* pad */,
77                 0x08, 0x00, /* header len */
78                 0x00, 0x00, 0x00, 0x00 /* present flags */
79         };
80         u8 *buf;
81         size_t len;
82
83         if (!wt->write_pcap_dumper)
84                 return;
85
86         os_memset(&h, 0, sizeof(h));
87         h.ts = wt->write_pcap_time;
88         len = sizeof(rtap) + len1 + len2;
89         buf = os_malloc(len);
90         if (buf == NULL)
91                 return;
92         os_memcpy(buf, rtap, sizeof(rtap));
93         if (buf1) {
94                 os_memcpy(buf + sizeof(rtap), buf1, len1);
95                 buf[sizeof(rtap) + 1] &= ~0x40; /* Clear Protected flag */
96         }
97         if (buf2)
98                 os_memcpy(buf + sizeof(rtap) + len1, buf2, len2);
99         h.caplen = len;
100         h.len = len;
101         pcap_dump(wt->write_pcap_dumper, &h, buf);
102         os_free(buf);
103 }