int
is_addr (const char *address)
{
#ifdef USE_IPV6
	if (address_family == AF_INET && is_inet_addr (address))
		return TRUE;
	else if (address_family == AF_INET6 && is_inet6_addr (address))
		return TRUE;
#else
	if (is_inet_addr (address))
		return (TRUE);
#endif

	return (FALSE);
}
int
main (int argc, char **argv)
{
    char *cmd = NULL;
    char *rawcmd = NULL;
    int result = STATE_UNKNOWN;
    int this_result = STATE_UNKNOWN;
    int i;

    setlocale (LC_ALL, "");
    setlocale (LC_NUMERIC, "C");
    bindtextdomain (PACKAGE, LOCALEDIR);
    textdomain (PACKAGE);

    addresses = malloc (sizeof(char*) * max_addr);
    addresses[0] = NULL;

    /* Parse extra opts if any */
    argv=np_extra_opts (&argc, argv, progname);

    if (process_arguments (argc, argv) == ERROR)
        usage4 (_("Could not parse arguments"));

    /* Set signal handling and alarm */
    if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
        usage4 (_("Cannot catch SIGALRM"));
    }

    /* If ./configure finds ping has timeout values, set plugin alarm slightly
     * higher so that we can use response from command line ping */
#if defined(PING_PACKETS_FIRST) && defined(PING_HAS_TIMEOUT)
    alarm (timeout_interval + 1);
#else
    alarm (timeout_interval);
#endif

    for (i = 0 ; i < n_addresses ; i++) {

#ifdef PING6_COMMAND
        if (address_family != AF_INET && is_inet6_addr(addresses[i]))
            rawcmd = strdup(PING6_COMMAND);
        else
            rawcmd = strdup(PING_COMMAND);
#else
        rawcmd = strdup(PING_COMMAND);
#endif

        /* does the host address of number of packets argument come first? */
#ifdef PING_PACKETS_FIRST
# ifdef PING_HAS_TIMEOUT
        xasprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
# else
        xasprintf (&cmd, rawcmd, max_packets, addresses[i]);
# endif
#else
        xasprintf (&cmd, rawcmd, addresses[i], max_packets);
#endif

        if (verbose >= 2)
            printf ("CMD: %s\n", cmd);

        /* run the command */
        this_result = run_ping (cmd, addresses[i]);

        if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) {
            printf ("%s\n", cmd);
            die (STATE_UNKNOWN,
                 _("CRITICAL - Could not interpret output from ping command\n"));
        }

        if (pl >= cpl || rta >= crta || rta < 0)
            this_result = STATE_CRITICAL;
        else if (pl >= wpl || rta >= wrta)
            this_result = STATE_WARNING;
        else if (pl >= 0 && rta >= 0)
            this_result = max_state (STATE_OK, this_result);

        if (n_addresses > 1 && this_result != STATE_UNKNOWN)
            die (STATE_OK, "%s is alive\n", addresses[i]);

        if (display_html == TRUE)
            printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
        if (pl == 100)
            printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text,
                    pl);
        else
            printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"),
                    state_text (this_result), warn_text, pl, rta);
        if (display_html == TRUE)
            printf ("</A>");

        /* Print performance data */
        printf("|%s", fperfdata ("rta", (double) rta, "ms",
                                 wrta>0?TRUE:FALSE, wrta,
                                 crta>0?TRUE:FALSE, crta,
                                 TRUE, 0, FALSE, 0));
        printf(" %s\n", perfdata ("pl", (long) pl, "%",
                                  wpl>0?TRUE:FALSE, wpl,
                                  cpl>0?TRUE:FALSE, cpl,
                                  TRUE, 0, FALSE, 0));

        if (verbose >= 2)
            printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);

        result = max_state (result, this_result);
        free (rawcmd);
        free (cmd);
    }

    return result;
}
int
main (int argc, char **argv)
{
/* normaly should be  int result = STATE_UNKNOWN; */

  int status = STATE_UNKNOWN;
  int result = 0;
  char *fping_prog = NULL;
  char *server = NULL;
  char *command_line = NULL;
  char *input_buffer = NULL;
  char *option_string = "";
  input_buffer = malloc (MAX_INPUT_BUFFER);

  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  /* Parse extra opts if any */
  argv=np_extra_opts (&argc, argv, progname);

  if (process_arguments (argc, argv) == ERROR)
    usage4 (_("Could not parse arguments"));

  server = strscpy (server, server_name);

  /* compose the command */
  if (target_timeout)
    xasprintf(&option_string, "%s-t %d ", option_string, target_timeout);
  if (packet_interval)
    xasprintf(&option_string, "%s-p %d ", option_string, packet_interval);
  if (sourceip)
    xasprintf(&option_string, "%s-S %s ", option_string, sourceip);
  if (sourceif)
    xasprintf(&option_string, "%s-I %s ", option_string, sourceif);

#ifdef PATH_TO_FPING6
  if (address_family != AF_INET && is_inet6_addr(server))
    fping_prog = strdup(PATH_TO_FPING6);
  else
    fping_prog = strdup(PATH_TO_FPING);
#else
  fping_prog = strdup(PATH_TO_FPING);
#endif

  xasprintf (&command_line, "%s %s-b %d -c %d %s", fping_prog,
            option_string, packet_size, packet_count, server);

  if (verbose)
    printf ("%s\n", command_line);

  /* run the command */
  child_process = spopen (command_line);
  if (child_process == NULL) {
    printf (_("Could not open pipe: %s\n"), command_line);
    return STATE_UNKNOWN;
  }

  child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  if (child_stderr == NULL) {
    printf (_("Could not open stderr for %s\n"), command_line);
  }

  while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
    if (verbose)
      printf ("%s", input_buffer);
    status = max_state (status, textscan (input_buffer));
  }

  /* If we get anything on STDERR, at least set warning */
  while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
    status = max_state (status, STATE_WARNING);
    if (verbose)
      printf ("%s", input_buffer);
    status = max_state (status, textscan (input_buffer));
  }
  (void) fclose (child_stderr);

  /* close the pipe */
  if (result = spclose (child_process))
    /* need to use max_state not max */
    status = max_state (status, STATE_WARNING);

  if (result > 1 ) {
    status = max_state (status, STATE_UNKNOWN);
    if (result == 2) {
      die (STATE_UNKNOWN, _("FPING UNKNOWN - IP address not found\n"));
    }
    if (result == 3) {
      die (STATE_UNKNOWN, _("FPING UNKNOWN - invalid commandline argument\n"));
    }
    if (result == 4) {
      die (STATE_UNKNOWN, _("FPING UNKNOWN - failed system call\n"));
    }

  }

  printf ("FPING %s - %s\n", state_text (status), server_name);

  return status;
}