From 62d3addb9285573cfe684c9dd51a68ea2d74e22d Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Thu, 29 Jun 2023 21:47:38 -0500 Subject: [PATCH] Progress: Fix dependencies and start adding network processing code. Remove not needed dependencies and add needed dependencies. This now processes socket files and network addresses for use by the program. --- data/build/tacocat/dependencies | 3 +- data/build/tacocat/settings | 2 +- sources/c/tacocat/main/common.c | 183 +++++++++++++++++++++++++++++++--- sources/c/tacocat/main/common/print.c | 3 + sources/c/tacocat/main/common/print.h | 3 + sources/c/tacocat/main/tacocat.h | 11 +- 6 files changed, 177 insertions(+), 28 deletions(-) diff --git a/data/build/tacocat/dependencies b/data/build/tacocat/dependencies index 6a0ace9..7eba949 100644 --- a/data/build/tacocat/dependencies +++ b/data/build/tacocat/dependencies @@ -8,14 +8,13 @@ f_string f_utf f_color f_console -f_conversion +f_network f_path f_pipe f_print f_signal f_thread -fl_conversion fl_print fll_error diff --git a/data/build/tacocat/settings b/data/build/tacocat/settings index 6ab16e5..b9cb1d3 100644 --- a/data/build/tacocat/settings +++ b/data/build/tacocat/settings @@ -32,7 +32,7 @@ build_indexer_arguments rcs build_language c build_libraries -lc -build_libraries-individual -lfll_error -lfll_print -lfll_program -lfl_conversion -lfl_print -lf_color -lf_console -lf_conversion -lf_memory -lf_path -lf_pipe -lf_print -lf_signal -lf_string -lf_type_array -lf_utf +build_libraries-individual -lfll_error -lfll_print -lfll_program -lfl_print -lf_color -lf_console -lf_memory -lf_network -lf_path -lf_pipe -lf_print -lf_signal -lf_string -lf_type_array -lf_utf build_libraries-individual_thread -lf_thread build_libraries-level -lfll_2 -lfll_1 -lfll_0 build_libraries-monolithic -lfll diff --git a/sources/c/tacocat/main/common.c b/sources/c/tacocat/main/common.c index 1753197..5681d33 100644 --- a/sources/c/tacocat/main/common.c +++ b/sources/c/tacocat/main/common.c @@ -174,7 +174,8 @@ extern "C" { } // for } - { + // Only process these when needed to avoid unnecessary operations. + if (!(main->setting.flag & (kt_tacocat_main_flag_copyright_e | kt_tacocat_main_flag_version_e |kt_tacocat_main_flag_help_e))) { const uint8_t parameters[] = { kt_tacocat_parameter_from_e, kt_tacocat_parameter_to_e, @@ -190,12 +191,17 @@ extern "C" { &main->setting.tos, }; - const bool const exists[] = { + const bool const must_exists[] = { F_true, F_false, }; f_number_unsigned_t j = 0; + f_number_unsigned_t k = 0; + f_number_unsigned_t length = 0; + f_status_t failed = F_none; + struct hostent host; + f_network_family_ip_t family = f_network_family_ip_t_initialize; for (i = 0; i < 2; ++i) { @@ -209,28 +215,55 @@ extern "C" { kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_string_dynamics_increase_by)); - return; + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; } for (j = 0; j < main->program.parameters.array[parameters[i]].values.used; ++j) { index = main->program.parameters.array[parameters[i]].values.array[j]; + strings[i]->array[j].used = 0; if (main->program.parameters.arguments.array[index].used) { - strings[i]->array[j].used = 0; + if (f_path_is_absolute(main->program.parameters.arguments.array[index]) == F_true || f_path_is_relative_current(main->program.parameters.arguments.array[index]) == F_true) { - main->setting.state.status = f_string_dynamic_append_nulless(main->program.parameters.arguments.array[index], &strings[i]->array[j]); + // Two is added to support a trailing NULL and a type character. + main->setting.state.status = f_string_dynamic_increase_by(main->program.parameters.arguments.array[index].used + 2, &strings[i]->array[j]); - if (F_status_is_error(main->setting.state.status)) { - macro_setting_load_print_first(); + if (F_status_is_error(main->setting.state.status)) { + macro_setting_load_print_first(); - kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_string_dynamic_append_nulless)); + kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_string_dynamic_increase_by)); - return; - } + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; + } + + main->setting.state.status = f_string_dynamic_append_nulless(main->program.parameters.arguments.array[index], &strings[i]->array[j]); - if (f_path_is_absolute(strings[i]->array[j]) == F_true || f_path_is_relative_current(strings[i]->array[j]) == F_true) { - if (exists[i]) { + if (F_status_is_error(main->setting.state.status)) { + macro_setting_load_print_first(); + + kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_string_dynamic_append_nulless)); + + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; + } + + // Designate this as a socket file by appending after the terminating NULL, past the used length. + strings[i]->array[j].string[strings[i]->array[j].used] = 0; + strings[i]->array[j].string[strings[i]->array[j].used + 1] = f_string_ascii_slash_forward_s.string[0]; + + if (must_exists[i]) { main->setting.state.status = f_file_exists(strings[i]->array[j], F_true); if (F_status_is_error(main->setting.state.status)) { @@ -238,12 +271,118 @@ extern "C" { kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_string_dynamic_append_nulless)); - return; + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; } } } else { - // @todo is network address, do validation. + memset(&host, 0, sizeof(struct hostent)); + + main->setting.state.status = f_network_from_ip_name(main->program.parameters.arguments.array[index], &host); + + if (F_status_is_error(main->setting.state.status)) { + macro_setting_load_print_first(); + + // @todo provide network-specific error messages. + kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_network_from_ip_name)); + + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; + } + + if (main->setting.state.status == F_data_not || !host.h_addr_list || !host.h_addr_list[0]) { + macro_setting_load_print_first(); + + // @todo provide network-specific error messages for when no hosts are returned. + kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_network_from_ip_name)); + + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; + } + + // Two is added to support a trailing NULL and a type character. + main->setting.state.status = f_string_dynamic_increase_by(INET6_ADDRSTRLEN + 2, &strings[i]->array[j]); + + if (F_status_is_error(main->setting.state.status)) { + macro_setting_load_print_first(); + + kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_string_dynamic_increase_by)); + + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; + } + + // Randomly select one of the addresses when there are more than one. + if (host.h_addr_list[1]) { + k = 2; + + while (host.h_addr_list[k++]); + + // Real randomness or security is not needed here, so fiddle with predictable but somewhat dynamic numbers. + srand(main->program.pid + j + host.h_addr_list[0][0]); + k = rand() % (k - 1); + } + else { + k = 0; + } + + // Two is added to support a trailing NULL and a type character. + main->setting.state.status = f_string_dynamic_increase_by(INET6_ADDRSTRLEN + 2, &strings[i]->array[j]); + + if (F_status_is_error(main->setting.state.status)) { + macro_setting_load_print_first(); + + kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_string_dynamic_increase_by)); + + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; + } + + if (host.h_addrtype == AF_INET) { + family.type = f_network_family_ip_4_e; + family.address.v4 = *((struct in_addr *) host.h_addr_list[k]); + } + else { + family.type = f_network_family_ip_6_e; + family.address.v6 = *((struct in6_addr *) host.h_addr_list[k]); + } + + main->setting.state.status = f_network_to_ip_string(family, &strings[i]->array[j]); + + if (main->setting.state.status == F_data_not || !host.h_addr_list || !host.h_addr_list[0]) { + macro_setting_load_print_first(); + + // @todo provide network-specific error messages for when no hosts are returned. + kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(f_network_from_ip_name)); + + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; + } + + // the terminating NULL, past the used length. + strings[i]->array[j].string[strings[i]->array[j].used] = 0; + strings[i]->array[j].string[strings[i]->array[j].used + 1] = (family.type & f_network_family_ip_4_e) + ? f_string_ascii_4_s.string[0] + : f_string_ascii_6_s.string[0]; } } else { @@ -253,7 +392,11 @@ extern "C" { fll_program_print_error_parameter_empty_value(&main->program.error, f_console_symbol_long_normal_s, longs[i]); - return; + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; } } // for } @@ -264,9 +407,17 @@ extern "C" { fll_program_print_error_parameter_missing_value(&main->program.error, f_console_symbol_long_normal_s, longs[i]); - return; + if (F_status_is_error_not(failed)) { + failed = main->setting.state.status; + } + + continue; } } // for + + if (F_status_is_error(failed)) { + main->setting.state.status = failed; + } } } #endif // _di_kt_tacocat_setting_load_ diff --git a/sources/c/tacocat/main/common/print.c b/sources/c/tacocat/main/common/print.c index 40fdc1e..33cfd6b 100644 --- a/sources/c/tacocat/main/common/print.c +++ b/sources/c/tacocat/main/common/print.c @@ -7,7 +7,10 @@ extern "C" { #ifndef _di_kt_tacocat_f_a_ const f_string_t kt_tacocat_f_a[] = { "f_console_parameter_process", + "f_network_from_ip_name", + "f_string_append_nulless", "f_string_dynamic_append_nulless", + "f_string_dynamic_increase_by", "f_string_dynamics_increase_by", "f_thread_create", "fll_program_parameter_process_context", diff --git a/sources/c/tacocat/main/common/print.h b/sources/c/tacocat/main/common/print.h index 40507b7..8883098 100644 --- a/sources/c/tacocat/main/common/print.h +++ b/sources/c/tacocat/main/common/print.h @@ -40,7 +40,10 @@ extern "C" { #ifndef _di_kt_tacocat_f_e_ enum { kt_tacocat_f_f_console_parameter_process_e, + kt_tacocat_f_f_network_from_ip_name_e, + kt_tacocat_f_f_string_append_nulless_e, kt_tacocat_f_f_string_dynamic_append_nulless_e, + kt_tacocat_f_f_string_dynamic_increase_by_e, kt_tacocat_f_f_string_dynamics_increase_by_e, kt_tacocat_f_f_thread_create_e, kt_tacocat_f_fll_program_parameter_process_context_e, diff --git a/sources/c/tacocat/main/tacocat.h b/sources/c/tacocat/main/tacocat.h index 2f04f94..60d571d 100644 --- a/sources/c/tacocat/main/tacocat.h +++ b/sources/c/tacocat/main/tacocat.h @@ -22,24 +22,17 @@ #include #include #include -#include -#include #include -#include #include -#include -#include #include -#include +#include +#include #include #include -#include #include #include // FLL-1 includes. -#include -#include #include // FLL-2 includes. -- 1.8.3.1