Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[dragonfly.git] / usr.sbin / IPXrouted / af.c
1 /*
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright (c) 1995 John Hay.  All rights reserved.
6  *
7  * This file includes significant work done at Cornell University by
8  * Bill Nesheim.  That work included by permission.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * $FreeBSD: src/usr.sbin/IPXrouted/af.c,v 1.6 1999/08/28 01:15:01 peter Exp $
39  *
40  * @(#)af.c     8.1 (Berkeley) 6/5/93
41  */
42
43 #include "defs.h"
44
45 /*
46  * Address family support routines
47  */
48 af_hash_t       null_hash;
49 af_netmatch_t   null_netmatch;
50 af_output_t     null_output;
51 af_portmatch_t  null_portmatch;
52 af_portcheck_t  null_portcheck;
53 af_checkhost_t  null_checkhost;
54 af_ishost_t     null_ishost;
55 af_canon_t      null_canon;
56
57 void    ipxnet_hash(struct sockaddr_ipx *, struct afhash *);
58 int     ipxnet_netmatch(struct sockaddr_ipx *, struct sockaddr_ipx *);
59 void    ipxnet_output(int, int, struct sockaddr_ipx *, int);
60 int     ipxnet_portmatch(struct sockaddr_ipx *);
61 int     ipxnet_checkhost(struct sockaddr_ipx *);
62 int     ipxnet_ishost(struct sockaddr_ipx *);
63 void    ipxnet_canon(struct sockaddr_ipx *);
64
65 #define NIL \
66         { null_hash,            null_netmatch,          null_output, \
67           null_portmatch,       null_portcheck,         null_checkhost, \
68           null_ishost,          null_canon }
69 #define IPXNET \
70         { (af_hash_t *)ipxnet_hash, \
71           (af_netmatch_t *)ipxnet_netmatch, \
72           (af_output_t *)ipxnet_output, \
73           (af_portmatch_t *)ipxnet_portmatch, \
74           (af_portcheck_t *)ipxnet_portmatch, \
75           (af_checkhost_t *)ipxnet_checkhost, \
76           (af_ishost_t *)ipxnet_ishost, \
77           (af_canon_t *)ipxnet_canon }
78
79 struct afswitch afswitch[AF_MAX] =
80         { NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL,
81           NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL,
82           NIL, NIL, NIL, IPXNET, NIL, NIL };
83
84 struct sockaddr_ipx ipxnet_default = {
85         .sipx_len = sizeof(struct sockaddr_ipx),
86         .sipx_family = AF_IPX
87 };
88
89 union ipx_net ipx_anynet;
90 union ipx_net ipx_zeronet;
91
92 void
93 ipxnet_hash(struct sockaddr_ipx *sipx, struct afhash *hp)
94 {
95         long hash;
96 #if 0
97         u_short *s = sipx->sipx_addr.x_host.s_host;
98 #endif
99         u_char *c;
100
101         c = sipx->sipx_addr.x_net.c_net;
102
103 #define IMVAL   33
104         hash = 0;
105         hash = hash * IMVAL + *c++;
106         hash = hash * IMVAL + *c++;
107         hash = hash * IMVAL + *c++;
108         hash = hash * IMVAL + *c++;
109 #undef IMVAL
110
111         hp->afh_nethash = hash;
112         hp->afh_nethash ^= (hash >> 8);
113         hp->afh_nethash ^= (hash >> 16);
114         hp->afh_nethash ^= (hash >> 24);
115
116 #if 0
117         hash = 0;
118         hash = *s++; hash <<= 8; hash += *s++; hash <<= 8; hash += *s;
119         hp->afh_hosthash = hash;
120 #endif
121 }
122
123 int
124 ipxnet_netmatch(struct sockaddr_ipx *sxn1, struct sockaddr_ipx *sxn2)
125 {
126         return (ipx_neteq(sxn1->sipx_addr, sxn2->sipx_addr));
127 }
128
129 /*
130  * Verify the message is from the right port.
131  */
132 int
133 ipxnet_portmatch(struct sockaddr_ipx *sipx)
134 {
135         
136         return (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP );
137 }
138
139
140 /*
141  * ipx output routine.
142  */
143 #ifdef DEBUG
144 int do_output = 0;
145 #endif
146 void
147 ipxnet_output(int s, int flags, struct sockaddr_ipx *sipx, int size)
148 {
149         struct sockaddr_ipx dst;
150
151         dst = *sipx;
152         sipx = &dst;
153         if (sipx->sipx_addr.x_port == 0)
154                 sipx->sipx_addr.x_port = htons(IPXPORT_RIP);
155 #ifdef DEBUG
156         if(do_output || ntohs(msg->rip_cmd) == RIPCMD_REQUEST)
157 #endif  
158         /*
159          * Kludge to allow us to get routes out to machines that
160          * don't know their addresses yet; send to that address on
161          * ALL connected nets
162          */
163          if (ipx_neteqnn(sipx->sipx_addr.x_net, ipx_zeronet)) {
164                 extern struct interface *ifnet;
165                 struct interface *ifp;
166                 
167                 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
168                         sipx->sipx_addr.x_net = 
169                                 satoipx_addr(ifp->int_addr).x_net;
170                         sendto(s, msg, size, flags,
171                             (struct sockaddr *)sipx, sizeof (*sipx));
172                 }
173                 return;
174         }
175         
176         sendto(s, msg, size, flags,
177             (struct sockaddr *)sipx, sizeof (*sipx));
178 }
179
180 /*
181  * Return 1 if we want this route.
182  * We use this to disallow route net G entries for one for multiple
183  * point to point links.
184  */
185 int
186 ipxnet_checkhost(struct sockaddr_ipx *sipx)
187 {
188         struct interface *ifp = if_ifwithnet((struct sockaddr *)sipx);
189         /*
190          * We want this route if there is no more than one 
191          * point to point interface with this network.
192          */
193         if (ifp == NULL || (ifp->int_flags & IFF_POINTOPOINT)==0) return (1);
194         return (ifp->int_sq.n == ifp->int_sq.p);
195 }
196
197 /*
198  * Return 1 if the address is
199  * for a host, 0 for a network.
200  */
201 int
202 ipxnet_ishost(struct sockaddr_ipx *sipx)
203 {
204         u_short *s = sipx->sipx_addr.x_host.s_host;
205
206         if ((s[0]==0x0000) && (s[1]==0x0000) && (s[2]==0x0000))
207                 return (0);
208         if ((s[0]==0xffff) && (s[1]==0xffff) && (s[2]==0xffff))
209                 return (0);
210
211         return (1);
212 }
213
214 void
215 ipxnet_canon(struct sockaddr_ipx *sipx)
216 {
217
218         sipx->sipx_addr.x_port = 0;
219 }
220
221 void
222 null_hash(struct sockaddr *addr, struct afhash *hp)
223 {
224
225         hp->afh_nethash = hp->afh_hosthash = 0;
226 }
227
228 int
229 null_netmatch(struct sockaddr *a1, struct sockaddr *a2)
230 {
231
232         return (0);
233 }
234
235 void
236 null_output(int s, int f, struct sockaddr *a1, int n)
237 {
238
239         ;
240 }
241
242 int
243 null_portmatch(struct sockaddr *a1)
244 {
245
246         return (0);
247 }
248
249 int
250 null_portcheck(struct sockaddr *a1)
251 {
252
253         return (0);
254 }
255
256 int
257 null_ishost(struct sockaddr *a1)
258 {
259
260         return (0);
261 }
262
263 int
264 null_checkhost(struct sockaddr *a1)
265 {
266
267         return (0);
268 }
269
270 void
271 null_canon(struct sockaddr *a1)
272 {
273
274         ;
275 }
276