Big reformatting of everything after a change of indent options (now --no-tabs)

master
Stephane Bortzmeyer 16 years ago
parent 5dc79b9cc8
commit 0f6aefca60

File diff suppressed because it is too large Load Diff

@ -21,91 +21,91 @@
void void
domquery_usage(char *msg) domquery_usage(char *msg)
{ {
fprintf(stderr, "%s\n", msg); fprintf(stderr, "%s\n", msg);
poptPrintUsage(poptcon, stderr, 0); poptPrintUsage(poptcon, stderr, 0);
err_quit(" domain"); err_quit(" domain");
} }
char * char *
init(int argc, char **argv) init(int argc, char **argv)
{ {
int value; int value;
xmlrpc_value *result; xmlrpc_value *result;
xmlrpc_bool free, read_contacts; xmlrpc_bool free, read_contacts;
xmlrpc_int32 reason; xmlrpc_int32 reason;
char *msg, *hostname; char *msg, *hostname;
struct poptOption options[] = { struct poptOption options[] = {
{"read-contacts", 'c', POPT_ARG_NONE, &read_contacts, 0, {"read-contacts", 'c', POPT_ARG_NONE, &read_contacts, 0,
"Read also the contacts of the domain [NOT IMPLEMENTED]", "Read also the contacts of the domain [NOT IMPLEMENTED]",
""}, ""},
POPT_AUTOHELP POPT_TABLEEND POPT_AUTOHELP POPT_TABLEEND
}; };
poptcon = poptGetContext(NULL, argc, argv, options, POPT_CONTEXT_KEEP_FIRST); poptcon = poptGetContext(NULL, argc, argv, options, POPT_CONTEXT_KEEP_FIRST);
while ((value = poptGetNextOpt(poptcon)) > 0) { while ((value = poptGetNextOpt(poptcon)) > 0) {
if (value < -1) { if (value < -1) {
sprintf(msg, "%s: %s", sprintf(msg, "%s: %s",
poptBadOption(poptcon, POPT_BADOPTION_NOALIAS), poptBadOption(poptcon, POPT_BADOPTION_NOALIAS),
poptStrerror(value)); poptStrerror(value));
domquery_usage(msg); domquery_usage(msg);
} }
} }
hostname = (char *) poptGetArg(poptcon); /* Not used */ hostname = (char *) poptGetArg(poptcon); /* Not used */
domain = (char *) poptGetArg(poptcon); domain = (char *) poptGetArg(poptcon);
if (domain == NULL || !strcmp(domain, "")) if (domain == NULL || !strcmp(domain, ""))
domquery_usage("Mandatory request missing"); domquery_usage("Mandatory request missing");
return NULL; return NULL;
} }
void void
start_raw() start_raw()
{ {
/* Start up our XML-RPC client library. */ /* Start up our XML-RPC client library. */
xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, CLIENT_NAME, CLIENT_VERSION); xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, CLIENT_NAME, CLIENT_VERSION);
/* Initialize our error-handling environment. */ /* Initialize our error-handling environment. */
xmlrpc_env_init(&env); xmlrpc_env_init(&env);
printf("env initialized\n"); printf("env initialized\n");
} }
int int
execute() execute()
{ {
xmlrpc_value *result; xmlrpc_value *result;
xmlrpc_value *domain_h; xmlrpc_value *domain_h;
xmlrpc_int32 found; xmlrpc_int32 found;
xmlrpc_value *error; xmlrpc_value *error;
/* char *dst; dst = HTAnchor_findAddress(ENDPOINT); */ /* char *dst; dst = HTAnchor_findAddress(ENDPOINT); */
printf("Ready to call\n"); printf("Ready to call\n");
/* Call the server */ /* Call the server */
result = xmlrpc_client_call(&env, ENDPOINT, "domquery", "(s)", domain); result = xmlrpc_client_call(&env, ENDPOINT, "domquery", "(s)", domain);
die_if_fault_occurred(&env); die_if_fault_occurred(&env);
printf("Call done, now parsing\n"); printf("Call done, now parsing\n");
xmlrpc_parse_value(&env, result, "{s:i,*}", "found", &found); xmlrpc_parse_value(&env, result, "{s:i,*}", "found", &found);
die_if_fault_occurred(&env); die_if_fault_occurred(&env);
if (found) { if (found) {
printf("%s is there\n", domain); printf("%s is there\n", domain);
xmlrpc_parse_value(&env, result, "{s:S,i,s:A,*}", "domain", xmlrpc_parse_value(&env, result, "{s:S,i,s:A,*}", "domain",
&domain_h, "found", &found, "error", &error); &domain_h, "found", &found, "error", &error);
die_if_fault_occurred(&env); die_if_fault_occurred(&env);
} }
/* Dispose of our result value. */ /* Dispose of our result value. */
xmlrpc_DECREF(result); xmlrpc_DECREF(result);
return 0; return 0;
} }
void void
terminate() terminate()
{ {
/* Clean up our error-handling environment. */ /* Clean up our error-handling environment. */
xmlrpc_env_clean(&env); xmlrpc_env_clean(&env);
/* Shutdown our XML-RPC client library. */ /* Shutdown our XML-RPC client library. */
xmlrpc_client_cleanup(); xmlrpc_client_cleanup();
} }

@ -16,65 +16,65 @@
void void
die_if_fault_occurred(xmlrpc_env * env) die_if_fault_occurred(xmlrpc_env * env)
{ {
if (env->fault_occurred) { if (env->fault_occurred) {
fprintf(stderr, "XML-RPC Fault: %s (%d)\n", fprintf(stderr, "XML-RPC Fault: %s (%d)\n",
env->fault_string, env->fault_code); env->fault_string, env->fault_code);
exit(1); exit(1);
} }
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int value; int value;
xmlrpc_value *result; xmlrpc_value *result;
xmlrpc_bool free, read_contacts; xmlrpc_bool free, read_contacts;
xmlrpc_int32 reason; xmlrpc_int32 reason;
xmlrpc_value *domain_h; xmlrpc_value *domain_h;
xmlrpc_int32 found; xmlrpc_int32 found;
xmlrpc_value *error; xmlrpc_value *error;
xmlrpc_env env; xmlrpc_env env;
char *domain; char *domain;
char *date, *holder; char *date, *holder;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s domain\n", argv[0]); fprintf(stderr, "Usage: %s domain\n", argv[0]);
exit(1); exit(1);
} }
domain = argv[1]; domain = argv[1];
/* Start up our XML-RPC client library. */ /* Start up our XML-RPC client library. */
xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, CLIENT_NAME, CLIENT_VERSION); xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, CLIENT_NAME, CLIENT_VERSION);
/* Initialize our error-handling environment. */ /* Initialize our error-handling environment. */
xmlrpc_env_init(&env); xmlrpc_env_init(&env);
/* Call the server */ /* Call the server */
result = xmlrpc_client_call(&env, ENDPOINT, "domquery", "(s)", domain); result = xmlrpc_client_call(&env, ENDPOINT, "domquery", "(s)", domain);
die_if_fault_occurred(&env); die_if_fault_occurred(&env);
xmlrpc_parse_value(&env, result, "{s:i,*}", "found", &found); xmlrpc_parse_value(&env, result, "{s:i,*}", "found", &found);
die_if_fault_occurred(&env); die_if_fault_occurred(&env);
if (found) { if (found) {
printf("%s is there\n", domain); printf("%s is there\n", domain);
xmlrpc_parse_value(&env, result, "{s:S,s:i,s:A,*}", "domain", xmlrpc_parse_value(&env, result, "{s:S,s:i,s:A,*}", "domain",
&domain_h, "found", &found, "error", &error); &domain_h, "found", &found, "error", &error);
die_if_fault_occurred(&env); die_if_fault_occurred(&env);
/* printf ("Type of domain: %d\n", xmlrpc_value_type(domain_h)); */ /* printf ("Type of domain: %d\n", xmlrpc_value_type(domain_h)); */
xmlrpc_parse_value(&env, domain_h, "{s:s,s:s,*}", "registered", xmlrpc_parse_value(&env, domain_h, "{s:s,s:s,*}", "registered",
&date, "org", &holder); &date, "org", &holder);
die_if_fault_occurred(&env); die_if_fault_occurred(&env);
printf("Registered on %s by %s\n", date, holder); printf("Registered on %s by %s\n", date, holder);
} else { } else {
printf("Unknown domain %s\n", domain); printf("Unknown domain %s\n", domain);
} }
/* Dispose of our result value. */ /* Dispose of our result value. */
xmlrpc_DECREF(result); xmlrpc_DECREF(result);
/* Clean up our error-handling environment. */ /* Clean up our error-handling environment. */
xmlrpc_env_clean(&env); xmlrpc_env_clean(&env);
/* Shutdown our XML-RPC client library. */ /* Shutdown our XML-RPC client library. */
xmlrpc_client_cleanup(); xmlrpc_client_cleanup();
} }

@ -6,8 +6,7 @@
void void
die_if_fault_occurred(xmlrpc_env * env) die_if_fault_occurred(xmlrpc_env * env)
{ {
if (env->fault_occurred) { if (env->fault_occurred) {
err_quit("XML-RPC Fault: %s (%d)\n", err_quit("XML-RPC Fault: %s (%d)\n", env->fault_string, env->fault_code);
env->fault_string, env->fault_code); }
}
} }

@ -14,39 +14,38 @@ echoping_options options;
char * char *
init(const int argc, const char **argv, echoping_options global_options) init(const int argc, const char **argv, echoping_options global_options)
{ {
if (global_options.udp) if (global_options.udp)
err_quit err_quit("Sorry, UDP is not yet compatible with this daytime plugin");
("Sorry, UDP is not yet compatible with this daytime plugin"); options = global_options;
options = global_options; return "daytime";
return "daytime";
} }
void void
start(struct addrinfo *res) start(struct addrinfo *res)
{ {
daytime_server = *res; daytime_server = *res;
} }
int int
execute() execute()
{ {
int nr; int nr;
FILE *file; FILE *file;
#define MAX 256 #define MAX 256
char recvline[MAX]; char recvline[MAX];
if ((sockfd = if ((sockfd =
socket(daytime_server.ai_family, daytime_server.ai_socktype, socket(daytime_server.ai_family, daytime_server.ai_socktype,
daytime_server.ai_protocol)) < 0) daytime_server.ai_protocol)) < 0)
err_sys("Can't open socket"); err_sys("Can't open socket");
if (connect(sockfd, daytime_server.ai_addr, daytime_server.ai_addrlen) < 0) if (connect(sockfd, daytime_server.ai_addr, daytime_server.ai_addrlen) < 0)
err_sys("Can't connect to server"); err_sys("Can't connect to server");
if ((file = fdopen(sockfd, "r")) == NULL) if ((file = fdopen(sockfd, "r")) == NULL)
err_sys("Cannot fdopen"); err_sys("Cannot fdopen");
nr = readline(file, recvline, MAX, 1); nr = readline(file, recvline, MAX, 1);
if (options.verbose) if (options.verbose)
printf("%s", recvline); printf("%s", recvline);
close(sockfd); close(sockfd);
return 1; return 1;
} }
void void

