
#define MAX_CHAN 4096

/* Copyright (C) 1995 NetWin World, New Zealand, All rights reserved. */
#ifdef OS2
#define BSD_SELECT   TRUE
#include <i86.h>
#include <dos.h>
#endif


#ifdef WIN32
#define WIN_SOCK TRUE
#endif

#ifdef INCREASE_FD
#define FD_SETSIZE 2048
#endif

#ifdef WIN_SOCK
#define FD_SETSIZE MAX_CHAN /* This should let us have this many connectins ??*/


#include <winsock.h>
#define socket_close closesocket
#define socket_ioctl ioctlsocket
#define socket_read(s,b,l) recv(s,b,l,0)
#define socket_write(s,b,l) send(s,b,l,0)
#define GOT_SOCKADDR TRUE
#define NOTUNIX TRUE
#endif

#ifdef NETWARE
#define NOTUNIX TRUE
#define GOT_SOCKADDR TRUE
#define NEED_DTABLE TRUE
#endif

#ifdef __MWERKS__ /*RHP we will take this to mean we are making a Mac version*/
#include "Mac.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "sys/socket.h"
#include "sys/ioctl.h"
#include "netdb.h"
#include "sys/time.h"
#include "socket.ext.h"
#include "MacSockDefs.h"
#endif

#ifdef unix
#ifndef NOTUNIX
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#ifdef SCO
#include "myhostdb.h"
#else
#include <netdb.h>
#endif
#include <sys/time.h>
#ifndef OS2
#include <unistd.h>
#endif
#ifdef LINUX
#include <netinet/in.h>
#else /* This one works for solaris , why the hell isn't it standardized */
#ifdef SCO
#include <sys/netinet/in.h>
#else
#include <netinet/in.h>
#ifndef OS2
#include <arpa/inet.h>
#include <unistd.h>
#endif
#endif
#endif
#define SOCKET_DEF TRUE
#endif
#endif

#ifdef OS2
#include <sys/select.h>
#include <process.h>
#include <nerrno.h>
#include <utils.h>
#undef SOCKET_DEF
#define socket_ioctl ioctl
#define socket_read(s,b,l) recv(s,b,l,0)
#define socket_write(s,b,l) send(s,b,l,0)
#define socket_close(s) soclose(s)
#endif


#ifdef SOCKET_DEF
#define socket_close close
#define socket_open open
#define socket_read read
#define socket_ioctl ioctl
#define socket_write write
#define NEED_BCOPY TRUE

#endif
#ifdef NEED_DECLARE
int socket(unsigned int address,unsigned int type,unsigned int protocol);
void socket_perror(char *s);
int socket_read(int channel, char *buffer, int size);
int socket_write(int channel, char *buffer, int size);
int connect();
int select();
int socket_close();
int setsockopt();
int bind();
int listen();
int accept();
char *bset();
char *bzero();
int htons();
int sleep();
char *inet_ntoa();
int getdtablesize();
#endif

#ifndef WIN32
#include <unistd.h>
#include <fcntl.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <signal.h>

#define VERSION_ONLY 1
#ifdef WIN32
#include <process.h>
#else
#include <unistd.h>
#ifndef OS2
#include <sys/wait.h>
#endif
#endif
#ifdef NETWARE
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define socket_read(s,b,l) recv(s,b,l,0)
#define socket_write(s,b,l) send(s,b,l,0)
#define socket_close close
#define socket_ioctl ioctl
NETDB_DEFINE_CONTEXT
NETINET_DEFINE_CONTEXT
#endif





#ifdef WIN_SOCK
static SOCKET x_listen = INVALID_SOCKET;
static SOCKET chan[MAX_CHAN]; /*Should keep list of inuse ones.*/
#else
static short x_listen = -1;
static short chan[MAX_CHAN]; /*Should keep list of inuse ones.*/
#define SOCKET short
#endif

void tcp_startup(void);

#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif

/*
 7 15:32:04 14:info: Multi link from (160.79.54.12) to (209.251.2.11)
 7 15:32:04 14:info: nntp: Doing reverse DNS lookup
*/

int do_reverse(char *ipnum);
int main(int argc, char *argv[])
{
	int i;
	if (argc<2) {
usage:		printf("Usage: testdns -rev n.n.n.n\n");
		return 0;
	}
	tcp_startup();
	for (i=1; i<argc; i++) {
		if (strncmp(argv[i],"-rev",4)==0) {
			do_reverse(argv[++i]);
		} else {
			printf("Invalid option (%s)\n",argv[i]);	
			goto usage;
		}
	}
	return 0;
}
int do_reverse(char *ipnum)
{
        struct sockaddr_in sin;
        struct hostent *hp;
	int start;
	start = time(NULL);

	sin.sin_addr.s_addr = inet_addr(ipnum);
	printf("Looking up (%s)\n",ipnum);
        hp = gethostbyaddr( (char *) &(sin.sin_addr), sizeof(sin.sin_addr), AF_INET);
	printf("Result %p\n",hp);
	if (hp!=NULL) if (hp->h_name!=NULL) {
		printf("result (%s)\n",hp->h_name);
	}
	printf("Delay %d seconds\n",time(NULL)-start);
	return TRUE;
}

void tcp_startup(void)
{
#ifdef WIN_SOCK
        WORD wVersionRequested;
        WSADATA wsaData;
        int err;
        wVersionRequested = MAKEWORD( 1, 1 );

        err = WSAStartup( wVersionRequested, &wsaData );
        if ( err != 0 ) { printf("No winsock found of correct version\n"); return;}
        if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) {
                printf("No winsock found of correct version, trying to continue\n");
                goto ret;
        }

/* wmsg("Socket library found and is version 1.1, Great!\n"); */
/* The Windows Sockets DLL is acceptable. Proceed. */
ret: ;
#endif

}
