2004-04-05 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* Whois (RFC 954) plugin.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define IN_PLUGIN
|
2004-04-09 13:05:16 +00:00
|
|
|
#include "../../echoping.h"
|
2004-04-05 20:56:19 +00:00
|
|
|
|
|
|
|
#define MAX_REQUEST 256
|
|
|
|
|
|
|
|
struct addrinfo whois_server;
|
2004-04-06 09:49:26 +00:00
|
|
|
const char *request = NULL;
|
|
|
|
int dump = FALSE;
|
2004-04-05 20:56:19 +00:00
|
|
|
int n;
|
|
|
|
int sockfd;
|
|
|
|
FILE *files = NULL;
|
2004-04-06 09:49:26 +00:00
|
|
|
poptContext whois_poptcon;
|
|
|
|
|
|
|
|
void
|
|
|
|
whois_usage (const char *msg)
|
|
|
|
{
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
printf ("Error: %s\n", msg);
|
|
|
|
}
|
|
|
|
poptPrintUsage (whois_poptcon, stdout, 0);
|
|
|
|
exit (1);
|
|
|
|
}
|
2004-04-05 20:56:19 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
init (const int argc, const char **argv)
|
|
|
|
{
|
2004-04-06 09:49:26 +00:00
|
|
|
int value;
|
|
|
|
char *msg = malloc (256);
|
|
|
|
/* popt variables */
|
|
|
|
struct poptOption options[] = {
|
|
|
|
{"request", 'r', POPT_ARG_STRING, &request, 'r',
|
|
|
|
"Request/query (a domain name or something else, depending on the server) to send to the whois server",
|
|
|
|
"request"},
|
|
|
|
{"dump", 'd', POPT_ARG_NONE, &dump, 'd',
|
|
|
|
"Dumps the reply from the whois server",
|
|
|
|
""},
|
|
|
|
POPT_AUTOHELP POPT_TABLEEND
|
|
|
|
};
|
|
|
|
whois_poptcon = poptGetContext (NULL, argc,
|
|
|
|
argv, options, POPT_CONTEXT_KEEP_FIRST);
|
|
|
|
while ((value = poptGetNextOpt (whois_poptcon)) > 0)
|
|
|
|
{
|
|
|
|
if (value < -1)
|
|
|
|
{
|
|
|
|
sprintf (msg, "%s: %s",
|
|
|
|
poptBadOption (whois_poptcon, POPT_BADOPTION_NOALIAS),
|
|
|
|
poptStrerror (value));
|
|
|
|
whois_usage (msg);
|
|
|
|
}
|
|
|
|
switch ((char) value)
|
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sprintf (msg, "Wrong option %d (%c)", value, (char) value);
|
|
|
|
whois_usage (msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (request == NULL)
|
|
|
|
whois_usage ("Mandatory request missing");
|
|
|
|
|
2004-04-05 20:56:19 +00:00
|
|
|
return "nicname";
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
start (struct addrinfo *res)
|
|
|
|
{
|
|
|
|
whois_server = *res;
|
|
|
|
}
|
|
|
|
|
2004-04-06 20:30:06 +00:00
|
|
|
int
|
2004-04-05 20:56:19 +00:00
|
|
|
execute ()
|
|
|
|
{
|
|
|
|
int nr = 0;
|
|
|
|
char recvline[MAX_LINE + 1];
|
|
|
|
char complete_request[MAX_REQUEST];
|
|
|
|
if ((sockfd =
|
|
|
|
socket (whois_server.ai_family, whois_server.ai_socktype,
|
|
|
|
whois_server.ai_protocol)) < 0)
|
|
|
|
err_sys ("Can't open socket");
|
|
|
|
if (connect (sockfd, whois_server.ai_addr, whois_server.ai_addrlen) < 0)
|
|
|
|
err_sys ("Can't connect to server");
|
|
|
|
if ((files = fdopen (sockfd, "r")) == NULL)
|
|
|
|
err_sys ("Cannot fdopen");
|
|
|
|
sprintf (complete_request, "%s\r\n", request);
|
|
|
|
n = strlen (complete_request);
|
|
|
|
if (writen (sockfd, complete_request, n) != n)
|
|
|
|
err_sys ("writen error on socket");
|
|
|
|
/* Read from the server */
|
2004-04-06 09:49:26 +00:00
|
|
|
while ((nr = readline (files, recvline, n, 0)) > 0)
|
|
|
|
if (dump)
|
|
|
|
printf ("%s", recvline);
|
|
|
|
if (dump)
|
|
|
|
printf ("\n");
|
2004-04-05 20:56:19 +00:00
|
|
|
close (sockfd);
|
2004-04-06 20:30:06 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void terminate() {
|
2004-04-05 20:56:19 +00:00
|
|
|
}
|