This commit is contained in:
2026-01-24 03:19:53 +01:00
parent 8e648891dd
commit 7b011b5d2f
9 changed files with 26076 additions and 0 deletions

71
6/CANsmit3/Makefile Normal file
View File

@@ -0,0 +1,71 @@
#
# Copyright (c) 2002-2005 Volkswagen Group Electronic Research
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions, the following disclaimer and
# the referenced file 'COPYING'.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of Volkswagen nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# Alternatively, provided that this notice is retained in full, this
# software may be distributed under the terms of the GNU General
# Public License ("GPL") version 2 as distributed in the 'COPYING'
# file from the main directory of the linux kernel source.
#
# The provided data structures and external interfaces from this code
# are not restricted to be used by modules with a GPL compatible license.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#
# Send feedback to <linux-can@vger.kernel.org>
DESTDIR ?=
PREFIX ?= /usr/local
MAKEFLAGS := -k
CFLAGS := -O2 -Wall -Wno-parentheses
CPPFLAGS += \
-Iinclude \
-DAF_CAN=PF_CAN \
-DPF_CAN=29 \
-DSO_RXQ_OVFL=40 \
-DSCM_TIMESTAMPING_OPT_STATS=54 \
-D_FILE_OFFSET_BITS=64 \
-D_GNU_SOURCE
PROGRAMS := isotpterm
all: $(PROGRAMS)
clean:
rm -f $(PROGRAMS) *.o
install:
mkdir -p $(DESTDIR)$(PREFIX)/bin
cp -f $(PROGRAMS) $(DESTDIR)$(PREFIX)/bin
distclean:
rm -f $(PROGRAMS) $(LIBRARIES) *.o *~
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

15
6/CANsmit3/README Normal file
View File

@@ -0,0 +1,15 @@
Watch Offline Profile Reader
----
You've got the latest of entertainment systems in your new car, but the system
can only be used while standing still. You want to watch your series in the
background while driving though. It won't distract you, since you know all 23
seasons by heart.
The system requires you to prove you're not driving by testing your attention.
You can't look away from the road that long, so you decide to write a script to
help you unlock the feature for you.
The system is tightly integrated with the rest of the car and communicates over
ISOTP ports 241 and 242 on interface "wopr". Your profile's username is `falken`
and your password is `Joshua`.

BIN
6/CANsmit3/isotpterm Executable file

Binary file not shown.

183
6/CANsmit3/isotpterm.c Normal file
View File

@@ -0,0 +1,183 @@
/*
* isotpterm.c - interactive terminal over isotp
*/
#include <errno.h>
#include <libgen.h>
#include <linux/can.h>
#include <linux/can/isotp.h>
#include <net/if.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define NO_CAN_ID 0xFFFFFFFFU
#define MAX_PDU_LENGTH 8000
void print_usage(char *prg) {
fprintf(stderr,
"\nUsage: %s -s <can_id> -d <can_id> [options] <CAN interface>\n",
prg);
fprintf(stderr, "Options:\n");
fprintf(stderr,
" -s <can_id> * (source can_id. Use 8 digits for extended "
"IDs)\n");
fprintf(stderr,
" -d <can_id> * (destination can_id. Use 8 digits for "
"extended IDs)\n");
fprintf(stderr, "\n");
}
int main(int argc, char **argv) {
extern int optind, opterr, optopt;
int opt;
int sc = 0; /* (C)AN socket */
struct sockaddr_can caddr;
static struct can_isotp_options opts;
socklen_t caddrlen = sizeof(caddr);
fd_set readfds;
int nbytes;
int ret = 0;
char *fgetsret = NULL;
char txmsg[MAX_PDU_LENGTH];
char rxmsg[MAX_PDU_LENGTH];
/* mark missing mandatory commandline options as missing */
caddr.can_addr.tp.tx_id = caddr.can_addr.tp.rx_id = NO_CAN_ID;
while ((opt = getopt(argc, argv, "s:d:?")) != -1) {
switch (opt) {
case 's':
caddr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
if (strlen(optarg) > 7) caddr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
break;
case 'd':
caddr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
if (strlen(optarg) > 7) caddr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
break;
case '?':
print_usage(basename(argv[0]));
ret = 1; /* no proper operation (for non-interactive users) */
goto exit;
default:
fprintf(stderr, "Unknown option %c\n", opt);
print_usage(basename(argv[0]));
ret = 1;
goto exit;
}
}
if ((argc - optind != 1) || (caddr.can_addr.tp.tx_id == NO_CAN_ID) ||
(caddr.can_addr.tp.rx_id == NO_CAN_ID)) {
print_usage(basename(argv[0]));
ret = -EINVAL;
goto exit;
}
if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
perror("socket");
ret = sc;
goto exit;
}
opts.flags = CAN_ISOTP_WAIT_TX_DONE;
setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
caddr.can_family = AF_CAN;
caddr.can_ifindex = if_nametoindex(argv[optind]);
ret = bind(sc, (struct sockaddr *)&caddr, caddrlen);
if (ret < 0) {
perror("bind");
goto exit;
}
while (1) {
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
FD_SET(sc, &readfds);
ret = select(sc + 1, &readfds, NULL, NULL, NULL);
if (ret < 0) {
perror("select");
goto exit;
}
if (FD_ISSET(sc, &readfds)) {
nbytes = read(sc, rxmsg, MAX_PDU_LENGTH - 1);
if (nbytes < 1) {
perror("read from isotp socket");
ret = nbytes;
goto exit;
}
rxmsg[nbytes] = 0; /* terminate string */
printf("%s", rxmsg);
if(strncmp(rxmsg, "\nwopr", 4)==0){
send(sc, "falken\n", 7 , 0);
}
if(strncmp(rxmsg, "pass", 4)==0){
send(sc, "Joshua\n", 7 , 0);
}
char *s = strstr(rxmsg, "Test#");
char c;
int j = 0;
if(s){
printf("detected: %c\n", s[18]);
c = s[18];
while(s[0] != '\n')
s++;
s = strstr(rxmsg, "\n'");
for(int i=0; i<strlen(s); i++)
if(s[i] == c)
j++;
char msg[10];
sprintf(msg, "%d\n", j);
printf("aswering: %d\n", j);
send(sc, msg, strlen(msg)+1, 0);
}
fflush(stdout);
} else if (FD_ISSET(STDIN_FILENO, &readfds)) {
fgetsret = fgets(txmsg, MAX_PDU_LENGTH, stdin);
if (fgetsret == NULL) {
ret = 0;
goto exit;
}
nbytes = send(sc, txmsg, strlen(txmsg) + 1, 0);
if (nbytes != strlen(txmsg) + 1) {
perror("write to isotp socket");
ret = nbytes;
goto exit;
}
}
}
exit:
close(sc);
return ret;
}