sap_output.c revision 15248
1/*
2 * Copyright (c) 1995 John Hay.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 *    must display the following acknowledgement:
14 *	This product includes software developed by John Hay.
15 * 4. Neither the name of the author nor the names of any co-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 John Hay 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 John Hay 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 *	$Id: sap_output.c,v 1.3 1995/12/05 04:59:56 julian Exp $
32 */
33
34/*
35 * Routing Table Management Daemon
36 */
37#include "defs.h"
38
39/*
40 * Apply the function "f" to all non-passive
41 * interfaces.  If the interface supports the
42 * use of broadcasting use it, otherwise address
43 * the output to the known router.
44 */
45void
46sap_supply_toall(void)
47{
48	register struct interface *ifp;
49	struct sockaddr dst;
50	register struct sockaddr_ipx *ipx_dst;
51	register int flags;
52	extern struct interface *ifnet;
53
54	ipx_dst = (struct sockaddr_ipx *)&dst;
55
56	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
57		if (ifp->int_flags & IFF_PASSIVE)
58			continue;
59
60		dst = ifp->int_flags & IFF_BROADCAST ? ifp->int_broadaddr :
61		      ifp->int_flags & IFF_POINTOPOINT ? ifp->int_dstaddr :
62		      ifp->int_addr;
63
64		ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP);
65
66		flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
67		sap_supply(&dst, flags, ifp, SAP_WILDCARD);
68	}
69}
70
71void
72sapsndmsg(dst, flags, ifp)
73	struct sockaddr *dst;
74	int flags;
75	struct interface *ifp;
76{
77	struct sockaddr t_dst;
78	struct sockaddr_ipx *ipx_dst;
79
80	t_dst = *dst;
81	ipx_dst = (struct sockaddr_ipx *)&t_dst;
82
83	if (ipx_dst->sipx_addr.x_port == 0)
84		ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP);
85
86        (*afswitch[dst->sa_family].af_output)
87		(sapsock, flags, &t_dst,
88		sizeof (struct sap_packet) + sizeof(u_short));
89	TRACE_SAP_OUTPUT(ifp, &t_dst,
90			 sizeof (struct sap_packet) + sizeof(u_short));
91}
92
93/*
94 * Supply dst with the contents of the SAP tables. If the ServType ==
95 * SAP_WILDCARD (0xFFFF) supply the whole table, otherwise only the
96 * services that are of ServType. If this won't fit in one packet, chop
97 * it up into several.
98 *
99 * This must be done using the split horizon algorithm.
100 * 1. Don't send SAP info to the interface from where it was received.
101 * 2. If a service is received from more than one interface and the cost is
102 *    the same, don't publish it on either interface. I am calling this
103 *    clones.
104 */
105void
106sap_supply(dst, flags, ifp, ServType)
107	struct sockaddr *dst;
108	int flags;
109	struct interface *ifp;
110	int ServType;
111{
112	register struct sap_entry *sap;
113	register struct sap_entry *csap; /* Clone route */
114	register struct sap_hash *sh;
115	register struct sap_info *n = sap_msg->sap;
116	struct sap_hash *base = sap_head;
117	struct sockaddr_ipx *sipx =  (struct sockaddr_ipx *) dst;
118	af_output_t *output = afswitch[dst->sa_family].af_output;
119	int size, metric;
120
121	if (sipx->sipx_port == 0)
122		sipx->sipx_port = htons(IPXPORT_SAP);
123
124	sap_msg->sap_cmd = ntohs(SAP_RESP);
125
126	for (sh = base; sh < &base[SAPHASHSIZ]; sh++)
127	for (sap = sh->forw; sap != (struct sap_entry *)sh; sap = sap->forw) {
128		size = (char *)n - (char *)sap_msg;
129		if (size >= ((MAXSAPENTRIES * sizeof (struct sap_info)) +
130				sizeof (sap_msg->sap_cmd))) {
131			(*output)(sapsock, flags, dst, size);
132			TRACE_SAP_OUTPUT(ifp, dst, size);
133			n = sap_msg->sap;
134		}
135
136		/*
137		 * Check for the servicetype except if the ServType is
138		 * a wildcard (0xFFFF).
139		 */
140		if ((ServType != SAP_WILDCARD) &&
141		    (ServType != sap->sap.ServType))
142			continue;
143
144		/*
145		 * This should do rule one and two of the split horizon
146		 * algorithm.
147		 */
148		if (sap->ifp == ifp)
149			continue;
150
151		/*
152		 * Rule 2.
153		 * Look if we have clones (different routes to the same
154		 * place with exactly the same cost).
155		 *
156		 * We should not publish on any of the clone interfaces.
157		 */
158		csap = sap->clone;
159		while (csap) {
160			if (csap->ifp == ifp)
161				goto next;
162			csap = csap->clone;
163		}
164
165		/*
166		 * Don't advertise services with more than 15 hops. It
167		 * will be confused with a service that has gone down.
168		 */
169		if (ntohs(sap->sap.hops) == (HOPCNT_INFINITY - 1))
170			continue;
171		metric = min(ntohs(sap->sap.hops) + 1, HOPCNT_INFINITY);
172
173		*n = sap->sap;
174		n->hops = htons(metric);
175		n++;
176next:
177	}
178	if (n != sap_msg->sap) {
179		size = (char *)n - (char *)sap_msg;
180		(*output)(sapsock, flags, dst, size);
181		TRACE_SAP_OUTPUT(ifp, dst, size);
182	}
183}
184
185