Remove advertising header from all userland binaries.
[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  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * static char sccsid[] = "@(#)if.c     5.1 (Berkeley) 6/4/85"; (routed/if.c)
32  *
33  * $FreeBSD: src/usr.sbin/IPXrouted/if.c,v 1.5 1999/08/28 01:15:02 peter Exp $
34  *
35  * @(#)if.c     8.1 (Berkeley) 6/5/93
36  */
37
38 /*
39  * Routing Table Management Daemon
40  */
41 #include "defs.h"
42
43 extern  struct interface *ifnet;
44
45 /*
46  * Find the interface with address addr.
47  */
48 struct interface *
49 if_ifwithaddr(struct sockaddr *addr)
50 {
51         struct interface *ifp;
52
53 #define same(a1, a2) \
54         (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 10) == 0)
55         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
56                 if (ifp->int_flags & IFF_REMOTE)
57                         continue;
58                 if (ifp->int_addr.sa_family != addr->sa_family)
59                         continue;
60                 if (same(&ifp->int_addr, addr))
61                         break;
62                 if ((ifp->int_flags & IFF_BROADCAST) &&
63                     same(&ifp->int_broadaddr, addr))
64                         break;
65         }
66         return (ifp);
67 }
68
69 /*
70  * Find the point-to-point interface with destination address addr.
71  */
72 struct interface *
73 if_ifwithdstaddr(struct sockaddr *addr)
74 {
75         struct interface *ifp;
76
77         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
78                 if ((ifp->int_flags & IFF_POINTOPOINT) == 0)
79                         continue;
80                 if (same(&ifp->int_dstaddr, addr))
81                         break;
82         }
83         return (ifp);
84 }
85
86 /*
87  * Find the interface on the network 
88  * of the specified address.
89  */
90 struct interface *
91 if_ifwithnet(struct sockaddr *addr)
92 {
93         struct interface *ifp;
94         int af = addr->sa_family;
95         int (*netmatch)(struct sockaddr *, struct sockaddr *);
96
97         if (af >= AF_MAX)
98                 return (0);
99         netmatch = afswitch[af].af_netmatch;
100         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
101                 if (ifp->int_flags & IFF_REMOTE)
102                         continue;
103                 if (af != ifp->int_addr.sa_family)
104                         continue;
105                 if ((*netmatch)(addr, &ifp->int_addr))
106                         break;
107         }
108         return (ifp);
109 }
110
111 /*
112  * Find an interface from which the specified address
113  * should have come from.  Used for figuring out which
114  * interface a packet came in on -- for tracing.
115  */
116 struct interface *
117 if_iflookup(struct sockaddr *addr)
118 {
119         struct interface *ifp, *maybe;
120         int af = addr->sa_family;
121         int (*netmatch)(struct sockaddr *, struct sockaddr *);
122
123         if (af >= AF_MAX)
124                 return (0);
125         maybe = NULL;
126         netmatch = afswitch[af].af_netmatch;
127         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
128                 if (ifp->int_addr.sa_family != af)
129                         continue;
130                 if (same(&ifp->int_addr, addr))
131                         break;
132                 if ((ifp->int_flags & IFF_BROADCAST) &&
133                     same(&ifp->int_broadaddr, addr))
134                         break;
135                 if (maybe == NULL && (*netmatch)(addr, &ifp->int_addr))
136                         maybe = ifp;
137         }
138         if (ifp == NULL)
139                 ifp = maybe;
140         return (ifp);
141 }