Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / IPXrouted / sap_output.c
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  * $FreeBSD: src/usr.sbin/IPXrouted/sap_output.c,v 1.9 1999/08/28 01:15:04 peter Exp $
32  * $DragonFly: src/usr.sbin/IPXrouted/sap_output.c,v 1.2 2003/06/17 04:29:52 dillon Exp $
33  */
34
35 /*
36  * Routing Table Management Daemon
37  */
38 #include <unistd.h>
39 #include "defs.h"
40
41 /*
42  * Apply the function "f" to all non-passive
43  * interfaces.  If the interface supports the
44  * use of broadcasting use it, otherwise address
45  * the output to the known router.
46  */
47 void
48 sap_supply_toall(changesonly)
49         int changesonly;
50 {
51         register struct interface *ifp;
52         struct sockaddr dst;
53         register struct sockaddr_ipx *ipx_dst;
54         register int flags;
55         extern struct interface *ifnet;
56
57         ipx_dst = (struct sockaddr_ipx *)&dst;
58
59         for (ifp = ifnet; ifp; ifp = ifp->int_next) {
60                 if (ifp->int_flags & IFF_PASSIVE)
61                         continue;
62
63                 dst = ifp->int_flags & IFF_BROADCAST ? ifp->int_broadaddr :
64                       ifp->int_flags & IFF_POINTOPOINT ? ifp->int_dstaddr :
65                       ifp->int_addr;
66
67                 ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP);
68
69                 flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
70                 sap_supply(&dst, flags, ifp, SAP_WILDCARD, changesonly);
71         }
72 }
73
74 void 
75 sapsndmsg(dst, flags, ifp, changesonly)
76         struct sockaddr *dst;
77         int flags;
78         struct interface *ifp;
79         int changesonly;
80 {
81         struct sockaddr t_dst;
82         struct sockaddr_ipx *ipx_dst;
83
84         t_dst = *dst;
85         ipx_dst = (struct sockaddr_ipx *)&t_dst;
86
87         if (ipx_dst->sipx_addr.x_port == 0)
88                 ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP);
89
90         (*afswitch[dst->sa_family].af_output)
91                 (sapsock, flags, &t_dst, 
92                 sizeof (struct sap_packet) + sizeof(u_short));
93         TRACE_SAP_OUTPUT(ifp, &t_dst, 
94                          sizeof (struct sap_packet) + sizeof(u_short));
95 }
96
97 /*
98  * Supply dst with the contents of the SAP tables. If the ServType ==
99  * SAP_WILDCARD (0xFFFF) supply the whole table, otherwise only the
100  * services that are of ServType. If this won't fit in one packet, chop 
101  * it up into several.
102  *
103  * This must be done using the split horizon algorithm.
104  * 1. Don't send SAP info to the interface from where it was received.
105  * 2. If a service is received from more than one interface and the cost is
106  *    the same, don't publish it on either interface. I am calling this
107  *    clones.
108  */
109 void
110 sap_supply(dst, flags, ifp, ServType, changesonly)
111         struct sockaddr *dst;
112         int flags;
113         struct interface *ifp;
114         int ServType;
115         int changesonly;
116 {
117         register struct sap_entry *sap;
118         register struct sap_entry *csap; /* Clone route */
119         register struct sap_hash *sh;
120         register struct sap_info *n = sap_msg->sap;
121         struct sap_hash *base = sap_head;
122         struct sockaddr_ipx *sipx =  (struct sockaddr_ipx *) dst;
123         af_output_t *output = afswitch[dst->sa_family].af_output;
124         int size, metric;
125         int delay = 0;
126
127         if (sipx->sipx_port == 0)
128                 sipx->sipx_port = htons(IPXPORT_SAP);
129
130         sap_msg->sap_cmd = ntohs(SAP_RESP);
131
132         for (sh = base; sh < &base[SAPHASHSIZ]; sh++)
133         for (sap = sh->forw; sap != (struct sap_entry *)sh; sap = sap->forw) {
134                 size = (char *)n - (char *)sap_msg;
135                 if (size >= ((MAXSAPENTRIES * sizeof (struct sap_info)) +
136                                 sizeof (sap_msg->sap_cmd))) {
137                         (*output)(sapsock, flags, dst, size);
138                         TRACE_SAP_OUTPUT(ifp, dst, size);
139                         n = sap_msg->sap;
140                         delay++;
141                         if(delay == 2) {
142                                 usleep(50000);
143                                 delay = 0;
144                         }
145                 }
146
147                 if (changesonly && !(sap->state & RTS_CHANGED))
148                         continue;
149
150                 /*
151                  * Check for the servicetype except if the ServType is
152                  * a wildcard (0xFFFF).
153                  */
154                 if ((ServType != SAP_WILDCARD) &&
155                     (ServType != sap->sap.ServType))
156                         continue;
157
158                 /*
159                  * This should do rule one and two of the split horizon
160                  * algorithm.
161                  */
162                 if (sap->ifp == ifp)
163                         continue;
164
165                 /*
166                  * Rule 2.
167                  * Look if we have clones (different routes to the same
168                  * place with exactly the same cost).
169                  *
170                  * We should not publish on any of the clone interfaces.
171                  */
172                 csap = sap->clone;
173                 while (csap) {
174                         if (csap->ifp == ifp)
175                                 goto next;
176                         csap = csap->clone;
177                 }
178
179                 /*
180                  * Don't advertise services with more than 15 hops. It
181                  * will be confused with a service that has gone down.
182                  */
183                 if (ntohs(sap->sap.hops) == (HOPCNT_INFINITY - 1))
184                         continue;
185                 metric = min(ntohs(sap->sap.hops) + 1, HOPCNT_INFINITY);
186
187                 *n = sap->sap;
188                 n->hops = htons(metric);
189                 n++;
190 next:
191         }
192         if (n != sap_msg->sap) {
193                 size = (char *)n - (char *)sap_msg;
194                 (*output)(sapsock, flags, dst, size);
195                 TRACE_SAP_OUTPUT(ifp, dst, size);
196         }
197 }
198