xref: /original-bsd/sbin/XNSrouted/output.c (revision 4d072710)
1 /*
2  * Copyright (c) 1985 The Regents of the University of California.
3  * 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  * 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.
19  */
20 
21 #ifndef lint
22 static char sccsid[] = "@(#)output.c	5.6 (Berkeley) 09/19/88";
23 #endif /* not lint */
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  */
36 toall(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*/
59 sendmsg(dst, flags, ifp)
60 	struct sockaddr *dst;
61 	int flags;
62 	struct interface *ifp;
63 {
64 
65 	(*afswitch[dst->sa_family].af_output)
66 		(flags, dst, sizeof (struct rip));
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  */
74 supply(dst, flags, ifp)
75 	struct sockaddr *dst;
76 	int flags;
77 	struct interface *ifp;
78 {
79 	register struct rt_entry *rt;
80 	register struct rthash *rh;
81 	register struct netinfo *nn;
82 	register struct netinfo *n = msg->rip_nets;
83 	struct rthash *base = hosthash;
84 	struct sockaddr_ns *sns =  (struct sockaddr_ns *) dst;
85 	int (*output)() = afswitch[dst->sa_family].af_output;
86 	int doinghost = 1, size, metric;
87 	union ns_net net;
88 
89 	msg->rip_cmd = ntohs(RIPCMD_RESPONSE);
90 again:
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)) {
95 			(*output)(flags, dst, size);
96 			TRACE_OUTPUT(ifp, dst, size);
97 			n = msg->rip_nets;
98 		}
99 		sns = (struct sockaddr_ns *)&rt->rt_dst;
100 	        if ((rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
101 			sns = (struct sockaddr_ns *)&rt->rt_router;
102 		metric = min(rt->rt_metric + 1, HOPCNT_INFINITY);
103 		net = sns->sns_addr.x_net;
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++) {
110 			if (ns_neteqnn(net, nn->rip_dst)) {
111 				if (metric < ntohs(nn->rip_metric))
112 					nn->rip_metric = htons(metric);
113 				goto next;
114 			}
115 		}
116 		n->rip_dst = net;
117 		n->rip_metric = htons(metric);
118 		n++;
119 	next:;
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;
128 		(*output)(flags, dst, size);
129 		TRACE_OUTPUT(ifp, dst, size);
130 	}
131 }
132