xref: /csrg-svn/sbin/routed/if.c (revision 9015)
1*9015Ssam #ifndef lint
2*9015Ssam static char sccsid[] = "@(#)if.c	4.1 11/02/82";
3*9015Ssam #endif
4*9015Ssam 
5*9015Ssam /*
6*9015Ssam  * Routing Table Management Daemon
7*9015Ssam  */
8*9015Ssam #include "router.h"
9*9015Ssam 
10*9015Ssam extern	struct interface *ifnet;
11*9015Ssam 
12*9015Ssam /*
13*9015Ssam  * Find the interface with address add.
14*9015Ssam  */
15*9015Ssam struct interface *
16*9015Ssam if_ifwithaddr(addr)
17*9015Ssam 	struct sockaddr *addr;
18*9015Ssam {
19*9015Ssam 	register struct interface *ifp;
20*9015Ssam 
21*9015Ssam #define	same(a1, a2) \
22*9015Ssam 	(bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
23*9015Ssam 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
24*9015Ssam 		if (ifp->int_flags & IFF_REMOTE)
25*9015Ssam 			continue;
26*9015Ssam 		if (ifp->int_addr.sa_family != addr->sa_family)
27*9015Ssam 			continue;
28*9015Ssam 		if (same(&ifp->int_addr, addr))
29*9015Ssam 			break;
30*9015Ssam 		if ((ifp->int_flags & IFF_BROADCAST) &&
31*9015Ssam 		    same(&ifp->int_broadaddr, addr))
32*9015Ssam 			break;
33*9015Ssam 	}
34*9015Ssam 	return (ifp);
35*9015Ssam }
36*9015Ssam 
37*9015Ssam /*
38*9015Ssam  * Find the interface on the network
39*9015Ssam  * of the specified address.
40*9015Ssam  */
41*9015Ssam struct interface *
42*9015Ssam if_ifwithnet(addr)
43*9015Ssam 	register struct sockaddr *addr;
44*9015Ssam {
45*9015Ssam 	register struct interface *ifp;
46*9015Ssam 	register int af = addr->sa_family;
47*9015Ssam 	register int (*netmatch)();
48*9015Ssam 
49*9015Ssam 	if (af >= AF_MAX)
50*9015Ssam 		return (0);
51*9015Ssam 	netmatch = afswitch[af].af_netmatch;
52*9015Ssam 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
53*9015Ssam 		if (ifp->int_flags & IFF_REMOTE)
54*9015Ssam 			continue;
55*9015Ssam 		if (af != ifp->int_addr.sa_family)
56*9015Ssam 			continue;
57*9015Ssam 		if ((*netmatch)(addr, &ifp->int_addr))
58*9015Ssam 			break;
59*9015Ssam 	}
60*9015Ssam 	return (ifp);
61*9015Ssam }
62*9015Ssam 
63*9015Ssam /*
64*9015Ssam  * Find an interface from which the specified address
65*9015Ssam  * should have come from.  Used for figuring out which
66*9015Ssam  * interface a packet came in on -- for tracing.
67*9015Ssam  */
68*9015Ssam struct interface *
69*9015Ssam if_iflookup(addr)
70*9015Ssam 	struct sockaddr *addr;
71*9015Ssam {
72*9015Ssam 	register struct interface *ifp, *maybe;
73*9015Ssam 	register int af = addr->sa_family;
74*9015Ssam 	register int (*netmatch)();
75*9015Ssam 
76*9015Ssam 	if (af >= AF_MAX)
77*9015Ssam 		return (0);
78*9015Ssam 	maybe = 0;
79*9015Ssam 	netmatch = afswitch[af].af_netmatch;
80*9015Ssam 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
81*9015Ssam 		if (ifp->int_addr.sa_family != af)
82*9015Ssam 			continue;
83*9015Ssam 		if (same(&ifp->int_addr, addr))
84*9015Ssam 			break;
85*9015Ssam 		if ((ifp->int_flags & IFF_BROADCAST) &&
86*9015Ssam 		    same(&ifp->int_broadaddr, addr))
87*9015Ssam 			break;
88*9015Ssam 		if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr))
89*9015Ssam 			maybe = ifp;
90*9015Ssam 	}
91*9015Ssam 	if (ifp == 0)
92*9015Ssam 		ifp = maybe;
93*9015Ssam 	return (ifp);
94*9015Ssam }
95