@ -13,45 +13,43 @@ echoping_options options;
char * char *
init(const int argc, const char **argv, echoping_options global_options) init(const int argc, const char **argv, echoping_options global_options)
{ {
options = global_options; options = global_options;
/* TODO: the service returned must depend on the options */ /* TODO: the service returned must depend on the options */
return "echo"; return "echo";
} }
void void
start(struct addrinfo *res) start(struct addrinfo *res)
{ {
smallservices_server = *res; smallservices_server = *res;
} }
int int
execute() execute()
{ {
int nr; int nr;
#define MAX 256 #define MAX 256
#define TEST_STRING "test" #define TEST_STRING "test"
char result[MAX]; char result[MAX];
if ((sockfd = if ((sockfd =
socket(smallservices_server.ai_family, smallservices_server.ai_socktype, socket(smallservices_server.ai_family, smallservices_server.ai_socktype,
smallservices_server.ai_protocol)) < 0) smallservices_server.ai_protocol)) < 0)
err_sys("Can't open socket"); err_sys("Can't open socket");
if (connect if (connect
(sockfd, smallservices_server.ai_addr, (sockfd, smallservices_server.ai_addr, smallservices_server.ai_addrlen) < 0)
smallservices_server.ai_addrlen) < 0) err_sys("Can't connect to server");
err_sys("Can't connect to server"); if (write(sockfd, TEST_STRING, strlen(TEST_STRING)) != strlen(TEST_STRING))
if (write(sockfd, TEST_STRING, strlen(TEST_STRING)) != strlen(TEST_STRING)) err_sys("Cannot write");
err_sys("Cannot write"); nr = read(sockfd, result, strlen(TEST_STRING));
nr = read(sockfd, result, strlen(TEST_STRING)); if (nr != strlen(TEST_STRING))
if (nr != strlen(TEST_STRING)) err_sys("Cannot read (only %i bytes)", nr); /* TODO: the server may send
err_sys("Cannot read (only %i bytes)", nr); /* TODO: the server * the result in chunks, we
* may send the * should loop */
* result in chunks, if (strcmp(result, TEST_STRING) != 0)
* we should loop */ err_sys("Result \"%s\" is different from test string \"%s\"",
if (strcmp(result, TEST_STRING) != 0) result, TEST_STRING);
err_sys("Result \"%s\" is different from test string \"%s\"", close(sockfd);
result, TEST_STRING); return 1;
close(sockfd);
return 1;
} }
void void

File diff suppressed because it is too large Load Diff

@ -7,7 +7,7 @@
void void
my_perror() my_perror()
{ {
fprintf(stderr, " %s\n", sys_err_str()); fprintf(stderr, " %s\n", sys_err_str());
} }
/* /*
@ -22,18 +22,18 @@ my_perror()
void void
err_ret(char *str, ...) err_ret(char *str, ...)
{ {
va_list args; va_list args;
va_start(args, str); va_start(args, str);
vfprintf(stderr, str, args); vfprintf(stderr, str, args);
va_end(args); va_end(args);
my_perror(); my_perror();
fflush(stdout); fflush(stdout);
fflush(stderr); fflush(stderr);
return; return;
} }
/* /*
@ -49,14 +49,14 @@ err_ret(char *str, ...)
void void
err_quit(char *str, ...) err_quit(char *str, ...)
{ {
va_list args; va_list args;
va_start(args, str); va_start(args, str);
vfprintf(stderr, str, args); vfprintf(stderr, str, args);
fputc('\n', stderr); fputc('\n', stderr);
va_end(args); va_end(args);
exit(1); exit(1);
} }
/* /*
@ -73,23 +73,23 @@ err_quit(char *str, ...)
void void
err_sys(char *str, ...) err_sys(char *str, ...)
{ {
va_list args; va_list args;
va_start(args, str); va_start(args, str);
vfprintf(stderr, str, args); vfprintf(stderr, str, args);
va_end(args); va_end(args);
my_perror(); my_perror();
exit(1); exit(1);
} }
void void
usage(poptContext context) usage(poptContext context)
{ {
poptPrintUsage(context, stderr, 0); poptPrintUsage(context, stderr, 0);
fprintf(stderr, " hostname [plugin-options...]\n"); fprintf(stderr, " hostname [plugin-options...]\n");
exit(1); exit(1);
} }
/* /*
@ -105,12 +105,12 @@ usage(poptContext context)
char * char *
sys_err_str() sys_err_str()
{ {
static char msgstr[200]; static char msgstr[200];
if (errno != 0) { if (errno != 0) {
sprintf(msgstr, "(%s)", strerror(errno)); sprintf(msgstr, "(%s)", strerror(errno));
} else { } else {
msgstr[0] = '\0'; msgstr[0] = '\0';
} }
return (msgstr); return (msgstr);
} }

@ -11,149 +11,136 @@ char big_recvline[MAXTOREAD];
char * char *
make_http_sendline(char *url, char *host, int port, int nocache) make_http_sendline(char *url, char *host, int port, int nocache)
{ {
short sport = (short) port; short sport = (short) port;
int size = 350; /* Enough? RFC 2616, section 3.2.1 says 255 int size = 350; /* Enough? RFC 2616, section 3.2.1 says 255 should
* should be enough, although there is no * be enough, although there is no hard limit. We
* hard limit. We reserve more because there * reserve more because there * are the protocol
* * are the protocol elements, the HTTP * elements, the HTTP headers, etc */
* headers, etc */ char *sendline = (char *) malloc(size);
char *sendline = (char *) malloc(size); char *hostname = (char *) malloc(size);
char *hostname = (char *) malloc(size); char *cache_directive = "";
char *cache_directive = ""; int result;
int result;
#ifdef HTTP10 #ifdef HTTP10
if (nocache) if (nocache)
cache_directive = "Pragma: no-cache\r\n"; /* RFC 1945, cache_directive = "Pragma: no-cache\r\n"; /* RFC 1945, "Hypertext
* "Hypertext * Transfer Protocol * --
* Transfer Protocol * HTTP/1.0" */
* * -- HTTP/1.0" */ result = snprintf(sendline, size,
result = snprintf(sendline, size, "GET %s HTTP/1.0\r\nUser-Agent: Echoping/%s\r\n%s\r\n",
"GET %s HTTP/1.0\r\nUser-Agent: Echoping/%s\r\n%s\r\n", url, VERSION, cache_directive);
url, VERSION, cache_directive);
#else #else
if (nocache) { if (nocache) {
if (nocache == 1) if (nocache == 1)
cache_directive = "Cache-control: max-age=0\r\n"; /* Simply cache_directive = "Cache-control: max-age=0\r\n"; /* Simply force a
* force * recheck with
* a * the server */
* recheck else
* with cache_directive = "Cache-control: no-cache\r\n"; /* RFC 2616
* the * "Hypertext
* server * Transfer Protocol
*/ * -- HTTP/1.1" */
else }
cache_directive = "Cache-control: no-cache\r\n"; /* RFC strncpy(hostname, HTParse(url, "", PARSE_HOST), size); /* See bug #1688940
* 2616 * to see why we use
* "Hypertext * * * strNcpy. */
* Transfer hostname[size] = '\0'; /* Not added automatically */
* Protocol -- if (!strcmp(hostname, ""))
* HTTP/1.1" */ snprintf(hostname, size, "%s:%d", host, sport);
} result = snprintf(sendline, size,
strncpy(hostname, HTParse(url, "", PARSE_HOST), size); /* See bug #1688940 "GET %s HTTP/1.1\r\nUser-Agent: Echoping/%s\r\nHost: %s\r\nConnection: close\r\n%s\r\n",
* to see why we use url, VERSION, hostname, cache_directive);
* * strNcpy. */ free(hostname);
hostname[size] = '\0'; /* Not added automatically */
if (!strcmp(hostname, ""))
snprintf(hostname, size, "%s:%d", host, sport);
result = snprintf(sendline, size,
"GET %s HTTP/1.1\r\nUser-Agent: Echoping/%s\r\nHost: %s\r\nConnection: close\r\n%s\r\n",
url, VERSION, hostname, cache_directive);
free(hostname);
#endif #endif
if (result >= size) if (result >= size)
err_quit("URL and/or hostname too long(s)"); err_quit("URL and/or hostname too long(s)");
return sendline; return sendline;
} }
int int
read_from_server(CHANNEL fs, short ssl, boolean accept_redirects) read_from_server(CHANNEL fs, short ssl, boolean accept_redirects)
{ {
int nr = 0; int nr = 0;
int total = 0; int total = 0;
char reply_code; char reply_code;
int first_line = TRUE; int first_line = TRUE;
short body = FALSE; short body = FALSE;
#ifdef OPENSSL #ifdef OPENSSL
int sslcode; int sslcode;
#endif #endif
while (!body && !timeout_flag) { while (!body && !timeout_flag) {
if (!ssl) if (!ssl)
nr = readline(fs.fs, big_recvline, MAXTOREAD, TRUE); nr = readline(fs.fs, big_recvline, MAXTOREAD, TRUE);
#ifdef OPENSSL #ifdef OPENSSL
else { else {
nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, TRUE); nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, TRUE);
if (nr == -1) { if (nr == -1) {
sslcode = ERR_get_error(); sslcode = ERR_get_error();
err_ret("SSL_readline error: %s", err_ret("SSL_readline error: %s", ERR_error_string(sslcode, NULL));
ERR_error_string(sslcode, NULL)); }
} }
}
#endif #endif
#ifdef GNUTLS #ifdef GNUTLS
else else
{ {
nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, TRUE); nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, TRUE);
if (nr == -1) { if (nr == -1) {
err_ret("TLS_readline error: %s", err_ret("TLS_readline error: %s", gnutls_strerror(nr));
gnutls_strerror(nr)); }
} }
}
#endif #endif
/* /*
* printf ("DEBUG: reading \"%s\"\n (%d chars)\n", * printf ("DEBUG: reading \"%s\"\n (%d chars)\n",
* big_recvline, nr); * big_recvline, nr);
*/ */
/* /*
* HTTP replies should be separated by CR-LF. Unfortunately, * HTTP replies should be separated by CR-LF. Unfortunately,
* some servers send only CR :-( * some servers send only CR :-(
*/ */
body = ((nr == 2) || (nr == 1)); /* Empty line CR-LF seen */ body = ((nr == 2) || (nr == 1)); /* Empty line CR-LF seen */
if ((nr < 1) && (timeout_flag)) /* Probably a timeout */ if ((nr < 1) && (timeout_flag)) /* Probably a timeout */
return -1; return -1;
if (nr < 1) if (nr < 1)
/* SourceForge bug #109385 */ /* SourceForge bug #109385 */
/* err_sys ("Error reading HTTP header"); */ /* err_sys ("Error reading HTTP header"); */
return -1; return -1;
/* /*
* if ((int) big_recvline[nr-1] == 10) nr--; * if ((int) big_recvline[nr-1] == 10) nr--;
*/ */
if (first_line) { if (first_line) {
reply_code = big_recvline[9]; /* 9 because "HTTP/1.x reply_code = big_recvline[9]; /* 9 because "HTTP/1.x 200..." */
* 200..." */ if (reply_code != '2' && !(reply_code == '3' && accept_redirects))
if (reply_code != '2' /*
&& !(reply_code == '3' && accept_redirects)) * Status codes beginning with 3 are not
/* * errors See bug #850674 and RFC 2616,
* Status codes beginning with 3 are not * section 10.3
* errors See bug #850674 and RFC 2616, */
* section 10.3 err_quit("HTTP error \"%s\"", big_recvline);
*/ }
err_quit("HTTP error \"%s\"", big_recvline); total = total + nr;
} first_line = FALSE;
total = total + nr; }
first_line = FALSE; /* Read the body */
} if (!ssl)
/* Read the body */ nr = readline(fs.fs, big_recvline, MAXTOREAD, FALSE);
if (!ssl)
nr = readline(fs.fs, big_recvline, MAXTOREAD, FALSE);
#ifdef OPENSSL #ifdef OPENSSL
else else
nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, FALSE); nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, FALSE);
#endif #endif
#ifdef GNUTLS #ifdef GNUTLS
else else
nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, FALSE); nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, FALSE);
#endif #endif
/* /*
* printf ("DEBUG: reading body \"%s\"\n (%d chars)\n", big_recvline, * printf ("DEBUG: reading body \"%s\"\n (%d chars)\n", big_recvline,
* nr); * nr);
*/ */
if ((nr < 2) && (timeout_flag)) /* Probably a timeout */ if ((nr < 2) && (timeout_flag)) /* Probably a timeout */
return -1; return -1;
if (nr < 2) /* Hmm, if the body is empty, we'll get a * * * * * if (nr < 2) /* Hmm, if the body is empty, we'll get a * * * * *
* * meaningless error message */ * * * meaningless error message */
err_sys("Error reading HTTP body"); err_sys("Error reading HTTP body");
total = total + nr; total = total + nr;
return total; /* How to do if we want only the body's size? */ return total; /* How to do if we want only the body's size? */
} }
#endif /* HTTP */ #endif /* HTTP */

