don't need dir.h
[unix-history] / usr / src / sbin / XNSrouted / output.c
CommitLineData
3efa4c38 1/*
3565c602
KB
2 * Copyright (c) 1985 The Regents of the University of California.
3 * All rights reserved.
3efa4c38 4 *
3565c602
KB
5 * This file includes significant work done at Cornell University by
6 * Bill Nesheim. That work included by permission.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
3efa4c38
KS
19 */
20
df6b4396 21#ifndef lint
3565c602
KB
22static char sccsid[] = "@(#)output.c 5.6 (Berkeley) %G%";
23#endif /* not lint */
df6b4396
KS
24
25/*
26 * Routing Table Management Daemon
27 */
28#include "defs.h"
29
30/*
31 * Apply the function "f" to all non-passive
32 * interfaces. If the interface supports the
33 * use of broadcasting use it, otherwise address
34 * the output to the known router.
35 */
36toall(f)
37 int (*f)();
38{
39 register struct interface *ifp;
40 register struct sockaddr *dst;
41 register int flags;
42 extern struct interface *ifnet;
43
44 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
45 if (ifp->int_flags & IFF_PASSIVE)
46 continue;
47 dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr :
48 ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr :
49 &ifp->int_addr;
50 flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
51 (*f)(dst, flags, ifp);
52 }
53}
54
55/*
56 * Output a preformed packet.
57 */
58/*ARGSUSED*/
59sendmsg(dst, flags, ifp)
60 struct sockaddr *dst;
61 int flags;
62 struct interface *ifp;
63{
64
48681466
KS
65 (*afswitch[dst->sa_family].af_output)
66 (flags, dst, sizeof (struct rip));
df6b4396
KS
67 TRACE_OUTPUT(ifp, dst, sizeof (struct rip));
68}
69
70/*
71 * Supply dst with the contents of the routing tables.
72 * If this won't fit in one packet, chop it up into several.
73 */
74supply(dst, flags, ifp)
75 struct sockaddr *dst;
76 int flags;
77 struct interface *ifp;
78{
79 register struct rt_entry *rt;
df6b4396 80 register struct rthash *rh;
4a10c08d
KS
81 register struct netinfo *nn;
82 register struct netinfo *n = msg->rip_nets;
df6b4396 83 struct rthash *base = hosthash;
48681466 84 struct sockaddr_ns *sns = (struct sockaddr_ns *) dst;
df6b4396 85 int (*output)() = afswitch[dst->sa_family].af_output;
4a10c08d 86 int doinghost = 1, size, metric;
84f93e8f 87 union ns_net net;
df6b4396
KS
88
89 msg->rip_cmd = ntohs(RIPCMD_RESPONSE);
90again:
91 for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
92 for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
93 size = (char *)n - (char *)msg;
94 if (size > MAXPACKETSIZE - sizeof (struct netinfo)) {
48681466 95 (*output)(flags, dst, size);
df6b4396
KS
96 TRACE_OUTPUT(ifp, dst, size);
97 n = msg->rip_nets;
98 }
48681466 99 sns = (struct sockaddr_ns *)&rt->rt_dst;
3efa4c38
KS
100 if ((rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
101 sns = (struct sockaddr_ns *)&rt->rt_router;
4a10c08d 102 metric = min(rt->rt_metric + 1, HOPCNT_INFINITY);
84f93e8f 103 net = sns->sns_addr.x_net;
4a10c08d
KS
104 /*
105 * Make sure that we don't put out a two net entries
106 * for a pt to pt link (one for the G route, one for the if)
107 * This is a kludge, and won't work if there are lots of nets.
108 */
109 for (nn = msg->rip_nets; nn < n; nn++) {
84f93e8f
KS
110 if (ns_neteqnn(net, nn->rip_dst)) {
111 if (metric < ntohs(nn->rip_metric))
4a10c08d
KS
112 nn->rip_metric = htons(metric);
113 goto next;
114 }
115 }
84f93e8f 116 n->rip_dst = net;
4a10c08d 117 n->rip_metric = htons(metric);
df6b4396 118 n++;
4a10c08d 119 next:;
df6b4396
KS
120 }
121 if (doinghost) {
122 doinghost = 0;
123 base = nethash;
124 goto again;
125 }
126 if (n != msg->rip_nets) {
127 size = (char *)n - (char *)msg;
48681466 128 (*output)(flags, dst, size);
df6b4396
KS
129 TRACE_OUTPUT(ifp, dst, size);
130 }
131}