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