version 1.16, 2002/09/05 17:22:16 |
version 1.17, 2002/12/17 20:17:49 |
|
|
*/ |
*/ |
|
|
#ifndef lint |
#ifndef lint |
static char copyright[] = |
static const char copyright[] = |
"@(#) Copyright (c) 1980, 1993\n\ |
"@(#) Copyright (c) 1980, 1993\n\ |
The Regents of the University of California. All rights reserved.\n"; |
The Regents of the University of California. All rights reserved.\n"; |
#endif /* not lint */ |
#endif /* not lint */ |
|
|
#ifndef lint |
#ifndef lint |
#if 0 |
#if 0 |
static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93"; |
static const char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93"; |
#else |
#else |
static char rcsid[] = "$OpenBSD$"; |
static const char rcsid[] = "$OpenBSD$"; |
#endif |
#endif |
#endif /* not lint */ |
#endif /* not lint */ |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/socket.h> |
#include <sys/socket.h> |
|
|
#include <netinet/in.h> |
#include <netinet/in.h> |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
#include <netdb.h> |
#include <netdb.h> |
|
|
#include <err.h> |
#include <err.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
|
#define WHOIS_INIC_FALLBACK 0x02 |
#define WHOIS_INIC_FALLBACK 0x02 |
#define WHOIS_QUICK 0x04 |
#define WHOIS_QUICK 0x04 |
|
|
static void usage(void); |
static __dead void usage(void); |
static void whois(char *, struct addrinfo *, int); |
static int whois(char *, struct addrinfo *, int); |
|
|
int |
int |
main(argc, argv) |
main(int argc, char **argv) |
int argc; |
|
char **argv; |
|
{ |
{ |
int ch, i, j, error; |
int ch, i, j, error, rval; |
int use_qnichost, flags; |
int use_qnichost, flags; |
char *host; |
size_t len; |
char *qnichost; |
char *host, *qnichost, *name; |
struct addrinfo hints, *res; |
struct addrinfo hints, *res; |
|
|
#ifdef SOCKS |
#ifdef SOCKS |
|
|
qnichost = NULL; |
qnichost = NULL; |
flags = 0; |
flags = 0; |
use_qnichost = 0; |
use_qnichost = 0; |
|
rval = 0; |
while ((ch = getopt(argc, argv, "adgh:ilmpqQrR")) != -1) |
while ((ch = getopt(argc, argv, "adgh:ilmpqQrR")) != -1) |
switch((char)ch) { |
switch((char)ch) { |
case 'a': |
case 'a': |
|
|
if (!(flags & WHOIS_QUICK)) |
if (!(flags & WHOIS_QUICK)) |
flags |= WHOIS_INIC_FALLBACK | WHOIS_RECURSE; |
flags |= WHOIS_INIC_FALLBACK | WHOIS_RECURSE; |
} |
} |
while (argc--) { |
for (name = *argv; (name = *argv) != NULL; argv++) { |
if (use_qnichost) { |
if (use_qnichost) { |
if (qnichost) { |
if (qnichost) { |
free(qnichost); |
free(qnichost); |
qnichost = NULL; |
qnichost = NULL; |
} |
} |
for (i = j = 0; (*argv)[i]; i++) |
for (i = j = 0; name[i]; i++) |
if ((*argv)[i] == '.') |
if (name[i] == '.') |
j = i; |
j = i; |
if (j != 0) { |
if (j != 0) { |
int len = i - j + 1 + strlen(QNICHOST_TAIL); |
len = i - j + 1 + strlen(QNICHOST_TAIL); |
|
qnichost = (char *)calloc(len, sizeof(char)); |
qnichost = (char *) calloc(len, sizeof(char)); |
|
if (!qnichost) |
if (!qnichost) |
err(1, "malloc"); |
err(1, "calloc"); |
strlcpy(qnichost, *argv + j + 1, len); |
strlcpy(qnichost, name + j + 1, len); |
strlcat(qnichost, QNICHOST_TAIL, len); |
strlcat(qnichost, QNICHOST_TAIL, len); |
memset(&hints, 0, sizeof(hints)); |
memset(&hints, 0, sizeof(hints)); |
hints.ai_flags = 0; |
hints.ai_flags = 0; |
|
|
hints.ai_socktype = SOCK_STREAM; |
hints.ai_socktype = SOCK_STREAM; |
error = getaddrinfo(qnichost, "whois", |
error = getaddrinfo(qnichost, "whois", |
&hints, &res); |
&hints, &res); |
if (error != 0) |
if (error) { |
errx(EX_NOHOST, "%s: %s", qnichost, |
warnx("%s: %s", qnichost, |
gai_strerror(error)); |
gai_strerror(error)); |
|
rval++; |
|
continue; |
|
} |
} |
} |
} |
} |
if (!qnichost) { |
if (!qnichost) { |
|
|
hints.ai_family = AF_UNSPEC; |
hints.ai_family = AF_UNSPEC; |
hints.ai_socktype = SOCK_STREAM; |
hints.ai_socktype = SOCK_STREAM; |
error = getaddrinfo(host, "whois", &hints, &res); |
error = getaddrinfo(host, "whois", &hints, &res); |
if (error != 0) |
if (error) { |
errx(EX_NOHOST, "%s: %s", host, |
warnx("%s: %s", host, gai_strerror(error)); |
gai_strerror(error)); |
rval++; |
|
continue; |
|
} |
} |
} |
|
|
whois(*argv++, res, flags); |
whois(name, res, flags); |
freeaddrinfo(res); |
freeaddrinfo(res); |
} |
} |
exit(0); |
exit(rval); |
} |
} |
|
|
static void |
static int |
whois(name, res, flags) |
whois(char *name, struct addrinfo *res, int flags) |
char *name; |
|
struct addrinfo *res; |
|
int flags; |
|
{ |
{ |
FILE *sfi, *sfo; |
FILE *sfi, *sfo; |
char *buf, *p, *nhost, *nbuf = NULL; |
char *buf, *p, *nhost, *nbuf = NULL; |
size_t len; |
size_t len; |
int s, nomatch; |
int s, nomatch, rval; |
const char *reason = NULL; |
const char *reason = NULL; |
|
|
s = -1; |
s = -1; |
|
rval = 0; |
for (/*nothing*/; res; res = res->ai_next) { |
for (/*nothing*/; res; res = res->ai_next) { |
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
if (s < 0) { |
if (s < 0) { |
|
|
} |
} |
if (s < 0) { |
if (s < 0) { |
if (reason) |
if (reason) |
err(EX_OSERR, "%s", reason); |
warn("%s", reason); |
else |
else |
errx(EX_OSERR, "unknown error in connection attempt"); |
warn("unknown error in connection attempt"); |
|
return (rval + 1); |
} |
} |
|
|
sfi = fdopen(s, "r"); |
sfi = fdopen(s, "r"); |
|
|
else if (buf[len - 1] == '\n') |
else if (buf[len - 1] == '\n') |
buf[len - 1] = '\0'; |
buf[len - 1] = '\0'; |
else { |
else { |
nbuf = malloc(len + 1); |
if ((nbuf = malloc(len + 1)) == NULL) |
|
err(1, "malloc"); |
memcpy(nbuf, buf, len); |
memcpy(nbuf, buf, len); |
nbuf[len] = '\0'; |
nbuf[len] = '\0'; |
buf = nbuf; |
buf = nbuf; |
|
|
hints.ai_family = AF_UNSPEC; |
hints.ai_family = AF_UNSPEC; |
hints.ai_socktype = SOCK_STREAM; |
hints.ai_socktype = SOCK_STREAM; |
error = getaddrinfo(nhost, "whois", &hints, &res2); |
error = getaddrinfo(nhost, "whois", &hints, &res2); |
if (error != 0) { |
if (error) { |
warnx("%s: %s", nhost, gai_strerror(error)); |
warnx("%s: %s", nhost, gai_strerror(error)); |
return; |
return (rval + 1); |
} |
} |
if (!nomatch) |
if (!nomatch) |
free(nhost); |
free(nhost); |
whois(name, res2, 0); |
rval += whois(name, res2, 0); |
freeaddrinfo(res2); |
freeaddrinfo(res2); |
} |
} |
|
return (rval); |
} |
} |
|
|
static void |
static __dead void |
usage() |
usage(void) |
{ |
{ |
|
|
(void)fprintf(stderr, |
(void)fprintf(stderr, |