+ fp = fopen("/etc/gateways", "r");
+ if (fp == NULL)
+ return;
+ qual = buf;
+ dname = buf + 64;
+ gname = buf + ((BUFSIZ - 64) / 3);
+ type = buf + (((BUFSIZ - 64) * 2) / 3);
+ bzero((char *)&dst, sizeof (dst));
+ bzero((char *)&gate, sizeof (gate));
+ dst.sin_family = gate.sin_family = AF_INET;
+ /* format: {net | host} XX gateway XX metric DD [passive]\n */
+#define readentry(fp) \
+ fscanf((fp), "%s %s gateway %s metric %d %s\n", \
+ type, dname, gname, &metric, qual)
+ for (;;) {
+ struct hostent *host;
+ struct netent *net;
+
+ if (readentry(fp) == EOF)
+ break;
+ if (strcmp(type, "net") == 0) {
+ net = getnetbyname(dname);
+ if (net == 0 || net->n_addrtype != AF_INET)
+ continue;
+ dst.sin_addr = inet_makeaddr(net->n_net, INADDR_ANY);
+ } else if (strcmp(type, "host") == 0) {
+ host = gethostbyname(dname);
+ if (host == 0)
+ continue;
+ bcopy(host->h_addr, &dst.sin_addr, host->h_length);
+ } else
+ continue;
+ host = gethostbyname(gname);
+ if (host == 0)
+ continue;
+ bcopy(host->h_addr, &gate.sin_addr, host->h_length);
+ ifp = (struct interface *)malloc(sizeof (*ifp));
+ bzero((char *)ifp, sizeof (*ifp));
+ ifp->int_flags = IFF_REMOTE;
+ /* can't identify broadcast capability */
+ ifp->int_net = inet_netof(dst.sin_addr);
+ if (strcmp(type, "host") == 0) {
+ ifp->int_flags |= IFF_POINTOPOINT;
+ ifp->int_dstaddr = *((struct sockaddr *)&dst);
+ }
+ if (strcmp(qual, "passive") == 0)
+ ifp->int_flags |= IFF_PASSIVE;
+ else
+ /* assume no duplicate entries */
+ externalinterfaces++;
+ ifp->int_addr = *((struct sockaddr *)&gate);
+ ifp->int_metric = metric;
+ ifp->int_next = ifnet;
+ ifnet = ifp;
+ addrouteforif(ifp);
+ }
+ fclose(fp);