forked from gawen947/wsn-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsn-sniffer-cli.c
305 lines (260 loc) · 7.94 KB
/
wsn-sniffer-cli.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* File: wsn-sniffer-cli.c
Copyright (C) 2013 David Hauweele <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include <err.h>
#include "version.h"
#include "pcap-write.h"
#include "dump.h"
#include "help.h"
#include "uart.h"
#include "xatoi.h"
#include "input.h"
#include "protocol.h"
#include "protocol-mqueue.h"
#include "signal-utils.h"
#include "mac-decode.h"
#include "mac-display.h"
#include "802154-parse.h"
#define TARGET "Sniffer-CLI"
static bool payload;
static unsigned int mac_info;
/* static unsigned int payload_info; */
static prot_mqueue_t mqueue;
static int fd;
static void parse_frame_message(const unsigned char *data, size_t size)
{
struct mac_frame frame;
/* We except a raw frame so we don't need to renormalize anything. */
if(mac_decode(&frame, data, true, size) < 0) {
#ifndef NDEBUG
hex_dump(data, size);
#endif /* NDEBUG */
warnx("cannot decode frame");
/* We do not show invalid frame as most of it
is probably uninitialized garbage. */
free_mac_frame(&frame);
return;
}
/* Display the frame live. */
mac_display(&frame, mac_info);
/* For now we do not try decode payload.
Instead we just dump the packet. */
if(payload && frame.payload) {
printf("Payload:\n");
hex_dump(frame.payload, frame.size);
}
putchar('\n');
/* Append the frame to the PCAP file. */
pcap_append_frame(data, size);
/* FIXME: This particular free call may be spared if we provided a way for
mac_decode to avoid copying the payload. */
free_mac_frame(&frame);
}
static bool message_cb(const unsigned char *data,
enum prot_mtype type,
size_t size)
{
if(size == 0)
errx(EXIT_FAILURE, "empty message");
switch(type) {
case(PROT_MTYPE_FRAME):
parse_frame_message(data, size);
break;
case(PROT_MTYPE_CONTROL):
/* We do not accept any control message for the sniffer.
Except the common ones. */
if(!prot_preparse_control(data, size))
errx(EXIT_FAILURE, "unmanaged control message %s",
prot_ctype_string(data[0]));
break;
default:
warnx("invalid event ignored");
#ifndef NDEBUG
hex_dump(data, size);
#endif /* NDEBUG */
exit(EXIT_FAILURE);
}
/* This function is always happy so it always returns true. */
return true;
}
static void cleanup(void)
{
/* We have to close the file descriptor too. */
close(fd);
/* Ensure that the PCAP file is closed properly to flush buffers. */
close_writing_pcap();
/* Destroy the message queue. */
prot_mqueue_destroy(mqueue);
}
static void sig_cleanup(int signum)
{
exit(EXIT_SUCCESS);
}
static void sig_flush(int signum)
{
pcap_write_flush();
}
int main(int argc, char *argv[])
{
const char *name;
const char *tty = NULL;
const char *pcap = NULL;
unsigned short channel;
speed_t speed = B0;
int timeout = 0;
int err;
int exit_status = EXIT_FAILURE;
name = (const char *)strrchr(argv[0], '/');
name = name ? (name + 1) : argv[0];
/* The message queue must be initialized before parsing the arguments. */
mqueue = prot_mqueue_creat();
enum opt {
OPT_COMMIT = 0x100
};
struct opt_help helps[] = {
{ 'h', "help", "Show this help message" },
{ 'V', "version", "Print version information" },
#ifdef COMMIT
{ 0, "commit", "Display commit information" },
#endif /* COMMIT */
{ 'T', "timeout", "Specify the timeout" },
{ 'C', "channel", "Configure the channel" },
{ 'b', "baud", "Specify the baud rate" },
{ 'p', "pcap", "Save packets in the specified PCAP file" },
{ 'c', "show-control", "Display frame control information" },
{ 's', "show-seqno", "Display sequence number" },
{ 'a', "show-addr", "Display addresses fields" },
{ 'S', "show-security", "Display security auxiliary field" },
{ 'M', "show-mac", "Display all informations about MAC frames" },
{ 'P', "show-payload", "Try to decode and display the payload" },
{ 'F', "show-fcs", "Display the frame check sequence" },
{ 'A', "show-all", "Display all informations" },
{ 0, NULL, NULL }
};
struct option opts[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
#ifdef COMMIT
{ "commit", no_argument, NULL, OPT_COMMIT },
#endif /* COMMIT */
{ "timeout", required_argument, NULL, 'T'},
{ "channel", required_argument, NULL, 'C' },
{ "baud", required_argument, NULL, 'b' },
{ "pcap", required_argument, NULL, 'p' },
{ "show-control", no_argument, NULL, 'c' },
{ "show-seqno", no_argument, NULL, 's' },
{ "show-addr", no_argument, NULL, 'a' },
{ "show-security", no_argument, NULL, 'S' },
{ "show-mac", no_argument, NULL, 'M' },
{ "show-fcs", no_argument, NULL, 'F' },
{ "show-payload", no_argument, NULL, 'P' },
{ "show-all", no_argument, NULL, 'A' },
{ NULL, 0, NULL, 0 }
};
while(1) {
int c = getopt_long(argc, argv, "hVp:C:cb:T:saSMFPA", opts, NULL);
if(c == -1)
break;
switch(c) {
case('p'):
pcap = optarg;
break;
case('b'):
speed = baud(optarg);
break;
case('T'):
timeout = xatou(optarg, &err);
if(err || timeout == 0)
errx(EXIT_FAILURE, "invalid timeout value");
break;
case('C'):
channel = parse_channel(optarg);
prot_mqueue_add_control(mqueue,
PROT_CTYPE_CONFIG_CHANNEL,
&channel,
sizeof(unsigned short));
break;
case('c'):
mac_info |= MI_CONTROL;
break;
case('s'):
mac_info |= MI_SEQNO;
break;
case('a'):
mac_info |= MI_ADDR;
break;
case('S'):
mac_info |= MI_SECURITY;
break;
case('M'):
mac_info = MI_ALL;
break;
case('F'):
mac_info |= MI_FCS;
break;
case('P'):
payload = true;
break;
case('A'):
mac_info = MI_ALL;
/* TODO: payload_info = PI_ALL */
break;
#ifdef COMMIT
case(OPT_COMMIT):
commit();
exit_status = EXIT_SUCCESS;
goto EXIT;
#endif /* COMMIT */
case('V'):
version(TARGET);
exit_status = EXIT_SUCCESS;
goto EXIT;
case('h'):
exit_status = EXIT_SUCCESS;
default:
help(name, "[OPTIONS] ... TTY", helps);
goto EXIT;
}
}
if((argc - optind) != 1)
errx(EXIT_FAILURE, "except tty device");
tty = argv[optind];
if(!pcap && !mac_info /* && !payload_info */)
warnx("doing nothing as requested");
if(pcap)
open_writing_pcap(pcap);
/* Register the cleanup function as the most common way to leave the event
loop is SIGINT. The program may also quit because of an error or the
SIGTERM signal. So we need to register an exit hook and signals too. A
setup function will register all signals and for us so we only care about
the cleanup functions themselves. */
setup_sig(cleanup, sig_cleanup, sig_flush);
/* That's where we really start the operations. */
fd = open_uart(tty, speed);
/* Initialisation of the transceiver
with a set of commands. */
prot_mqueue_sendall(mqueue, fd);
/* Read until timeout (if requested). */
input_loop(fd, message_cb, "Waiting", timeout);
exit_status = EXIT_SUCCESS;
EXIT:
return exit_status;
}