| 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, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. All advertising materials mentioning features or use of this software |
| 17 | * must display the following acknowledgement: |
| 18 | * This product includes software developed by the University of |
| 19 | * California, Berkeley and its contributors. |
| 20 | * 4. Neither the name of the University nor the names of its contributors |
| 21 | * may be used to endorse or promote products derived from this software |
| 22 | * without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | * SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #ifndef lint |
| 38 | static char sccsid[] = "@(#)output.c 5.8 (Berkeley) 2/26/91"; |
| 39 | #endif /* not lint */ |
| 40 | |
| 41 | /* |
| 42 | * Routing Table Management Daemon |
| 43 | */ |
| 44 | #include "defs.h" |
| 45 | |
| 46 | /* |
| 47 | * Apply the function "f" to all non-passive |
| 48 | * interfaces. If the interface supports the |
| 49 | * use of broadcasting use it, otherwise address |
| 50 | * the output to the known router. |
| 51 | */ |
| 52 | toall(f) |
| 53 | int (*f)(); |
| 54 | { |
| 55 | register struct interface *ifp; |
| 56 | register struct sockaddr *dst; |
| 57 | register int flags; |
| 58 | extern struct interface *ifnet; |
| 59 | |
| 60 | for (ifp = ifnet; ifp; ifp = ifp->int_next) { |
| 61 | if (ifp->int_flags & IFF_PASSIVE) |
| 62 | continue; |
| 63 | dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr : |
| 64 | ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr : |
| 65 | &ifp->int_addr; |
| 66 | flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0; |
| 67 | (*f)(dst, flags, ifp); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Output a preformed packet. |
| 73 | */ |
| 74 | /*ARGSUSED*/ |
| 75 | sndmsg(dst, flags, ifp) |
| 76 | struct sockaddr *dst; |
| 77 | int flags; |
| 78 | struct interface *ifp; |
| 79 | { |
| 80 | |
| 81 | (*afswitch[dst->sa_family].af_output) |
| 82 | (flags, dst, sizeof (struct rip)); |
| 83 | TRACE_OUTPUT(ifp, dst, sizeof (struct rip)); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Supply dst with the contents of the routing tables. |
| 88 | * If this won't fit in one packet, chop it up into several. |
| 89 | */ |
| 90 | supply(dst, flags, ifp) |
| 91 | struct sockaddr *dst; |
| 92 | int flags; |
| 93 | struct interface *ifp; |
| 94 | { |
| 95 | register struct rt_entry *rt; |
| 96 | register struct rthash *rh; |
| 97 | register struct netinfo *nn; |
| 98 | register struct netinfo *n = msg->rip_nets; |
| 99 | struct rthash *base = hosthash; |
| 100 | struct sockaddr_ns *sns = (struct sockaddr_ns *) dst; |
| 101 | int (*output)() = afswitch[dst->sa_family].af_output; |
| 102 | int doinghost = 1, size, metric; |
| 103 | union ns_net net; |
| 104 | |
| 105 | msg->rip_cmd = ntohs(RIPCMD_RESPONSE); |
| 106 | again: |
| 107 | for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) |
| 108 | for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) { |
| 109 | size = (char *)n - (char *)msg; |
| 110 | if (size > MAXPACKETSIZE - sizeof (struct netinfo)) { |
| 111 | (*output)(flags, dst, size); |
| 112 | TRACE_OUTPUT(ifp, dst, size); |
| 113 | n = msg->rip_nets; |
| 114 | } |
| 115 | sns = (struct sockaddr_ns *)&rt->rt_dst; |
| 116 | if ((rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST) |
| 117 | sns = (struct sockaddr_ns *)&rt->rt_router; |
| 118 | metric = min(rt->rt_metric + 1, HOPCNT_INFINITY); |
| 119 | net = sns->sns_addr.x_net; |
| 120 | /* |
| 121 | * Make sure that we don't put out a two net entries |
| 122 | * for a pt to pt link (one for the G route, one for the if) |
| 123 | * This is a kludge, and won't work if there are lots of nets. |
| 124 | */ |
| 125 | for (nn = msg->rip_nets; nn < n; nn++) { |
| 126 | if (ns_neteqnn(net, nn->rip_dst)) { |
| 127 | if (metric < ntohs(nn->rip_metric)) |
| 128 | nn->rip_metric = htons(metric); |
| 129 | goto next; |
| 130 | } |
| 131 | } |
| 132 | n->rip_dst = net; |
| 133 | n->rip_metric = htons(metric); |
| 134 | n++; |
| 135 | next:; |
| 136 | } |
| 137 | if (doinghost) { |
| 138 | doinghost = 0; |
| 139 | base = nethash; |
| 140 | goto again; |
| 141 | } |
| 142 | if (n != msg->rip_nets) { |
| 143 | size = (char *)n - (char *)msg; |
| 144 | (*output)(flags, dst, size); |
| 145 | TRACE_OUTPUT(ifp, dst, size); |
| 146 | } |
| 147 | } |