xref: /original-bsd/sbin/routed/if.c (revision f72a343a)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)if.c	5.3 (Berkeley) 04/20/86";
9 #endif not lint
10 
11 /*
12  * Routing Table Management Daemon
13  */
14 #include "defs.h"
15 
16 extern	struct interface *ifnet;
17 
18 /*
19  * Find the interface with address addr.
20  */
21 struct interface *
22 if_ifwithaddr(addr)
23 	struct sockaddr *addr;
24 {
25 	register struct interface *ifp;
26 
27 #define	same(a1, a2) \
28 	(bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
29 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
30 		if (ifp->int_flags & IFF_REMOTE)
31 			continue;
32 		if (ifp->int_addr.sa_family != addr->sa_family)
33 			continue;
34 		if (same(&ifp->int_addr, addr))
35 			break;
36 		if ((ifp->int_flags & IFF_BROADCAST) &&
37 		    same(&ifp->int_broadaddr, addr))
38 			break;
39 	}
40 	return (ifp);
41 }
42 
43 /*
44  * Find the point-to-point interface with destination address addr.
45  */
46 struct interface *
47 if_ifwithdstaddr(addr)
48 	struct sockaddr *addr;
49 {
50 	register struct interface *ifp;
51 
52 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
53 		if ((ifp->int_flags & IFF_POINTOPOINT) == 0)
54 			continue;
55 		if (same(&ifp->int_dstaddr, addr))
56 			break;
57 	}
58 	return (ifp);
59 }
60 
61 /*
62  * Find the interface on the network
63  * of the specified address.
64  */
65 struct interface *
66 if_ifwithnet(addr)
67 	register struct sockaddr *addr;
68 {
69 	register struct interface *ifp;
70 	register int af = addr->sa_family;
71 	register int (*netmatch)();
72 
73 	if (af >= af_max)
74 		return (0);
75 	netmatch = afswitch[af].af_netmatch;
76 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
77 		if (ifp->int_flags & IFF_REMOTE)
78 			continue;
79 		if (af != ifp->int_addr.sa_family)
80 			continue;
81 		if ((*netmatch)(addr, &ifp->int_addr))
82 			break;
83 	}
84 	return (ifp);
85 }
86 
87 /*
88  * Find an interface from which the specified address
89  * should have come from.  Used for figuring out which
90  * interface a packet came in on -- for tracing.
91  */
92 struct interface *
93 if_iflookup(addr)
94 	struct sockaddr *addr;
95 {
96 	register struct interface *ifp, *maybe;
97 	register int af = addr->sa_family;
98 	register int (*netmatch)();
99 
100 	if (af >= af_max)
101 		return (0);
102 	maybe = 0;
103 	netmatch = afswitch[af].af_netmatch;
104 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
105 		if (ifp->int_addr.sa_family != af)
106 			continue;
107 		if (same(&ifp->int_addr, addr))
108 			break;
109 		if ((ifp->int_flags & IFF_BROADCAST) &&
110 		    same(&ifp->int_broadaddr, addr))
111 			break;
112 		if ((ifp->int_flags & IFF_POINTOPOINT) &&
113 		    same(&ifp->int_dstaddr, addr))
114 			break;
115 		if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr))
116 			maybe = ifp;
117 	}
118 	if (ifp == 0)
119 		ifp = maybe;
120 	return (ifp);
121 }
122