Skip to content

Commit

Permalink
psh/ifconfig: Change form of error reporting in display
Browse files Browse the repository at this point in the history
Program no longer stops, if the ioctl fails due to some function being
unimplemented. Now it displays that feature is "unavailable".

JIRA: RTOS-628
  • Loading branch information
astalke committed Nov 23, 2023
1 parent dde0aae commit d9b9a04
Showing 1 changed file with 83 additions and 52 deletions.
135 changes: 83 additions & 52 deletions psh/ifconfig/ifconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <inttypes.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if_arp.h>
#include <ifaddrs.h>

#include "../psh.h"
Expand Down Expand Up @@ -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;
}


Expand Down

0 comments on commit d9b9a04

Please sign in to comment.