📄 interface.c
字号:
if ((slash = strchr(adr, '/')) == NULL) return 0; *slash++ = '\0'; prefix = strtoul(slash, &end, 0); if (*end != '\0') return -1; if (name) { sprintf(name, "/%d", prefix); } mask->sin_family = AF_INET; mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix)); return 1;}#endif /* KEEP_UNUSED */static struct aftype inet_aftype ={ "inet", "DARPA Internet", AF_INET, sizeof(unsigned long), NULL /* UNUSED INET_print */, INET_sprint, NULL /* UNUSED INET_input */, NULL /* UNUSED INET_reserror */, NULL /*INET_rprint */ , NULL /*INET_rinput */ , NULL /* UNUSED INET_getnetmask */, -1, NULL};#endif /* HAVE_AFINET *//* Display an UNSPEC address. */static char *UNSPEC_print(unsigned char *ptr){ static char buff[64]; char *pos; unsigned int i; pos = buff; for (i = 0; i < sizeof(struct sockaddr); i++) { pos += sprintf(pos, "%02X-", (*ptr++ & 0377)); } buff[strlen(buff) - 1] = '\0'; return (buff);}/* Display an UNSPEC socket address. */static char *UNSPEC_sprint(struct sockaddr *sap, int numeric){ static char buf[64]; if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf)); return (UNSPEC_print(sap->sa_data));}static struct aftype unspec_aftype ={ "unspec", "UNSPEC", AF_UNSPEC, 0, UNSPEC_print, UNSPEC_sprint, NULL, NULL, NULL,};static struct aftype *aftypes[] ={#if HAVE_AFUNIX &unix_aftype,#endif#if HAVE_AFINET &inet_aftype,#endif#if HAVE_AFINET6 &inet6_aftype,#endif#if HAVE_AFAX25 &ax25_aftype,#endif#if HAVE_AFNETROM &netrom_aftype,#endif#if HAVE_AFROSE &rose_aftype,#endif#if HAVE_AFIPX &ipx_aftype,#endif#if HAVE_AFATALK &ddp_aftype,#endif#if HAVE_AFECONET &ec_aftype,#endif#if HAVE_AFASH &ash_aftype,#endif#if HAVE_AFX25 &x25_aftype,#endif &unspec_aftype, NULL};#ifdef KEEP_UNUSEDstatic short sVafinit = 0;static void afinit(){ unspec_aftype.title = _("UNSPEC");#if HAVE_AFUNIX unix_aftype.title = _("UNIX Domain");#endif#if HAVE_AFINET inet_aftype.title = _("DARPA Internet");#endif#if HAVE_AFINET6 inet6_aftype.title = _("IPv6");#endif#if HAVE_AFAX25 ax25_aftype.title = _("AMPR AX.25");#endif#if HAVE_AFNETROM netrom_aftype.title = _("AMPR NET/ROM");#endif#if HAVE_AFIPX ipx_aftype.title = _("Novell IPX");#endif#if HAVE_AFATALK ddp_aftype.title = _("Appletalk DDP");#endif#if HAVE_AFECONET ec_aftype.title = _("Econet");#endif#if HAVE_AFX25 x25_aftype.title = _("CCITT X.25");#endif#if HAVE_AFROSE rose_aftype.title = _("AMPR ROSE");#endif#if HAVE_AFASH ash_aftype.title = _("Ash");#endif sVafinit = 1;}static int aftrans_opt(const char *arg){ struct aftrans_t *paft; char *tmp1, *tmp2; char buf[256]; safe_strncpy(buf, arg, sizeof(buf)); tmp1 = buf; while (tmp1) { tmp2 = index(tmp1, ','); if (tmp2) *(tmp2++) = '\0'; paft = aftrans; for (paft = aftrans; paft->alias; paft++) { if (strcmp(tmp1, paft->alias)) continue; if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) { fprintf(stderr, _("Too much address family arguments.\n")); return (0); } if (paft->flag) (*paft->flag)++; if (afname[0]) strcat(afname, ","); strcat(afname, paft->name); break; } if (!paft->alias) { fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1); return (1); } tmp1 = tmp2; } return (0);}/* set the default AF list from the program name or a constant value */static void aftrans_def(char *tool, char *argv0, char *dflt){ char *tmp; char *buf; strcpy(afname, dflt); if (!(tmp = strrchr(argv0, '/'))) tmp = argv0; /* no slash?! */ else tmp++; if (!(buf = strdup(tmp))) return; if (strlen(tool) >= strlen(tmp)) { free(buf); return; } tmp = buf + (strlen(tmp) - strlen(tool)); if (strcmp(tmp, tool) != 0) { free(buf); return; } *tmp = '\0'; if ((tmp = strchr(buf, '_'))) *tmp = '\0'; afname[0] = '\0'; if (aftrans_opt(buf)) strcpy(afname, buf); free(buf);}/* Check our protocol family table for this family. */static struct aftype *get_aftype(const char *name){ struct aftype **afp;#ifdef KEEP_UNUSED if (!sVafinit) afinit();#endif /* KEEP_UNUSED */ afp = aftypes; while (*afp != NULL) { if (!strcmp((*afp)->name, name)) return (*afp); afp++; } if (index(name, ',')) fprintf(stderr, _("Please don't supply more than one address family.\n")); return (NULL);}#endif /* KEEP_UNUSED *//* Check our protocol family table for this family. */static struct aftype *get_afntype(int af){ struct aftype **afp;#ifdef KEEP_UNUSED if (!sVafinit) afinit();#endif /* KEEP_UNUSED */ afp = aftypes; while (*afp != NULL) { if ((*afp)->af == af) return (*afp); afp++; } return (NULL);}/* Check our protocol family table for this family and return its socket */static int get_socket_for_af(int af){ struct aftype **afp;#ifdef KEEP_UNUSED if (!sVafinit) afinit();#endif /* KEEP_UNUSED */ afp = aftypes; while (*afp != NULL) { if ((*afp)->af == af) return (*afp)->fd; afp++; } return -1;}#ifdef KEEP_UNUSED/* type: 0=all, 1=getroute */static void print_aflist(int type) { int count = 0; char * txt; struct aftype **afp;#ifdef KEEP_UNUSED if (!sVafinit) afinit();#endif /* KEEP_UNUSED */ afp = aftypes; while (*afp != NULL) { if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) { afp++; continue; } if ((count % 3) == 0) fprintf(stderr,count?"\n ":" "); txt = (*afp)->name; if (!txt) txt = ".."; fprintf(stderr,"%s (%s) ",txt,_((*afp)->title)); count++; afp++; } fprintf(stderr,"\n");}#endif /* KEEP_UNUSED */struct user_net_device_stats { unsigned long long rx_packets; /* total packets received */ unsigned long long tx_packets; /* total packets transmitted */ unsigned long long rx_bytes; /* total bytes received */ unsigned long long tx_bytes; /* total bytes transmitted */ unsigned long rx_errors; /* bad packets received */ unsigned long tx_errors; /* packet transmit problems */ unsigned long rx_dropped; /* no space in linux buffers */ unsigned long tx_dropped; /* no space available in linux */ unsigned long rx_multicast; /* multicast packets received */ unsigned long rx_compressed; unsigned long tx_compressed; unsigned long collisions; /* detailed rx_errors: */ unsigned long rx_length_errors; unsigned long rx_over_errors; /* receiver ring buff overflow */ unsigned long rx_crc_errors; /* recved pkt with crc error */ unsigned long rx_frame_errors; /* recv'd frame alignment error */ unsigned long rx_fifo_errors; /* recv'r fifo overrun */ unsigned long rx_missed_errors; /* receiver missed packet */ /* detailed tx_errors */ unsigned long tx_aborted_errors; unsigned long tx_carrier_errors; unsigned long tx_fifo_errors; unsigned long tx_heartbeat_errors; unsigned long tx_window_errors;};struct interface { struct interface *next, *prev; char name[IFNAMSIZ]; /* interface name */ short type; /* if type */ short flags; /* various flags */ int metric; /* routing metric */ int mtu; /* MTU value */ int tx_queue_len; /* transmit queue length */ struct ifmap map; /* hardware setup */ struct sockaddr addr; /* IP address */ struct sockaddr dstaddr; /* P-P IP address */ struct sockaddr broadaddr; /* IP broadcast address */ struct sockaddr netmask; /* IP network mask */ struct sockaddr ipxaddr_bb; /* IPX network address */ struct sockaddr ipxaddr_sn; /* IPX network address */ struct sockaddr ipxaddr_e3; /* IPX network address */ struct sockaddr ipxaddr_e2; /* IPX network address */ struct sockaddr ddpaddr; /* Appletalk DDP address */ struct sockaddr ecaddr; /* Econet address */ int has_ip; int has_ipx_bb; int has_ipx_sn; int has_ipx_e3; int has_ipx_e2; int has_ax25; int has_ddp; int has_econet; char hwaddr[32]; /* HW address */ int statistics_valid; struct user_net_device_stats stats; /* statistics */ int keepalive; /* keepalive value for SLIP */ int outfill; /* outfill value for SLIP */};int interface_opt_a = 0; /* show all interfaces */#ifdef KEEP_UNUSEDstatic int opt_i = 0; /* show the statistics */static int opt_v = 0; /* debugging output flag */static int addr_family = 0; /* currently selected AF */#endif /* KEEP_UNUSED */static struct interface *int_list, *int_last;static int skfd = -1; /* generic raw socket desc. */static int sockets_open(int family){ struct aftype **aft; int sfd = -1; static int force = -1; if (force < 0) { force = 0; if (get_kernel_revision() < KRELEASE(2, 1, 0)) force = 1; if (access("/proc/net", R_OK)) force = 1; } for (aft = aftypes; *aft; aft++) { struct aftype *af = *aft; int type = SOCK_DGRAM; if (af->af == AF_UNSPEC) continue; if (family && family != af->af) continue; if (af->fd != -1) { sfd = af->fd; continue; } /* Check some /proc file first to not stress kmod */ if (!family && !force && af->flag_file) { if (access(af->flag_file, R_OK)) continue; }#if HAVE_AFNETROM if (af->af == AF_NETROM) type = SOCK_SEQPACKET;#endif#if HAVE_AFX25 if (af->af == AF_X25) type = SOCK_SEQPACKET;#endif af->fd = socket(af->af, type, 0); if (af->fd >= 0) sfd = af->fd; } if (sfd < 0) fprintf(stderr, _("No usable address families found.\n")); return sfd;}/* like strcmp(), but knows about numbers */static int nstrcmp(const char *astr, const char *b){ const char *a = astr; while (*a == *b) { if (*a == '\0') return 0; a++; b++; } if (isdigit(*a)) { if (!isdigit(*b)) return -1; while (a > astr) { a--; if (!isdigit(*a)) { a++; break; } if (!isdigit(*b)) return -1; b--; } return atoi(a) > atoi(b) ? 1 : -1; } return *a - *b;}static struct interface *add_interface(char *name){ struct interface *ife, **nextp, *new; for (ife = int_last; ife; ife = ife->prev) { int n = nstrcmp(ife->name, name); if (n == 0) return ife; if (n < 0) break; } new(new); safe_strncpy(new->name, name, IFNAMSIZ); nextp = ife ? &ife->next : &int_list; new->prev = ife; new->next = *nextp; if (new->next) new->next->prev = new; else int_last = new; *nextp = new; return new; }static int if_readconf(void){ int numreqs = 30; struct ifconf ifc; struct ifreq *ifr; int n, err = -1; int skfd; /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets (as of 2.1.128) */ skfd = get_socket_for_af(AF_INET); if (skfd < 0) { fprintf(stderr, _("warning: no inet socket available: %s\n"), strerror(errno)); /* Try to soldier on with whatever socket we can get hold of. */ skfd = sockets_open(0); if (skfd < 0) return -1; } ifc.ifc_buf = NULL; for (;;) { ifc.ifc_len = sizeof(struct ifreq) * numreqs; ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len); if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) { perror("SIOCGIFCONF"); goto out; } if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) { /* assume it overflowed and try again */ numreqs += 10; continue; } break; } ifr = ifc.ifc_req;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -