forked from linux-can/can-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
j1939spy.c
295 lines (272 loc) · 7.04 KB
/
j1939spy.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
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright (c) 2011 EIA Electronics
*
* Authors:
* Kurt Van Dijck <kurt.van.dijck@eia.be>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the version 2 of the GNU General Public License
* as published by the Free Software Foundation
*/
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <err.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include "libj1939.h"
/*
* getopt
*/
static const char help_msg[] =
"j1939spy: An SAE J1939 spy utility" "\n"
"Usage: j1939spy [OPTION...] [[IFACE:][NAME|SA][,PGN]]" "\n"
"Options:\n"
" -P, --promisc Run in promiscuous mode" "\n"
" (= receive traffic not for this ECU)" "\n"
" -b, --block=SIZE Use a receive buffer of SIZE (default 1024)" "\n"
" -t, --time[=a|d|z|A] Show time: (a)bsolute, (d)elta, (z)ero, (A)bsolute w date" "\n"
;
#ifdef _GNU_SOURCE
static struct option long_opts[] = {
{ "help", no_argument, NULL, '?', },
{ "promisc", no_argument, NULL, 'P', },
{ "block", required_argument, NULL, 'b', },
{ "time", optional_argument, NULL, 't', },
{ },
};
#else
#define getopt_long(argc, argv, optstring, longopts, longindex) \
getopt((argc), (argv), (optstring))
#endif
static const char optstring[] = "vPb:t::?";
/*
* static variables
*/
static struct {
struct sockaddr_can addr;
int promisc;
int time;
int pkt_len;
} s = {
.pkt_len = 1024,
.addr.can_addr.j1939 = {
.name = J1939_NO_NAME,
.addr = J1939_NO_ADDR,
.pgn = J1939_NO_PGN,
},
};
/*
* useful buffers
*/
static const int ival_1 = 1;
static char ctrlmsg[
CMSG_SPACE(sizeof(struct timeval))
+ CMSG_SPACE(sizeof(uint8_t)) /* dest addr */
+ CMSG_SPACE(sizeof(uint64_t)) /* dest name */
+ CMSG_SPACE(sizeof(uint8_t)) /* priority */
];
static struct iovec iov;
static struct msghdr msg;
static struct cmsghdr *cmsg;
static uint8_t *buf;
/*
* program
*/
int main(int argc, char **argv)
{
int ret, sock, opt;
unsigned int j, len;
struct timeval tref, tdut, ttmp;
struct sockaddr_can src;
struct j1939_filter filt;
int filter = 0;
uint8_t priority, dst_addr;
uint64_t dst_name;
uint64_t recvflags;
/* argument parsing */
while ((opt = getopt_long(argc, argv, optstring, long_opts, NULL)) != -1)
switch (opt) {
case 'b':
s.pkt_len = strtoul(optarg, 0, 0);
break;
case 'P':
++s.promisc;
break;
case 't':
if (optarg) {
if (!strchr("adzA", optarg[0]))
err(1, "unknown time option '%c'",
optarg[0]);
s.time = optarg[0];
} else {
s.time = 'z';
}
break;
default:
fputs(help_msg, stderr);
exit(1);
break;
}
if (argv[optind]) {
optarg = argv[optind];
ret = libj1939_str2addr(optarg, 0, &s.addr);
if (ret < 0) {
err(0, "bad URI %s", optarg);
return 1;
}
}
buf = malloc(s.pkt_len);
if (!buf)
err(1, "malloc %u", s.pkt_len);
/* setup socket */
sock = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
if (sock < 0)
err(1, "socket(can, dgram, j1939)");
memset(&filt, 0, sizeof(filt));
if (s.addr.can_addr.j1939.name) {
filt.name = s.addr.can_addr.j1939.name;
filt.name_mask = ~0ULL;
++filter;
}
if (s.addr.can_addr.j1939.addr < 0xff) {
filt.addr = s.addr.can_addr.j1939.addr;
filt.addr_mask = ~0;
++filter;
}
if (s.addr.can_addr.j1939.pgn <= J1939_PGN_MAX) {
filt.pgn = s.addr.can_addr.j1939.pgn;
filt.pgn_mask = ~0;
++filter;
}
if (filter) {
ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, &filt, sizeof(filt));
if (ret < 0)
err(1, "setsockopt filter");
}
if (s.promisc) {
ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_PROMISC, &ival_1, sizeof(ival_1));
if (ret < 0)
err(1, "setsockopt promisc");
}
if (s.time) {
ret = setsockopt(sock, SOL_SOCKET, SO_TIMESTAMP, &ival_1, sizeof(ival_1));
if (ret < 0)
err(1, "setsockopt timestamp");
}
ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &s.pkt_len, sizeof(s.pkt_len));
if (ret < 0)
err(1, "setsockopt rcvbuf %u", s.pkt_len);
/* bind(): to default, only ifindex is used. */
memset(&src, 0, sizeof(src));
src.can_ifindex = s.addr.can_ifindex;
src.can_family = AF_CAN;
src.can_addr.j1939.name = J1939_NO_NAME;
src.can_addr.j1939.addr = J1939_NO_ADDR;
src.can_addr.j1939.pgn = J1939_NO_PGN;
ret = bind(sock, (void *)&src, sizeof(src));
if (ret < 0)
err(1, "bind(%s)", argv[1]);
/* these settings are static and can be held out of the hot path */
iov.iov_base = &buf[0];
msg.msg_name = &src;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = &ctrlmsg;
memset(&tref, 0, sizeof(tref));
while (1) {
/* these settings may be modified by recvmsg() */
iov.iov_len = s.pkt_len;
msg.msg_namelen = sizeof(src);
msg.msg_controllen = sizeof(ctrlmsg);
msg.msg_flags = 0;
ret = recvmsg(sock, &msg, 0);
//ret = recvfrom(buf, s.pkt_len, 0, (void *)&addr, &len);
if (ret < 0) {
switch (errno) {
case ENETDOWN:
err(0, "ifindex %i", s.addr.can_ifindex);
continue;
case EINTR:
continue;
default:
err(1, "recvmsg(ifindex %i)", s.addr.can_ifindex);
break;
}
}
len = ret;
recvflags = 0;
dst_addr = 0;
priority = 0;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
switch (cmsg->cmsg_level) {
case SOL_SOCKET:
if (cmsg->cmsg_type == SCM_TIMESTAMP) {
memcpy(&tdut, CMSG_DATA(cmsg), sizeof(tdut));
recvflags |= 1ULL << cmsg->cmsg_type;
}
break;
case SOL_CAN_J1939:
recvflags |= 1ULL << cmsg->cmsg_type;
if (cmsg->cmsg_type == SCM_J1939_DEST_ADDR)
dst_addr = *CMSG_DATA(cmsg);
else if (cmsg->cmsg_type == SCM_J1939_DEST_NAME)
memcpy(&dst_name, CMSG_DATA(cmsg), cmsg->cmsg_len - CMSG_LEN(0));
else if (cmsg->cmsg_type == SCM_J1939_PRIO)
priority = *CMSG_DATA(cmsg);
break;
}
}
if (recvflags & (1ULL << SCM_TIMESTAMP)) {
if ('z' == s.time) {
if (!tref.tv_sec)
tref = tdut;
timersub(&tdut, &tref, &ttmp);
tdut = ttmp;
goto abs_time;
} else if ('d' == s.time) {
timersub(&tdut, &tref, &ttmp);
tref = tdut;
tdut = ttmp;
goto abs_time;
} else if ('a' == s.time) {
abs_time:
printf("(%llu.%04llu)", (unsigned long long)tdut.tv_sec, (unsigned long long)tdut.tv_usec / 100);
} else if ('A' == s.time) {
struct tm tm;
tm = *localtime(&tdut.tv_sec);
printf("(%04u%02u%02uT%02u%02u%02u.%04llu)",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
(unsigned long long)tdut.tv_usec/100);
}
}
printf(" %s ", libj1939_addr2str(&src));
if (recvflags & (1ULL << SCM_J1939_DEST_NAME))
printf("%016llx ", (unsigned long long)dst_name);
else if (recvflags & (1ULL << SCM_J1939_DEST_ADDR))
printf("%02x ", dst_addr);
else
printf("- ");
printf("!%u ", priority);
printf("[%i%s]", len, (msg.msg_flags & MSG_TRUNC) ? "..." : "");
for (j = 0; j < len;) {
unsigned int end = j + 4;
if (end > len)
end = len;
printf(" ");
for (; j < end; ++j)
printf("%02x", buf[j]);
}
printf("\n");
}
free(buf);
return 0;
}