@ -13,94 +13,94 @@
void * void *
make_icp_sendline(url, shost, opcode, length) make_icp_sendline(url, shost, opcode, length)
const char *url; const char *url;
u_num32 *shost; u_num32 *shost;
icp_opcode opcode; icp_opcode opcode;
int *length; int *length;
{ {
icp_common_t *headerp = NULL; icp_common_t *headerp = NULL;
char *buf = NULL; char *buf = NULL;
char *urloffset = NULL; char *urloffset = NULL;
u_num32 flags = ICP_FLAG_SRC_RTT; u_num32 flags = ICP_FLAG_SRC_RTT;
u_num32 pad = 0; u_num32 pad = 0;
u_num32 reqnum = 4711; u_num32 reqnum = 4711;
unsigned short buf_len; unsigned short buf_len;
buf_len = sizeof(icp_common_t) + strlen(url) + 1; buf_len = sizeof(icp_common_t) + strlen(url) + 1;
if (opcode == ICP_OP_QUERY) if (opcode == ICP_OP_QUERY)
buf_len += sizeof(u_num32); buf_len += sizeof(u_num32);
buf = calloc(buf_len, 1); buf = calloc(buf_len, 1);
headerp = (icp_common_t *) buf; headerp = (icp_common_t *) buf;
headerp->opcode = opcode; headerp->opcode = opcode;
headerp->version = ICP_VERSION_CURRENT; headerp->version = ICP_VERSION_CURRENT;
headerp->length = htons(buf_len); headerp->length = htons(buf_len);
headerp->reqnum = htonl(reqnum); headerp->reqnum = htonl(reqnum);
headerp->flags = htonl(flags); headerp->flags = htonl(flags);
headerp->pad = pad; headerp->pad = pad;
if (shost) if (shost)
headerp->shostid = htonl(*shost); headerp->shostid = htonl(*shost);
/* urloffset = (char *) ((int) buf + sizeof(icp_common_t)); */ /* urloffset = (char *) ((int) buf + sizeof(icp_common_t)); */
urloffset = (char *) (buf + sizeof(icp_common_t)); urloffset = (char *) (buf + sizeof(icp_common_t));
if (opcode == ICP_OP_QUERY) if (opcode == ICP_OP_QUERY)
urloffset += sizeof(u_num32); urloffset += sizeof(u_num32);
memcpy(urloffset, url, strlen(url)); memcpy(urloffset, url, strlen(url));
*length = buf_len; *length = buf_len;
return buf; return buf;
} }
int int
recv_icp(sockfd, buf, retcode) recv_icp(sockfd, buf, retcode)
int sockfd; int sockfd;
char *buf; char *buf;
char *retcode; char *retcode;
{ /* { /*
* based on draft-wessels-icp-v2-02.txt * based on draft-wessels-icp-v2-02.txt
*/ */
icp_common_t *headerp = (icp_common_t *) buf; icp_common_t *headerp = (icp_common_t *) buf;
int nr, length; int nr, length;
unsigned char opcode; unsigned char opcode;
static char *icp_op_code[] = { static char *icp_op_code[] = {
/* 0 */ "ICP_OP_INVALID", /* 0 */ "ICP_OP_INVALID",
/* 1 */ "ICP_OP_QUERY", /* 1 */ "ICP_OP_QUERY",
/* 2 */ "ICP_OP_HIT", /* 2 */ "ICP_OP_HIT",
/* 3 */ "ICP_OP_MISS", /* 3 */ "ICP_OP_MISS",
/* 4 */ "ICP_OP_ERR", /* 4 */ "ICP_OP_ERR",
/* 5 */ "", /* 5 */ "",
/* 6 */ "", /* 6 */ "",
/* 7 */ "", /* 7 */ "",
/* 8 */ "", /* 8 */ "",
/* 9 */ "", /* 9 */ "",
/* 10 */ "ICP_OP_SECHO", /* 10 */ "ICP_OP_SECHO",
/* 11 */ "ICP_OP_DECHO", /* 11 */ "ICP_OP_DECHO",
/* 12 */ "", /* 12 */ "",
/* 13 */ "", /* 13 */ "",
/* 14 */ "", /* 14 */ "",
/* 15 */ "", /* 15 */ "",
/* 16 */ "", /* 16 */ "",
/* 17 */ "", /* 17 */ "",
/* 18 */ "", /* 18 */ "",
/* 19 */ "", /* 19 */ "",
/* 20 */ "", /* 20 */ "",
/* 21 */ "ICP_OP_MISS_NOFETCH", /* 21 */ "ICP_OP_MISS_NOFETCH",
/* 22 */ "ICP_OP_DENIED", /* 22 */ "ICP_OP_DENIED",
/* 23 */ "ICP_OP_HIT_OBJ" /* 23 */ "ICP_OP_HIT_OBJ"
}; };
nr = recvfrom(sockfd, buf, DEFLINE, 0, (struct sockaddr *) 0, (int *) 0); nr = recvfrom(sockfd, buf, DEFLINE, 0, (struct sockaddr *) 0, (int *) 0);
if (nr < 0) { if (nr < 0) {
if (timeout_flag) if (timeout_flag)
err_quit("Timeout while reading"); err_quit("Timeout while reading");
else else
err_sys("No reply from ICP proxy server"); err_sys("No reply from ICP proxy server");
} }
opcode = headerp->opcode; opcode = headerp->opcode;
length = ntohs(headerp->length); length = ntohs(headerp->length);
sprintf(retcode, "ICP reply: \42%s\42", icp_op_code[opcode]); sprintf(retcode, "ICP reply: \42%s\42", icp_op_code[opcode]);
return length; return length;
} }
#endif /* ICP */ #endif /* ICP */

