/* 
 * [ NETINFO.HNAME.C ]                                          [ 2/85, CF ]
 * 
 * hname.c - tcp interface to NIC hostnames server
 * 
 * Author:	Christopher A. Kent
 * 		Dept. of Computer Sciences
 * 		Purdue University
 * Date:	Mon Nov 29 1982
 * Copyright (c) 1982 Christopher A. Kent
 *	permission to use for non-profit acitivities is hereby granted
 */
#include <stdio.h>
#include <netlib.h>
#include <sys/types.h>
#include <net/con.h>

#define	NAMESOCK	101		/* name socket is 101 */
#define	NICHOST		"sri-nic"	/* guess who! */
#define	STDTIMO		30		/* timeout on connection */
#define TCPFILE		"/dev/net/net"

main(argc, argv)
char **argv;
{
	struct con	npo;		/* net connection struct */
	netaddr		nichost;	/* address of the NIC */
	int		nfd;		/* net connection */
	FILE		*net;		/* associated file stream */
	register int	c;		/* read them chars faster  */

	if(argc < 2){
		fprintf(stderr,
		    "Usage: %s [all] [hname host] [haddr addr]\n", argv[0]);
		exit(-1);
	}

	nichost = gethost(NICHOST);
	if(isbadhost(nichost)){
		fprintf(stderr, "So who is %s, anyway?\n", NICHOST);
		exit(-1);
	}
	npo.c_mode = (CONACT | CONTCP);
	npo.c_sbufs = npo.c_rbufs = 1;
	npo.c_timeo = STDTIMO;
	npo.c_proto = 0;
	npo.c_lport = 0;
	npo.c_fport = NAMESOCK;
	npo.c_fcon = nichost;
	mkanyhost(npo.c_lcon);
	if((nfd = open (TCPFILE, &npo)) >= 0){
		ioctl(nfd, NETSETE, NULL);
		fprintf(stderr, "Open\n");
	}else{
		fprintf(stderr, "Can't connect\n");
		exit(-1);
	}
	net = fdopen(nfd, "w");
	while(--argc)
		fprintf(net, " %s", *++argv);
	fprintf(net, "\r\n");
	fflush(net);

	net = fdopen(nfd, "r");
	while((c = getc(net)) != EOF)
		if(c != '\r')
			putchar(c);
	exit(0);
}


