[net.sources] Network Routing in one script

tim (06/21/82)

: This shell script will setup nmail a Network Automatic Routing program
echo Creating a directory nmail.srcs
mkdir nmail.srcs
echo Creating Readme file.
cat >nmail.srcs/Readme <<E*O*F
These routines make up an automated  routing  algorithm  for
the USENET.  In addition to this file there should be:

    nmail.c - the source program for the routing algorithm
    nmail.h - the header with changeable defines
    runme   - the shell script that does all the work for you
    sites   - the topology file of the network
    makenet - an awk program to convert the sites file to the
		necessary internal format of the network

The script in runme will prompt for the  name of
your site and the full path where  the  topology
file is to be placed.Example: For my site it was

ucf-cs /usr1/vlsi/tim/net/network

For Mark Horton's site it might be:

cbosgd /usr/local/network

Note: the name can be anything  you  want, but  be  sure  to
specify the entire path name.  The program created is called
nmail which will find the shortest connection  path  to  the
specified site and execute mail passing that path.  Example:
>From my site typing

nmail cbosgd!mark dave unc.smb@udel-relay

will execute mail with the paths

duke!chico!harpo!cbosg!cbosgd!mark dave duke!decvax!ucbvax!unc.smb@udel-relay

Note: the first was a USENET path, the second  was  a  local
user,  the  third was an ARPANET (CSNET) address.  This pro-
gram is definitely inferior to the routing program just sub-
mitted to the net by unc!smb but the advantages of this pro-
gram are that it should install in under 5 minutes  with  no
changes to existing system software.  Also the total size of
everything is under 500 lines of commented code  verses  the
over  2500  lines  of  smb's.   I would recommend installing
smb's version but this will give you a quick and  easy  sub-
stitute  in the mean time.  The path generated is definitely
shortest but not necessarily the least cost since no  calcu-
lation of baud rates or polling frequencies is made.

                    Tim Curry
                    USENET:  duke!ucf-cs!tim
                    ARPANET: ucf-cs.tim@udel-relay
E*O*F
echo Creating makenet file
cat >nmail.srcs/makenet <<E*O*F
cp sites /tmp/site1
cp sites /tmp/site2
awk 'FILENAME == "/tmp/site1" {sites[\$1]=NR; print \$1}
     FILENAME == "/tmp/site2" {for(i=2; i <= NF; i++) \\
                                printf "%s ",sites[\$i];printf "0\n"}
     FILENAME == "nmail.h" && \$1 == "#define" && \$2 == "LOCAL" \\
                              {printf "%d 0\n", sites[\$3]}' \\
     /tmp/site1 /tmp/site2 nmail.h >\$1
rm -f /tmp/site1 /tmp/site2
echo The network topology file is installed.
E*O*F
echo Creating nmail.c file
cat >nmail.srcs/nmail.c <<E*O*F
/*
 *
 * TITLE       : nmail
 *
 * PURPOSE     : To pre-process network addresses finding the shortest
 *                  path through the network
 *
 * PROGRAMMER  : Timothy W. Curry at University of Central Florida
 *
 * DATE        : April 7, 1982
 *
 * ENTRY PTS.  : main
 *
 * PARAMETERS  : addresses of the form: name or site!name or name@arpa_site
 *
 */

/* include the headers to work with standard I/O; isdigit; nmailer defines */

#include <stdio.h>
#include <ctype.h>

#include "nmail.h"

