"Fossies" - the Fresh Open Source Software Archive

Member "HTTPing-2.9/help.c" (29 Oct 2022, 12470 Bytes) of package /linux/www/HTTPing-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "help.c" see the Fossies "Dox" file reference documentation.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 #include <unistd.h>
    5 #include <sys/utsname.h>
    6 #include <libintl.h>
    7 
    8 #include "gen.h"
    9 #include "main.h"
   10 #include "help.h"
   11 #include "utils.h"
   12 
   13 void new_version_alert(void)
   14 {
   15     char new_version = 0;
   16     FILE *fh = fopen(SPAM_FILE, "r");
   17     if (!fh)
   18         new_version = 1;
   19     else
   20     {
   21         char buffer[4096], *dummy = 0x00;
   22 
   23         fgets(buffer, sizeof buffer, fh);
   24 
   25         fclose(fh);
   26 
   27         dummy = strchr(buffer, '\n');
   28         if (dummy)
   29             *dummy = 0x00;
   30 
   31         if (strcmp(buffer, VERSION) != 0)
   32             new_version = 1;
   33     }
   34 
   35     if (new_version)
   36     {
   37         struct utsname buf;
   38         FILE *fh = fopen(SPAM_FILE, "w");
   39         if (fh)
   40         {
   41             fprintf(fh, "%s\n", VERSION);
   42 
   43             fclose(fh);
   44         }
   45 
   46         printf("Welcome to the new HTTPing version " VERSION "!\n\n");
   47 #ifdef NC
   48         printf("Did you know that with -K you can start a fullscreen GUI version with nice graphs and lots more information? And that you can disable the moving graphs with -D?\n");
   49 #ifndef FW
   50         printf("And if you compile this program with libfftw3, that it can also show a fourier transform of the measured values?\n");
   51 #endif
   52 #else
   53         printf("Did you know that if you compile this program with NCURSES, that it then includes a nice GUI with lots more information and graphs?\n");
   54 #endif
   55 
   56 #if !defined(TCP_TFO) && defined(linux)
   57         if (uname(&buf) == 0)
   58         {
   59             char **rparts = NULL;
   60             int n_rparts = 0;
   61 
   62             split_string(buf.release, ".", &rparts, &n_rparts);
   63 
   64             if (n_rparts >= 2 && ((atoi(rparts[0]) >= 3 && atoi(rparts[1]) >= 6) || atoi(rparts[0]) >= 4))
   65                 printf("This program supports TCP Fast Open! (if compiled in and only on Linux kernels 3.6 or more recent) See the readme.txt how to enable this.\n");
   66 
   67             free_splitted_string(rparts, n_rparts);
   68         }
   69 #endif
   70 
   71         printf("\n\n");
   72     }
   73 }
   74 
   75 void version(void)
   76 {
   77     fprintf(stderr, gettext("HTTPing v" VERSION ", (C) 2003-2017 folkert@vanheusden.com\n"));
   78 #ifndef NO_SSL
   79     fprintf(stderr, gettext(" * SSL support included (-l)\n"));
   80 #endif
   81 
   82 #ifdef NC
   83 #ifdef FW
   84     fprintf(stderr, gettext(" * ncurses interface with FFT included (-K)\n"));
   85 #else
   86     fprintf(stderr, gettext(" * ncurses interface included (-K)\n"));
   87 #endif
   88 #endif
   89 
   90 #ifdef TCP_TFO
   91     fprintf(stderr, gettext(" * TFO (TCP fast open) support included (-F)\n"));
   92 #endif
   93     fprintf(stderr, gettext("\n"));
   94 }
   95 
   96 void format_help(const char *short_str, const char *long_str, const char *descr)
   97 {
   98     int par_width = SWITCHES_COLUMN_WIDTH, max_wrap_width = par_width / 2, cur_par_width = 0;
   99     int descr_width = max_x - (par_width + 1);
  100     char *line = NULL, *p = (char *)descr;
  101     char first = 1;
  102 
  103     if (long_str && short_str)
  104         str_add(&line, "%-4s / %s", short_str, long_str);
  105     else if (long_str)
  106         str_add(&line, "%s", long_str);
  107     else
  108         str_add(&line, "%s", short_str);
  109 
  110     cur_par_width = fprintf(stderr, "%-*s ", par_width, line);
  111 
  112     free(line);
  113 
  114     if (par_width + 1 >= max_x || cur_par_width >= max_x)
  115     {
  116         fprintf(stderr, "%s\n", descr);
  117         return;
  118     }
  119 
  120     for(;strlen(p);)
  121     {
  122         char *n =  NULL, *kn = NULL, *copy = NULL;
  123         int n_len = 0, len_after_ww = 0, len_before_ww = 0;
  124         int str_len = 0, cur_descr_width = first ? max_x - cur_par_width : descr_width;
  125 
  126         while(*p == ' ')
  127             p++;
  128 
  129         str_len = strlen(p);
  130         if (!str_len)
  131             break;
  132 
  133         len_before_ww = min(str_len, cur_descr_width);
  134 
  135         n = &p[len_before_ww];
  136         kn = n;
  137 
  138         if (str_len > cur_descr_width)
  139         { 
  140             while (*n != ' ' && n_len < max_wrap_width)
  141             {
  142                 n--;
  143                 n_len++;
  144             }
  145 
  146             if (n_len >= max_wrap_width)
  147                 n = kn;
  148         }
  149 
  150         len_after_ww = (int)(n - p);
  151         if (len_after_ww <= 0)
  152             break;
  153 
  154         copy = (char *)malloc(len_after_ww + 1);
  155         memcpy(copy, p, len_after_ww);
  156         copy[len_after_ww] = 0x00;
  157 
  158         if (first)
  159             first = 0;
  160         else
  161             fprintf(stderr, "%*s ", par_width, "");
  162 
  163         fprintf(stderr, "%s\n", copy);
  164 
  165         free(copy);
  166 
  167         p = n;
  168     }
  169 }
  170 
  171 void usage(const char *me)
  172 {
  173     char *dummy = NULL, has_color = 0;
  174     char host[256] = { 0 };
  175 
  176     /* where to connect to */
  177     fprintf(stderr, gettext(" *** where to connect to ***\n"));
  178     format_help("-g x", "--url", gettext("URL to ping (e.g. -g http://localhost/)"));
  179     format_help("-h x", "--hostname", gettext("hostname to ping (e.g. localhost) - use either -g or -h"));
  180     format_help("-p x", "--port", gettext("portnumber (e.g. 80) - use with -h"));
  181     format_help("-6", "--ipv6", gettext("use IPv6 when resolving/connecting"));
  182 #ifndef NO_SSL
  183     format_help("-l", "--use-ssl", gettext("connect using SSL. pinging an https URL automatically enables this setting"));
  184 #endif
  185     fprintf(stderr, gettext("\n"));
  186 
  187     /* proxy settings */
  188     fprintf(stderr, gettext(" *** proxy settings ***\n"));
  189     format_help("-x x", "--proxy", gettext("x should be \"host:port\" which are the network settings of the http/https proxy server. ipv6 ip-address should be \"[ip:address]:port\""));
  190     format_help("-E", NULL, gettext("fetch proxy settings from environment variables"));
  191     format_help(NULL, "--proxy-user x", gettext("username for authentication against proxy"));
  192     format_help(NULL, "--proxy-password x", gettext("password for authentication against proxy"));
  193     format_help(NULL, "--proxy-password-file x", gettext("read password for proxy authentication from file x"));
  194     format_help("-5", NULL, gettext("proxy is a socks5 server"));
  195     format_help(NULL, "--proxy-buster x", gettext("adds \"&x=[random value]\" to the request URL"));
  196     fprintf(stderr, gettext("\n"));
  197 
  198     /* timing settings */
  199     fprintf(stderr, gettext(" *** timing settings ***\n"));
  200     format_help("-c x", "--count", gettext("how many times to ping"));
  201     format_help("-i x", "--interval", gettext("delay between each ping"));
  202     format_help("-t x", "--timeout", gettext("timeout (default: 30s)"));
  203     format_help(NULL, "--ai / --adaptive-interval", gettext("execute pings at multiples of interval relative to start, automatically enabled in ncurses output mode"));
  204     format_help("-f", "--flood", gettext("flood connect (no delays)"));
  205     fprintf(stderr, gettext("\n"));
  206 
  207     /* http settings */
  208     fprintf(stderr, gettext(" *** HTTP settings ***\n"));
  209     format_help("-Z", "--no-cache", gettext("ask any proxies on the way not to cache the requests"));
  210     format_help(NULL, "--divert-connect", gettext("connect to a different host than in the URL given"));
  211     format_help(NULL, "--keep-cookies", gettext("return the cookies given by the HTTP server in the following request(s)"));
  212     format_help(NULL, "--no-host-header", gettext("do not add \"Host:\"-line to the request headers"));
  213     format_help("-Q", "--persistent-connections", gettext("use a persistent connection, i.e. reuse the same TCP connection for multiple HTTP requests. usually possible when 'Connection: Keep-Alive' is sent by server. adds a 'C' to the output if httping had to reconnect"));
  214     format_help("-I x", "--user-agent", gettext("use 'x' for the UserAgent header"));
  215     format_help("-R x", "--referer", gettext("use 'x' for the Referer header"));
  216     format_help(NULL, "--header", gettext("adds an extra request-header"));
  217     fprintf(stderr, gettext("\n"));
  218 
  219     /* network settings */
  220     fprintf(stderr, gettext(" *** networking settings ***\n"));
  221     format_help(NULL, "--max-mtu", gettext("limit the MTU size"));
  222     format_help(NULL, "--no-tcp-nodelay", gettext("do not disable Naggle"));
  223     format_help(NULL, "--recv-buffer", gettext("receive buffer size"));
  224     format_help(NULL, "--tx-buffer", gettext("transmit buffer size"));
  225     format_help("-r", "--resolve-once", gettext("resolve hostname only once (useful when pinging roundrobin DNS: also takes the first DNS lookup out of the loop so that the first measurement is also correct)"));
  226     format_help("-W", NULL, gettext("do not abort the program if resolving failed: keep retrying"));
  227     format_help("-y x", "--bind-to", gettext("bind to an ip-address (and thus interface) with an optional port"));
  228 #ifdef TCP_TFO
  229     format_help("-F", "--tcp-fast-open", gettext("\"TCP fast open\" (TFO), reduces the latency of TCP connects"));
  230 #endif
  231 #ifdef linux
  232     format_help(NULL, "--priority", gettext("set priority of packets"));
  233 #endif
  234     format_help(NULL, "--tos", gettext("set TOS (type of service)"));
  235     fprintf(stderr, gettext("\n"));
  236 
  237     /* http authentication */
  238     fprintf(stderr, gettext(" *** HTTP authentication ***\n"));
  239     format_help("-A", "--basic-auth", gettext("activate (\"basic\") authentication"));
  240     format_help("-U x", "--username", gettext("username for authentication"));
  241     format_help("-P x", "--password", gettext("password for authentication"));
  242     format_help("-T x", NULL, gettext("read the password fom the file 'x' (replacement for -P)"));
  243     fprintf(stderr, gettext("\n"));
  244 
  245     /* output settings */
  246     fprintf(stderr, gettext(" *** output settings ***\n"));
  247     format_help("-s", "--show-statuscodes", gettext("show statuscodes"));
  248     format_help("-S", "--split-time", gettext("split measured time in its individual components (resolve, connect, send, receive, disconnect)"));
  249     format_help(NULL, "--threshold-red", gettext("from what ping value to show the value in red (must be bigger than yellow), only in color mode (-Y)"));
  250     format_help(NULL, "--threshold-yellow", gettext("from what ping value to show the value in yellow"));
  251     format_help(NULL, "--threshold-show", gettext("from what ping value to show the results"));
  252     format_help(NULL, "--timestamp / --ts", gettext("put a timestamp before the measured values, use -v to include the date and -vv to show in microseconds"));
  253     format_help(NULL, "--aggregate x[,y[,z]]", gettext("show an aggregate each x[/y[/z[/etc]]] seconds"));
  254 #ifndef NO_SSL
  255     format_help("-z", "--show-fingerprint", gettext("show fingerprint (SSL)"));
  256     format_help(NULL, "--ca-path", gettext("path to ca certificates (SSL)"));
  257 #endif
  258     format_help("-v", NULL, gettext("verbose mode"));
  259     fprintf(stderr, gettext("\n"));
  260 
  261     /* GET settings */
  262     fprintf(stderr, gettext(" *** \"GET\" (instead of HTTP \"HEAD\") settings ***\n"));
  263     format_help("-G", "--get-request", gettext("do a GET request instead of HEAD (read the contents of the page as well)"));
  264     format_help("-b", "--show-transfer-speed", gettext("show transfer speed in KB/s (use with -G)"));
  265     format_help("-B", "--show-xfer-speed-compressed", gettext("like -b but use compression if available"));
  266     format_help("-L x", "--data-limit", gettext("limit the amount of data transferred (for -b) to 'x' (in bytes)"));
  267     format_help("-X", "--show-kb", gettext("show the number of KB transferred (for -b)"));
  268     fprintf(stderr, gettext("\n"));
  269 
  270     /* output mode settings */
  271     fprintf(stderr, gettext(" *** output mode settings ***\n"));
  272     format_help("-q", "--quiet", gettext("quiet, only returncode"));
  273     format_help("-m", "--parseable-output", gettext("give machine parseable output (see also -o and -e)"));
  274     format_help("-M", NULL, gettext("json output, cannot be combined with -m"));
  275     format_help("-o rc,rc,...", "--ok-result-codes", gettext("what http results codes indicate 'ok' comma separated WITHOUT spaces inbetween default is 200, use with -e"));
  276     format_help("-e x", "--result-string", gettext("string to display when http result code doesn't match"));
  277     format_help("-n warn,crit", "--nagios-mode-1 / --nagios-mode-2", gettext("Nagios-mode: return 1 when avg. response time >= warn, 2 if >= crit, otherwhise return 0"));
  278     format_help("-N x", NULL, gettext("Nagios mode 2: return 0 when all fine, 'x' when anything failes"));
  279     format_help("-C cookie=value", "--cookie", gettext("add a cookie to the request"));
  280     format_help("-Y", "--colors", gettext("add colors"));
  281     format_help("-a", "--audible-ping", gettext("audible ping"));
  282     fprintf(stderr, gettext("\n"));
  283 
  284     /* GUI/ncurses mode */
  285 #if defined(NC)
  286     fprintf(stderr, gettext(" *** GUI/ncurses mode settings ***\n"));
  287     format_help("-K", "--ncurses / --gui", gettext("ncurses/GUI mode"));
  288 #if defined(FW)
  289     format_help(NULL, "--draw-phase", gettext("draw phase (fourier transform) in gui"));
  290 #endif
  291     format_help(NULL, "--slow-log", gettext("when the duration is x or more, show ping line in the slow log window (the middle window)"));
  292     format_help(NULL, "--graph-limit x", gettext("do not scale to values above x"));
  293     format_help("-D", "--no-graph", gettext("do not show graphs (in ncurses/GUI mode)"));
  294     fprintf(stderr, gettext("\n"));
  295 #endif
  296 
  297     format_help("-V", "--version", gettext("show the version"));
  298     fprintf(stderr, gettext("\n"));
  299 
  300     dummy = getenv("TERM");
  301     if (dummy)
  302     {
  303         if (strstr(dummy, "ANSI") || strstr(dummy, "xterm") || strstr(dummy, "screen"))
  304             has_color = 1;
  305     }
  306 
  307     if (gethostname(host, sizeof host))
  308         strcpy(host, "localhost");
  309 
  310     fprintf(stderr, gettext("Example:\n"));
  311     fprintf(stderr, "\t%s %s%s -s -Z\n\n", me, host, has_color ? " -Y" : "");
  312 
  313     new_version_alert();
  314 }