diff --git a/SRC/HTParse.c b/SRC/HTParse.c index 832474f..09255a3 100644 --- a/SRC/HTParse.c +++ b/SRC/HTParse.c @@ -7,110 +7,92 @@ #define FREE(x) if (x) {free(x); x = NULL;} -struct struct_parts - { - char *access; - char *host; - char *absolute; - char *relative; +struct struct_parts { + char *access; + char *host; + char *absolute; + char *relative; /* char * search; no - treated as part of path */ - char *anchor; - }; + char *anchor; +}; /* Strings of any length ** --------------------- */ -PUBLIC int strcasecomp -ARGS2 ( - CONST char *, a, - CONST char *, b) +PUBLIC int strcasecomp +ARGS2(CONST char *, a, CONST char *, b) { - CONST char *p = a; - CONST char *q = b; - - for (p = a, q = b; *p && *q; p++, q++) - { - int diff = TOLOWER (*p) - TOLOWER (*q); - if (diff) - return diff; + CONST char *p = a; + CONST char *q = b; + + for (p = a, q = b; *p && *q; p++, q++) { + int diff = TOLOWER(*p) - TOLOWER(*q); + if (diff) + return diff; } - if (*p) - return 1; /* p was longer than q */ - if (*q) - return -1; /* p was shorter than q */ - return 0; /* Exact match */ + if (*p) + return 1; /* p was longer than q */ + if (*q) + return -1; /* p was shorter than q */ + return 0; /* Exact match */ } /* With count limit ** ---------------- */ -PUBLIC int strncasecomp -ARGS3 ( - CONST char *, a, - CONST char *, b, - int, n) +PUBLIC int strncasecomp +ARGS3(CONST char *, a, CONST char *, b, int, n) { - CONST char *p = a; - CONST char *q = b; - - for (p = a, q = b;; - p++, q++) - { - int diff; - if (p == (a + n)) - return 0; /* Match up to n characters */ - if (!(*p && *q)) - return (*p - *q); - diff = TOLOWER (*p) - TOLOWER (*q); - if (diff) - return diff; + CONST char *p = a; + CONST char *q = b; + + for (p = a, q = b;; p++, q++) { + int diff; + if (p == (a + n)) + return 0; /* Match up to n characters */ + if (!(*p && *q)) + return (*p - *q); + diff = TOLOWER(*p) - TOLOWER(*q); + if (diff) + return diff; } - /*NOTREACHED */ + /* NOTREACHED */ } /* Allocate a new copy of a string, and returns it */ -PUBLIC char *HTSACopy -ARGS2 ( - char **, dest, - CONST char *, src) +PUBLIC char *HTSACopy +ARGS2(char **, dest, CONST char *, src) { - FREE (*dest); - if (src) - { - *dest = (char *) malloc (strlen (src) + 1); - if (*dest == NULL) - outofmem (__FILE__, "HTSACopy"); - strcpy (*dest, src); + FREE(*dest); + if (src) { + *dest = (char *) malloc(strlen(src) + 1); + if (*dest == NULL) + outofmem(__FILE__, "HTSACopy"); + strcpy(*dest, src); } - return *dest; + return *dest; } /* String Allocate and Concatenate */ -PUBLIC char *HTSACat -ARGS2 ( - char **, dest, - CONST char *, src) +PUBLIC char *HTSACat +ARGS2(char **, dest, CONST char *, src) { - if (src && *src) - { - if (*dest) - { - int length = strlen (*dest); - *dest = (char *) realloc (*dest, length + strlen (src) + 1); - if (*dest == NULL) - outofmem (__FILE__, "HTSACat"); - strcpy (*dest + length, src); - } - else - { - *dest = (char *) malloc (strlen (src) + 1); - if (*dest == NULL) - outofmem (__FILE__, "HTSACat"); - strcpy (*dest, src); - } + if (src && *src) { + if (*dest) { + int length = strlen(*dest); + *dest = (char *) realloc(*dest, length + strlen(src) + 1); + if (*dest == NULL) + outofmem(__FILE__, "HTSACat"); + strcpy(*dest + length, src); + } else { + *dest = (char *) malloc(strlen(src) + 1); + if (*dest == NULL) + outofmem(__FILE__, "HTSACat"); + strcpy(*dest, src); + } } - return *dest; + return *dest; } @@ -122,24 +104,21 @@ ARGS2 ( ** Return value points to first non-white character, or to 0 if none. ** All trailing white space is OVERWRITTEN with zero. */ -PUBLIC char *HTStrip -ARGS1 ( - char *, s) +PUBLIC char *HTStrip +ARGS1(char *, s) { #define SPACE(c) ((c == ' ') || (c == '\t') || (c == '\n')) - char *p = s; - for (p = s; *p; p++) - ; /* Find end of string */ - for (p--; p >= s; p--) - { - if (SPACE (*p)) - *p = '\0'; /* Zap trailing blanks */ - else - break; + char *p = s; + for (p = s; *p; p++); /* Find end of string */ + for (p--; p >= s; p--) { + if (SPACE(*p)) + *p = '\0'; /* Zap trailing blanks */ + else + break; } - while (SPACE (*s)) - s++; /* Strip leading blanks */ - return s; + while (SPACE(*s)) + s++; /* Strip leading blanks */ + return s; } /* Scan a filename for its consituents. scan() @@ -152,119 +131,100 @@ ARGS1 ( ** host, anchor and access may be nonzero if they were specified. ** Any which are nonzero point to zero terminated strings. */ -PRIVATE void scan -ARGS2 ( - char *, name, - struct struct_parts *, parts) +PRIVATE void scan +ARGS2(char *, name, struct struct_parts *, parts) { - char *after_access; - char *p; - /* int length = strlen (name); */ + char *after_access; + char *p; + /* int length = strlen (name); */ - parts->access = NULL; - parts->host = NULL; - parts->absolute = NULL; - parts->relative = NULL; - parts->anchor = NULL; + parts->access = NULL; + parts->host = NULL; + parts->absolute = NULL; + parts->relative = NULL; + parts->anchor = NULL; - /* + /* ** Scan left-to-right for a scheme (access). - */ - after_access = name; - for (p = name; *p; p++) - { - if (*p == ':') - { - *p = '\0'; - parts->access = name; /* Access name has been specified */ - after_access = (p + 1); - break; - } - if (*p == '/' || *p == '#' || *p == ';' || *p == '?') - break; + */ + after_access = name; + for (p = name; *p; p++) { + if (*p == ':') { + *p = '\0'; + parts->access = name; /* Access name has been specified */ + after_access = (p + 1); + break; + } + if (*p == '/' || *p == '#' || *p == ';' || *p == '?') + break; } #ifdef NOTDEFINED - for (p = (name + length - 1); p >= name; p--) - { -#endif /* NOTDEFINED */ - /* + for (p = (name + length - 1); p >= name; p--) { +#endif /* NOTDEFINED */ + /* ** Scan left-to-right for a fragment (anchor). - */ - for (p = after_access; *p; p++) - { - if (*p == '#') - { - parts->anchor = (p + 1); - *p = '\0'; /* terminate the rest */ - } - } - - /* + */ + for (p = after_access; *p; p++) { + if (*p == '#') { + parts->anchor = (p + 1); + *p = '\0'; /* terminate the rest */ + } + } + + /* ** Scan left-to-right for a host or absolute path. - */ - p = after_access; - if (*p == '/') - { - if (p[1] == '/') - { - parts->host = (p + 2); /* host has been specified */ - *p = '\0'; /* Terminate access */ - p = strchr (parts->host, '/'); /* look for end of host name if any */ - if (p != NULL) - { - *p = '\0'; /* Terminate host */ - parts->absolute = (p + 1); /* Root has been found */ - } - } - else - { - parts->absolute = (p + 1); /* Root found but no host */ - } - } - else - { - parts->relative = (*after_access) ? - after_access : NULL; /* NULL for "" */ - } - - /* + */ + p = after_access; + if (*p == '/') { + if (p[1] == '/') { + parts->host = (p + 2); /* host has been specified */ + *p = '\0'; /* Terminate access */ + p = strchr(parts->host, '/'); /* look for end of host name if any */ + if (p != NULL) { + *p = '\0'; /* Terminate host */ + parts->absolute = (p + 1); /* Root has been found */ + } + } else { + parts->absolute = (p + 1); /* Root found but no host */ + } + } else { + parts->relative = (*after_access) ? after_access : NULL; /* NULL for + * "" */ + } + + /* ** Check schemes that commonly have unescaped hashes. - */ - if (parts->access && parts->anchor) - { - if ((!parts->host && strcasecomp (parts->access, "lynxcgi")) || - !strcasecomp (parts->access, "nntp") || - !strcasecomp (parts->access, "snews") || - !strcasecomp (parts->access, "news") || - !strcasecomp (parts->access, "data")) - { - /* - * Access specified but no host and not a lynxcgi URL, so the - * anchor may not really be one, e.g., news:j462#36487@foo.bar, - * or it's an nntp or snews URL, or news URL with a host. - * Restore the '#' in the address. - */ - *(parts->anchor - 1) = '#'; - parts->anchor = NULL; - } - } - -#ifdef NOT_DEFINED /* search is just treated as part of path */ - { - char *p = (relative ? relative : absolute); - if (p != NULL) - { - char *q = strchr (p, '?'); /* Any search string? */ - if (q != NULL) - { - *q = '\0'; /* If so, chop that off. */ - parts->search = (q + 1); - } - } - } -#endif /* NOT_DEFINED */ - } /*scan */ + */ + if (parts->access && parts->anchor) { + if ((!parts->host && strcasecomp(parts->access, "lynxcgi")) || + !strcasecomp(parts->access, "nntp") || + !strcasecomp(parts->access, "snews") || + !strcasecomp(parts->access, "news") || + !strcasecomp(parts->access, "data")) { + /* + * Access specified but no host and not a lynxcgi URL, so the + * anchor may not really be one, e.g., news:j462#36487@foo.bar, + * or it's an nntp or snews URL, or news URL with a host. + * Restore the '#' in the address. + */ + *(parts->anchor - 1) = '#'; + parts->anchor = NULL; + } + } +#ifdef NOT_DEFINED /* search is just treated as part of path */ + { + char *p = (relative ? relative : absolute); + if (p != NULL) { + char *q = strchr(p, '?'); /* Any search string? */ + if (q != NULL) { + *q = '\0'; /* If so, chop that off. */ + parts->search = (q + 1); + } + } + } +#endif /* NOT_DEFINED */ + } /* scan */ /* Parse a Name relative to another name. HTParse() @@ -281,271 +241,238 @@ ARGS2 ( ** On exit, ** returns A pointer to a malloc'd string which MUST BE FREED */ - PUBLIC char *HTParse ARGS3 ( - CONST char *, aName, - CONST char *, relatedName, - int, wanted) - { - char *result = NULL; - char *return_value = NULL; - int len; - char *name = NULL; - char *rel = NULL; - char *p; - char *access; - struct struct_parts given, related; - - if (TRACE) - fprintf (stderr, - "HTParse: aName:%s relatedName:%s\n", aName, relatedName); - - /* - ** Allocate the output string. - */ - len = strlen (aName) + strlen (relatedName) + 10; - result = (char *) malloc (len); /* Lots of space: more than enough */ - if (result == NULL) - outofmem (__FILE__, "HTParse"); - result[0] = '\0'; /* Clear string */ - - /* - ** Make working copies of the input strings to cut up. - */ - StrAllocCopy (name, aName); - StrAllocCopy (rel, relatedName); - - /* - ** Cut up the strings into URL fields. - */ - scan (name, &given); - scan (rel, &related); - - /* - ** Handle the scheme (access) field. - */ - if (given.access && given.host && !given.relative && !given.absolute) - { - if (!strcmp (given.access, "http") || - !strcmp (given.access, "https") || - !strcmp (given.access, "ftp")) - /* - ** Assume root. - */ - given.absolute = ""; - } - access = given.access ? given.access : related.access; - if (wanted & PARSE_ACCESS) - { - if (access) - { - strcat (result, access); - if (wanted & PARSE_PUNCTUATION) - strcat (result, ":"); - } - } - - /* - ** If different schemes, inherit nothing. - ** - ** We'll try complying with RFC 1808 and - ** the Fielding draft, and inherit nothing - ** if both schemes are given, rather than - ** only when they differ, except for - ** file URLs - FM - ** - ** After trying it for a while, it's still - ** premature, IHMO, to go along with it, so - ** this is back to inheriting for identical - ** schemes whether or not they are "file". - ** If you want to try it again yourself, - ** uncomment the strncasecomp() below. - FM - */ - if ((given.access && related.access) && - ( /* strcasecomp(given.access, "file") || */ - strcmp (given.access, related.access))) - { - related.host = NULL; - related.absolute = NULL; - related.relative = NULL; - related.anchor = NULL; - } - - /* - ** Handle the host field. - */ - if (wanted & PARSE_HOST) - if (given.host || related.host) - { - char *tail = result + strlen (result); - if (wanted & PARSE_PUNCTUATION) - strcat (result, "//"); - strcat (result, given.host ? given.host : related.host); + PUBLIC char *HTParse ARGS3(CONST char *, aName, + CONST char *, relatedName, int, wanted) { + char *result = NULL; + char *return_value = NULL; + int len; + char *name = NULL; + char *rel = NULL; + char *p; + char *access; + struct struct_parts given, related; + + if (TRACE) + fprintf(stderr, + "HTParse: aName:%s relatedName:%s\n", aName, relatedName); + + /* + ** Allocate the output string. + */ + len = strlen(aName) + strlen(relatedName) + 10; + result = (char *) malloc(len); /* Lots of space: more than enough */ + if (result == NULL) + outofmem(__FILE__, "HTParse"); + result[0] = '\0'; /* Clear string */ + + /* + ** Make working copies of the input strings to cut up. + */ + StrAllocCopy(name, aName); + StrAllocCopy(rel, relatedName); + + /* + ** Cut up the strings into URL fields. + */ + scan(name, &given); + scan(rel, &related); + + /* + ** Handle the scheme (access) field. + */ + if (given.access && given.host && !given.relative && !given.absolute) { + if (!strcmp(given.access, "http") || + !strcmp(given.access, "https") || !strcmp(given.access, "ftp")) + /* + ** Assume root. + */ + given.absolute = ""; + } + access = given.access ? given.access : related.access; + if (wanted & PARSE_ACCESS) { + if (access) { + strcat(result, access); + if (wanted & PARSE_PUNCTUATION) + strcat(result, ":"); + } + } + + /* + ** If different schemes, inherit nothing. + ** + ** We'll try complying with RFC 1808 and + ** the Fielding draft, and inherit nothing + ** if both schemes are given, rather than + ** only when they differ, except for + ** file URLs - FM + ** + ** After trying it for a while, it's still + ** premature, IHMO, to go along with it, so + ** this is back to inheriting for identical + ** schemes whether or not they are "file". + ** If you want to try it again yourself, + ** uncomment the strncasecomp() below. - FM + */ + if ((given.access && related.access) && ( /* strcasecomp(given.access, + * "file") || */ + strcmp(given.access, + related.access))) { + related.host = NULL; + related.absolute = NULL; + related.relative = NULL; + related.anchor = NULL; + } + + /* + ** Handle the host field. + */ + if (wanted & PARSE_HOST) + if (given.host || related.host) { + char *tail = result + strlen(result); + if (wanted & PARSE_PUNCTUATION) + strcat(result, "//"); + strcat(result, given.host ? given.host : related.host); #define CLEAN_URLS #ifdef CLEAN_URLS - /* - ** Ignore default port numbers, and trailing dots on FQDNs, - ** which will only cause identical addresses to look different. - */ - { - char *p, *h; - p = strchr (tail, ':'); - if (p != NULL && !isdigit ((unsigned char) p[1])) - /* - ** Colon not followed by a port number. - */ - *p = '\0'; - if (p != NULL && p != '\0' && access != NULL) - { - /* - ** Port specified. - */ - if ((!strcmp (access, "http") && !strcmp (p, ":80")) || - (!strcmp (access, "gopher") && !strcmp (p, ":70")) || - (!strcmp (access, "ftp") && !strcmp (p, ":21")) || - (!strcmp (access, "wais") && !strcmp (p, ":210")) || - (!strcmp (access, "nntp") && !strcmp (p, ":119")) || - (!strcmp (access, "news") && !strcmp (p, ":119")) || - (!strcmp (access, "snews") && !strcmp (p, ":563")) || - (!strcmp (access, "finger") && !strcmp (p, ":79")) || - (!strcmp (access, "cso") && !strcmp (p, ":105"))) - *p = '\0'; /* It is the default: ignore it */ - } - if (p == NULL) - { - int len = strlen (tail); - - if (len > 0) - { - h = tail + len - 1; /* last char of hostname */ - if (*h == '.') - *h = '\0'; /* chop final . */ - } - } - else - { - h = p; - h--; /* End of hostname */ - if (*h == '.') - { - /* - ** Slide p over h. - */ - while (*p != '\0') - *h++ = *p++; - *h = '\0'; /* terminate */ - } - } - } -#endif /* CLEAN_URLS */ - } - - /* - ** If different hosts, inherit no path. - */ - if (given.host && related.host) - if (strcmp (given.host, related.host) != 0) - { - related.absolute = NULL; - related.relative = NULL; - related.anchor = NULL; - } - - /* - ** Handle the path. - */ - if (wanted & PARSE_PATH) - { - if (access && !given.absolute && given.relative) - { - if (!strcasecomp (access, "nntp") || - !strcasecomp (access, "snews") || - (!strcasecomp (access, "news") && - !strncasecomp (result, "news://", 7))) - { - /* - * Treat all given nntp or snews paths, - * or given paths for news URLs with a host, - * as absolute. - */ - given.absolute = given.relative; - given.relative = NULL; - } - } - if (given.absolute) - { /* All is given */ - if (wanted & PARSE_PUNCTUATION) - strcat (result, "/"); - strcat (result, given.absolute); - if (TRACE) - fprintf (stderr, "1\n"); - } - else if (related.absolute) - { /* Adopt path not name */ - strcat (result, "/"); - strcat (result, related.absolute); - if (given.relative) - { - p = strchr (result, '?'); /* Search part? */ - if (p == NULL) - p = (result + strlen (result) - 1); - for (; *p != '/'; p--) - ; /* last / */ - p[1] = '\0'; /* Remove filename */ - strcat (result, given.relative); /* Add given one */ - HTSimplify (result); - } - if (TRACE) - fprintf (stderr, "2\n"); - } - else if (given.relative) - { - strcat (result, given.relative); /* what we've got */ - if (TRACE) - fprintf (stderr, "3\n"); - } - else if (related.relative) - { - strcat (result, related.relative); - if (TRACE) - fprintf (stderr, "4\n"); - } - else - { /* No inheritance */ - if (strncasecomp (aName, "lynxcgi:", 8) && - strncasecomp (aName, "lynxexec:", 9) && - strncasecomp (aName, "lynxprog:", 9)) - { - strcat (result, "/"); - } - if (!strcmp (result, "news:/")) - result[5] = '*'; - if (TRACE) - fprintf (stderr, "5\n"); - } - } - - /* - ** Handle the fragment (anchor). - */ - if (wanted & PARSE_ANCHOR) - if ((given.anchor && *given.anchor) || - (!given.anchor && related.anchor)) - { - if (wanted & PARSE_PUNCTUATION) - strcat (result, "#"); - strcat (result, (given.anchor) ? - given.anchor : related.anchor); - } - if (TRACE) - fprintf (stderr, "HTParse: result:%s\n", result); - FREE (rel); - FREE (name); - - StrAllocCopy (return_value, result); - FREE (result); - - return return_value; /* exactly the right length */ - } + /* + ** Ignore default port numbers, and trailing dots on FQDNs, + ** which will only cause identical addresses to look different. + */ + { + char *p, *h; + p = strchr(tail, ':'); + if (p != NULL && !isdigit((unsigned char) p[1])) + /* + ** Colon not followed by a port number. + */ + *p = '\0'; + if (p != NULL && p != '\0' && access != NULL) { + /* + ** Port specified. + */ + if ((!strcmp(access, "http") && !strcmp(p, ":80")) || + (!strcmp(access, "gopher") && !strcmp(p, ":70")) || + (!strcmp(access, "ftp") && !strcmp(p, ":21")) || + (!strcmp(access, "wais") && !strcmp(p, ":210")) || + (!strcmp(access, "nntp") && !strcmp(p, ":119")) || + (!strcmp(access, "news") && !strcmp(p, ":119")) || + (!strcmp(access, "snews") && !strcmp(p, ":563")) || + (!strcmp(access, "finger") && !strcmp(p, ":79")) || + (!strcmp(access, "cso") && !strcmp(p, ":105"))) + *p = '\0'; /* It is the default: ignore it */ + } + if (p == NULL) { + int len = strlen(tail); + + if (len > 0) { + h = tail + len - 1; /* last char of hostname */ + if (*h == '.') + *h = '\0'; /* chop final . */ + } + } else { + h = p; + h--; /* End of hostname */ + if (*h == '.') { + /* + ** Slide p over h. + */ + while (*p != '\0') + *h++ = *p++; + *h = '\0'; /* terminate */ + } + } + } +#endif /* CLEAN_URLS */ + } + + /* + ** If different hosts, inherit no path. + */ + if (given.host && related.host) + if (strcmp(given.host, related.host) != 0) { + related.absolute = NULL; + related.relative = NULL; + related.anchor = NULL; + } + + /* + ** Handle the path. + */ + if (wanted & PARSE_PATH) { + if (access && !given.absolute && given.relative) { + if (!strcasecomp(access, "nntp") || + !strcasecomp(access, "snews") || + (!strcasecomp(access, "news") && + !strncasecomp(result, "news://", 7))) { + /* + * Treat all given nntp or snews paths, + * or given paths for news URLs with a host, + * as absolute. + */ + given.absolute = given.relative; + given.relative = NULL; + } + } + if (given.absolute) { /* All is given */ + if (wanted & PARSE_PUNCTUATION) + strcat(result, "/"); + strcat(result, given.absolute); + if (TRACE) + fprintf(stderr, "1\n"); + } else if (related.absolute) { /* Adopt path not name */ + strcat(result, "/"); + strcat(result, related.absolute); + if (given.relative) { + p = strchr(result, '?'); /* Search part? */ + if (p == NULL) + p = (result + strlen(result) - 1); + for (; *p != '/'; p--); /* last / */ + p[1] = '\0'; /* Remove filename */ + strcat(result, given.relative); /* Add given one */ + HTSimplify(result); + } + if (TRACE) + fprintf(stderr, "2\n"); + } else if (given.relative) { + strcat(result, given.relative); /* what we've got */ + if (TRACE) + fprintf(stderr, "3\n"); + } else if (related.relative) { + strcat(result, related.relative); + if (TRACE) + fprintf(stderr, "4\n"); + } else { /* No inheritance */ + if (strncasecomp(aName, "lynxcgi:", 8) && + strncasecomp(aName, "lynxexec:", 9) && + strncasecomp(aName, "lynxprog:", 9)) { + strcat(result, "/"); + } + if (!strcmp(result, "news:/")) + result[5] = '*'; + if (TRACE) + fprintf(stderr, "5\n"); + } + } + + /* + ** Handle the fragment (anchor). + */ + if (wanted & PARSE_ANCHOR) + if ((given.anchor && *given.anchor) || (!given.anchor && related.anchor)) { + if (wanted & PARSE_PUNCTUATION) + strcat(result, "#"); + strcat(result, (given.anchor) ? given.anchor : related.anchor); + } + if (TRACE) + fprintf(stderr, "HTParse: result:%s\n", result); + FREE(rel); + FREE(name); + + StrAllocCopy(return_value, result); + FREE(result); + + return return_value; /* exactly the right length */ + } /* Simplify a filename. HTSimplify() ** -------------------- @@ -562,83 +489,71 @@ ARGS2 ( ** ** or ../../albert.html */ - PUBLIC void HTSimplify ARGS1 ( - char *, filename) - { - char *p; - char *q, *q1; - - if (filename == NULL) - return; - - if ((filename[0] && filename[1]) && strchr (filename, '/') != NULL) - { - for (p = (filename + 2); *p; p++) - { - if (*p == '/') - { - if ((p[1] == '.') && (p[2] == '.') && - (p[3] == '/' || p[3] == '\0')) - { - /* - ** Handle "/../" or "/..". - */ - for (q = (p - 1); (q >= filename) && (*q != '/'); q--) - /* - ** Back up to previous slash or beginning of string. - */ - ; - if ((q[0] == '/') && strncmp (q, "/../", 4) && - !((q - 1) > filename && q[-1] == '/')) - { - /* - ** Not at beginning of string or in a - ** host field, so remove the "/xxx/..". - */ - q1 = (p + 3); - p = q; - while (*q1 != '\0') - *p++ = *q1++; - *p = '\0'; /* terminate */ + PUBLIC void HTSimplify ARGS1(char *, filename) { + char *p; + char *q, *q1; + + if (filename == NULL) + return; + + if ((filename[0] && filename[1]) && strchr(filename, '/') != NULL) { + for (p = (filename + 2); *p; p++) { + if (*p == '/') { + if ((p[1] == '.') && (p[2] == '.') && + (p[3] == '/' || p[3] == '\0')) { + /* + ** Handle "/../" or "/..". + */ + for (q = (p - 1); (q >= filename) && (*q != '/'); q--) + /* + ** Back up to previous slash or beginning of string. + */ + ; + if ((q[0] == '/') && strncmp(q, "/../", 4) && + !((q - 1) > filename && q[-1] == '/')) { + /* + ** Not at beginning of string or in a + ** host field, so remove the "/xxx/..". + */ + q1 = (p + 3); + p = q; + while (*q1 != '\0') + *p++ = *q1++; + *p = '\0'; /* terminate */ #ifdef NOTDEFINED - /* - ** Make sure filename has at least one slash. - */ - if (*filename == '\0') - { - *filename = '/'; - *(filename + 1) = '\0'; - } -#endif /* NOTDEFINED */ - /* - ** Start again with previous slash. - */ - p = (q - 1); - } - } - else if (p[1] == '.' && p[2] == '/') - { - /* - ** Handle "./" by removing the characters. - */ - q = p; - q1 = (p + 2); - while (*q1 != '\0') - *q++ = *q1++; - *q = '\0'; /* terminate */ - p--; - } - else if (p[1] == '.' && p[2] == '\0') - { - /* - ** Handle terminal "." by removing the character. - */ - p[1] = '\0'; - } - } - } - } - } + /* + ** Make sure filename has at least one slash. + */ + if (*filename == '\0') { + *filename = '/'; + *(filename + 1) = '\0'; + } +#endif /* NOTDEFINED */ + /* + ** Start again with previous slash. + */ + p = (q - 1); + } + } else if (p[1] == '.' && p[2] == '/') { + /* + ** Handle "./" by removing the characters. + */ + q = p; + q1 = (p + 2); + while (*q1 != '\0') + *q++ = *q1++; + *q = '\0'; /* terminate */ + p--; + } else if (p[1] == '.' && p[2] == '\0') { + /* + ** Handle terminal "." by removing the character. + */ + p[1] = '\0'; + } + } + } + } + } /* Make Relative Name. HTRelative() ** ------------------- @@ -657,63 +572,51 @@ ARGS2 ( ** The caller is responsible for freeing the resulting name later. ** */ - PUBLIC char *HTRelative ARGS2 ( - CONST char *, aName, - CONST char *, relatedName) - { - char *result = NULL; - CONST char *p = aName; - CONST char *q = relatedName; - CONST char *after_access = NULL; - CONST char *path = NULL; - CONST char *last_slash = NULL; - int slashes = 0; - - for (; *p; p++, q++) - { /* Find extent of match */ - if (*p != *q) - break; - if (*p == ':') - after_access = p + 1; - if (*p == '/') - { - last_slash = p; - slashes++; - if (slashes == 3) - path = p; - } - } - - /* q, p point to the first non-matching character or zero */ - - if (!after_access) - { /* Different access */ - StrAllocCopy (result, aName); - } - else if (slashes < 3) - { /* Different nodes */ - StrAllocCopy (result, after_access); - } - else if (slashes == 3) - { /* Same node, different path */ - StrAllocCopy (result, path); - } - else - { /* Some path in common */ - int levels = 0; - for (; *q && (*q != '#'); q++) - if (*q == '/') - levels++; - result = (char *) malloc (3 * levels + strlen (last_slash) + 1); - if (result == NULL) - outofmem (__FILE__, "HTRelative"); - result[0] = '\0'; - for (; levels; levels--) - strcat (result, "../"); - strcat (result, last_slash + 1); - } - if (TRACE) - fprintf (stderr, "HT: `%s' expressed relative to\n `%s' is\n `%s'.", - aName, relatedName, result); - return result; - } + PUBLIC char *HTRelative ARGS2(CONST char *, aName, CONST char *, relatedName) { + char *result = NULL; + CONST char *p = aName; + CONST char *q = relatedName; + CONST char *after_access = NULL; + CONST char *path = NULL; + CONST char *last_slash = NULL; + int slashes = 0; + + for (; *p; p++, q++) { /* Find extent of match */ + if (*p != *q) + break; + if (*p == ':') + after_access = p + 1; + if (*p == '/') { + last_slash = p; + slashes++; + if (slashes == 3) + path = p; + } + } + + /* q, p point to the first non-matching character or zero */ + + if (!after_access) { /* Different access */ + StrAllocCopy(result, aName); + } else if (slashes < 3) { /* Different nodes */ + StrAllocCopy(result, after_access); + } else if (slashes == 3) { /* Same node, different path */ + StrAllocCopy(result, path); + } else { /* Some path in common */ + int levels = 0; + for (; *q && (*q != '#'); q++) + if (*q == '/') + levels++; + result = (char *) malloc(3 * levels + strlen(last_slash) + 1); + if (result == NULL) + outofmem(__FILE__, "HTRelative"); + result[0] = '\0'; + for (; levels; levels--) + strcat(result, "../"); + strcat(result, last_slash + 1); + } + if (TRACE) + fprintf(stderr, "HT: `%s' expressed relative to\n `%s' is\n `%s'.", + aName, relatedName, result); + return result; + } diff --git a/SRC/contrib/adamsnames/domquery.c b/SRC/contrib/adamsnames/domquery.c index 442fd80..536ca23 100644 --- a/SRC/contrib/adamsnames/domquery.c +++ b/SRC/contrib/adamsnames/domquery.c @@ -21,91 +21,91 @@ void domquery_usage(char *msg) { - fprintf(stderr, "%s\n", msg); - poptPrintUsage(poptcon, stderr, 0); - err_quit(" domain"); + fprintf(stderr, "%s\n", msg); + poptPrintUsage(poptcon, stderr, 0); + err_quit(" domain"); } char * init(int argc, char **argv) { - int value; - xmlrpc_value *result; - xmlrpc_bool free, read_contacts; - xmlrpc_int32 reason; - char *msg, *hostname; - - struct poptOption options[] = { - {"read-contacts", 'c', POPT_ARG_NONE, &read_contacts, 0, - "Read also the contacts of the domain [NOT IMPLEMENTED]", - ""}, - POPT_AUTOHELP POPT_TABLEEND - }; - poptcon = poptGetContext(NULL, argc, argv, options, POPT_CONTEXT_KEEP_FIRST); - while ((value = poptGetNextOpt(poptcon)) > 0) { - if (value < -1) { - sprintf(msg, "%s: %s", - poptBadOption(poptcon, POPT_BADOPTION_NOALIAS), - poptStrerror(value)); - domquery_usage(msg); - } - } - hostname = (char *) poptGetArg(poptcon); /* Not used */ - domain = (char *) poptGetArg(poptcon); - if (domain == NULL || !strcmp(domain, "")) - domquery_usage("Mandatory request missing"); - - return NULL; + int value; + xmlrpc_value *result; + xmlrpc_bool free, read_contacts; + xmlrpc_int32 reason; + char *msg, *hostname; + + struct poptOption options[] = { + {"read-contacts", 'c', POPT_ARG_NONE, &read_contacts, 0, + "Read also the contacts of the domain [NOT IMPLEMENTED]", + ""}, + POPT_AUTOHELP POPT_TABLEEND + }; + poptcon = poptGetContext(NULL, argc, argv, options, POPT_CONTEXT_KEEP_FIRST); + while ((value = poptGetNextOpt(poptcon)) > 0) { + if (value < -1) { + sprintf(msg, "%s: %s", + poptBadOption(poptcon, POPT_BADOPTION_NOALIAS), + poptStrerror(value)); + domquery_usage(msg); + } + } + hostname = (char *) poptGetArg(poptcon); /* Not used */ + domain = (char *) poptGetArg(poptcon); + if (domain == NULL || !strcmp(domain, "")) + domquery_usage("Mandatory request missing"); + + return NULL; } void start_raw() { - /* Start up our XML-RPC client library. */ - xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, CLIENT_NAME, CLIENT_VERSION); + /* Start up our XML-RPC client library. */ + xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, CLIENT_NAME, CLIENT_VERSION); - /* Initialize our error-handling environment. */ - xmlrpc_env_init(&env); + /* Initialize our error-handling environment. */ + xmlrpc_env_init(&env); - printf("env initialized\n"); + printf("env initialized\n"); } int execute() { - xmlrpc_value *result; - xmlrpc_value *domain_h; - xmlrpc_int32 found; - xmlrpc_value *error; - /* char *dst; dst = HTAnchor_findAddress(ENDPOINT); */ - printf("Ready to call\n"); - /* Call the server */ - result = xmlrpc_client_call(&env, ENDPOINT, "domquery", "(s)", domain); - die_if_fault_occurred(&env); - printf("Call done, now parsing\n"); - - xmlrpc_parse_value(&env, result, "{s:i,*}", "found", &found); - die_if_fault_occurred(&env); - if (found) { - printf("%s is there\n", domain); - xmlrpc_parse_value(&env, result, "{s:S,i,s:A,*}", "domain", - &domain_h, "found", &found, "error", &error); - die_if_fault_occurred(&env); - } - /* Dispose of our result value. */ - xmlrpc_DECREF(result); - return 0; + xmlrpc_value *result; + xmlrpc_value *domain_h; + xmlrpc_int32 found; + xmlrpc_value *error; + /* char *dst; dst = HTAnchor_findAddress(ENDPOINT); */ + printf("Ready to call\n"); + /* Call the server */ + result = xmlrpc_client_call(&env, ENDPOINT, "domquery", "(s)", domain); + die_if_fault_occurred(&env); + printf("Call done, now parsing\n"); + + xmlrpc_parse_value(&env, result, "{s:i,*}", "found", &found); + die_if_fault_occurred(&env); + if (found) { + printf("%s is there\n", domain); + xmlrpc_parse_value(&env, result, "{s:S,i,s:A,*}", "domain", + &domain_h, "found", &found, "error", &error); + die_if_fault_occurred(&env); + } + /* Dispose of our result value. */ + xmlrpc_DECREF(result); + return 0; } void terminate() { - /* Clean up our error-handling environment. */ - xmlrpc_env_clean(&env); + /* Clean up our error-handling environment. */ + xmlrpc_env_clean(&env); - /* Shutdown our XML-RPC client library. */ - xmlrpc_client_cleanup(); + /* Shutdown our XML-RPC client library. */ + xmlrpc_client_cleanup(); } diff --git a/SRC/contrib/adamsnames/test-domquery.c b/SRC/contrib/adamsnames/test-domquery.c index 573eb65..c57ad42 100644 --- a/SRC/contrib/adamsnames/test-domquery.c +++ b/SRC/contrib/adamsnames/test-domquery.c @@ -16,65 +16,65 @@ void die_if_fault_occurred(xmlrpc_env * env) { - if (env->fault_occurred) { - fprintf(stderr, "XML-RPC Fault: %s (%d)\n", - env->fault_string, env->fault_code); - exit(1); - } + if (env->fault_occurred) { + fprintf(stderr, "XML-RPC Fault: %s (%d)\n", + env->fault_string, env->fault_code); + exit(1); + } } int main(int argc, char **argv) { - int value; - xmlrpc_value *result; - xmlrpc_bool free, read_contacts; - xmlrpc_int32 reason; - xmlrpc_value *domain_h; - xmlrpc_int32 found; - xmlrpc_value *error; - xmlrpc_env env; - char *domain; - char *date, *holder; + int value; + xmlrpc_value *result; + xmlrpc_bool free, read_contacts; + xmlrpc_int32 reason; + xmlrpc_value *domain_h; + xmlrpc_int32 found; + xmlrpc_value *error; + xmlrpc_env env; + char *domain; + char *date, *holder; - if (argc != 2) { - fprintf(stderr, "Usage: %s domain\n", argv[0]); - exit(1); - } + if (argc != 2) { + fprintf(stderr, "Usage: %s domain\n", argv[0]); + exit(1); + } - domain = argv[1]; + domain = argv[1]; - /* Start up our XML-RPC client library. */ - xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, CLIENT_NAME, CLIENT_VERSION); + /* Start up our XML-RPC client library. */ + xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, CLIENT_NAME, CLIENT_VERSION); - /* Initialize our error-handling environment. */ - xmlrpc_env_init(&env); + /* Initialize our error-handling environment. */ + xmlrpc_env_init(&env); - /* Call the server */ - result = xmlrpc_client_call(&env, ENDPOINT, "domquery", "(s)", domain); - die_if_fault_occurred(&env); + /* Call the server */ + result = xmlrpc_client_call(&env, ENDPOINT, "domquery", "(s)", domain); + die_if_fault_occurred(&env); - xmlrpc_parse_value(&env, result, "{s:i,*}", "found", &found); - die_if_fault_occurred(&env); - if (found) { - printf("%s is there\n", domain); - xmlrpc_parse_value(&env, result, "{s:S,s:i,s:A,*}", "domain", - &domain_h, "found", &found, "error", &error); - die_if_fault_occurred(&env); - /* printf ("Type of domain: %d\n", xmlrpc_value_type(domain_h)); */ - xmlrpc_parse_value(&env, domain_h, "{s:s,s:s,*}", "registered", - &date, "org", &holder); - die_if_fault_occurred(&env); - printf("Registered on %s by %s\n", date, holder); - } else { - printf("Unknown domain %s\n", domain); - } - /* Dispose of our result value. */ - xmlrpc_DECREF(result); - /* Clean up our error-handling environment. */ - xmlrpc_env_clean(&env); + xmlrpc_parse_value(&env, result, "{s:i,*}", "found", &found); + die_if_fault_occurred(&env); + if (found) { + printf("%s is there\n", domain); + xmlrpc_parse_value(&env, result, "{s:S,s:i,s:A,*}", "domain", + &domain_h, "found", &found, "error", &error); + die_if_fault_occurred(&env); + /* printf ("Type of domain: %d\n", xmlrpc_value_type(domain_h)); */ + xmlrpc_parse_value(&env, domain_h, "{s:s,s:s,*}", "registered", + &date, "org", &holder); + die_if_fault_occurred(&env); + printf("Registered on %s by %s\n", date, holder); + } else { + printf("Unknown domain %s\n", domain); + } + /* Dispose of our result value. */ + xmlrpc_DECREF(result); + /* Clean up our error-handling environment. */ + xmlrpc_env_clean(&env); - /* Shutdown our XML-RPC client library. */ - xmlrpc_client_cleanup(); + /* Shutdown our XML-RPC client library. */ + xmlrpc_client_cleanup(); } diff --git a/SRC/contrib/adamsnames/util.c b/SRC/contrib/adamsnames/util.c index 5c7a230..295536e 100644 --- a/SRC/contrib/adamsnames/util.c +++ b/SRC/contrib/adamsnames/util.c @@ -6,8 +6,7 @@ void die_if_fault_occurred(xmlrpc_env * env) { - if (env->fault_occurred) { - err_quit("XML-RPC Fault: %s (%d)\n", - env->fault_string, env->fault_code); - } + if (env->fault_occurred) { + err_quit("XML-RPC Fault: %s (%d)\n", env->fault_string, env->fault_code); + } } diff --git a/SRC/contrib/daytime/daytime.c b/SRC/contrib/daytime/daytime.c index b0a2b4b..7f9e95d 100644 --- a/SRC/contrib/daytime/daytime.c +++ b/SRC/contrib/daytime/daytime.c @@ -14,39 +14,38 @@ echoping_options options; char * init(const int argc, const char **argv, echoping_options global_options) { - if (global_options.udp) - err_quit - ("Sorry, UDP is not yet compatible with this daytime plugin"); - options = global_options; - return "daytime"; + if (global_options.udp) + err_quit("Sorry, UDP is not yet compatible with this daytime plugin"); + options = global_options; + return "daytime"; } void start(struct addrinfo *res) { - daytime_server = *res; + daytime_server = *res; } int execute() { - int nr; - FILE *file; + int nr; + FILE *file; #define MAX 256 - char recvline[MAX]; - if ((sockfd = - socket(daytime_server.ai_family, daytime_server.ai_socktype, - daytime_server.ai_protocol)) < 0) - err_sys("Can't open socket"); - if (connect(sockfd, daytime_server.ai_addr, daytime_server.ai_addrlen) < 0) - err_sys("Can't connect to server"); - if ((file = fdopen(sockfd, "r")) == NULL) - err_sys("Cannot fdopen"); - nr = readline(file, recvline, MAX, 1); - if (options.verbose) - printf("%s", recvline); - close(sockfd); - return 1; + char recvline[MAX]; + if ((sockfd = + socket(daytime_server.ai_family, daytime_server.ai_socktype, + daytime_server.ai_protocol)) < 0) + err_sys("Can't open socket"); + if (connect(sockfd, daytime_server.ai_addr, daytime_server.ai_addrlen) < 0) + err_sys("Can't connect to server"); + if ((file = fdopen(sockfd, "r")) == NULL) + err_sys("Cannot fdopen"); + nr = readline(file, recvline, MAX, 1); + if (options.verbose) + printf("%s", recvline); + close(sockfd); + return 1; } void diff --git a/SRC/contrib/small-services/small-services.c b/SRC/contrib/small-services/small-services.c index b327200..d8b6af6 100644 --- a/SRC/contrib/small-services/small-services.c +++ b/SRC/contrib/small-services/small-services.c @@ -13,45 +13,43 @@ echoping_options options; char * init(const int argc, const char **argv, echoping_options global_options) { - options = global_options; - /* TODO: the service returned must depend on the options */ - return "echo"; + options = global_options; + /* TODO: the service returned must depend on the options */ + return "echo"; } void start(struct addrinfo *res) { - smallservices_server = *res; + smallservices_server = *res; } int execute() { - int nr; + int nr; #define MAX 256 #define TEST_STRING "test" - char result[MAX]; - if ((sockfd = - socket(smallservices_server.ai_family, smallservices_server.ai_socktype, - smallservices_server.ai_protocol)) < 0) - err_sys("Can't open socket"); - if (connect - (sockfd, smallservices_server.ai_addr, - smallservices_server.ai_addrlen) < 0) - err_sys("Can't connect to server"); - if (write(sockfd, TEST_STRING, strlen(TEST_STRING)) != strlen(TEST_STRING)) - err_sys("Cannot write"); - nr = read(sockfd, result, strlen(TEST_STRING)); - if (nr != strlen(TEST_STRING)) - err_sys("Cannot read (only %i bytes)", nr); /* TODO: the server - * may send the - * result in chunks, - * we should loop */ - if (strcmp(result, TEST_STRING) != 0) - err_sys("Result \"%s\" is different from test string \"%s\"", - result, TEST_STRING); - close(sockfd); - return 1; + char result[MAX]; + if ((sockfd = + socket(smallservices_server.ai_family, smallservices_server.ai_socktype, + smallservices_server.ai_protocol)) < 0) + err_sys("Can't open socket"); + if (connect + (sockfd, smallservices_server.ai_addr, smallservices_server.ai_addrlen) < 0) + err_sys("Can't connect to server"); + if (write(sockfd, TEST_STRING, strlen(TEST_STRING)) != strlen(TEST_STRING)) + err_sys("Cannot write"); + nr = read(sockfd, result, strlen(TEST_STRING)); + if (nr != strlen(TEST_STRING)) + err_sys("Cannot read (only %i bytes)", nr); /* TODO: the server may send + * the result in chunks, we + * should loop */ + if (strcmp(result, TEST_STRING) != 0) + err_sys("Result \"%s\" is different from test string \"%s\"", + result, TEST_STRING); + close(sockfd); + return 1; } void diff --git a/SRC/echoping.c b/SRC/echoping.c index b81f616..c3d0a43 100644 --- a/SRC/echoping.c +++ b/SRC/echoping.c @@ -18,7 +18,7 @@ char *progname; * process. Useless but harmless otherwise. In practice, while OSF/1 is happy * with it, SunOS refuses to use fflush on a NULL and Linux fails. */ -#undef FLUSH_OUTPUT /* Not really supported, see the TODO */ +#undef FLUSH_OUTPUT /* Not really supported, see the TODO */ /* Global variables for main and printstats */ @@ -40,1547 +40,1441 @@ extern int tvcmp(); int main(argc, argv) - int argc; - const char *argv[]; + int argc; + const char *argv[]; { - int result; - int remaining = argc; - char **leftover; + int result; + int remaining = argc; + char **leftover; - int sockfd = -1; - struct addrinfo hints, *res; + int sockfd = -1; + struct addrinfo hints, *res; #ifdef LIBIDN - struct addrinfo hints_numeric; + struct addrinfo hints_numeric; #endif - int error; - char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; + int error; + char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; #ifdef NI_WITHSCOPEID - const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID; + const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID; #else - const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; + const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; #endif - FILE *files = NULL; - CHANNEL channel; - int verbose = FALSE; - int dump_config = FALSE; - int module_find = FALSE; - int n, nr = 0; + FILE *files = NULL; + CHANNEL channel; + int verbose = FALSE; + int dump_config = FALSE; + int module_find = FALSE; + int n, nr = 0; #ifdef OPENSSL - int sslcode; - char rand_file[MAX_LINE]; + int sslcode; + char rand_file[MAX_LINE]; #endif - char *sendline, recvline[MAX_LINE + 1]; - boolean accept_http_redirects = FALSE; - char *http_hostname = NULL; + char *sendline, recvline[MAX_LINE + 1]; + boolean accept_http_redirects = FALSE; + char *http_hostname = NULL; #ifdef ICP - char retcode[DEFLINE]; - int length; + char retcode[DEFLINE]; + int length; #endif - struct timeval newtv, oldtv; - void printstats(); + struct timeval newtv, oldtv; + void printstats(); #ifdef HAVE_USLEEP - float wait = 1.0; + float wait = 1.0; #else - unsigned int wait = 1; -#endif - unsigned char fill = ' '; - char *fill_s; - boolean fill_requested = FALSE; - unsigned int i = 0; - char *plugin_name = NULL; - char *complete_plugin_name = NULL; - char *ext; - void *plugin = NULL; - int plugin_result = -3; /* Initialize to illegal value */ + unsigned int wait = 1; +#endif + unsigned char fill = ' '; + char *fill_s; + boolean fill_requested = FALSE; + unsigned int i = 0; + char *plugin_name = NULL; + char *complete_plugin_name = NULL; + char *ext; + void *plugin = NULL; + int plugin_result = -3; /* Initialize to illegal value */ - void to_alarm(); /* our alarm() signal handler */ - void interrupted(); - unsigned int timeout = 10; - boolean timeout_requested = 0; - boolean size_requested = 0; - char *url = ""; - boolean measure_data_transfer_only = FALSE; + void to_alarm(); /* our alarm() signal handler */ + void interrupted(); + unsigned int timeout = 10; + boolean timeout_requested = 0; + boolean size_requested = 0; + char *url = ""; + boolean measure_data_transfer_only = FALSE; #if USE_SIGACTION - struct sigaction mysigaction; + struct sigaction mysigaction; #endif - char *plugin_port_name, *port_name; - boolean plugin_raw = FALSE; - boolean port_to_use = USE_ECHO; - boolean http = 0; - boolean smtp = 0; - boolean discard = 0; - boolean chargen = 0; - boolean udp = 0; - boolean icp = 0; + char *plugin_port_name, *port_name; + boolean plugin_raw = FALSE; + boolean port_to_use = USE_ECHO; + boolean http = 0; + boolean smtp = 0; + boolean discard = 0; + boolean chargen = 0; + boolean udp = 0; + boolean icp = 0; - boolean nocache = 0; + boolean nocache = 0; #ifdef ICP - icp_opcode opcode = ICP_OP_QUERY; + icp_opcode opcode = ICP_OP_QUERY; #endif - boolean tcp = FALSE; - boolean ssl = FALSE; + boolean tcp = FALSE; + boolean ssl = FALSE; - boolean stop_at_newlines = 1; + boolean stop_at_newlines = 1; #ifdef OPENSSL - SSL_METHOD *meth; - SSL_CTX *ctx = NULL; - SSL *sslh = NULL; + SSL_METHOD *meth; + SSL_CTX *ctx = NULL; + SSL *sslh = NULL; #endif #ifdef GNUTLS - gnutls_session session; - gnutls_certificate_credentials xcred; - int tls_result; - const int cert_type_priority[3] = { GNUTLS_CRT_X509, - GNUTLS_CRT_OPENPGP, 0 - }; + gnutls_session session; + gnutls_certificate_credentials xcred; + int tls_result; + const int cert_type_priority[3] = { GNUTLS_CRT_X509, + GNUTLS_CRT_OPENPGP, 0 + }; #endif - int priority; - int priority_requested = 0; - int tos; - int tos_requested = 0; - int protocol; - boolean sctp_requested = FALSE; - int sctp = 0; + int priority; + int priority_requested = 0; + int tos; + int tos_requested = 0; + int protocol; + boolean sctp_requested = FALSE; + int sctp = 0; #ifdef HAVE_TCP_INFO - struct tcp_info tcpinfo; - socklen_t socket_length = sizeof(tcpinfo); + struct tcp_info tcpinfo; + socklen_t socket_length = sizeof(tcpinfo); #endif - char *p; - echoping_options global_options; + char *p; + echoping_options global_options; - /* popt variables */ - const struct poptOption options[] = { - {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v'}, - {"dump-configuration", 'V', POPT_ARG_NONE, &dump_config, 'V', - "Displays echoping compiled-in configuration"}, - {"help", '?', POPT_ARG_NONE, NULL, '?'}, - {"size", 's', POPT_ARG_INT, &size, 's'}, - {"number", 'n', POPT_ARG_INT, &number, 'n', "Number of iterations"}, + /* popt variables */ + const struct poptOption options[] = { + {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v'}, + {"dump-configuration", 'V', POPT_ARG_NONE, &dump_config, 'V', + "Displays echoping compiled-in configuration"}, + {"help", '?', POPT_ARG_NONE, NULL, '?'}, + {"size", 's', POPT_ARG_INT, &size, 's'}, + {"number", 'n', POPT_ARG_INT, &number, 'n', "Number of iterations"}, #ifdef HAVE_USLEEP - {"wait", 'w', POPT_ARG_FLOAT, &wait, 'w', - "Delay between iterations"}, + {"wait", 'w', POPT_ARG_FLOAT, &wait, 'w', + "Delay between iterations"}, #else - {"wait", 'w', POPT_ARG_INT, &wait, 'w', "Delay between iterations"}, -#endif - {"discard", 'd', POPT_ARG_NONE, &discard, 'd'}, - {"chargen", 'c', POPT_ARG_NONE, &chargen, 'c'}, - {"http", 'h', POPT_ARG_STRING, &url, 'h'}, - {"accept-http-redirects", 'R', POPT_ARG_NONE, &accept_http_redirects, - 'R', - "Accept HTTP return codes 3xx (redirections)"}, - {"hostname", 'H', POPT_ARG_STRING, &http_hostname, 'H', - "Hostname to use in HTTP Host: header"}, - {"icp", 'i', POPT_ARG_STRING, &url, 'i', - "ICP protocol, for Web proxies/caches"}, - {"udp", 'u', POPT_ARG_NONE, &udp, 'u'}, - {"timeout", 't', POPT_ARG_INT, &timeout, 't'}, - {"fill", 'f', POPT_ARG_STRING, &fill_s, 'f'}, - {"smtp", 'S', POPT_ARG_NONE, &smtp, 'S'}, - {"ssl", 'C', POPT_ARG_NONE, &ssl, 'C'}, - {"priority", 'p', POPT_ARG_INT, &priority, 'p'}, - {"type-of-service", 'P', POPT_ARG_INT, &tos, 'P'}, - {"sctp", 'T', POPT_ARG_NONE, &sctp, 'T'}, - {"check-original", 'a', POPT_ARG_NONE, NULL, 'a', - "For HTTP through a proxy/cache"}, - {"ignore-cache", 'A', POPT_ARG_NONE, NULL, 'A', - "For HTTP through a proxy/cache"}, - {"ipv4", '4', POPT_ARG_NONE, NULL, '4'}, - {"ipv6", '6', POPT_ARG_NONE, NULL, '6'}, - {"module", 'm', POPT_ARG_STRING, &plugin_name, 'm', - "Loads the given plugin"}, - {"data-only", 'D', POPT_ARG_NONE, NULL, 'D'}, - {"num-std-dev", 'N', POPT_ARG_INT, &n_stddev, 'N', - "Number of standard deviations to classify outliers"}, - POPT_TABLEEND - }; - poptContext poptcon; + {"wait", 'w', POPT_ARG_INT, &wait, 'w', "Delay between iterations"}, +#endif + {"discard", 'd', POPT_ARG_NONE, &discard, 'd'}, + {"chargen", 'c', POPT_ARG_NONE, &chargen, 'c'}, + {"http", 'h', POPT_ARG_STRING, &url, 'h'}, + {"accept-http-redirects", 'R', POPT_ARG_NONE, &accept_http_redirects, + 'R', + "Accept HTTP return codes 3xx (redirections)"}, + {"hostname", 'H', POPT_ARG_STRING, &http_hostname, 'H', + "Hostname to use in HTTP Host: header"}, + {"icp", 'i', POPT_ARG_STRING, &url, 'i', + "ICP protocol, for Web proxies/caches"}, + {"udp", 'u', POPT_ARG_NONE, &udp, 'u'}, + {"timeout", 't', POPT_ARG_INT, &timeout, 't'}, + {"fill", 'f', POPT_ARG_STRING, &fill_s, 'f'}, + {"smtp", 'S', POPT_ARG_NONE, &smtp, 'S'}, + {"ssl", 'C', POPT_ARG_NONE, &ssl, 'C'}, + {"priority", 'p', POPT_ARG_INT, &priority, 'p'}, + {"type-of-service", 'P', POPT_ARG_INT, &tos, 'P'}, + {"sctp", 'T', POPT_ARG_NONE, &sctp, 'T'}, + {"check-original", 'a', POPT_ARG_NONE, NULL, 'a', + "For HTTP through a proxy/cache"}, + {"ignore-cache", 'A', POPT_ARG_NONE, NULL, 'A', + "For HTTP through a proxy/cache"}, + {"ipv4", '4', POPT_ARG_NONE, NULL, '4'}, + {"ipv6", '6', POPT_ARG_NONE, NULL, '6'}, + {"module", 'm', POPT_ARG_STRING, &plugin_name, 'm', + "Loads the given plugin"}, + {"data-only", 'D', POPT_ARG_NONE, NULL, 'D'}, + {"num-std-dev", 'N', POPT_ARG_INT, &n_stddev, 'N', + "Number of standard deviations to classify outliers"}, + POPT_TABLEEND + }; + poptContext poptcon; - global_options.udp = FALSE; - global_options.verbose = FALSE; + global_options.udp = FALSE; + global_options.verbose = FALSE; - null_timeval.tv_sec = 0; - null_timeval.tv_usec = 0; - max_timeval.tv_sec = 1000000000; - max_timeval.tv_usec = 999999; + null_timeval.tv_sec = 0; + null_timeval.tv_usec = 0; + max_timeval.tv_sec = 1000000000; + max_timeval.tv_usec = 999999; - return_code = 0; - number = 1; - total = null_timeval; - median = null_timeval; - max = null_timeval; - min = max_timeval; - stddev = null_timeval; - port_name = malloc(NI_MAXSERV); - strcpy(port_name, ECHO_TCP_PORT); + return_code = 0; + number = 1; + total = null_timeval; + median = null_timeval; + 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++) { - results[i].valid = 0; - } - progname = (char *) argv[0]; + for (i = 0; i <= MAX_ITERATIONS; i++) { + results[i].valid = 0; + } + progname = (char *) argv[0]; - poptcon = - poptGetContext(NULL, argc, argv, options, POPT_CONTEXT_POSIXMEHARDER); + poptcon = poptGetContext(NULL, argc, argv, options, POPT_CONTEXT_POSIXMEHARDER); - while ((result = poptGetNextOpt(poptcon)) != -1) { - if (result < -1) { - fprintf(stderr, "%s: %s\n", - poptBadOption(poptcon, POPT_BADOPTION_NOALIAS), - poptStrerror(result)); - usage(poptcon); - } - remaining--; - switch ((char) result) { - case '?': - poptPrintHelp(poptcon, stdout, 0); - fprintf(stdout, " hostname [plugin-options...]\n"); - fprintf(stdout, - " (You can get a list of available plugins with \"ls %s\")\n", - PLUGINS_DIR); - exit(0); - case 'V': - printf("%s\n", COMPILATION_OPTIONS); - exit(0); - case 'v': - break; - case 'r': - break; - case 'u': - break; - case 'C': - break; - case 'd': - strcpy(port_name, DISCARD_TCP_PORT); - port_to_use = USE_DISCARD; - break; - case 'c': - strcpy(port_name, CHARACTER_GENERATOR_TCP_PORT); - port_to_use = USE_CHARGEN; - stop_at_newlines = 0; - break; - case 'i': - remaining--; - strcpy(port_name, DEFAULT_ICP_UDP_PORT); - port_to_use = USE_ICP; - udp = 1; - icp = 1; - break; - case 'h': - remaining--; - strcpy(port_name, DEFAULT_HTTP_TCP_PORT); - port_to_use = USE_HTTP; - http = 1; - break; - case 'R': - accept_http_redirects = TRUE; - break; - case 'H': - remaining--; - break; - case 'a': - nocache = 1; - break; - case 'A': - nocache = 2; - break; - case 'f': - remaining--; - if (strlen(fill_s) > 1) - err_quit - ("Argument --fill should be a one-character string"); - fill = fill_s[0]; - fill_requested = 1; - break; - case 'S': - strcpy(port_name, "smtp"); - port_to_use = USE_SMTP; - break; - case 'D': - measure_data_transfer_only = TRUE; - break; - case 'N': - remaining--; - break; - case 'p': - remaining--; - priority_requested = 1; - break; - case 'P': - remaining--; - tos_requested = 1; - break; - case 'T': - sctp_requested = TRUE; - break; - case 's': - remaining--; - if (size > MAX_LINE) { - (void) fprintf(stderr, - "%s: packet size too large, max is %d.\n", - progname, MAX_LINE); - exit(1); - } - if (size <= 0) { - (void) fprintf(stderr, "%s: illegal packet size.\n", - progname); - exit(1); - } - size_requested = 1; - break; - case 't': - remaining--; - timeout_requested = 1; - if (size <= 0) { - (void) fprintf(stderr, "%s: illegal timeout.\n", - progname); - exit(1); - } - break; - case 'n': - remaining--; - if (number > MAX_ITERATIONS) { - (void) fprintf(stderr, - "%s: number of iterations too large, max is %d.\n", - progname, MAX_ITERATIONS); - exit(1); - } - if (number <= 0) { - (void) fprintf(stderr, - "%s: illegal number of iterations.\n", - progname); - exit(1); - } - break; - case 'w': - remaining--; - if (wait <= 0) - /* - * atoi returns zero when there is an error. - * So we cannot use '-w 0' to specify no - * waiting. - */ - { - (void) fprintf(stderr, - "%s: illegal waiting time.\n", - progname); - exit(1); - } - break; - case '4': - family = AF_INET; - break; - case '6': - family = AF_INET6; - break; - case 'm': - remaining--; - module_find = TRUE; - break; - default: - printf("Unknown character option %d (%c)", result, - (char) result); - usage(poptcon); - } - } - if (udp && ((port_to_use == USE_CHARGEN) || - (port_to_use == USE_HTTP) || (port_to_use == USE_SMTP))) { - (void) fprintf(stderr, - "%s: I don't know how to use this port with UDP.\n", - progname); - exit(1); - } - if ((http || smtp) && (fill_requested)) { - (void) fprintf(stderr, - "%s: Filling incompatible with HTTP connections.\n", - progname); - exit(1); - } - if (n_stddev && number <= 2) { - (void) fprintf(stderr, - "%s: Average and standard deviation are meaningless since you perform only two or less iteration(s).\n", - progname); - exit(1); - } + while ((result = poptGetNextOpt(poptcon)) != -1) { + if (result < -1) { + fprintf(stderr, "%s: %s\n", + poptBadOption(poptcon, POPT_BADOPTION_NOALIAS), + poptStrerror(result)); + usage(poptcon); + } + remaining--; + switch ((char) result) { + case '?': + poptPrintHelp(poptcon, stdout, 0); + fprintf(stdout, " hostname [plugin-options...]\n"); + fprintf(stdout, + " (You can get a list of available plugins with \"ls %s\")\n", + PLUGINS_DIR); + exit(0); + case 'V': + printf("%s\n", COMPILATION_OPTIONS); + exit(0); + case 'v': + break; + case 'r': + break; + case 'u': + break; + case 'C': + break; + case 'd': + strcpy(port_name, DISCARD_TCP_PORT); + port_to_use = USE_DISCARD; + break; + case 'c': + strcpy(port_name, CHARACTER_GENERATOR_TCP_PORT); + port_to_use = USE_CHARGEN; + stop_at_newlines = 0; + break; + case 'i': + remaining--; + strcpy(port_name, DEFAULT_ICP_UDP_PORT); + port_to_use = USE_ICP; + udp = 1; + icp = 1; + break; + case 'h': + remaining--; + strcpy(port_name, DEFAULT_HTTP_TCP_PORT); + port_to_use = USE_HTTP; + http = 1; + break; + case 'R': + accept_http_redirects = TRUE; + break; + case 'H': + remaining--; + break; + case 'a': + nocache = 1; + break; + case 'A': + nocache = 2; + break; + case 'f': + remaining--; + if (strlen(fill_s) > 1) + err_quit("Argument --fill should be a one-character string"); + fill = fill_s[0]; + fill_requested = 1; + break; + case 'S': + strcpy(port_name, "smtp"); + port_to_use = USE_SMTP; + break; + case 'D': + measure_data_transfer_only = TRUE; + break; + case 'N': + remaining--; + break; + case 'p': + remaining--; + priority_requested = 1; + break; + case 'P': + remaining--; + tos_requested = 1; + break; + case 'T': + sctp_requested = TRUE; + break; + case 's': + remaining--; + if (size > MAX_LINE) { + (void) fprintf(stderr, + "%s: packet size too large, max is %d.\n", + progname, MAX_LINE); + exit(1); + } + if (size <= 0) { + (void) fprintf(stderr, "%s: illegal packet size.\n", progname); + exit(1); + } + size_requested = 1; + break; + case 't': + remaining--; + timeout_requested = 1; + if (size <= 0) { + (void) fprintf(stderr, "%s: illegal timeout.\n", progname); + exit(1); + } + break; + case 'n': + remaining--; + if (number > MAX_ITERATIONS) { + (void) fprintf(stderr, + "%s: number of iterations too large, max is %d.\n", + progname, MAX_ITERATIONS); + exit(1); + } + if (number <= 0) { + (void) fprintf(stderr, + "%s: illegal number of iterations.\n", progname); + exit(1); + } + break; + case 'w': + remaining--; + if (wait <= 0) + /* + * atoi returns zero when there is an error. + * So we cannot use '-w 0' to specify no + * waiting. + */ + { + (void) fprintf(stderr, "%s: illegal waiting time.\n", progname); + exit(1); + } + break; + case '4': + family = AF_INET; + break; + case '6': + family = AF_INET6; + break; + case 'm': + remaining--; + module_find = TRUE; + break; + default: + printf("Unknown character option %d (%c)", result, (char) result); + usage(poptcon); + } + } + if (udp && ((port_to_use == USE_CHARGEN) || + (port_to_use == USE_HTTP) || (port_to_use == USE_SMTP))) { + (void) fprintf(stderr, + "%s: I don't know how to use this port with UDP.\n", + progname); + exit(1); + } + if ((http || smtp) && (fill_requested)) { + (void) fprintf(stderr, + "%s: Filling incompatible with HTTP connections.\n", + progname); + exit(1); + } + if (n_stddev && number <= 2) { + (void) fprintf(stderr, + "%s: Average and standard deviation are meaningless since you perform only two or less iteration(s).\n", + progname); + exit(1); + } #if ! (defined(OPENSSL) || defined(GNUTLS)) - if (ssl) { - (void) fprintf(stderr, - "%s: not compiled with SSL/TLS support.\n", progname); - exit(1); - } + if (ssl) { + (void) fprintf(stderr, "%s: not compiled with SSL/TLS support.\n", progname); + exit(1); + } #endif #ifndef HTTP - if (http) { - (void) fprintf(stderr, "%s: Not compiled with HTTP support.\n", - progname); - exit(1); - } + if (http) { + (void) fprintf(stderr, "%s: Not compiled with HTTP support.\n", progname); + exit(1); + } #endif #ifndef SMTP - if (smtp) { - (void) fprintf(stderr, "%s: Not compiled with SMTP support.\n", - progname); - exit(1); - } + if (smtp) { + (void) fprintf(stderr, "%s: Not compiled with SMTP support.\n", progname); + exit(1); + } #endif #ifndef ICP - if (icp) { - (void) fprintf(stderr, "%s: Not compiled with ICP support.\n", - progname); - exit(1); - } -#endif - if ((http || smtp) && size_requested) { - (void) fprintf(stderr, - "%s: %s and message size specification are incompatible.\n", - progname, http ? "HTTP" : "SMTP"); - exit(1); - } - if (ssl && !http) { - (void) fprintf(stderr, - "%s: SSL is only supported for HTTP requests.\n", - progname); - exit(1); - } - if (!udp && !icp) - tcp = TRUE; - if (ssl && http) { - strcpy(port_name, DEFAULT_HTTPS_TCP_PORT); - } - if (!http && accept_http_redirects) { - (void) fprintf(stderr, - "%s: accept-http-redirects does not make sens if you do not use HTTP.\n", - progname); - exit(1); - } + if (icp) { + (void) fprintf(stderr, "%s: Not compiled with ICP support.\n", progname); + exit(1); + } +#endif + if ((http || smtp) && size_requested) { + (void) fprintf(stderr, + "%s: %s and message size specification are incompatible.\n", + progname, http ? "HTTP" : "SMTP"); + exit(1); + } + if (ssl && !http) { + (void) fprintf(stderr, + "%s: SSL is only supported for HTTP requests.\n", progname); + exit(1); + } + if (!udp && !icp) + tcp = TRUE; + if (ssl && http) { + strcpy(port_name, DEFAULT_HTTPS_TCP_PORT); + } + if (!http && accept_http_redirects) { + (void) fprintf(stderr, + "%s: accept-http-redirects does not make sens if you do not use HTTP.\n", + progname); + exit(1); + } #ifndef USE_TOS - if (tos_requested) { - (void) fprintf(stderr, - "%s: Not compiled with Type Of Service support.\n", - progname); - exit(1); - } + if (tos_requested) { + (void) fprintf(stderr, + "%s: Not compiled with Type Of Service support.\n", progname); + exit(1); + } #endif #ifndef USE_PRIORITY - if (priority_requested) { - (void) fprintf(stderr, - "%s: Not compiled with socket priority support.\n", - progname); - exit(1); - } + if (priority_requested) { + (void) fprintf(stderr, + "%s: Not compiled with socket priority support.\n", progname); + exit(1); + } #endif #ifndef HAVE_SCTP - if (sctp_requested) { - (void) fprintf(stderr, - "%s: Not compiled with SCTP support.\n", progname); - exit(1); - } -#endif - remaining--; /* No argv[0] this time */ - leftover = (char **) &argv[argc - remaining]; - if (plugin_name) { - 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) { - /* Retries with the absolute name */ - complete_plugin_name = (char *) malloc(MAX_LINE); - sprintf(complete_plugin_name, "%s/%s", PLUGINS_DIR, - plugin_name); - plugin = dlopen(complete_plugin_name, RTLD_NOW); - } - if (!plugin) { + if (sctp_requested) { + (void) fprintf(stderr, "%s: Not compiled with SCTP support.\n", progname); + exit(1); + } +#endif + remaining--; /* No argv[0] this time */ + leftover = (char **) &argv[argc - remaining]; + if (plugin_name) { + 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) { + /* Retries with the absolute name */ + complete_plugin_name = (char *) malloc(MAX_LINE); + sprintf(complete_plugin_name, "%s/%s", PLUGINS_DIR, plugin_name); + plugin = dlopen(complete_plugin_name, RTLD_NOW); + } + if (!plugin) { #if DEBIAN - /* A bit of help for the poor user */ - fprintf(stderr, - "You may have to load the recommended packages " - "to run this plugin.\n" - "'dpkg -s echoping | grep Recommends' to know them.\n\n"); -#endif - err_sys - ("Cannot load \"%s\" (I tried the short name, then the complete name in \"%s\"): %s", - plugin_name, PLUGINS_DIR, dlerror()); - } - plugin_init = dlsym(plugin, "init"); - if (!plugin_init) { - err_sys("Cannot find init in %s: %s", plugin_name, - dlerror()); - } - global_options.udp = udp; - global_options.verbose = verbose; - if (family == AF_INET) - global_options.only_ipv4 = 1; - else - global_options.only_ipv4 = 0; - if (family == AF_INET6) - global_options.only_ipv6 = 1; - else - global_options.only_ipv6 = 0; - plugin_port_name = - plugin_init(remaining, (const char **) leftover, global_options); - if (plugin_port_name != NULL) { - strcpy(port_name, plugin_port_name); - plugin_raw = FALSE; - plugin_start = dlsym(plugin, "start"); - if (!plugin_start) { - err_sys("Cannot find start in %s: %s", plugin_name, - dlerror()); - } - } else { - port_name = 0; - plugin_raw = TRUE; - plugin_raw_start = dlsym(plugin, "start_raw"); - if (!plugin_raw_start) { - err_sys("Cannot find start_raw in %s: %s", - plugin_name, dlerror()); - } - } - plugin_execute = dlsym(plugin, "execute"); - if (!plugin_execute) { - err_sys("Cannot find execute in %s: %s", plugin_name, - dlerror()); - } - plugin_terminate = dlsym(plugin, "terminate"); - if (!plugin_terminate) { - err_sys("Cannot find terminate in %s: %s", plugin_name, - dlerror()); - } - } - if (remaining == 0) { - (void) fprintf(stderr, "No host name indicated\n"); - usage(poptcon); - } - if (!module_find && remaining != 1) { - printf("%d args remaining, should be 1\n", remaining); - usage(poptcon); - } - if (verbose) { - printf("\nThis is %s, version %s.\n\n", progname, VERSION); - } - server = leftover[0]; + /* A bit of help for the poor user */ + fprintf(stderr, + "You may have to load the recommended packages " + "to run this plugin.\n" + "'dpkg -s echoping | grep Recommends' to know them.\n\n"); +#endif + err_sys + ("Cannot load \"%s\" (I tried the short name, then the complete name in \"%s\"): %s", + plugin_name, PLUGINS_DIR, dlerror()); + } + plugin_init = dlsym(plugin, "init"); + if (!plugin_init) { + err_sys("Cannot find init in %s: %s", plugin_name, dlerror()); + } + global_options.udp = udp; + global_options.verbose = verbose; + if (family == AF_INET) + global_options.only_ipv4 = 1; + else + global_options.only_ipv4 = 0; + if (family == AF_INET6) + global_options.only_ipv6 = 1; + else + global_options.only_ipv6 = 0; + plugin_port_name = + plugin_init(remaining, (const char **) leftover, global_options); + if (plugin_port_name != NULL) { + strcpy(port_name, plugin_port_name); + plugin_raw = FALSE; + plugin_start = dlsym(plugin, "start"); + if (!plugin_start) { + err_sys("Cannot find start in %s: %s", plugin_name, dlerror()); + } + } else { + port_name = 0; + plugin_raw = TRUE; + plugin_raw_start = dlsym(plugin, "start_raw"); + if (!plugin_raw_start) { + err_sys("Cannot find start_raw in %s: %s", plugin_name, dlerror()); + } + } + plugin_execute = dlsym(plugin, "execute"); + if (!plugin_execute) { + err_sys("Cannot find execute in %s: %s", plugin_name, dlerror()); + } + plugin_terminate = dlsym(plugin, "terminate"); + if (!plugin_terminate) { + err_sys("Cannot find terminate in %s: %s", plugin_name, dlerror()); + } + } + if (remaining == 0) { + (void) fprintf(stderr, "No host name indicated\n"); + usage(poptcon); + } + if (!module_find && remaining != 1) { + printf("%d args remaining, should be 1\n", remaining); + usage(poptcon); + } + if (verbose) { + printf("\nThis is %s, version %s.\n\n", progname, VERSION); + } + server = leftover[0]; #ifdef LIBIDN - locale_server = server; - utf8_server = stringprep_locale_to_utf8(server); - if (utf8_server) - server = utf8_server; - else - err_quit("Cannot convert %s to UTF-8 encoding: wrong locale (%s)?", - server, stringprep_locale_charset()); -#endif - if (!http && !icp) { - for (p = server; *p && (*p != ':'); p++) { - } - if (*p && (*p == ':')) { - (void) fprintf(stderr, - "%s: The syntax hostname:port is only for HTTP or ICP\n", - progname); - exit(1); - } - } - signal(SIGINT, interrupted); - memset(&hints, 0, sizeof(hints)); - hints.ai_family = family; - hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM; + locale_server = server; + utf8_server = stringprep_locale_to_utf8(server); + if (utf8_server) + server = utf8_server; + else + err_quit("Cannot convert %s to UTF-8 encoding: wrong locale (%s)?", + server, stringprep_locale_charset()); +#endif + if (!http && !icp) { + for (p = server; *p && (*p != ':'); p++) { + } + if (*p && (*p == ':')) { + (void) fprintf(stderr, + "%s: The syntax hostname:port is only for HTTP or ICP\n", + progname); + exit(1); + } + } + signal(SIGINT, interrupted); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = family; + hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM; #ifdef HTTP - if (http || icp) { - char *text_port = NULL; + if (http || icp) { + char *text_port = NULL; - if (*server == '[') { /* RFC 3986, section 3.2.2 */ - server++; - for (p = server; *p && *p != ']'; p++) { - } - p++; - if (*p == ':') { - p--; - *p = 0; - text_port = p + 2; - strncpy(port_name, text_port, NI_MAXSERV); - } else { - p--; - if (*p == ']') { - /* No port number */ - *p = 0; - } else { - (void) fprintf(stderr, - "%s: Invalid syntax for IPv6 address.\n", - progname); - exit(1); - } - } - } else { - for (p = server; *p; p++) { - if (*p == ':') { - *p = 0; - text_port = p + 1; - if (strcmp(text_port, "")) /* See bug * - * #850672 */ - strncpy(port_name, text_port, - NI_MAXSERV); - } - } - } - if (text_port == NULL || !strcmp(text_port, "")) { - error = getaddrinfo(server, port_name, &hints, &res); - if (error) { - if (error == EAI_SERVICE) { - if (strcmp(port_name, DEFAULT_HTTP_TCP_PORT) - == 0) { - strcpy(port_name, "80"); - } else - if (strcmp - (port_name, - DEFAULT_HTTPS_TCP_PORT) == 0) { - strcpy(port_name, "443"); - } else - if (strcmp - (port_name, - DEFAULT_ICP_UDP_PORT) == 0) { - strcpy(port_name, "3130"); - } - } - } - } - } + if (*server == '[') { /* RFC 3986, section 3.2.2 */ + server++; + for (p = server; *p && *p != ']'; p++) { + } + p++; + if (*p == ':') { + p--; + *p = 0; + text_port = p + 2; + strncpy(port_name, text_port, NI_MAXSERV); + } else { + p--; + if (*p == ']') { + /* No port number */ + *p = 0; + } else { + (void) fprintf(stderr, + "%s: Invalid syntax for IPv6 address.\n", + progname); + exit(1); + } + } + } else { + for (p = server; *p; p++) { + if (*p == ':') { + *p = 0; + text_port = p + 1; + if (strcmp(text_port, "")) /* See bug * * #850672 */ + strncpy(port_name, text_port, NI_MAXSERV); + } + } + } + if (text_port == NULL || !strcmp(text_port, "")) { + error = getaddrinfo(server, port_name, &hints, &res); + if (error) { + if (error == EAI_SERVICE) { + if (strcmp(port_name, DEFAULT_HTTP_TCP_PORT) + == 0) { + strcpy(port_name, "80"); + } else if (strcmp(port_name, DEFAULT_HTTPS_TCP_PORT) == 0) { + strcpy(port_name, "443"); + } else if (strcmp(port_name, DEFAULT_ICP_UDP_PORT) == 0) { + strcpy(port_name, "3130"); + } + } + } + } + } #endif #ifdef LIBIDN - /* - * Check if it is an address or a name (libidn will have trouble with - * IPv6 addresses otherwise) - */ - memset(&hints_numeric, 0, sizeof(hints_numeric)); - hints_numeric.ai_family = family; - hints_numeric.ai_flags = AI_NUMERICHOST; - error = getaddrinfo(server, port_name, &hints_numeric, &res); - if (error && error == EAI_NONAME) { /* A name, not an address */ - if ((result = - idna_to_ascii_8z(utf8_server, &ace_server, - IDNA_USE_STD3_ASCII_RULES)) != IDNA_SUCCESS) { - if (result == IDNA_CONTAINS_LDH) - err_quit("Illegal name for host: %s", server); /* foo@bar - * or - * similar - * errors */ - else - err_quit("IDN error for host: %s %d", server, - result); - } - if (strcmp(utf8_server, ace_server)) { - if (verbose) - printf("ACE name of the server: %s\n", ace_server); - server = ace_server; - } - } /* Else it is an address, do not IDNize it */ -#endif - if (!plugin || !plugin_raw) { - error = getaddrinfo(server, port_name, &hints, &res); - if (error) { - err_quit("getaddrinfo error for host: %s %s", - server, gai_strerror(error)); - } - if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), - pbuf, sizeof(pbuf), niflags) != 0) { - strcpy(hbuf, "?"); - strcpy(pbuf, "?"); - } - } - if (plugin) { - if (verbose) { - printf("Running start() for the plugin %s...\n", - plugin_name); - } - if (plugin_raw) - plugin_raw_start(); - else - plugin_start(res); - } + /* + * Check if it is an address or a name (libidn will have trouble with + * IPv6 addresses otherwise) + */ + memset(&hints_numeric, 0, sizeof(hints_numeric)); + hints_numeric.ai_family = family; + hints_numeric.ai_flags = AI_NUMERICHOST; + error = getaddrinfo(server, port_name, &hints_numeric, &res); + if (error && error == EAI_NONAME) { /* A name, not an address */ + if ((result = + idna_to_ascii_8z(utf8_server, &ace_server, + IDNA_USE_STD3_ASCII_RULES)) != IDNA_SUCCESS) { + if (result == IDNA_CONTAINS_LDH) + err_quit("Illegal name for host: %s", server); /* foo@bar or + * similar errors */ + else + err_quit("IDN error for host: %s %d", server, result); + } + if (strcmp(utf8_server, ace_server)) { + if (verbose) + printf("ACE name of the server: %s\n", ace_server); + server = ace_server; + } + } /* Else it is an address, do not IDNize it */ +#endif + if (!plugin || !plugin_raw) { + error = getaddrinfo(server, port_name, &hints, &res); + if (error) { + err_quit("getaddrinfo error for host: %s %s", + server, gai_strerror(error)); + } + if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), + pbuf, sizeof(pbuf), niflags) != 0) { + strcpy(hbuf, "?"); + strcpy(pbuf, "?"); + } + } + if (plugin) { + if (verbose) { + printf("Running start() for the plugin %s...\n", plugin_name); + } + if (plugin_raw) + plugin_raw_start(); + else + plugin_start(res); + } #ifdef HTTP - if (http) { - if (http_hostname != NULL) - server = http_hostname; - sendline = make_http_sendline(url, server, atoi(pbuf), nocache); - } else + if (http) { + if (http_hostname != NULL) + server = http_hostname; + sendline = make_http_sendline(url, server, atoi(pbuf), nocache); + } else #endif #ifdef SMTP - if (smtp) { - sendline = "QUIT\r\n"; /* Surprises some SMTP servers which log a - * frightening NOQUEUE. Anyone knows better? - * * * * * See bug #1512776 */ - } else + if (smtp) { + sendline = "QUIT\r\n"; /* Surprises some SMTP servers which log a + * frightening NOQUEUE. Anyone knows better? * * * + * * See bug #1512776 */ + } else #endif #ifdef ICP - if (icp) { - if (res->ai_family == AF_INET) { - sendline = - make_icp_sendline(url, &(res->ai_addr), opcode, &length); - } else { + if (icp) { + if (res->ai_family == AF_INET) { + sendline = make_icp_sendline(url, &(res->ai_addr), opcode, &length); + } else { - /* - * TODO: we should be able to create a ICP hostid for - * IPv6 addresses... See the Squid IPv6 patch at - * http://devel.squid-cache.org/projects.html#ipv6, - * for instance the following code. - */ - sendline = - make_icp_sendline(url, (void *) NULL, opcode, &length); - /* - * - headerp->shostid = theOutICPAddr.s_addr; + ** FIXME ** we - * should get more unique data from IPv6 address +xmemcpy - * (&headerp->shostid, &theOutICPAddr, sizeof - * (headerp->shostid)); */ - } - } else -#endif - if (!fill_requested) { - sendline = random_string(size); - } else { - sendline = (char *) malloc(size + 1); - for (i = 0; i < size; i++) - sendline[i] = fill; - sendline[size] = 0; - } - n = strlen(sendline); + /* + * TODO: we should be able to create a ICP hostid for + * IPv6 addresses... See the Squid IPv6 patch at + * http://devel.squid-cache.org/projects.html#ipv6, + * for instance the following code. + */ + sendline = make_icp_sendline(url, (void *) NULL, opcode, &length); + /* + * - headerp->shostid = theOutICPAddr.s_addr; + ** FIXME ** we + * should get more unique data from IPv6 address +xmemcpy + * (&headerp->shostid, &theOutICPAddr, sizeof + * (headerp->shostid)); */ + } + } else +#endif + if (!fill_requested) { + sendline = random_string(size); + } else { + sendline = (char *) malloc(size + 1); + for (i = 0; i < size; i++) + sendline[i] = fill; + sendline[size] = 0; + } + n = strlen(sendline); #ifdef OPENSSL - if (ssl) { - SSL_load_error_strings(); - SSLeay_add_ssl_algorithms(); - /* - * The following RAND_ calls are only for systems insecure - * enough to fail to have /dev/urandom. Bug #132001 - */ - RAND_file_name(rand_file, sizeof(rand_file)); - RAND_write_file(rand_file); - RAND_load_file(rand_file, 1024); - meth = SSLv23_client_method(); - if ((ctx = SSL_CTX_new(meth)) == NULL) - err_sys("Cannot create a new SSL context"); - /* - * Bug reported by Sebastian Siewior - * . It seems that OpenSSL - * crashes on non blocking sockets when trying to close them - * (OpenSSL will try write on a closed socket). - */ + if (ssl) { + SSL_load_error_strings(); + SSLeay_add_ssl_algorithms(); + /* + * The following RAND_ calls are only for systems insecure + * enough to fail to have /dev/urandom. Bug #132001 + */ + RAND_file_name(rand_file, sizeof(rand_file)); + RAND_write_file(rand_file); + RAND_load_file(rand_file, 1024); + meth = SSLv23_client_method(); + if ((ctx = SSL_CTX_new(meth)) == NULL) + err_sys("Cannot create a new SSL context"); + /* + * Bug reported by Sebastian Siewior + * . It seems that OpenSSL + * crashes on non blocking sockets when trying to close them + * (OpenSSL will try write on a closed socket). + */ #ifdef USE_SIGACTION - mysigaction.sa_handler = SIG_IGN; - sigemptyset(&mysigaction.sa_mask); - if ((sigaction(SIGPIPE, &mysigaction, NULL)) < 0); /* Ignore it - */ + mysigaction.sa_handler = SIG_IGN; + sigemptyset(&mysigaction.sa_mask); + if ((sigaction(SIGPIPE, &mysigaction, NULL)) < 0); /* Ignore it */ #else - signal(SIGPIPE, SIG_IGN); + signal(SIGPIPE, SIG_IGN); #endif - } + } #endif #ifdef GNUTLS - if (ssl) { - gnutls_global_init(); - gnutls_certificate_allocate_credentials(&xcred); - /* - * gnutls_certificate_set_x509_trust_file(xcred, CAFILE, - * GNUTLS_X509_FMT_PEM); - */ - } + if (ssl) { + gnutls_global_init(); + gnutls_certificate_allocate_credentials(&xcred); + /* + * gnutls_certificate_set_x509_trust_file(xcred, CAFILE, + * GNUTLS_X509_FMT_PEM); + */ + } #endif - for (i = 1; i <= number; i++) { - timeout_flag = 0; - if (timeout_requested) - alarm(timeout); - if (i > 1) { + for (i = 1; i <= number; i++) { + timeout_flag = 0; + if (timeout_requested) + alarm(timeout); + if (i > 1) { #ifdef HAVE_USLEEP - /* - * SUSv3 states that the argument to usleep() shall - * be less * than 1000000, so split into two calls if - * necessary. Bug #1473872, fix by Jeff Rizzo - - * riz@sourceforge - */ - if (wait >= 1) { - sleep((unsigned int) wait); - } - usleep((wait - (unsigned int) wait) * 1000000); + /* + * SUSv3 states that the argument to usleep() shall + * be less * than 1000000, so split into two calls if + * necessary. Bug #1473872, fix by Jeff Rizzo - + * riz@sourceforge + */ + if (wait >= 1) { + sleep((unsigned int) wait); + } + usleep((wait - (unsigned int) wait) * 1000000); #else - sleep(wait); + sleep(wait); #endif - } - attempts++; + } + attempts++; #ifdef OPENSSL - if (ssl) - /* - * Despite what the OpenSSL documentation says, we - * must allocate a new SSL structure at each - * iteration, otherwise, some* SSL servers fail at - * the second iteration with: error:1406D0D9:SSL - * routines:GET_SERVER_HELLO:reuse cert type not zero - * Bug #130151 - */ - if ((sslh = SSL_new(ctx)) == NULL) - err_sys("Cannot initialize SSL context"); -#endif - /* - * Open a socket. - */ - protocol = res->ai_protocol; + if (ssl) + /* + * Despite what the OpenSSL documentation says, we + * must allocate a new SSL structure at each + * iteration, otherwise, some* SSL servers fail at + * the second iteration with: error:1406D0D9:SSL + * routines:GET_SERVER_HELLO:reuse cert type not zero + * Bug #130151 + */ + if ((sslh = SSL_new(ctx)) == NULL) + err_sys("Cannot initialize SSL context"); +#endif + /* + * Open a socket. + */ + protocol = res->ai_protocol; #ifdef HAVE_SCTP - if (sctp) - protocol = IPPROTO_SCTP; -#endif - if (!plugin) { - if ((sockfd = - socket(res->ai_family, res->ai_socktype, protocol)) < 0) - err_sys("Can't open socket"); - if (udp) { - struct addrinfo hints2, *res2; + if (sctp) + protocol = IPPROTO_SCTP; +#endif + if (!plugin) { + if ((sockfd = socket(res->ai_family, res->ai_socktype, protocol)) < 0) + err_sys("Can't open socket"); + if (udp) { + struct addrinfo hints2, *res2; - memset(&hints2, 0, sizeof(hints2)); - hints2.ai_family = res->ai_family; - hints2.ai_flags = AI_PASSIVE; - hints2.ai_socktype = SOCK_DGRAM; - error = getaddrinfo(NULL, "0", &hints2, &res2); - if (error) { - err_sys("getaddrinfo error"); - } - if (bind(sockfd, res2->ai_addr, res2->ai_addrlen) < - 0) { - err_sys("bind error"); - } - } + memset(&hints2, 0, sizeof(hints2)); + hints2.ai_family = res->ai_family; + hints2.ai_flags = AI_PASSIVE; + hints2.ai_socktype = SOCK_DGRAM; + error = getaddrinfo(NULL, "0", &hints2, &res2); + if (error) { + err_sys("getaddrinfo error"); + } + if (bind(sockfd, res2->ai_addr, res2->ai_addrlen) < 0) { + err_sys("bind error"); + } + } #ifdef USE_PRIORITY - if (priority_requested) { - if (verbose) { - printf - ("Setting socket priority to %d (0x%02x)\n", - priority, (unsigned int) priority); - } - if (setsockopt(sockfd, - SOL_SOCKET, - SO_PRIORITY, - (void *) &priority, - (socklen_t) sizeof(priority))) { - err_sys("Failed setting socket priority"); - } - } + if (priority_requested) { + if (verbose) { + printf + ("Setting socket priority to %d (0x%02x)\n", + priority, (unsigned int) priority); + } + if (setsockopt(sockfd, + SOL_SOCKET, + SO_PRIORITY, + (void *) &priority, (socklen_t) sizeof(priority))) { + err_sys("Failed setting socket priority"); + } + } #endif #if USE_TOS - if (tos_requested) { - if (verbose) { - printf - ("Setting IP type of service octet to %d (0x%02x)\n", - tos, (unsigned int) tos); - } - if (setsockopt(sockfd, - SOL_IP, - IP_TOS, (void *) &tos, - (socklen_t) sizeof(tos))) { - err_sys - ("Failed setting IP type of service octet"); - } - } -#endif - } - if (verbose) { - if (!plugin) { - if (tcp) { - printf - ("Trying to connect to internet address %s %s to transmit %u bytes...\n", - hbuf, pbuf, n); - } + if (tos_requested) { + if (verbose) { + printf + ("Setting IP type of service octet to %d (0x%02x)\n", + tos, (unsigned int) tos); + } + if (setsockopt(sockfd, + SOL_IP, + IP_TOS, (void *) &tos, (socklen_t) sizeof(tos))) { + err_sys("Failed setting IP type of service octet"); + } + } +#endif + } + if (verbose) { + if (!plugin) { + if (tcp) { + printf + ("Trying to connect to internet address %s %s to transmit %u bytes...\n", + hbuf, pbuf, n); + } #ifdef ICP - if (icp) { - printf - ("Trying to send an ICP packet of %u bytes to the internet address %s...\n", - length, hbuf); - } -#endif - else { - printf - ("Trying to send %u bytes to internet address %s...\n", - size, hbuf); - } - } else { - if (plugin_raw) - printf("Trying to call plugin %s...\n", - plugin_name); - else - printf - ("Trying to call plugin %s for internet address %s %s...\n", - plugin_name, hbuf, pbuf); - } - } + if (icp) { + printf + ("Trying to send an ICP packet of %u bytes to the internet address %s...\n", + length, hbuf); + } +#endif + else { + printf + ("Trying to send %u bytes to internet address %s...\n", + size, hbuf); + } + } else { + if (plugin_raw) + printf("Trying to call plugin %s...\n", plugin_name); + else + printf + ("Trying to call plugin %s for internet address %s %s...\n", + plugin_name, hbuf, pbuf); + } + } #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != 0) { - err_sys("I cannot flush"); - } -#endif - if ((tcp || plugin) && timeout_requested) { /* echoping's - * timeout has a - * different - * semantic in TCP - * and UDP */ + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } +#endif + if ((tcp || plugin) && timeout_requested) { /* echoping's timeout has a + * different semantic in TCP + * and UDP */ #ifdef USE_SIGACTION - mysigaction.sa_handler = to_alarm; - sigemptyset(&mysigaction.sa_mask); - /* Default behavior doesn't seem portable? */ + mysigaction.sa_handler = to_alarm; + sigemptyset(&mysigaction.sa_mask); + /* Default behavior doesn't seem portable? */ #ifdef SA_INTERRUPT - mysigaction.sa_flags = SA_INTERRUPT; + mysigaction.sa_flags = SA_INTERRUPT; #else - mysigaction.sa_flags = (int) 0; + mysigaction.sa_flags = (int) 0; #endif - if ((sigaction(SIGALRM, &mysigaction, NULL)) < 0) - err_sys("Cannot set signal handler"); + if ((sigaction(SIGALRM, &mysigaction, NULL)) < 0) + err_sys("Cannot set signal handler"); #else - signal(SIGALRM, to_alarm); -#endif - timeout_flag = 0; /* for signal handler */ - alarm(timeout); - } - (void) gettimeofday(&oldtv, (struct timezone *) NULL); - /* work out the time it took... */ - if (measure_data_transfer_only) { - measured = recvtv; - tvsub(&measured, &sendtv); - } else { - measured = newtv; - tvsub(&measured, &oldtv); - } - if (plugin) { - plugin_result = plugin_execute(); - /* If plugin_result == -1, there is a temporary error and we - * did not get data, we must not use it in the average / - * median calculations. So, successes will not be - * incremented later. */ - if (plugin_result == -2) - err_quit(""); - } else { - if (!icp) { - /* - * Connect to the server. - */ - (void) gettimeofday(&conntv, - (struct timezone *) NULL); - if (connect(sockfd, res->ai_addr, res->ai_addrlen) < - 0) { - if ((errno == EINTR) && (timeout_flag)) { - printf("Timeout while connecting\n"); - close(sockfd); - continue; + signal(SIGALRM, to_alarm); +#endif + timeout_flag = 0; /* for signal handler */ + alarm(timeout); + } + (void) gettimeofday(&oldtv, (struct timezone *) NULL); + /* work out the time it took... */ + if (measure_data_transfer_only) { + measured = recvtv; + tvsub(&measured, &sendtv); + } else { + measured = newtv; + tvsub(&measured, &oldtv); + } + if (plugin) { + plugin_result = plugin_execute(); + /* If plugin_result == -1, there is a temporary error and we did not + * get data, we must not use it in the average / median calculations. + * So, successes will not be incremented later. */ + if (plugin_result == -2) + err_quit(""); + } else { + if (!icp) { + /* + * Connect to the server. + */ + (void) gettimeofday(&conntv, (struct timezone *) NULL); + if (connect(sockfd, res->ai_addr, res->ai_addrlen) < 0) { + if ((errno == EINTR) && (timeout_flag)) { + printf("Timeout while connecting\n"); + close(sockfd); + continue; #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != 0) { - err_sys("I cannot flush"); - } -#endif - } else - err_sys("Can't connect to server"); - /* TODO: it would be better to continue: if - * -n was given, other iterations may - * succeed. A flag indicating success or - * error is probably necessary, it would - * replace the mess around 'if - * (!timeout_flag && (!plugin || - * plugin_result >= 0))' */ - } else { - if (tcp) { - (void) gettimeofday(&connectedtv, - (struct timezone - *) NULL); - temp = connectedtv; - tvsub(&temp, &conntv); - if (verbose) { - printf("Connected...\n"); - printf - ("TCP Latency: %d.%06d seconds\n", - (int) temp.tv_sec, - (int) temp.tv_usec); - } - } - } - if (verbose && tcp) { + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } +#endif + } else + err_sys("Can't connect to server"); + /* TODO: it would be better to continue: if -n was given, other + * iterations may succeed. A flag indicating success or error is + * probably necessary, it would replace the mess around 'if + * (!timeout_flag && (!plugin || plugin_result >= 0))' */ + } else { + if (tcp) { + (void) gettimeofday(&connectedtv, (struct timezone *) NULL); + temp = connectedtv; + tvsub(&temp, &conntv); + if (verbose) { + printf("Connected...\n"); + printf + ("TCP Latency: %d.%06d seconds\n", + (int) temp.tv_sec, (int) temp.tv_usec); + } + } + } + if (verbose && tcp) { #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != 0) { - err_sys("I cannot flush"); - } -#endif - } - if (!udp && !ssl) - if ((files = fdopen(sockfd, "r")) == NULL) - err_sys("Cannot fdopen"); + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } +#endif + } + if (!udp && !ssl) + if ((files = fdopen(sockfd, "r")) == NULL) + err_sys("Cannot fdopen"); #ifdef OPENSSL - if (ssl) { - SSL_set_fd(sslh, sockfd); - if (SSL_connect(sslh) == -1) - if ((errno == EINTR) - && (timeout_flag)) { - printf - ("Timeout while starting SSL\n"); - close(sockfd); - continue; - } - if (verbose) - printf("SSL connection using %s\n", - SSL_get_cipher(sslh)); - /* - * We could check the server's - * certificate or other funny things - */ - } + if (ssl) { + SSL_set_fd(sslh, sockfd); + if (SSL_connect(sslh) == -1) + if ((errno == EINTR) + && (timeout_flag)) { + printf("Timeout while starting SSL\n"); + close(sockfd); + continue; + } + if (verbose) + printf("SSL connection using %s\n", SSL_get_cipher(sslh)); + /* + * We could check the server's + * certificate or other funny things + */ + } #endif #ifdef GNUTLS - if (ssl) { - tls_result = - gnutls_init(&session, GNUTLS_CLIENT); - if (tls_result != 0) - err_sys - ("Cannot create a new TLS session"); - gnutls_set_default_priority(session); - gnutls_certificate_type_set_priority(session, - cert_type_priority); - gnutls_credentials_set(session, - GNUTLS_CRD_CERTIFICATE, - xcred); - gnutls_transport_set_ptr(session, - (gnutls_transport_ptr) - (long) sockfd); - tls_result = gnutls_handshake(session); - if (tls_result < 0) { - if ((errno == EINTR) - && (timeout_flag)) { - printf - ("Timeout while starting TLS\n"); - close(sockfd); - continue; - } else { - err_sys - ("Cannot start the TLS session: %s", - gnutls_strerror - (tls_result)); - } - } - if (verbose) - printf - ("TLS connection using \"%s\"\n", - gnutls_cipher_get_name - (gnutls_cipher_get(session))); - /* - * We could check the server's - * certificate or other funny things. - * See - * http://www.gnu.org/software/gnutls/ - * documentation/gnutls/gnutls.html#SE - * CTION00622000000000000000 - */ - } -#endif - } else { - /* No initial connection */ - } - if ((port_to_use == USE_ECHO) || (port_to_use == USE_DISCARD) - || (port_to_use == USE_HTTP) || (port_to_use == USE_ICP) - || (port_to_use == USE_SMTP)) { - if (!udp) { - if (!ssl) { - /* - * Write something to the - * server - */ + if (ssl) { + tls_result = gnutls_init(&session, GNUTLS_CLIENT); + if (tls_result != 0) + err_sys("Cannot create a new TLS session"); + gnutls_set_default_priority(session); + gnutls_certificate_type_set_priority(session, + cert_type_priority); + gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred); + gnutls_transport_set_ptr(session, (gnutls_transport_ptr) + (long) sockfd); + tls_result = gnutls_handshake(session); + if (tls_result < 0) { + if ((errno == EINTR) + && (timeout_flag)) { + printf("Timeout while starting TLS\n"); + close(sockfd); + continue; + } else { + err_sys + ("Cannot start the TLS session: %s", + gnutls_strerror(tls_result)); + } + } + if (verbose) + printf + ("TLS connection using \"%s\"\n", + gnutls_cipher_get_name(gnutls_cipher_get(session))); + /* + * We could check the server's + * certificate or other funny things. + * See + * http://www.gnu.org/software/gnutls/ + * documentation/gnutls/gnutls.html#SE + * CTION00622000000000000000 + */ + } +#endif + } else { + /* No initial connection */ + } + if ((port_to_use == USE_ECHO) || (port_to_use == USE_DISCARD) + || (port_to_use == USE_HTTP) || (port_to_use == USE_ICP) + || (port_to_use == USE_SMTP)) { + if (!udp) { + if (!ssl) { + /* + * Write something to the + * server + */ #ifdef SMTP - if (port_to_use == USE_SMTP) { - /* Get the greeting line */ - nr = smtp_read_response_from_server(files); - } -#endif - if (writen(sockfd, sendline, n) != n) { - if ((nr < 0 || nr != n) - && timeout_flag) { - printf - ("Timeout while writing (%d byte(s) written so far)\n", - (nr == - -1) ? 0 : nr); - nr = n; - close(sockfd); - continue; - } else - err_sys - ("writen error on TCP socket %d", - sockfd); - } - } + if (port_to_use == USE_SMTP) { + /* Get the greeting line */ + nr = smtp_read_response_from_server(files); + } +#endif + if (writen(sockfd, sendline, n) != n) { + if ((nr < 0 || nr != n) + && timeout_flag) { + printf + ("Timeout while writing (%d byte(s) written so far)\n", + (nr == -1) ? 0 : nr); + nr = n; + close(sockfd); + continue; + } else + err_sys("writen error on TCP socket %d", sockfd); + } + } #ifdef OPENSSL - else { - if ((rc = - SSL_write(sslh, sendline, - n)) != n) { - if ((nr < 0 || nr != n) - && timeout_flag) { - nr = n; - printf - ("Timeout while writing\n"); - close(sockfd); - continue; - } else { - sslcode = - ERR_get_error(); - err_sys - ("SSL_write error on socket: %s", - ERR_error_string - (sslcode, - NULL)); - } - } - } + else { + if ((rc = SSL_write(sslh, sendline, n)) != n) { + if ((nr < 0 || nr != n) + && timeout_flag) { + nr = n; + printf("Timeout while writing\n"); + close(sockfd); + continue; + } else { + sslcode = ERR_get_error(); + err_sys + ("SSL_write error on socket: %s", + ERR_error_string(sslcode, NULL)); + } + } + } #endif #ifdef GNUTLS - else - { - if ((rc = - gnutls_record_send(session, - sendline, - strlen - (sendline))) - != n) { - if ((nr < 0 || nr != n) - && timeout_flag) { - nr = n; - printf - ("Timeout while writing\n"); - close(sockfd); - continue; - } else { - err_sys - ("gnutls_record_send error %d on socket: %s", - rc, - gnutls_strerror - (rc)); - } - } - } -#endif - } else { + else + { + if ((rc = + gnutls_record_send(session, sendline, strlen(sendline))) + != n) { + if ((nr < 0 || nr != n) + && timeout_flag) { + nr = n; + printf("Timeout while writing\n"); + close(sockfd); + continue; + } else { + err_sys + ("gnutls_record_send error %d on socket: %s", + rc, gnutls_strerror(rc)); + } + } + } +#endif + } else { #ifdef ICP - if (icp) { - if (sendto - (sockfd, sendline, length, 0, - res->ai_addr, - res->ai_addrlen) != length) - err_sys - ("sendto error on socket"); - } else -#endif - /* - * if (sendto(sockfd, sendline, n, 0, - * &serv_addr, sizeof(serv_addr)) != n) - * err_sys("sendto error on socket"); - */ - if (send(sockfd, sendline, n, 0) != n) - err_sys("send error on socket"); - } - if (verbose) { - (void) gettimeofday(&sendtv, - (struct timezone *) - NULL); + if (icp) { + if (sendto + (sockfd, sendline, length, 0, + res->ai_addr, res->ai_addrlen) != length) + err_sys("sendto error on socket"); + } else +#endif + /* + * if (sendto(sockfd, sendline, n, 0, + * &serv_addr, sizeof(serv_addr)) != n) + * err_sys("sendto error on socket"); + */ + if (send(sockfd, sendline, n, 0) != n) + err_sys("send error on socket"); + } + if (verbose) { + (void) gettimeofday(&sendtv, (struct timezone *) + NULL); #ifdef ICP - if (icp) - printf("Sent (%d bytes)...\n", - length); - else + if (icp) + printf("Sent (%d bytes)...\n", length); + else #endif - printf("Sent (%d bytes)...\n", n); + printf("Sent (%d bytes)...\n", n); #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != 0) { - err_sys("I cannot flush"); - } -#endif - } - } - if (tcp && !discard) { - fd_set mask; - int n = 0; + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } +#endif + } + } + if (tcp && !discard) { + fd_set mask; + int n = 0; - FD_ZERO(&mask); + FD_ZERO(&mask); - if (!(http && ssl)) - n = fileno(files); + if (!(http && ssl)) + n = fileno(files); #ifdef OPENSSL - else { - n = SSL_get_fd(sslh); - } + else { + n = SSL_get_fd(sslh); + } #endif #ifdef GNUTLS - else - { - n = sockfd; - } -#endif - FD_SET(n, &mask); - if (select(n + 1, &mask, 0, 0, NULL) > 0) { - (void) gettimeofday(&recvtv, - (struct timezone *) - NULL); - temp = recvtv; - tvsub(&temp, &sendtv); - if (verbose) - printf - ("Application Latency: %d.%06d seconds\n", - (int) temp.tv_sec, - (int) temp.tv_usec); - } - } - if ((port_to_use == USE_ECHO) || (port_to_use == USE_CHARGEN) - || (port_to_use == USE_HTTP) || (port_to_use == USE_ICP) - || (port_to_use == USE_SMTP)) { - if (!udp) { - if (!http && !smtp && !discard) { - /* Read from the server */ - nr = readline(files, recvline, n, - stop_at_newlines); - } else if (discard) { - /* No reply, no read */ - } + else + { + n = sockfd; + } +#endif + FD_SET(n, &mask); + if (select(n + 1, &mask, 0, 0, NULL) > 0) { + (void) gettimeofday(&recvtv, (struct timezone *) + NULL); + temp = recvtv; + tvsub(&temp, &sendtv); + if (verbose) + printf + ("Application Latency: %d.%06d seconds\n", + (int) temp.tv_sec, (int) temp.tv_usec); + } + } + if ((port_to_use == USE_ECHO) || (port_to_use == USE_CHARGEN) + || (port_to_use == USE_HTTP) || (port_to_use == USE_ICP) + || (port_to_use == USE_SMTP)) { + if (!udp) { + if (!http && !smtp && !discard) { + /* Read from the server */ + nr = readline(files, recvline, n, stop_at_newlines); + } else if (discard) { + /* No reply, no read */ + } #ifdef HTTP - else if (http) { - if (!ssl) - channel.fs = files; + else if (http) { + if (!ssl) + channel.fs = files; #ifdef OPENSSL - else - channel.ssl = sslh; + else + channel.ssl = sslh; #endif #ifdef GNUTLS - else - channel.tls = session; + else + channel.tls = session; #endif - nr = read_from_server(channel, ssl, - accept_http_redirects); - } + nr = read_from_server(channel, ssl, accept_http_redirects); + } #endif #ifdef SMTP - else if (smtp) { - nr = smtp_read_response_from_server - (files); - } + else if (smtp) { + nr = smtp_read_response_from_server(files); + } #endif - } else { /* UDP */ + } else { /* UDP */ #ifdef USE_SIGACTION - mysigaction.sa_handler = to_alarm; - sigemptyset(&mysigaction.sa_mask); + mysigaction.sa_handler = to_alarm; + sigemptyset(&mysigaction.sa_mask); #ifdef SA_INTERRUPT - mysigaction.sa_flags = SA_INTERRUPT; + mysigaction.sa_flags = SA_INTERRUPT; #else - mysigaction.sa_flags = (int) 0; + mysigaction.sa_flags = (int) 0; #endif - if ((sigaction(SIGALRM, &mysigaction, NULL)) - < 0) - err_sys("Cannot set signal handler"); + if ((sigaction(SIGALRM, &mysigaction, NULL)) + < 0) + err_sys("Cannot set signal handler"); #else - signal(SIGALRM, to_alarm); + signal(SIGALRM, to_alarm); #endif - timeout_flag = 0; /* for signal - * handler */ + timeout_flag = 0; /* for signal handler */ #ifdef ICP - if (icp) { - nr = recv_icp(sockfd, recvline, - retcode); - if (verbose) { - printf("%s\n", retcode); - } - } else { -#endif - nr = recv(sockfd, recvline, n, 0); - /* - * nr = recvfrom(sockfd, recvline, n, 0, - * (struct sockaddr *) 0, (int *) 0); - * recvfrom fails on SunOS on connected - * sockets. - */ - /* - * Todo: in UDP, we should loop to read: we - * can have several reads necessary. - */ - if ((nr < 0) && (errno == EINTR) - && (timeout_flag)) { - nr = n; - printf("Timeout\n"); + if (icp) { + nr = recv_icp(sockfd, recvline, retcode); + if (verbose) { + printf("%s\n", retcode); + } + } else { +#endif + nr = recv(sockfd, recvline, n, 0); + /* + * nr = recvfrom(sockfd, recvline, n, 0, + * (struct sockaddr *) 0, (int *) 0); + * recvfrom fails on SunOS on connected + * sockets. + */ + /* + * Todo: in UDP, we should loop to read: we + * can have several reads necessary. + */ + if ((nr < 0) && (errno == EINTR) + && (timeout_flag)) { + nr = n; + printf("Timeout\n"); #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != - 0) { - err_sys - ("I cannot flush"); - } + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } #endif - } + } #ifdef ICP - } -#endif - } - if (!http && !icp && !smtp && !discard) { - if ((nr < 0 || nr != n) && timeout_flag) - /* - * if ((nr < 0 || nr != n) && - * (errno == EINTR) && - * timeout_flag) - */ - { - printf - ("Timeout while reading (%d byte(s) read)\n", - (nr == -1) ? 0 : nr); - nr = n; + } +#endif + } + if (!http && !icp && !smtp && !discard) { + if ((nr < 0 || nr != n) && timeout_flag) + /* + * if ((nr < 0 || nr != n) && + * (errno == EINTR) && + * timeout_flag) + */ + { + printf + ("Timeout while reading (%d byte(s) read)\n", + (nr == -1) ? 0 : nr); + nr = n; #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != 0) { - err_sys("I cannot flush"); - } -#endif - close(sockfd); - continue; - } - if (nr < 0 || nr != n) - err_sys - ("readline error: %d bytes read, %d bytes requested", - nr, n); - } else - /* This is probably HTTP */ - { - /* - * printf ("DEBUG: received %d bytes - * (flag is %d, errno is %d)\n", nr, - * timeout_flag, errno); - */ - if ((errno == EINTR) && timeout_flag) { - printf - ("Timeout while reading (%d byte(s) read so far)\n", - (nr == -1) ? 0 : nr); + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } +#endif + close(sockfd); + continue; + } + if (nr < 0 || nr != n) + err_sys + ("readline error: %d bytes read, %d bytes requested", + nr, n); + } else + /* This is probably HTTP */ + { + /* + * printf ("DEBUG: received %d bytes + * (flag is %d, errno is %d)\n", nr, + * timeout_flag, errno); + */ + if ((errno == EINTR) && timeout_flag) { + printf + ("Timeout while reading (%d byte(s) read so far)\n", + (nr == -1) ? 0 : nr); #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != 0) { - err_sys("I cannot flush"); - } -#endif - close(sockfd); - continue; - } - if (nr < 0) { - err_ret("Error reading HTTP reply"); - } - } - if (verbose) - printf("%d bytes read from server.\n", nr); - } - } /* That's all, folks */ - alarm(0); + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } +#endif + close(sockfd); + continue; + } + if (nr < 0) { + err_ret("Error reading HTTP reply"); + } + } + if (verbose) + printf("%d bytes read from server.\n", nr); + } + } /* That's all, folks */ + alarm(0); #ifdef HAVE_TCP_INFO - /* Thanks to Perry Lorier for the tip. See a - * longer paper in http://linuxgazette.net/136/pfeiffer.html */ - if (tcp && verbose) { - if (getsockopt - (sockfd, SOL_TCP, TCP_INFO, &tcpinfo, &socket_length) - != -1) { - /* TODO: find out the meaning of the various fields - * inthe struct tcp_info (it seems documented only - * in the Linux kernel sources) and display stuff - * like reordering (see RFC 4737), window, lost - * packets, etc. */ - printf - ("Estimated TCP RTT: %.04f seconds (std. deviation %0.03f)\n", - tcpinfo.tcpi_rtt / 1000000.0, - tcpinfo.tcpi_rttvar / 1000000.0); - } - } -#endif - if (http) { + /* Thanks to Perry Lorier for the tip. See a longer paper + * in http://linuxgazette.net/136/pfeiffer.html */ + if (tcp && verbose) { + if (getsockopt(sockfd, SOL_TCP, TCP_INFO, &tcpinfo, &socket_length) + != -1) { + /* TODO: find out the meaning of the various fields inthe struct + * tcp_info (it seems documented only in the Linux kernel sources) + * and display stuff like reordering (see RFC 4737), window, lost + * packets, etc. */ + printf + ("Estimated TCP RTT: %.04f seconds (std. deviation %0.03f)\n", + tcpinfo.tcpi_rtt / 1000000.0, tcpinfo.tcpi_rttvar / 1000000.0); + } + } +#endif + if (http) { #ifdef OPENSSL - if (ssl) - SSL_shutdown(channel.ssl); - else + if (ssl) + SSL_shutdown(channel.ssl); + else #endif #ifdef GNUTLS - if (ssl) - shutdown(sockfd, SHUT_RDWR); - else + if (ssl) + shutdown(sockfd, SHUT_RDWR); + else #endif - fclose(channel.fs); - } - close(sockfd); + fclose(channel.fs); + } + close(sockfd); - (void) gettimeofday(&newtv, (struct timezone *) NULL); - temp = newtv; - tvsub(&temp, &oldtv); - if (!timeout_flag && (!plugin || plugin_result >= 0)) { /* If it - * worked... - */ - tvadd(&total, &temp); + (void) gettimeofday(&newtv, (struct timezone *) NULL); + temp = newtv; + tvsub(&temp, &oldtv); + if (!timeout_flag && (!plugin || plugin_result >= 0)) { /* If it worked... */ + tvadd(&total, &temp); - /* Check */ - if (!plugin) { - if (port_to_use == USE_ECHO) { - if (strcmp(sendline, recvline) != 0) { - printf(" I wrote:\n%s\n", sendline); - printf(" and I got back:\n%s\n", - recvline); - err_quit("Strange server"); - } - if (verbose) { - printf("Checked\n"); + /* Check */ + if (!plugin) { + if (port_to_use == USE_ECHO) { + if (strcmp(sendline, recvline) != 0) { + printf(" I wrote:\n%s\n", sendline); + printf(" and I got back:\n%s\n", recvline); + err_quit("Strange server"); + } + if (verbose) { + printf("Checked\n"); #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != 0) { - err_sys("I cannot flush"); - } -#endif - } - } - if (port_to_use == USE_CHARGEN) { - sendline = CHARGENERATED; - recvline[strlen(sendline)] = 0; - if (strcmp(sendline, recvline) != 0) { - /* - * TODO: it does not work if - * the size is lower than the - * length of CHARGENERATED - */ - printf(" I got back:\n%s\n", - recvline); - printf - (" instead of the most common:\n%s\n", - sendline); - err_ret("Strange server"); - } - if (verbose) { - printf("Checked\n"); - } - } - } - tvsub(&newtv, &oldtv); - tvmin(&min, &newtv); - tvmax(&max, &newtv); - printf("Elapsed time: %d.%06d seconds\n", - (int) newtv.tv_sec, (int) newtv.tv_usec); + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } +#endif + } + } + if (port_to_use == USE_CHARGEN) { + sendline = CHARGENERATED; + recvline[strlen(sendline)] = 0; + if (strcmp(sendline, recvline) != 0) { + /* + * TODO: it does not work if + * the size is lower than the + * length of CHARGENERATED + */ + printf(" I got back:\n%s\n", recvline); + printf(" instead of the most common:\n%s\n", sendline); + err_ret("Strange server"); + } + if (verbose) { + printf("Checked\n"); + } + } + } + tvsub(&newtv, &oldtv); + tvmin(&min, &newtv); + tvmax(&max, &newtv); + printf("Elapsed time: %d.%06d seconds\n", + (int) newtv.tv_sec, (int) newtv.tv_usec); #ifdef FLUSH_OUTPUT - if (fflush((FILE *) NULL) != 0) { - err_sys("I cannot flush"); - } -#endif - results[i - 1].valid = 1; - if (measure_data_transfer_only) - results[i - 1].timevalue = measured; - else - results[i - 1].timevalue = newtv; - successes++; - } - if (number > 1) { + if (fflush((FILE *) NULL) != 0) { + err_sys("I cannot flush"); + } +#endif + results[i - 1].valid = 1; + if (measure_data_transfer_only) + results[i - 1].timevalue = measured; + else + results[i - 1].timevalue = newtv; + successes++; + } + if (number > 1) { #ifdef OPENSSL - if (ssl) { - /* - * SSL_clear (sslh); No, we have to free. Bug - * #130151 - */ - SSL_free(sslh); - } + if (ssl) { + /* + * SSL_clear (sslh); No, we have to free. Bug + * #130151 + */ + SSL_free(sslh); + } #endif #ifdef GNUTLS - if (ssl) { - gnutls_bye(channel.tls, GNUTLS_SHUT_RDWR); - gnutls_deinit(session); - /* - * gnutls_certificate_free_credentials(xcred); - * - */ - } -#endif - } - } /* End of main loop */ + if (ssl) { + gnutls_bye(channel.tls, GNUTLS_SHUT_RDWR); + gnutls_deinit(session); + /* + * gnutls_certificate_free_credentials(xcred); + * + */ + } +#endif + } + } /* End of main loop */ - /* Clean */ - if (plugin) - plugin_terminate(); - /* It would be nice to clean here for OpenSSL */ + /* Clean */ + if (plugin) + plugin_terminate(); + /* It would be nice to clean here for OpenSSL */ #ifdef GNUTLS - if (ssl) { - gnutls_global_deinit(); - } -#endif - printstats(); - if (successes >= 1) - exit(0); - else - exit(1); + if (ssl) { + gnutls_global_deinit(); + } +#endif + printstats(); + if (successes >= 1) + exit(0); + else + exit(1); } void printstats() { - int i; + int i; - /* if ((number > 1) && ((!udp) || (successes > 0))) { */ - if (successes > 1) { - printf("---\n"); - if (successes < attempts) - printf("Warning: %d message(s) lost (%d %%)\n", - attempts - successes, - ((attempts - successes) * 100) / attempts); - printf("Minimum time: %d.%06d seconds (%.0f bytes per sec.)\n", - (int) min.tv_sec, (int) min.tv_usec, - (double) size / tv2double(min)); - printf("Maximum time: %d.%06d seconds (%.0f bytes per sec.)\n", - (int) max.tv_sec, (int) max.tv_usec, - (double) size / tv2double(max)); - tvavg(&total, successes); - printf("Average time: %d.%06d seconds (%.0f bytes per sec.)\n", - (int) total.tv_sec, (int) total.tv_usec, - (double) size / tv2double(total)); - /* - * The number of bytes/second, as printed above, is not - * really meaningful: size does not reflect the number of - * bytes exchanged. With echo, N = 2*size, with discard, N = - * size, with http, N = size + (response)... - */ - tvstddev(&stddev, successes, total, results); - printf("Standard deviation: %d.%06d\n", - (int) stddev.tv_sec, (int) stddev.tv_usec); - for (i = 0; i < number; i++) { - if (results[i].valid) - good_results[j++] = results[i].timevalue; - } - if (successes != j) /* Todo: bug! */ - err_quit("successes (%d) is different from j (%d)", - successes, j); - qsort(good_results, successes, sizeof(struct timeval), tvcmp); - /* - * for (i = 1; i <= number; i++) { printf("---\nTime %d th: - * %d.%06d seconds\n", i, results[i-1].tv_sec, - * results[i-1].tv_usec); } - */ - if ((successes % 2) == 1) { - /* - * printf("Searching good_results[%d]\n", (successes - * + 1) / 2 - 1); - */ - median = good_results[((successes + 1) / 2 - 1)]; - } else { - /* - * printf("Searching good_results[%d] and [%d]\n", - * (successes / 2) - 1, successes / 2); - */ - tvadd(&median, &good_results[(successes / 2) - 1]); - tvadd(&median, &good_results[successes / 2]); - tvavg(&median, 2); - } - printf("Median time: %d.%06d seconds (%.0f bytes per sec.)\n", - (int) median.tv_sec, (int) median.tv_usec, - (double) size / tv2double(median)); - if (n_stddev) { - tvstddevavg(&stddev, successes, total, results, - (double) n_stddev); - printf - ("Average of values within %d standard deviation%s: %d.%06d\n", - n_stddev, (n_stddev == 1 ? "" : "s"), - (int) stddev.tv_sec, (int) stddev.tv_usec); - } - } + /* if ((number > 1) && ((!udp) || (successes > 0))) { */ + if (successes > 1) { + printf("---\n"); + if (successes < attempts) + printf("Warning: %d message(s) lost (%d %%)\n", + attempts - successes, ((attempts - successes) * 100) / attempts); + printf("Minimum time: %d.%06d seconds (%.0f bytes per sec.)\n", + (int) min.tv_sec, (int) min.tv_usec, (double) size / tv2double(min)); + printf("Maximum time: %d.%06d seconds (%.0f bytes per sec.)\n", + (int) max.tv_sec, (int) max.tv_usec, (double) size / tv2double(max)); + tvavg(&total, successes); + printf("Average time: %d.%06d seconds (%.0f bytes per sec.)\n", + (int) total.tv_sec, (int) total.tv_usec, + (double) size / tv2double(total)); + /* + * The number of bytes/second, as printed above, is not + * really meaningful: size does not reflect the number of + * bytes exchanged. With echo, N = 2*size, with discard, N = + * size, with http, N = size + (response)... + */ + tvstddev(&stddev, successes, total, results); + printf("Standard deviation: %d.%06d\n", + (int) stddev.tv_sec, (int) stddev.tv_usec); + for (i = 0; i < number; i++) { + if (results[i].valid) + good_results[j++] = results[i].timevalue; + } + if (successes != j) /* Todo: bug! */ + err_quit("successes (%d) is different from j (%d)", successes, j); + qsort(good_results, successes, sizeof(struct timeval), tvcmp); + /* + * for (i = 1; i <= number; i++) { printf("---\nTime %d th: + * %d.%06d seconds\n", i, results[i-1].tv_sec, + * results[i-1].tv_usec); } + */ + if ((successes % 2) == 1) { + /* + * printf("Searching good_results[%d]\n", (successes + * + 1) / 2 - 1); + */ + median = good_results[((successes + 1) / 2 - 1)]; + } else { + /* + * printf("Searching good_results[%d] and [%d]\n", + * (successes / 2) - 1, successes / 2); + */ + tvadd(&median, &good_results[(successes / 2) - 1]); + tvadd(&median, &good_results[successes / 2]); + tvavg(&median, 2); + } + printf("Median time: %d.%06d seconds (%.0f bytes per sec.)\n", + (int) median.tv_sec, (int) median.tv_usec, + (double) size / tv2double(median)); + if (n_stddev) { + tvstddevavg(&stddev, successes, total, results, (double) n_stddev); + printf + ("Average of values within %d standard deviation%s: %d.%06d\n", + n_stddev, (n_stddev == 1 ? "" : "s"), + (int) stddev.tv_sec, (int) stddev.tv_usec); + } + } } /* @@ -1595,14 +1489,14 @@ printstats() void to_alarm() { - /* printf ("DEBUG: timeout handler called\n"); */ - timeout_flag = 1; /* set flag for function above */ + /* printf ("DEBUG: timeout handler called\n"); */ + timeout_flag = 1; /* set flag for function above */ } void interrupted() { - printf("Interrupted by user\n"); - printstats(); - exit(1); + printf("Interrupted by user\n"); + printstats(); + exit(1); } diff --git a/SRC/error.c b/SRC/error.c index 7056105..6c9d056 100644 --- a/SRC/error.c +++ b/SRC/error.c @@ -7,7 +7,7 @@ void my_perror() { - fprintf(stderr, " %s\n", sys_err_str()); + fprintf(stderr, " %s\n", sys_err_str()); } /* @@ -22,18 +22,18 @@ my_perror() void err_ret(char *str, ...) { - va_list args; + va_list args; - va_start(args, str); - vfprintf(stderr, str, args); - va_end(args); + va_start(args, str); + vfprintf(stderr, str, args); + va_end(args); - my_perror(); + my_perror(); - fflush(stdout); - fflush(stderr); + fflush(stdout); + fflush(stderr); - return; + return; } /* @@ -49,14 +49,14 @@ err_ret(char *str, ...) void err_quit(char *str, ...) { - va_list args; + va_list args; - va_start(args, str); - vfprintf(stderr, str, args); - fputc('\n', stderr); - va_end(args); + va_start(args, str); + vfprintf(stderr, str, args); + fputc('\n', stderr); + va_end(args); - exit(1); + exit(1); } /* @@ -73,23 +73,23 @@ err_quit(char *str, ...) void err_sys(char *str, ...) { - va_list args; + va_list args; - va_start(args, str); - vfprintf(stderr, str, args); - va_end(args); + va_start(args, str); + vfprintf(stderr, str, args); + va_end(args); - my_perror(); + my_perror(); - exit(1); + exit(1); } void usage(poptContext context) { - poptPrintUsage(context, stderr, 0); - fprintf(stderr, " hostname [plugin-options...]\n"); - exit(1); + poptPrintUsage(context, stderr, 0); + fprintf(stderr, " hostname [plugin-options...]\n"); + exit(1); } /* @@ -105,12 +105,12 @@ usage(poptContext context) char * sys_err_str() { - static char msgstr[200]; - - if (errno != 0) { - sprintf(msgstr, "(%s)", strerror(errno)); - } else { - msgstr[0] = '\0'; - } - return (msgstr); + static char msgstr[200]; + + if (errno != 0) { + sprintf(msgstr, "(%s)", strerror(errno)); + } else { + msgstr[0] = '\0'; + } + return (msgstr); } diff --git a/SRC/http.c b/SRC/http.c index e4b9662..8be54b8 100644 --- a/SRC/http.c +++ b/SRC/http.c @@ -11,149 +11,136 @@ char big_recvline[MAXTOREAD]; char * make_http_sendline(char *url, char *host, int port, int nocache) { - short sport = (short) port; - int size = 350; /* Enough? RFC 2616, section 3.2.1 says 255 - * should be enough, although there is no - * hard limit. We reserve more because there - * * are the protocol elements, the HTTP - * headers, etc */ - char *sendline = (char *) malloc(size); - char *hostname = (char *) malloc(size); - char *cache_directive = ""; - int result; + short sport = (short) port; + int size = 350; /* Enough? RFC 2616, section 3.2.1 says 255 should + * be enough, although there is no hard limit. We + * reserve more because there * are the protocol + * elements, the HTTP headers, etc */ + char *sendline = (char *) malloc(size); + char *hostname = (char *) malloc(size); + char *cache_directive = ""; + int result; #ifdef HTTP10 - if (nocache) - cache_directive = "Pragma: no-cache\r\n"; /* RFC 1945, - * "Hypertext - * Transfer Protocol - * * -- HTTP/1.0" */ - result = snprintf(sendline, size, - "GET %s HTTP/1.0\r\nUser-Agent: Echoping/%s\r\n%s\r\n", - url, VERSION, cache_directive); + if (nocache) + cache_directive = "Pragma: no-cache\r\n"; /* RFC 1945, "Hypertext + * Transfer Protocol * -- + * HTTP/1.0" */ + result = snprintf(sendline, size, + "GET %s HTTP/1.0\r\nUser-Agent: Echoping/%s\r\n%s\r\n", + url, VERSION, cache_directive); #else - if (nocache) { - if (nocache == 1) - cache_directive = "Cache-control: max-age=0\r\n"; /* Simply - * force - * a - * recheck - * with - * the - * server - */ - else - cache_directive = "Cache-control: no-cache\r\n"; /* RFC - * 2616 - * "Hypertext - * Transfer - * Protocol -- - * HTTP/1.1" */ - } - strncpy(hostname, HTParse(url, "", PARSE_HOST), size); /* See bug #1688940 - * to see why we use - * * strNcpy. */ - 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); + if (nocache) { + if (nocache == 1) + cache_directive = "Cache-control: max-age=0\r\n"; /* Simply force a + * recheck with + * the server */ + else + cache_directive = "Cache-control: no-cache\r\n"; /* RFC 2616 + * "Hypertext + * Transfer Protocol + * -- HTTP/1.1" */ + } + strncpy(hostname, HTParse(url, "", PARSE_HOST), size); /* See bug #1688940 + * to see why we use + * * * strNcpy. */ + 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 - if (result >= size) - err_quit("URL and/or hostname too long(s)"); - return sendline; + if (result >= size) + err_quit("URL and/or hostname too long(s)"); + return sendline; } int read_from_server(CHANNEL fs, short ssl, boolean accept_redirects) { - int nr = 0; - int total = 0; - char reply_code; - int first_line = TRUE; - short body = FALSE; + int nr = 0; + int total = 0; + char reply_code; + int first_line = TRUE; + short body = FALSE; #ifdef OPENSSL - int sslcode; + int sslcode; #endif - while (!body && !timeout_flag) { - if (!ssl) - nr = readline(fs.fs, big_recvline, MAXTOREAD, TRUE); + while (!body && !timeout_flag) { + if (!ssl) + nr = readline(fs.fs, big_recvline, MAXTOREAD, TRUE); #ifdef OPENSSL - else { - nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, TRUE); - if (nr == -1) { - sslcode = ERR_get_error(); - err_ret("SSL_readline error: %s", - ERR_error_string(sslcode, NULL)); - } - } + else { + nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, TRUE); + if (nr == -1) { + sslcode = ERR_get_error(); + err_ret("SSL_readline error: %s", ERR_error_string(sslcode, NULL)); + } + } #endif #ifdef GNUTLS - else - { - nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, TRUE); - if (nr == -1) { - err_ret("TLS_readline error: %s", - gnutls_strerror(nr)); - } - } + else + { + nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, TRUE); + if (nr == -1) { + err_ret("TLS_readline error: %s", gnutls_strerror(nr)); + } + } #endif - /* - * printf ("DEBUG: reading \"%s\"\n (%d chars)\n", - * big_recvline, nr); - */ - /* - * HTTP replies should be separated by CR-LF. Unfortunately, - * some servers send only CR :-( - */ - body = ((nr == 2) || (nr == 1)); /* Empty line CR-LF seen */ - if ((nr < 1) && (timeout_flag)) /* Probably a timeout */ - return -1; - if (nr < 1) - /* SourceForge bug #109385 */ - /* err_sys ("Error reading HTTP header"); */ - return -1; - /* - * if ((int) big_recvline[nr-1] == 10) nr--; - */ - if (first_line) { - reply_code = big_recvline[9]; /* 9 because "HTTP/1.x - * 200..." */ - if (reply_code != '2' - && !(reply_code == '3' && accept_redirects)) - /* - * Status codes beginning with 3 are not - * errors See bug #850674 and RFC 2616, - * section 10.3 - */ - err_quit("HTTP error \"%s\"", big_recvline); - } - total = total + nr; - first_line = FALSE; - } - /* Read the body */ - if (!ssl) - nr = readline(fs.fs, big_recvline, MAXTOREAD, FALSE); + /* + * printf ("DEBUG: reading \"%s\"\n (%d chars)\n", + * big_recvline, nr); + */ + /* + * HTTP replies should be separated by CR-LF. Unfortunately, + * some servers send only CR :-( + */ + body = ((nr == 2) || (nr == 1)); /* Empty line CR-LF seen */ + if ((nr < 1) && (timeout_flag)) /* Probably a timeout */ + return -1; + if (nr < 1) + /* SourceForge bug #109385 */ + /* err_sys ("Error reading HTTP header"); */ + return -1; + /* + * if ((int) big_recvline[nr-1] == 10) nr--; + */ + if (first_line) { + reply_code = big_recvline[9]; /* 9 because "HTTP/1.x 200..." */ + if (reply_code != '2' && !(reply_code == '3' && accept_redirects)) + /* + * Status codes beginning with 3 are not + * errors See bug #850674 and RFC 2616, + * section 10.3 + */ + err_quit("HTTP error \"%s\"", big_recvline); + } + total = total + nr; + first_line = FALSE; + } + /* Read the body */ + if (!ssl) + nr = readline(fs.fs, big_recvline, MAXTOREAD, FALSE); #ifdef OPENSSL - else - nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, FALSE); + else + nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, FALSE); #endif #ifdef GNUTLS - else - nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, FALSE); + else + nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, FALSE); #endif - /* - * printf ("DEBUG: reading body \"%s\"\n (%d chars)\n", big_recvline, - * nr); - */ - if ((nr < 2) && (timeout_flag)) /* Probably a timeout */ - return -1; - if (nr < 2) /* Hmm, if the body is empty, we'll get a * * * * * - * * meaningless error message */ - err_sys("Error reading HTTP body"); - total = total + nr; - return total; /* How to do if we want only the body's size? */ + /* + * printf ("DEBUG: reading body \"%s\"\n (%d chars)\n", big_recvline, + * nr); + */ + if ((nr < 2) && (timeout_flag)) /* Probably a timeout */ + return -1; + if (nr < 2) /* Hmm, if the body is empty, we'll get a * * * * * + * * * meaningless error message */ + err_sys("Error reading HTTP body"); + total = total + nr; + return total; /* How to do if we want only the body's size? */ } -#endif /* HTTP */ +#endif /* HTTP */ diff --git a/SRC/icp.c b/SRC/icp.c index 0b0a32b..07a42d8 100644 --- a/SRC/icp.c +++ b/SRC/icp.c @@ -13,94 +13,94 @@ void * make_icp_sendline(url, shost, opcode, length) - const char *url; - u_num32 *shost; - icp_opcode opcode; - int *length; + const char *url; + u_num32 *shost; + icp_opcode opcode; + int *length; { - icp_common_t *headerp = NULL; - char *buf = NULL; - char *urloffset = NULL; - u_num32 flags = ICP_FLAG_SRC_RTT; - u_num32 pad = 0; - u_num32 reqnum = 4711; - unsigned short buf_len; + icp_common_t *headerp = NULL; + char *buf = NULL; + char *urloffset = NULL; + u_num32 flags = ICP_FLAG_SRC_RTT; + u_num32 pad = 0; + u_num32 reqnum = 4711; + unsigned short buf_len; - buf_len = sizeof(icp_common_t) + strlen(url) + 1; - if (opcode == ICP_OP_QUERY) - buf_len += sizeof(u_num32); - buf = calloc(buf_len, 1); - headerp = (icp_common_t *) buf; - headerp->opcode = opcode; - headerp->version = ICP_VERSION_CURRENT; - headerp->length = htons(buf_len); - headerp->reqnum = htonl(reqnum); - headerp->flags = htonl(flags); - headerp->pad = pad; - if (shost) - headerp->shostid = htonl(*shost); - /* urloffset = (char *) ((int) buf + sizeof(icp_common_t)); */ - urloffset = (char *) (buf + sizeof(icp_common_t)); - if (opcode == ICP_OP_QUERY) - urloffset += sizeof(u_num32); - memcpy(urloffset, url, strlen(url)); - *length = buf_len; - return buf; + buf_len = sizeof(icp_common_t) + strlen(url) + 1; + if (opcode == ICP_OP_QUERY) + buf_len += sizeof(u_num32); + buf = calloc(buf_len, 1); + headerp = (icp_common_t *) buf; + headerp->opcode = opcode; + headerp->version = ICP_VERSION_CURRENT; + headerp->length = htons(buf_len); + headerp->reqnum = htonl(reqnum); + headerp->flags = htonl(flags); + headerp->pad = pad; + if (shost) + headerp->shostid = htonl(*shost); + /* urloffset = (char *) ((int) buf + sizeof(icp_common_t)); */ + urloffset = (char *) (buf + sizeof(icp_common_t)); + if (opcode == ICP_OP_QUERY) + urloffset += sizeof(u_num32); + memcpy(urloffset, url, strlen(url)); + *length = buf_len; + return buf; } int recv_icp(sockfd, buf, retcode) - int sockfd; - char *buf; - char *retcode; + int sockfd; + char *buf; + char *retcode; -{ /* - * based on draft-wessels-icp-v2-02.txt - */ - icp_common_t *headerp = (icp_common_t *) buf; - int nr, length; - unsigned char opcode; - static char *icp_op_code[] = { - /* 0 */ "ICP_OP_INVALID", - /* 1 */ "ICP_OP_QUERY", - /* 2 */ "ICP_OP_HIT", - /* 3 */ "ICP_OP_MISS", - /* 4 */ "ICP_OP_ERR", - /* 5 */ "", - /* 6 */ "", - /* 7 */ "", - /* 8 */ "", - /* 9 */ "", - /* 10 */ "ICP_OP_SECHO", - /* 11 */ "ICP_OP_DECHO", - /* 12 */ "", - /* 13 */ "", - /* 14 */ "", - /* 15 */ "", - /* 16 */ "", - /* 17 */ "", - /* 18 */ "", - /* 19 */ "", - /* 20 */ "", - /* 21 */ "ICP_OP_MISS_NOFETCH", - /* 22 */ "ICP_OP_DENIED", - /* 23 */ "ICP_OP_HIT_OBJ" - }; +{ /* + * based on draft-wessels-icp-v2-02.txt + */ + icp_common_t *headerp = (icp_common_t *) buf; + int nr, length; + unsigned char opcode; + static char *icp_op_code[] = { + /* 0 */ "ICP_OP_INVALID", + /* 1 */ "ICP_OP_QUERY", + /* 2 */ "ICP_OP_HIT", + /* 3 */ "ICP_OP_MISS", + /* 4 */ "ICP_OP_ERR", + /* 5 */ "", + /* 6 */ "", + /* 7 */ "", + /* 8 */ "", + /* 9 */ "", + /* 10 */ "ICP_OP_SECHO", + /* 11 */ "ICP_OP_DECHO", + /* 12 */ "", + /* 13 */ "", + /* 14 */ "", + /* 15 */ "", + /* 16 */ "", + /* 17 */ "", + /* 18 */ "", + /* 19 */ "", + /* 20 */ "", + /* 21 */ "ICP_OP_MISS_NOFETCH", + /* 22 */ "ICP_OP_DENIED", + /* 23 */ "ICP_OP_HIT_OBJ" + }; - nr = recvfrom(sockfd, buf, DEFLINE, 0, (struct sockaddr *) 0, (int *) 0); - if (nr < 0) { - if (timeout_flag) - err_quit("Timeout while reading"); - else - err_sys("No reply from ICP proxy server"); - } - opcode = headerp->opcode; - length = ntohs(headerp->length); - sprintf(retcode, "ICP reply: \42%s\42", icp_op_code[opcode]); - return length; + nr = recvfrom(sockfd, buf, DEFLINE, 0, (struct sockaddr *) 0, (int *) 0); + if (nr < 0) { + if (timeout_flag) + err_quit("Timeout while reading"); + else + err_sys("No reply from ICP proxy server"); + } + opcode = headerp->opcode; + length = ntohs(headerp->length); + sprintf(retcode, "ICP reply: \42%s\42", icp_op_code[opcode]); + return length; } -#endif /* ICP */ +#endif /* ICP */ diff --git a/SRC/plugins/dns/dns.c b/SRC/plugins/dns/dns.c index 78df2ae..fb8300e 100644 --- a/SRC/plugins/dns/dns.c +++ b/SRC/plugins/dns/dns.c @@ -35,180 +35,178 @@ boolean no_recurse = FALSE; ****************************************************************/ int nsError(error, domain) - int error; - char *domain; + int error; + char *domain; { - switch (error) { - case HOST_NOT_FOUND: - err_ret("Unknown domain: %s\n", domain); - return -1; - case NO_DATA: - err_ret("No records of type %s for %s in the Answer section\n", - to_upper(type_name), domain); - return -1; - case TRY_AGAIN: - err_ret("No response for query\n"); - return -2; - default: - err_ret("Unexpected error\n"); - return -1; - } + switch (error) { + case HOST_NOT_FOUND: + err_ret("Unknown domain: %s\n", domain); + return -1; + case NO_DATA: + err_ret("No records of type %s for %s in the Answer section\n", + to_upper(type_name), domain); + return -1; + case TRY_AGAIN: + err_ret("No response for query\n"); + return -2; + default: + err_ret("Unexpected error\n"); + return -1; + } } void dns_usage(const char *msg) { - if (msg) { - fprintf(stderr, "Error: %s\n", msg); - } - poptPrintUsage(dns_poptcon, stderr, 0); - fprintf(stderr, " request\n"); - exit(1); + if (msg) { + fprintf(stderr, "Error: %s\n", msg); + } + poptPrintUsage(dns_poptcon, stderr, 0); + fprintf(stderr, " request\n"); + exit(1); } char * init(const int argc, const char **argv) { - int value; - char *hostname; - char *msg = malloc(256); - char *upper_type_name = NULL; - /* popt variables */ - struct poptOption options[] = { - {"type", 't', POPT_ARG_STRING, &type_name, 0, - "Type of resources queried (A, MX, SOA, etc)", - "type"}, - {"tcp", (char) NULL, POPT_ARG_NONE, &use_tcp, 0, - "Use TCP for the request (virtual circuit)", - "tcp"}, - {"no-recurse", (char) NULL, POPT_ARG_NONE, &no_recurse, 0, - "Do not ask recursion", - "no-recurse"}, - POPT_AUTOHELP POPT_TABLEEND - }; - dns_poptcon = poptGetContext(NULL, argc, - argv, options, POPT_CONTEXT_KEEP_FIRST); - while ((value = poptGetNextOpt(dns_poptcon)) > 0) { - } - if (value < -1) { - sprintf(msg, "%s: %s", - poptBadOption(dns_poptcon, POPT_BADOPTION_NOALIAS), - poptStrerror(value)); - dns_usage(msg); - } - hostname = (char *) poptGetArg(dns_poptcon); /* Not used */ - request = (char *) poptGetArg(dns_poptcon); - if (request == NULL) - dns_usage("Mandatory request missing"); - if ((type_name == NULL) || !strcmp(type_name, "")) { - type = T_A; - type_name = "A"; - } else { - upper_type_name = (char *) to_upper(type_name); - /* - * TODO: a better algorithm. Use dns_rdatatype_fromtext in - * BIND ? - */ - if (!strcmp(upper_type_name, "A")) - type = T_A; - else if (!strcmp(upper_type_name, "AAAA")) - type = T_AAAA; - else if (!strcmp(upper_type_name, "NS")) - type = T_NS; - else if (!strcmp(upper_type_name, "SOA")) - type = T_SOA; - else if (!strcmp(upper_type_name, "MX")) - type = T_MX; - else if (!strcmp(upper_type_name, "SRV")) - type = T_SRV; - else if (!strcmp(upper_type_name, "CNAME")) - type = T_CNAME; - else if (!strcmp(upper_type_name, "PTR")) - type = T_PTR; - else if (!strcmp(upper_type_name, "TXT")) - type = T_TXT; - else if (!strcmp(upper_type_name, "ANY")) - type = T_ANY; - else - dns_usage("Unknown type"); - } - return "domain"; + int value; + char *hostname; + char *msg = malloc(256); + char *upper_type_name = NULL; + /* popt variables */ + struct poptOption options[] = { + {"type", 't', POPT_ARG_STRING, &type_name, 0, + "Type of resources queried (A, MX, SOA, etc)", + "type"}, + {"tcp", (char) NULL, POPT_ARG_NONE, &use_tcp, 0, + "Use TCP for the request (virtual circuit)", + "tcp"}, + {"no-recurse", (char) NULL, POPT_ARG_NONE, &no_recurse, 0, + "Do not ask recursion", + "no-recurse"}, + POPT_AUTOHELP POPT_TABLEEND + }; + dns_poptcon = poptGetContext(NULL, argc, argv, options, POPT_CONTEXT_KEEP_FIRST); + while ((value = poptGetNextOpt(dns_poptcon)) > 0) { + } + if (value < -1) { + sprintf(msg, "%s: %s", + poptBadOption(dns_poptcon, POPT_BADOPTION_NOALIAS), + poptStrerror(value)); + dns_usage(msg); + } + hostname = (char *) poptGetArg(dns_poptcon); /* Not used */ + request = (char *) poptGetArg(dns_poptcon); + if (request == NULL) + dns_usage("Mandatory request missing"); + if ((type_name == NULL) || !strcmp(type_name, "")) { + type = T_A; + type_name = "A"; + } else { + upper_type_name = (char *) to_upper(type_name); + /* + * TODO: a better algorithm. Use dns_rdatatype_fromtext in + * BIND ? + */ + if (!strcmp(upper_type_name, "A")) + type = T_A; + else if (!strcmp(upper_type_name, "AAAA")) + type = T_AAAA; + else if (!strcmp(upper_type_name, "NS")) + type = T_NS; + else if (!strcmp(upper_type_name, "SOA")) + type = T_SOA; + else if (!strcmp(upper_type_name, "MX")) + type = T_MX; + else if (!strcmp(upper_type_name, "SRV")) + type = T_SRV; + else if (!strcmp(upper_type_name, "CNAME")) + type = T_CNAME; + else if (!strcmp(upper_type_name, "PTR")) + type = T_PTR; + else if (!strcmp(upper_type_name, "TXT")) + type = T_TXT; + else if (!strcmp(upper_type_name, "ANY")) + type = T_ANY; + else + dns_usage("Unknown type"); + } + return "domain"; } void start(struct addrinfo *res) { - struct sockaddr name_server_sockaddr; - struct sockaddr_in name_server_sockaddr_in; - struct sockaddr_in6 name_server_sockaddr_in6; - name_server = *res; - name_server_sockaddr = *name_server.ai_addr; - if (name_server_sockaddr.sa_family == AF_INET) { - /* Converts a generic sockaddr to an IPv4 sockaddr_in */ - (void) memcpy((void *) &name_server_sockaddr_in, - &name_server_sockaddr, sizeof(struct sockaddr)); - } else if (name_server_sockaddr.sa_family == AF_INET6) { + struct sockaddr name_server_sockaddr; + struct sockaddr_in name_server_sockaddr_in; + struct sockaddr_in6 name_server_sockaddr_in6; + name_server = *res; + name_server_sockaddr = *name_server.ai_addr; + if (name_server_sockaddr.sa_family == AF_INET) { + /* Converts a generic sockaddr to an IPv4 sockaddr_in */ + (void) memcpy((void *) &name_server_sockaddr_in, + &name_server_sockaddr, sizeof(struct sockaddr)); + } else if (name_server_sockaddr.sa_family == AF_INET6) { #ifdef HAVE_RES_EXT - /* TODO: the code for IPv6 servers is hopelessly broken. Start again - */ - 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"); - /* Converts a generic sockaddr to an IPv6 sockaddr_in6 */ - (void) memcpy((void *) &name_server_sockaddr_in6, - &name_server_sockaddr, sizeof(struct sockaddr)); + /* TODO: the code for IPv6 servers is hopelessly broken. Start again */ + 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"); + /* Converts a generic sockaddr to an IPv6 sockaddr_in6 */ + (void) memcpy((void *) &name_server_sockaddr_in6, + &name_server_sockaddr, sizeof(struct sockaddr)); #else - err_quit - ("IPv6 name servers not supported on this platform, may be you should use the -4 option"); + err_quit + ("IPv6 name servers not supported on this platform, may be you should use the -4 option"); #endif - } else { - err_quit("Unknown family for address of the server"); - } - if (res_init() < 0) - err_sys("res_init"); - if (name_server_sockaddr.sa_family == AF_INET) { - _res.nsaddr_list[0] = name_server_sockaddr_in; - } else if (name_server_sockaddr.sa_family == AF_INET6) { + } else { + err_quit("Unknown family for address of the server"); + } + if (res_init() < 0) + err_sys("res_init"); + if (name_server_sockaddr.sa_family == AF_INET) { + _res.nsaddr_list[0] = name_server_sockaddr_in; + } else if (name_server_sockaddr.sa_family == AF_INET6) { #ifdef HAVE_RES_EXT - /* TODO: completely broken, dioes not work. Check in Stevens */ - (void) memcpy(_res_ext.nsaddr_list, &name_server_sockaddr_in6, - sizeof(struct sockaddr_in6)); + /* TODO: completely broken, dioes not work. Check in Stevens */ + (void) memcpy(_res_ext.nsaddr_list, &name_server_sockaddr_in6, + sizeof(struct sockaddr_in6)); #endif - } - _res.nscount = 1; - _res.options &= ~(RES_DNSRCH | RES_DEFNAMES | RES_NOALIASES); - if (use_tcp) { - _res.options |= RES_USEVC; - } - if (no_recurse) { - _res.options &= ~RES_RECURSE; - } + } + _res.nscount = 1; + _res.options &= ~(RES_DNSRCH | RES_DEFNAMES | RES_NOALIASES); + if (use_tcp) { + _res.options |= RES_USEVC; + } + if (no_recurse) { + _res.options &= ~RES_RECURSE; + } } int execute() { - union { - HEADER hdr; /* defined in resolv.h */ - u_char buf[PACKETSZ]; /* defined in arpa/nameser.h */ - } response; /* response buffers */ - int response_length; /* buffer length */ - if ((response_length = res_query(request, /* the domain we care about */ - C_IN, /* Internet class records */ - type, (u_char *) & response, /* response - * buffer */ - sizeof(response))) /* buffer size */ - <0) { /* If negative */ - nsError(h_errno, request); /* report the error */ - if (h_errno == TRY_AGAIN) - return -1; /* More luck next time? */ - else - return -2; /* Give in */ - } - /* - * TODO: better analysis of the replies. For instance, replies can be - * in the authority section (delegation info) - */ - return 0; + union { + HEADER hdr; /* defined in resolv.h */ + u_char buf[PACKETSZ]; /* defined in arpa/nameser.h */ + } response; /* response buffers */ + int response_length; /* buffer length */ + if ((response_length = res_query(request, /* the domain we care about */ + C_IN, /* Internet class records */ + type, (u_char *) & response, /* response + * buffer */ + sizeof(response))) /* buffer size */ + <0) { /* If negative */ + nsError(h_errno, request); /* report the error */ + if (h_errno == TRY_AGAIN) + return -1; /* More luck next time? */ + else + return -2; /* Give in */ + } + /* + * TODO: better analysis of the replies. For instance, replies can be + * in the authority section (delegation info) + */ + return 0; } void diff --git a/SRC/plugins/ldap/ldap.c b/SRC/plugins/ldap/ldap.c index bc5460e..bf6f69b 100644 --- a/SRC/plugins/ldap/ldap.c +++ b/SRC/plugins/ldap/ldap.c @@ -23,146 +23,141 @@ echoping_options global_options; void ldap_usage(const char *msg) { - if (msg) { - printf("LDAP plugin error: %s\n", msg); - } - poptPrintUsage(ldap_poptcon, stdout, 0); - exit(1); + if (msg) { + printf("LDAP plugin error: %s\n", msg); + } + poptPrintUsage(ldap_poptcon, stdout, 0); + exit(1); } char * init(const int argc, const char **argv, const echoping_options global_external_options) { - int value; - char *msg = malloc(MAX_LINE); - char *rest, *port_text; - char *scope_string = NULL; - /* popt variables */ - struct poptOption options[] = { - {"request", 'r', POPT_ARG_STRING, &request, 0, - "Request (filter) to send to the LDAP server", "r"}, - {"base", 'b', POPT_ARG_STRING, &base, 0, - "Base of the LDAP tree", "b"}, - {"scope", 's', POPT_ARG_STRING, &scope_string, 0, - "Scope of the search in the LDAP tree (sub, one or base)", "s"}, - {"port", 'p', POPT_ARG_INT, &port, 0, - "TCP port to connect to the LDAP server", "p"}, - POPT_AUTOHELP POPT_TABLEEND - }; - global_options = global_external_options; - if (global_options.udp) - err_quit("UDP makes no sense for a LDAP connection"); - ldap_poptcon = poptGetContext(NULL, argc, - argv, options, POPT_CONTEXT_KEEP_FIRST); - while ((value = poptGetNextOpt(ldap_poptcon)) > 0) { - } - if (value < -1) { - sprintf(msg, "%s: %s", - poptBadOption(ldap_poptcon, POPT_BADOPTION_NOALIAS), - poptStrerror(value)); - ldap_usage(msg); - } - if (port == 0) - port = LDAP_PORT; - hostname = poptGetArg(ldap_poptcon); - rest = poptGetArg(ldap_poptcon); - if (rest != NULL) { - fprintf(stderr, "%s: ", rest); - ldap_usage("Additional arguments"); - } - if (base == NULL) - base = ""; - if (request == NULL || !strcmp(request, "")) - request = "(objectclass=*)"; /* Default mentioned in OpenLDAP - * documentation. Joerg Roth fears - * that it may trigger "Size limit - * exceeded" if there are many - * objects at this node. RFC 4515 - * seems silent here. */ - if (scope_string != NULL) { - scope_string = (char *) to_upper(scope_string); - if (!strcmp(scope_string, "BASE")) - scope = LDAP_SCOPE_BASE; - else if (!strcmp(scope_string, "SUB")) - scope = LDAP_SCOPE_SUBTREE; - else if (!strcmp(scope_string, "ONE")) - scope = LDAP_SCOPE_ONELEVEL; - else - err_quit("Invalid scope \"%s\"", scope_string); - } - if (port == LDAP_PORT) { - return "ldap"; - } else { - port_text = malloc(99); - sprintf(port_text, "%d", port); - return port_text; - } + int value; + char *msg = malloc(MAX_LINE); + char *rest, *port_text; + char *scope_string = NULL; + /* popt variables */ + struct poptOption options[] = { + {"request", 'r', POPT_ARG_STRING, &request, 0, + "Request (filter) to send to the LDAP server", "r"}, + {"base", 'b', POPT_ARG_STRING, &base, 0, + "Base of the LDAP tree", "b"}, + {"scope", 's', POPT_ARG_STRING, &scope_string, 0, + "Scope of the search in the LDAP tree (sub, one or base)", "s"}, + {"port", 'p', POPT_ARG_INT, &port, 0, + "TCP port to connect to the LDAP server", "p"}, + POPT_AUTOHELP POPT_TABLEEND + }; + global_options = global_external_options; + if (global_options.udp) + err_quit("UDP makes no sense for a LDAP connection"); + ldap_poptcon = poptGetContext(NULL, argc, + argv, options, POPT_CONTEXT_KEEP_FIRST); + while ((value = poptGetNextOpt(ldap_poptcon)) > 0) { + } + if (value < -1) { + sprintf(msg, "%s: %s", + poptBadOption(ldap_poptcon, POPT_BADOPTION_NOALIAS), + poptStrerror(value)); + ldap_usage(msg); + } + if (port == 0) + port = LDAP_PORT; + hostname = poptGetArg(ldap_poptcon); + rest = poptGetArg(ldap_poptcon); + if (rest != NULL) { + fprintf(stderr, "%s: ", rest); + ldap_usage("Additional arguments"); + } + if (base == NULL) + base = ""; + if (request == NULL || !strcmp(request, "")) + request = "(objectclass=*)"; /* Default mentioned in OpenLDAP + * documentation. Joerg Roth fears that it + * may trigger "Size limit exceeded" if + * there are many objects at this node. RFC + * 4515 seems silent here. */ + if (scope_string != NULL) { + scope_string = (char *) to_upper(scope_string); + if (!strcmp(scope_string, "BASE")) + scope = LDAP_SCOPE_BASE; + else if (!strcmp(scope_string, "SUB")) + scope = LDAP_SCOPE_SUBTREE; + else if (!strcmp(scope_string, "ONE")) + scope = LDAP_SCOPE_ONELEVEL; + else + err_quit("Invalid scope \"%s\"", scope_string); + } + if (port == LDAP_PORT) { + return "ldap"; + } else { + port_text = malloc(99); + sprintf(port_text, "%d", port); + return port_text; + } } void start() { - int result; - LDAPMessage *response; + int result; + LDAPMessage *response; - session = ldap_init(hostname, port); - if (session == NULL) - err_sys("Cannot initialize LDAP"); - /* TODO: allow non-anonymous connections, with ldap_bind_simple_s */ - /* - * Unfortunately, ldap_init does not connect to the LDAP server. So - * connection errors (e.g. firewall), will not be detected here and - * loop will go on. - * - * To quote the man page: ldap_init() acts just like ldap_open(), but - * does not open a connection to the LDAP server. The actual - * connection open will occur when the first operation is attempted. - * At this time, ldap_init() is preferred. ldap_open() will be - * depreciated in a later release. - * - * So, we perform a dummy search immediately. - * - * See #1879652 for why we use "dummystuff" and not "*" - * - */ - result = ldap_search_s(session, base, LDAP_SCOPE_ONELEVEL, "(objectClass=dummystuff)", NULL, /* Return - * * all * - * * - * attributes - */ - 1, &response); - if (result != 0) { - err_quit - ("Cannot connect to %s (no LDAP server or wrong base, probably): %s", - hostname, ldap_err2string(result)); - } + session = ldap_init(hostname, port); + if (session == NULL) + err_sys("Cannot initialize LDAP"); + /* TODO: allow non-anonymous connections, with ldap_bind_simple_s */ + /* + * Unfortunately, ldap_init does not connect to the LDAP server. So + * connection errors (e.g. firewall), will not be detected here and + * loop will go on. + * + * To quote the man page: ldap_init() acts just like ldap_open(), but + * does not open a connection to the LDAP server. The actual + * connection open will occur when the first operation is attempted. + * At this time, ldap_init() is preferred. ldap_open() will be + * depreciated in a later release. + * + * So, we perform a dummy search immediately. + * + * See #1879652 for why we use "dummystuff" and not "*" + * + */ + result = ldap_search_s(session, base, LDAP_SCOPE_ONELEVEL, "(objectClass=dummystuff)", NULL, /* Return + * * all * + * * * + * attributes + */ + 1, &response); + if (result != 0) { + err_quit + ("Cannot connect to %s (no LDAP server or wrong base, probably): %s", + hostname, ldap_err2string(result)); + } } int execute() { - int result; - LDAPMessage *response; - result = ldap_search_s(session, base, scope, request, NULL, /* Return - * all - * attributes - */ - 0, /* Return attribute types *and* values */ - &response); - if (result != 0) { - err_ret("Cannot search \"%s\": %s", request, - ldap_err2string(result)); - return -1; - } - if (global_options.verbose) - printf("Retrieved: %d entries\n", - ldap_count_entries(session, response)); - return 0; + int result; + LDAPMessage *response; + result = ldap_search_s(session, base, scope, request, NULL, /* Return all + * attributes */ + 0, /* Return attribute types *and* values */ + &response); + if (result != 0) { + err_ret("Cannot search \"%s\": %s", request, ldap_err2string(result)); + return -1; + } + if (global_options.verbose) + printf("Retrieved: %d entries\n", ldap_count_entries(session, response)); + return 0; } void terminate() { - ldap_unbind_s(session); + ldap_unbind_s(session); } diff --git a/SRC/plugins/postgresql/postgresql.c b/SRC/plugins/postgresql/postgresql.c index 7fc461f..90adb55 100644 --- a/SRC/plugins/postgresql/postgresql.c +++ b/SRC/plugins/postgresql/postgresql.c @@ -28,120 +28,116 @@ echoping_options global_options; void postgresql_usage(const char *msg) { - if (msg) { - printf("PostgreSQL plugin error: %s\n", msg); - } - poptPrintUsage(postgresql_poptcon, stdout, 0); - fprintf(stderr, " [SQL-request]\n"); - exit(1); + if (msg) { + printf("PostgreSQL plugin error: %s\n", msg); + } + poptPrintUsage(postgresql_poptcon, stdout, 0); + fprintf(stderr, " [SQL-request]\n"); + exit(1); } char * init(const int argc, const char **argv, const echoping_options global_external_options) { - int value; - char *msg = malloc(256); - char *rest; - /* popt variables */ - struct poptOption options[] = { - {"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0, - "Connection information for the Postgresql server. Something like 'host=foo dbname=bar''", - ""}, - {"readall", 'a', POPT_ARG_NONE, &readall, 0, - "Read all the data sent by the Postgresql server", - ""}, - {"connect-each-time", 'e', POPT_ARG_NONE, &connect_each_time, 0, - "(Re)Connect to the Postgresql server at each iteration", - ""}, - POPT_AUTOHELP POPT_TABLEEND - }; - global_options = global_external_options; - if (global_options.udp) - err_quit("UDP makes no sense for a PostgreSQL connection"); - postgresql_poptcon = poptGetContext(NULL, argc, - argv, options, - POPT_CONTEXT_POSIXMEHARDER); - while ((value = poptGetNextOpt(postgresql_poptcon)) > 0) { - } - if (value < -1) { - sprintf(msg, "%s: %s", - poptBadOption(postgresql_poptcon, POPT_BADOPTION_NOALIAS), - poptStrerror(value)); - postgresql_usage(msg); - } - request = poptGetArg(postgresql_poptcon); - if (request == NULL) - request = "SELECT now()"; - rest = poptGetArg(postgresql_poptcon); - if (rest != NULL) - postgresql_usage("Erroneous additional arguments"); - if (conninfo == NULL) - conninfo = ""; - return NULL; /* We only use the conninfo, echoping does not see - * our hostname or port */ + int value; + char *msg = malloc(256); + char *rest; + /* popt variables */ + struct poptOption options[] = { + {"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0, + "Connection information for the Postgresql server. Something like 'host=foo dbname=bar''", + ""}, + {"readall", 'a', POPT_ARG_NONE, &readall, 0, + "Read all the data sent by the Postgresql server", + ""}, + {"connect-each-time", 'e', POPT_ARG_NONE, &connect_each_time, 0, + "(Re)Connect to the Postgresql server at each iteration", + ""}, + POPT_AUTOHELP POPT_TABLEEND + }; + global_options = global_external_options; + if (global_options.udp) + err_quit("UDP makes no sense for a PostgreSQL connection"); + postgresql_poptcon = poptGetContext(NULL, argc, + argv, options, POPT_CONTEXT_POSIXMEHARDER); + while ((value = poptGetNextOpt(postgresql_poptcon)) > 0) { + } + if (value < -1) { + sprintf(msg, "%s: %s", + poptBadOption(postgresql_poptcon, POPT_BADOPTION_NOALIAS), + poptStrerror(value)); + postgresql_usage(msg); + } + request = poptGetArg(postgresql_poptcon); + if (request == NULL) + request = "SELECT now()"; + rest = poptGetArg(postgresql_poptcon); + if (rest != NULL) + postgresql_usage("Erroneous additional arguments"); + if (conninfo == NULL) + conninfo = ""; + return NULL; /* We only use the conninfo, echoping does not see + * our hostname or port */ } void start_raw() { - if (!connect_each_time) { - conn = PQconnectdb(conninfo); - if (conn == NULL) { - err_quit("Cannot create connection\n"); - } - if (PQstatus(conn) == CONNECTION_BAD) { - err_quit("Connection failed: %s\n", PQerrorMessage(conn)); - } - } + if (!connect_each_time) { + conn = PQconnectdb(conninfo); + if (conn == NULL) { + err_quit("Cannot create connection\n"); + } + if (PQstatus(conn) == CONNECTION_BAD) { + err_quit("Connection failed: %s\n", PQerrorMessage(conn)); + } + } } int execute() { - unsigned int row, column; - char *result; - if (connect_each_time) { - conn = PQconnectdb(conninfo); - if (conn == NULL) { - err_ret("Cannot create connection\n"); - return -1; - } - if (PQstatus(conn) == CONNECTION_BAD) { - err_ret("Connection failed: %s\n", PQerrorMessage(conn)); - return -1; - } - } - res = PQexec(conn, request); - if (PQresultStatus(res) != PGRES_TUPLES_OK) { - err_ret("Cannot run \"%s\": %s\n", request, - PQresultErrorMessage(res)); - return -1; - } - if (global_options.verbose) - printf("%d tuples returned\n", PQntuples(res)); - if (readall) { - for (row = 0; row < PQntuples(res); row++) { - for (column = 0; column < PQnfields(res); column++) { - result = PQgetvalue(res, row, column); - if (result == NULL) { - err_ret("Cannot retrieve value [%d,%d]\n", - row, column); - return -1; - } - /* else { printf ("DEBUG: [%d,%d] %s\n", row, - * column, result); } */ - } - } - } - if (connect_each_time) - PQfinish(conn); - return 0; + unsigned int row, column; + char *result; + if (connect_each_time) { + conn = PQconnectdb(conninfo); + if (conn == NULL) { + err_ret("Cannot create connection\n"); + return -1; + } + if (PQstatus(conn) == CONNECTION_BAD) { + err_ret("Connection failed: %s\n", PQerrorMessage(conn)); + return -1; + } + } + res = PQexec(conn, request); + if (PQresultStatus(res) != PGRES_TUPLES_OK) { + err_ret("Cannot run \"%s\": %s\n", request, PQresultErrorMessage(res)); + return -1; + } + if (global_options.verbose) + printf("%d tuples returned\n", PQntuples(res)); + if (readall) { + for (row = 0; row < PQntuples(res); row++) { + for (column = 0; column < PQnfields(res); column++) { + result = PQgetvalue(res, row, column); + if (result == NULL) { + err_ret("Cannot retrieve value [%d,%d]\n", row, column); + return -1; + } + /* else { printf ("DEBUG: [%d,%d] %s\n", row, column, result); } */ + } + } + } + if (connect_each_time) + PQfinish(conn); + return 0; } void terminate() { - if (!connect_each_time) - PQfinish(conn); + if (!connect_each_time) + PQfinish(conn); } diff --git a/SRC/plugins/random/random.c b/SRC/plugins/random/random.c index 588d619..be02e28 100644 --- a/SRC/plugins/random/random.c +++ b/SRC/plugins/random/random.c @@ -12,11 +12,11 @@ char * init(const int argc, const char *argv[]) { - struct timeval tv; - (void) gettimeofday(&tv, (struct timezone *) NULL); - srand(tv.tv_usec); - return "7"; /* Not used, just to say we use the cooked interface - */ + struct timeval tv; + (void) gettimeofday(&tv, (struct timezone *) NULL); + srand(tv.tv_usec); + return "7"; /* Not used, just to say we use the cooked interface + */ } void @@ -27,8 +27,8 @@ start() int execute() { - usleep(rand() % 1000000); - return 1; + usleep(rand() % 1000000); + return 1; } void diff --git a/SRC/plugins/whois/whois.c b/SRC/plugins/whois/whois.c index fa12e49..d2e6a5c 100644 --- a/SRC/plugins/whois/whois.c +++ b/SRC/plugins/whois/whois.c @@ -26,105 +26,104 @@ echoping_options general_options; void whois_usage(const char *msg) { - if (msg) { - printf("Error: %s\n", msg); - } - poptPrintUsage(whois_poptcon, stdout, 0); - fprintf(stderr, " request\n"); - exit(1); + if (msg) { + printf("Error: %s\n", msg); + } + poptPrintUsage(whois_poptcon, stdout, 0); + fprintf(stderr, " request\n"); + exit(1); } char * init(const int argc, const char **argv, echoping_options global_options) { - int value; - char *msg = malloc(256); - char *rest; - /* popt variables */ - struct poptOption options[] = { - {"dump", 'd', POPT_ARG_NONE, &dump, 'd', - "Dumps the reply from the whois server", - ""}, - POPT_AUTOHELP POPT_TABLEEND - }; + int value; + char *msg = malloc(256); + char *rest; + /* popt variables */ + struct poptOption options[] = { + {"dump", 'd', POPT_ARG_NONE, &dump, 'd', + "Dumps the reply from the whois server", + ""}, + POPT_AUTOHELP POPT_TABLEEND + }; general_options = global_options; - if (global_options.udp) - err_quit("UDP is incompatible with this whois plugin"); - /* Will probably be catched before because /etc/services have no entry for - * UDP port 43 */ - whois_poptcon = poptGetContext(NULL, argc, - argv, options, POPT_CONTEXT_POSIXMEHARDER); - while ((value = poptGetNextOpt(whois_poptcon)) > 0) { - switch ((char) value) { - case 'd': - break; - default: - sprintf(msg, "Wrong option %d (%c)", value, (char) value); - whois_usage(msg); - } - } - if (value < -1) { - sprintf(msg, "%s: %s", - poptBadOption(whois_poptcon, POPT_BADOPTION_NOALIAS), - poptStrerror(value)); - whois_usage(msg); - } - request = (char *) poptGetArg(whois_poptcon); - if (request == NULL) - whois_usage("Mandatory request missing"); - rest = (char *) poptGetArg(whois_poptcon); - if (rest != NULL && strcmp(rest, "")) - whois_usage("Extraneous arguments ignored"); - return "nicname"; + if (global_options.udp) + err_quit("UDP is incompatible with this whois plugin"); + /* Will probably be catched before because /etc/services have no entry for UDP + * port 43 */ + whois_poptcon = poptGetContext(NULL, argc, + argv, options, POPT_CONTEXT_POSIXMEHARDER); + while ((value = poptGetNextOpt(whois_poptcon)) > 0) { + switch ((char) value) { + case 'd': + break; + default: + sprintf(msg, "Wrong option %d (%c)", value, (char) value); + whois_usage(msg); + } + } + if (value < -1) { + sprintf(msg, "%s: %s", + poptBadOption(whois_poptcon, POPT_BADOPTION_NOALIAS), + poptStrerror(value)); + whois_usage(msg); + } + request = (char *) poptGetArg(whois_poptcon); + if (request == NULL) + whois_usage("Mandatory request missing"); + rest = (char *) poptGetArg(whois_poptcon); + if (rest != NULL && strcmp(rest, "")) + whois_usage("Extraneous arguments ignored"); + return "nicname"; } void start(struct addrinfo *res) { - whois_server = *res; + whois_server = *res; } int execute() { - int nr = 0; - char recvline[MAX_LINE + 1]; - char complete_request[MAX_REQUEST]; + int nr = 0; + char recvline[MAX_LINE + 1]; + char complete_request[MAX_REQUEST]; #ifdef HAVE_TCP_INFO - struct tcp_info tcpinfo; - socklen_t socket_length = sizeof(tcpinfo); + struct tcp_info tcpinfo; + socklen_t socket_length = sizeof(tcpinfo); #endif - 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 */ - while ((nr = readline(files, recvline, MAX_LINE, 0)) > 0) - if (dump) - printf("%s", recvline); - if (dump) - printf("\n"); + 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 */ + while ((nr = readline(files, recvline, MAX_LINE, 0)) > 0) + if (dump) + printf("%s", recvline); + if (dump) + printf("\n"); #ifdef HAVE_TCP_INFO - /* Thanks to Perry Lorier for the tip */ - if (general_options.verbose) { - if (getsockopt - (sockfd, SOL_TCP, TCP_INFO, &tcpinfo, &socket_length) - != -1) { - printf("Estimated TCP RTT: %.04f seconds\n", - tcpinfo.tcpi_rtt / 1000000.0); - } - } + /* Thanks to Perry Lorier for the tip */ + if (general_options.verbose) { + if (getsockopt(sockfd, SOL_TCP, TCP_INFO, &tcpinfo, &socket_length) + != -1) { + printf("Estimated TCP RTT: %.04f seconds\n", + tcpinfo.tcpi_rtt / 1000000.0); + } + } #endif - close(sockfd); - return 1; + close(sockfd); + return 1; } void diff --git a/SRC/readline.c b/SRC/readline.c index ab83ebe..c0233ba 100644 --- a/SRC/readline.c +++ b/SRC/readline.c @@ -9,41 +9,41 @@ int readline(fs, ptr, maxlen, ln) - FILE *fs; - char *ptr; - int maxlen; - unsigned short ln; + FILE *fs; + char *ptr; + int maxlen; + unsigned short ln; { - int n = 1; - char *rc; - int r; + int n = 1; + char *rc; + int r; - /* Reading with fgets or fread instead of read one-character-at-a-time is - * more than ten times faster, on a local server. */ - if (ln) { - rc = fgets(ptr, maxlen + 1, fs); - if (rc == NULL) { - if (timeout_flag) - return n; - return (-1); - } - n = strlen(rc); - return n; - } else { - while (n < maxlen) { - r = fread(ptr, 1, maxlen, fs); - if (timeout_flag) - return r; - if (r == 0) { - if (n == 1) - return (0); /* EOF, no data read */ - else - break; /* EOF, some data was read */ - } - n = n + r; - } - } - return (n); + /* Reading with fgets or fread instead of read one-character-at-a-time is more + * than ten times faster, on a local server. */ + if (ln) { + rc = fgets(ptr, maxlen + 1, fs); + if (rc == NULL) { + if (timeout_flag) + return n; + return (-1); + } + n = strlen(rc); + return n; + } else { + while (n < maxlen) { + r = fread(ptr, 1, maxlen, fs); + if (timeout_flag) + return r; + if (r == 0) { + if (n == 1) + return (0); /* EOF, no data read */ + else + break; /* EOF, some data was read */ + } + n = n + r; + } + } + return (n); } #ifdef OPENSSL @@ -54,69 +54,67 @@ int buf_end; int SSL_readline(sslh, ptr, maxlen, ln) - SSL *sslh; - char *ptr; - int maxlen; - unsigned short ln; + SSL *sslh; + char *ptr; + int maxlen; + unsigned short ln; { - int rc = 0; - int n = 0; - int i, oi; - if (ln) { - /* Empty buffer */ - if (buf_end == 0) { - rc = SSL_read(sslh, SSL_buffer, maxlen); - if (rc == -1) - return rc; - buf_end = rc; - buf_ptr = 0; - } - /* No more data in the buffer */ - else if (buf_ptr == buf_end) { - buf_ptr = 0; - rc = SSL_read(sslh, SSL_buffer, maxlen); - if (rc == -1) - return rc; - buf_end = rc; - } else if (SSL_buffer[buf_end] != '\n') { - /* We have a probleme here is the first SSL_read sent back a - * text not finished by a \n. See www.SSL.de for an example. - * We get more data. See bug #230384 */ - rc = SSL_read(sslh, SSL_buffer + buf_end, maxlen); - if (rc == -1) - return rc; - buf_end = buf_end + rc; - } - for (oi = buf_ptr, i = buf_ptr; - i <= buf_end && SSL_buffer[i] != '\n'; i++) { - *ptr++ = SSL_buffer[i]; - buf_ptr++; - } - if (SSL_buffer[i] == '\n') - buf_ptr++; - *ptr = '\0'; - /* if (ln) printf ("SSL_readline returns %d (%s)\n", i - oi, - * SSL_buffer); */ - return (i - oi); - } else { - /* OpenSSL reads at most 4096 characters */ - rc = 1; /* Just to avoid exiting too soon */ - while (n < maxlen && rc != 0) { - if ((buf_end == 0) || (buf_ptr > buf_end)) { - rc = SSL_read(sslh, ptr, maxlen); - buf_end = 0; - buf_ptr = 0; - } else { - for (i = buf_ptr; i < maxlen && i <= buf_end; i++) { - *ptr++ = SSL_buffer[i]; - rc++; - } - buf_ptr = i; - } - n = n + rc; - } - return n; - } + int rc = 0; + int n = 0; + int i, oi; + if (ln) { + /* Empty buffer */ + if (buf_end == 0) { + rc = SSL_read(sslh, SSL_buffer, maxlen); + if (rc == -1) + return rc; + buf_end = rc; + buf_ptr = 0; + } + /* No more data in the buffer */ + else if (buf_ptr == buf_end) { + buf_ptr = 0; + rc = SSL_read(sslh, SSL_buffer, maxlen); + if (rc == -1) + return rc; + buf_end = rc; + } else if (SSL_buffer[buf_end] != '\n') { + /* We have a probleme here is the first SSL_read sent back a text not + * finished by a \n. See www.SSL.de for an example. We get more data. + * See bug #230384 */ + rc = SSL_read(sslh, SSL_buffer + buf_end, maxlen); + if (rc == -1) + return rc; + buf_end = buf_end + rc; + } + for (oi = buf_ptr, i = buf_ptr; i <= buf_end && SSL_buffer[i] != '\n'; i++) { + *ptr++ = SSL_buffer[i]; + buf_ptr++; + } + if (SSL_buffer[i] == '\n') + buf_ptr++; + *ptr = '\0'; + /* if (ln) printf ("SSL_readline returns %d (%s)\n", i - oi, SSL_buffer); */ + return (i - oi); + } else { + /* OpenSSL reads at most 4096 characters */ + rc = 1; /* Just to avoid exiting too soon */ + while (n < maxlen && rc != 0) { + if ((buf_end == 0) || (buf_ptr > buf_end)) { + rc = SSL_read(sslh, ptr, maxlen); + buf_end = 0; + buf_ptr = 0; + } else { + for (i = buf_ptr; i < maxlen && i <= buf_end; i++) { + *ptr++ = SSL_buffer[i]; + rc++; + } + buf_ptr = i; + } + n = n + rc; + } + return n; + } } #endif @@ -128,69 +126,65 @@ int buf_end; int TLS_readline(session, ptr, maxlen, ln) - gnutls_session session; - char *ptr; - int maxlen; - unsigned short ln; + gnutls_session session; + char *ptr; + int maxlen; + unsigned short ln; { - int rc = 0; - int n = 0; - int i, oi; - if (ln) { - /* Empty buffer */ - if (buf_end == 0) { - rc = gnutls_record_recv(session, TLS_buffer, maxlen); - if (rc == -1) - return rc; - buf_end = rc; - buf_ptr = 0; - } - /* No more data in the buffer */ - else if (buf_ptr == buf_end) { - buf_ptr = 0; - rc = gnutls_record_recv(session, TLS_buffer, maxlen); - if (rc == -1) - return rc; - buf_end = rc; - } else if (TLS_buffer[buf_end] != '\n') { - rc = gnutls_record_recv(session, TLS_buffer + buf_end, - maxlen); - if (rc == -1) - return rc; - buf_end = buf_end + rc; - } - for (oi = buf_ptr, i = buf_ptr; - i <= buf_end && TLS_buffer[i] != '\n'; i++) { - *ptr++ = TLS_buffer[i]; - buf_ptr++; - } - if (TLS_buffer[i] == '\n') - buf_ptr++; - *ptr = '\0'; - /* printf ("DEBUG: TLS_readline returns %d (%s)\n", i - oi, - * TLS_buffer); */ - return (i - oi); - } else { - rc = 1; /* Just to avoid exiting too soon */ - while (n < maxlen && rc > 0) { - if ((buf_end == 0) || (buf_ptr > buf_end)) { - rc = gnutls_record_recv(session, ptr, maxlen); - buf_end = 0; - buf_ptr = 0; - } else { - for (i = buf_ptr; i < maxlen && i <= buf_end; i++) { - *ptr++ = TLS_buffer[i]; - rc++; - /* printf ("DEBUG: Now %d chars read\n", - * rc); */ - } - buf_ptr = i; - } - if (rc > 0) - n = n + rc; - /* printf ("DEBUG: Now %d chars to send\n", n); */ - } - return n; - } + int rc = 0; + int n = 0; + int i, oi; + if (ln) { + /* Empty buffer */ + if (buf_end == 0) { + rc = gnutls_record_recv(session, TLS_buffer, maxlen); + if (rc == -1) + return rc; + buf_end = rc; + buf_ptr = 0; + } + /* No more data in the buffer */ + else if (buf_ptr == buf_end) { + buf_ptr = 0; + rc = gnutls_record_recv(session, TLS_buffer, maxlen); + if (rc == -1) + return rc; + buf_end = rc; + } else if (TLS_buffer[buf_end] != '\n') { + rc = gnutls_record_recv(session, TLS_buffer + buf_end, maxlen); + if (rc == -1) + return rc; + buf_end = buf_end + rc; + } + for (oi = buf_ptr, i = buf_ptr; i <= buf_end && TLS_buffer[i] != '\n'; i++) { + *ptr++ = TLS_buffer[i]; + buf_ptr++; + } + if (TLS_buffer[i] == '\n') + buf_ptr++; + *ptr = '\0'; + /* printf ("DEBUG: TLS_readline returns %d (%s)\n", i - oi, TLS_buffer); */ + return (i - oi); + } else { + rc = 1; /* Just to avoid exiting too soon */ + while (n < maxlen && rc > 0) { + if ((buf_end == 0) || (buf_ptr > buf_end)) { + rc = gnutls_record_recv(session, ptr, maxlen); + buf_end = 0; + buf_ptr = 0; + } else { + for (i = buf_ptr; i < maxlen && i <= buf_end; i++) { + *ptr++ = TLS_buffer[i]; + rc++; + /* printf ("DEBUG: Now %d chars read\n", rc); */ + } + buf_ptr = i; + } + if (rc > 0) + n = n + rc; + /* printf ("DEBUG: Now %d chars to send\n", n); */ + } + return n; + } } #endif diff --git a/SRC/smtp.c b/SRC/smtp.c index 3eff2d2..57c2997 100644 --- a/SRC/smtp.c +++ b/SRC/smtp.c @@ -13,22 +13,22 @@ char big_recvline[MAXTOREAD]; int smtp_read_response_from_server(FILE * fs) { - int nr; - int i; + int nr; + int i; - for (i = 0; i < MAXSMTPLINES; i++) { - nr = readline(fs, big_recvline, MAXTOREAD, TRUE); - if (nr <= 4) { - return -1; - } - if (big_recvline[3] == ' ') { - return nr; - } - if (big_recvline[3] != '-') { - return -1; - } - } - return -1; + for (i = 0; i < MAXSMTPLINES; i++) { + nr = readline(fs, big_recvline, MAXTOREAD, TRUE); + if (nr <= 4) { + return -1; + } + if (big_recvline[3] == ' ') { + return nr; + } + if (big_recvline[3] != '-') { + return -1; + } + } + return -1; } -#endif /* SMTP */ +#endif /* SMTP */ diff --git a/SRC/util.c b/SRC/util.c index b06618f..76e2d70 100644 --- a/SRC/util.c +++ b/SRC/util.c @@ -13,39 +13,39 @@ char * random_string(unsigned length) { - char *state = (char *) malloc(sizeof(char) * STATES); - char *result = (char *) malloc(length + 1); - int i, number; - unsigned seed = (unsigned) time((time_t *) NULL); + char *state = (char *) malloc(sizeof(char) * STATES); + char *result = (char *) malloc(length + 1); + int i, number; + unsigned seed = (unsigned) time((time_t *) NULL); - /* printf ("Seed is %u\n", seed); */ + /* printf ("Seed is %u\n", seed); */ - /* Initialize random generator */ - (void) initstate(seed, state, STATES); + /* Initialize random generator */ + (void) initstate(seed, state, STATES); - for (i = 0; i < length; i++) { - number = (random() % 94) + 33; - /* printf ("Number for %d is %d\n", i, number); */ - result[i] = (char) number; - } - result[length] = '\0'; + for (i = 0; i < length; i++) { + number = (random() % 94) + 33; + /* printf ("Number for %d is %d\n", i, number); */ + result[i] = (char) number; + } + result[length] = '\0'; - /* printf ("Result is %s\n", result); */ + /* printf ("Result is %s\n", result); */ - return result; + return result; } char * to_upper(char *input) { - int c; - char *result; - result = (char *) malloc(strlen(input)); - for (c = 0; c < strlen(input); c++) - result[c] = toupper((int)input[c]); - result[strlen(input)] = '\0'; - return result; + int c; + char *result; + result = (char *) malloc(strlen(input)); + for (c = 0; c < strlen(input); c++) + result[c] = toupper((int) input[c]); + result[strlen(input)] = '\0'; + return result; } /* @@ -54,91 +54,91 @@ to_upper(char *input) */ void tvsub(out, in) - struct timeval *out, *in; + struct timeval *out, *in; { - if ((out->tv_usec -= in->tv_usec) < 0) { - --out->tv_sec; - out->tv_usec += 1000000; - } - out->tv_sec -= in->tv_sec; + if ((out->tv_usec -= in->tv_usec) < 0) { + --out->tv_sec; + out->tv_usec += 1000000; + } + out->tv_sec -= in->tv_sec; } /* tvadd -- Adds 2 timeval structs: out = out + in. */ void tvadd(out, in) - struct timeval *out, *in; + struct timeval *out, *in; { - if ((out->tv_usec += in->tv_usec) >= 1000000) { - ++out->tv_sec; - out->tv_usec -= 1000000; - } - out->tv_sec += in->tv_sec; + if ((out->tv_usec += in->tv_usec) >= 1000000) { + ++out->tv_sec; + out->tv_usec -= 1000000; + } + out->tv_sec += in->tv_sec; } /* tvavg -- Averages a timeval struct */ void tvavg(out, number) - struct timeval *out; - int number; + struct timeval *out; + int number; { - double result; - /* - * out->tv_sec = out->tv_sec/number; out->tv_usec = - * out->tv_usec/number; - */ - result = (1000000 * out->tv_sec + out->tv_usec) / number; - /* printf ("Result of average is %f\n", result) */ ; - out->tv_sec = (long) (result / 1000000); - out->tv_usec = (long) (result - (out->tv_sec * 1000000)); + double result; + /* + * out->tv_sec = out->tv_sec/number; out->tv_usec = + * out->tv_usec/number; + */ + result = (1000000 * out->tv_sec + out->tv_usec) / number; + /* printf ("Result of average is %f\n", result) */ ; + out->tv_sec = (long) (result / 1000000); + out->tv_usec = (long) (result - (out->tv_sec * 1000000)); } /* tvstddev -- Computes the standard deviation of a set of results */ void tvstddev(out, number, average, results) - struct timeval *out; - int number; - struct timeval average; - struct result *results; + struct timeval *out; + int number; + struct timeval average; + struct result *results; { - int i; - struct timeval result = null_timeval; - struct timeval avg = null_timeval; + int i; + struct timeval result = null_timeval; + struct timeval avg = null_timeval; #ifdef DEBUG - struct timeval var = null_timeval; + struct timeval var = null_timeval; #endif - struct timeval large, small; - double d_offset, d_square, d_variance = 0; - *out = null_timeval; - for (i = 0; i < number; i++) { - if (results[i].valid == 1) { - result = results[i].timevalue; + struct timeval large, small; + double d_offset, d_square, d_variance = 0; + *out = null_timeval; + for (i = 0; i < number; i++) { + if (results[i].valid == 1) { + result = results[i].timevalue; #ifdef DEBUG - printf("DEBUG: Value is %f (average is %f)\n", tv2double - (result), tv2double(average)); + printf("DEBUG: Value is %f (average is %f)\n", tv2double + (result), tv2double(average)); #endif - avg = average; - if (tvcmp(&result, &avg) == -1) { - small = result; - large = avg; - } else { - large = result; - small = avg; - } - tvsub(&large, &small); + avg = average; + if (tvcmp(&result, &avg) == -1) { + small = result; + large = avg; + } else { + large = result; + small = avg; + } + tvsub(&large, &small); #ifdef DEBUG - printf("abs offset is %f\n", tv2double(large)); + printf("abs offset is %f\n", tv2double(large)); #endif - d_offset = tv2double(large); - d_square = d_offset * d_offset; - d_variance += d_square; + d_offset = tv2double(large); + d_square = d_offset * d_offset; + d_variance += d_square; #ifdef DEBUG - printf("variance is now %f\n", tv2double(var)); + printf("variance is now %f\n", tv2double(var)); #endif - } - } - result = double2tv(sqrt(d_variance / (double) number)); - out->tv_sec = result.tv_sec; - out->tv_usec = result.tv_usec; + } + } + result = double2tv(sqrt(d_variance / (double) number)); + out->tv_sec = result.tv_sec; + out->tv_usec = result.tv_usec; } @@ -147,116 +147,115 @@ tvstddev(out, number, average, results) /* TODO: IWBN to return the number of excluded outliers */ void tvstddevavg(out, number, average, results, n_stddev) - struct timeval *out; /* contains std dev on entry */ - int number; - struct timeval average; - struct result *results; - double n_stddev; + struct timeval *out; /* contains std dev on entry */ + int number; + struct timeval average; + struct result *results; + double n_stddev; { - int i, valid = 0; - struct timeval result; /* working value */ - struct timeval var = null_timeval; /* result accumulator */ - double x; - double maxdev = tv2double(*out) * n_stddev; + int i, valid = 0; + struct timeval result; /* working value */ + struct timeval var = null_timeval; /* result accumulator */ + double x; + double maxdev = tv2double(*out) * n_stddev; - if (tvcmp(out, &null_timeval) == 0) { - /* if the SD is 0 then we just return the average */ - *out = average; - return; - } + if (tvcmp(out, &null_timeval) == 0) { + /* if the SD is 0 then we just return the average */ + *out = average; + return; + } - for (i = 0; i < number; i++) { - if (results[i].valid == 1) { - result = results[i].timevalue; - tvsub(&result, &average); - /* printf ("value is %f (stddev is %f)\n", tv2double - * (result), tv2double (stddev)); */ - /* ensure that result (difference to average) is absolute - * value */ - if (tvcmp(&result, &null_timeval) == -1) { - result = average; - tvsub(&result, &results[i].timevalue); - } - x = tv2double(result); - /* printf("value is %g maxdev %g\n",x,maxdev); */ - if (x <= maxdev) { - /* deviation is less than stddev */ - tvadd(&var, &results[i].timevalue); - valid++; - } else { - /* printf("dropped\n"); */ - } - } - } - /* printf ("total is %f in %d samples\n", tv2double (var), valid); */ - if (valid > 0) { - *out = double2tv(tv2double(var) / valid); - } else { - *out = null_timeval; - } + for (i = 0; i < number; i++) { + if (results[i].valid == 1) { + result = results[i].timevalue; + tvsub(&result, &average); + /* printf ("value is %f (stddev is %f)\n", tv2double (result), tv2double + * (stddev)); */ + /* ensure that result (difference to average) is absolute value */ + if (tvcmp(&result, &null_timeval) == -1) { + result = average; + tvsub(&result, &results[i].timevalue); + } + x = tv2double(result); + /* printf("value is %g maxdev %g\n",x,maxdev); */ + if (x <= maxdev) { + /* deviation is less than stddev */ + tvadd(&var, &results[i].timevalue); + valid++; + } else { + /* printf("dropped\n"); */ + } + } + } + /* printf ("total is %f in %d samples\n", tv2double (var), valid); */ + if (valid > 0) { + *out = double2tv(tv2double(var) / valid); + } else { + *out = null_timeval; + } } /* tvcmp -- Compares two timeval structs */ int tvcmp(left, right) - struct timeval *left, *right; + struct timeval *left, *right; { - if (left->tv_sec < right->tv_sec) { - return -1; - } - if (left->tv_sec > right->tv_sec) { - return 1; - } - if (left->tv_usec < right->tv_usec) { - return -1; - } - if (left->tv_usec > right->tv_usec) { - return 1; - } - return 0; + if (left->tv_sec < right->tv_sec) { + return -1; + } + if (left->tv_sec > right->tv_sec) { + return 1; + } + if (left->tv_usec < right->tv_usec) { + return -1; + } + if (left->tv_usec > right->tv_usec) { + return 1; + } + return 0; } /* tvmin */ void tvmin(champion, challenger) - struct timeval *champion, *challenger; + struct timeval *champion, *challenger; { - if (tvcmp(champion, challenger) == 1) { - champion->tv_sec = challenger->tv_sec; - champion->tv_usec = challenger->tv_usec; - } + if (tvcmp(champion, challenger) == 1) { + champion->tv_sec = challenger->tv_sec; + champion->tv_usec = challenger->tv_usec; + } } /* tvmax */ void tvmax(champion, challenger) - struct timeval *champion, *challenger; + struct timeval *champion, *challenger; { - if (tvcmp(champion, challenger) == -1) { - champion->tv_sec = challenger->tv_sec; - champion->tv_usec = challenger->tv_usec; - } + if (tvcmp(champion, challenger) == -1) { + champion->tv_sec = challenger->tv_sec; + champion->tv_usec = challenger->tv_usec; + } } double tv2double(tv) - struct timeval tv; + struct timeval tv; { - double result; - result = - (((((double) tv.tv_sec) * 1000000.0) + (double) tv.tv_usec) / 1000000.0); - /* printf ("Double is %9.3f\n", result); */ - return result; + double result; + result = + (((((double) tv.tv_sec) * 1000000.0) + (double) tv.tv_usec) / 1000000.0); + /* printf ("Double is %9.3f\n", result); */ + return result; } struct timeval double2tv(x) - double x; + double x; { - struct timeval result; - result.tv_sec = (int) (x); - result.tv_usec = (int) ((x - result.tv_sec) * 1000000); - return result; + struct timeval result; + result.tv_sec = (int) (x); + result.tv_usec = (int) ((x - result.tv_sec) * 1000000); + return result; } diff --git a/SRC/writen.c b/SRC/writen.c index 8398fd6..544f68b 100644 --- a/SRC/writen.c +++ b/SRC/writen.c @@ -9,24 +9,24 @@ int writen(fd, ptr, nbytes) - register int fd; - register char *ptr; - register int nbytes; + register int fd; + register char *ptr; + register int nbytes; { - int nleft, nwritten; + int nleft, nwritten; - nleft = nbytes; - while (nleft > 0) { - nwritten = write(fd, ptr, nleft); - if (nwritten <= 0) - return (nwritten); /* error */ - /* Some systems, such as Digital's OSF1 (Digital Unix) doesn't set - * the returned value to -1, even when interrupted by an alarm, - * whatever says the documentation. errno is not set. */ - if ((nwritten < nleft) && timeout_flag) - return nwritten; - nleft -= nwritten; - ptr += nwritten; - } - return (nbytes - nleft); + nleft = nbytes; + while (nleft > 0) { + nwritten = write(fd, ptr, nleft); + if (nwritten <= 0) + return (nwritten); /* error */ + /* Some systems, such as Digital's OSF1 (Digital Unix) doesn't set the + * returned value to -1, even when interrupted by an alarm, whatever says + * the documentation. errno is not set. */ + if ((nwritten < nleft) && timeout_flag) + return nwritten; + nleft -= nwritten; + ptr += nwritten; + } + return (nbytes - nleft); }