1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id: test-pcap-to-erf.c,v 1.3 2006/02/27 03:41:12 perry Exp $ |
---|
28 | * |
---|
29 | */ |
---|
30 | #ifndef WIN32 |
---|
31 | # include <sys/time.h> |
---|
32 | # include <netinet/in.h> |
---|
33 | # include <netinet/in_systm.h> |
---|
34 | # include <netinet/tcp.h> |
---|
35 | # include <netinet/ip.h> |
---|
36 | # include <netinet/ip_icmp.h> |
---|
37 | # include <arpa/inet.h> |
---|
38 | # include <sys/socket.h> |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <stdio.h> |
---|
42 | #include <stdlib.h> |
---|
43 | #include <assert.h> |
---|
44 | #include <string.h> |
---|
45 | #include <sys/types.h> |
---|
46 | #include <time.h> |
---|
47 | #include <string.h> |
---|
48 | |
---|
49 | #include "libtrace.h" |
---|
50 | |
---|
51 | void iferr(libtrace_t *trace) |
---|
52 | { |
---|
53 | libtrace_err_t err = trace_get_err(trace); |
---|
54 | if (err.err_num==0) |
---|
55 | return; |
---|
56 | printf("Error: %s\n",err.problem); |
---|
57 | exit(1); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | int main(int argc, char *argv[]) { |
---|
62 | libtrace_t *trace; |
---|
63 | libtrace_packet_t *packet; |
---|
64 | int result; |
---|
65 | uint64_t tsft; |
---|
66 | uint16_t freq, tmp16; |
---|
67 | uint32_t fcs; |
---|
68 | uint8_t flags, rate, sdbm, ndbm, sdb, antenna, tmp8; |
---|
69 | |
---|
70 | uint32_t total_fcs, expected_fcs = 3012874435; |
---|
71 | uint16_t total_freq, expected_freq = 24170; |
---|
72 | |
---|
73 | void *l; |
---|
74 | libtrace_linktype_t lt; |
---|
75 | trace = trace_create("pcapfile:traces/10_packets_radiotap.pcap"); |
---|
76 | iferr(trace); |
---|
77 | |
---|
78 | trace_start(trace); |
---|
79 | iferr(trace); |
---|
80 | |
---|
81 | packet=trace_create_packet(); |
---|
82 | |
---|
83 | trace_read_packet(trace, packet); |
---|
84 | |
---|
85 | l = trace_get_link(packet); |
---|
86 | lt = trace_get_link_type(packet); |
---|
87 | |
---|
88 | /* Check that the right linktype is being reported for this trace */ |
---|
89 | assert(lt == TRACE_TYPE_80211_RADIO); |
---|
90 | |
---|
91 | /* Check that fields that exist in this trace are reported as |
---|
92 | * existing */ |
---|
93 | assert(trace_get_wireless_tsft(l,lt,&tsft)); |
---|
94 | assert(trace_get_wireless_rate(l,lt,&rate)); |
---|
95 | assert(trace_get_wireless_freq(l,lt,&freq)); |
---|
96 | assert(trace_get_wireless_signal_strength_dbm(l,lt,(int8_t *)&sdbm)); |
---|
97 | assert(trace_get_wireless_noise_strength_dbm(l,lt,(int8_t *)&ndbm)); |
---|
98 | assert(trace_get_wireless_signal_strength_db(l,lt,&sdb)); |
---|
99 | assert(trace_get_wireless_antenna(l,lt,&antenna)); |
---|
100 | assert(trace_get_wireless_fcs(l,lt,&fcs)); |
---|
101 | |
---|
102 | /* Check that the fields that do not exist in this trace are |
---|
103 | * reported as not existing */ |
---|
104 | assert(!trace_get_wireless_noise_strength_db(l,lt,&tmp8)); |
---|
105 | assert(!trace_get_wireless_tx_attenuation(l,lt,&tmp16)); |
---|
106 | assert(!trace_get_wireless_tx_attenuation_db(l,lt,&tmp16)); |
---|
107 | assert(!trace_get_wireless_tx_power_dbm(l,lt,(int8_t *)&tmp8)); |
---|
108 | |
---|
109 | /* Check that the functions are returning the right values for |
---|
110 | * this trace |
---|
111 | * TODO: Check all fields :) |
---|
112 | */ |
---|
113 | total_fcs = fcs; |
---|
114 | total_freq = freq; |
---|
115 | |
---|
116 | while((result = trace_read_packet(trace, packet)) > 0) { |
---|
117 | if(trace_get_wireless_fcs(l,lt,&fcs)) |
---|
118 | total_fcs += fcs; |
---|
119 | if(trace_get_wireless_freq(l,lt,&freq)) |
---|
120 | total_freq += freq; |
---|
121 | } |
---|
122 | |
---|
123 | assert(total_fcs == expected_fcs); |
---|
124 | assert(total_freq == expected_freq); |
---|
125 | |
---|
126 | trace_destroy_packet(packet); |
---|
127 | trace_destroy(trace); |
---|
128 | |
---|
129 | /* Now check that we don't process non-radiotap traces */ |
---|
130 | |
---|
131 | trace = trace_create("pcapfile:traces/100_packets.pcap"); |
---|
132 | iferr(trace); |
---|
133 | trace_start(trace); |
---|
134 | iferr(trace); |
---|
135 | packet = trace_create_packet(); |
---|
136 | trace_read_packet(trace,packet); |
---|
137 | l = trace_get_link(packet); |
---|
138 | lt = trace_get_link_type(packet); |
---|
139 | assert(lt != TRACE_TYPE_80211_RADIO); |
---|
140 | |
---|
141 | assert(!trace_get_wireless_tsft(l,lt,&tsft)); |
---|
142 | assert(!trace_get_wireless_rate(l,lt,&rate)); |
---|
143 | assert(!trace_get_wireless_freq(l,lt,&freq)); |
---|
144 | assert(!trace_get_wireless_signal_strength_dbm(l,lt,(int8_t *)&sdbm)); |
---|
145 | assert(!trace_get_wireless_noise_strength_dbm(l,lt,(int8_t *)&ndbm)); |
---|
146 | assert(!trace_get_wireless_signal_strength_db(l,lt,&sdb)); |
---|
147 | assert(!trace_get_wireless_antenna(l,lt,&antenna)); |
---|
148 | assert(!trace_get_wireless_fcs(l,lt,&fcs)); |
---|
149 | assert(!trace_get_wireless_noise_strength_db(l,lt,&tmp8)); |
---|
150 | assert(!trace_get_wireless_tx_attenuation(l,lt,&tmp16)); |
---|
151 | assert(!trace_get_wireless_tx_attenuation_db(l,lt,&tmp16)); |
---|
152 | assert(!trace_get_wireless_tx_power_dbm(l,lt,(int8_t *)&tmp8)); |
---|
153 | |
---|
154 | |
---|
155 | trace_destroy_packet(packet); |
---|
156 | trace_destroy(trace); |
---|
157 | printf("%s: success\n", argv[0]); |
---|
158 | return 0; |
---|
159 | } |
---|