* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1983 Regents of the University of California.\n\
static char sccsid
[] = "@(#)htable.c 5.5 (Berkeley) %G%";
* htable - convert NIC host table into a UNIX format.
* NIC format is described in RFC 810, 1 March 1982.
#include "htable.h" /* includes <sys/types.h> */
#define DATELINES 3 /* these lines usually contain the date */
#define MAXNETS 30 /* array size for local, connected nets */
FILE *hf
; /* hosts file */
FILE *gf
; /* gateways file */
FILE *nf
; /* networks file */
struct gateway
*savegateway(), *gatewayto();
int connected_nets
[MAXNETS
];
nconnected
= addlocal(argv
[1], connected_nets
);
nlocal
= addlocal(argv
[1], local_nets
);
if (freopen(infile
, "r", stdin
) == NULL
) {
hf
= fopen("hosts", "w");
copylocal(hf
, "localhosts");
gf
= fopen("gateways", "w");
copygateways(gf
, "localgateways");
nf
= fopen("networks", "w");
copylocal(nf
, "localnetworks");
copycomments(stdin
, hf
, DATELINES
);
"usage: %s [ -c connected-nets ] [-l local-nets ] [ input-file ]\n",
* Turn a comma-separated list of network names or numbers in dot notation
* (e.g. "arpanet, 128.32") into an array of net numbers.
while (*p
&& *p
!= ',' && !isspace(*p
))
while (*arg
&& isspace(*arg
))
fprintf(stderr
, "%s: Too many networks in list\n",
if (getnetaddr(arg
, &nets
[nfound
]))
fprintf(stderr
, "%s: %s: unknown network\n",
p
= malloc(strlen(str
) + 1);
nm
= (struct name
*)malloc(sizeof (struct name
));
do_entry(keyword
, addrlist
, namelist
, cputype
, opsys
, protos
)
struct name
*namelist
, *cputype
, *opsys
, *protos
;
register struct addr
*al
, *al2
;
register struct name
*nl
;
struct addr
*connect_addr
;
fprintf(stderr
, "htable: net");
putnet(stderr
, inet_netof(addrlist
->addr_val
));
fprintf(stderr
, " missing names.\n");
fprintf(nf
, "%-16.16s", lower(nl
->name_val
));
putnet(nf
, inet_netof(al
->addr_val
));
while (nl
= nl
->name_link
) {
fprintf(nf
, cp
, lower(nl
->name_val
));
/* locate locally connected address, if one */
for (al
= addrlist
; al
; al
= al
->addr_link
)
if (connectedto(inet_netof(al
->addr_val
)))
* Not connected to known networks. Save for later.
struct gateway
*gw
, *firstgw
= (struct gateway
*) NULL
;
for (al
= addrlist
; al
; al
= al
->addr_link
) {
net
= inet_netof(al
->addr_val
);
gw
= savegateway(namelist
, net
,
if (firstgw
== (struct gateway
*) NULL
)
gw
->g_firstent
= firstgw
;
* Connected to a known network.
* Mark this as the gateway to all other networks
* that are on the addrlist (unless we already have
for (al
= addrlist
; al
; al
= al
->addr_link
) {
/* suppress duplicates -- not optimal */
net
= inet_netof(al
->addr_val
);
if (connectedto(net
) || gatewayto(net
))
printgateway(net
, namelist
->name_val
, 1);
(void) savegateway(namelist
, net
, al
->addr_val
, 1);
* Put the gateway in the hosts file.
putaddr(hf
, connect_addr
->addr_val
);
for (nl
= namelist
; nl
; nl
= nl
->name_link
) {
fprintf(hf
, cp
, lower(nl
->name_val
));
fprintf(hf
, "\t# gateway\n");
if (!local(inet_netof(al
->addr_val
))) {
putaddr(hf
, al
->addr_val
);
for (nl
= namelist
; nl
; nl
= nl
->name_link
) {
fprintf(hf
, cp
, lower(nl
->name_val
));
fprintf(stderr
, "Unknown keyword: %d.\n", keyword
);
printgateway(net
, name
, metric
)
np
= getnetbyaddr(net
, AF_INET
);
fprintf(gf
, "%s", np
->n_name
);
fprintf(gf
, " gateway %s metric %d passive\n",
lhf
= fopen(filename
, "r");
fprintf(stderr
, "Warning, no %s file.\n", filename
);
while (cc
= fread(buf
, 1, sizeof(buf
), lhf
))
copygateways(f
, filename
)
lhf
= fopen(filename
, "r");
fprintf(stderr
, "Warning, no %s file.\n", filename
);
/* format: {net | host} XX gateway XX metric DD [passive]\n */
if (fgets(buf
, sizeof(buf
), lhf
) == (char *)NULL
)
sscanf(buf
, "%s %s gateway %s metric %d %s",
type
, dname
, gname
, &metric
, junk
) < 5)
if (!getnetaddr(dname
, &net
))
if (!gethostaddr(gname
, &addr
))
(void) savegateway(nl
, net
, addr
, metric
);
struct netent
*np
= getnetbyname(name
);
*addr
= inet_network(name
);
if (np
->n_addrtype
!= AF_INET
)
hp
= gethostbyname(name
);
*addr
= *(u_long
*)(hp
->h_addr
);
copycomments(in
, out
, ccount
)
for (count
=0; count
< ccount
; count
++) {
if ((fgets(buf
, sizeof(buf
), in
) == NULL
) || (buf
[0] != ';'))
#define UC(b) (((int)(b))&0xff)
* Print network number in internet-standard dot notation;
* v is in host byte order.
fprintf(f
, "%d.%d", UC(v
>> 8), UC(v
));
fprintf(f
, "%d.%d.%d", UC(v
>> 16), UC(v
>> 8), UC(v
));
register char *a
= (char *)&v
;
sprintf(buf
,"%d.%d.%d.%d", UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]));
fprintf(f
, "%-16.16s", buf
);
register struct name
*nl
, *nl2
;
register struct addr
*al
, *al2
;
al2
= al
->addr_link
, free((char *)al
);
struct gateway
*gateways
= 0;
struct gateway
*lastgateway
= 0;
register struct gateway
*gp
;
for (gp
= gateways
; gp
; gp
= gp
->g_link
)
if ((gp
->g_net
== net
) && (gp
->g_metric
> 0))
return ((struct gateway
*) NULL
);
savegateway(namelist
, net
, addr
, metric
)
register struct gateway
*gp
;
gp
= (struct gateway
*)malloc(sizeof (struct gateway
));
fprintf(stderr
, "htable: out of memory\n");
gp
->g_link
= (struct gateway
*) NULL
;
lastgateway
->g_link
= gp
;
for (i
= 0; i
< nconnected
; i
++)
if (connected_nets
[i
] == net
)
for (i
= 0; i
< nlocal
; i
++)
if (local_nets
[i
] == net
)
* Go through list of gateways, finding connections for gateways
* that are not yet connected.
register struct gateway
*gp
, *gw
, *ggp
;
register int hops
, changed
= 1;
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.
gp
->g_metric
= gw
->g_metric
+ 1;
for (ggp
= gp
->g_firstent
; ggp
->g_name
== gp
->g_name
;
if (gatewayto(ggp
->g_net
))
ggp
->g_metric
= gp
->g_metric
;
gw
->g_dst
->g_name
->name_val
, gp
->g_metric
);
* Put the gateway in the hosts file,
* using the address for the connected net.
for (nl
= gp
->g_name
; nl
; nl
= nl
->name_link
) {
fprintf(hf
, cp
, lower(nl
->name_val
));
fprintf(hf
, "\t# gateway\n");