/* Simple test to demonstrate tcp_wrappers functionality from a server's
 * point of view under RISC OS
 * by Theo Markettos (theo@markettos.org.uk)
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sockets.h"

#include "syslogif.h"
#include "tcpd.h"

int main(void)
{
  char buf[1024];
  char daemonName[256];
  int ip[4],address;
  struct request_info request;
  struct sockaddr sin;
  struct sockaddr_in *sinet;
  int mode=0,ha=0;

  printf("This program will test entries in Choices:Internet.Hosts* for testd\n\n");
  printf("Log errors to 1) syslog or 2) console? ");
  do
  {
    scanf("%d",&mode);
  } while ((mode!=1) && (mode!=2));

  /* set up what we want to happen about logging */
  switch (mode)
  {
    case 1:
      /* use Doggysoft's syslog -
       * output will appear in a logfile !Syslog.Logs.tcpdtest */
      tcpd_context.use_syslog = 1;
      tcpd_context.syslog_file = "tcpdtest";
      /* logging levels */
      tcpd_context.syslog_error_level = 0;
      tcpd_context.syslog_warning_level = 50;
      break;
    case 2:
      /* just use printf */
      tcpd_context.use_syslog = 0;
      tcpd_context.syslog_file = NULL;
      tcpd_context.syslog_error_level = 0;
      tcpd_context.syslog_warning_level = 50;
      break;
  }

  /* set up our request structure, and tell it we're claiming to
   * be a daemon 'testd' unless the user says otherwise
   */
  printf("Which entry do you wish to check (ie what server shall we claim to be?).  Press Return alone to try 'testd'\n");
  scanf("%s",daemonName);
  if (daemonName=='\0')
    strcpy(daemonName,"testd");
  request_init(&request,RQ_DAEMON,daemonName,0);

  /* this is a macro (see tcpd.h) which sets up default gethostbyname/
   * getaddrbyname functions.  Replace these with your own if required,
   * but see sock_hostname and sock_hostaddr to see what your functions
   * should do (they are more than just calls to gethostbyXXXX)
   */
  sock_methods(&request);

  mode=0;
  printf("Check 1) hostname or 2) IP address (dotted quad)? ");
  do
  {
    scanf("%d",&mode);
  } while ((mode!=1) && (mode!=2));

  switch (mode)
  {
    case 1:
      printf("Enter hostname: ");
      scanf("%s",&buf);
      /* add more information to the connection database */
      request_set(&request,RQ_CLIENT_NAME,buf,0);
      break;
    case 2:
      sinet = (struct sockaddr_in *) &sin;
      sinet->sin_family = AF_INET;
      sinet->sin_port = htons(1234);
      printf("Enter IP address: ");
      scanf("%d.%d.%d.%d",&ip[3],&ip[2],&ip[1],&ip[0]);
      address = (ip[3]<<24) | (ip[2]<<16) | (ip[1]<<8) | ip[0];
      printf("addr = %x\n",address);
      sinet->sin_addr.s_addr = htonl(address);
      /* tell tcp_wrappers about our (alleged) connection */
      request_set(&request,RQ_CLIENT_SIN,&sin);
      break;
  }

  switch (ha=hosts_access(&request))
  {
    case 0:
      printf("Denied\n");
      break;
    case 1:
      printf("Granted\n");
      break;
    default:
      printf("Returned %d - this shouldn't happen!\n",ha);
      break;
  }

  return 0;
}
