testj1939: add optional promiscuous mode and boradcast support

Upstream version of J1939 stack has different UAPI. To make documented
testj1939 examples work again we need at least boradcast flag.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
pull/175/head
Oleksij Rempel 2019-11-29 12:41:52 +01:00
parent 9ae877ce32
commit 5d3f8073b2
1 changed files with 30 additions and 1 deletions

View File

@ -39,7 +39,9 @@ static const char help_msg[] =
" This atually receives packets\n" " This atually receives packets\n"
" -c Issue connect()\n" " -c Issue connect()\n"
" -p=PRIO Set priority to PRIO\n" " -p=PRIO Set priority to PRIO\n"
" -P Promiscuous mode. Allow to receive all packets\n"
" -b Do normal bind with SA+1 and rebind with actual SA\n" " -b Do normal bind with SA+1 and rebind with actual SA\n"
" -B Allow to send and receive broadcast packets.\n"
" -o Omit bind\n" " -o Omit bind\n"
" -n Emit 64bit NAMEs in output\n" " -n Emit 64bit NAMEs in output\n"
" -w[TIME] Return after TIME (default 1) seconds\n" " -w[TIME] Return after TIME (default 1) seconds\n"
@ -49,7 +51,7 @@ static const char help_msg[] =
"\n" "\n"
; ;
static const char optstring[] = "?vbos::rep:cnw::"; static const char optstring[] = "?vbBPos::rep:cnw::";
static void onsigalrm(int sig) static void onsigalrm(int sig)
{ {
@ -92,6 +94,7 @@ int main(int argc, char *argv[])
int valid_peername = 0; int valid_peername = 0;
int todo_send = 0, todo_recv = 0, todo_echo = 0, todo_prio = -1; int todo_send = 0, todo_recv = 0, todo_echo = 0, todo_prio = -1;
int todo_connect = 0, todo_names = 0, todo_wait = 0, todo_rebind = 0; int todo_connect = 0, todo_names = 0, todo_wait = 0, todo_rebind = 0;
int todo_broadcast = 0, todo_promisc = 0;
int no_bind = 0; int no_bind = 0;
/* argument parsing */ /* argument parsing */
@ -114,6 +117,9 @@ int main(int argc, char *argv[])
case 'p': case 'p':
todo_prio = strtoul(optarg, NULL, 0); todo_prio = strtoul(optarg, NULL, 0);
break; break;
case 'P':
todo_promisc = 1;
break;
case 'c': case 'c':
todo_connect = 1; todo_connect = 1;
break; break;
@ -123,6 +129,9 @@ int main(int argc, char *argv[])
case 'b': case 'b':
todo_rebind = 1; todo_rebind = 1;
break; break;
case 'B':
todo_broadcast = 1;
break;
case 'o': case 'o':
no_bind = 1; no_bind = 1;
break; break;
@ -161,6 +170,26 @@ int main(int argc, char *argv[])
if (ret < 0) if (ret < 0)
err(1, "socket(j1939)"); err(1, "socket(j1939)");
if (todo_promisc) {
if (verbose)
fprintf(stderr, "- setsockopt(, SOL_SOCKET, SO_J1939_PROMISC, %d, %zd);\n",
todo_promisc, sizeof(todo_promisc));
ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_PROMISC,
&todo_promisc, sizeof(todo_promisc));
if (ret < 0)
err(1, "setsockopt: filed to set promiscuous mode");
}
if (todo_broadcast) {
if (verbose)
fprintf(stderr, "- setsockopt(, SOL_SOCKET, SO_BROADCAST, %d, %zd);\n",
todo_broadcast, sizeof(todo_broadcast));
ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
&todo_broadcast, sizeof(todo_broadcast));
if (ret < 0)
err(1, "setsockopt: filed to set broadcast");
}
if (todo_prio >= 0) { if (todo_prio >= 0) {
if (verbose) if (verbose)
fprintf(stderr, "- setsockopt(, SOL_CAN_J1939, SO_J1939_SEND_PRIO, &%i);\n", todo_prio); fprintf(stderr, "- setsockopt(, SOL_CAN_J1939, SO_J1939_SEND_PRIO, &%i);\n", todo_prio);