forked from linux-can/can-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jcat.c
369 lines (304 loc) · 7.26 KB
/
jcat.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2018 Pengutronix, Oleksij Rempel <[email protected]>
*/
#include <err.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <inttypes.h>
#include <net/if.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libj1939.h"
#define J1939_MAX_ETP_PACKET_SIZE (7 * 0x00ffffff)
#define JCAT_BUF_SIZE (1000 * 1024)
/*
* min()/max()/clamp() macros that also do
* strict type-checking.. See the
* "unnecessary" pointer comparison.
*/
#define min(x, y) ({ \
typeof(x) _min1 = (x); \
typeof(y) _min2 = (y); \
(void) (&_min1 == &_min2); \
_min1 < _min2 ? _min1 : _min2; })
struct jcat_priv {
int sock;
int infile;
int outfile;
unsigned int todo_send;
int todo_prio;
bool valid_peername;
bool todo_recv;
bool todo_filesize;
bool todo_connect;
struct sockaddr_can sockname;
struct sockaddr_can peername;
};
static const char help_msg[] =
"jcat: netcat tool for j1939\n"
"Usage: jcat FROM TO\n"
" FROM / TO - or [IFACE][:[SA][,[PGN][,NAME]]]\n"
"Options:\n"
" -i <infile> (default stdin)\n"
" -s[=LEN] Initial send of LEN bytes dummy data\n"
" -r Receive data\n"
"\n"
"Example:\n"
"jcat -i some_file_to_send can0:0x80 :0x90,0x12300\n"
"jcat can0:0x90 -r > /tmp/some_file_to_receive\n"
"\n"
;
static const char optstring[] = "?i:vs::rp:cnw::";
static void jcat_init_sockaddr_can(struct sockaddr_can *sac)
{
sac->can_family = AF_CAN;
sac->can_addr.j1939.addr = J1939_NO_ADDR;
sac->can_addr.j1939.name = J1939_NO_NAME;
sac->can_addr.j1939.pgn = J1939_NO_PGN;
}
static int jcat_sendfile(struct jcat_priv *priv, int out_fd, int in_fd,
off_t *offset, size_t count)
{
int ret = EXIT_SUCCESS;
off_t orig = 0;
char *buf;
size_t to_read, num_read, num_sent, tot_sent, buf_size;
int round = 0;
buf_size = min((size_t)J1939_MAX_ETP_PACKET_SIZE, count);
buf = malloc(buf_size);
if (!buf) {
warn("can't allocate buf");
ret = EXIT_FAILURE;
goto do_nofree;
}
if (offset) {
/* Save current file offset and set offset to value in '*offset' */
orig = lseek(in_fd, 0, SEEK_CUR);
if (orig == -1) {
ret = EXIT_FAILURE;
goto do_free;
}
if (lseek(in_fd, *offset, SEEK_SET) == -1) {
ret = EXIT_FAILURE;
goto do_free;
}
}
tot_sent = 0;
while (count > 0) {
to_read = min(buf_size, count);
num_read = read(in_fd, buf, to_read);
if (num_read == -1) {
ret = EXIT_FAILURE;
goto do_free;
}
if (num_read == 0)
break; /* EOF */
if (priv->valid_peername && !priv->todo_connect)
num_sent = sendto(out_fd, buf, num_read, 0,
(void *)&priv->peername,
sizeof(priv->peername));
else
num_sent = send(out_fd, buf, num_read, 0);
if (num_sent == -1) {
warn("sendfile: write() transferr error");
ret = EXIT_FAILURE;
goto do_free;
}
if (num_sent == 0) /* Should never happen */ {
warn("sendfile: write() transferred 0 bytes");
ret = EXIT_FAILURE;
goto do_free;
}
if (num_sent != num_read) {
warn("sendfile: write() not full transfer: %zi %zi",
num_sent, num_read);
ret = EXIT_FAILURE;
goto do_free;
}
round++;
count -= num_sent;
tot_sent += num_sent;
}
if (offset) {
/* Return updated file offset in '*offset', and reset the file offset
to the value it had when we were called. */
*offset = lseek(in_fd, 0, SEEK_CUR);
if (*offset == -1) {
ret = EXIT_FAILURE;
goto do_free;
}
if (lseek(in_fd, orig, SEEK_SET) == -1) {
ret = EXIT_FAILURE;
goto do_free;
}
}
do_free:
free(buf);
do_nofree:
return ret;
}
static size_t jcat_get_file_size(int fd)
{
off_t offset;
offset = lseek(fd, 0, SEEK_END);
if (offset == -1)
error(1, errno, "%s lseek()\n", __func__);
if (lseek(fd, 0, SEEK_SET) == -1)
error(1, errno, "%s lseek() start\n", __func__);
return offset;
}
static int jcat_send(struct jcat_priv *priv)
{
if (priv->todo_filesize)
priv->todo_send = jcat_get_file_size(priv->infile);
if (!priv->todo_send)
return EXIT_FAILURE;
return jcat_sendfile(priv, priv->sock, priv->infile, NULL,
priv->todo_send);
}
static int jcat_recv_one(struct jcat_priv *priv, uint8_t *buf, size_t buf_size)
{
socklen_t peernamelen;
int ret;
peernamelen = sizeof(priv->peername);
ret = recvfrom(priv->sock, buf, buf_size, 0,
(void *)&priv->peername, &peernamelen);
if (ret < 0) {
warn("recvfrom()");
return EXIT_FAILURE;
}
ret = write(priv->outfile, buf, ret);
if (ret < 0) {
warn("write stdout()");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static int jcat_recv(struct jcat_priv *priv)
{
int ret = EXIT_SUCCESS;
size_t buf_size;
uint8_t *buf;
buf_size = J1939_MAX_ETP_PACKET_SIZE;
buf = malloc(buf_size);
if (!buf) {
warn("can't allocate rx buf");
return EXIT_FAILURE;;
}
while (priv->todo_recv) {
ret = jcat_recv_one(priv, buf, buf_size);
if (ret)
break;
}
free(buf);
return ret;
}
static int jcat_sock_prepare(struct jcat_priv *priv)
{
int ret;
/* open socket */
priv->sock = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
if (priv->sock < 0) {
warn("socket(j1939)");
return EXIT_FAILURE;
}
if (priv->todo_prio >= 0) {
ret = setsockopt(priv->sock, SOL_CAN_J1939, SO_J1939_SEND_PRIO,
&priv->todo_prio, sizeof(priv->todo_prio));
if (ret < 0) {
warn("set priority %i", priv->todo_prio);
return EXIT_FAILURE;
}
}
ret = bind(priv->sock, (void *)&priv->sockname, sizeof(priv->sockname));
if (ret < 0) {
warn("bind()");
return EXIT_FAILURE;
}
if (!priv->todo_connect)
return EXIT_SUCCESS;
if (!priv->valid_peername) {
warn("no peername supplied");
return EXIT_FAILURE;
}
ret = connect(priv->sock, (void *)&priv->peername,
sizeof(priv->peername));
if (ret < 0) {
warn("connect()");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static int jcat_parse_args(struct jcat_priv *priv, int argc, char *argv[])
{
int opt;
/* argument parsing */
while ((opt = getopt(argc, argv, optstring)) != -1)
switch (opt) {
case 'i':
priv->infile = open(optarg, O_RDONLY);
if (priv->infile == -1)
error(EXIT_FAILURE, errno, "can't open input file");
priv->todo_filesize = 1;
break;
case 's':
priv->todo_send = strtoul(optarg ?: "8", NULL, 0);
break;
case 'r':
priv->todo_recv = 1;
break;
case 'p':
priv->todo_prio = strtoul(optarg, NULL, 0);
break;
case 'c':
priv->todo_connect = 1;
break;
default:
fputs(help_msg, stderr);
return EXIT_FAILURE;
}
if (argv[optind]) {
if (strcmp("-", argv[optind]))
libj1939_parse_canaddr(argv[optind], &priv->sockname);
optind++;
}
if (argv[optind]) {
if (strcmp("-", argv[optind])) {
libj1939_parse_canaddr(argv[optind], &priv->peername);
priv->valid_peername = 1;
}
optind++;
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
struct jcat_priv *priv;
int ret;
priv = malloc(sizeof(*priv));
if (!priv)
error(EXIT_FAILURE, errno, "can't allocate priv");
bzero(priv, sizeof(*priv));
priv->todo_prio = -1;
priv->infile = STDIN_FILENO;
priv->outfile = STDOUT_FILENO;
jcat_init_sockaddr_can(&priv->sockname);
jcat_init_sockaddr_can(&priv->peername);
ret = jcat_parse_args(priv, argc, argv);
if (ret)
return ret;
ret = jcat_sock_prepare(priv);
if (ret)
return ret;
if (priv->todo_recv)
ret = jcat_recv(priv);
else
ret = jcat_send(priv);
close(priv->infile);
return ret;
}