/************************************************************************
*                 WHOIS server for Multinet TCP
*************************************************************************
* Author : Eric Smith
*          (Multinet communications adapted from tcpechoserver.c)
* Date   : 12/20/91
* Usage  : For instructions on using the WHOIS server, read the file
*          whois.help.
*          For instructions on creating the whois database and reconfiguring
*          the fields in the database, read the file whoisadmin.help.
*
*  To compile and link this server:
*
*    $ CC WHOISD
*    $ CC WHOIS_PARSE
*    $ LINK WHOISD,WHOIS_PARSE,SYS$INPUT:/OPT
*    MULTINET:MULTINET_SOCKET_LIBRARY/SHARE
*    SYS$SHARE:VAXCRTL/SHARE
*    ^Z
*
*  To configure the MULTINET_SERVER process to automatically create
*  a process running this image when a connection arrives:
*
*    $ MULTINET CONFIGURE/SERVER
*    MultiNet Server Configuration Utility 2.2(19)
*    [Reading in symbols from SERVER image MULTINET:SERVER.EXE]
*    [Reading in configuration from MULTINET:SERVICES.MASTER_SERVER]
*    SERVER-CONFIG>ADD WHOISD
*    [Adding new configuration entry for service "WHOISD"]
*    Protocol: [TCP] TCP
*    TCP Port number: 43
*    Program to run: [Enter path of executable]
*    [Added service WHOISD to configuration]
*    [Selected service is now WHOISD]
*    SERVER-CONFIG>RESTART
****************************************************************************/
#include <stdio.h>
#include "multinet_root:[multinet.include.sys]types.h"
#include "multinet_root:[multinet.include.sys]socket.h"
#include "multinet_root:[multinet.include.netinet]in.h"
#include "multinet_root:[multinet.include]netdb.h"

static unsigned short sock;

main()
{
  int n, Status;
  char buf[100];
  struct sockaddr_in sin;
  struct hostent *hp;
  static struct {int Size; char *Ptr;} Descr={9 ,"SYS$INPUT"};
/*
 *  $ASSIGN a channel to SYS$INPUT. This channel is the channel
 *  to the network connection.
 */
  Status = SYS$ASSIGN(&Descr, &sock, 0, 0);
  if (!(Status&1)) exit(Status);
/*
 *  Use getpeername() to find out who made the connection to
 *  us, so we can act exactly like the example tcpechoserver-standalone.
 */

  n = sizeof(sin);	   /* Pass in the length */
  if (getpeername(sock, &sin, &n) < 0) {
    socket_perror("whoisd: getpeername");
    exit(0x10000000);
  }

/*
 *  `sin' will be a sockaddr_in structure describing the
 *  remote IP address (and port #) which the connection
 *  was made from. Before we start to echo data, write a
 *  string into the network describing this port.
 */
  hp = gethostbyaddr(&sin.sin_addr, sizeof(sin.sin_addr), AF_INET);
  if (hp) {
/*
 * We found a corresponding hostname, format the string
 * one way...
 */
    sprintf(buf, "Connection received from %s [%s]\r\n",
            hp->h_name, inet_ntoa(sin.sin_addr));
  }
  else {
/*
 * This host not in the host tables or Domain Name Server.
 */
    sprintf(buf, "Connection received from [%s]\r\n",
            inet_ntoa(sin.sin_addr));
  }
  socket_write(sock, buf, strlen(buf));
/*
 * Read command from network.
 */
  n = socket_read(sock, buf, sizeof(buf));
  if (n < 0)
    socket_perror("whoisd: read");
  else {
    buf[n] = 0;
/*
 * Call whois parser.
 */
    whois_parse(buf);
  }
/*
 *  Now close down the connection...
 */
  socket_close(sock);
  exit(1);
}



/*
 * This routine provides the output routine to the network.
 */
output_string(buffer)
  char *buffer;
{
  socket_write(sock, buffer, strlen(buffer));
}
