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

📄 if.c

📁 This is the demo source code for embedded VxWorks, which include severl functions. The function sour
💻 C
📖 第 1 页 / 共 2 页
字号:
	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {		if (ifa->ifa_addr->sa_family != af)			continue;		ifa_maybe = ifa;		if (ifa->ifa_netmask == 0) {			if (equal(addr, ifa->ifa_addr) ||			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))				return (ifa);			continue;		}		cp = addr->sa_data;		cp2 = ifa->ifa_addr->sa_data;		cp3 = ifa->ifa_netmask->sa_data;		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;		for (; cp3 < cplim; cp3++)			if ((*cp++ ^ *cp2++) & *cp3)				break;		if (cp3 == cplim)			return (ifa);	}	return (ifa_maybe);}/*  * Find an interface address for a specified destination or gateway */struct ifaddr *ifa_ifwithroute(flags, dst, gateway)	int flags;	struct sockaddr	*dst, *gateway;{	register struct ifaddr *ifa;	if ((flags & RTF_GATEWAY) == 0) {		/*		 * If we are adding a route to an interface,		 * and the interface is a pt to pt link		 * we should search for the destination		 * as our clue to the interface.  Otherwise		 * we can use the local address.		 */		ifa = 0;		if (flags & RTF_HOST) 			ifa = ifa_ifwithdstaddr(dst);		if (ifa == 0)			ifa = ifa_ifwithaddr(gateway);	} else {		/*		 * If we are adding a route to a remote net		 * or host, the gateway may still be on the		 * other end of a pt to pt link.		 */		ifa = ifa_ifwithdstaddr(gateway);	}	if (ifa == 0)		ifa = ifa_ifwithnet(gateway);	if (ifa == 0) {		struct rtentry *rt = rtalloc1(dst, 0);		if (rt == 0)			return (0);		rt->rt_refcnt--;		if ((ifa = rt->rt_ifa) == 0)			return (0);	}	if (ifa->ifa_addr->sa_family != dst->sa_family) {		struct ifaddr *oifa = ifa;		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);		if (ifa == 0)			ifa = oifa;	}	return (ifa);}/* free the interface address */voidifafree(ifa)	register struct ifaddr *ifa;{	if (ifa == NULL)		panic("ifafree");	if (ifa->ifa_refcnt == 0)		{		FREE(ifa, MT_IFADDR);  		}	else		ifa->ifa_refcnt--;}/* * Default action when installing a route with a Link Level gateway. * Lookup an appropriate real ifa to point to. * This should be moved to /sys/net/link.c eventually. */voidlink_rtrequest(cmd, rt, sa)	int cmd;	register struct rtentry *rt;	struct sockaddr *sa;{	register struct ifaddr *ifa;	struct sockaddr *dst;	struct ifnet *ifp;	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))		return;	if ((ifa = ifaof_ifpforaddr(dst, ifp))) {		IFAFREE(rt->rt_ifa);		rt->rt_ifa = ifa;		ifa->ifa_refcnt++;		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)			ifa->ifa_rtrequest(cmd, rt, sa);	}}/* * Mark an interface down and notify protocols of * the transition. * NOTE: must be called at splnet or eqivalent. */voidif_down(ifp)	register struct ifnet *ifp;{	register struct ifaddr *ifa;	ifp->if_flags &= ~IFF_UP;	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);	if_qflush(&ifp->if_snd);        if (_rtIfaceMsgHook)             (*_rtIfaceMsgHook) (ifp);}/* * Mark an interface up and notify protocols of * the transition. * NOTE: must be called at splnet or equivalent. */voidif_up(ifp)	register struct ifnet *ifp;{	ifp->if_flags |= IFF_UP;#ifdef notyet        {	register struct ifaddr *ifa;	/* this has no effect on IP, and will kill all ISO connections XXX */	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)		pfctlinput(PRC_IFUP, ifa->ifa_addr);        }#endif        if (_rtIfaceMsgHook)             (*_rtIfaceMsgHook) (ifp);}/* * Flush an interface queue. */voidif_qflush(ifq)	register struct ifqueue *ifq;{	register struct mbuf *m, *n;	n = ifq->ifq_head;	while ((m = n)) {		n = m->m_act;		m_freem(m);	}	ifq->ifq_head = 0;	ifq->ifq_tail = 0;	ifq->ifq_len = 0;}/* * Handle interface watchdog timer routines.  Called * from softclock, we decrement timers (if set) and * call the appropriate interface routine on expiration. */void if_slowtimo (void)    {    register struct ifnet *ifp;    for (ifp = ifnet; ifp; ifp = ifp->if_next)	{	if (ifp->if_timer == 0 || --ifp->if_timer)	    continue;	if (ifp->if_watchdog)	    (*ifp->if_watchdog)(ifp->if_unit);	}    /* XXX using watchdogs good idea? calculate clock rate once only -gae */    wdStart (ifslowtimoWd, sysClkRateGet()/IFNET_SLOWHZ,	    (FUNCPTR) netJobAdd, (int) if_slowtimo);    }/* ifunit () has been moved to ifLib.c *//* * Interface ioctls. */intifioctl(so, cmd, data)	struct socket *so;	u_long cmd;	caddr_t data;{	register struct ifnet *ifp;	register struct ifreq *ifr;	switch (cmd) {	case SIOCGIFCONF:	case OSIOCGIFCONF:		return (ifconf(cmd, data));#ifdef	INET	case SIOCSARP:	case SIOCDARP:		/* FALL THROUGH */	case SIOCGARP:		return (arpioctl(cmd, data));#endif	/* INET */	}	ifr = (struct ifreq *)data;	ifp = ifunit(ifr->ifr_name);	if (ifp == 0)		return (ENXIO);	switch (cmd) {	case SIOCGIFFLAGS:		ifr->ifr_flags = ifp->if_flags;		break;	case SIOCGIFMETRIC:		ifr->ifr_metric = ifp->if_metric;		break;	case SIOCSIFFLAGS:		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {			int s = splimp();			if_down(ifp);			splx(s);                        m2SetIfLastChange(ifp->if_name, ifp->if_unit,			                  ifp->if_flags);		}		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {			int s = splimp();			if_up(ifp);			splx(s);                        m2SetIfLastChange(ifp->if_name, ifp->if_unit,			                  ifp->if_flags);		}		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |			(ifr->ifr_flags &~ IFF_CANTCHANGE);		ifr->ifr_flags = ifp->if_flags;		if (ifp->if_ioctl)			(void) (*ifp->if_ioctl)(ifp, cmd, data);		break;	case SIOCSIFMETRIC:		ifp->if_metric = ifr->ifr_metric;		break;	case SIOCADDMULTI:	case SIOCDELMULTI:		if (ifp->if_ioctl == 0)			return (EOPNOTSUPP);		return ((*ifp->if_ioctl)(ifp, cmd, data));	default:		if (so->so_proto == 0)			return (EOPNOTSUPP);#ifndef COMPAT_43		return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,			cmd, data, ifp));#else	    {                int error = 0;		int ocmd = cmd;		switch (cmd) {		case SIOCSIFDSTADDR:		case SIOCSIFADDR:		case SIOCSIFBRDADDR:		case SIOCSIFNETMASK:#if _BYTE_ORDER != _BIG_ENDIAN			if (ifr->ifr_addr.sa_family == 0 &&			    ifr->ifr_addr.sa_len < 16) {				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;				ifr->ifr_addr.sa_len = 16;			}#else			if (ifr->ifr_addr.sa_len == 0)				ifr->ifr_addr.sa_len = 16;#endif			break;		case OSIOCGIFADDR:			cmd = SIOCGIFADDR;			break;		case OSIOCGIFDSTADDR:			cmd = SIOCGIFDSTADDR;			break;		case OSIOCGIFBRDADDR:			cmd = SIOCGIFBRDADDR;			break;		case OSIOCGIFNETMASK:			cmd = SIOCGIFNETMASK;		}		error =  ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,							    cmd, data, ifp));		switch (ocmd) {		case OSIOCGIFADDR:		case OSIOCGIFDSTADDR:		case OSIOCGIFBRDADDR:		case OSIOCGIFNETMASK:			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;		}		return (error);	    }#endif	}	return (0);}/* * Return interface configuration * of system.  List may be used * in later ioctl's (above) to get * other information. *//*ARGSUSED*/intifconf(cmd, data)	int cmd;	caddr_t data;{	register struct ifconf *ifc = (struct ifconf *)data;	register struct ifnet *ifp = ifnet;	register struct ifaddr *ifa;	register char *cp, *ep;	struct ifreq ifr, *ifrp;	int space = ifc->ifc_len, error = 0;	ifrp = ifc->ifc_req;	ep = ifr.ifr_name + sizeof (ifr.ifr_name) - 2;	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) {		strncpy(ifr.ifr_name, ifp->if_name, sizeof(ifr.ifr_name) - 2);		for (cp = ifr.ifr_name; cp < ep && *cp; cp++)			continue;		*cp++ = '0' + ifp->if_unit; *cp = '\0';		if ((ifa = ifp->if_addrlist) == 0) {			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,			    sizeof(ifr));			if (error)				break;			space -= sizeof (ifr), ifrp++;		} else 		    for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) {			register struct sockaddr *sa = ifa->ifa_addr;#ifdef COMPAT_43			if (cmd == OSIOCGIFCONF) {				struct osockaddr *osa =					 (struct osockaddr *)&ifr.ifr_addr;				ifr.ifr_addr = *sa;				osa->sa_family = sa->sa_family;				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,						sizeof (ifr));				ifrp++;			} else#endif			if (sa->sa_len <= sizeof(*sa)) {				ifr.ifr_addr = *sa;				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,						sizeof (ifr));				ifrp++;			} else {				space -= sa->sa_len - sizeof(*sa);				if (space < sizeof (ifr))					break;				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,						sizeof (ifr.ifr_name));				if (error == 0)				    error = copyout((caddr_t)sa,				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);				ifrp = (struct ifreq *)					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);			}			if (error)				break;			space -= sizeof (ifr);		}	}	ifc->ifc_len -= space;	return (error);}/* * Set/clear promiscuous mode on interface ifp based on the truth value * of pswitch.  The calls are reference counted so that only the first * "on" request actually has an effect, as does the final "off" request. * Results are undefined if the "off" and "on" requests are not matched. */intifpromisc(ifp, pswitch)	struct ifnet *ifp;	int pswitch;{	struct ifreq ifr;	/*	 * If the device is not configured up, we cannot put it in	 * promiscuous mode.	 */	if ((ifp->if_flags & IFF_UP) == 0)		return (ENETDOWN);	if (pswitch) {		if (ifp->if_pcount++ != 0)			return (0);		ifp->if_flags |= IFF_PROMISC;	} else {		if (--ifp->if_pcount > 0)			return (0);		ifp->if_flags &= ~IFF_PROMISC;	}	ifr.ifr_flags = ifp->if_flags;	return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));}static char *sprint_d(n, buf, buflen)	u_int n;	char *buf;	int buflen;{	register char *cp = buf + buflen - 1;	*cp = 0;	do {		cp--;		*cp = "0123456789"[n % 10];		n /= 10;	} while (n != 0);	return (cp);}

⌨️ 快捷键说明

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