@ -35,180 +35,178 @@ boolean no_recurse = FALSE;
****************************************************************/ ****************************************************************/
int int
nsError(error, domain) nsError(error, domain)
int error; int error;
char *domain; char *domain;
{ {
switch (error) { switch (error) {
case HOST_NOT_FOUND: case HOST_NOT_FOUND:
err_ret("Unknown domain: %s\n", domain); err_ret("Unknown domain: %s\n", domain);
return -1; return -1;
case NO_DATA: case NO_DATA:
err_ret("No records of type %s for %s in the Answer section\n", err_ret("No records of type %s for %s in the Answer section\n",
to_upper(type_name), domain); to_upper(type_name), domain);
return -1; return -1;
case TRY_AGAIN: case TRY_AGAIN:
err_ret("No response for query\n"); err_ret("No response for query\n");
return -2; return -2;
default: default:
err_ret("Unexpected error\n"); err_ret("Unexpected error\n");
return -1; return -1;
} }
} }
void void
dns_usage(const char *msg) dns_usage(const char *msg)
{ {
if (msg) { if (msg) {
fprintf(stderr, "Error: %s\n", msg); fprintf(stderr, "Error: %s\n", msg);
} }
poptPrintUsage(dns_poptcon, stderr, 0); poptPrintUsage(dns_poptcon, stderr, 0);
fprintf(stderr, " request\n"); fprintf(stderr, " request\n");
exit(1); exit(1);
} }
char * char *
init(const int argc, const char **argv) init(const int argc, const char **argv)
{ {
int value; int value;
char *hostname; char *hostname;
char *msg = malloc(256); char *msg = malloc(256);
char *upper_type_name = NULL; char *upper_type_name = NULL;
/* popt variables */ /* popt variables */
struct poptOption options[] = { struct poptOption options[] = {
{"type", 't', POPT_ARG_STRING, &type_name, 0, {"type", 't', POPT_ARG_STRING, &type_name, 0,
"Type of resources queried (A, MX, SOA, etc)", "Type of resources queried (A, MX, SOA, etc)",
"type"}, "type"},
{"tcp", (char) NULL, POPT_ARG_NONE, &use_tcp, 0, {"tcp", (char) NULL, POPT_ARG_NONE, &use_tcp, 0,
"Use TCP for the request (virtual circuit)", "Use TCP for the request (virtual circuit)",
"tcp"}, "tcp"},
{"no-recurse", (char) NULL, POPT_ARG_NONE, &no_recurse, 0, {"no-recurse", (char) NULL, POPT_ARG_NONE, &no_recurse, 0,
"Do not ask recursion", "Do not ask recursion",
"no-recurse"}, "no-recurse"},
POPT_AUTOHELP POPT_TABLEEND POPT_AUTOHELP POPT_TABLEEND
}; };
dns_poptcon = poptGetContext(NULL, argc, dns_poptcon = poptGetContext(NULL, argc, argv, options, POPT_CONTEXT_KEEP_FIRST);
argv, options, POPT_CONTEXT_KEEP_FIRST); while ((value = poptGetNextOpt(dns_poptcon)) > 0) {
while ((value = poptGetNextOpt(dns_poptcon)) > 0) { }
} if (value < -1) {
if (value < -1) { sprintf(msg, "%s: %s",
sprintf(msg, "%s: %s", poptBadOption(dns_poptcon, POPT_BADOPTION_NOALIAS),
poptBadOption(dns_poptcon, POPT_BADOPTION_NOALIAS), poptStrerror(value));
poptStrerror(value)); dns_usage(msg);
dns_usage(msg); }
} hostname = (char *) poptGetArg(dns_poptcon); /* Not used */
hostname = (char *) poptGetArg(dns_poptcon); /* Not used */ request = (char *) poptGetArg(dns_poptcon);
request = (char *) poptGetArg(dns_poptcon); if (request == NULL)
if (request == NULL) dns_usage("Mandatory request missing");
dns_usage("Mandatory request missing"); if ((type_name == NULL) || !strcmp(type_name, "")) {
if ((type_name == NULL) || !strcmp(type_name, "")) { type = T_A;
type = T_A; type_name = "A";
type_name = "A"; } else {
} else { upper_type_name = (char *) to_upper(type_name);
upper_type_name = (char *) to_upper(type_name); /*
/* * TODO: a better algorithm. Use dns_rdatatype_fromtext in
* TODO: a better algorithm. Use dns_rdatatype_fromtext in * BIND ?
* BIND ? */
*/ if (!strcmp(upper_type_name, "A"))
if (!strcmp(upper_type_name, "A")) type = T_A;
type = T_A; else if (!strcmp(upper_type_name, "AAAA"))
else if (!strcmp(upper_type_name, "AAAA")) type = T_AAAA;
type = T_AAAA; else if (!strcmp(upper_type_name, "NS"))
else if (!strcmp(upper_type_name, "NS")) type = T_NS;
type = T_NS; else if (!strcmp(upper_type_name, "SOA"))
else if (!strcmp(upper_type_name, "SOA")) type = T_SOA;
type = T_SOA; else if (!strcmp(upper_type_name, "MX"))
else if (!strcmp(upper_type_name, "MX")) type = T_MX;
type = T_MX; else if (!strcmp(upper_type_name, "SRV"))
else if (!strcmp(upper_type_name, "SRV")) type = T_SRV;
type = T_SRV; else if (!strcmp(upper_type_name, "CNAME"))
else if (!strcmp(upper_type_name, "CNAME")) type = T_CNAME;
type = T_CNAME; else if (!strcmp(upper_type_name, "PTR"))
else if (!strcmp(upper_type_name, "PTR")) type = T_PTR;
type = T_PTR; else if (!strcmp(upper_type_name, "TXT"))
else if (!strcmp(upper_type_name, "TXT")) type = T_TXT;
type = T_TXT; else if (!strcmp(upper_type_name, "ANY"))
else if (!strcmp(upper_type_name, "ANY")) type = T_ANY;
type = T_ANY; else
else dns_usage("Unknown type");
dns_usage("Unknown type"); }
} return "domain";
return "domain";
} }
void void
start(struct addrinfo *res) start(struct addrinfo *res)
{ {
struct sockaddr name_server_sockaddr; struct sockaddr name_server_sockaddr;
struct sockaddr_in name_server_sockaddr_in; struct sockaddr_in name_server_sockaddr_in;
struct sockaddr_in6 name_server_sockaddr_in6; struct sockaddr_in6 name_server_sockaddr_in6;
name_server = *res; name_server = *res;
name_server_sockaddr = *name_server.ai_addr; name_server_sockaddr = *name_server.ai_addr;
if (name_server_sockaddr.sa_family == AF_INET) { if (name_server_sockaddr.sa_family == AF_INET) {
/* Converts a generic sockaddr to an IPv4 sockaddr_in */ /* Converts a generic sockaddr to an IPv4 sockaddr_in */
(void) memcpy((void *) &name_server_sockaddr_in, (void) memcpy((void *) &name_server_sockaddr_in,
&name_server_sockaddr, sizeof(struct sockaddr)); &name_server_sockaddr, sizeof(struct sockaddr));
} else if (name_server_sockaddr.sa_family == AF_INET6) { } else if (name_server_sockaddr.sa_family == AF_INET6) {
#ifdef HAVE_RES_EXT #ifdef HAVE_RES_EXT
/* TODO: the code for IPv6 servers is hopelessly broken. Start again /* TODO: the code for IPv6 servers is hopelessly broken. Start again */
*/ fprintf(stderr,
fprintf(stderr, "WARNING: IPv6 nameservers not really supported yet (experts may apply). Falling back to IPv4 and the default server. You may use -4, too\n");
"WARNING: IPv6 nameservers not really supported yet (experts may apply). Falling back to IPv4 and the default server. You may use -4, too\n"); /* Converts a generic sockaddr to an IPv6 sockaddr_in6 */
/* Converts a generic sockaddr to an IPv6 sockaddr_in6 */ (void) memcpy((void *) &name_server_sockaddr_in6,
(void) memcpy((void *) &name_server_sockaddr_in6, &name_server_sockaddr, sizeof(struct sockaddr));
&name_server_sockaddr, sizeof(struct sockaddr));
#else #else
err_quit err_quit
("IPv6 name servers not supported on this platform, may be you should use the -4 option"); ("IPv6 name servers not supported on this platform, may be you should use the -4 option");
#endif #endif
} else { } else {
err_quit("Unknown family for address of the server"); err_quit("Unknown family for address of the server");
} }
if (res_init() < 0) if (res_init() < 0)
err_sys("res_init"); err_sys("res_init");
if (name_server_sockaddr.sa_family == AF_INET) { if (name_server_sockaddr.sa_family == AF_INET) {
_res.nsaddr_list[0] = name_server_sockaddr_in; _res.nsaddr_list[0] = name_server_sockaddr_in;
} else if (name_server_sockaddr.sa_family == AF_INET6) { } else if (name_server_sockaddr.sa_family == AF_INET6) {
#ifdef HAVE_RES_EXT #ifdef HAVE_RES_EXT
/* TODO: completely broken, dioes not work. Check in Stevens */ /* TODO: completely broken, dioes not work. Check in Stevens */
(void) memcpy(_res_ext.nsaddr_list, &name_server_sockaddr_in6, (void) memcpy(_res_ext.nsaddr_list, &name_server_sockaddr_in6,
sizeof(struct sockaddr_in6)); sizeof(struct sockaddr_in6));
#endif #endif
} }
_res.nscount = 1; _res.nscount = 1;
_res.options &= ~(RES_DNSRCH | RES_DEFNAMES | RES_NOALIASES); _res.options &= ~(RES_DNSRCH | RES_DEFNAMES | RES_NOALIASES);
if (use_tcp) { if (use_tcp) {
_res.options |= RES_USEVC; _res.options |= RES_USEVC;
} }
if (no_recurse) { if (no_recurse) {
_res.options &= ~RES_RECURSE; _res.options &= ~RES_RECURSE;
} }
} }
int int
execute() execute()
{ {
union { union {
HEADER hdr; /* defined in resolv.h */ HEADER hdr; /* defined in resolv.h */
u_char buf[PACKETSZ]; /* defined in arpa/nameser.h */ u_char buf[PACKETSZ]; /* defined in arpa/nameser.h */
} response; /* response buffers */ } response; /* response buffers */
int response_length; /* buffer length */ int response_length; /* buffer length */
if ((response_length = res_query(request, /* the domain we care about */ if ((response_length = res_query(request, /* the domain we care about */
C_IN, /* Internet class records */ C_IN, /* Internet class records */
type, (u_char *) & response, /* response type, (u_char *) & response, /* response
* buffer */ * buffer */
sizeof(response))) /* buffer size */ sizeof(response))) /* buffer size */
<0) { /* If negative */ <0) { /* If negative */
nsError(h_errno, request); /* report the error */ nsError(h_errno, request); /* report the error */
if (h_errno == TRY_AGAIN) if (h_errno == TRY_AGAIN)
return -1; /* More luck next time? */ return -1; /* More luck next time? */
else else
return -2; /* Give in */ return -2; /* Give in */
} }
/* /*
* TODO: better analysis of the replies. For instance, replies can be * TODO: better analysis of the replies. For instance, replies can be
* in the authority section (delegation info) * in the authority section (delegation info)
*/ */
return 0; return 0;
} }
void void

