This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Whois


Bob Zimmerman schrieb am 2001-06-29, 7:23:

> Is there a port of WHOIS? 

I don't know who wrote it, i found it in the cygwin archives:

/*
 * Cygwin whois
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>

#define	NICHOST		"whois.internic.net"
#define	DNICHOST	"whois.nic.mil"
#define	GNICHOST	"whois.nic.gov"
#define	ANICHOST	"whois.arin.net"
#define	RNICHOST	"whois.ripe.net"
#define	PNICHOST	"whois.apnic.net"
#define RUNICHOST   "whois.ripn.net"
#define DENICHOST   "whois.nic.de"
#define	WHOIS_PORT	43

static void usage (void);

int
main(int argc, char **argv)
{
	register FILE *sfi, *sfo;
	register int ch;
	struct sockaddr_in sin;
	struct hostent *hp;
	struct servent *sp;
	int s;
	char *host;

#ifdef	SOCKS
	SOCKSinit(argv[0]);
#endif

	host = NICHOST;
	while ((ch = getopt(argc, argv, "badgh:prR")) != -1)
		switch((char)ch) {
		case 'b':
			host = DENICHOST;
			break;
		case 'a':
			host = ANICHOST;
			break;
		case 'd':
			host = DNICHOST;
			break;
		case 'g':
			host = GNICHOST;
			break;
		case 'h':
			host = optarg;
			break;
		case 'p':
			host = PNICHOST;
			break;
		case 'r':
			host = RNICHOST;
			break;
		case 'R':
			host = RUNICHOST;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (!argc)
		usage();

	s = socket(PF_INET, SOCK_STREAM, 0);
	if (s < 0){
		fprintf(stderr, "%s: %s", "socket", strerror(errno));
		return -1;
	}

	memset(&sin, 0, sizeof sin);
/*	sin.sin_len = sizeof sin; */
	sin.sin_family = AF_INET;
	
	hp = gethostbyname(host);
	if (hp == NULL){
		fprintf(stderr, "%s: %s", host, "gethostbyname() failed\n");
		return -2;
	}
	host = (char *)hp->h_name;
	sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0];

	sp = getservbyname("whois", "tcp");
	if (sp == NULL)
		sin.sin_port = htons(WHOIS_PORT);
	else
		sin.sin_port = sp->s_port;

	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0){
		fprintf(stderr, "%s: %s", "connect", strerror(errno));
		return -3;
	}

	sfi = fdopen(s, "r");
	sfo = fdopen(s, "w");
	if (sfi == NULL || sfo == NULL){
		fprintf(stderr, "%s: %s", "fdopen", strerror(errno));
		return -5;
	}
	while (argc-- > 1)
		(void)fprintf(sfo, "%s ", *argv++);
	(void)fprintf(sfo, "%s\r\n", *argv);
	(void)fflush(sfo);
	while ((ch = getc(sfi)) != EOF)
		putchar(ch);
	exit(0);
}

static void
usage()
{
	fprintf(stderr, "usage: whois [-badgprR] [-h hostname] name ...\n");
	exit(-5);
}


/* gph */

-- 
=^..^=

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]