xref: /csrg-svn/sbin/XNSrouted/if.c (revision 35591)
1*35591Sbostic /*
2*35591Sbostic  * Copyright (c) 1983 The Regents of the University of California.
3*35591Sbostic  * All rights reserved.
4*35591Sbostic  *
5*35591Sbostic  * Redistribution and use in source and binary forms are permitted
6*35591Sbostic  * provided that the above copyright notice and this paragraph are
7*35591Sbostic  * duplicated in all such forms and that any documentation,
8*35591Sbostic  * advertising materials, and other materials related to such
9*35591Sbostic  * distribution and use acknowledge that the software was developed
10*35591Sbostic  * by the University of California, Berkeley.  The name of the
11*35591Sbostic  * University may not be used to endorse or promote products derived
12*35591Sbostic  * from this software without specific prior written permission.
13*35591Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35591Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35591Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*35591Sbostic  *
17*35591Sbostic  * static char sccsid[] = "@(#)if.c	5.1 (Berkeley) 6/4/85"; (routed/if.c)
18*35591Sbostic  */
19*35591Sbostic 
20*35591Sbostic #ifndef lint
21*35591Sbostic static char sccsid[] = "@(#)if.c	5.1 (Berkeley) 09/20/88";
22*35591Sbostic #endif /* not lint */
23*35591Sbostic 
24*35591Sbostic /*
25*35591Sbostic  * Routing Table Management Daemon
26*35591Sbostic  */
27*35591Sbostic #include "defs.h"
28*35591Sbostic 
29*35591Sbostic extern	struct interface *ifnet;
30*35591Sbostic 
31*35591Sbostic /*
32*35591Sbostic  * Find the interface with address addr.
33*35591Sbostic  */
34*35591Sbostic struct interface *
35*35591Sbostic if_ifwithaddr(addr)
36*35591Sbostic 	struct sockaddr *addr;
37*35591Sbostic {
38*35591Sbostic 	register struct interface *ifp;
39*35591Sbostic 
40*35591Sbostic #define	same(a1, a2) \
41*35591Sbostic 	(bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
42*35591Sbostic 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
43*35591Sbostic 		if (ifp->int_flags & IFF_REMOTE)
44*35591Sbostic 			continue;
45*35591Sbostic 		if (ifp->int_addr.sa_family != addr->sa_family)
46*35591Sbostic 			continue;
47*35591Sbostic 		if (same(&ifp->int_addr, addr))
48*35591Sbostic 			break;
49*35591Sbostic 		if ((ifp->int_flags & IFF_BROADCAST) &&
50*35591Sbostic 		    same(&ifp->int_broadaddr, addr))
51*35591Sbostic 			break;
52*35591Sbostic 	}
53*35591Sbostic 	return (ifp);
54*35591Sbostic }
55*35591Sbostic 
56*35591Sbostic /*
57*35591Sbostic  * Find the point-to-point interface with destination address addr.
58*35591Sbostic  */
59*35591Sbostic struct interface *
60*35591Sbostic if_ifwithdstaddr(addr)
61*35591Sbostic 	struct sockaddr *addr;
62*35591Sbostic {
63*35591Sbostic 	register struct interface *ifp;
64*35591Sbostic 
65*35591Sbostic 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
66*35591Sbostic 		if ((ifp->int_flags & IFF_POINTOPOINT) == 0)
67*35591Sbostic 			continue;
68*35591Sbostic 		if (same(&ifp->int_dstaddr, addr))
69*35591Sbostic 			break;
70*35591Sbostic 	}
71*35591Sbostic 	return (ifp);
72*35591Sbostic }
73*35591Sbostic 
74*35591Sbostic /*
75*35591Sbostic  * Find the interface on the network
76*35591Sbostic  * of the specified address.
77*35591Sbostic  */
78*35591Sbostic struct interface *
79*35591Sbostic if_ifwithnet(addr)
80*35591Sbostic 	register struct sockaddr *addr;
81*35591Sbostic {
82*35591Sbostic 	register struct interface *ifp;
83*35591Sbostic 	register int af = addr->sa_family;
84*35591Sbostic 	register int (*netmatch)();
85*35591Sbostic 
86*35591Sbostic 	if (af >= AF_MAX)
87*35591Sbostic 		return (0);
88*35591Sbostic 	netmatch = afswitch[af].af_netmatch;
89*35591Sbostic 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
90*35591Sbostic 		if (ifp->int_flags & IFF_REMOTE)
91*35591Sbostic 			continue;
92*35591Sbostic 		if (af != ifp->int_addr.sa_family)
93*35591Sbostic 			continue;
94*35591Sbostic 		if ((*netmatch)(addr, &ifp->int_addr))
95*35591Sbostic 			break;
96*35591Sbostic 	}
97*35591Sbostic 	return (ifp);
98*35591Sbostic }
99*35591Sbostic 
100*35591Sbostic /*
101*35591Sbostic  * Find an interface from which the specified address
102*35591Sbostic  * should have come from.  Used for figuring out which
103*35591Sbostic  * interface a packet came in on -- for tracing.
104*35591Sbostic  */
105*35591Sbostic struct interface *
106*35591Sbostic if_iflookup(addr)
107*35591Sbostic 	struct sockaddr *addr;
108*35591Sbostic {
109*35591Sbostic 	register struct interface *ifp, *maybe;
110*35591Sbostic 	register int af = addr->sa_family;
111*35591Sbostic 	register int (*netmatch)();
112*35591Sbostic 
113*35591Sbostic 	if (af >= AF_MAX)
114*35591Sbostic 		return (0);
115*35591Sbostic 	maybe = 0;
116*35591Sbostic 	netmatch = afswitch[af].af_netmatch;
117*35591Sbostic 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
118*35591Sbostic 		if (ifp->int_addr.sa_family != af)
119*35591Sbostic 			continue;
120*35591Sbostic 		if (same(&ifp->int_addr, addr))
121*35591Sbostic 			break;
122*35591Sbostic 		if ((ifp->int_flags & IFF_BROADCAST) &&
123*35591Sbostic 		    same(&ifp->int_broadaddr, addr))
124*35591Sbostic 			break;
125*35591Sbostic 		if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr))
126*35591Sbostic 			maybe = ifp;
127*35591Sbostic 	}
128*35591Sbostic 	if (ifp == 0)
129*35591Sbostic 		ifp = maybe;
130*35591Sbostic 	return (ifp);
131*35591Sbostic }
132