xref: /original-bsd/sbin/XNSrouted/af.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This file includes significant work done at Cornell University by
6  * Bill Nesheim.  That work included by permission.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)af.c	8.1 (Berkeley) 06/05/93";
13 #endif /* not lint */
14 
15 #include "defs.h"
16 
17 /*
18  * Address family support routines
19  */
20 int	null_hash(), null_netmatch(), null_output(),
21 	null_portmatch(), null_portcheck(),
22 	null_checkhost(), null_ishost(), null_canon();
23 int	xnnet_hash(), xnnet_netmatch(), xnnet_output(),
24 	xnnet_portmatch(),
25 	xnnet_checkhost(), xnnet_ishost(), xnnet_canon();
26 #define NIL \
27 	{ null_hash,		null_netmatch,		null_output, \
28 	  null_portmatch,	null_portcheck,		null_checkhost, \
29 	  null_ishost,		null_canon }
30 #define	XNSNET \
31 	{ xnnet_hash,		xnnet_netmatch,		xnnet_output, \
32 	  xnnet_portmatch,	xnnet_portmatch,	xnnet_checkhost, \
33 	  xnnet_ishost,		xnnet_canon }
34 
35 struct afswitch afswitch[AF_MAX] =
36 	{ NIL, NIL, NIL, NIL, NIL, NIL, XNSNET, NIL, NIL, NIL, NIL };
37 
38 struct sockaddr_ns xnnet_default = { sizeof(struct sockaddr_ns), AF_NS };
39 
40 union ns_net ns_anynet;
41 union ns_net ns_zeronet;
42 
43 xnnet_hash(sns, hp)
44 	register struct sockaddr_ns *sns;
45 	struct afhash *hp;
46 {
47 	register long hash = 0;
48 	register u_short *s = sns->sns_addr.x_host.s_host;
49 	union ns_net_u net;
50 
51 	net.net_e = sns->sns_addr.x_net;
52 	hp->afh_nethash = net.long_e;
53 	hash = *s++; hash <<= 8; hash += *s++; hash <<= 8; hash += *s;
54 	hp->afh_hosthash = hash;
55 }
56 
57 xnnet_netmatch(sxn1, sxn2)
58 	struct sockaddr_ns *sxn1, *sxn2;
59 {
60 	return (ns_neteq(sxn1->sns_addr, sxn2->sns_addr));
61 }
62 
63 /*
64  * Verify the message is from the right port.
65  */
66 xnnet_portmatch(sns)
67 	register struct sockaddr_ns *sns;
68 {
69 
70 	return (ntohs(sns->sns_addr.x_port) == IDPPORT_RIF );
71 }
72 
73 
74 /*
75  * xns output routine.
76  */
77 #ifdef DEBUG
78 int do_output = 0;
79 #endif
80 xnnet_output(flags, sns, size)
81 	int flags;
82 	struct sockaddr_ns *sns;
83 	int size;
84 {
85 	struct sockaddr_ns dst;
86 
87 	dst = *sns;
88 	sns = &dst;
89 	if (sns->sns_addr.x_port == 0)
90 		sns->sns_addr.x_port = htons(IDPPORT_RIF);
91 #ifdef DEBUG
92 	if(do_output || ntohs(msg->rip_cmd) == RIPCMD_REQUEST)
93 #endif
94 	/*
95 	 * Kludge to allow us to get routes out to machines that
96 	 * don't know their addresses yet; send to that address on
97 	 * ALL connected nets
98 	 */
99 	 if (ns_neteqnn(sns->sns_addr.x_net, ns_zeronet)) {
100 	 	extern  struct interface *ifnet;
101 	 	register struct interface *ifp;
102 
103 		for (ifp = ifnet; ifp; ifp = ifp->int_next) {
104 			sns->sns_addr.x_net =
105 				satons_addr(ifp->int_addr).x_net;
106 			(void) sendto(s, msg, size, flags,
107 			    (struct sockaddr *)sns, sizeof (*sns));
108 		}
109 		return;
110 	}
111 
112 	(void) sendto(s, msg, size, flags,
113 	    (struct sockaddr *)sns, sizeof (*sns));
114 }
115 
116 /*
117  * Return 1 if we want this route.
118  * We use this to disallow route net G entries for one for multiple
119  * point to point links.
120  */
121 xnnet_checkhost(sns)
122 	struct sockaddr_ns *sns;
123 {
124 	register struct interface *ifp = if_ifwithnet(sns);
125 	/*
126 	 * We want this route if there is no more than one
127 	 * point to point interface with this network.
128 	 */
129 	if (ifp == 0 || (ifp->int_flags & IFF_POINTOPOINT)==0) return (1);
130 	return (ifp->int_sq.n == ifp->int_sq.p);
131 }
132 
133 /*
134  * Return 1 if the address is
135  * for a host, 0 for a network.
136  */
137 xnnet_ishost(sns)
138 struct sockaddr_ns *sns;
139 {
140 	register u_short *s = sns->sns_addr.x_host.s_host;
141 
142 	if ((s[0]==0xffff) && (s[1]==0xffff) && (s[2]==0xffff))
143 		return (0);
144 	else
145 		return (1);
146 }
147 
148 xnnet_canon(sns)
149 	struct sockaddr_ns *sns;
150 {
151 
152 	sns->sns_addr.x_port = 0;
153 }
154 
155 /*ARGSUSED*/
156 null_hash(addr, hp)
157 	struct sockaddr *addr;
158 	struct afhash *hp;
159 {
160 
161 	hp->afh_nethash = hp->afh_hosthash = 0;
162 }
163 
164 /*ARGSUSED*/
165 null_netmatch(a1, a2)
166 	struct sockaddr *a1, *a2;
167 {
168 
169 	return (0);
170 }
171 
172 /*ARGSUSED*/
173 null_output(s, f, a1, n)
174 	int s, f;
175 	struct sockaddr *a1;
176 	int n;
177 {
178 
179 	;
180 }
181 
182 /*ARGSUSED*/
183 null_portmatch(a1)
184 	struct sockaddr *a1;
185 {
186 
187 	return (0);
188 }
189 
190 /*ARGSUSED*/
191 null_portcheck(a1)
192 	struct sockaddr *a1;
193 {
194 
195 	return (0);
196 }
197 
198 /*ARGSUSED*/
199 null_ishost(a1)
200 	struct sockaddr *a1;
201 {
202 
203 	return (0);
204 }
205 
206 /*ARGSUSED*/
207 null_checkhost(a1)
208 	struct sockaddr *a1;
209 {
210 
211 	return (0);
212 }
213 
214 /*ARGSUSED*/
215 null_canon(a1)
216 	struct sockaddr *a1;
217 {
218 
219 	;
220 }
221