New popt library for the plugins

Initial
Stephane Bortzmeyer 20 years ago
parent 1b179e9f79
commit a1d91e1af1

@ -27,6 +27,9 @@
/* Define to 1 if you have the `dl' library (-ldl). */
#undef HAVE_LIBDL
/* Define to 1 if you have the `popt' library (-lpopt). */
#undef HAVE_LIBPOPT
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H

@ -85,6 +85,7 @@ main (argc, argv)
unsigned short fill_requested = 0;
unsigned int i = 0;
char *plugin_name = NULL;
char *ext;
void *plugin;
char *dl_result;
@ -98,7 +99,7 @@ main (argc, argv)
struct sigaction mysigaction;
#endif
char port_name[NI_MAXSERV];
char *plugin_port_name, *port_name;
unsigned short port_to_use = USE_ECHO;
unsigned short http = 0;
unsigned short smtp = 0;
@ -181,6 +182,7 @@ main (argc, argv)
max = null_timeval;
min = max_timeval;
stddev = null_timeval;
port_name = malloc (NI_MAXSERV);
strcpy (port_name, ECHO_TCP_PORT);
for (i = 0; i <= MAX_ITERATIONS; i++)
@ -197,7 +199,7 @@ main (argc, argv)
{
if (result < -1)
{
err_ret ("%s: %s",
printf ("%s: %s",
poptBadOption (poptcon, POPT_BADOPTION_NOALIAS),
poptStrerror (result));
usage ();
@ -322,6 +324,7 @@ main (argc, argv)
module_find = TRUE;
break;
default:
printf ("Unknown character option %d (%c)", result, (char) result);
usage ();
}
}
@ -426,7 +429,9 @@ main (argc, argv)
leftover = (char **) &argv[argc - remaining];
if (plugin_name)
{
/* TODO: add '.so' to the plugin name if it's not already there */
ext = strstr(plugin_name, ".so");
if ((ext == NULL) || (strcmp (ext, ".so") != 0))
sprintf (plugin_name, "%s.so", plugin_name);
plugin = dlopen (plugin_name, RTLD_NOW);
if (!plugin)
{
@ -438,7 +443,11 @@ main (argc, argv)
{
err_sys ("Cannot find init in %s: %s", plugin_name, dl_result);
}
strcpy (port_name, plugin_init (remaining, (const char **) leftover));
plugin_port_name = plugin_init (remaining, (const char **) leftover);
if (plugin_port_name != NULL)
strcpy (port_name, plugin_port_name);
else
port_name = 0;
plugin_start = dlsym (plugin, "start");
dl_result = dlerror ();
if (dl_result)
@ -456,8 +465,11 @@ main (argc, argv)
{
tcp = 1;
}
if (remaining != 1)
if (remaining == 0)
usage ();
if (!module_find && remaining != 1)
{
printf ("%d args remaning, should be 1\n", remaining);
usage ();
}
if (verbose)

@ -88,7 +88,7 @@ void
usage ()
{
fprintf (stderr,
"Usage: %s [-4] [-6] [-v] [-r] [-f fill] [-t timeout] [-c] [-d] [-u] [-s size] [-n number] [-w delay] [-h url] [-i url] [-p priority] [-P tos] [-C] [-S] hostname[:port]\n",
"Usage: %s [-4] [-6] [-v] [-r] [-f fill] [-t timeout] [-c] [-d] [-u] [-s size] [-n number] [-w delay] [-h url] [-i url] [-p priority] [-P tos] [-C] [-S] [-m plugin] hostname[:port]\n",
progname);
exit (1);
}

@ -1,5 +1,7 @@
/*
* Pseudo-random plugin. Just an example. $Id$
* Pseudo-random plugin. Just an example.
*
* $Id$
*/
#include <stdlib.h>
@ -7,12 +9,17 @@
#include <time.h>
#include <unistd.h>
void
char *
init (const int argc, const char *argv[])
{
struct timeval tv;
(void) gettimeofday (&tv, (struct timezone *) NULL);
srand (tv.tv_usec);
return NULL;
}
void start ()
{
}
void

@ -10,14 +10,64 @@
#define MAX_REQUEST 256
struct addrinfo whois_server;
const char *request = "nic.fr";
const char *request = NULL;
int dump = FALSE;
int n;
int sockfd;
FILE *files = NULL;
poptContext whois_poptcon;
void
whois_usage (const char *msg)
{
if (msg)
{
printf ("Error: %s\n", msg);
}
poptPrintUsage (whois_poptcon, stdout, 0);
exit (1);
}
char *
init (const int argc, const char **argv)
{
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");
return "nicname";
}
@ -46,6 +96,10 @@ execute ()
if (writen (sockfd, complete_request, n) != n)
err_sys ("writen error on socket");
/* Read from the server */
nr = readline (files, recvline, n, 0);
while ((nr = readline (files, recvline, n, 0)) > 0)
if (dump)
printf ("%s", recvline);
if (dump)
printf ("\n");
close (sockfd);
}

Loading…
Cancel
Save