@ -23,146 +23,141 @@ echoping_options global_options;
void void
ldap_usage(const char *msg) ldap_usage(const char *msg)
{ {
if (msg) { if (msg) {
printf("LDAP plugin error: %s\n", msg); printf("LDAP plugin error: %s\n", msg);
} }
poptPrintUsage(ldap_poptcon, stdout, 0); poptPrintUsage(ldap_poptcon, stdout, 0);
exit(1); exit(1);
} }
char * char *
init(const int argc, const char **argv, init(const int argc, const char **argv,
const echoping_options global_external_options) const echoping_options global_external_options)
{ {
int value; int value;
char *msg = malloc(MAX_LINE); char *msg = malloc(MAX_LINE);
char *rest, *port_text; char *rest, *port_text;
char *scope_string = NULL; char *scope_string = NULL;
/* popt variables */ /* popt variables */
struct poptOption options[] = { struct poptOption options[] = {
{"request", 'r', POPT_ARG_STRING, &request, 0, {"request", 'r', POPT_ARG_STRING, &request, 0,
"Request (filter) to send to the LDAP server", "r"}, "Request (filter) to send to the LDAP server", "r"},
{"base", 'b', POPT_ARG_STRING, &base, 0, {"base", 'b', POPT_ARG_STRING, &base, 0,
"Base of the LDAP tree", "b"}, "Base of the LDAP tree", "b"},
{"scope", 's', POPT_ARG_STRING, &scope_string, 0, {"scope", 's', POPT_ARG_STRING, &scope_string, 0,
"Scope of the search in the LDAP tree (sub, one or base)", "s"}, "Scope of the search in the LDAP tree (sub, one or base)", "s"},
{"port", 'p', POPT_ARG_INT, &port, 0, {"port", 'p', POPT_ARG_INT, &port, 0,
"TCP port to connect to the LDAP server", "p"}, "TCP port to connect to the LDAP server", "p"},
POPT_AUTOHELP POPT_TABLEEND POPT_AUTOHELP POPT_TABLEEND
}; };
global_options = global_external_options; global_options = global_external_options;
if (global_options.udp) if (global_options.udp)
err_quit("UDP makes no sense for a LDAP connection"); err_quit("UDP makes no sense for a LDAP connection");
ldap_poptcon = poptGetContext(NULL, argc, ldap_poptcon = poptGetContext(NULL, argc,
argv, options, POPT_CONTEXT_KEEP_FIRST); argv, options, POPT_CONTEXT_KEEP_FIRST);
while ((value = poptGetNextOpt(ldap_poptcon)) > 0) { while ((value = poptGetNextOpt(ldap_poptcon)) > 0) {
} }
if (value < -1) { if (value < -1) {
sprintf(msg, "%s: %s", sprintf(msg, "%s: %s",
poptBadOption(ldap_poptcon, POPT_BADOPTION_NOALIAS), poptBadOption(ldap_poptcon, POPT_BADOPTION_NOALIAS),
poptStrerror(value)); poptStrerror(value));
ldap_usage(msg); ldap_usage(msg);
} }
if (port == 0) if (port == 0)
port = LDAP_PORT; port = LDAP_PORT;
hostname = poptGetArg(ldap_poptcon); hostname = poptGetArg(ldap_poptcon);
rest = poptGetArg(ldap_poptcon); rest = poptGetArg(ldap_poptcon);
if (rest != NULL) { if (rest != NULL) {
fprintf(stderr, "%s: ", rest); fprintf(stderr, "%s: ", rest);
ldap_usage("Additional arguments"); ldap_usage("Additional arguments");
} }
if (base == NULL) if (base == NULL)
base = ""; base = "";
if (request == NULL || !strcmp(request, "")) if (request == NULL || !strcmp(request, ""))
request = "(objectclass=*)"; /* Default mentioned in OpenLDAP request = "(objectclass=*)"; /* Default mentioned in OpenLDAP
* documentation. Joerg Roth fears * documentation. Joerg Roth fears that it
* that it may trigger "Size limit * may trigger "Size limit exceeded" if
* exceeded" if there are many * there are many objects at this node. RFC
* objects at this node. RFC 4515 * 4515 seems silent here. */
* seems silent here. */ if (scope_string != NULL) {
if (scope_string != NULL) { scope_string = (char *) to_upper(scope_string);
scope_string = (char *) to_upper(scope_string); if (!strcmp(scope_string, "BASE"))
if (!strcmp(scope_string, "BASE")) scope = LDAP_SCOPE_BASE;
scope = LDAP_SCOPE_BASE; else if (!strcmp(scope_string, "SUB"))
else if (!strcmp(scope_string, "SUB")) scope = LDAP_SCOPE_SUBTREE;
scope = LDAP_SCOPE_SUBTREE; else if (!strcmp(scope_string, "ONE"))
else if (!strcmp(scope_string, "ONE")) scope = LDAP_SCOPE_ONELEVEL;
scope = LDAP_SCOPE_ONELEVEL; else
else err_quit("Invalid scope \"%s\"", scope_string);
err_quit("Invalid scope \"%s\"", scope_string); }
} if (port == LDAP_PORT) {
if (port == LDAP_PORT) { return "ldap";
return "ldap"; } else {
} else { port_text = malloc(99);
port_text = malloc(99); sprintf(port_text, "%d", port);
sprintf(port_text, "%d", port); return port_text;
return port_text; }
}
} }
void void
start() start()
{ {
int result; int result;
LDAPMessage *response; LDAPMessage *response;
session = ldap_init(hostname, port); session = ldap_init(hostname, port);
if (session == NULL) if (session == NULL)
err_sys("Cannot initialize LDAP"); err_sys("Cannot initialize LDAP");
/* TODO: allow non-anonymous connections, with ldap_bind_simple_s */ /* TODO: allow non-anonymous connections, with ldap_bind_simple_s */
/* /*
* Unfortunately, ldap_init does not connect to the LDAP server. So * Unfortunately, ldap_init does not connect to the LDAP server. So
* connection errors (e.g. firewall), will not be detected here and * connection errors (e.g. firewall), will not be detected here and
* loop will go on. * loop will go on.
* *
* To quote the man page: ldap_init() acts just like ldap_open(), but * To quote the man page: ldap_init() acts just like ldap_open(), but
* does not open a connection to the LDAP server. The actual * does not open a connection to the LDAP server. The actual
* connection open will occur when the first operation is attempted. * connection open will occur when the first operation is attempted.
* At this time, ldap_init() is preferred. ldap_open() will be * At this time, ldap_init() is preferred. ldap_open() will be
* depreciated in a later release. * depreciated in a later release.
* *
* So, we perform a dummy search immediately. * So, we perform a dummy search immediately.
* *
* See #1879652 for why we use "dummystuff" and not "*" * See #1879652 for why we use "dummystuff" and not "*"
* *
*/ */
result = ldap_search_s(session, base, LDAP_SCOPE_ONELEVEL, "(objectClass=dummystuff)", NULL, /* Return result = ldap_search_s(session, base, LDAP_SCOPE_ONELEVEL, "(objectClass=dummystuff)", NULL, /* Return
* * all * * * all *
* * * * *
* attributes * attributes
*/ */
1, &response); 1, &response);
if (result != 0) { if (result != 0) {
err_quit err_quit
("Cannot connect to %s (no LDAP server or wrong base, probably): %s", ("Cannot connect to %s (no LDAP server or wrong base, probably): %s",
hostname, ldap_err2string(result)); hostname, ldap_err2string(result));
} }
} }
int int
execute() execute()
{ {
int result; int result;
LDAPMessage *response; LDAPMessage *response;
result = ldap_search_s(session, base, scope, request, NULL, /* Return result = ldap_search_s(session, base, scope, request, NULL, /* Return all
* all * attributes */
* attributes 0, /* Return attribute types *and* values */
*/ &response);
0, /* Return attribute types *and* values */ if (result != 0) {
&response); err_ret("Cannot search \"%s\": %s", request, ldap_err2string(result));
if (result != 0) { return -1;
err_ret("Cannot search \"%s\": %s", request, }
ldap_err2string(result)); if (global_options.verbose)
return -1; printf("Retrieved: %d entries\n", ldap_count_entries(session, response));
} return 0;
if (global_options.verbose)
printf("Retrieved: %d entries\n",
ldap_count_entries(session, response));
return 0;
} }
void void
terminate() terminate()
{ {
ldap_unbind_s(session); ldap_unbind_s(session);
} }

