summaryrefslogtreecommitdiffstats
path: root/usr.bin/whois/whois.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/whois/whois.c')
-rw-r--r--usr.bin/whois/whois.c71
1 files changed, 46 insertions, 25 deletions
diff --git a/usr.bin/whois/whois.c b/usr.bin/whois/whois.c
index 7b7be08..fc13f26 100644
--- a/usr.bin/whois/whois.c
+++ b/usr.bin/whois/whois.c
@@ -42,7 +42,7 @@ static const char copyright[] =
static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
- "$Id$";
+ "$Id: whois.c,v 1.4 1997/08/26 11:16:08 charnier Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -52,9 +52,15 @@ static const char rcsid[] =
#include <netdb.h>
#include <stdio.h>
#include <string.h>
+#include <sysexits.h>
#include <unistd.h>
-#define NICHOST "whois.internic.net"
+#define NICHOST "whois.internic.net"
+#define DNICHOST "nic.ddn.mil"
+#define ANICHOST "whois.arin.net"
+#define RNICHOST "whois.ripe.net"
+#define PNICHOST "whois.apnic.net"
+#define WHOIS_PORT 43
static void usage __P((void));
@@ -76,11 +82,23 @@ main(argc, argv)
#endif
host = NICHOST;
- while ((ch = getopt(argc, argv, "h:")) != -1)
+ while ((ch = getopt(argc, argv, "adh:pr")) != -1)
switch((char)ch) {
+ case 'a':
+ host = ANICHOST;
+ break;
+ case 'd':
+ host = DNICHOST;
+ break;
case 'h':
host = optarg;
break;
+ case 'p':
+ host = PNICHOST;
+ break;
+ case 'r':
+ host = RNICHOST;
+ break;
case '?':
default:
usage();
@@ -91,32 +109,35 @@ main(argc, argv)
if (!argc)
usage();
- hp = gethostbyname(host);
- if (hp == NULL) {
- (void)fprintf(stderr, "whois: %s: ", host);
- herror((char *)NULL);
- exit(1);
- }
- host = hp->h_name;
- s = socket(hp->h_addrtype, SOCK_STREAM, 0);
+ s = socket(PF_INET, SOCK_STREAM, 0);
if (s < 0)
- err(1, "socket");
- bzero((caddr_t)&sin, sizeof (sin));
- sin.sin_family = hp->h_addrtype;
- bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
+ err(EX_OSERR, "socket");
+
+ memset(&sin, 0, sizeof sin);
+ sin.sin_len = sizeof sin;
+ sin.sin_family = AF_INET;
+
+ if (inet_aton(host, &sin.sin_addr) == 0) {
+ hp = gethostbyname2(host, AF_INET);
+ if (hp == NULL)
+ errx(EX_NOHOST, "%s: %s", host, hstrerror(h_errno));
+ host = hp->h_name;
+ sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0];
+ }
+
sp = getservbyname("whois", "tcp");
if (sp == NULL)
- errx(1, "whois/tcp: unknown service");
- sin.sin_port = sp->s_port;
+ sin.sin_port = htons(WHOIS_PORT);
+ else
+ sin.sin_port = sp->s_port;
+
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
- err(1, "connect");
+ err(EX_OSERR, "connect");
+
sfi = fdopen(s, "r");
sfo = fdopen(s, "w");
- if (sfi == NULL || sfo == NULL) {
- warn("fdopen");
- (void)close(s);
- exit(1);
- }
+ if (sfi == NULL || sfo == NULL)
+ err(EX_OSERR, "fdopen");
while (argc-- > 1)
(void)fprintf(sfo, "%s ", *argv++);
(void)fprintf(sfo, "%s\r\n", *argv);
@@ -129,6 +150,6 @@ main(argc, argv)
static void
usage()
{
- (void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
- exit(1);
+ (void)fprintf(stderr, "usage: whois [-adpr] [-h hostname] name ...\n");
+ exit(EX_USAGE);
}
OpenPOWER on IntegriCloud