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