xref: /csrg-svn/sbin/routed/if.c (revision 10245)
19015Ssam #ifndef lint
2*10245Ssam static char sccsid[] = "@(#)if.c	4.2 01/11/83";
39015Ssam #endif
49015Ssam 
59015Ssam /*
69015Ssam  * Routing Table Management Daemon
79015Ssam  */
8*10245Ssam #include "defs.h"
99015Ssam 
109015Ssam extern	struct interface *ifnet;
119015Ssam 
129015Ssam /*
139015Ssam  * Find the interface with address add.
149015Ssam  */
159015Ssam struct interface *
169015Ssam if_ifwithaddr(addr)
179015Ssam 	struct sockaddr *addr;
189015Ssam {
199015Ssam 	register struct interface *ifp;
209015Ssam 
219015Ssam #define	same(a1, a2) \
229015Ssam 	(bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
239015Ssam 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
249015Ssam 		if (ifp->int_flags & IFF_REMOTE)
259015Ssam 			continue;
269015Ssam 		if (ifp->int_addr.sa_family != addr->sa_family)
279015Ssam 			continue;
289015Ssam 		if (same(&ifp->int_addr, addr))
299015Ssam 			break;
309015Ssam 		if ((ifp->int_flags & IFF_BROADCAST) &&
319015Ssam 		    same(&ifp->int_broadaddr, addr))
329015Ssam 			break;
339015Ssam 	}
349015Ssam 	return (ifp);
359015Ssam }
369015Ssam 
379015Ssam /*
389015Ssam  * Find the interface on the network
399015Ssam  * of the specified address.
409015Ssam  */
419015Ssam struct interface *
429015Ssam if_ifwithnet(addr)
439015Ssam 	register struct sockaddr *addr;
449015Ssam {
459015Ssam 	register struct interface *ifp;
469015Ssam 	register int af = addr->sa_family;
479015Ssam 	register int (*netmatch)();
489015Ssam 
499015Ssam 	if (af >= AF_MAX)
509015Ssam 		return (0);
519015Ssam 	netmatch = afswitch[af].af_netmatch;
529015Ssam 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
539015Ssam 		if (ifp->int_flags & IFF_REMOTE)
549015Ssam 			continue;
559015Ssam 		if (af != ifp->int_addr.sa_family)
569015Ssam 			continue;
579015Ssam 		if ((*netmatch)(addr, &ifp->int_addr))
589015Ssam 			break;
599015Ssam 	}
609015Ssam 	return (ifp);
619015Ssam }
629015Ssam 
639015Ssam /*
649015Ssam  * Find an interface from which the specified address
659015Ssam  * should have come from.  Used for figuring out which
669015Ssam  * interface a packet came in on -- for tracing.
679015Ssam  */
689015Ssam struct interface *
699015Ssam if_iflookup(addr)
709015Ssam 	struct sockaddr *addr;
719015Ssam {
729015Ssam 	register struct interface *ifp, *maybe;
739015Ssam 	register int af = addr->sa_family;
749015Ssam 	register int (*netmatch)();
759015Ssam 
769015Ssam 	if (af >= AF_MAX)
779015Ssam 		return (0);
789015Ssam 	maybe = 0;
799015Ssam 	netmatch = afswitch[af].af_netmatch;
809015Ssam 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
819015Ssam 		if (ifp->int_addr.sa_family != af)
829015Ssam 			continue;
839015Ssam 		if (same(&ifp->int_addr, addr))
849015Ssam 			break;
859015Ssam 		if ((ifp->int_flags & IFF_BROADCAST) &&
869015Ssam 		    same(&ifp->int_broadaddr, addr))
879015Ssam 			break;
889015Ssam 		if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr))
899015Ssam 			maybe = ifp;
909015Ssam 	}
919015Ssam 	if (ifp == 0)
929015Ssam 		ifp = maybe;
939015Ssam 	return (ifp);
949015Ssam }
95