⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interface.c

📁 ifconfig源码
💻 C
📖 第 1 页 / 共 4 页
字号:
    return -1;}#endif /* KEEP_UNUSED */static struct hwtype ppp_hwtype ={    "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,    NULL, NULL, NULL /* UNUSED do_ppp */, 0};#endif				/* HAVE_PPP */static struct hwtype *hwtypes[] ={    &loop_hwtype,#if HAVE_HWSLIP    &slip_hwtype,    &cslip_hwtype,    &slip6_hwtype,    &cslip6_hwtype,    &adaptive_hwtype,#endif#if HAVE_HWSTRIP    &strip_hwtype,#endif#if HAVE_HWASH    &ash_hwtype,#endif#if HAVE_HWETHER    &ether_hwtype,#endif#if HAVE_HWTR    &tr_hwtype,#ifdef ARPHRD_IEEE802_TR    &tr_hwtype1, #endif#endif#if HAVE_HWAX25    &ax25_hwtype,#endif#if HAVE_HWNETROM    &netrom_hwtype,#endif#if HAVE_HWROSE    &rose_hwtype,#endif#if HAVE_HWTUNNEL    &tunnel_hwtype,#endif#if HAVE_HWPPP    &ppp_hwtype,#endif#if HAVE_HWHDLCLAPB    &hdlc_hwtype,    &lapb_hwtype,#endif#if HAVE_HWARC    &arcnet_hwtype,#endif#if HAVE_HWFR    &dlci_hwtype,    &frad_hwtype,#endif#if HAVE_HWSIT    &sit_hwtype,#endif#if HAVE_HWFDDI    &fddi_hwtype,#endif#if HAVE_HWHIPPI    &hippi_hwtype,#endif#if HAVE_HWIRDA    &irda_hwtype,#endif#if HAVE_HWEC    &ec_hwtype,#endif#if HAVE_HWX25    &x25_hwtype,#endif    &unspec_hwtype,    NULL};#ifdef KEEP_UNUSEDstatic short sVhwinit = 0;static void hwinit(){    loop_hwtype.title = _("Local Loopback");    unspec_hwtype.title = _("UNSPEC");#if HAVE_HWSLIP    slip_hwtype.title = _("Serial Line IP");    cslip_hwtype.title = _("VJ Serial Line IP");    slip6_hwtype.title = _("6-bit Serial Line IP");    cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");    adaptive_hwtype.title = _("Adaptive Serial Line IP");#endif#if HAVE_HWETHER    ether_hwtype.title = _("Ethernet");#endif#if HAVE_HWASH    ash_hwtype.title = _("Ash");#endif#if HAVE_HWFDDI    fddi_hwtype.title = _("Fiber Distributed Data Interface");#endif#if HAVE_HWHIPPI    hippi_hwtype.title = _("HIPPI");#endif#if HAVE_HWAX25    ax25_hwtype.title = _("AMPR AX.25");#endif#if HAVE_HWROSE    rose_hwtype.title = _("AMPR ROSE");#endif#if HAVE_HWNETROM    netrom_hwtype.title = _("AMPR NET/ROM");#endif#if HAVE_HWX25    x25_hwtype.title = _("generic X.25");#endif#if HAVE_HWTUNNEL    tunnel_hwtype.title = _("IPIP Tunnel");#endif#if HAVE_HWPPP    ppp_hwtype.title = _("Point-to-Point Protocol");#endif#if HAVE_HWHDLCLAPB    hdlc_hwtype.title = _("(Cisco)-HDLC");    lapb_hwtype.title = _("LAPB");#endif#if HAVE_HWARC    arcnet_hwtype.title = _("ARCnet");#endif#if HAVE_HWFR    dlci_hwtype.title = _("Frame Relay DLCI");    frad_hwtype.title = _("Frame Relay Access Device");#endif#if HAVE_HWSIT    sit_hwtype.title = _("IPv6-in-IPv4");#endif#if HAVE_HWIRDA    irda_hwtype.title = _("IrLAP");#endif#if HAVE_HWTR    tr_hwtype.title = _("16/4 Mbps Token Ring");#ifdef ARPHRD_IEEE802_TR    tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ; #endif#endif#if HAVE_HWEC    ec_hwtype.title = _("Econet");#endif    sVhwinit = 1;}#endif /* KEEP_UNUSED */#ifdef IFF_PORTSELstatic const char *if_port_text[][4] ={    /* Keep in step with <linux/netdevice.h> */    {"unknown", NULL, NULL, NULL},    {"10base2", "bnc", "coax", NULL},    {"10baseT", "utp", "tpe", NULL},    {"AUI", "thick", "db15", NULL},    {"100baseT", NULL, NULL, NULL},    {"100baseTX", NULL, NULL, NULL},    {"100baseFX", NULL, NULL, NULL},    {NULL, NULL, NULL, NULL},};#endif/* Check our hardware type table for this type. */static struct hwtype *get_hwntype(int type){    struct hwtype **hwp;#ifdef KEEP_UNUSED    if (!sVhwinit)	hwinit();#endif /* KEEP_UNUSED */    hwp = hwtypes;    while (*hwp != NULL) {	if ((*hwp)->type == type)	    return (*hwp);	hwp++;    }    return (NULL);}/* return 1 if address is all zeros */static int hw_null_address(struct hwtype *hw, void *ap){    unsigned int i;    unsigned char *address = (unsigned char *)ap;    for (i = 0; i < hw->alen; i++)	if (address[i])	    return 0;    return 1;}static const char TRext[] = "\0\0k\0M";static void print_bytes_scaled(unsigned long long ull, const char *end){	unsigned long long int_part;	unsigned long frac_part;	const char *ext;	int i;	frac_part = 0;	ext = TRext;	int_part = ull;	for (i=0 ; i<2 ; i++) {		if (int_part >= 1024) {			frac_part = ((int_part % 1024) * 10) / 1024;			int_part /= 1024;			ext += 2;			/* Kb, Mb */		}	}	printf("X bytes:%Lu (%Lu.%lu %sb)%s", ull, int_part, frac_part, ext, end);}static void ife_print(struct interface *ptr){    struct aftype *ap;    struct hwtype *hw;    int hf;    int can_compress = 0;#if HAVE_AFIPX    static struct aftype *ipxtype = NULL;#endif#if HAVE_AFECONET    static struct aftype *ectype = NULL;#endif#if HAVE_AFATALK    static struct aftype *ddptype = NULL;#endif#if HAVE_AFINET6    FILE *f;    char addr6[40], devname[20];    struct sockaddr_in6 sap;    int plen, scope, dad_status, if_idx;    extern struct aftype inet6_aftype;    char addr6p[8][5];#endif    ap = get_afntype(ptr->addr.sa_family);    if (ap == NULL)	ap = get_afntype(0);    hf = ptr->type;    if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)	can_compress = 1;    hw = get_hwntype(hf);    if (hw == NULL)	hw = get_hwntype(-1);    printf(_("%-9.9s Link encap:%s  "), ptr->name, _(hw->title));    /* For some hardware types (eg Ash, ATM) we don't print the        hardware address if it's null.  */    if (hw->print != NULL && (! (hw_null_address(hw, ptr->hwaddr) &&				  hw->suppress_null_addr)))	printf(_("HWaddr %s  "), hw->print(ptr->hwaddr));#ifdef IFF_PORTSEL    if (ptr->flags & IFF_PORTSEL) {	printf(_("Media:%s"), if_port_text[ptr->map.port][0]);	if (ptr->flags & IFF_AUTOMEDIA)	    printf(_("(auto)"));    }#endif    printf("\n");#if HAVE_AFINET    if (ptr->has_ip) {	printf(_("          %s addr:%s "), ap->name,	       ap->sprint(&ptr->addr, 1));	if (ptr->flags & IFF_POINTOPOINT) {	    printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));	}	if (ptr->flags & IFF_BROADCAST) {	    printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));	}	printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));    }#endif#if HAVE_AFINET6    if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {	while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",		      addr6p[0], addr6p[1], addr6p[2], addr6p[3],		      addr6p[4], addr6p[5], addr6p[6], addr6p[7],		  &if_idx, &plen, &scope, &dad_status, devname) != EOF) {	    if (!strcmp(devname, ptr->name)) {		sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",			addr6p[0], addr6p[1], addr6p[2], addr6p[3],			addr6p[4], addr6p[5], addr6p[6], addr6p[7]);		inet6_aftype.input(1, addr6, (struct sockaddr *) &sap);		printf(_("          inet6 addr: %s/%d"),		 inet6_aftype.sprint((struct sockaddr *) &sap, 1), plen);		printf(_(" Scope:"));		switch (scope) {		case 0:		    printf(_("Global"));		    break;		case IPV6_ADDR_LINKLOCAL:		    printf(_("Link"));		    break;		case IPV6_ADDR_SITELOCAL:		    printf(_("Site"));		    break;		case IPV6_ADDR_COMPATv4:		    printf(_("Compat"));		    break;		case IPV6_ADDR_LOOPBACK:		    printf(_("Host"));		    break;		default:		    printf(_("Unknown"));		}		printf("\n");	    }	}	fclose(f);    }#endif#if HAVE_AFIPX    if (ipxtype == NULL)	ipxtype = get_afntype(AF_IPX);    if (ipxtype != NULL) {	if (ptr->has_ipx_bb)	    printf(_("          IPX/Ethernet II addr:%s\n"),		   ipxtype->sprint(&ptr->ipxaddr_bb, 1));	if (ptr->has_ipx_sn)	    printf(_("          IPX/Ethernet SNAP addr:%s\n"),		   ipxtype->sprint(&ptr->ipxaddr_sn, 1));	if (ptr->has_ipx_e2)	    printf(_("          IPX/Ethernet 802.2 addr:%s\n"),		   ipxtype->sprint(&ptr->ipxaddr_e2, 1));	if (ptr->has_ipx_e3)	    printf(_("          IPX/Ethernet 802.3 addr:%s\n"),		   ipxtype->sprint(&ptr->ipxaddr_e3, 1));    }#endif#if HAVE_AFATALK    if (ddptype == NULL)	ddptype = get_afntype(AF_APPLETALK);    if (ddptype != NULL) {	if (ptr->has_ddp)	    printf(_("          EtherTalk Phase 2 addr:%s\n"), ddptype->sprint(&ptr->ddpaddr, 1));    }#endif#if HAVE_AFECONET    if (ectype == NULL)	ectype = get_afntype(AF_ECONET);    if (ectype != NULL) {	if (ptr->has_econet)	    printf(_("          econet addr:%s\n"), ectype->sprint(&ptr->ecaddr, 1));    }#endif    printf("          ");    /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */    if (ptr->flags == 0)	printf(_("[NO FLAGS] "));    if (ptr->flags & IFF_UP)	printf(_("UP "));    if (ptr->flags & IFF_BROADCAST)	printf(_("BROADCAST "));    if (ptr->flags & IFF_DEBUG)	printf(_("DEBUG "));    if (ptr->flags & IFF_LOOPBACK)	printf(_("LOOPBACK "));    if (ptr->flags & IFF_POINTOPOINT)	printf(_("POINTOPOINT "));    if (ptr->flags & IFF_NOTRAILERS)	printf(_("NOTRAILERS "));    if (ptr->flags & IFF_RUNNING)	printf(_("RUNNING "));    if (ptr->flags & IFF_NOARP)	printf(_("NOARP "));    if (ptr->flags & IFF_PROMISC)	printf(_("PROMISC "));    if (ptr->flags & IFF_ALLMULTI)	printf(_("ALLMULTI "));    if (ptr->flags & IFF_SLAVE)	printf(_("SLAVE "));    if (ptr->flags & IFF_MASTER)	printf(_("MASTER "));    if (ptr->flags & IFF_MULTICAST)	printf(_("MULTICAST "));#ifdef HAVE_DYNAMIC    if (ptr->flags & IFF_DYNAMIC)	printf(_("DYNAMIC "));#endif    /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */    printf(_(" MTU:%d  Metric:%d"),	   ptr->mtu, ptr->metric ? ptr->metric : 1);#ifdef SIOCSKEEPALIVE    if (ptr->outfill || ptr->keepalive)	printf(_("  Outfill:%d  Keepalive:%d"),	       ptr->outfill, ptr->keepalive);#endif    printf("\n");    /* If needed, display the interface statistics. */    if (ptr->statistics_valid) {	printf("          ");	printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),	       ptr->stats.rx_packets, ptr->stats.rx_errors,	       ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,	       ptr->stats.rx_frame_errors);	if (can_compress)	    printf(_("             compressed:%lu\n"), ptr->stats.rx_compressed);	printf("          ");	printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),	       ptr->stats.tx_packets, ptr->stats.tx_errors,	       ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,	       ptr->stats.tx_carrier_errors);	printf(_("          collisions:%lu "), ptr->stats.collisions);	if (can_compress)	    printf(_("compressed:%lu "), ptr->stats.tx_compressed);	if (ptr->tx_queue_len != -1)	    printf(_("txqueuelen:%d "), ptr->tx_queue_len);	printf("\n          R");	print_bytes_scaled(ptr->stats.rx_bytes, "  T");	print_bytes_scaled(ptr->stats.tx_bytes, "\n");    }    if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||	 ptr->map.base_addr)) {	printf("          ");	if (ptr->map.irq)	    printf(_("Interrupt:%d "), ptr->map.irq);	if (ptr->map.base_addr >= 0x100)	/* Only print devices using it for 						   I/O maps */	    printf(_("Base address:0x%x "), ptr->map.base_addr);	if (ptr->map.mem_start) {	    printf(_("Memory:%lx-%lx "), ptr->map.mem_start, ptr->map.mem_end);	}	if (ptr->map.dma)	    printf(_("DMA chan:%x "), ptr->map.dma);	printf("\n");    }    printf("\n");}static int do_if_print(struct interface *ife, void *cookie){    int *opt_a = (int *) cookie;    int res;     res = do_if_fetch(ife);     if (res >= 0) {   	if ((ife->flags & IFF_UP) || *opt_a)	    ife_print(ife);    }    return res;}static struct interface *lookup_interface(char *name){    struct interface *ife = NULL;    if (if_readlist_proc(name) < 0) 	    return NULL;     ife = add_interface(name);     return ife;}/* for ipv4 add/del modes */static int if_print(char *ifname){    int res;    if (!ifname) {	res = for_all_interfaces(do_if_print, &interface_opt_a);    } else {	struct interface *ife;	ife = lookup_interface(ifname);	res = do_if_fetch(ife); 	if (res >= 0) 	    ife_print(ife);    }    return res; }int display_interfaces(char *ifname){    int status;    /* Create a channel to the NET kernel. */    if ((skfd = sockets_open(0)) < 0) {		perror_msg_and_die("socket");    }    /* Do we have to show the current setup? */    status = if_print(ifname);    close(skfd);    exit(status < 0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -