/* * Construct an Internet address representation. * If the nflag has been supplied, give * numeric value, otherwise try for symbolic name. */ static inline void inetname(struct sockaddr_in *from) { char *cp; static char domain[MAXHOSTNAMELEN + 1]; char name[MAXHOSTNAMELEN + 1]; static int first = 1; const char *ina; if (first && !nflag) { first = 0; if (getdomainname(domain, MAXHOSTNAMELEN) != 0) domain[0] = 0; } cp = 0; if (!nflag && from->sin_addr.s_addr != INADDR_ANY) { if(INET_rresolve(name, sizeof(name), from, 0x4000, 0xffffffff) >= 0) { if ((cp = strchr(name, '.')) && !strcmp(cp + 1, domain)) *cp = 0; cp = (char *)name; } } ina = inet_ntoa(from->sin_addr); if (nflag) printf(" %s", ina); else printf(" %s (%s)", (cp ? cp : ina), ina); }
/* Display an Internet socket address. */ static char * INET_sprint(struct sockaddr *sap, int numeric) { static char buff[128]; if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return(NLS_CATBUFF (catfd, inetSet, inet_none, "[NONE SET]", buff, 128)); if (INET_rresolve(buff, (struct sockaddr_in *) sap, numeric) != 0) return(NULL); return(buff); }
/* Display an Internet socket address. */ static char *INET_sprint(struct sockaddr *sap, int numeric) { static char buff[128]; if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); if (INET_rresolve(buff, (struct sockaddr_in *) sap, numeric, 0xffffff00) != 0) return (NULL); return (buff); }
char *INET_sprintmask(const struct sockaddr_storage *sasp, int numeric, unsigned int netmask) { static char buff[128]; if (sasp->ss_family == 0xFFFF || sasp->ss_family == 0) return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); if (INET_rresolve(buff, sizeof(buff), sasp, numeric, netmask) != 0) return (NULL); return (buff); }
static void snprint_ip_port(char *ip_port, int size, struct sockaddr *addr, int port, const char *proto, int numeric) { const char *port_name; #if ENABLE_FEATURE_IPV6 if (addr->sa_family == AF_INET6) { INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr, (numeric & NETSTAT_NUMERIC) ? 0x0fff : 0); } else #endif { INET_rresolve(ip_port, size, (struct sockaddr_in *)addr, 0x4000 | ((numeric & NETSTAT_NUMERIC) ? 0x0fff : 0), 0xffffffff); } port_name = get_sname(htons(port), proto, numeric); if ((strlen(ip_port) + strlen(port_name)) > 22) ip_port[22 - strlen(port_name)] = '\0'; ip_port += strlen(ip_port); strcat(ip_port, ":"); strcat(ip_port, port_name); }
void displayroutes(int noresolve, int netstatfmt) { char devname[64], flags[16], sdest[16], sgw[16]; unsigned long int d, g, m; int flgs, ref, use, metric, mtu, win, ir; struct sockaddr_in s_addr; struct in_addr mask; FILE *fp = bb_xfopen("/proc/net/route", "r"); bb_printf("Kernel IP routing table\n" "Destination Gateway Genmask" " Flags %s Iface\n", netstatfmt ? " MSS Window irtt" : "Metric Ref Use"); if (fscanf(fp, "%*[^\n]\n") < 0) { /* Skip the first line. */ goto ERROR; /* Empty or missing line, or read error. */ } while (1) { int r; r = fscanf(fp, "%63s%lx%lx%X%d%d%d%lx%d%d%d\n", devname, &d, &g, &flgs, &ref, &use, &metric, &m, &mtu, &win, &ir); if (r != 11) { if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */ break; } ERROR: bb_error_msg_and_die("fscanf"); } if (!(flgs & RTF_UP)) { /* Skip interfaces that are down. */ continue; } set_flags(flags, (flgs & IPV4_MASK)); #ifdef RTF_REJECT if (flgs & RTF_REJECT) { flags[0] = '!'; } #endif memset(&s_addr, 0, sizeof(struct sockaddr_in)); s_addr.sin_family = AF_INET; s_addr.sin_addr.s_addr = d; INET_rresolve(sdest, sizeof(sdest), &s_addr, (noresolve | 0x8000), m); /* Default instead of *. */ s_addr.sin_addr.s_addr = g; INET_rresolve(sgw, sizeof(sgw), &s_addr, (noresolve | 0x4000), m); /* Host instead of net. */ mask.s_addr = m; bb_printf("%-16s%-16s%-16s%-6s", sdest, sgw, inet_ntoa(mask), flags); if (netstatfmt) { bb_printf("%5d %-5d %6d %s\n", mtu, win, ir, devname); } else { bb_printf("%-6d %-2d %7d %s\n", metric, ref, use, devname); } } }
void displayroutes(int noresolve, int netstatfmt) { char buff[256]; int nl = 0; struct in_addr dest; struct in_addr gw; struct in_addr mask; int flgs, ref, use, metric, mtu, win, ir; char flags[64]; unsigned long int d, g, m; char sdest[16], sgw[16]; FILE *fp = bb_xfopen("/proc/net/route", "r"); if (noresolve) noresolve = 0x0fff; printf("Kernel IP routing table\n"); printf ("Destination Gateway Genmask Flags %s Iface\n", netstatfmt ? " MSS Window irtt" : "Metric Ref Use"); while (fgets(buff, sizeof(buff), fp) != NULL) { if (nl) { int ifl = 0; int numeric; struct sockaddr_in s_addr; while (buff[ifl] != ' ' && buff[ifl] != '\t' && buff[ifl] != '\0') ifl++; buff[ifl] = 0; /* interface */ if (sscanf(buff + ifl + 1, "%lx%lx%X%d%d%d%lx%d%d%d", &d, &g, &flgs, &ref, &use, &metric, &m, &mtu, &win, &ir) != 10) { bb_error_msg_and_die("Unsuported kernel route format\n"); } ifl = 0; /* parse flags */ if (flgs & RTF_UP) { if (flgs & RTF_REJECT) flags[ifl++] = '!'; else flags[ifl++] = 'U'; if (flgs & RTF_GATEWAY) flags[ifl++] = 'G'; if (flgs & RTF_HOST) flags[ifl++] = 'H'; if (flgs & RTF_REINSTATE) flags[ifl++] = 'R'; if (flgs & RTF_DYNAMIC) flags[ifl++] = 'D'; if (flgs & RTF_MODIFIED) flags[ifl++] = 'M'; flags[ifl] = 0; dest.s_addr = d; gw.s_addr = g; mask.s_addr = m; memset(&s_addr, 0, sizeof(struct sockaddr_in)); s_addr.sin_family = AF_INET; s_addr.sin_addr = dest; numeric = noresolve | 0x8000; /* default instead of * */ INET_rresolve(sdest, sizeof(sdest), &s_addr, numeric, m); numeric = noresolve | 0x4000; /* host instead of net */ s_addr.sin_addr = gw; INET_rresolve(sgw, sizeof(sgw), &s_addr, numeric, m); printf("%-16s%-16s%-16s%-6s", sdest, sgw, inet_ntoa(mask), flags); if (netstatfmt) printf("%5d %-5d %6d %s\n", mtu, win, ir, buff); else printf("%-6d %-2d %7d %s\n", metric, ref, use, buff); } } nl++; } }