Merge pull request #316 from Rubusch/lothar/cansniffer

cansniffer: fix detection of invalid provided CAN interface, refacs
pull/319/head
Marc Kleine-Budde 2021-10-19 08:43:20 +02:00 committed by GitHub
commit 02e8b1ff39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 65 deletions

View File

@ -102,14 +102,14 @@ PROGRAMS := \
all: $(PROGRAMS)
clean:
rm -f $(PROGRAMS) *.o
rm -f $(PROGRAMS) *.o mcp251xfd/*.o
install:
mkdir -p $(DESTDIR)$(PREFIX)/bin
cp -f $(PROGRAMS) $(DESTDIR)$(PREFIX)/bin
distclean:
rm -f $(PROGRAMS) $(LIBRARIES) *.o *~
distclean: clean
rm -f $(PROGRAMS) $(LIBRARIES) *~
asc2log.o: lib.h
canbusload.o: lib.h

View File

@ -236,7 +236,7 @@ int main(int argc, char **argv)
long currcms = 0;
long lastcms = 0;
unsigned char quiet = 0;
int opt, ret;
int opt, ret = 0;
struct timeval timeo, start_tv, tv;
struct sockaddr_can addr;
int i;
@ -343,9 +343,11 @@ int main(int argc, char **argv)
}
}
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0) {
perror("bind");
return 1;
close(s);
return ret;
}
gettimeofday(&start_tv, NULL);
@ -362,11 +364,13 @@ int main(int argc, char **argv)
timeo.tv_sec = 0;
timeo.tv_usec = 10000 * loop;
if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
ret = select(s+1, &rdfs, NULL, NULL, &timeo);
if (ret < 0) {
//perror("select");
running = 0;
continue;
}
} else
ret = 0;
gettimeofday(&tv, NULL);
currcms = (tv.tv_sec - start_tv.tv_sec) * 100 + (tv.tv_usec / 10000);
@ -386,7 +390,7 @@ int main(int argc, char **argv)
printf("%s", CSR_SHOW); /* show cursor */
close(s);
return 0;
return ret;
}
void do_modify_sniftab(unsigned int value, unsigned int mask, char cmd)
@ -441,7 +445,7 @@ int handle_keyb(void)
}
/* check for single SFF/EFF CAN ID length */
if (!((clen == 3) || (clen == 8)))
if (clen != 3 && clen != 8)
break;
/* enable/disable single SFF/EFF CAN ID */
@ -576,16 +580,15 @@ int handle_frame(int fd, long currcms)
pos = sniftab_index(cf.can_id);
if (pos < 0) {
/* CAN ID not existing */
if (idx < MAX_SLOTS) {
/* assign new slot */
pos = idx++;
rx_changed = true;
run_qsort = true;
} else {
if (idx >= MAX_SLOTS) {
/* informative exit */
perror("number of different CAN IDs exceeded MAX_SLOTS");
return 0; /* quit */
}
/* assign new slot */
pos = idx++;
rx_changed = true;
run_qsort = true;
}
else {
if (cf.can_dlc == sniftab[pos].current.can_dlc)
@ -780,8 +783,11 @@ void writesettings(char* name)
strncat(fname, name, 29 - strlen(fname));
fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd <= 0) {
printf("unable to write setting file '%s'!\n", fname);
return;
}
if (fd > 0) {
for (i = 0; i < idx ;i++) {
sprintf(buf, "<%08X>%c.", sniftab[i].current.can_id, (is_set(i, ENABLE))?'1':'0');
if (write(fd, buf, 12) < 0)
@ -797,9 +803,6 @@ void writesettings(char* name)
}
close(fd);
}
else
printf("unable to write setting file '%s'!\n", fname);
}
int readsettings(char* name)
{
@ -812,10 +815,15 @@ int readsettings(char* name)
strncat(fname, name, 29 - strlen(fname));
fd = open(fname, O_RDONLY);
if (fd > 0) {
if (fd <= 0) {
return -1;
}
idx = 0;
while (!done) {
if (read(fd, &buf, 29) == 29) {
if (read(fd, &buf, 29) != 29) {
done = true;
continue;
}
unsigned long id = strtoul(&buf[1], (char **)NULL, 16);
sniftab[idx].current.can_id = id;
@ -833,15 +841,9 @@ int readsettings(char* name)
if (++idx >= MAX_SLOTS)
break;
}
else
done = true;
}
close(fd);
}
else
return -1;
return idx;
}