[net.sources] Minor fix to Pathto

elvy@harvard.ARPA (Marc Elvy) (08/14/84)

()

It was pointed out that the pathto program had a minor bug in its error
reporting.  Namely "pathto -h" passed the argument on to the host machine,
where it was rejected as an illegal switch.  While it will not cause anything
to malfunction (now), if a -h flag is ever added to rpath, then things will
not work correctly, so I fixed it.  The new version is enclosed.

Sorry about that.

Marc

Enc.
---------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>

/*
**	$Header: pathto.c,v 1.4 84/08/14 15:39:45 source Exp $
**
**	This is the frontend TCP program for Harvard's pathalias database.
**	It makes a connection with a host machine (default harvard.arpa)
**	and queries that host about the paths from the HOST to other network
**	sites around the world.  The sites should be listed on the command
**	line, and paths to them through various networks will be returned.
**	At present the ARPANET and the UUCP network are known about.  Others
**	may be integrated later.  Note that, unfortunately, the path given
**	is from the host (usually harvard), so routing will be through it.
**
**	Questions or comments should be posted to elvy@harvard.{uucp,arpa}.
**	I would appreciate being warned of any bugs and fixes.    Marc Elvy
*/


# define	PATHHOST	"harvard"



main (argc, argv)
int argc;
char **argv;
{
    register int c, s;
    register FILE *sfi, *sfo;
    char *host = PATHHOST, *NAME = *argv;
    char buffer[512], *strcat();
    struct sockaddr_in sin;
    struct hostent *hp;
    struct servent *sp;
    extern int errno;

    --argc, ++argv;

    /*
    **	If the "-h" option (which is, currently, the only option) is given,
    **	we reset the host to a machine other than PATHHOST.  This should
    **	be done if another database machine is closer than PATHHOST.  If
    **	another machine is ALWAYS closer, then PATHHOST should be changed.
    */

    if ((argc < 1) || (strcmp (*argv, "-h") == 0))
    {
	if (argc > 2)
	{
	    ++argv, argc -= 2;
	    host = *argv++;
	}
	else
	{
	    fprintf (stderr, "Usage:  %s [ -h host ] site(s)\n", NAME);
	    exit (1);
	}
    }

    if ((hp = gethostbyname (host)) == (struct hostent *) NULL)
    {
	fprintf (stderr, "%s:  Unknown host (%s).\n", NAME, host);
	exit (1);
    }
    host = hp->h_name;
    if ((s = socket (hp->h_addrtype, SOCK_STREAM, 0)) < 0)
    {
	perror ("pathto (socket)");
	exit (errno);
    }
    sin.sin_family = hp->h_addrtype;
    if (bind (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
    {
	perror ("pathto (bind)");
	exit (errno);
    }
    bcopy (hp->h_addr, &sin.sin_addr, hp->h_length);
    if ((sp = getservbyname ("path", "tcp")) == NULL)
    {
	fprintf (stderr, "%s:  TCP/path is an unknown service.\n", NAME);
	exit (4);
    }
    sin.sin_port = sp->s_port;
    if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
    {
	perror ("pathto (connect)");
	exit (errno);
    }
    if (((sfi = fdopen (s, "r")) == (FILE *) NULL) ||
	((sfo = fdopen (s, "w")) == (FILE *) NULL))
    {
	perror ("fdopen");
	(void) close (s);
	exit (errno);
    }
    *buffer = (char) NULL;
    while (*argv)
    {
	(void) strcat (buffer, *argv++);
	(void) strcat (buffer, " ");
    }
    (void) strcat (buffer, "\n");
    (void) fputs (buffer, sfo);
    (void) fflush(sfo);
    while ((c = getc (sfi)) != EOF)
	putchar (c);
    (void) fclose(sfo);
    (void) fclose(sfi);
}

		      Marc A. Elvy  ( elvy@harvard.{arpa,uucp} )
			     Aiken Computation Laboratory
				  Harvard University