]> Kevux Git Server - kevux-tools/commitdiff
Progress: Further work in TacocaT.
authorKevin Day <thekevinday@gmail.com>
Mon, 4 Dec 2023 01:48:51 +0000 (19:48 -0600)
committerKevin Day <thekevinday@gmail.com>
Mon, 4 Dec 2023 01:48:51 +0000 (19:48 -0600)
While working on testing, cleaning up, and finish the code I stopped to make changes to the networking code.
This is the random state the code ended up being in while I adjusted focus.

I will have to resume here and figure out what I was last doing before my adjusted focus happened.

sources/c/tacocat/main/common.c
sources/c/tacocat/main/common.h
sources/c/tacocat/main/common/print.c
sources/c/tacocat/main/common/print.h
sources/c/tacocat/main/print/error.c
sources/c/tacocat/main/print/error.h
sources/c/tacocat/main/process.c
sources/c/tacocat/main/send.c

index 79de62dad90f2f57bf187cfb8052f9d9cbf3362b..9225e8db947a8c7b05a9b0e521275bd4bbc47ddd 100644 (file)
@@ -380,16 +380,16 @@ extern "C" {
             }
             else if (main->setting.flag & kt_tacocat_main_flag_resolve_classic_e) {
               memset(&host, 0, sizeof(struct hostent));
+              memset(&family, 0, sizeof(f_network_family_ip_t));
 
-              port = 0;
               address = main->program.parameters.arguments.array[index];
               f_char_t address_string[address.used];
-              f_string_range_t port_range = f_string_range_t_initialize;
+              f_string_range_double_t range_ip = f_string_range_double_t_initialize;
 
               memcpy(address_string, address.string, address.used);
               address.string = address_string;
 
-              f_network_is_ip_address(address, &port, &main->setting.state);
+              f_network_is_ip_address(address, &range_ip, &main->setting.state);
 
               if (F_status_is_error(main->setting.state.status)) {
                 // @todo print error message about bad port number or similar.
@@ -398,24 +398,27 @@ extern "C" {
 
               if (main->setting.state.status == F_network_version_four) {
                 host.h_addrtype = f_socket_address_family_inet4_e;
+                family.type = f_network_family_ip_4_e;
               }
               else if (main->setting.state.status == F_network_version_six) {
                 host.h_addrtype = f_socket_address_family_inet6_e;
+                family.type = f_network_family_ip_6_e;
               }
               else {
                 host.h_addrtype = 0;
+                family.type = f_network_family_none_e;
               }
 
-              kt_tacocat_setting_load_address_port_extract(main, &address, &port, &port_range);
+              kt_tacocat_setting_load_address_port_extract(main, range_ip, &address, &port);
 
               if (F_status_is_error(main->setting.state.status)) {
                 macro_setting_load_print_first();
 
-                if (port_range.start > port_range.stop) {
+                if (F_status_set_fine(main->setting.state.status) == F_parameter) {
                   kt_tacocat_print_error(&main->program.error, macro_kt_tacocat_f(kt_tacocat_setting_load_address_port_extract));
                 }
                 else {
-                  kt_tacocat_print_error_port_number_invalid(&main->program.error, address, port_range);
+                  kt_tacocat_print_error_port_number_invalid(&main->program.error, address, range_ip);
                 }
 
                 if (F_status_is_error_not(failed)) {
@@ -438,6 +441,10 @@ extern "C" {
                 main->setting.state.status = f_string_dynamic_append(address, &sets[i]->array[j].network);
 
                 macro_setting_load_handle_send_receive_error_continue_2(f_string_dynamic_append);
+
+                main->setting.state.status = f_network_from_ip_string(address, &family);
+
+                macro_setting_load_handle_send_receive_error_continue_2(f_network_from_ip_string);
               }
               else {
                 main->setting.state.status = f_network_from_ip_name(address, &host);
@@ -484,15 +491,6 @@ extern "C" {
 
                 macro_setting_load_handle_send_receive_error_continue_2(f_memory_array_increase_by);
 
-                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]);
-                }
-                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, &sets[i]->array[j].network);
 
                 if (main->setting.state.status == F_data_not || !host.h_addr_list || !host.h_addr_list[0]) {
@@ -521,12 +519,12 @@ extern "C" {
               if (host.h_addrtype == f_socket_address_family_inet4_e) {
                 sets[i]->array[j].socket.domain = f_socket_protocol_family_inet4_e;
                 sets[i]->array[j].socket.address.inet4.sin_port = htons((in_port_t) port);
-                sets[i]->array[j].socket.address.inet4.sin_addr.s_addr = INADDR_ANY;
+                sets[i]->array[j].socket.address.inet4.sin_addr.s_addr = family.address.v4.s_addr;
               }
               else if (host.h_addrtype == f_socket_address_family_inet6_e) {
                 sets[i]->array[j].socket.domain = f_socket_protocol_family_inet6_e;
                 sets[i]->array[j].socket.address.inet6.sin6_port = htons((in_port_t) port);
-                sets[i]->array[j].socket.address.inet6.sin6_addr = in6addr_any;
+                sets[i]->array[j].socket.address.inet6.sin6_addr = family.address.v6;
               }
             }
             else {
@@ -628,11 +626,11 @@ extern "C" {
 #endif // _di_kt_tacocat_setting_load_send_receive_
 
 #ifndef _di_kt_tacocat_setting_load_address_port_extract_
-  void kt_tacocat_setting_load_address_port_extract(kt_tacocat_main_t * const main, f_string_static_t * const address, f_number_unsigned_t * const port, f_string_range_t * const port_range) {
+  void kt_tacocat_setting_load_address_port_extract(kt_tacocat_main_t * const main, const f_string_range_double_t range_ip, f_string_static_t * const address, f_number_unsigned_t * const port) {
 
     if (!main) return;
 
-    if (!address || !port || !port_range) {
+    if (!address || !port) {
       main->setting.state.status = F_status_set_error(F_parameter);
 
       return;
@@ -644,24 +642,26 @@ extern "C" {
       return;
     }
 
-    port_range->stop = address->used;
-
     if (main->setting.state.status == F_network_version_four || main->setting.state.status == F_network_version_six) {
-      port_range->start = 0;
-
-      if (*port) {
-        port_range->start = *port;
+      if (range_ip.start_2 <= range_ip.stop_2) {
         *port = 0;
 
-        const f_string_static_t adjusted = macro_f_string_static_t_initialize_1(address->string + port_range->start, 0, address->used - port_range->start);
+        {
+          const f_string_static_t adjusted = macro_f_string_static_t_initialize_1(address->string + range_ip.start_2, 0, (range_ip.start_2 - range_ip.stop_2) + 1);
 
-        main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, adjusted, port);
-        if (F_status_is_error(main->setting.state.status)) return;
+          main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, adjusted, port);
+          if (F_status_is_error(main->setting.state.status)) return;
+        }
 
-        address->string[port_range->start] = 0;
-        address->used = port_range->start;
+        address->string[range_ip.start_2] = 0;
+        address->used = range_ip.start_2;
 
         while (address->used && address->string[address->used] != f_string_ascii_colon_s.string[0]) --address->used;
+
+        // Be sure to also remove the colon.
+        if (address->used) {
+          address->string[address->used--] = 0;
+        }
       }
 
       main->setting.state.status = F_okay;
@@ -669,26 +669,7 @@ extern "C" {
       return;
     }
 
-    *port = 0;
-    port_range->start = address->used;
-
-    while (--port_range->start) {
-      if (address->string[port_range->start] == f_string_ascii_colon_s.string[0]) break;
-    } // while
-
-    if (port_range->start && ++port_range->start < address->used) {
-      const f_string_static_t adjusted = macro_f_string_static_t_initialize_1(address->string + port_range->start, 0, address->used - port_range->start);
-
-      main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, adjusted, port);
-      if (F_status_is_error(main->setting.state.status)) return;
-
-      address->string[--port_range->start] = 0;
-      address->used = port_range->start;
-      main->setting.state.status = F_okay;
-    }
-    else {
-      main->setting.state.status = F_number_not;
-    }
+    main->setting.state.status = F_number_not;
   }
 #endif // _di_kt_tacocat_setting_load_address_port_extract_
 
index a9b7a30b1caed921ae53333fc0d6d709a3260412..4607ba61ed11d33d22c9ce8df31f3b8d583a2ea9 100644 (file)
@@ -110,6 +110,8 @@ extern "C" {
  *     F_parameter (with error bit) if a parameter is invalid.
  *
  *     Errors (with error bit) from: fl_conversion_dynamic_to_unsigned_detect().
+ * @param range_ip
+ *   The range in the string representing the extracted address and port number.
  * @param address
  *   The string representing the address to extract the port number from.
  *   If a valid number is found, then this is updated to truncate the length at the colon and a NULL termination is inserted at the colon.
@@ -124,16 +126,13 @@ extern "C" {
  *   - Decimal (base 10) is used for all other cases.
  * @param port
  *   The extracted port number.
- * @param port_range
- *   The range in the string representing the extracted port number.
- *   This is primarily intended for printing errors.
  *
  *   When main.setting.state.status is set to either F_network_version_four or F_network_version_six when calling this function, the port represents the location within the address string that the port number begins.
  *
  * @see fl_conversion_dynamic_to_unsigned_detect()
  */
 #ifndef _di_kt_tacocat_setting_load_address_port_extract_
-  extern void kt_tacocat_setting_load_address_port_extract(kt_tacocat_main_t * const main, f_string_static_t * const address, f_number_unsigned_t * const port, f_string_range_t * const port_range);
+  extern void kt_tacocat_setting_load_address_port_extract(kt_tacocat_main_t * const main, const f_string_range_double_t range_ip, f_string_static_t * const address, f_number_unsigned_t * const port);
 #endif // _di_kt_tacocat_setting_load_address_port_extract_
 
 #ifdef __cplusplus
index 747c2578466276ebdb6ca6ffe04671f7ac589631..3763f8c3448a29f3a146c7097023539703c04680 100644 (file)
@@ -17,6 +17,7 @@ extern "C" {
     "f_memory_array_increase_by",
     "f_memory_array_resize",
     "f_network_from_ip_name",
+    "f_network_from_ip_string",
     "f_network_is_ip_address",
     "f_socket_accept",
     "f_socket_bind_inet4",
index e3d57be40de770c07549c1a6a1f7c311fe7aa411..58e05e266d633865dc447e43652d32627ebf1bea 100644 (file)
@@ -50,6 +50,7 @@ extern "C" {
     kt_tacocat_f_f_memory_array_increase_by_e,
     kt_tacocat_f_f_memory_array_resize_e,
     kt_tacocat_f_f_network_from_ip_name_e,
+    kt_tacocat_f_f_network_from_ip_string_e,
     kt_tacocat_f_f_network_is_ip_address_e,
     kt_tacocat_f_f_socket_accept_e,
     kt_tacocat_f_f_socket_bind_inet4_e,
index 1d05db02e8b79944dcfbc4b4646809c0a6e24acf..82d81a0940b91e38ae539d312f0a293e818a18be 100644 (file)
@@ -249,17 +249,19 @@ extern "C" {
 #endif // _di_kt_tacocat_print_error_parameter_value_resolve_unknown_
 
 #ifndef _di_kt_tacocat_print_error_port_number_invalid_
-  f_status_t kt_tacocat_print_error_port_number_invalid(fl_print_t * const print, const f_string_static_t address, const f_string_range_t port_range) {
+  f_status_t kt_tacocat_print_error_port_number_invalid(fl_print_t * const print, const f_string_static_t address, const f_string_range_double_t range_ip) {
 
     if (!print || !print->custom) return F_status_set_error(F_output_not);
     if (print->verbosity < f_console_verbosity_error_e) return F_output_not;
 
     kt_tacocat_main_t * const main = (kt_tacocat_main_t *) print->custom;
 
+    const f_string_range_t range = macro_f_string_range_t_initialize_1(range_ip.start_2, range_ip.stop_2);
+
     f_file_stream_lock(print->to);
 
     fl_print_format("%[%QUnknown or invalid port number%] ", print->to, print->set->error, print->prefix, print->set->error);
-    fl_print_format("%[%/Q%]", print->to, print->set->notable, address, port_range, print->set->notable);
+    fl_print_format("%[%/Q%]", print->to, print->set->notable, address, range, print->set->notable);
     fl_print_format(" %[from the address '%]", print->to, print->set->error, print->set->error, f_string_eol_s);
     fl_print_format(f_string_format_Q_single_s.string, print->to, print->set->notable, address, print->set->notable);
     fl_print_format("%['.%]%r", print->to, print->set->error, print->set->error, f_string_eol_s);
index 373b7b87398e4a9a4946a0dada74180e416b4fb4..4bc3a81f1d852dfb02d97c3d848188dd3d033051 100644 (file)
@@ -361,8 +361,8 @@ extern "C" {
  *   This does not alter print.custom.setting.state.status.
  * @param address
  *   The entire address string, including the port number.
- * @param port_range
- *   The range within the address string representing the port.
+ * @param range_ip
+ *   The positions within the address representing the IP address and port.
  *
  * @return
  *   F_okay on success.
@@ -373,7 +373,7 @@ extern "C" {
  * @see fll_error_file_print()
  */
 #ifndef _di_kt_tacocat_print_error_port_number_invalid_
-  extern f_status_t kt_tacocat_print_error_port_number_invalid(fl_print_t * const print, const f_string_static_t address, const f_string_range_t port_range);
+  extern f_status_t kt_tacocat_print_error_port_number_invalid(fl_print_t * const print, const f_string_static_t address, const f_string_range_double_t range_ip);
 #endif // _di_kt_tacocat_print_error_port_number_invalid_
 
 /**
index 72a0fccc29e3afce5abede2110694e9e99b9f497..f8a341911471aa577191b70ef1293d3894769dec 100644 (file)
@@ -239,9 +239,6 @@ extern "C" {
         continue;
       }
 
-      // The id_data socket ID is the same when sending (writing) to the socket.
-      main->setting.send.array[i].socket.id_data = main->setting.send.array[i].socket.id;
-
       // Make the socket re-usable.
       {
         value_socket = 1;
@@ -260,7 +257,17 @@ extern "C" {
       for (main->setting.send.array[i].retry = 0; main->setting.send.array[i].retry < kt_tacocat_startup_retry_max_d; ++main->setting.send.array[i].retry) {
 
         if (main->setting.send.array[i].socket.domain == f_socket_protocol_family_inet4_e || main->setting.send.array[i].socket.domain == f_socket_protocol_family_inet6_e || main->setting.send.array[i].socket.domain == f_socket_protocol_family_local_e) {
-          main->setting.send.array[i].status = f_socket_connect(main->setting.send.array[i].socket);
+          main->setting.send.array[i].status = f_socket_connect(main->setting.send.array[i].socket); // @fixme this is currently failing because part of the parameters is invalid (figure out why).
+
+          // The id_data socket ID is the same when sending (writing) to the socket.
+          if (F_status_is_error_not(main->setting.send.array[i].status)) {
+            main->setting.send.array[i].socket.id_data = main->setting.send.array[i].socket.id;
+          }
+
+          // Treat invalid file descriptor as an error as the descriptor should be valid at this point in time.
+          if (main->setting.send.array[i].status == F_file_descriptor) {
+            main->setting.send.array[i].status = F_status_set_error(F_file_descriptor);
+          }
         }
         else {
           main->setting.status_send = F_status_set_error(F_parameter);
@@ -322,6 +329,7 @@ extern "C" {
         main->setting.send_polls.array[i].revents = 0;
       }
       else {
+        // @todo do not forget to utilize the poll fd.
         main->setting.send_polls.array[i].fd = main->setting.send.array[i].socket.id;
         main->setting.send_polls.array[i].events = f_poll_write_e;
         main->setting.send_polls.array[i].revents = 0;
index ca1be97711339f99c9e02b4ad326515af190c34b..6c1e54f268ecf173251941261621d3c7de6ba0c7 100644 (file)
@@ -282,10 +282,11 @@ extern "C" {
     }
 
     if (set->flag == kt_tacocat_socket_flag_send_connect_e) {
-      set->status = f_socket_connect(set->socket);
-      macro_kt_send_process_handle_error_exit_1(main, f_socket_connect, kt_tacocat_send_connect_s, set->network, set->status, set->name, set->flag);
+      // @fixme this is already performed, so do not do this here, the whole kt_tacocat_socket_flag_send_connect_e stage and kt_tacocat_send_connect_s should be removed.
+      //set->status = f_socket_connect(set->socket);
+      //macro_kt_send_process_handle_error_exit_1(main, f_socket_connect, kt_tacocat_send_connect_s, set->network, set->status, set->name, set->flag);
 
-      set->socket.id_data = set->socket.id;
+      //set->socket.id_data = set->socket.id;
       set->flag = kt_tacocat_socket_flag_send_header_e;
     }
 
@@ -345,6 +346,8 @@ extern "C" {
         kt_tacocat_print_warning_on_file(&main->program.warning, macro_kt_tacocat_f(f_socket_disconnect), kt_tacocat_send_done_s, set->network, set->status, set->name, f_file_operation_close_s);
       }
 
+      f_file_close_id(&set->socket.id_data);
+
       set->socket.id = -1;
       set->socket.id_data = -1;
       set->status = F_okay;