diff --git a/slcan_attach.c b/slcan_attach.c index 8410b65..7027666 100644 --- a/slcan_attach.c +++ b/slcan_attach.c @@ -88,7 +88,8 @@ int main(int argc, char **argv) int send_read_status_flags = 0; char *speed = NULL; char *btr = NULL; - char buf[IFNAMSIZ+1]; + char buf[20]; + static struct ifreq ifr; char *tty; char *name = NULL; int opt; @@ -133,7 +134,7 @@ int main(int argc, char **argv) case 'n': name = optarg; - if (strlen(name) > IFNAMSIZ-1) + if (strlen(name) > sizeof(ifr.ifr_newname) - 1) print_usage(argv[0]); break; @@ -201,16 +202,15 @@ int main(int argc, char **argv) } /* retrieve the name of the created CAN netdevice */ - if (ioctl (fd, SIOCGIFNAME, buf) < 0) { + if (ioctl (fd, SIOCGIFNAME, ifr.ifr_name) < 0) { perror("ioctl SIOCGIFNAME"); exit(1); } - printf("attached tty %s to netdevice %s\n", tty, buf); + printf("attached tty %s to netdevice %s\n", tty, ifr.ifr_name); /* try to rename the created device if requested */ if (name) { - struct ifreq ifr; int s = socket(PF_INET, SOCK_DGRAM, 0); printf("rename netdevice %s to %s ... ", buf, name); @@ -218,8 +218,9 @@ int main(int argc, char **argv) if (s < 0) perror("socket for interface rename"); else { - strncpy (ifr.ifr_name, buf, IFNAMSIZ); - strncpy (ifr.ifr_newname, name, IFNAMSIZ); + /* current slcan%d name is still in ifr.ifr_name */ + memset (ifr.ifr_newname, 0, sizeof(ifr.ifr_newname)); + strncpy (ifr.ifr_newname, name, sizeof(ifr.ifr_newname) - 1); if (ioctl(s, SIOCSIFNAME, &ifr) < 0) printf("failed!\n"); diff --git a/slcand.c b/slcand.c index 3c8c755..220048a 100644 --- a/slcand.c +++ b/slcand.c @@ -180,7 +180,8 @@ int main(int argc, char *argv[]) char *tty = NULL; char const *devprefix = "/dev/"; char *name = NULL; - char buf[IFNAMSIZ+1]; + char buf[20]; + static struct ifreq ifr; struct termios tios; speed_t old_ispeed; speed_t old_ospeed; @@ -270,6 +271,8 @@ int main(int argc, char *argv[]) print_usage(argv[0]); name = argv[optind + 1]; + if (name && (strlen(name) > sizeof(ifr.ifr_newname) - 1)) + print_usage(argv[0]); /* Prepare the tty device name string */ pch = strstr(tty, devprefix); @@ -369,23 +372,23 @@ int main(int argc, char *argv[]) } /* retrieve the name of the created CAN netdevice */ - if (ioctl(fd, SIOCGIFNAME, buf) < 0) { + if (ioctl(fd, SIOCGIFNAME, ifr.ifr_name) < 0) { perror("ioctl SIOCGIFNAME"); exit(EXIT_FAILURE); } - syslogger(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf); + syslogger(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, ifr.ifr_name); /* try to rename the created netdevice */ if (name) { - struct ifreq ifr; int s = socket(PF_INET, SOCK_DGRAM, 0); if (s < 0) perror("socket for interface rename"); else { - strncpy(ifr.ifr_name, buf, IFNAMSIZ); - strncpy(ifr.ifr_newname, name, IFNAMSIZ); + /* current slcan%d name is still in ifr.ifr_name */ + memset (ifr.ifr_newname, 0, sizeof(ifr.ifr_newname)); + strncpy (ifr.ifr_newname, name, sizeof(ifr.ifr_newname) - 1); if (ioctl(s, SIOCSIFNAME, &ifr) < 0) { syslogger(LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);