if_nametoindex.c

来自「unix network ,unix network unix networ」· C语言 代码 · 共 52 行

C
52
字号
/* include if_nametoindex */#include	"unpifi.h"#include	"unproute.h"unsigned intif_nametoindex(const char *name){	unsigned int		idx, namelen;	char				*buf, *next, *lim;	size_t				len;	struct if_msghdr	*ifm;	struct sockaddr		*sa, *rti_info[RTAX_MAX];	struct sockaddr_dl	*sdl;	if ( (buf = net_rt_iflist(0, 0, &len)) == NULL)		return(0);	namelen = strlen(name);	lim = buf + len;	for (next = buf; next < lim; next += ifm->ifm_msglen) {		ifm = (struct if_msghdr *) next;		if (ifm->ifm_type == RTM_IFINFO) {			sa = (struct sockaddr *) (ifm + 1);			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);			if ( (sa = rti_info[RTAX_IFP]) != NULL) {				if (sa->sa_family == AF_LINK) {					sdl = (struct sockaddr_dl *) sa;					if (sdl->sdl_nlen == namelen && strncmp(&sdl->sdl_data[0], name, sdl->sdl_nlen) == 0) {						idx = sdl->sdl_index;	/* save before free() */						free(buf);						return(idx);					}				}			}		}	}	free(buf);	return(0);		/* no match for name */}/* end if_nametoindex */unsigned intIf_nametoindex(const char *name){	int		idx;	if ( (idx = if_nametoindex(name)) == 0)		err_quit("if_nametoindex error for %s", name);	return(idx);}

⌨️ 快捷键说明

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