/** Sprintf into a newly allocated buffer * Memory MUST be freed. Dies if memory cannot be allocated. * @param strp Pointer to a pointer that will be set to the newly allocated string * @param fmt Format string like sprintf * @param ... Variable number of arguments for format string * @return int Size of allocated string. */ int safe_asprintf(char **strp, const char *fmt, ...) { va_list ap; int retval; va_start(ap, fmt); retval = safe_vasprintf(strp, fmt, ap); va_end(ap); return (retval); }
/** @internal */ int iptables_do_command(const char *format, ...) { va_list vlist; char *fmt_cmd = NULL, *cmd = NULL; s_config *config; int rc; int i; va_start(vlist, format); safe_vasprintf(&fmt_cmd, format, vlist); va_end(vlist); config = config_get_config(); if (config->ip6) { safe_asprintf(&cmd, "ip6tables %s", fmt_cmd); } else { safe_asprintf(&cmd, "iptables %s", fmt_cmd); } free(fmt_cmd); debug(LOG_DEBUG, "Executing command: %s", cmd); for (i = 0; i < 5; i++) { rc = execute(cmd, fw_quiet); if (rc == 4) { /* iptables error code 4 indicates a resource problem that might * be temporary. So we retry to insert the rule a few times. (Mitar) */ sleep(1); } else { break; } } if (!fw_quiet && rc != 0) { debug(LOG_ERR, "Nonzero exit status %d from command: %s", rc, cmd); } free(cmd); return rc; }
/** @internal * */ static int iptables_do_command(const char *format, ...) { va_list vlist; char *fmt_cmd; char *cmd; int rc; va_start(vlist, format); safe_vasprintf(&fmt_cmd, format, vlist); va_end(vlist); safe_asprintf(&cmd, "iptables %s", fmt_cmd); free(fmt_cmd); iptables_insert_gateway_id(&cmd); debug(LOG_DEBUG, "Executing command: %s", cmd); int exec_count = 0; while(exec_count <= 3){ rc = execute(cmd, fw_quiet); if (rc != 0) { // If quiet, do not display the error //if (fw_quiet == 0) debug(LOG_ERR, "iptables command failed(%d): %s", rc, cmd); //else if (fw_quiet == 1) // debug(LOG_DEBUG, "iptables command failed(%d): %s", rc, cmd); } else break; exec_count++; } free(cmd); return rc; }
/** @internal */ int iptables_do_command(const char *format, ...) { va_list vlist; char *fmt_cmd = NULL; s_config *config; char *iptables; int rc; int i; va_start(vlist, format); safe_vasprintf(&fmt_cmd, format, vlist); va_end(vlist); config = config_get_config(); iptables = config->ip6 ? "ip6tables" : "iptables"; for (i = 0; i < 5; i++) { if (fw_quiet) { rc = execute("%s --wait %s > /dev/null 2>&1", iptables, fmt_cmd); } else { rc = execute("%s --wait %s", iptables, fmt_cmd); } if (rc == 4) { /* iptables error code 4 indicates a resource problem that might * be temporary. So we retry to insert the rule a few times. (Mitar) */ sleep(1); } else { break; } } free(fmt_cmd); return rc; }
/** @internal */ static int tc_do_command(char *format, ...) { va_list vlist; char *fmt_cmd; char *cmd; int rc; va_start(vlist, format); safe_vasprintf(&fmt_cmd, format, vlist); va_end(vlist); safe_asprintf(&cmd, "tc %s", fmt_cmd); free(fmt_cmd); debug(LOG_DEBUG, "Executing command: %s", cmd); rc = execute(cmd, tc_quiet); free(cmd); return rc; }