]> git.ipfire.org Git - thirdparty/hostap.git/blame - wlantest/writepcap.c
wlantest: Add BIP-GMAC-128/256 test vectors
[thirdparty/hostap.git] / wlantest / writepcap.c
CommitLineData
64f45d07
JM
1/*
2 * PCAP capture file writer
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
64f45d07
JM
7 */
8
9#include "utils/includes.h"
ef00c780
JM
10#include <pcap.h>
11#include <pcap-bpf.h>
64f45d07
JM
12
13#include "utils/common.h"
14#include "wlantest.h"
15
16
17int write_pcap_init(struct wlantest *wt, const char *fname)
18{
19 wt->write_pcap = pcap_open_dead(DLT_IEEE802_11_RADIO, 4000);
20 if (wt->write_pcap == NULL)
21 return -1;
22 wt->write_pcap_dumper = pcap_dump_open(wt->write_pcap, fname);
23 if (wt->write_pcap_dumper == NULL) {
24 pcap_close(wt->write_pcap);
25 wt->write_pcap = NULL;
26 return -1;
27 }
28
29 wpa_printf(MSG_DEBUG, "Writing PCAP dump to '%s'", fname);
30
31 return 0;
32}
33
34
35void write_pcap_deinit(struct wlantest *wt)
36{
37 if (wt->write_pcap_dumper) {
38 pcap_dump_close(wt->write_pcap_dumper);
39 wt->write_pcap_dumper = NULL;
40 }
41 if (wt->write_pcap) {
42 pcap_close(wt->write_pcap);
43 wt->write_pcap = NULL;
44 }
45}
46
47
48void write_pcap_captured(struct wlantest *wt, const u8 *buf, size_t len)
49{
50 struct pcap_pkthdr h;
51
52 if (!wt->write_pcap_dumper)
53 return;
54
55 os_memset(&h, 0, sizeof(h));
56 gettimeofday(&wt->write_pcap_time, NULL);
57 h.ts = wt->write_pcap_time;
58 h.caplen = len;
59 h.len = len;
60 pcap_dump(wt->write_pcap_dumper, &h, buf);
61}
62
63
64void write_pcap_decrypted(struct wlantest *wt, const u8 *buf1, size_t len1,
65 const u8 *buf2, size_t len2)
66{
67 struct pcap_pkthdr h;
68 u8 rtap[] = {
69 0x00 /* rev */,
70 0x00 /* pad */,
71 0x08, 0x00, /* header len */
72 0x00, 0x00, 0x00, 0x00 /* present flags */
73 };
74 u8 *buf;
75 size_t len;
76
77 if (!wt->write_pcap_dumper)
78 return;
79
80 os_memset(&h, 0, sizeof(h));
81 h.ts = wt->write_pcap_time;
82 len = sizeof(rtap) + len1 + len2;
83 buf = os_malloc(len);
84 if (buf == NULL)
85 return;
86 os_memcpy(buf, rtap, sizeof(rtap));
87 if (buf1) {
88 os_memcpy(buf + sizeof(rtap), buf1, len1);
89 buf[sizeof(rtap) + 1] &= ~0x40; /* Clear Protected flag */
90 }
91 if (buf2)
92 os_memcpy(buf + sizeof(rtap) + len1, buf2, len2);
93 h.caplen = len;
94 h.len = len;
95 pcap_dump(wt->write_pcap_dumper, &h, buf);
96 os_free(buf);
97}