Replace MALLOC()/bzero() with MALLOC(M_ZERO).
[dragonfly.git] / usr.sbin / IPXrouted / af.c
blobe02d066755fce908dcc8ebd0fd59e74ca34c8483
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1995 John Hay. All rights reserved.
7 * This file includes significant work done at Cornell University by
8 * Bill Nesheim. That work included by permission.
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.
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.
38 * $FreeBSD: src/usr.sbin/IPXrouted/af.c,v 1.6 1999/08/28 01:15:01 peter Exp $
39 * $DragonFly: src/usr.sbin/IPXrouted/af.c,v 1.4 2004/12/18 22:48:02 swildner Exp $
41 * @(#)af.c 8.1 (Berkeley) 6/5/93
44 #include "defs.h"
47 * Address family support routines
49 af_hash_t null_hash;
50 af_netmatch_t null_netmatch;
51 af_output_t null_output;
52 af_portmatch_t null_portmatch;
53 af_portcheck_t null_portcheck;
54 af_checkhost_t null_checkhost;
55 af_ishost_t null_ishost;
56 af_canon_t null_canon;
58 void ipxnet_hash(struct sockaddr_ipx *, struct afhash *);
59 int ipxnet_netmatch(struct sockaddr_ipx *, struct sockaddr_ipx *);
60 void ipxnet_output(int, int, struct sockaddr_ipx *, int);
61 int ipxnet_portmatch(struct sockaddr_ipx *);
62 int ipxnet_checkhost(struct sockaddr_ipx *);
63 int ipxnet_ishost(struct sockaddr_ipx *);
64 void ipxnet_canon(struct sockaddr_ipx *);
66 #define NIL \
67 { null_hash, null_netmatch, null_output, \
68 null_portmatch, null_portcheck, null_checkhost, \
69 null_ishost, null_canon }
70 #define IPXNET \
71 { (af_hash_t *)ipxnet_hash, \
72 (af_netmatch_t *)ipxnet_netmatch, \
73 (af_output_t *)ipxnet_output, \
74 (af_portmatch_t *)ipxnet_portmatch, \
75 (af_portcheck_t *)ipxnet_portmatch, \
76 (af_checkhost_t *)ipxnet_checkhost, \
77 (af_ishost_t *)ipxnet_ishost, \
78 (af_canon_t *)ipxnet_canon }
80 struct afswitch afswitch[AF_MAX] =
81 { NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL,
82 NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL,
83 NIL, NIL, NIL, IPXNET, NIL, NIL };
85 struct sockaddr_ipx ipxnet_default = { sizeof(struct sockaddr_ipx), AF_IPX };
87 union ipx_net ipx_anynet;
88 union ipx_net ipx_zeronet;
90 void
91 ipxnet_hash(struct sockaddr_ipx *sipx, struct afhash *hp)
93 long hash;
94 #if 0
95 u_short *s = sipx->sipx_addr.x_host.s_host;
96 #endif
97 u_char *c;
99 c = sipx->sipx_addr.x_net.c_net;
101 #define IMVAL 33
102 hash = 0;
103 hash = hash * IMVAL + *c++;
104 hash = hash * IMVAL + *c++;
105 hash = hash * IMVAL + *c++;
106 hash = hash * IMVAL + *c++;
107 #undef IMVAL
109 hp->afh_nethash = hash;
110 hp->afh_nethash ^= (hash >> 8);
111 hp->afh_nethash ^= (hash >> 16);
112 hp->afh_nethash ^= (hash >> 24);
114 #if 0
115 hash = 0;
116 hash = *s++; hash <<= 8; hash += *s++; hash <<= 8; hash += *s;
117 hp->afh_hosthash = hash;
118 #endif
122 ipxnet_netmatch(struct sockaddr_ipx *sxn1, struct sockaddr_ipx *sxn2)
124 return (ipx_neteq(sxn1->sipx_addr, sxn2->sipx_addr));
128 * Verify the message is from the right port.
131 ipxnet_portmatch(struct sockaddr_ipx *sipx)
134 return (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP );
139 * ipx output routine.
141 #ifdef DEBUG
142 int do_output = 0;
143 #endif
144 void
145 ipxnet_output(int s, int flags, struct sockaddr_ipx *sipx, int size)
147 struct sockaddr_ipx dst;
149 dst = *sipx;
150 sipx = &dst;
151 if (sipx->sipx_addr.x_port == 0)
152 sipx->sipx_addr.x_port = htons(IPXPORT_RIP);
153 #ifdef DEBUG
154 if(do_output || ntohs(msg->rip_cmd) == RIPCMD_REQUEST)
155 #endif
157 * Kludge to allow us to get routes out to machines that
158 * don't know their addresses yet; send to that address on
159 * ALL connected nets
161 if (ipx_neteqnn(sipx->sipx_addr.x_net, ipx_zeronet)) {
162 extern struct interface *ifnet;
163 struct interface *ifp;
165 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
166 sipx->sipx_addr.x_net =
167 satoipx_addr(ifp->int_addr).x_net;
168 sendto(s, msg, size, flags,
169 (struct sockaddr *)sipx, sizeof (*sipx));
171 return;
174 sendto(s, msg, size, flags,
175 (struct sockaddr *)sipx, sizeof (*sipx));
179 * Return 1 if we want this route.
180 * We use this to disallow route net G entries for one for multiple
181 * point to point links.
184 ipxnet_checkhost(struct sockaddr_ipx *sipx)
186 struct interface *ifp = if_ifwithnet((struct sockaddr *)sipx);
188 * We want this route if there is no more than one
189 * point to point interface with this network.
191 if (ifp == 0 || (ifp->int_flags & IFF_POINTOPOINT)==0) return (1);
192 return (ifp->int_sq.n == ifp->int_sq.p);
196 * Return 1 if the address is
197 * for a host, 0 for a network.
200 ipxnet_ishost(struct sockaddr_ipx *sipx)
202 u_short *s = sipx->sipx_addr.x_host.s_host;
204 if ((s[0]==0x0000) && (s[1]==0x0000) && (s[2]==0x0000))
205 return (0);
206 if ((s[0]==0xffff) && (s[1]==0xffff) && (s[2]==0xffff))
207 return (0);
209 return (1);
212 void
213 ipxnet_canon(struct sockaddr_ipx *sipx)
216 sipx->sipx_addr.x_port = 0;
219 void
220 null_hash(struct sockaddr *addr, struct afhash *hp)
223 hp->afh_nethash = hp->afh_hosthash = 0;
227 null_netmatch(struct sockaddr *a1, struct sockaddr *a2)
230 return (0);
233 void
234 null_output(int s, int f, struct sockaddr *a1, int n)
241 null_portmatch(struct sockaddr *a1)
244 return (0);
248 null_portcheck(struct sockaddr *a1)
251 return (0);
255 null_ishost(struct sockaddr *a1)
258 return (0);
262 null_checkhost(struct sockaddr *a1)
265 return (0);
268 void
269 null_canon(struct sockaddr *a1)