From 6133ac94f0119bb402796875555c592f242afdb7 Mon Sep 17 00:00:00 2001 From: Bastian Stender Date: Mon, 22 Jan 2018 11:44:24 +0100 Subject: [PATCH 1/2] jacd: print relevant API calls on verbose The output is now comparable to testj1939's -v output. Signed-off-by: Bastian Stender --- Makefile | 3 ++- jacd.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 21c2e78..0fd201a 100644 --- a/Makefile +++ b/Makefile @@ -86,6 +86,7 @@ canbusload.o: lib.h log2long.o: lib.h log2asc.o: lib.h asc2log.o: lib.h +jacd.o: libj1939.h jspy.o: libj1939.h jsr.o: libj1939.h testj1939.o: libj1939.h @@ -100,7 +101,7 @@ log2long: log2long.o lib.o log2asc: log2asc.o lib.o asc2log: asc2log.o lib.o canbusload: canbusload.o canframelen.o - +jacd: jacd.o libj1939.o jspy: jspy.o libj1939.o jsr: jsr.o libj1939.o testj1939: testj1939.o libj1939.o diff --git a/jacd.c b/jacd.c index 1358918..416928d 100644 --- a/jacd.c +++ b/jacd.c @@ -25,6 +25,8 @@ #include #include +#include "libj1939.h" + static const char help_msg[] = "jacd: An SAE J1939 address claiming daemon" "\n" "Usage: jacd [options] NAME [INTF]" "\n" @@ -196,26 +198,36 @@ static int open_socket(const char *device, uint64_t name) }, }; + if (s.verbose) + fprintf(stderr, "- socket(PF_CAN, SOCK_DGRAM, CAN_J1939);\n"); sock = ret = socket(PF_CAN, SOCK_DGRAM, CAN_J1939); if (ret < 0) error(1, errno, "socket(j1939)"); + if (s.verbose) + fprintf(stderr, "- setsockopt(, SOL_SOCKET, SO_BINDTODEVICE, %s, %zd);\n", device, strlen(device)); ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)); if (ret < 0) error(1, errno, "bindtodevice %s", device); + if (s.verbose) + fprintf(stderr, "- setsockopt(, SOL_CAN_J1939, SO_J1939_FILTER, , %zd);\n", sizeof(filt)); ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, &filt, sizeof(filt)); if (ret < 0) error(1, errno, "setsockopt filter"); value = 1; + if (s.verbose) + fprintf(stderr, "- setsockopt(, SOL_CAN_J1939, SO_J1939_RECV_OWN, %d, %zd);\n", value, sizeof(value)); ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_RECV_OWN, &value, sizeof(value)); if (ret < 0) error(1, errno, "setsockopt receive own msgs"); + if (s.verbose) + fprintf(stderr, "- bind(, %s, %zi);\n", libj1939_addr2str(&saddr), sizeof(saddr)); ret = bind(sock, (void *)&saddr, sizeof(saddr)); if (ret < 0) error(1, errno, "bind()"); @@ -231,6 +243,8 @@ static int repeat_address(int sock, uint64_t name) memcpy(dat, &name, 8); if (!host_is_little_endian()) bswap(dat, 8); + if (s.verbose) + fprintf(stderr, "- send(, %" PRId64 ", 8, 0);\n", name); ret = send(sock, dat, 8, 0); if (must_warn(ret)) error(1, errno, "send address claim for 0x%02x", s.last_sa); @@ -248,6 +262,8 @@ static int claim_address(int sock, uint64_t name, int sa) }, }; + if (s.verbose) + fprintf(stderr, "- bind(, %s, %zi);\n", libj1939_addr2str(&saddr), sizeof(saddr)); ret = bind(sock, (void *)&saddr, sizeof(saddr)); if (ret < 0) error(1, errno, "rebind with sa 0x%02x", sa); @@ -265,6 +281,8 @@ static int request_addresses(int sock) .can_addr.j1939.addr = J1939_NO_ADDR, }; + if (s.verbose) + fprintf(stderr, "- sendto(, { 0, 0xee, 0, }, %zi, 0, %s, %zi);\n", sizeof(dat), libj1939_addr2str(&saddr), sizeof(saddr)); ret = sendto(sock, dat, sizeof(dat), 0, (void *)&saddr, sizeof(saddr)); if (must_warn(ret)) error(1, errno, "send request for address claims"); From cf039f14fb4083c35937a191f800198eefd0b5b4 Mon Sep 17 00:00:00 2001 From: Bastian Stender Date: Fri, 19 Jan 2018 15:30:29 +0100 Subject: [PATCH 2/2] jacd: provide interface index while opening socket and address claiming This is a direct consequence of not allowing bind to any device ("3cb6a1204598 j1939: socket: bind() cleanups"), i.e. can_ifindex == 0. This means before this change jacd would receive from any interface that was opened by another program. Without this patch jacd fails with: acd: bind(): No such device A more verbose call shows the missing interface: $ jacd -v 1122334455667788 jacd: ready for can0:1122334455667788 - socket(PF_CAN, SOCK_DGRAM, CAN_J1939); - setsockopt(, SOL_SOCKET, SO_BINDTODEVICE, can0, 4); - setsockopt(, SOL_CAN_J1939, SO_J1939_FILTER, , 84); - setsockopt(, SOL_CAN_J1939, SO_J1939_RECV_OWN, 1, 4); - bind(, :fe,0ee00,1122334455667788, 24); jacd: bind(): No such device Signed-off-by: Bastian Stender --- jacd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/jacd.c b/jacd.c index 416928d..bac4060 100644 --- a/jacd.c +++ b/jacd.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -196,6 +197,7 @@ static int open_socket(const char *device, uint64_t name) .addr = J1939_IDLE_ADDR, .pgn = 0x0ee00, }, + .can_ifindex = if_nametoindex(s.intf), }; if (s.verbose) @@ -260,6 +262,7 @@ static int claim_address(int sock, uint64_t name, int sa) .addr = sa, .pgn = 0x0ee00, }, + .can_ifindex = if_nametoindex(s.intf), }; if (s.verbose)