+ gp->g_addr = addr;
+ gp->g_metric = metric;
+ if (metric == 1)
+ gp->g_dst = gp;
+ return (gp);
+}
+
+connectedto(net)
+ u_long net;
+{
+ register i;
+
+ for (i = 0; i < nconnected; i++)
+ if (connected_nets[i] == net)
+ return(1);
+ return(0);
+}
+
+local(net)
+ u_long net;
+{
+ register i;
+
+ for (i = 0; i < nlocal; i++)
+ if (local_nets[i] == net)
+ return(1);
+ return(0);
+}
+
+#define MAXHOPS 10
+
+/*
+ * Go through list of gateways, finding connections for gateways
+ * that are not yet connected.
+ */
+dogateways()
+{
+ register struct gateway *gp, *gw, *ggp;
+ register int hops, changed = 1;
+ struct name *nl;
+ char *cp;
+
+ for (hops = 0; hops < MAXHOPS && changed; hops++, changed = 0) {
+ for (gp = gateways; gp; gp = gp->g_link)
+ if ((gp->g_metric == 0) && (gw = gatewayto(gp->g_net))) {
+ /*
+ * Found a new connection.
+ * For each other network that this gateway is on,
+ * add a new gateway to that network.
+ */
+ changed = 1;
+ gp->g_dst = gw->g_dst;
+ gp->g_metric = gw->g_metric + 1;
+ for (ggp = gp->g_firstent; ggp->g_name == gp->g_name;
+ ggp = ggp->g_link) {
+ if (ggp == gp)
+ continue;
+ if (gatewayto(ggp->g_net))
+ continue;
+ ggp->g_dst = gp->g_dst;
+ ggp->g_metric = gp->g_metric;
+ printgateway(ggp->g_net,
+ gw->g_dst->g_name->name_val, gp->g_metric);
+ }
+ /*
+ * Put the gateway in the hosts file,
+ * using the address for the connected net.
+ */
+ putaddr(hf, gp->g_addr);
+ cp = "%s";
+ for (nl = gp->g_name; nl; nl = nl->name_link) {
+ fprintf(hf, cp, lower(nl->name_val));
+ cp = " %s";
+ }
+ fprintf(hf, "\t# gateway\n");
+ }
+ }