@ -28,120 +28,116 @@ echoping_options global_options;
void void
postgresql_usage(const char *msg) postgresql_usage(const char *msg)
{ {
if (msg) { if (msg) {
printf("PostgreSQL plugin error: %s\n", msg); printf("PostgreSQL plugin error: %s\n", msg);
} }
poptPrintUsage(postgresql_poptcon, stdout, 0); poptPrintUsage(postgresql_poptcon, stdout, 0);
fprintf(stderr, " [SQL-request]\n"); fprintf(stderr, " [SQL-request]\n");
exit(1); exit(1);
} }
char * char *
init(const int argc, const char **argv, init(const int argc, const char **argv,
const echoping_options global_external_options) const echoping_options global_external_options)
{ {
int value; int value;
char *msg = malloc(256); char *msg = malloc(256);
char *rest; char *rest;
/* popt variables */ /* popt variables */
struct poptOption options[] = { struct poptOption options[] = {
{"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0, {"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0,
"Connection information for the Postgresql server. Something like 'host=foo dbname=bar''", "Connection information for the Postgresql server. Something like 'host=foo dbname=bar''",
""}, ""},
{"readall", 'a', POPT_ARG_NONE, &readall, 0, {"readall", 'a', POPT_ARG_NONE, &readall, 0,
"Read all the data sent by the Postgresql server", "Read all the data sent by the Postgresql server",
""}, ""},
{"connect-each-time", 'e', POPT_ARG_NONE, &connect_each_time, 0, {"connect-each-time", 'e', POPT_ARG_NONE, &connect_each_time, 0,
"(Re)Connect to the Postgresql server at each iteration", "(Re)Connect to the Postgresql server at each iteration",
""}, ""},
POPT_AUTOHELP POPT_TABLEEND POPT_AUTOHELP POPT_TABLEEND
}; };
global_options = global_external_options; global_options = global_external_options;
if (global_options.udp) if (global_options.udp)
err_quit("UDP makes no sense for a PostgreSQL connection"); err_quit("UDP makes no sense for a PostgreSQL connection");
postgresql_poptcon = poptGetContext(NULL, argc, postgresql_poptcon = poptGetContext(NULL, argc,
argv, options, argv, options, POPT_CONTEXT_POSIXMEHARDER);
POPT_CONTEXT_POSIXMEHARDER); while ((value = poptGetNextOpt(postgresql_poptcon)) > 0) {
while ((value = poptGetNextOpt(postgresql_poptcon)) > 0) { }
} if (value < -1) {
if (value < -1) { sprintf(msg, "%s: %s",
sprintf(msg, "%s: %s", poptBadOption(postgresql_poptcon, POPT_BADOPTION_NOALIAS),
poptBadOption(postgresql_poptcon, POPT_BADOPTION_NOALIAS), poptStrerror(value));
poptStrerror(value)); postgresql_usage(msg);
postgresql_usage(msg); }
} request = poptGetArg(postgresql_poptcon);
request = poptGetArg(postgresql_poptcon); if (request == NULL)
if (request == NULL) request = "SELECT now()";
request = "SELECT now()"; rest = poptGetArg(postgresql_poptcon);
rest = poptGetArg(postgresql_poptcon); if (rest != NULL)
if (rest != NULL) postgresql_usage("Erroneous additional arguments");
postgresql_usage("Erroneous additional arguments"); if (conninfo == NULL)
if (conninfo == NULL) conninfo = "";
conninfo = ""; return NULL; /* We only use the conninfo, echoping does not see
return NULL; /* We only use the conninfo, echoping does not see * our hostname or port */
* our hostname or port */
} }
void void
start_raw() start_raw()
{ {
if (!connect_each_time) { if (!connect_each_time) {
conn = PQconnectdb(conninfo); conn = PQconnectdb(conninfo);
if (conn == NULL) { if (conn == NULL) {
err_quit("Cannot create connection\n"); err_quit("Cannot create connection\n");
} }
if (PQstatus(conn) == CONNECTION_BAD) { if (PQstatus(conn) == CONNECTION_BAD) {
err_quit("Connection failed: %s\n", PQerrorMessage(conn)); err_quit("Connection failed: %s\n", PQerrorMessage(conn));
} }
} }
} }
int int
execute() execute()
{ {
unsigned int row, column; unsigned int row, column;
char *result; char *result;
if (connect_each_time) { if (connect_each_time) {
conn = PQconnectdb(conninfo); conn = PQconnectdb(conninfo);
if (conn == NULL) { if (conn == NULL) {
err_ret("Cannot create connection\n"); err_ret("Cannot create connection\n");
return -1; return -1;
} }
if (PQstatus(conn) == CONNECTION_BAD) { if (PQstatus(conn) == CONNECTION_BAD) {
err_ret("Connection failed: %s\n", PQerrorMessage(conn)); err_ret("Connection failed: %s\n", PQerrorMessage(conn));
return -1; return -1;
} }
} }
res = PQexec(conn, request); res = PQexec(conn, request);
if (PQresultStatus(res) != PGRES_TUPLES_OK) { if (PQresultStatus(res) != PGRES_TUPLES_OK) {
err_ret("Cannot run \"%s\": %s\n", request, err_ret("Cannot run \"%s\": %s\n", request, PQresultErrorMessage(res));
PQresultErrorMessage(res)); return -1;
return -1; }
} if (global_options.verbose)
if (global_options.verbose) printf("%d tuples returned\n", PQntuples(res));
printf("%d tuples returned\n", PQntuples(res)); if (readall) {
if (readall) { for (row = 0; row < PQntuples(res); row++) {
for (row = 0; row < PQntuples(res); row++) { for (column = 0; column < PQnfields(res); column++) {
for (column = 0; column < PQnfields(res); column++) { result = PQgetvalue(res, row, column);
result = PQgetvalue(res, row, column); if (result == NULL) {
if (result == NULL) { err_ret("Cannot retrieve value [%d,%d]\n", row, column);
err_ret("Cannot retrieve value [%d,%d]\n", return -1;
row, column); }
return -1; /* else { printf ("DEBUG: [%d,%d] %s\n", row, column, result); } */
} }
/* else { printf ("DEBUG: [%d,%d] %s\n", row, }
* column, result); } */ }
} if (connect_each_time)
} PQfinish(conn);
} return 0;
if (connect_each_time)
PQfinish(conn);
return 0;
} }
void void
terminate() terminate()
{ {
if (!connect_each_time) if (!connect_each_time)
PQfinish(conn); PQfinish(conn);
} }

@ -12,11 +12,11 @@
char * char *
init(const int argc, const char *argv[]) init(const int argc, const char *argv[])
{ {
struct timeval tv; struct timeval tv;
(void) gettimeofday(&tv, (struct timezone *) NULL); (void) gettimeofday(&tv, (struct timezone *) NULL);
srand(tv.tv_usec); srand(tv.tv_usec);
return "7"; /* Not used, just to say we use the cooked interface return "7"; /* Not used, just to say we use the cooked interface
*/ */
} }
void void
@ -27,8 +27,8 @@ start()
int int
execute() execute()
{ {
usleep(rand() % 1000000); usleep(rand() % 1000000);
return 1; return 1;
} }
void void

@ -26,105 +26,104 @@ echoping_options general_options;
void void
whois_usage(const char *msg) whois_usage(const char *msg)
{ {
if (msg) { if (msg) {
printf("Error: %s\n", msg); printf("Error: %s\n", msg);
} }
poptPrintUsage(whois_poptcon, stdout, 0); poptPrintUsage(whois_poptcon, stdout, 0);
fprintf(stderr, " request\n"); fprintf(stderr, " request\n");
exit(1); exit(1);
} }
char * char *
init(const int argc, const char **argv, echoping_options global_options) init(const int argc, const char **argv, echoping_options global_options)
{ {
int value; int value;
char *msg = malloc(256); char *msg = malloc(256);
char *rest; char *rest;
/* popt variables */ /* popt variables */
struct poptOption options[] = { struct poptOption options[] = {
{"dump", 'd', POPT_ARG_NONE, &dump, 'd', {"dump", 'd', POPT_ARG_NONE, &dump, 'd',
"Dumps the reply from the whois server", "Dumps the reply from the whois server",
""}, ""},
POPT_AUTOHELP POPT_TABLEEND POPT_AUTOHELP POPT_TABLEEND
}; };
general_options = global_options; general_options = global_options;
if (global_options.udp) if (global_options.udp)
err_quit("UDP is incompatible with this whois plugin"); err_quit("UDP is incompatible with this whois plugin");
/* Will probably be catched before because /etc/services have no entry for /* Will probably be catched before because /etc/services have no entry for UDP
* UDP port 43 */ * port 43 */
whois_poptcon = poptGetContext(NULL, argc, whois_poptcon = poptGetContext(NULL, argc,
argv, options, POPT_CONTEXT_POSIXMEHARDER); argv, options, POPT_CONTEXT_POSIXMEHARDER);
while ((value = poptGetNextOpt(whois_poptcon)) > 0) { while ((value = poptGetNextOpt(whois_poptcon)) > 0) {
switch ((char) value) { switch ((char) value) {
case 'd': case 'd':
break; break;
default: default:
sprintf(msg, "Wrong option %d (%c)", value, (char) value); sprintf(msg, "Wrong option %d (%c)", value, (char) value);
whois_usage(msg); whois_usage(msg);
} }
} }
if (value < -1) { if (value < -1) {
sprintf(msg, "%s: %s", sprintf(msg, "%s: %s",
poptBadOption(whois_poptcon, POPT_BADOPTION_NOALIAS), poptBadOption(whois_poptcon, POPT_BADOPTION_NOALIAS),
poptStrerror(value)); poptStrerror(value));
whois_usage(msg); whois_usage(msg);
} }
request = (char *) poptGetArg(whois_poptcon); request = (char *) poptGetArg(whois_poptcon);
if (request == NULL) if (request == NULL)
whois_usage("Mandatory request missing"); whois_usage("Mandatory request missing");
rest = (char *) poptGetArg(whois_poptcon); rest = (char *) poptGetArg(whois_poptcon);
if (rest != NULL && strcmp(rest, "")) if (rest != NULL && strcmp(rest, ""))
whois_usage("Extraneous arguments ignored"); whois_usage("Extraneous arguments ignored");
return "nicname"; return "nicname";
} }
void void
start(struct addrinfo *res) start(struct addrinfo *res)
{ {
whois_server = *res; whois_server = *res;
} }
int int
execute() execute()
{ {
int nr = 0; int nr = 0;
char recvline[MAX_LINE + 1]; char recvline[MAX_LINE + 1];
char complete_request[MAX_REQUEST]; char complete_request[MAX_REQUEST];
#ifdef HAVE_TCP_INFO #ifdef HAVE_TCP_INFO
struct tcp_info tcpinfo; struct tcp_info tcpinfo;
socklen_t socket_length = sizeof(tcpinfo); socklen_t socket_length = sizeof(tcpinfo);
#endif #endif
if ((sockfd = if ((sockfd =
socket(whois_server.ai_family, whois_server.ai_socktype, socket(whois_server.ai_family, whois_server.ai_socktype,
whois_server.ai_protocol)) < 0) whois_server.ai_protocol)) < 0)
err_sys("Can't open socket"); err_sys("Can't open socket");
if (connect(sockfd, whois_server.ai_addr, whois_server.ai_addrlen) < 0) if (connect(sockfd, whois_server.ai_addr, whois_server.ai_addrlen) < 0)
err_sys("Can't connect to server"); err_sys("Can't connect to server");
if ((files = fdopen(sockfd, "r")) == NULL) if ((files = fdopen(sockfd, "r")) == NULL)
err_sys("Cannot fdopen"); err_sys("Cannot fdopen");
sprintf(complete_request, "%s\r\n", request); sprintf(complete_request, "%s\r\n", request);
n = strlen(complete_request); n = strlen(complete_request);
if (writen(sockfd, complete_request, n) != n) if (writen(sockfd, complete_request, n) != n)
err_sys("writen error on socket"); err_sys("writen error on socket");
/* Read from the server */ /* Read from the server */
while ((nr = readline(files, recvline, MAX_LINE, 0)) > 0) while ((nr = readline(files, recvline, MAX_LINE, 0)) > 0)
if (dump) if (dump)
printf("%s", recvline); printf("%s", recvline);
if (dump) if (dump)
printf("\n"); printf("\n");
#ifdef HAVE_TCP_INFO #ifdef HAVE_TCP_INFO
/* Thanks to Perry Lorier <perry@coders.net> for the tip */ /* Thanks to Perry Lorier <perry@coders.net> for the tip */
if (general_options.verbose) { if (general_options.verbose) {
if (getsockopt if (getsockopt(sockfd, SOL_TCP, TCP_INFO, &tcpinfo, &socket_length)
(sockfd, SOL_TCP, TCP_INFO, &tcpinfo, &socket_length) != -1) {
!= -1) { printf("Estimated TCP RTT: %.04f seconds\n",
printf("Estimated TCP RTT: %.04f seconds\n", tcpinfo.tcpi_rtt / 1000000.0);
tcpinfo.tcpi_rtt / 1000000.0); }
} }
}
#endif #endif
close(sockfd); close(sockfd);
return 1; return 1;
} }
void void

@ -9,41 +9,41 @@
int int
readline(fs, ptr, maxlen, ln) readline(fs, ptr, maxlen, ln)
FILE *fs; FILE *fs;
char *ptr; char *ptr;
int maxlen; int maxlen;
unsigned short ln; unsigned short ln;
{ {
int n = 1; int n = 1;
char *rc; char *rc;
int r; int r;
/* Reading with fgets or fread instead of read one-character-at-a-time is /* Reading with fgets or fread instead of read one-character-at-a-time is more
* more than ten times faster, on a local server. */ * than ten times faster, on a local server. */
if (ln) { if (ln) {
rc = fgets(ptr, maxlen + 1, fs); rc = fgets(ptr, maxlen + 1, fs);
if (rc == NULL) { if (rc == NULL) {
if (timeout_flag) if (timeout_flag)
return n; return n;
return (-1); return (-1);
} }
n = strlen(rc); n = strlen(rc);
return n; return n;
} else { } else {
while (n < maxlen) { while (n < maxlen) {
r = fread(ptr, 1, maxlen, fs); r = fread(ptr, 1, maxlen, fs);
if (timeout_flag) if (timeout_flag)
return r; return r;
if (r == 0) { if (r == 0) {
if (n == 1) if (n == 1)
return (0); /* EOF, no data read */ return (0); /* EOF, no data read */
else else
break; /* EOF, some data was read */ break; /* EOF, some data was read */
} }
n = n + r; n = n + r;
} }
} }
return (n); return (n);
} }
#ifdef OPENSSL #ifdef OPENSSL
@ -54,69 +54,67 @@ int buf_end;
int int
SSL_readline(sslh, ptr, maxlen, ln) SSL_readline(sslh, ptr, maxlen, ln)
SSL *sslh; SSL *sslh;
char *ptr; char *ptr;
int maxlen; int maxlen;
unsigned short ln; unsigned short ln;
{ {
int rc = 0; int rc = 0;
int n = 0; int n = 0;
int i, oi; int i, oi;
if (ln) { if (ln) {
/* Empty buffer */ /* Empty buffer */
if (buf_end == 0) { if (buf_end == 0) {
rc = SSL_read(sslh, SSL_buffer, maxlen); rc = SSL_read(sslh, SSL_buffer, maxlen);
if (rc == -1) if (rc == -1)
return rc; return rc;
buf_end = rc; buf_end = rc;
buf_ptr = 0; buf_ptr = 0;
} }
/* No more data in the buffer */ /* No more data in the buffer */
else if (buf_ptr == buf_end) { else if (buf_ptr == buf_end) {
buf_ptr = 0; buf_ptr = 0;
rc = SSL_read(sslh, SSL_buffer, maxlen); rc = SSL_read(sslh, SSL_buffer, maxlen);
if (rc == -1) if (rc == -1)
return rc; return rc;
buf_end = rc; buf_end = rc;
} else if (SSL_buffer[buf_end] != '\n') { } else if (SSL_buffer[buf_end] != '\n') {
/* We have a probleme here is the first SSL_read sent back a /* We have a probleme here is the first SSL_read sent back a text not
* text not finished by a \n. See www.SSL.de for an example. * finished by a \n. See www.SSL.de for an example. We get more data.
* We get more data. See bug #230384 */ * See bug #230384 */
rc = SSL_read(sslh, SSL_buffer + buf_end, maxlen); rc = SSL_read(sslh, SSL_buffer + buf_end, maxlen);
if (rc == -1) if (rc == -1)
return rc; return rc;
buf_end = buf_end + rc; buf_end = buf_end + rc;
} }
for (oi = buf_ptr, i = buf_ptr; for (oi = buf_ptr, i = buf_ptr; i <= buf_end && SSL_buffer[i] != '\n'; i++) {
i <= buf_end && SSL_buffer[i] != '\n'; i++) { *ptr++ = SSL_buffer[i];
*ptr++ = SSL_buffer[i]; buf_ptr++;
buf_ptr++; }
} if (SSL_buffer[i] == '\n')
if (SSL_buffer[i] == '\n') buf_ptr++;
buf_ptr++; *ptr = '\0';
*ptr = '\0'; /* if (ln) printf ("SSL_readline returns %d (%s)\n", i - oi, SSL_buffer); */
/* if (ln) printf ("SSL_readline returns %d (%s)\n", i - oi, return (i - oi);
* SSL_buffer); */ } else {
return (i - oi); /* OpenSSL reads at most 4096 characters */
} else { rc = 1; /* Just to avoid exiting too soon */
/* OpenSSL reads at most 4096 characters */ while (n < maxlen && rc != 0) {
rc = 1; /* Just to avoid exiting too soon */ if ((buf_end == 0) || (buf_ptr > buf_end)) {
while (n < maxlen && rc != 0) { rc = SSL_read(sslh, ptr, maxlen);
if ((buf_end == 0) || (buf_ptr > buf_end)) { buf_end = 0;
rc = SSL_read(sslh, ptr, maxlen); buf_ptr = 0;
buf_end = 0; } else {
buf_ptr = 0; for (i = buf_ptr; i < maxlen && i <= buf_end; i++) {
} else { *ptr++ = SSL_buffer[i];
for (i = buf_ptr; i < maxlen && i <= buf_end; i++) { rc++;
*ptr++ = SSL_buffer[i]; }
rc++; buf_ptr = i;
} }
buf_ptr = i; n = n + rc;
} }
n = n + rc; return n;
} }
return n;
}
} }
#endif #endif
@ -128,69 +126,65 @@ int buf_end;
int int
TLS_readline(session, ptr, maxlen, ln) TLS_readline(session, ptr, maxlen, ln)
gnutls_session session; gnutls_session session;
char *ptr; char *ptr;
int maxlen; int maxlen;
unsigned short ln; unsigned short ln;
{ {
int rc = 0; int rc = 0;
int n = 0; int n = 0;
int i, oi; int i, oi;
if (ln) { if (ln) {
/* Empty buffer */ /* Empty buffer */
if (buf_end == 0) { if (buf_end == 0) {
rc = gnutls_record_recv(session, TLS_buffer, maxlen); rc = gnutls_record_recv(session, TLS_buffer, maxlen);
if (rc == -1) if (rc == -1)
return rc; return rc;
buf_end = rc; buf_end = rc;
buf_ptr = 0; buf_ptr = 0;
} }
/* No more data in the buffer */ /* No more data in the buffer */
else if (buf_ptr == buf_end) { else if (buf_ptr == buf_end) {
buf_ptr = 0; buf_ptr = 0;
rc = gnutls_record_recv(session, TLS_buffer, maxlen); rc = gnutls_record_recv(session, TLS_buffer, maxlen);
if (rc == -1) if (rc == -1)
return rc; return rc;
buf_end = rc; buf_end = rc;
} else if (TLS_buffer[buf_end] != '\n') { } else if (TLS_buffer[buf_end] != '\n') {
rc = gnutls_record_recv(session, TLS_buffer + buf_end, rc = gnutls_record_recv(session, TLS_buffer + buf_end, maxlen);
maxlen); if (rc == -1)
if (rc == -1) return rc;
return rc; buf_end = buf_end + rc;
buf_end = buf_end + rc; }
} for (oi = buf_ptr, i = buf_ptr; i <= buf_end && TLS_buffer[i] != '\n'; i++) {
for (oi = buf_ptr, i = buf_ptr; *ptr++ = TLS_buffer[i];
i <= buf_end && TLS_buffer[i] != '\n'; i++) { buf_ptr++;
*ptr++ = TLS_buffer[i]; }
buf_ptr++; if (TLS_buffer[i] == '\n')
} buf_ptr++;
if (TLS_buffer[i] == '\n') *ptr = '\0';
buf_ptr++; /* printf ("DEBUG: TLS_readline returns %d (%s)\n", i - oi, TLS_buffer); */
*ptr = '\0'; return (i - oi);
/* printf ("DEBUG: TLS_readline returns %d (%s)\n", i - oi, } else {
* TLS_buffer); */ rc = 1; /* Just to avoid exiting too soon */
return (i - oi); while (n < maxlen && rc > 0) {
} else { if ((buf_end == 0) || (buf_ptr > buf_end)) {
rc = 1; /* Just to avoid exiting too soon */ rc = gnutls_record_recv(session, ptr, maxlen);
while (n < maxlen && rc > 0) { buf_end = 0;
if ((buf_end == 0) || (buf_ptr > buf_end)) { buf_ptr = 0;
rc = gnutls_record_recv(session, ptr, maxlen); } else {
buf_end = 0; for (i = buf_ptr; i < maxlen && i <= buf_end; i++) {
buf_ptr = 0; *ptr++ = TLS_buffer[i];
} else { rc++;
for (i = buf_ptr; i < maxlen && i <= buf_end; i++) { /* printf ("DEBUG: Now %d chars read\n", rc); */
*ptr++ = TLS_buffer[i]; }
rc++; buf_ptr = i;
/* printf ("DEBUG: Now %d chars read\n", }
* rc); */ if (rc > 0)
} n = n + rc;
buf_ptr = i; /* printf ("DEBUG: Now %d chars to send\n", n); */
} }
if (rc > 0) return n;
n = n + rc; }
/* printf ("DEBUG: Now %d chars to send\n", n); */
}
return n;
}
} }
#endif #endif

@ -13,22 +13,22 @@ char big_recvline[MAXTOREAD];
int int
smtp_read_response_from_server(FILE * fs) smtp_read_response_from_server(FILE * fs)
{ {
int nr; int nr;
int i; int i;
for (i = 0; i < MAXSMTPLINES; i++) { for (i = 0; i < MAXSMTPLINES; i++) {
nr = readline(fs, big_recvline, MAXTOREAD, TRUE); nr = readline(fs, big_recvline, MAXTOREAD, TRUE);
if (nr <= 4) { if (nr <= 4) {
return -1; return -1;
} }
if (big_recvline[3] == ' ') { if (big_recvline[3] == ' ') {
return nr; return nr;
} }
if (big_recvline[3] != '-') { if (big_recvline[3] != '-') {
return -1; return -1;
} }
} }
return -1; return -1;
} }
#endif /* SMTP */ #endif /* SMTP */

@ -13,39 +13,39 @@ char *
random_string(unsigned length) random_string(unsigned length)
{ {
char *state = (char *) malloc(sizeof(char) * STATES); char *state = (char *) malloc(sizeof(char) * STATES);
char *result = (char *) malloc(length + 1); char *result = (char *) malloc(length + 1);
int i, number; int i, number;
unsigned seed = (unsigned) time((time_t *) NULL); unsigned seed = (unsigned) time((time_t *) NULL);
/* printf ("Seed is %u\n", seed); */ /* printf ("Seed is %u\n", seed); */
/* Initialize random generator */ /* Initialize random generator */
(void) initstate(seed, state, STATES); (void) initstate(seed, state, STATES);
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
number = (random() % 94) + 33; number = (random() % 94) + 33;
/* printf ("Number for %d is %d\n", i, number); */ /* printf ("Number for %d is %d\n", i, number); */
result[i] = (char) number; result[i] = (char) number;
} }
result[length] = '\0'; result[length] = '\0';
/* printf ("Result is %s\n", result); */ /* printf ("Result is %s\n", result); */
return result; return result;
} }
char * char *
to_upper(char *input) to_upper(char *input)
{ {
int c; int c;
char *result; char *result;
result = (char *) malloc(strlen(input)); result = (char *) malloc(strlen(input));
for (c = 0; c < strlen(input); c++) for (c = 0; c < strlen(input); c++)
result[c] = toupper((int)input[c]); result[c] = toupper((int) input[c]);
result[strlen(input)] = '\0'; result[strlen(input)] = '\0';
return result; return result;
} }
/* /*
@ -54,91 +54,91 @@ to_upper(char *input)
*/ */
void void
tvsub(out, in) tvsub(out, in)
struct timeval *out, *in; struct timeval *out, *in;
{ {
if ((out->tv_usec -= in->tv_usec) < 0) { if ((out->tv_usec -= in->tv_usec) < 0) {
--out->tv_sec; --out->tv_sec;
out->tv_usec += 1000000; out->tv_usec += 1000000;
} }
out->tv_sec -= in->tv_sec; out->tv_sec -= in->tv_sec;
} }
/* tvadd -- Adds 2 timeval structs: out = out + in. */ /* tvadd -- Adds 2 timeval structs: out = out + in. */
void void
tvadd(out, in) tvadd(out, in)
struct timeval *out, *in; struct timeval *out, *in;
{ {
if ((out->tv_usec += in->tv_usec) >= 1000000) { if ((out->tv_usec += in->tv_usec) >= 1000000) {
++out->tv_sec; ++out->tv_sec;
out->tv_usec -= 1000000; out->tv_usec -= 1000000;
} }
out->tv_sec += in->tv_sec; out->tv_sec += in->tv_sec;
} }
/* tvavg -- Averages a timeval struct */ /* tvavg -- Averages a timeval struct */
void void
tvavg(out, number) tvavg(out, number)
struct timeval *out; struct timeval *out;
int number; int number;
{ {
double result; double result;
/* /*
* out->tv_sec = out->tv_sec/number; out->tv_usec = * out->tv_sec = out->tv_sec/number; out->tv_usec =
* out->tv_usec/number; * out->tv_usec/number;
*/ */
result = (1000000 * out->tv_sec + out->tv_usec) / number; result = (1000000 * out->tv_sec + out->tv_usec) / number;
/* printf ("Result of average is %f\n", result) */ ; /* printf ("Result of average is %f\n", result) */ ;
out->tv_sec = (long) (result / 1000000); out->tv_sec = (long) (result / 1000000);
out->tv_usec = (long) (result - (out->tv_sec * 1000000)); out->tv_usec = (long) (result - (out->tv_sec * 1000000));
} }
/* tvstddev -- Computes the standard deviation of a set of results */ /* tvstddev -- Computes the standard deviation of a set of results */
void void
tvstddev(out, number, average, results) tvstddev(out, number, average, results)
struct timeval *out; struct timeval *out;
int number; int number;
struct timeval average; struct timeval average;
struct result *results; struct result *results;
{ {
int i; int i;
struct timeval result = null_timeval; struct timeval result = null_timeval;
struct timeval avg = null_timeval; struct timeval avg = null_timeval;
#ifdef DEBUG #ifdef DEBUG
struct timeval var = null_timeval; struct timeval var = null_timeval;
#endif #endif
struct timeval large, small; struct timeval large, small;
double d_offset, d_square, d_variance = 0; double d_offset, d_square, d_variance = 0;
*out = null_timeval; *out = null_timeval;
for (i = 0; i < number; i++) { for (i = 0; i < number; i++) {
if (results[i].valid == 1) { if (results[i].valid == 1) {
result = results[i].timevalue; result = results[i].timevalue;
#ifdef DEBUG #ifdef DEBUG
printf("DEBUG: Value is %f (average is %f)\n", tv2double printf("DEBUG: Value is %f (average is %f)\n", tv2double
(result), tv2double(average)); (result), tv2double(average));
#endif #endif
avg = average; avg = average;
if (tvcmp(&result, &avg) == -1) { if (tvcmp(&result, &avg) == -1) {
small = result; small = result;
large = avg; large = avg;
} else { } else {
large = result; large = result;
small = avg; small = avg;
} }
tvsub(&large, &small); tvsub(&large, &small);
#ifdef DEBUG #ifdef DEBUG
printf("abs offset is %f\n", tv2double(large)); printf("abs offset is %f\n", tv2double(large));
#endif #endif
d_offset = tv2double(large); d_offset = tv2double(large);
d_square = d_offset * d_offset; d_square = d_offset * d_offset;
d_variance += d_square; d_variance += d_square;
#ifdef DEBUG #ifdef DEBUG
printf("variance is now %f\n", tv2double(var)); printf("variance is now %f\n", tv2double(var));
#endif #endif
} }
} }
result = double2tv(sqrt(d_variance / (double) number)); result = double2tv(sqrt(d_variance / (double) number));
out->tv_sec = result.tv_sec; out->tv_sec = result.tv_sec;
out->tv_usec = result.tv_usec; out->tv_usec = result.tv_usec;
} }
@ -147,116 +147,115 @@ tvstddev(out, number, average, results)
/* TODO: IWBN to return the number of excluded outliers */ /* TODO: IWBN to return the number of excluded outliers */
void void
tvstddevavg(out, number, average, results, n_stddev) tvstddevavg(out, number, average, results, n_stddev)
struct timeval *out; /* contains std dev on entry */ struct timeval *out; /* contains std dev on entry */
int number; int number;
struct timeval average; struct timeval average;
struct result *results; struct result *results;
double n_stddev; double n_stddev;
{ {
int i, valid = 0; int i, valid = 0;
struct timeval result; /* working value */ struct timeval result; /* working value */
struct timeval var = null_timeval; /* result accumulator */ struct timeval var = null_timeval; /* result accumulator */
double x; double x;
double maxdev = tv2double(*out) * n_stddev; double maxdev = tv2double(*out) * n_stddev;
if (tvcmp(out, &null_timeval) == 0) { if (tvcmp(out, &null_timeval) == 0) {
/* if the SD is 0 then we just return the average */ /* if the SD is 0 then we just return the average */
*out = average; *out = average;
return; return;
} }
for (i = 0; i < number; i++) { for (i = 0; i < number; i++) {
if (results[i].valid == 1) { if (results[i].valid == 1) {
result = results[i].timevalue; result = results[i].timevalue;
tvsub(&result, &average); tvsub(&result, &average);
/* printf ("value is %f (stddev is %f)\n", tv2double /* printf ("value is %f (stddev is %f)\n", tv2double (result), tv2double
* (result), tv2double (stddev)); */ * (stddev)); */
/* ensure that result (difference to average) is absolute /* ensure that result (difference to average) is absolute value */
* value */ if (tvcmp(&result, &null_timeval) == -1) {
if (tvcmp(&result, &null_timeval) == -1) { result = average;
result = average; tvsub(&result, &results[i].timevalue);
tvsub(&result, &results[i].timevalue); }
} x = tv2double(result);
x = tv2double(result); /* printf("value is %g maxdev %g\n",x,maxdev); */
/* printf("value is %g maxdev %g\n",x,maxdev); */ if (x <= maxdev) {
if (x <= maxdev) { /* deviation is less than stddev */
/* deviation is less than stddev */ tvadd(&var, &results[i].timevalue);
tvadd(&var, &results[i].timevalue); valid++;
valid++; } else {
} else { /* printf("dropped\n"); */
/* printf("dropped\n"); */ }
} }
} }
} /* printf ("total is %f in %d samples\n", tv2double (var), valid); */
/* printf ("total is %f in %d samples\n", tv2double (var), valid); */ if (valid > 0) {
if (valid > 0) { *out = double2tv(tv2double(var) / valid);
*out = double2tv(tv2double(var) / valid); } else {
} else { *out = null_timeval;
*out = null_timeval; }
}
} }
/* tvcmp -- Compares two timeval structs */ /* tvcmp -- Compares two timeval structs */
int int
tvcmp(left, right) tvcmp(left, right)
struct timeval *left, *right; struct timeval *left, *right;
{ {
if (left->tv_sec < right->tv_sec) { if (left->tv_sec < right->tv_sec) {
return -1; return -1;
} }
if (left->tv_sec > right->tv_sec) { if (left->tv_sec > right->tv_sec) {
return 1; return 1;
} }
if (left->tv_usec < right->tv_usec) { if (left->tv_usec < right->tv_usec) {
return -1; return -1;
} }
if (left->tv_usec > right->tv_usec) { if (left->tv_usec > right->tv_usec) {
return 1; return 1;
} }
return 0; return 0;
} }
/* tvmin */ /* tvmin */
void void
tvmin(champion, challenger) tvmin(champion, challenger)
struct timeval *champion, *challenger; struct timeval *champion, *challenger;
{ {
if (tvcmp(champion, challenger) == 1) { if (tvcmp(champion, challenger) == 1) {
champion->tv_sec = challenger->tv_sec; champion->tv_sec = challenger->tv_sec;
champion->tv_usec = challenger->tv_usec; champion->tv_usec = challenger->tv_usec;
} }
} }
/* tvmax */ /* tvmax */
void void
tvmax(champion, challenger) tvmax(champion, challenger)
struct timeval *champion, *challenger; struct timeval *champion, *challenger;
{ {
if (tvcmp(champion, challenger) == -1) { if (tvcmp(champion, challenger) == -1) {
champion->tv_sec = challenger->tv_sec; champion->tv_sec = challenger->tv_sec;
champion->tv_usec = challenger->tv_usec; champion->tv_usec = challenger->tv_usec;
} }
} }
double double
tv2double(tv) tv2double(tv)
struct timeval tv; struct timeval tv;
{ {
double result; double result;
result = result =
(((((double) tv.tv_sec) * 1000000.0) + (double) tv.tv_usec) / 1000000.0); (((((double) tv.tv_sec) * 1000000.0) + (double) tv.tv_usec) / 1000000.0);
/* printf ("Double is %9.3f\n", result); */ /* printf ("Double is %9.3f\n", result); */
return result; return result;
} }
struct timeval struct timeval
double2tv(x) double2tv(x)
double x; double x;
{ {
struct timeval result; struct timeval result;
result.tv_sec = (int) (x); result.tv_sec = (int) (x);
result.tv_usec = (int) ((x - result.tv_sec) * 1000000); result.tv_usec = (int) ((x - result.tv_sec) * 1000000);
return result; return result;
} }