main(argc,argv)
int   argc;
char *argv[];
{  /*
    * Variable Usage:
    * 	net - the input file descriptor name for the network topology file
    *	site_name - a storage buffer to hold the name of a site
    *	plen,psrc,pdest - pointer variables used to build the list of site
    *		names. plen holds the length of the site name; psrc holds
    *		the current character position in the source buffer; pdest
    *		in the destination buffer
    *	dest - holds the name of the site we are trying to reach
    *	addr - holds both the name of the destination site and name of the
    *		user to receive the message
    *	i,k,j,l,m,n - integer counters
    *	ppos - keeps track of the current character position in paths
    *	ptr - index into the list of site links
    *	used - an array to hold the sites already visited while finding
    *		the shortest path
    *	pstk - the stack onto which the breadth first search places values
    */

   FILE *net,*fopen();
   char site_name[SITENAMELEN];
   char *plen,*psrc,*pdest;
   char paths[256],dest[14],addr[30];
   int  i,j,k,l,m,n,ppos;
   short int *ptr,used[MAXSITES],pstk[MAXSITES][2];

   if (argc == 1)
   {  fprintf(stderr,"nmail: no addresses given.\n");
      nmailusage();
   }

   if ((net = fopen(NETFILE,"r")) == NULL)
   {  fprintf(stderr,"nmail: can't open network configuration file.\n");
      exit(1);
   }

   /* the SITES buffer will consist of a byte with the length of
	the site name in it followed by the site name		*/

   *SITES = '\0';
   pdest = SITES;
   for (NUMSITES=1; ; NUMSITES++)
   {  if ((pdest-SITES+SITENAMELEN) > SITEBUFSZ)
      {  fprintf(stderr,"SITES buffer overflowed\n");
         exit(1);
      }
      fscanf(net,"%s",site_name);
      if (isdigit(*site_name)) break;
      for (plen=pdest,psrc=site_name; *++pdest = *psrc++; (*plen)++);
   }

   /* LINKS will be an array of numbers indicating which sites are connected
	to which.  LINDEX is used to index into the LINKS array.	    */

   LINKS[1] = (short int) atoi(site_name);
   LINDEX[1] = &LINKS[1];
   for (i=2,j=1; i<=NUMSITES+1 ; i++)
   {   while (LINKS[j] != 0)
       {  fscanf(net,"%d",&k);
	  j++;
          LINKS[j] = (short int) k;
       }
       LINDEX[i] = &LINKS[j];
       j--;
   }

   /* the last number was the value for the local site */

   Local = LINKS[j];

   /* initialize the buffer to mail */

   paths[0] = '\0';
   ppos = 0;
   for (i=1; i<argc ;i++)
   {  addr[0] = '\0';

      /* if its a local address, then no need to process it */

      if (!(index(argv[i],'@')) && !(index(argv[i],'!')))
      {  strcat(paths,argv[i]);
	 strcat(paths," ");
	 ppos += strlen(argv[i]) + 1;
      }

      /* otherwise a path must be found ! */

      else
      {  if (index(argv[i],'@') && !(index(argv[i],'!'))) strcpy(addr,ARPA);
         strcat(addr,argv[i]);
         strcat(addr," ");

	 /* set dest = name of destination site */

         for (j=0; addr[j] != '!' ;j++)
         {  dest[j] = addr[j];
	    dest[j+1] = '\0';
	    if (dest[j] == '\0')
	    {  fprintf(stderr,"nmail: improperly formed address.\n");
	       nmailusage();
	    }
         }

	 /* find the numeric index of dest for LINDEX to use */

         for (j=0,k=1,m = strlen(dest); k<NUMSITES ;k++)
         {  l = SITES[j];
	    if (l == m)
	    {  for (j++,l+=j,n=0; j<l ; j++,n++)
	          if (SITES[j] != dest[n]) break;
	       if (j == l) break;
	       j = l;
	    }
	    else j += l + 1;
         }
         if (k == NUMSITES)
         {  fprintf(stderr,"nmail: site %s is not in the network.\n",dest);
	    exit(1);
         }

	 /* do a breadth first search to find a path from source to sink */

	 if (k != Local)
         {  for (j=0,l=1,m=0,used[0] = k; k!=Local ;k=pstk[m++][1])
            {  for (ptr=LINDEX[k]; ptr<LINDEX[k+1] ;ptr++)
	       {  for (n=0; n<l ;n++) if (used[n] == *ptr) break;
	          if (n==l)
	          {  used[l] = *ptr;
	             l++;
	             pstk[j][0] = k;
	             pstk[j][1] = *ptr;
	             j++;
	          }
	       }
            }

	 /* convert the numeric site values to thier character names
		and place them in the paths buffer			*/

            k = pstk[m-1][0];
            while (k != pstk[0][0])
            {  for (j=0,l=0; j<k-1 ;j++) l += SITES[l] + 1;
	       n = SITES[l];
	       for (j=0,l++; j<n ;j++,l++,ppos++) paths[ppos] = SITES[l];
	       paths[ppos++] = '!';
	       for ( ; pstk[m][1] != k ;m--);
	       k = pstk[m][0];
            }
	    paths[ppos] = '\0';
         }

         strcat(paths,addr);
         ppos += strlen(addr);
      }
   }

   /* when all the paths are resolved, pass control over to mail */

   printf("%s\n",paths);
   execl("/usr/ucb/mail","mail",paths,0);
}

