2015-04-25 00:51:44 +00:00
|
|
|
/*-
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2002-03-07 10:06:00 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-03-07 10:06:00 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif /* not lint */
|
2002-02-03 07:35:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-04-25 00:51:44 +00:00
|
|
|
#include <sys/poll.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netinet/in.h>
|
1998-06-12 12:55:46 +00:00
|
|
|
#include <arpa/inet.h>
|
2001-05-28 21:37:29 +00:00
|
|
|
#include <ctype.h>
|
1997-08-26 11:16:08 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netdb.h>
|
2001-07-20 16:44:44 +00:00
|
|
|
#include <stdarg.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
1999-11-17 19:11:51 +00:00
|
|
|
#include <stdlib.h>
|
1997-08-26 11:16:08 +00:00
|
|
|
#include <string.h>
|
1998-02-19 19:07:50 +00:00
|
|
|
#include <sysexits.h>
|
1997-08-26 11:16:08 +00:00
|
|
|
#include <unistd.h>
|
2015-04-25 00:51:44 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-06-14 17:30:46 +00:00
|
|
|
#define ABUSEHOST "whois.abuse.net"
|
1998-02-19 19:07:50 +00:00
|
|
|
#define ANICHOST "whois.arin.net"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define BNICHOST "whois.registro.br"
|
|
|
|
#define FNICHOST "whois.afrinic.net"
|
2016-01-22 16:43:49 +00:00
|
|
|
#define GERMNICHOST "de" QNICHOST_TAIL
|
2015-05-14 10:33:51 +00:00
|
|
|
#define GNICHOST "whois.nic.gov"
|
|
|
|
#define IANAHOST "whois.iana.org"
|
|
|
|
#define INICHOST "whois.networksolutions.com"
|
2004-12-11 13:14:07 +00:00
|
|
|
#define KNICHOST "whois.krnic.net"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define LNICHOST "whois.lacnic.net"
|
1999-11-17 19:11:51 +00:00
|
|
|
#define MNICHOST "whois.ra.net"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define NICHOST "whois.crsnic.net"
|
2015-05-14 11:37:36 +00:00
|
|
|
#define PDBHOST "whois.peeringdb.com"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define PNICHOST "whois.apnic.net"
|
2015-05-14 10:33:01 +00:00
|
|
|
#define QNICHOST_HEAD "whois.nic."
|
1999-11-17 19:11:51 +00:00
|
|
|
#define QNICHOST_TAIL ".whois-servers.net"
|
2015-05-14 10:33:51 +00:00
|
|
|
#define RNICHOST "whois.ripe.net"
|
2016-01-22 16:43:49 +00:00
|
|
|
#define VNICHOST "whois.verisign-grs.com"
|
2015-05-14 10:33:51 +00:00
|
|
|
|
2001-08-05 19:37:12 +00:00
|
|
|
#define DEFAULT_PORT "whois"
|
2015-05-14 10:33:51 +00:00
|
|
|
|
1999-11-17 19:11:51 +00:00
|
|
|
#define WHOIS_RECURSE 0x01
|
2001-10-12 17:39:36 +00:00
|
|
|
#define WHOIS_QUICK 0x02
|
2016-01-22 16:43:49 +00:00
|
|
|
#define WHOIS_SPAM_ME 0x04
|
2001-10-12 17:39:36 +00:00
|
|
|
|
|
|
|
#define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
|
1999-11-17 19:11:51 +00:00
|
|
|
|
2015-05-14 10:31:37 +00:00
|
|
|
static struct {
|
|
|
|
const char *suffix, *server;
|
|
|
|
} whoiswhere[] = {
|
|
|
|
/* Various handles */
|
|
|
|
{ "-ARIN", ANICHOST },
|
|
|
|
{ "-NICAT", "at" QNICHOST_TAIL },
|
|
|
|
{ "-NORID", "no" QNICHOST_TAIL },
|
|
|
|
{ "-RIPE", RNICHOST },
|
|
|
|
/* Nominet's whois server doesn't return referrals to JANET */
|
|
|
|
{ ".ac.uk", "ac.uk" QNICHOST_TAIL },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2016-01-22 16:43:49 +00:00
|
|
|
#define WHOIS_REFERRAL(s) { s, sizeof(s) - 1 }
|
|
|
|
static struct {
|
|
|
|
const char *prefix;
|
|
|
|
size_t len;
|
|
|
|
} whois_referral[] = {
|
|
|
|
WHOIS_REFERRAL("Whois Server: "),
|
|
|
|
WHOIS_REFERRAL("WHOIS Server: "),
|
|
|
|
WHOIS_REFERRAL(" Whois Server: "),
|
|
|
|
WHOIS_REFERRAL("refer: "),
|
|
|
|
WHOIS_REFERRAL("Registrant Street1:Whois Server:"),
|
|
|
|
WHOIS_REFERRAL("ReferralServer: whois://"),
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2011-11-06 18:50:33 +00:00
|
|
|
static const char *port = DEFAULT_PORT;
|
2001-06-27 23:06:47 +00:00
|
|
|
|
2001-07-17 20:40:41 +00:00
|
|
|
static char *choose_server(char *);
|
2015-05-14 10:33:01 +00:00
|
|
|
static struct addrinfo *gethostinfo(char const *host, int exitnoname);
|
2002-02-04 02:12:49 +00:00
|
|
|
static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
|
2001-06-22 01:52:37 +00:00
|
|
|
static void usage(void);
|
2002-02-03 07:35:32 +00:00
|
|
|
static void whois(const char *, const char *, int);
|
1997-08-26 11:16:08 +00:00
|
|
|
|
|
|
|
int
|
2001-06-22 01:52:37 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-08-05 19:37:12 +00:00
|
|
|
const char *country, *host;
|
2001-06-22 01:52:37 +00:00
|
|
|
char *qnichost;
|
2001-07-20 16:44:44 +00:00
|
|
|
int ch, flags, use_qnichost;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1996-04-23 06:01:10 +00:00
|
|
|
#ifdef SOCKS
|
|
|
|
SOCKSinit(argv[0]);
|
|
|
|
#endif
|
|
|
|
|
2001-08-05 19:37:12 +00:00
|
|
|
country = host = qnichost = NULL;
|
|
|
|
flags = use_qnichost = 0;
|
2016-01-22 16:43:49 +00:00
|
|
|
while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:PQrRS")) != -1) {
|
2001-06-22 01:52:37 +00:00
|
|
|
switch (ch) {
|
1998-02-19 19:07:50 +00:00
|
|
|
case 'a':
|
|
|
|
host = ANICHOST;
|
|
|
|
break;
|
2001-08-05 19:37:12 +00:00
|
|
|
case 'A':
|
|
|
|
host = PNICHOST;
|
|
|
|
break;
|
2004-06-14 17:30:46 +00:00
|
|
|
case 'b':
|
|
|
|
host = ABUSEHOST;
|
|
|
|
break;
|
2001-08-05 19:37:12 +00:00
|
|
|
case 'c':
|
|
|
|
country = optarg;
|
2001-10-17 16:28:12 +00:00
|
|
|
break;
|
2006-01-23 13:49:39 +00:00
|
|
|
case 'f':
|
|
|
|
host = FNICHOST;
|
|
|
|
break;
|
1999-02-01 19:22:27 +00:00
|
|
|
case 'g':
|
|
|
|
host = GNICHOST;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'h':
|
|
|
|
host = optarg;
|
|
|
|
break;
|
1999-11-09 09:01:38 +00:00
|
|
|
case 'i':
|
|
|
|
host = INICHOST;
|
|
|
|
break;
|
2004-06-14 14:03:10 +00:00
|
|
|
case 'I':
|
|
|
|
host = IANAHOST;
|
|
|
|
break;
|
2004-12-11 13:14:07 +00:00
|
|
|
case 'k':
|
|
|
|
host = KNICHOST;
|
|
|
|
break;
|
2002-11-10 16:58:56 +00:00
|
|
|
case 'l':
|
|
|
|
host = LNICHOST;
|
|
|
|
break;
|
1999-11-17 19:11:51 +00:00
|
|
|
case 'm':
|
|
|
|
host = MNICHOST;
|
|
|
|
break;
|
1998-02-19 19:07:50 +00:00
|
|
|
case 'p':
|
2001-08-05 19:37:12 +00:00
|
|
|
port = optarg;
|
1998-02-19 19:07:50 +00:00
|
|
|
break;
|
2015-05-14 11:37:36 +00:00
|
|
|
case 'P':
|
|
|
|
host = PDBHOST;
|
|
|
|
break;
|
1999-11-17 19:11:51 +00:00
|
|
|
case 'Q':
|
|
|
|
flags |= WHOIS_QUICK;
|
|
|
|
break;
|
1998-02-19 19:07:50 +00:00
|
|
|
case 'r':
|
|
|
|
host = RNICHOST;
|
|
|
|
break;
|
2016-01-22 16:43:49 +00:00
|
|
|
case 'R':
|
|
|
|
flags |= WHOIS_RECURSE;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
flags |= WHOIS_SPAM_ME;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
2001-06-22 01:52:37 +00:00
|
|
|
/* NOTREACHED */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1999-12-06 21:18:32 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2001-08-05 19:37:12 +00:00
|
|
|
if (!argc || (country != NULL && host != NULL))
|
1994-05-27 12:33:43 +00:00
|
|
|
usage();
|
|
|
|
|
1999-11-17 19:11:51 +00:00
|
|
|
/*
|
2015-05-14 10:31:37 +00:00
|
|
|
* If no host or country is specified, try to determine the top
|
|
|
|
* level domain from the query, or fall back to NICHOST.
|
1999-11-17 19:11:51 +00:00
|
|
|
*/
|
2001-08-05 19:37:12 +00:00
|
|
|
if (host == NULL && country == NULL) {
|
2015-05-14 10:32:35 +00:00
|
|
|
if ((host = getenv("WHOIS_SERVER")) == NULL &&
|
|
|
|
(host = getenv("RA_SERVER")) == NULL) {
|
2014-06-25 15:39:08 +00:00
|
|
|
use_qnichost = 1;
|
|
|
|
host = NICHOST;
|
|
|
|
if (!(flags & WHOIS_QUICK))
|
|
|
|
flags |= WHOIS_RECURSE;
|
|
|
|
}
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
2002-02-03 07:35:32 +00:00
|
|
|
while (argc-- > 0) {
|
2001-08-05 19:37:12 +00:00
|
|
|
if (country != NULL) {
|
|
|
|
s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
|
2002-02-03 07:35:32 +00:00
|
|
|
whois(*argv, qnichost, flags);
|
2001-10-17 16:28:12 +00:00
|
|
|
} else if (use_qnichost)
|
2001-07-20 16:44:44 +00:00
|
|
|
if ((qnichost = choose_server(*argv)) != NULL)
|
2002-02-03 07:35:32 +00:00
|
|
|
whois(*argv, qnichost, flags);
|
2001-07-20 16:44:44 +00:00
|
|
|
if (qnichost == NULL)
|
2002-02-03 07:35:32 +00:00
|
|
|
whois(*argv, host, flags);
|
2001-06-22 01:52:37 +00:00
|
|
|
free(qnichost);
|
|
|
|
qnichost = NULL;
|
2002-02-03 07:35:32 +00:00
|
|
|
argv++;
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2001-07-17 20:40:41 +00:00
|
|
|
/*
|
|
|
|
* This function will remove any trailing periods from domain, after which it
|
|
|
|
* returns a pointer to newly allocated memory containing the whois server to
|
|
|
|
* be queried, or a NULL if the correct server couldn't be determined. The
|
|
|
|
* caller must remember to free(3) the allocated memory.
|
2015-05-14 10:31:37 +00:00
|
|
|
*
|
|
|
|
* If the domain is an IPv6 address or has a known suffix, that determines
|
2015-05-14 10:33:01 +00:00
|
|
|
* the server, else if the TLD is a number, query ARIN, else try a couple of
|
|
|
|
* formulaic server names. Fail if the domain does not contain '.'.
|
2001-07-17 20:40:41 +00:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
choose_server(char *domain)
|
|
|
|
{
|
|
|
|
char *pos, *retval;
|
2015-05-14 10:31:37 +00:00
|
|
|
int i;
|
2015-05-14 10:33:01 +00:00
|
|
|
struct addrinfo *res;
|
2001-07-17 20:40:41 +00:00
|
|
|
|
2010-01-14 10:56:54 +00:00
|
|
|
if (strchr(domain, ':')) {
|
|
|
|
s_asprintf(&retval, "%s", ANICHOST);
|
|
|
|
return (retval);
|
|
|
|
}
|
2016-01-22 16:43:49 +00:00
|
|
|
if (strncasecmp(domain, "AS", 2) == 0) {
|
|
|
|
size_t len = strspn(domain + 2, "0123456789");
|
|
|
|
if (domain[len + 2] == '\0') {
|
|
|
|
s_asprintf(&retval, "%s", ANICHOST);
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 10:31:37 +00:00
|
|
|
for (pos = strchr(domain, '\0'); pos > domain && pos[-1] == '.';)
|
|
|
|
*--pos = '\0';
|
2001-07-17 20:40:41 +00:00
|
|
|
if (*domain == '\0')
|
|
|
|
errx(EX_USAGE, "can't search for a null string");
|
2015-05-14 10:31:37 +00:00
|
|
|
for (i = 0; whoiswhere[i].suffix != NULL; i++) {
|
|
|
|
size_t suffix_len = strlen(whoiswhere[i].suffix);
|
|
|
|
if (domain + suffix_len < pos &&
|
|
|
|
strcasecmp(pos - suffix_len, whoiswhere[i].suffix) == 0) {
|
|
|
|
s_asprintf(&retval, "%s", whoiswhere[i].server);
|
|
|
|
return (retval);
|
|
|
|
}
|
2003-03-25 17:00:03 +00:00
|
|
|
}
|
2001-07-17 20:40:41 +00:00
|
|
|
while (pos > domain && *pos != '.')
|
|
|
|
--pos;
|
2001-07-22 18:23:24 +00:00
|
|
|
if (pos <= domain)
|
|
|
|
return (NULL);
|
2015-05-14 10:33:01 +00:00
|
|
|
if (isdigit((unsigned char)*++pos)) {
|
2001-07-20 16:44:44 +00:00
|
|
|
s_asprintf(&retval, "%s", ANICHOST);
|
2015-05-14 10:33:01 +00:00
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
/* Try possible alternative whois server name formulae. */
|
|
|
|
for (i = 0; ; ++i) {
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
s_asprintf(&retval, "%s%s", QNICHOST_HEAD, pos);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
res = gethostinfo(retval, 0);
|
|
|
|
if (res) {
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return (retval);
|
|
|
|
} else {
|
|
|
|
free(retval);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2001-07-17 20:40:41 +00:00
|
|
|
}
|
|
|
|
|
2001-10-17 16:28:12 +00:00
|
|
|
static struct addrinfo *
|
2015-05-14 10:33:01 +00:00
|
|
|
gethostinfo(char const *host, int exit_on_noname)
|
2001-07-20 16:44:44 +00:00
|
|
|
{
|
|
|
|
struct addrinfo hints, *res;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2016-01-22 16:43:49 +00:00
|
|
|
hints.ai_flags = AI_CANONNAME;
|
2001-07-20 16:44:44 +00:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2015-05-14 10:33:01 +00:00
|
|
|
res = NULL;
|
2001-08-05 19:37:12 +00:00
|
|
|
error = getaddrinfo(host, port, &hints, &res);
|
2015-05-14 10:33:01 +00:00
|
|
|
if (error && (exit_on_noname || error != EAI_NONAME))
|
|
|
|
err(EX_NOHOST, "%s: %s", host, gai_strerror(error));
|
2001-07-20 16:44:44 +00:00
|
|
|
return (res);
|
2001-10-17 16:28:12 +00:00
|
|
|
}
|
2001-07-20 16:44:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper for asprintf(3) that exits on error.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
s_asprintf(char **ret, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
if (vasprintf(ret, format, ap) == -1) {
|
|
|
|
va_end(ap);
|
|
|
|
err(EX_OSERR, "vasprintf()");
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
1999-11-17 19:11:51 +00:00
|
|
|
static void
|
2002-02-03 07:35:32 +00:00
|
|
|
whois(const char *query, const char *hostname, int flags)
|
1999-11-17 19:11:51 +00:00
|
|
|
{
|
2014-01-04 15:51:52 +00:00
|
|
|
FILE *fp;
|
2002-02-03 07:35:32 +00:00
|
|
|
struct addrinfo *hostres, *res;
|
2001-10-12 17:39:36 +00:00
|
|
|
char *buf, *host, *nhost, *p;
|
2016-01-22 16:43:49 +00:00
|
|
|
int s = -1, f, antispam;
|
2015-05-14 10:32:05 +00:00
|
|
|
nfds_t i, j;
|
2016-01-22 16:43:49 +00:00
|
|
|
size_t len, count;
|
2015-04-25 00:51:44 +00:00
|
|
|
struct pollfd *fds;
|
|
|
|
int timeout = 180;
|
1999-11-17 19:11:51 +00:00
|
|
|
|
2002-02-03 07:35:32 +00:00
|
|
|
hostres = gethostinfo(hostname, 1);
|
2015-04-25 00:51:44 +00:00
|
|
|
for (res = hostres, count = 0; res; res = res->ai_next)
|
|
|
|
count++;
|
|
|
|
|
2016-01-22 16:43:49 +00:00
|
|
|
antispam = (flags & WHOIS_SPAM_ME) == 0 &&
|
|
|
|
strcmp(hostres->ai_canonname, VNICHOST) == 0;
|
|
|
|
|
2015-04-25 00:51:44 +00:00
|
|
|
fds = calloc(count, sizeof(*fds));
|
|
|
|
if (fds == NULL)
|
|
|
|
err(EX_OSERR, "calloc()");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse the result list elements and make non-block
|
|
|
|
* connection attempts.
|
|
|
|
*/
|
|
|
|
count = i = 0;
|
|
|
|
for (res = hostres; res != NULL; res = res->ai_next) {
|
|
|
|
s = socket(res->ai_family, res->ai_socktype | SOCK_NONBLOCK,
|
|
|
|
res->ai_protocol);
|
2001-06-22 01:52:37 +00:00
|
|
|
if (s < 0)
|
2001-06-01 13:43:20 +00:00
|
|
|
continue;
|
2015-04-25 00:51:44 +00:00
|
|
|
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
|
|
|
|
if (errno == EINPROGRESS) {
|
|
|
|
/* Add the socket to poll list */
|
|
|
|
fds[i].fd = s;
|
|
|
|
fds[i].events = POLLERR | POLLHUP |
|
|
|
|
POLLIN | POLLOUT;
|
|
|
|
count++;
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Poll only if we have something to poll,
|
|
|
|
* otherwise just go ahead and try next
|
|
|
|
* address
|
|
|
|
*/
|
|
|
|
if (count == 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are at the last address, poll until a connection is
|
|
|
|
* established or we failed all connection attempts.
|
|
|
|
*/
|
|
|
|
if (res->ai_next == NULL)
|
|
|
|
timeout = INFTIM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Poll the watched descriptors for successful connections:
|
|
|
|
* if we still have more untried resolved addresses, poll only
|
|
|
|
* once; otherwise, poll until all descriptors have errors,
|
|
|
|
* which will be considered as ETIMEDOUT later.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = poll(fds, i, timeout);
|
|
|
|
if (n == 0) {
|
|
|
|
/*
|
|
|
|
* No event reported in time. Try with a
|
|
|
|
* smaller timeout (but cap at 2-3ms)
|
|
|
|
* after a new host have been added.
|
|
|
|
*/
|
|
|
|
if (timeout >= 3)
|
|
|
|
timeout <<= 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
} else if (n < 0) {
|
|
|
|
/*
|
|
|
|
* errno here can only be EINTR which we would want
|
|
|
|
* to clean up and bail out.
|
|
|
|
*/
|
|
|
|
s = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the event(s) we have seen.
|
|
|
|
*/
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
if (fds[j].fd == -1 || fds[j].events == 0 ||
|
|
|
|
fds[j].revents == 0)
|
|
|
|
continue;
|
|
|
|
if (fds[j].revents & ~(POLLIN | POLLOUT)) {
|
|
|
|
close(s);
|
|
|
|
fds[j].fd = -1;
|
|
|
|
fds[j].events = 0;
|
|
|
|
count--;
|
|
|
|
continue;
|
|
|
|
} else if (fds[j].revents & (POLLIN | POLLOUT)) {
|
|
|
|
/* Connect succeeded. */
|
|
|
|
s = fds[j].fd;
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} while (timeout == INFTIM && count != 0);
|
1999-12-06 21:18:32 +00:00
|
|
|
}
|
2015-04-25 00:51:44 +00:00
|
|
|
|
|
|
|
/* All attempts were failed */
|
|
|
|
s = -1;
|
|
|
|
if (count == 0)
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
|
|
|
|
done:
|
|
|
|
/* Close all watched fds except the succeeded one */
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
if (fds[j].fd != s && fds[j].fd != -1)
|
|
|
|
close(fds[j].fd);
|
|
|
|
|
|
|
|
if (s != -1) {
|
|
|
|
/* Restore default blocking behavior. */
|
2015-05-14 10:33:33 +00:00
|
|
|
if ((f = fcntl(s, F_GETFL)) != -1) {
|
|
|
|
f &= ~O_NONBLOCK;
|
|
|
|
if (fcntl(s, F_SETFL, f) == -1)
|
2015-04-25 00:51:44 +00:00
|
|
|
err(EX_OSERR, "fcntl()");
|
|
|
|
} else
|
|
|
|
err(EX_OSERR, "fcntl()");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(fds);
|
2002-02-03 07:35:32 +00:00
|
|
|
freeaddrinfo(hostres);
|
2015-04-25 00:51:44 +00:00
|
|
|
if (s == -1)
|
2001-06-22 01:52:37 +00:00
|
|
|
err(EX_OSERR, "connect()");
|
1998-02-19 19:07:50 +00:00
|
|
|
|
2014-01-04 15:51:52 +00:00
|
|
|
fp = fdopen(s, "r+");
|
|
|
|
if (fp == NULL)
|
2001-06-22 01:52:37 +00:00
|
|
|
err(EX_OSERR, "fdopen()");
|
2004-08-25 15:34:44 +00:00
|
|
|
if (strcmp(hostname, GERMNICHOST) == 0) {
|
2015-08-23 08:22:00 +00:00
|
|
|
fprintf(fp, "-T dn,ace -C ISO-8859-1 %s\r\n", query);
|
2007-01-19 08:13:17 +00:00
|
|
|
} else if (strcmp(hostname, "dk" QNICHOST_TAIL) == 0) {
|
2014-01-04 15:51:52 +00:00
|
|
|
fprintf(fp, "--show-handles %s\r\n", query);
|
2016-01-22 16:43:49 +00:00
|
|
|
} else if (antispam) {
|
|
|
|
fprintf(fp, "domain %s\r\n", query);
|
2004-08-25 15:34:44 +00:00
|
|
|
} else {
|
2014-01-04 15:51:52 +00:00
|
|
|
fprintf(fp, "%s\r\n", query);
|
2004-08-25 15:34:44 +00:00
|
|
|
}
|
2014-01-04 15:51:52 +00:00
|
|
|
fflush(fp);
|
1999-11-17 19:11:51 +00:00
|
|
|
nhost = NULL;
|
2014-01-04 15:51:52 +00:00
|
|
|
while ((buf = fgetln(fp, &len)) != NULL) {
|
2001-10-12 17:39:36 +00:00
|
|
|
while (len > 0 && isspace((unsigned char)buf[len - 1]))
|
2001-06-22 01:52:37 +00:00
|
|
|
buf[--len] = '\0';
|
2001-10-12 17:39:36 +00:00
|
|
|
printf("%.*s\n", (int)len, buf);
|
1999-11-17 19:11:51 +00:00
|
|
|
|
2001-06-27 23:06:47 +00:00
|
|
|
if ((flags & WHOIS_RECURSE) && nhost == NULL) {
|
2016-01-22 16:43:49 +00:00
|
|
|
for (i = 0; whois_referral[i].prefix != NULL; i++) {
|
|
|
|
if (strncmp(buf,
|
|
|
|
whois_referral[i].prefix,
|
|
|
|
whois_referral[i].len) != 0)
|
|
|
|
continue;
|
|
|
|
host = buf + whois_referral[i].len;
|
|
|
|
for (p = host; p < buf + len; p++)
|
|
|
|
if (!ishost(*p))
|
2001-10-12 17:39:36 +00:00
|
|
|
break;
|
|
|
|
s_asprintf(&nhost, "%.*s",
|
2016-01-22 16:43:49 +00:00
|
|
|
(int)(p - host), host);
|
|
|
|
break;
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 15:51:52 +00:00
|
|
|
fclose(fp);
|
2001-06-22 01:52:37 +00:00
|
|
|
if (nhost != NULL) {
|
2002-02-03 07:35:32 +00:00
|
|
|
whois(query, nhost, 0);
|
2001-06-22 01:52:37 +00:00
|
|
|
free(nhost);
|
1999-11-17 19:11:51 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
1997-08-26 11:16:08 +00:00
|
|
|
static void
|
2001-06-22 01:52:37 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-06-22 01:52:37 +00:00
|
|
|
fprintf(stderr,
|
2015-05-14 11:37:36 +00:00
|
|
|
"usage: whois [-aAbfgiIklmPQr] [-c country-code | -h hostname] "
|
2001-08-05 19:37:12 +00:00
|
|
|
"[-p port] name ...\n");
|
1998-02-19 19:07:50 +00:00
|
|
|
exit(EX_USAGE);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|