@ -9,24 +9,24 @@
int int
writen(fd, ptr, nbytes) writen(fd, ptr, nbytes)
register int fd; register int fd;
register char *ptr; register char *ptr;
register int nbytes; register int nbytes;
{ {
int nleft, nwritten; int nleft, nwritten;
nleft = nbytes; nleft = nbytes;
while (nleft > 0) { while (nleft > 0) {
nwritten = write(fd, ptr, nleft); nwritten = write(fd, ptr, nleft);
if (nwritten <= 0) if (nwritten <= 0)
return (nwritten); /* error */ return (nwritten); /* error */
/* Some systems, such as Digital's OSF1 (Digital Unix) doesn't set /* Some systems, such as Digital's OSF1 (Digital Unix) doesn't set the
* the returned value to -1, even when interrupted by an alarm, * returned value to -1, even when interrupted by an alarm, whatever says
* whatever says the documentation. errno is not set. */ * the documentation. errno is not set. */
if ((nwritten < nleft) && timeout_flag) if ((nwritten < nleft) && timeout_flag)
return nwritten; return nwritten;
nleft -= nwritten; nleft -= nwritten;
ptr += nwritten; ptr += nwritten;
} }
return (nbytes - nleft); return (nbytes - nleft);
} }

Loading…
Cancel
Save