Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / IPXrouted / if.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright (c) 1995 John Hay.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * static char sccsid[] = "@(#)if.c     5.1 (Berkeley) 6/4/85"; (routed/if.c)
36  *
37  * $FreeBSD: src/usr.sbin/IPXrouted/if.c,v 1.5 1999/08/28 01:15:02 peter Exp $
38  * $DragonFly: src/usr.sbin/IPXrouted/if.c,v 1.2 2003/06/17 04:29:52 dillon Exp $
39  *
40  * @(#)if.c     8.1 (Berkeley) 6/5/93
41  */
42
43 /*
44  * Routing Table Management Daemon
45  */
46 #include "defs.h"
47
48 extern  struct interface *ifnet;
49
50 /*
51  * Find the interface with address addr.
52  */
53 struct interface *
54 if_ifwithaddr(addr)
55         struct sockaddr *addr;
56 {
57         register struct interface *ifp;
58
59 #define same(a1, a2) \
60         (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 10) == 0)
61         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
62                 if (ifp->int_flags & IFF_REMOTE)
63                         continue;
64                 if (ifp->int_addr.sa_family != addr->sa_family)
65                         continue;
66                 if (same(&ifp->int_addr, addr))
67                         break;
68                 if ((ifp->int_flags & IFF_BROADCAST) &&
69                     same(&ifp->int_broadaddr, addr))
70                         break;
71         }
72         return (ifp);
73 }
74
75 /*
76  * Find the point-to-point interface with destination address addr.
77  */
78 struct interface *
79 if_ifwithdstaddr(addr)
80         struct sockaddr *addr;
81 {
82         register struct interface *ifp;
83
84         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
85                 if ((ifp->int_flags & IFF_POINTOPOINT) == 0)
86                         continue;
87                 if (same(&ifp->int_dstaddr, addr))
88                         break;
89         }
90         return (ifp);
91 }
92
93 /*
94  * Find the interface on the network 
95  * of the specified address.
96  */
97 struct interface *
98 if_ifwithnet(addr)
99         register struct sockaddr *addr;
100 {
101         register struct interface *ifp;
102         register int af = addr->sa_family;
103         register int (*netmatch)();
104
105         if (af >= AF_MAX)
106                 return (0);
107         netmatch = afswitch[af].af_netmatch;
108         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
109                 if (ifp->int_flags & IFF_REMOTE)
110                         continue;
111                 if (af != ifp->int_addr.sa_family)
112                         continue;
113                 if ((*netmatch)(addr, &ifp->int_addr))
114                         break;
115         }
116         return (ifp);
117 }
118
119 /*
120  * Find an interface from which the specified address
121  * should have come from.  Used for figuring out which
122  * interface a packet came in on -- for tracing.
123  */
124 struct interface *
125 if_iflookup(addr)
126         struct sockaddr *addr;
127 {
128         register struct interface *ifp, *maybe;
129         register int af = addr->sa_family;
130         register int (*netmatch)();
131
132         if (af >= AF_MAX)
133                 return (0);
134         maybe = 0;
135         netmatch = afswitch[af].af_netmatch;
136         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
137                 if (ifp->int_addr.sa_family != af)
138                         continue;
139                 if (same(&ifp->int_addr, addr))
140                         break;
141                 if ((ifp->int_flags & IFF_BROADCAST) &&
142                     same(&ifp->int_broadaddr, addr))
143                         break;
144                 if (maybe == 0 && (*netmatch)(addr, &ifp->int_addr))
145                         maybe = ifp;
146         }
147         if (ifp == 0)
148                 ifp = maybe;
149         return (ifp);
150 }