nmailusage()
{  fprintf(stderr,"usage: nmail addr {addr} \nWhere addr = site!name for ");
   fprintf(stderr,"uucp-address or name@site for ARPANET-address.\n");
   exit(1);
}
E*O*F
echo Creating nmail.h file
cat >nmail.srcs/nmail.h <<E*O*F
/*
 * nmail.h
 *
 * The header file containing defines you might wish to change.
 *
 * SITENAMELEN - The maximum length of the name of a site.
 *
 * MAXSITES - In an attempt to hold down memory usage, a liberal
 *		upper limit is given.  Dynamic calculation is
 *		not worth the trouble in this case.
 *
 * AVGLINKS - Some sites have >20 links.  Most have 1.  Want to
 *		keep the memory size declarations down so this
 *		is an average.
 * SITEBUFSZ - The size of the buffer to hold the site names.
 *
 * NETFILE - Where the topology description is kept.
 *
 * LOCAL - The name of the originating (local) site.
 *
 * ARPA - The name of the site that will gateway mail to the ARPANET
 *
 */

#define SITENAMELEN	15
#define MAXSITES	500
#define AVGLINKS	4
#define SITEBUFSZ	SITENAMELEN*MAXSITES
#define NETFILE		"/usr1/vlsi/tim/net/network"
#define LOCAL		ucf-cs
#define ARPA		"ucbvax!"

short int   NUMSITES,LINKS[MAXSITES*AVGLINKS],*LINDEX[MAXSITES],Local;
char  SITES[SITEBUFSZ];
E*O*F
echo Creating runme file
cat >nmail.srcs/runme <<E*O*F
echo Enter your site name and the full path to the topology file:
read sitename filename
sed -e '/d.*LOCAL/s/\(d.*LOCAL\).*\$/\1		'\$sitename/ \\
    -e '/d.*NETFILE/s?\(d.*NETFILE\).*\$?\1		"'\$filename\"? \\
    nmail.h >/tmp/nmail.h
