int inet_netmatch(struct sockaddr *sa1, struct sockaddr *sa2) { struct sockaddr_in *sin1=(struct sockaddr_in *)sa1; struct sockaddr_in *sin2=(struct sockaddr_in *)sa2; return (inet_netof_subnet(sin1->sin_addr) == inet_netof_subnet(sin2->sin_addr)); }
void inet_hash(struct sockaddr *sa, struct afhash *hp) { struct sockaddr_in *sin=(struct sockaddr_in *)sa; u_long n; n = inet_netof_subnet(sin->sin_addr); if (n) while ((n & 0xff) == 0) n >>= 8; hp->afh_nethash = n; hp->afh_hosthash = ntohl(sin->sin_addr.s_addr); hp->afh_hosthash &= 0x7fffffff; }
void gwkludge(void) { struct sockaddr_in dst, gate; FILE *fp; char *type, *dname, *gname, *qual, buf[BUFSIZ]; struct interface *ifp; int metric, n; struct rt_entry route; fp = fopen(_PATH_GATEWAYS, "r"); if (fp == NULL) return; qual = buf; dname = buf + 64; gname = buf + ((BUFSIZ - 64) / 3); type = buf + (((BUFSIZ - 64) * 2) / 3); memset(&dst, 0, sizeof (dst)); memset(&gate, 0, sizeof (gate)); memset(&route, 0, sizeof(route)); /* format: {net | host} XX gateway XX metric DD [passive | external]\n */ #define readentry(fp) \ fscanf((fp), "%s %s gateway %s metric %d %s\n", \ type, dname, gname, &metric, qual) for (;;) { if ((n = readentry(fp)) == EOF) break; /* * Lusertrap. Vendors should ship the line * * CONFIGME CONFIGME gateway CONFIGME metric 1 * */ if (strcmp(type,"CONFIGME")==0) { fprintf(stderr,"Not starting gated. Please configure first.\n"); exit(1); } if (!getnetorhostname(type, dname, &dst)) continue; if (!gethostnameornumber(gname, &gate)) continue; if (metric == 0) /* XXX */ metric = 1; if (strcmp(qual, "passive") == 0) { /* * Passive entries aren't placed in our tables, * only the kernel's, so we don't copy all of the * external routing information within a net. * Internal machines should use the default * route to a suitable gateway (like us). */ route.rt_dst = *(struct sockaddr *) &dst; route.rt_router = *(struct sockaddr *) &gate; route.rt_flags = RTF_UP; if (strcmp(type, "host") == 0) route.rt_flags |= RTF_HOST; if (metric) route.rt_flags |= RTF_GATEWAY; (void) rtioctl(ADD, &route.rt_rt); continue; } if (strcmp(qual, "external") == 0) { /* * Entries marked external are handled * by other means, e.g. EGP, * and are placed in our tables only * to prevent overriding them * with something else. */ rtadd((struct sockaddr *)&dst, (struct sockaddr *)&gate, metric, RTS_EXTERNAL|RTS_PASSIVE); continue; } /* assume no duplicate entries */ externalinterfaces++; ifp = (struct interface *)malloc(sizeof (*ifp)); memset(ifp, 0, sizeof (*ifp)); ifp->int_flags = IFF_REMOTE; /* can't identify broadcast capability */ ifp->int_net = inet_netof_subnet(dst.sin_addr); if (strcmp(type, "host") == 0) { ifp->int_flags |= IFF_POINTOPOINT; ifp->int_dstaddr = *((struct sockaddr *)&dst); } ifp->int_addr = *((struct sockaddr *)&gate); ifp->int_metric = metric; ifp->int_next = ifnet; ifnet = ifp; addrouteforif(ifp); } fclose(fp); }