text2pcap: Use common failure messages, exit codes

Use the common exit codes and failure messages.
This commit is contained in:
John Thacker 2021-12-24 08:31:58 -05:00
parent 96c299c6b8
commit 0d68a1364f
1 changed files with 73 additions and 73 deletions

View File

@ -72,6 +72,7 @@
#include <wsutil/file_util.h> #include <wsutil/file_util.h>
#include <cli_main.h> #include <cli_main.h>
#include <ui/cmdarg_err.h> #include <ui/cmdarg_err.h>
#include <ui/exit_codes.h>
#include <ui/text_import.h> #include <ui/text_import.h>
#include <ui/version_info.h> #include <ui/version_info.h>
#include <ui/failure_message.h> #include <ui/failure_message.h>
@ -338,6 +339,7 @@ print_usage (FILE *output)
static int static int
parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump_params * const params) parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump_params * const params)
{ {
int ret;
int c; int c;
char *p; char *p;
static const struct ws_option long_options[] = { static const struct ws_option long_options[] = {
@ -372,9 +374,9 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
case 'N': interface_name = ws_optarg; break; case 'N': interface_name = ws_optarg; break;
case 'o': case 'o':
if (ws_optarg[0] != 'h' && ws_optarg[0] != 'o' && ws_optarg[0] != 'd') { if (ws_optarg[0] != 'h' && ws_optarg[0] != 'o' && ws_optarg[0] != 'd') {
fprintf(stderr, "Bad argument for '-o': %s\n", ws_optarg); cmdarg_err("Bad argument for '-o': %s", ws_optarg);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
switch (ws_optarg[0]) { switch (ws_optarg[0]) {
case 'o': offset_base = 8; break; case 'o': offset_base = 8; break;
@ -385,9 +387,9 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
case 'e': case 'e':
hdr_ethernet = TRUE; hdr_ethernet = TRUE;
if (sscanf(ws_optarg, "%x", &hdr_ethernet_proto) < 1) { if (sscanf(ws_optarg, "%x", &hdr_ethernet_proto) < 1) {
fprintf(stderr, "Bad argument for '-e': %s\n", ws_optarg); cmdarg_err("Bad argument for '-e': %s", ws_optarg);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
break; break;
@ -395,9 +397,9 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
hdr_ip_proto = strtol(ws_optarg, &p, 10); hdr_ip_proto = strtol(ws_optarg, &p, 10);
if (p == ws_optarg || *p != '\0' || hdr_ip_proto < 0 || if (p == ws_optarg || *p != '\0' || hdr_ip_proto < 0 ||
hdr_ip_proto > 255) { hdr_ip_proto > 255) {
fprintf(stderr, "Bad argument for '-i': %s\n", ws_optarg); cmdarg_err("Bad argument for '-i': %s", ws_optarg);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
hdr_ethernet = TRUE; hdr_ethernet = TRUE;
break; break;
@ -409,35 +411,35 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
hdr_udp = FALSE; hdr_udp = FALSE;
hdr_sctp_src = (guint32)strtol(ws_optarg, &p, 10); hdr_sctp_src = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || (*p != ',' && *p != '\0')) { if (p == ws_optarg || (*p != ',' && *p != '\0')) {
fprintf(stderr, "Bad src port for '-%c'\n", c); cmdarg_err("Bad src port for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (*p == '\0') { if (*p == '\0') {
fprintf(stderr, "No dest port specified for '-%c'\n", c); cmdarg_err("No dest port specified for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
p++; p++;
ws_optarg = p; ws_optarg = p;
hdr_sctp_dest = (guint32)strtol(ws_optarg, &p, 10); hdr_sctp_dest = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || (*p != ',' && *p != '\0')) { if (p == ws_optarg || (*p != ',' && *p != '\0')) {
fprintf(stderr, "Bad dest port for '-s'\n"); cmdarg_err("Bad dest port for '-s'");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (*p == '\0') { if (*p == '\0') {
fprintf(stderr, "No tag specified for '-%c'\n", c); cmdarg_err("No tag specified for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
p++; p++;
ws_optarg = p; ws_optarg = p;
hdr_sctp_tag = (guint32)strtol(ws_optarg, &p, 10); hdr_sctp_tag = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || *p != '\0') { if (p == ws_optarg || *p != '\0') {
fprintf(stderr, "Bad tag for '-%c'\n", c); cmdarg_err("Bad tag for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
hdr_ip_proto = 132; hdr_ip_proto = 132;
@ -450,35 +452,35 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
hdr_udp = FALSE; hdr_udp = FALSE;
hdr_sctp_src = (guint32)strtol(ws_optarg, &p, 10); hdr_sctp_src = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || (*p != ',' && *p != '\0')) { if (p == ws_optarg || (*p != ',' && *p != '\0')) {
fprintf(stderr, "Bad src port for '-%c'\n", c); cmdarg_err("Bad src port for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (*p == '\0') { if (*p == '\0') {
fprintf(stderr, "No dest port specified for '-%c'\n", c); cmdarg_err("No dest port specified for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
p++; p++;
ws_optarg = p; ws_optarg = p;
hdr_sctp_dest = (guint32)strtol(ws_optarg, &p, 10); hdr_sctp_dest = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || (*p != ',' && *p != '\0')) { if (p == ws_optarg || (*p != ',' && *p != '\0')) {
fprintf(stderr, "Bad dest port for '-s'\n"); cmdarg_err("Bad dest port for '-s'");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (*p == '\0') { if (*p == '\0') {
fprintf(stderr, "No ppi specified for '-%c'\n", c); cmdarg_err("No ppi specified for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
p++; p++;
ws_optarg = p; ws_optarg = p;
hdr_data_chunk_ppid = (guint32)strtoul(ws_optarg, &p, 10); hdr_data_chunk_ppid = (guint32)strtoul(ws_optarg, &p, 10);
if (p == ws_optarg || *p != '\0') { if (p == ws_optarg || *p != '\0') {
fprintf(stderr, "Bad ppi for '-%c'\n", c); cmdarg_err("Bad ppi for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
hdr_ip_proto = 132; hdr_ip_proto = 132;
@ -498,22 +500,22 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
hdr_data_chunk = FALSE; hdr_data_chunk = FALSE;
hdr_src_port = (guint32)strtol(ws_optarg, &p, 10); hdr_src_port = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || (*p != ',' && *p != '\0')) { if (p == ws_optarg || (*p != ',' && *p != '\0')) {
fprintf(stderr, "Bad src port for '-u'\n"); cmdarg_err("Bad src port for '-u'");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (*p == '\0') { if (*p == '\0') {
fprintf(stderr, "No dest port specified for '-u'\n"); cmdarg_err("No dest port specified for '-u'");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
p++; p++;
ws_optarg = p; ws_optarg = p;
hdr_dest_port = (guint32)strtol(ws_optarg, &p, 10); hdr_dest_port = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || *p != '\0') { if (p == ws_optarg || *p != '\0') {
fprintf(stderr, "Bad dest port for '-u'\n"); cmdarg_err("Bad dest port for '-u'");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
hdr_ip_proto = 17; hdr_ip_proto = 17;
hdr_ethernet = TRUE; hdr_ethernet = TRUE;
@ -526,22 +528,22 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
hdr_data_chunk = FALSE; hdr_data_chunk = FALSE;
hdr_src_port = (guint32)strtol(ws_optarg, &p, 10); hdr_src_port = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || (*p != ',' && *p != '\0')) { if (p == ws_optarg || (*p != ',' && *p != '\0')) {
fprintf(stderr, "Bad src port for '-T'\n"); cmdarg_err("Bad src port for '-T'");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (*p == '\0') { if (*p == '\0') {
fprintf(stderr, "No dest port specified for '-u'\n"); cmdarg_err("No dest port specified for '-u'");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
p++; p++;
ws_optarg = p; ws_optarg = p;
hdr_dest_port = (guint32)strtol(ws_optarg, &p, 10); hdr_dest_port = (guint32)strtol(ws_optarg, &p, 10);
if (p == ws_optarg || *p != '\0') { if (p == ws_optarg || *p != '\0') {
fprintf(stderr, "Bad dest port for '-T'\n"); cmdarg_err("Bad dest port for '-T'");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
hdr_ip_proto = 6; hdr_ip_proto = 6;
hdr_ethernet = TRUE; hdr_ethernet = TRUE;
@ -561,9 +563,9 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
p = strchr(ws_optarg, ','); p = strchr(ws_optarg, ',');
if (!p) { if (!p) {
fprintf(stderr, "Bad source param addr for '-%c'\n", c); cmdarg_err("Bad source param addr for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
*p = '\0'; *p = '\0';
@ -581,36 +583,36 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
if (hdr_ipv6 == TRUE) { if (hdr_ipv6 == TRUE) {
if (!ws_inet_pton6(ws_optarg, &hdr_ipv6_src_addr)) { if (!ws_inet_pton6(ws_optarg, &hdr_ipv6_src_addr)) {
fprintf(stderr, "Bad src addr -%c '%s'\n", c, p); cmdarg_err("Bad src addr -%c '%s'", c, p);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
} else { } else {
if (!ws_inet_pton4(ws_optarg, &hdr_ip_src_addr)) { if (!ws_inet_pton4(ws_optarg, &hdr_ip_src_addr)) {
fprintf(stderr, "Bad src addr -%c '%s'\n", c, p); cmdarg_err("Bad src addr -%c '%s'", c, p);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
} }
p++; p++;
if (*p == '\0') { if (*p == '\0') {
fprintf(stderr, "No dest addr specified for '-%c'\n", c); cmdarg_err("No dest addr specified for '-%c'", c);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (hdr_ipv6 == TRUE) { if (hdr_ipv6 == TRUE) {
if (!ws_inet_pton6(p, &hdr_ipv6_dest_addr)) { if (!ws_inet_pton6(p, &hdr_ipv6_dest_addr)) {
fprintf(stderr, "Bad dest addr for -%c '%s'\n", c, p); cmdarg_err("Bad dest addr for -%c '%s'", c, p);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
} else { } else {
if (!ws_inet_pton4(p, &hdr_ip_dest_addr)) { if (!ws_inet_pton4(p, &hdr_ip_dest_addr)) {
fprintf(stderr, "Bad dest addr for -%c '%s'\n", c, p); cmdarg_err("Bad dest addr for -%c '%s'", c, p);
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
} }
break; break;
@ -619,26 +621,26 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
case '?': case '?':
default: default:
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
} }
if (ws_optind >= argc || argc-ws_optind < 2) { if (ws_optind >= argc || argc-ws_optind < 2) {
fprintf(stderr, "Must specify input and output filename\n"); cmdarg_err("Must specify input and output filename");
print_usage(stderr); print_usage(stderr);
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (max_offset > WTAP_MAX_PACKET_SIZE_STANDARD) { if (max_offset > WTAP_MAX_PACKET_SIZE_STANDARD) {
fprintf(stderr, "Maximum packet length cannot be more than %d bytes\n", cmdarg_err("Maximum packet length cannot be more than %d bytes",
WTAP_MAX_PACKET_SIZE_STANDARD); WTAP_MAX_PACKET_SIZE_STANDARD);
return EXIT_FAILURE; return INVALID_OPTION;
} }
/* Some validation */ /* Some validation */
if (pcap_link_type != 1 && hdr_ethernet) { if (pcap_link_type != 1 && hdr_ethernet) {
fprintf(stderr, "Dummy headers (-e, -i, -u, -s, -S -T) cannot be specified with link type override (-l)\n"); cmdarg_err("Dummy headers (-e, -i, -u, -s, -S -T) cannot be specified with link type override (-l)");
return EXIT_FAILURE; return INVALID_OPTION;
} }
if (hdr_ip_proto != -1 && !(hdr_ip || hdr_ipv6)) { if (hdr_ip_proto != -1 && !(hdr_ip || hdr_ipv6)) {
@ -648,8 +650,8 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
if (hdr_ip_proto == -1 && (hdr_ip || hdr_ipv6)) { if (hdr_ip_proto == -1 && (hdr_ip || hdr_ipv6)) {
/* if -4 or -6 option is specified without an IP protocol then fail */ /* if -4 or -6 option is specified without an IP protocol then fail */
fprintf(stderr, "IP protocol requires a next layer protocol number\n"); cmdarg_err("IP protocol requires a next layer protocol number");
return EXIT_FAILURE; return INVALID_OPTION;
} }
if ((hdr_tcp || hdr_udp || hdr_sctp) && !(hdr_ip || hdr_ipv6)) { if ((hdr_tcp || hdr_udp || hdr_sctp) && !(hdr_ip || hdr_ipv6)) {
@ -672,9 +674,8 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
input_filename = argv[ws_optind]; input_filename = argv[ws_optind];
input_file = ws_fopen(input_filename, "rb"); input_file = ws_fopen(input_filename, "rb");
if (!input_file) { if (!input_file) {
fprintf(stderr, "Cannot open file [%s] for reading: %s\n", open_failure_message(input_filename, errno, FALSE);
input_filename, g_strerror(errno)); return OPEN_ERROR;
return EXIT_FAILURE;
} }
} else { } else {
input_filename = "Standard input"; input_filename = "Standard input";
@ -693,8 +694,10 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
params->tsprec = WTAP_TSPREC_USEC; params->tsprec = WTAP_TSPREC_USEC;
file_type_subtype = wtap_pcap_file_type_subtype(); file_type_subtype = wtap_pcap_file_type_subtype();
} }
if (write_file_header(params, file_type_subtype, interface_name) != EXIT_SUCCESS) { if ((ret = write_file_header(params, file_type_subtype, interface_name)) != EXIT_SUCCESS) {
return EXIT_FAILURE; g_free(params->idb_inf);
wtap_dump_params_cleanup(params);
return ret;
} }
if (strcmp(argv[ws_optind+1], "-") != 0) { if (strcmp(argv[ws_optind+1], "-") != 0) {
@ -712,7 +715,7 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump
file_type_subtype); file_type_subtype);
g_free(params->idb_inf); g_free(params->idb_inf);
wtap_dump_params_cleanup(params); wtap_dump_params_cleanup(params);
return EXIT_FAILURE; return OPEN_ERROR;
} }
info->mode = TEXT_IMPORT_HEXDUMP; info->mode = TEXT_IMPORT_HEXDUMP;
@ -848,7 +851,7 @@ main(int argc, char *argv[])
ws_log_init("text2pcap", vcmdarg_err); ws_log_init("text2pcap", vcmdarg_err);
/* Early logging command-line initialization. */ /* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1); ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
#ifdef _WIN32 #ifdef _WIN32
create_app_running_mutex(); create_app_running_mutex();
@ -859,17 +862,14 @@ main(int argc, char *argv[])
wtap_init(TRUE); wtap_init(TRUE);
memset(&info, 0, sizeof(info)); memset(&info, 0, sizeof(info));
if (parse_options(argc, argv, &info, &params) != EXIT_SUCCESS) { if ((ret = parse_options(argc, argv, &info, &params)) != EXIT_SUCCESS) {
ret = EXIT_FAILURE;
goto clean_exit; goto clean_exit;
} }
assert(input_file != NULL); assert(input_file != NULL);
assert(wdh != NULL); assert(wdh != NULL);
if (text_import(&info) != EXIT_SUCCESS) { ret = text_import(&info);
ret = EXIT_FAILURE;
}
if (debug) if (debug)
fprintf(stderr, "\n-------------------------\n"); fprintf(stderr, "\n-------------------------\n");
@ -889,7 +889,7 @@ clean_exit:
char *err_info; char *err_info;
if (!wtap_dump_close(wdh, &err, &err_info)) { if (!wtap_dump_close(wdh, &err, &err_info)) {
cfile_close_failure_message(output_filename, err, err_info); cfile_close_failure_message(output_filename, err, err_info);
ret = EXIT_FAILURE; ret = 2;
} }
g_free(params.idb_inf); g_free(params.idb_inf);
} }