mv /tmp/nmail.h nmail.h
echo Header file changed reflecting sitename = \$sitename and
echo the topology file = \$filename
echo Creating the topology file.
chmod +x ./makenet
./makenet \$filename
cc -O -o nmail nmail.c
echo The program can be found in the file "nmail".
E*O*F
echo Creating sites file
cat >nmail.srcs/sites <<E*O*F
adiron	duke
alice	npois rabbit research
allegra	mhtsa psuvax princeton
azure	tekmdp
bio	reed
brl-bmd	duke
bwkna	mhuxj mhexa hlexa
cbosg	harpo cbosgd rmas70 nscs mhuxt npois
cbosgd	cbosg
cca	ucb csin ima
cg-d	decvax
chico	harpo zeppo esquire duke
cires	hao
cmcl2	presby nybcb
cornell	vax135
csin	cca
cwruecmp	decvax cwrunix
cwrunix	cwruecmp
cubs45	esquire
dadla-a	dadla-b
dadla-b	tekmdp dadla-a
dcdwestvax	sdcsvax
decvax	microsoft cwruecmp duke pur-ee wivax sultan ittvax genradbolton yale-comix utzoo watmath cg-d ucbvax
druxj	ihnss
duke	chico decvax duke34 dukgeri phs unc mcnc tucc adiron brl-bmd reed ucf-cs
duke34	duke
dukgeri	duke
eagle	rdb mhuxj mitccc mit-vax wheps harpo mhuxt
eiss	npois pyuxbb eisx
eisx	eiss
esquire	chico cubs45
floyd	harpo vax135
genradbolton	decvax
gi	sytek psi
gsp86	intelqa
hao	menlo70 cires
harpo	utah-cs presby cbosg floyd zeppo chico whuxlb mhtsa rdb npois
hlexa	bwkna
ho3e2	houxi
hoasp	houxi
hocsb	houxi hocsd hocse hocsf hocsg
hocsd	hocsb
hocse	hocsb
hocsf	hocsb
hocsg	hocsb
hou5d	houxi
houca	houxi
houti	houxi
hound	houxi
houxa	houxi
houxb	houxi
houxc	houxi
houxd	houxi
houxe	houxi lime
houxf	houxi
houxg	houxi lime
houxh	houxi
houxi	ihnss npois hocsb ho3e2 u1100a houca houti hound houxa houxb houxc houxd houxe houxf houxg houxh houxj houxm houxn houxo houxp houxr houxs houxt houxv houxw houxy hoasp hou5d vax135
houxj	houxi
houxm	houxi
houxn	houxi
houxo	houxi
houxp	houxi
houxr	houxi
houxs	houxi
houxt	houxi
houxv	houxi
houxw	houxi
houxy	houxi
hp-pcd	hplabs ogcvax
hpda	hplabs
hplabs	sri-unix menlo70 hpda hp-pcd
hpuxa	mhtsa
ico	ima
idis	mcnc
ih1ap	ihnss
ihima	ihnss
ihldt	ihnss
ihlpb	ihnss
ihnss	ihuxf ihuxg ihuxh ihuxi ihuxj ihuxk ihuxl ihuxm ihuxn ihuxo ihuxp ihuxs ihlpb ihldt iwlc8 ihima ih1ap ihps3 cbosg druxj mhtsa houxi ucbvax wheps
ihps3	ihnss stolaf
ihuxf	ihnss
ihuxg	ihnss
ihuxh	ihnss
ihuxi	ihnss
ihuxj	ihnss
ihuxk	ihnss
ihuxl	ihnss
ihuxm	ihnss
ihuxn	ihnss
ihuxo	ihnss
ihuxp	ihnss
ihuxs	ihnss
ikonas	mcnc
ima	cca ico
intelqa	sytek gsp86
ittvax	decvax sii qumix tpdcvax
iwlc8	ihnss
lime	houxe houxg we13
mcnc	duke unc web40 wolfvax tucc idis ikonas
menlo70	hplabs hao sytek nsc
mh3bs	mhtsa
mhb5c	mhuxj
mhexa	bwkna
mhtsa	allegra eagle ihnss harpo hpuxa mh3bs wjh12
mhuxa	mhuxj
mhuxh	mhuxj
mhuxj	eagle mhuxa mhuxh mhuxm mhuxv mhb5c pyuxjj bwkna
mhuxm	mhuxj
mhuxt	eagle cbosg
mhuxv	mhuxj
microsoft	decvax
minn-ua	stolaf
mit-vax	eagle
mitccc	rabbit mitmath eagle
mitmath	mitccc
npois	harpo houxi ucbvax alice cbosg eiss u1100s
nscs	cbosg
nybca	nybcb
nybcb	cmcl2 nybca
nsc	menlo70
ogcvax	teklabs hp-pcd
philabs	sdcsvax
phonlab	sdcattb
phs	duke
presby	harpo uofp cmcl2
princeton	allegra
psi	gi
psuvax	allegra
pucc	purdue
pur-ee	decvax purdue uiucdcs
pur-phy	purdue
purdue	pur-ee pucc pur-phy
pyuxbb	eiss
pyuxjj	mhuxj
qumix	ittvax
rabbit	alice mitccc
rdb	harpo eagle
reed	duke bio
research	alice
rmas70	cbosg rmasvax rvb
rmasvax	rmas70
rvb	rmas70
scl	utah-cs
scs23	slinac
scs40	slinac
sdaaron	sdcsvax
sdcarl	ucsfcgl sdcattb
sdcatta	sdcattb
sdcattb	sdcarl phonlab sdcatta sdcsvax
sdcsvax	sdcattb sdcsvax sdqmlab sdaaron dcdwestvax philabs
sdqmlab	sdcsvax
sii	ittvax
slinac	utah-cs scs40 scs23
src-unix	ucb
sri-unix	ucb hplabs
stolaf	ihps3 minn-ua
sultan	decvax
sytek	menlo70 gi intelqa zehntel
tekcad	teklabs
tekid	teklabs
teklabs	tekmdp ucbcad tekcad ogcvax tekid
tekmdp	azure teklabs dadla-b
tpdcvax	ittvax
trigraph	utzoo
tucc	duke mcnc unc
u1100a	houxi
u1100s	npois
ucb	ucbarpa uwvax src-unix cca sri-unix
ucbarpa	ucbopt ucbonyx ucbcad ucbcory populi ucbvax ucb
ucbcad	ucbarpa teklabs
ucbcory	ucbarpa
ucbonyx	ucbarpa
ucbopt	ucbarpa
populi	ucbarpa
ucbvax	ucbarpa decvax ihnss ucsfcgl
ucf-cs	duke
ucsfcgl	ucbvax sdcarl
uicsovax	uiucdcs
uiucdcs	pur-ee uicsovax
unc	duke mcnc tucc
uofp	presby
utah-cs	harpo utah-gr scl slinac
utah-gr	utah-cs
utzoo	decvax trigraph
uwvax	ucb
vax135	houxi cornell floyd
watarts	watmath
watcgl	watmath
watmath	decvax watarts watcgl
we13	lime
web40	mcnc
wheps	ihnss zeppo eagle
whuxlb	harpo
wivax	decvax
wjh12	mhtsa
wolfvax	mcnc
yale-comix	decvax
zehntel	sytek
zeppo	harpo chico wheps
E*O*F
chmod +x nmail.srcs/runme
echo You should now change directory to nmail.srcs and read the file "Readme"
echo Then run the shell script in runme.