From d9b9a044582977da96ae5e415a52a70c46c6c38c Mon Sep 17 00:00:00 2001 From: Andrzej Stalke Date: Thu, 23 Nov 2023 15:25:39 +0100 Subject: [PATCH] psh/ifconfig: Change form of error reporting in display Program no longer stops, if the ioctl fails due to some function being unimplemented. Now it displays that feature is "unavailable". JIRA: RTOS-628 --- psh/ifconfig/ifconfig.c | 135 ++++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 52 deletions(-) diff --git a/psh/ifconfig/ifconfig.c b/psh/ifconfig/ifconfig.c index 09e08d7f..67109f4a 100644 --- a/psh/ifconfig/ifconfig.c +++ b/psh/ifconfig/ifconfig.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "../psh.h" @@ -79,89 +80,119 @@ static inline const char *psh_ifconfigPrintFlag(unsigned int flag) static inline int psh_ifconfigPrintInterface(const struct ifaddrs *interface, int sd) { - unsigned int flags, i; - struct in_addr addr, broadcast, mask; + static const char errMsg[] = "unavailable"; + const char *msg; + unsigned int interfaceFlags, i; int ret; struct ifreq ioctlInterface; (void)strncpy(ioctlInterface.ifr_name, interface->ifa_name, IFNAMSIZ - 1); + printf("%-10s", interface->ifa_name); + /* Get interface flags */ ret = ioctl(sd, SIOCGIFFLAGS, &ioctlInterface); if (ret < 0) { + /* Being unable to obtain flags is a critical error */ perror("ioctl(SIOCGIFFLAGS)"); return ret; } - flags = ioctlInterface.ifr_flags; + interfaceFlags = ioctlInterface.ifr_flags; - /* Get interface IP address */ - ret = ioctl(sd, SIOCGIFADDR, &ioctlInterface); - if (ret < 0) { - perror("ioctl(SIOCGIFADDR)"); - return ret; + printf("Link encap:"); + switch (interfaceFlags & (IFF_LOOPBACK | IFF_POINTOPOINT)) { + case IFF_LOOPBACK: + printf("Local Loopback"); + break; + case IFF_POINTOPOINT: + printf("Point to Point"); + break; + case 0: + /* Get interface hardware address */ + (void)memset(&ioctlInterface.ifr_hwaddr, 0, sizeof(struct sockaddr)); + if (ioctl(sd, SIOCGIFHWADDR, &ioctlInterface) < 0) { + printf("%s", errMsg); + } + else { + if (ioctlInterface.ifr_hwaddr.sa_family == ARPHRD_ETHER) { + printf("Ethernet HWAddr"); + for (ret = 0; ret < 6; ++ret) { + printf(":%02hhx", ioctlInterface.ifr_hwaddr.sa_data[ret]); + } + } + else { + printf("Unimplemented"); + } + } + break; + default: + printf("Unimplemented"); + break; } - addr = ((struct sockaddr_in *)&ioctlInterface.ifr_addr)->sin_addr; + printf("\n%10s", ""); - /* Get interface broadcast IP address */ - ret = ioctl(sd, SIOCGIFBRDADDR, &ioctlInterface); - if (ret < 0) { - perror("ioctl(SIOCGIFBRDADDR)"); - return ret; + /* Get interface IP address */ + if (ioctl(sd, SIOCGIFADDR, &ioctlInterface) < 0) { + msg = errMsg; } - broadcast = ((struct sockaddr_in *)&ioctlInterface.ifr_addr)->sin_addr; - - /* Get interface IP mask */ - ret = ioctl(sd, SIOCGIFNETMASK, &ioctlInterface); - if (ret < 0) { - perror("ioctl(SIOCGIFNETMASK)"); - return ret; + else { + msg = inet_ntoa(((struct sockaddr_in *)&ioctlInterface.ifr_addr)->sin_addr); + } + printf("%s:%s ", ((interfaceFlags & IFF_POINTOPOINT) == 0) ? "inet addr" : "local", msg); + if ((interfaceFlags & IFF_POINTOPOINT) != 0) { + if (ioctl(sd, SIOCGIFDSTADDR, &ioctlInterface) < 0) { + msg = errMsg; + } + else { + msg = inet_ntoa(((struct sockaddr_in *)&ioctlInterface.ifr_dstaddr)->sin_addr); + } + printf("remote:%s ", msg); } - mask = ((struct sockaddr_in *)&ioctlInterface.ifr_addr)->sin_addr; - - printf("%-10s", interface->ifa_name); - if ((flags & IFF_LOOPBACK) == 0) { - /* Get interface hardware address */ - (void)memset(&ioctlInterface.ifr_hwaddr, 0, sizeof(struct sockaddr)); - ret = ioctl(sd, SIOCGIFHWADDR, &ioctlInterface); - if (ret < 0) { - perror("ioctl(SIOCGIFHWADDR)"); - return ret; + if ((interfaceFlags & IFF_POINTOPOINT) == 0) { + /* Get interface broadcast IP address */ + if (ioctl(sd, SIOCGIFBRDADDR, &ioctlInterface) < 0) { + msg = errMsg; } - printf("HWAddr"); - for (ret = 0; ret < 6; ++ret) { - printf(":%02hhx", ioctlInterface.ifr_hwaddr.sa_data[ret]); + else { + msg = inet_ntoa(((struct sockaddr_in *)&ioctlInterface.ifr_addr)->sin_addr); } - printf("\n%10s", ""); + printf("Broadcast:%s ", msg); + } + + /* Get interface IP mask */ + if (ioctl(sd, SIOCGIFNETMASK, &ioctlInterface) < 0) { + msg = errMsg; + } + else { + msg = inet_ntoa(((struct sockaddr_in *)&ioctlInterface.ifr_addr)->sin_addr); } + printf("Mask:%s\n", msg); - printf("inet addr:%s ", inet_ntoa(addr)); - printf("Broadcast:%s ", inet_ntoa(broadcast)); - printf("Mask:%s\n", inet_ntoa(mask)); - if (flags != 0) { + if (interfaceFlags != 0) { printf("%10s", ""); - for (i = 1; i <= flags; i <<= 1) { - if ((flags & i) != 0) { + for (i = 1; i <= interfaceFlags; i <<= 1) { + if ((interfaceFlags & i) != 0) { printf("%s ", psh_ifconfigPrintFlag(i)); } } } /* Get MTU */ - ret = ioctl(sd, SIOCGIFMTU, &ioctlInterface); - if (ret < 0) { - perror("ioctl(SIOCGIFMTU)"); - return ret; + if (ioctl(sd, SIOCGIFMTU, &ioctlInterface) < 0) { + printf("MTU:%s", errMsg); + } + else { + printf("MTU:%d", ioctlInterface.ifr_mtu); } - printf("MTU:%d", ioctlInterface.ifr_mtu); /* Get metric */ - ret = ioctl(sd, SIOCGIFMETRIC, &ioctlInterface); - if (ret < 0) { - perror("ioctl(SIOCGIFMETRIC)"); - return ret; + if (ioctl(sd, SIOCGIFMETRIC, &ioctlInterface) < 0) { + printf(" Metric:%s", errMsg); + } + else { + printf(" Metric:%d", (ioctlInterface.ifr_metric != 0) ? ioctlInterface.ifr_metric : 1); } - printf(" Metric:%d", (ioctlInterface.ifr_metric != 0) ? ioctlInterface.ifr_metric : 1); puts("\n"); - return ret; + return 0; }