From: Kevin Day Date: Tue, 1 Mar 2022 00:49:51 +0000 (-0600) Subject: Update: Socket parameters, rename f_socket_bind_file() to f_socket_bind_local(),... X-Git-Tag: 0.5.9~115 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=7b9da12b643bac9a4a96caafb9f225e5bdb21a33;p=fll Update: Socket parameters, rename f_socket_bind_file() to f_socket_bind_local(), and return F_local_not when UNIX socket is required. Make the f_socket_t passed to the f_socket_bind() and f_socket_bind_file() a pointer constant. Use the f_socket_bind_local() rather than f_socket_bind_file() to better communicate that this is a local (UNIX) socket. The f_socket_bind_local() requires the socket to be a local (UNIX) socket, so return F_local_not (with error bit) if it is not a local (UNIX) socket. --- diff --git a/level_0/f_socket/c/socket.c b/level_0/f_socket/c/socket.c index 30b0e8e..0d78e54 100644 --- a/level_0/f_socket/c/socket.c +++ b/level_0/f_socket/c/socket.c @@ -47,9 +47,12 @@ extern "C" { #endif // _di_f_socket_accept_ #ifndef _di_f_socket_bind_ - f_status_t f_socket_bind(const f_socket_t socket) { + f_status_t f_socket_bind(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 (bind(socket.id, socket.address, socket.length) == -1) { + if (bind(socket->id, socket->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); @@ -69,18 +72,27 @@ extern "C" { } #endif // _di_f_socket_bind_ -#ifndef _di_f_socket_bind_file_ - f_status_t f_socket_bind_file(const f_socket_t socket) { +#ifndef _di_f_socket_bind_local_ + f_status_t f_socket_bind_local(f_socket_t * const socket) { #ifndef _di_level_0_parameter_checking_ - if (socket.domain != f_socket_domain_file_d) return F_status_set_error(F_parameter); + if (!socket) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - memset(socket.address, 0, sizeof(struct sockaddr_un)); - ((struct sockaddr_un *) socket.address)->sun_family = f_socket_domain_file_d; + if (socket->domain != f_socket_domain_file_d) { + return F_status_set_error(F_local_not); + } - strncpy(((struct sockaddr_un *) socket.address)->sun_path, socket.name, sizeof(((struct sockaddr_un *) socket.address)->sun_path) - 1); + memset(socket->address, 0, sizeof(struct sockaddr_un)); + + { + struct sockaddr_un *address = (struct sockaddr_un *) socket->address; + + address->sun_family = f_socket_domain_file_d; + + strncpy(address->sun_path, socket->name, sizeof(address->sun_path) - 1); + } - if (bind(socket.id, socket.address, sizeof(struct sockaddr_un)) == -1) { + if (bind(socket->id, socket->address, sizeof(struct sockaddr_un)) == -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); @@ -98,7 +110,7 @@ extern "C" { return F_none; } -#endif // _di_f_socket_bind_file_ +#endif // _di_f_socket_bind_local_ #ifndef _di_f_socket_connect_ f_status_t f_socket_connect(const f_socket_t socket) { diff --git a/level_0/f_socket/c/socket.h b/level_0/f_socket/c/socket.h index 7ff2d0a..7fdcf7c 100644 --- a/level_0/f_socket/c/socket.h +++ b/level_0/f_socket/c/socket.h @@ -109,11 +109,11 @@ extern "C" { * @see bind() */ #ifndef _di_f_socket_bind_ - extern f_status_t f_socket_bind(const f_socket_t socket); + extern f_status_t f_socket_bind(f_socket_t * const socket); #endif // _di_f_socket_bind_ /** - * Bind a socket to a UNIX socket file. + * Bind a socket to a local (UNIX) socket file. * * This does initialize and memset() the address with the address set to a UNIX socket (struct sockaddr_un). * @@ -132,6 +132,7 @@ extern "C" { * F_busy_address (with error bit) if address is already in use (therefore unavailable). * F_directory_found_not (with error bit) if directory was not found. * F_file_found_not (with error bit) if file not found. + * F_local_not (with erro bit) if domain in not a UNIX socket. * F_memory_not (with error bit) if out of memory. * F_name (with error bit) on path name error. * F_parameter (with error bit) if a parameter is invalid. @@ -144,9 +145,9 @@ extern "C" { * @see memset() * @see strncpy() */ -#ifndef _di_f_socket_bind_file_ - extern f_status_t f_socket_bind_file(const f_socket_t socket); -#endif // _di_f_socket_bind_file_ +#ifndef _di_f_socket_bind_local_ + extern f_status_t f_socket_bind_local(f_socket_t * const socket); +#endif // _di_f_socket_bind_local_ /** * Connect to a socket.