From c1e06dd20cd25d6cf1dd7e94d5b72a61184cfc0b Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 8 Dec 2023 21:43:18 -0600 Subject: [PATCH] Update: Further re-design f_socket_connect(). This is a follow up to commit 596997daebd53246b205d0221c5c616bfafb7d23. Make sure to set the appropriate sizeof for the socket length. Update the documentation comments to communicate that the socket length is updated on the function calls. Make f_socket_connect()'s socket parameter a pointer. Set the socket length in f_socket_connect() to the appropriate size. Add documentation comments regarding the pointers that must not be NULL in f_socket. Update the related unit tests. --- level_0/f_socket/c/socket.c | 92 +++++++++++++--------- level_0/f_socket/c/socket.h | 61 +++++++++++++- .../f_socket/tests/unit/c/test-socket-connect.c | 6 +- level_0/f_socket/tests/unit/c/test-socket-read.c | 6 +- .../tests/unit/c/test-socket-read_stream.c | 6 +- level_0/f_socket/tests/unit/c/test-socket-write.c | 6 +- .../tests/unit/c/test-socket-write_stream.c | 6 +- 7 files changed, 128 insertions(+), 55 deletions(-) diff --git a/level_0/f_socket/c/socket.c b/level_0/f_socket/c/socket.c index 0c44ec9..8289c58 100644 --- a/level_0/f_socket/c/socket.c +++ b/level_0/f_socket/c/socket.c @@ -57,17 +57,17 @@ extern "C" { if (socket->form == f_socket_address_form_inet4_e) { address = (struct sockaddr *) &socket->address.inet4; socket->address.inet4.sin_family = f_socket_address_family_inet4_e; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_in); } else if (socket->form == f_socket_address_form_inet6_e) { address = (struct sockaddr *) &socket->address.inet6; socket->address.inet6.sin6_family = f_socket_address_family_inet6_e; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_in6); } else if (socket->form == f_socket_address_form_local_e) { address = (struct sockaddr *) &socket->address.local; socket->address.local.sun_family = f_socket_address_family_local_e; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_un); if (socket->name.used && socket->name.string && socket->name.used + 1 <= socket->name.size) { memcpy((void *) socket->address.local.sun_path, (void *) socket->name.string, socket->name.used); @@ -88,49 +88,49 @@ extern "C" { #ifdef _en_support_socket_address_at_ else if (socket->form == f_socket_address_form_at_e) { address = (struct sockaddr *) &socket->address.at; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_at); } #endif // _en_support_socket_address_at_ #ifdef _en_support_socket_address_ax25_ else if (socket->form == f_socket_address_form_ax25_e) { address = (struct sockaddr *) &socket->address.ax25; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_ax25); } #endif // _en_support_socket_address_ax25_ #ifdef _en_support_socket_address_dl_ else if (socket->form == f_socket_address_form_dl_e) { address = (struct sockaddr *) &socket->address.dl; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_dl); } #endif // _en_support_socket_address_dl_ #ifdef _en_support_socket_address_eon_ else if (socket->form == f_socket_address_form_eon_e) { address = (struct sockaddr *) &socket->address.eon; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_eon); } #endif // _en_support_socket_address_eon_ #ifdef _en_support_socket_address_ipx_ else if (socket->form == f_socket_address_form_ipx_e) { address = (struct sockaddr *) &socket->address.ipx; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_ipx); } #endif // _en_support_socket_address_ipx_ #ifdef _en_support_socket_address_iso_ else if (socket->form == f_socket_address_form_iso_e) { address = (struct sockaddr *) &socket->address.iso; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_iso); } #endif // _en_support_socket_address_iso_ #ifdef _en_support_socket_address_ns_ else if (socket->form == f_socket_address_form_ns_e) { address = (struct sockaddr *) &socket->address.ns; - socket->length = sizeof(struct sockaddr); + socket->length = sizeof(struct sockaddr_ns); } #endif // _en_support_socket_address_ns_ @@ -168,83 +168,99 @@ extern "C" { #endif // _di_f_socket_bind_ #ifndef _di_f_socket_connect_ - f_status_t f_socket_connect(const f_socket_t socket) { + f_status_t f_socket_connect(f_socket_t * const socket) { + #ifndef _di_level_0_parameter_checking_ + if (!socket) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ - if (socket.id == -1) return F_file_descriptor; + if (socket->id == -1) return F_file_descriptor; struct sockaddr * address = 0; - if (socket.form == f_socket_address_form_inet4_e) { - address = (struct sockaddr *) &socket.address.inet4; + if (socket->form == f_socket_address_form_inet4_e) { + address = (struct sockaddr *) &socket->address.inet4; + socket->length = sizeof(struct sockaddr_in); } - else if (socket.form == f_socket_address_form_inet6_e) { - address = (struct sockaddr *) &socket.address.inet6; + else if (socket->form == f_socket_address_form_inet6_e) { + address = (struct sockaddr *) &socket->address.inet6; + socket->length = sizeof(struct sockaddr_in6); } - else if (socket.form == f_socket_address_form_local_e) { - address = (struct sockaddr *) &socket.address.local; + else if (socket->form == f_socket_address_form_local_e) { + address = (struct sockaddr *) &socket->address.local; + socket->length = sizeof(struct sockaddr_un); } #ifdef _en_support_socket_address_arp_ - else if (socket.form == f_socket_address_form_arp_e) { - address = (struct sockaddr *) &socket.address.arp; + else if (socket->form == f_socket_address_form_arp_e) { + address = (struct sockaddr *) &socket->address.arp; + socket->length = sizeof(struct sockaddr); } #endif // _en_support_socket_address_arp_ #ifdef _en_support_socket_address_at_ - else if (socket.form == f_socket_address_form_at_e) { - address = (struct sockaddr *) &socket.address.at; + else if (socket->form == f_socket_address_form_at_e) { + address = (struct sockaddr *) &socket->address.at; + socket->length = sizeof(struct sockaddr_at); } #endif // _en_support_socket_address_at_ #ifdef _en_support_socket_address_ax25_ - else if (socket.form == f_socket_address_form_ax25_e) { - address = (struct sockaddr *) &socket.address.ax25; + else if (socket->form == f_socket_address_form_ax25_e) { + address = (struct sockaddr *) &socket->address.ax25; + socket->length = sizeof(struct sockaddr_ax25); } #endif // _en_support_socket_address_ax25_ #ifdef _en_support_socket_address_dl_ - else if (socket.form == f_socket_address_form_dl_e) { - address = (struct sockaddr *) &socket.address.dl; + else if (socket->form == f_socket_address_form_dl_e) { + address = (struct sockaddr *) &socket->address.dl; + socket->length = sizeof(struct sockaddr_dl); } #endif // _en_support_socket_address_dl_ #ifdef _en_support_socket_address_eon_ - else if (socket.form == f_socket_address_form_eon_e) { - address = (struct sockaddr *) &socket.address.eon; + else if (socket->form == f_socket_address_form_eon_e) { + address = (struct sockaddr *) &socket->address.eon; + socket->length = sizeof(struct sockaddr_eon); } #endif // _en_support_socket_address_eon_ #ifdef _en_support_socket_address_ipx_ - else if (socket.form == f_socket_address_form_ipx_e) { - address = (struct sockaddr *) &socket.address.ipx; + else if (socket->form == f_socket_address_form_ipx_e) { + address = (struct sockaddr *) &socket->address.ipx; + socket->length = sizeof(struct sockaddr_ipx); } #endif // _en_support_socket_address_ipx_ #ifdef _en_support_socket_address_iso_ - else if (socket.form == f_socket_address_form_iso_e) { - address = (struct sockaddr *) &socket.address.iso; + else if (socket->form == f_socket_address_form_iso_e) { + address = (struct sockaddr *) &socket->address.iso; + socket->length = sizeof(struct sockaddr_iso); } #endif // _en_support_socket_address_iso_ #ifdef _en_support_socket_address_ns_ - else if (socket.form == f_socket_address_form_ns_e) { - address = (struct sockaddr *) &socket.address.ns; + else if (socket->form == f_socket_address_form_ns_e) { + address = (struct sockaddr *) &socket->address.ns; + socket->length = sizeof(struct sockaddr_ns); } #endif // _en_support_socket_address_ns_ #ifdef _en_support_socket_address_x25_ - else if (socket.form == f_socket_address_form_x25_e) { - address = (struct sockaddr *) &socket.address.x25; + else if (socket->form == f_socket_address_form_x25_e) { + address = (struct sockaddr *) &socket->address.x25; + socket->length = sizeof(struct sockaddr_x25); } #endif // _en_support_socket_address_x25_ else { // Generic (f_socket_address_form_generic_e) or failsafe. - address = (struct sockaddr *) &socket.address.generic; + address = (struct sockaddr *) &socket->address.generic; + socket->length = sizeof(struct sockaddr); } - if (connect(socket.id, address, socket.length) == -1) { + if (connect(socket->id, address, socket->length) == -1) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EADDRINUSE) return F_status_set_error(F_busy_address); if (errno == EADDRNOTAVAIL) return F_status_set_error(F_available_not_address); diff --git a/level_0/f_socket/c/socket.h b/level_0/f_socket/c/socket.h index 7691ddf..66febd2 100644 --- a/level_0/f_socket/c/socket.h +++ b/level_0/f_socket/c/socket.h @@ -65,6 +65,8 @@ extern "C" { * The socket.id is the socket file descriptor used to establish the data file descriptor socket.id_data. * The socket.id_data, socket.address, and socket.length are updated upon a successful return. * + * Must not be NULL. + * * @return * F_okay on success. * @@ -108,7 +110,7 @@ extern "C" { * The socket.address may be any valid structure, like "struct sockaddr", "struct sockaddr_un", or "struct sockaddr_in". * The socket.address.*.*_family is conditionally altered by this function. * The caller must appropriately initialize and configure the socket.address. - * The socket.length must represent the full size of the address structure and is not altered by this function. + * The socket.length is updated based on socket.form. * The socket.id must refer to a valid socket file descriptor. * * For IPv4: @@ -137,6 +139,8 @@ extern "C" { * The socket.length is updated to represent the size of "struct sockaddr". * The socket.type is not modified. * + * Must not be NULL. + * * @return * F_okay on success. * @@ -171,6 +175,9 @@ extern "C" { * The socket.address must have the proper structure setup based on the socket.type value, like "f_socket_address_family_inet4_e". * Only domains defined with an associated structure in the f_socket_address_t are supported. * Only socket.id is used. + * The socket.length is updated based on socket.form. + * + * Must not be NULL. * * @return * F_okay on success. @@ -199,7 +206,7 @@ extern "C" { * @see connect() */ #ifndef _di_f_socket_connect_ - extern f_status_t f_socket_connect(const f_socket_t socket); + extern f_status_t f_socket_connect(f_socket_t * const socket); #endif // _di_f_socket_connect_ /** @@ -213,6 +220,8 @@ extern "C" { * The socket.type must be assigned the desired socket type. * The socket.id will be updated with a file descriptor representing the created socket. * + * Must not be NULL. + * * @return * F_okay on success. * @@ -247,11 +256,15 @@ extern "C" { * The protocol to use. * @param id_1 * The first of the pair of socket file descriptors. + * + * Must not be NULL. * @param id_2 * The second of the pair of socket file descriptors. * * This socket is supposed to be identical to the one specified by id_1. * + * Must not be NULL. + * * @return * F_okay on success. * @@ -294,6 +307,8 @@ extern "C" { * @param socket * The socket structure. * The socket.id must represent a valid socket file descriptor. + * + * Must not be NULL. * @param action * The action to perform on close. * f_socket_close_fast_e calls close(). @@ -328,6 +343,8 @@ extern "C" { * @param socket * The socket structure. * The socket.id must represent a valid socket file descriptor. + * + * Must not be NULL. * @param backlog_max * The max length of the pending connections queue. * Suggested default setting: 8. @@ -354,6 +371,8 @@ extern "C" { * @param socket * The socket structure. * The socket.id must represent a valid socket file descriptor. + * + * Must not be NULL. * @param level * The level in which the socket option is located. * This may be synonymous with "layer". @@ -361,9 +380,13 @@ extern "C" { * The option code used to represent the option name. * @param value * The value to assign. + * + * Must not be NULL. * @param length * The length of the value (often derived from a sizeof() call). * + * Must not be NULL. + * * @return * F_okay on success. * @@ -387,6 +410,8 @@ extern "C" { * @param socket * The socket structure. * The socket.id must represent a valid socket file descriptor. + * + * Must not be NULL. * @param level * The level in which the socket option is located. * This may be synonymous with "layer". @@ -394,6 +419,8 @@ extern "C" { * The option code used to represent the option name. * @param value * The value to assign. + * + * Must not be NULL. * @param length * The length of the value (often derived from a sizeof() call). * @@ -423,11 +450,15 @@ extern "C" { * The socket structure. * The socket.id must represent a valid socket file descriptor. * The socket.size_read is used to represent the buffer size in buffer and must not be larger than the actual size of the buffer. + * + * Must not be NULL. * @param name * The retrieved host name. * The name.size is used to determine as the max size. * If name.size is 0, then a default max (F_socket_default_name_max_d) is used. * + * Must not be NULL. + * * @return * F_okay on success. * @@ -460,6 +491,8 @@ extern "C" { * The socket.size_read is used to represent the buffer size in buffer and must not be larger than the actual size of the buffer. * The socket.address is used to store the name of the remote connection. * + * Must not be NULL. + * * @return * F_okay on success. * @@ -489,10 +522,14 @@ extern "C" { * The socket structure. * The socket.id_data must represent a valid file descriptor. * The socket.size_read is used to represent the buffer size in buffer and must not be larger than the actual size of the buffer. + * + * Must not be NULL. * @param flags * Read flags. * @param buffer * The buffer to populate. + * + * Must not be NULL. * @param length * (optional) The length of the buffer. * This gets replaced with the value of a positive ssize_t representing the length read. @@ -535,10 +572,14 @@ extern "C" { * @param socket * The socket structure. * The socket.id_data must represent a valid file descriptor. + * + * Must not be NULL. * @param flags * Read flags. * @param header * The message header. + * + * Must not be NULL. * @param length * (optional) The length of the buffer. * This gets replaced with the value of a positive ssize_t representing the length read. @@ -583,10 +624,14 @@ extern "C" { * The socket structure. * The socket.id_data must represent a valid file descriptor. * The socket.size_read is used to represent the buffer size in buffer and must not be larger than the actual size of the buffer. + * + * Must not be NULL. * @param flags * Read flags. * @param buffer * The buffer to populate. + * + * Must not be NULL. * @param length * (optional) The length of the buffer. * This gets replaced with the value of a positive ssize_t representing the length read. @@ -632,10 +677,14 @@ extern "C" { * The socket structure. * The socket.id_data must represent a valid socket file descriptor. * The socket.size_write is used to represent the buffer size in buffer and must not be larger than the actual size of the buffer. + * + * Must not be NULL. * @param flags * Read flags. * @param buffer * The buffer to populate. + * + * Must not be NULL. * @param length * (optional) The length of the buffer. * This gets replaced with the value of a positive ssize_t representing the length send. @@ -682,10 +731,14 @@ extern "C" { * @param socket * The socket structure. * The socket.id_data must represent a valid file descriptor. + * + * Must not be NULL. * @param flags * Read flags. * @param header * The message header. + * + * Must not be NULL. * @param length * (optional) The length of the buffer. * This gets replaced with the value of a positive ssize_t representing the length send. @@ -734,10 +787,14 @@ extern "C" { * The socket structure. * The socket.id_data must represent a valid socket file descriptor. * The socket.size_write is used to represent the buffer size in buffer and must not be larger than the actual size of the buffer. + * + * Must not be NULL. * @param flags * Read flags. * @param buffer * The buffer to populate. + * + * Must not be NULL. * @param length * (optional) The length of the buffer. * This gets replaced with the value of a positive ssize_t representing the length send. diff --git a/level_0/f_socket/tests/unit/c/test-socket-connect.c b/level_0/f_socket/tests/unit/c/test-socket-connect.c index 824b2be..bcec369 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-connect.c +++ b/level_0/f_socket/tests/unit/c/test-socket-connect.c @@ -58,7 +58,7 @@ void test__f_socket_connect__fails(void **state) { will_return(__wrap_connect, true); will_return(__wrap_connect, errnos[i]); - const f_status_t status = f_socket_connect(socket); + const f_status_t status = f_socket_connect(&socket); assert_int_equal(status, F_status_set_error(statuss[i])); } // for @@ -71,7 +71,7 @@ void test__f_socket_connect__returns_file_descriptor(void **state) { socket.id = -1; - const f_status_t status = f_socket_connect(socket); + const f_status_t status = f_socket_connect(&socket); assert_int_equal(status, F_file_descriptor); } @@ -86,7 +86,7 @@ void test__f_socket_connect__works(void **state) { { will_return(__wrap_connect, false); - const f_status_t status = f_socket_connect(socket); + const f_status_t status = f_socket_connect(&socket); assert_int_equal(status, F_okay); } diff --git a/level_0/f_socket/tests/unit/c/test-socket-read.c b/level_0/f_socket/tests/unit/c/test-socket-read.c index ade485f..71fbfb8 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-read.c +++ b/level_0/f_socket/tests/unit/c/test-socket-read.c @@ -8,7 +8,7 @@ extern "C" { void test__f_socket_read__fails(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; size_t length = 0; int errnos[] = { @@ -70,7 +70,7 @@ void test__f_socket_read__fails(void **state) { void test__f_socket_read__parameter_checking(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; { const f_status_t status = f_socket_read(0, 0, 0, 0); @@ -94,7 +94,7 @@ void test__f_socket_read__parameter_checking(void **state) { void test__f_socket_read__works(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; size_t length = 0; { diff --git a/level_0/f_socket/tests/unit/c/test-socket-read_stream.c b/level_0/f_socket/tests/unit/c/test-socket-read_stream.c index 7751877..e21fb24 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-read_stream.c +++ b/level_0/f_socket/tests/unit/c/test-socket-read_stream.c @@ -8,7 +8,7 @@ extern "C" { void test__f_socket_read_stream__fails(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; size_t length = 0; int errnos[] = { @@ -70,7 +70,7 @@ void test__f_socket_read_stream__fails(void **state) { void test__f_socket_read_stream__parameter_checking(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; { const f_status_t status = f_socket_read_stream(0, 0, 0, 0); @@ -94,7 +94,7 @@ void test__f_socket_read_stream__parameter_checking(void **state) { void test__f_socket_read_stream__works(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; size_t length = 0; { diff --git a/level_0/f_socket/tests/unit/c/test-socket-write.c b/level_0/f_socket/tests/unit/c/test-socket-write.c index c311fb0..3b6d8c3 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-write.c +++ b/level_0/f_socket/tests/unit/c/test-socket-write.c @@ -8,7 +8,7 @@ extern "C" { void test__f_socket_write__fails(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; size_t length = 0; int errnos[] = { @@ -76,7 +76,7 @@ void test__f_socket_write__fails(void **state) { void test__f_socket_write__parameter_checking(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; { const f_status_t status = f_socket_write(0, 0, 0, 0); @@ -100,7 +100,7 @@ void test__f_socket_write__parameter_checking(void **state) { void test__f_socket_write__works(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; size_t length = 0; { diff --git a/level_0/f_socket/tests/unit/c/test-socket-write_stream.c b/level_0/f_socket/tests/unit/c/test-socket-write_stream.c index 54c4491..f3b6511 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-write_stream.c +++ b/level_0/f_socket/tests/unit/c/test-socket-write_stream.c @@ -8,7 +8,7 @@ extern "C" { void test__f_socket_write_stream__fails(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; size_t length = 0; int errnos[] = { @@ -76,7 +76,7 @@ void test__f_socket_write_stream__fails(void **state) { void test__f_socket_write_stream__parameter_checking(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; { const f_status_t status = f_socket_write_stream(0, 0, 0, 0); @@ -100,7 +100,7 @@ void test__f_socket_write_stream__parameter_checking(void **state) { void test__f_socket_write_stream__works(void **state) { f_socket_t socket = f_socket_t_initialize; - char *buffer = "test"; + char * const buffer = "test"; size_t length = 0; { -- 1.8.3.1