From 5fb1a66c2bdad7d359ac595881bf660df960c7ad Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 7 Jul 2023 22:40:09 -0500 Subject: [PATCH] Progress: Further work in TacocaT. Do more of the needed configuration. Call the appropriate bind functions. --- sources/c/tacocat/main/common.c | 29 ++++++++++++++++++++++------- sources/c/tacocat/main/common/define.h | 15 ++++++++++----- sources/c/tacocat/main/common/print.c | 3 +++ sources/c/tacocat/main/common/print.h | 3 +++ sources/c/tacocat/main/process.c | 32 ++++++++++++++++++++++++++++---- 5 files changed, 66 insertions(+), 16 deletions(-) diff --git a/sources/c/tacocat/main/common.c b/sources/c/tacocat/main/common.c index cd652e1..60ae83f 100644 --- a/sources/c/tacocat/main/common.c +++ b/sources/c/tacocat/main/common.c @@ -264,6 +264,12 @@ extern "C" { macro_setting_load_handle_send_receive_error_continue_basic(f_string_dynamic_append_nulless); } + + strings[i]->array[j].string[strings[i]->array[j].used] = 0; + sockets[i]->array[j].domain = f_socket_protocol_family_local_e; + sockets[i]->array[j].protocol = f_socket_protocol_tcp_e; + sockets[i]->array[j].type = f_socket_address_family_local_e; + sockets[i]->array[j].name = strings[i]->array[j]; } else if (main->setting.flag & kt_tacocat_main_flag_resolve_classic_e) { memset(&host, 0, sizeof(struct hostent)); @@ -287,7 +293,7 @@ extern "C" { } // 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]); + main->setting.state.status = f_string_dynamic_increase_by(INET6_ADDRSTRLEN + 1, &strings[i]->array[j]); macro_setting_load_handle_send_receive_error_continue_basic(f_string_dynamic_increase_by); @@ -306,11 +312,11 @@ extern "C" { } // 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]); + main->setting.state.status = f_string_dynamic_increase_by(INET6_ADDRSTRLEN + 1, &strings[i]->array[j]); macro_setting_load_handle_send_receive_error_continue_basic(f_string_dynamic_increase_by); - if (host.h_addrtype == AF_INET) { + if (host.h_addrtype == f_socket_address_family_inet4_e) { family.type = f_network_family_ip_4_e; family.address.v4 = *((struct in_addr *) host.h_addr_list[k]); } @@ -334,11 +340,20 @@ extern "C" { 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]; + sockets[i]->array[j].protocol = f_socket_protocol_tcp_e; + sockets[i]->array[j].type = host.h_addrtype; + + if (host.h_addrtype == f_socket_address_family_inet4_e) { + sockets[i]->array[j].domain = f_socket_protocol_family_inet4_e; + sockets[i]->array[j].address.inet4.sin_port = 0; // @todo detect and pull port number from host name if supplied. + sockets[i]->array[j].address.inet4.sin_addr.s_addr = INADDR_ANY; + } + else if (host.h_addrtype == f_socket_address_family_inet6_e) { + sockets[i]->array[j].domain = f_socket_protocol_family_inet6_e; + sockets[i]->array[j].address.inet6.sin6_port = 0; // @todo detect and pull port number from host name if supplied. + sockets[i]->array[j].address.inet6.sin6_addr = in6addr_any; + } } else { // @todo Kevux DNS resolution. diff --git a/sources/c/tacocat/main/common/define.h b/sources/c/tacocat/main/common/define.h index c927259..5fafccb 100644 --- a/sources/c/tacocat/main/common/define.h +++ b/sources/c/tacocat/main/common/define.h @@ -24,24 +24,29 @@ extern "C" { * - large: An allocation step used for buffers that are anticipated to have large buffers. * - small: An allocation step used for buffers that are anticipated to have small buffers. * - * kt_tacocat_signal_*_d: - * - check: When not using threads, this is how often to perform the check (lower numbers incur more kernel I/O). - * - check_failsafe: When using threads, how many consecutive failures to check signal before aborting (as a recursion failsafe). + * kt_tacocat_backlog_*_d: + * - max: The max backlog size to use. * * kt_tacocat_block_size_*_d: * - receive: The block size in bytes to use when sending packets. * - send: The block size in bytes to use when receiving packets. + * + * kt_tacocat_signal_*_d: + * - check: When not using threads, this is how often to perform the check (lower numbers incur more kernel I/O). + * - check_failsafe: When using threads, how many consecutive failures to check signal before aborting (as a recursion failsafe). */ #ifndef _di_kt_tacocat_d_ #define kt_tacocat_allocation_console_d 4 #define kt_tacocat_allocation_large_d 2048 #define kt_tacocat_allocation_small_d 128 - #define kt_tacocat_signal_check_d 20000 - #define kt_tacocat_signal_check_failsafe_d 20000 + #define kt_tacocat_backlog_max_d 1024 #define kt_tacocat_block_size_receive_d 65535 #define kt_tacocat_block_size_send_d 65535 + + #define kt_tacocat_signal_check_d 20000 + #define kt_tacocat_signal_check_failsafe_d 20000 #endif // _di_kt_tacocat_d_ /** diff --git a/sources/c/tacocat/main/common/print.c b/sources/c/tacocat/main/common/print.c index 01e38a2..94f0aed 100644 --- a/sources/c/tacocat/main/common/print.c +++ b/sources/c/tacocat/main/common/print.c @@ -10,6 +10,9 @@ extern "C" { "f_file_open", "f_files_increase_by", "f_network_from_ip_name", + "f_socket_bind_inet4", + "f_socket_bind_inet6", + "f_socket_bind_local", "f_socket_close", "f_socket_create", "f_socket_disconnect", diff --git a/sources/c/tacocat/main/common/print.h b/sources/c/tacocat/main/common/print.h index 1d42038..36235ed 100644 --- a/sources/c/tacocat/main/common/print.h +++ b/sources/c/tacocat/main/common/print.h @@ -43,6 +43,9 @@ extern "C" { kt_tacocat_f_f_file_open_e, kt_tacocat_f_f_files_increase_by_e, kt_tacocat_f_f_network_from_ip_name_e, + kt_tacocat_f_f_socket_bind_inet4_e, + kt_tacocat_f_f_socket_bind_inet6_e, + kt_tacocat_f_f_socket_bind_local_e, kt_tacocat_f_f_socket_close_e, kt_tacocat_f_f_socket_create_e, kt_tacocat_f_f_socket_disconnect_e, diff --git a/sources/c/tacocat/main/process.c b/sources/c/tacocat/main/process.c index 3469a2d..a2dd618 100644 --- a/sources/c/tacocat/main/process.c +++ b/sources/c/tacocat/main/process.c @@ -30,13 +30,11 @@ extern "C" { // @todo Make this threaded, if threading is enabled (if threading is disabled then fork). // TacocaT is intended to be simple, so be lazy and create a thread/fork for every single receive and send. - // Create socket and connect to socket (bind / listen). + // Create, bind, and listen to the socket. for (; i < main->setting.socket_receives.used; ++i) { if (kt_tacocat_signal_check(main)) return; - // @todo check to see if connection is one of "local", "inet" (ipv4), or "inet6" (ipv6) and configure socket appropriately. - main->setting.status_receives.array[i] = f_socket_create(&main->setting.socket_receives.array[i]); if (F_status_is_error(main->setting.status_receives.array[i])) { @@ -47,7 +45,33 @@ extern "C" { continue; } - main->setting.status_receives.array[i] = f_socket_listen(&main->setting.socket_receives.array[i], 100); // @todo setup a max back log define rather than hardcode 100 here (and 100 is arbitrarily chosen). + if (main->setting.socket_receives.array[i].domain == f_socket_protocol_family_inet4_e) { + main->setting.status_receives.array[i] = f_socket_bind_inet4(&main->setting.socket_receives.array[i]); + } + else if (main->setting.socket_receives.array[i].domain == f_socket_protocol_family_inet6_e) { + main->setting.status_receives.array[i] = f_socket_bind_inet6(&main->setting.socket_receives.array[i]); + } + else if (main->setting.socket_receives.array[i].domain == f_socket_protocol_family_local_e) { + main->setting.status_receives.array[i] = f_socket_bind_local(&main->setting.socket_receives.array[i]); + } + else { + // @todo error, not supported. + } + + if (F_status_is_error(main->setting.status_receives.array[i])) { + main->setting.state.status = main->setting.status_receives.array[i]; + + kt_tacocat_print_error(&main->program.error, main->setting.socket_receives.array[i].domain == f_socket_protocol_family_inet4_e + ? macro_kt_tacocat_f(f_socket_bind_inet4) + : main->setting.socket_receives.array[i].domain == f_socket_protocol_family_inet6_e + ? macro_kt_tacocat_f(f_socket_bind_inet6) + : macro_kt_tacocat_f(f_socket_bind_local) + ); + + continue; + } + + main->setting.status_receives.array[i] = f_socket_listen(&main->setting.socket_receives.array[i], kt_tacocat_backlog_max_d); if (F_status_is_error(main->setting.status_receives.array[i])) { main->setting.state.status = main->setting.status_receives.array[i]; -- 1.8.3.1