The networking types are already in use, such as address family (AF_*) and protocol family (PF_*).
The known and supported socket address structures (struct sockaddr) do not directly relate to either.
Create a new enumeration that directly maps to these socket address structures (struct sockaddr), called an address form.
A 16-bit integer is hopefully more than is ever needed for this.
Rewrite f_socket_bind() and f_socket_connect() to use the socket.form enumeration.
The custom *_inet4(), *_inet6(), and *_local() functions are removed.
Add a generic, or fallback aka failsafe, type for handling the most basic socket address tructure (struct sockaddr).
if (!socket) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
- if (bind(socket->id, (struct sockaddr *) &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);
- if (errno == EFAULT) return F_status_set_error(F_buffer);
- if (errno == EINVAL) return F_status_set_error(F_parameter);
- if (errno == ELOOP) return F_status_set_error(F_loop);
- if (errno == ENAMETOOLONG) return F_status_set_error(F_string_too_large);
- if (errno == ENOENT) return F_status_set_error(F_file_found_not);
- if (errno == ENOMEM) return F_status_set_error(F_memory_not);
- if (errno == ENOTDIR) return F_status_set_error(F_directory_found_not);
- if (errno == ENOTSOCK) return F_status_set_error(F_socket_not);
-
- return F_status_set_error(F_failure);
+ struct sockaddr * address = 0;
+
+ 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);
+ }
+ 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);
+ }
+ 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);
+
+ 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);
+ socket->address.local.sun_path[socket->name.used] = 0;
+ }
+ else {
+ socket->address.local.sun_path[0] = 0;
+ }
}
- return F_okay;
- }
-#endif // _di_f_socket_bind_
+ #ifdef _en_support_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_
-#ifndef _di_f_socket_bind_inet4_
- f_status_t f_socket_bind_inet4(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_
+ #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);
+ }
+ #endif // _en_support_socket_address_at_
- if (socket->domain != f_socket_protocol_family_inet4_e) return F_status_set_error(F_address_not);
+ #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);
+ }
+ #endif // _en_support_socket_address_ax25_
- socket->address.inet4.sin_family = f_socket_address_family_inet4_e;
- socket->length = sizeof(struct sockaddr_in);
+ #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);
+ }
+ #endif // _en_support_socket_address_dl_
- if (bind(socket->id, (struct sockaddr *) &socket->address.inet4, 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);
- if (errno == EFAULT) return F_status_set_error(F_buffer);
- if (errno == EINVAL) return F_status_set_error(F_parameter);
- if (errno == ELOOP) return F_status_set_error(F_loop);
- if (errno == ENAMETOOLONG) return F_status_set_error(F_string_too_large);
- if (errno == ENOENT) return F_status_set_error(F_file_found_not);
- if (errno == ENOMEM) return F_status_set_error(F_memory_not);
- if (errno == ENOTDIR) return F_status_set_error(F_directory_found_not);
- if (errno == ENOTSOCK) return F_status_set_error(F_socket_not);
+ #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);
+ }
+ #endif // _en_support_socket_address_eon_
- return F_status_set_error(F_failure);
- }
+ #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);
+ }
+ #endif // _en_support_socket_address_ipx_
- return F_okay;
- }
-#endif // _di_f_socket_bind_inet4_
+ #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);
+ }
+ #endif // _en_support_socket_address_iso_
-#ifndef _di_f_socket_bind_inet6_
- f_status_t f_socket_bind_inet6(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_
+ #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);
+ }
+ #endif // _en_support_socket_address_ns_
- if (socket->domain != f_socket_protocol_family_inet6_e) return F_status_set_error(F_address_not);
+ #ifdef _en_support_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_
- socket->address.inet6.sin6_family = f_socket_address_family_inet6_e;
- socket->length = sizeof(struct sockaddr_in6);
+ // Generic (f_socket_address_form_generic_e) or failsafe.
+ else {
+ address = (struct sockaddr *) &socket->address.generic;
+ socket->length = sizeof(struct sockaddr);
+ }
- if (bind(socket->id, (struct sockaddr *) &socket->address.inet6, socket->length) == -1) {
+ if (bind(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);
return F_okay;
}
-#endif // _di_f_socket_bind_inet6_
+#endif // _di_f_socket_bind_
-#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) return F_status_set_error(F_parameter);
- #endif // _di_level_0_parameter_checking_
+#ifndef _di_f_socket_connect_
+ f_status_t f_socket_connect(const f_socket_t socket) {
- if (socket->domain != f_socket_protocol_family_local_e) return F_status_set_error(F_local_not);
+ if (socket.id == -1) return F_file_descriptor;
- socket->address.local.sun_family = f_socket_address_family_local_e;
- socket->length = sizeof(struct sockaddr_un);
+ struct sockaddr * address = 0;
- if (socket->name.used && socket->name.string) {
- memcpy((void *) socket->address.local.sun_path, (void *) socket->name.string, socket->name.used);
- socket->address.local.sun_path[socket->name.used] = 0;
+ if (socket.form == f_socket_address_form_inet4_e) {
+ address = (struct sockaddr *) &socket.address.inet4;
}
- else {
- socket->address.local.sun_path[0] = 0;
+ else if (socket.form == f_socket_address_form_inet6_e) {
+ address = (struct sockaddr *) &socket.address.inet6;
+ }
+ else if (socket.form == f_socket_address_form_local_e) {
+ address = (struct sockaddr *) &socket.address.local;
}
- if (bind(socket->id, (struct sockaddr *) &socket->address.local, 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);
- if (errno == EFAULT) return F_status_set_error(F_buffer);
- if (errno == EINVAL) return F_status_set_error(F_parameter);
- if (errno == ELOOP) return F_status_set_error(F_loop);
- if (errno == ENAMETOOLONG) return F_status_set_error(F_string_too_large);
- if (errno == ENOENT) return F_status_set_error(F_file_found_not);
- if (errno == ENOMEM) return F_status_set_error(F_memory_not);
- if (errno == ENOTDIR) return F_status_set_error(F_directory_found_not);
- if (errno == ENOTSOCK) return F_status_set_error(F_socket_not);
+ #ifdef _en_support_socket_address_arp_
+ else if (socket.form == f_socket_address_form_arp_e) {
+ address = (struct sockaddr *) &socket.address.arp;
+ }
+ #endif // _en_support_socket_address_arp_
- return F_status_set_error(F_failure);
- }
+ #ifdef _en_support_socket_address_at_
+ else if (socket.form == f_socket_address_form_at_e) {
+ address = (struct sockaddr *) &socket.address.at;
+ }
+ #endif // _en_support_socket_address_at_
- return F_okay;
- }
-#endif // _di_f_socket_bind_local_
+ #ifdef _en_support_socket_address_ax25_
+ else if (socket.form == f_socket_address_form_ax25_e) {
+ address = (struct sockaddr *) &socket.address.ax25;
+ }
+ #endif // _en_support_socket_address_ax25_
-#ifndef _di_f_socket_connect_
- f_status_t f_socket_connect(const f_socket_t socket) {
+ #ifdef _en_support_socket_address_dl_
+ else if (socket.form == f_socket_address_form_dl_e) {
+ address = (struct sockaddr *) &socket.address.dl;
+ }
+ #endif // _en_support_socket_address_dl_
- if (socket.id == -1) return F_file_descriptor;
+ #ifdef _en_support_socket_address_eon_
+ else if (socket.form == f_socket_address_form_eon_e) {
+ address = (struct sockaddr *) &socket.address.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;
+ }
+ #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;
+ }
+ #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;
+ }
+ #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;
+ }
+ #endif // _en_support_socket_address_x25_
+
+ else {
+
+ // Generic (f_socket_address_form_generic_e) or failsafe.
+ address = (struct sockaddr *) &socket.address.generic;
+ }
- if (connect(socket.id, (struct sockaddr *) &socket.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);
* @param socket
* The socket structure.
* The socket.address may be any valid structure, like "struct sockaddr", "struct sockaddr_un", or "struct sockaddr_in".
- * The socket.address.*.*_family is not directly altered by this function.
+ * 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.id must refer to a valid socket file descriptor.
*
+ * For IPv4:
+ * The socket.address.inet4.sin_family is directly assigned to f_socket_address_family_inet4_e.
+ * The socket.domain (potocol family) must be assigned to f_socket_protocol_family_inet4_e.
+ * The socket.length is updated to represent the size of "struct sockaddr_in".
+ * The socket.type is not modified.
+ *
+ * For IPv6:
+ * The socket.address.inet4.sin_family is directly assigned to f_socket_address_family_inet6_e.
+ * The socket.domain (potocol family) must be assigned to f_socket_protocol_family_inet6_e.
+ * The socket.length is updated to represent the size of "struct sockaddr_in6".
+ * The socket.type is not modified.
+ *
+ * For local (UNIX socket):
+ * The socket.address.local.sun_family is directly assigned to f_socket_address_family_local_e.
+ * The socket.address.local.sun_path is updated with the value from socket.name.string.
+ * The socket.domain (potocol family) must be assigned to f_socket_protocol_family_local_e.
+ * The socket.length is updated to represent the size of "struct sockaddr_un".
+ * The socket.name must be assigned to a path.
+ * The socket.type is not modified.
+ *
+ * For generic or unknown (failsafe):
+ * The socket.address.inet4.sin_family is directly assigned to f_socket_address_family_unspecified_e.
+ * The socket.domain (potocol family) must be assigned to f_socket_protocol_family_unspecified_e.
+ * The socket.length is updated to represent the size of "struct sockaddr".
+ * The socket.type is not modified.
+ *
* @return
* F_okay on success.
*
#endif // _di_f_socket_bind_
/**
- * Bind a socket to an IPv4 address.
- *
- * @param socket
- * The socket to use.
- *
- * The socket.address.inet4.sin_family is directly assigned to f_socket_address_family_inet4_e.
- * The socket.domain (potocol family) must be assigned to f_socket_protocol_family_inet4_e.
- * The socket.length is updated to represent the size of "struct sockaddr_in".
- * The socket.type (address family) will be assigned to f_socket_address_family_inet4_e.
- *
- * @return
- * F_okay on success.
- *
- * F_address (with error bit) if address is already in use (therefore unavailable).
- * F_address_not (with error bit) if socket.domain is not set to f_socket_protocol_family_inet4_e.
- * F_available_not_address (with error bit) if address is unavailable (is non-existent).
- * F_buffer (with error bit) if the buffer is invalid.
- * 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_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.
- * F_socket_not (with error bit) if the ID is not a socket descriptor.
- * F_string_too_large (with error bit) if string is too large to store in the buffer.
- *
- * F_failure (with error bit) for any other error.
- *
- * @see bind()
- */
-#ifndef _di_f_socket_bind_inet4_
- extern f_status_t f_socket_bind_inet4(f_socket_t * const socket);
-#endif // _di_f_socket_bind_inet4_
-
-/**
- * Bind a socket to an IPv6 address.
- *
- * @param socket
- * The socket to use.
- *
- * The socket.address.inet6.sin_family is directly assigned to f_socket_address_family_inet6_e.
- * The socket.domain (potocol family) must be assigned to f_socket_protocol_family_inet6_e.
- * The socket.length is updated to represent the size of "struct sockaddr_in6".
- * The socket.type (address family) will be assigned to f_socket_address_family_inet6_e.
- *
- * @return
- * F_okay on success.
- *
- * F_address (with error bit) if address is already in use (therefore unavailable).
- * F_address_not (with error bit) if socket.domain is not set to f_socket_protocol_family_inet6_e.
- * F_available_not_address (with error bit) if address is unavailable (is non-existent).
- * F_buffer (with error bit) if the buffer is invalid.
- * 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_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.
- * F_socket_not (with error bit) if the ID is not a socket descriptor.
- * F_string_too_large (with error bit) if string is too large to store in the buffer.
- *
- * F_failure (with error bit) for any other error.
- *
- * @see bind()
- */
-#ifndef _di_f_socket_bind_inet6_
- extern f_status_t f_socket_bind_inet6(f_socket_t * const socket);
-#endif // _di_f_socket_bind_inet6_
-
-/**
- * Bind a socket to a local (UNIX) socket file.
- *
- * @param socket
- * The socket to use.
- *
- * The socket.address.local.sun_family is directly assigned to f_socket_address_family_local_e.
- * The socket.address.local.sun_path is updated with the value from socket.name.string.
- * The socket.domain (potocol family) must be assigned to f_socket_protocol_family_local_e.
- * The socket.length is updated to represent the size of "struct sockaddr_un".
- * The socket.name must be assigned to a path.
- * The socket.type (address family) will be assigned to f_socket_address_family_local_e.
- *
- * @return
- * F_okay on success.
- *
- * F_address (with error bit) if address is already in use (therefore unavailable).
- * F_available_not_address (with error bit) if address is unavailable (is non-existent or not local).
- * F_buffer (with error bit) if the buffer is invalid.
- * 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 error bit) if socket.domain is not set to f_socket_protocol_family_local_e.
- * 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.
- * F_socket_not (with error bit) if the ID is not a socket descriptor.
- * F_string_too_large (with error bit) if string is too large to store in the buffer.
- *
- * F_failure (with error bit) for any other error.
- *
- * @see bind()
- * @see memcpy()
- */
-#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.
*
* This connects the socket against socket.id.
*
* @param socket
* The socket structure.
- * The socket.address may point to any valid structure, like "struct sockaddr", "struct sockaddr_un", or "struct sockaddr_in".
+ * 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.
*
* @return
#endif
/**
+ * Socket address forms.
+ *
+ * These are specific to the f_socket_address_t below for designating which form of sockaddr is being used.
+ *
+ * f_socket_address_form_*_e:
+ * - arp: Arp.
+ * - at: At.
+ * - ax25: Ax25.
+ * - dl: Dl.
+ * - eon: Eon.
+ * - inet4: Ipv4 internet network socket.
+ * - inet6: Ipv6 internet network socket..
+ * - ipx: Ipx.
+ * - iso: Iso.
+ * - local: Local (aka: Unix) socket (socket file or localhost).
+ * - ns: Ns.
+ * - generic: None specified, does not use extra fields and should be considered the fallback / failsafe (see AF_UNSPEC).
+ * - x25: X25.
+ */
+#ifndef _di_f_socket_address_form_e_
+ enum {
+ f_socket_address_form_generic_e = 0,
+ f_socket_address_form_inet4_e,
+ f_socket_address_form_inet6_e,
+ f_socket_address_form_local_e,
+
+ #ifdef _en_support_socket_address_arp_
+ f_socket_address_form_arp_e,
+ #endif // _en_support_socket_address_arp_
+
+ #ifdef _en_support_socket_address_at_
+ f_socket_address_form_at_e,
+ #endif // _en_support_socket_address_at_
+
+ #ifdef _en_support_socket_address_ax25_
+ f_socket_address_form_ax25_e,
+ #endif // _en_support_socket_address_ax25_
+
+ #ifdef _en_support_socket_address_dl_
+ f_socket_address_form_dl_e,
+ #endif // _en_support_socket_address_dl_
+
+ #ifdef _en_support_socket_address_eon_
+ f_socket_address_form_eon_e,
+ #endif // _en_support_socket_address_eon_
+
+ #ifdef _en_support_socket_address_ipx_
+ f_socket_address_form_ipx_e,
+ #endif // _en_support_socket_address_ipx_
+
+ #ifdef _en_support_socket_address_iso_
+ f_socket_address_form_iso_e,
+ #endif // _en_support_socket_address_iso_
+
+ #ifdef _en_support_socket_address_ns_
+ f_socket_address_form_ns_e,
+ #endif // _en_support_socket_address_ns_
+
+ #ifdef _en_support_socket_address_x25_
+ f_socket_address_form_x25_e,
+ #endif // _en_support_socket_address_x25_
+ }; // enum
+#endif // _di_f_socket_address_form_e_
+
+/**
* Provide a union to simplify using struct sockaddr in arrays.
*
* Managing different structures is not as practical.
*
* All of the properties may or may not be present, but inet4, inet6, and local are generally assumed to exist.
*
+ * These are associated with the address family, such as f_socket_address_family_local_e.
+ *
* Properties:
- * - arp: Arp.
- * - at: At.
- * - ax25: Ax25.
- * - dl: Dl.
- * - eon: Eon.
- * - inet4: Ipv4 internet network socket.
- * - inet6: Ipv6 internet network socket..
- * - ipx: Ipx.
- * - iso: Iso.
- * - local: Local (aka: Unix) socket (socket file or localhost).
- * - ns: Ns.
- * - x25: X25.
+ * - arp: Arp.
+ * - at: At.
+ * - ax25: Ax25.
+ * - dl: Dl.
+ * - eon: Eon.
+ * - inet4: Ipv4 internet network socket.
+ * - inet6: Ipv6 internet network socket..
+ * - ipx: Ipx.
+ * - iso: Iso.
+ * - local: Local (aka: Unix) socket (socket file or localhost).
+ * - ns: Ns.
+ * - generic: None specified, does not use extra fields and should be considered the fallback / failsafe (see AF_UNSPEC).
+ * - x25: X25.
*/
#ifndef _di_f_socket_address_t_
typedef union {
+ struct sockaddr generic;
struct sockaddr_in inet4;
struct sockaddr_in6 inet6;
struct sockaddr_un local;
#define f_socket_address_t_initialize { 0 }
+ #define f_socket_address_initialize_generic(value_generic) { .generic = value_generic }
#define f_socket_address_initialize_inet4(value_inet4) { .inet4 = value_inet4 }
#define f_socket_address_initialize_inet6(value_inet6) { .inet6 = value_inet6 }
#define f_socket_address_initialize_local(value_local) { .local = value_local }
* - domain: The socket domain (protocol family, such as f_socket_protocol_family_local_e).
* - protocol: The socket protocol (such as f_socket_protocol_tcp_e).
* - type: The socket type (address family, such as f_socket_address_family_local_e).
+ * - form: The form of the socket address type, such as f_socket_address_form_inet4_e for sockaddr_in.
*
* - size_read: The default number of 1-byte characters to read at a time and is often used for the read buffer size.
* - size_write: The default number of 1-byte characters to read at a time and is often used for the write buffer size.
int domain;
int protocol;
int type;
+ uint16_t form;
size_t size_read;
size_t size_write;
f_string_static_t name;
} f_socket_t;
- #define f_socket_t_initialize { -1, -1, 0, 0, 0, F_socket_default_read_size_d, F_socket_default_write_size_d, f_socket_address_t_initialize, 0, f_string_static_t_initialize }
+ #define f_socket_t_initialize { -1, -1, 0, 0, 0, f_socket_address_form_generic_e, F_socket_default_read_size_d, F_socket_default_write_size_d, f_socket_address_t_initialize, 0, f_string_static_t_initialize }
- #define macro_f_socket_t_initialize_1(address, length) { \
+ #define macro_f_socket_t_initialize_1(form, address, length) { \
-1, \
-1, \
0, \
0, \
0, \
+ form, \
F_socket_default_read_size_d, \
F_socket_default_write_size_d, \
address, \
f_string_empty_s \
}
- #define macro_f_socket_t_initialize_2(address, length, name) { \
+ #define macro_f_socket_t_initialize_2(form, address, length, name) { \
-1, \
-1, \
0, \
0, \
0, \
+ form, \
F_socket_default_read_size_d, \
F_socket_default_write_size_d, \
address, \
name \
}
- #define macro_f_socket_t_initialize_3(id, domain, protocol, type, address, length, name) { \
+ #define macro_f_socket_t_initialize_3(id, domain, protocol, type, form, address, length, name) { \
id, \
-1, \
domain, \
protocol, \
type, \
+ form, \
F_socket_default_read_size_d, \
F_socket_default_write_size_d, \
address, \
name \
}
- #define macro_f_socket_t_initialize_4(id, domain, protocol, type, size_read, size_write, address, length, name) { \
+ #define macro_f_socket_t_initialize_4(id, domain, protocol, type, form, size_read, size_write, address, length, name) { \
id, \
-1, \
domain, \
protocol, \
type, \
+ form, \
size_read, \
size_write, \
address, \
name \
}
- #define macro_f_socket_t_initialize_5(id, id_data, domain, protocol, type, size_read, size_write, address, length, name) { \
+ #define macro_f_socket_t_initialize_5(id, id_data, domain, protocol, type, form, size_read, size_write, address, length, name) { \
id, \
id_data, \
domain, \
protocol, \
type, \
+ form, \
size_read, \
size_write, \
address, \
file.domain = 0; \
file.protocol = 0; \
file.type = 0; \
+ file.form = 0; \
file.size_read = 0; \
file.size_write = 0; \
file.length = 0; \
file.domain = 0; \
file.protocol = 0; \
file.type = 0; \
+ file.form = f_socket_address_form_generic_e; \
file.size_read = F_socket_default_read_size_d; \
file.size_write = F_socket_default_write_size_d; \
file.length = 0; \
build_libraries -lc -lcmocka
build_libraries-individual -lf_memory -lf_string -lf_type_array -lf_socket
-build_sources_program test-socket-accept.c test-socket-bind.c test-socket-bind_inet4.c test-socket-bind_inet6.c test-socket-bind_local.c test-socket-connect.c test-socket-create.c test-socket-create_pair.c test-socket-disconnect.c test-socket-listen.c test-socket-name_host.c test-socket-name_peer.c test-socket-option_get.c test-socket-option_set.c test-socket-read.c test-socket-read_message.c test-socket-read_stream.c test-socket-write.c test-socket-write_message.c test-socket-write_stream.c
+build_sources_program test-socket-accept.c test-socket-bind.c test-socket-connect.c test-socket-create.c test-socket-create_pair.c test-socket-disconnect.c test-socket-listen.c test-socket-name_host.c test-socket-name_peer.c test-socket-option_get.c test-socket-option_set.c test-socket-read.c test-socket-read_message.c test-socket-read_stream.c test-socket-write.c test-socket-write_message.c test-socket-write_stream.c
build_sources_program test-socket-addressss_delete_callback.c test-socket-addressss_destroy_callback.c
build_sources_program test-socket-ss_delete_callback.c test-socket-ss_destroy_callback.c
void test__f_socket_bind__fails(void **state) {
- f_socket_t socket = f_socket_t_initialize;
-
int errnos[] = {
EACCES,
EADDRINUSE,
F_failure,
};
- for (uint8_t i = 0; i < 12; ++i) {
+ {
+ f_socket_t socket = f_socket_t_initialize;
- will_return(__wrap_bind, true);
- will_return(__wrap_bind, errnos[i]);
+ for (uint8_t i = 0; i < 12; ++i) {
- const f_status_t status = f_socket_bind(&socket);
+ will_return(__wrap_bind, true);
+ will_return(__wrap_bind, errnos[i]);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_status_set_error(statuss[i]));
+ } // for
+ }
+
+ {
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_inet4_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_inet4_e;
+ socket.form = f_socket_address_form_inet4_e;
+
+ for (uint8_t i = 0; i < 12; ++i) {
+
+ will_return(__wrap_bind, true);
+ will_return(__wrap_bind, errnos[i]);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_status_set_error(statuss[i]));
+ } // for
+ }
+
+ {
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_inet4_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_inet4_e;
+ socket.form = f_socket_address_form_inet4_e;
+
+ for (uint8_t i = 0; i < 12; ++i) {
+
+ will_return(__wrap_bind, true);
+ will_return(__wrap_bind, errnos[i]);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_status_set_error(statuss[i]));
+ } // for
+ }
+
+ {
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_inet6_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_inet6_e;
+ socket.form = f_socket_address_form_inet6_e;
+
+ for (uint8_t i = 0; i < 12; ++i) {
+
+ will_return(__wrap_bind, true);
+ will_return(__wrap_bind, errnos[i]);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_status_set_error(statuss[i]));
+ } // for
+ }
+
+ {
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_inet6_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_inet6_e;
+ socket.form = f_socket_address_form_inet6_e;
+
+ for (uint8_t i = 0; i < 12; ++i) {
+
+ will_return(__wrap_bind, true);
+ will_return(__wrap_bind, errnos[i]);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_status_set_error(statuss[i]));
+ } // for
+ }
+
+ {
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_local_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_local_e;
+ socket.form = f_socket_address_form_local_e;
- assert_int_equal(status, F_status_set_error(statuss[i]));
- } // for
+ for (uint8_t i = 0; i < 12; ++i) {
+
+ will_return(__wrap_bind, true);
+ will_return(__wrap_bind, errnos[i]);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_status_set_error(statuss[i]));
+ } // for
+ }
+
+ {
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_local_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_local_e;
+ socket.form = f_socket_address_form_local_e;
+
+ for (uint8_t i = 0; i < 12; ++i) {
+
+ will_return(__wrap_bind, true);
+ will_return(__wrap_bind, errnos[i]);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_status_set_error(statuss[i]));
+ } // for
+ }
}
void test__f_socket_bind__parameter_checking(void **state) {
void test__f_socket_bind__works(void **state) {
- f_socket_t socket = f_socket_t_initialize;
+ {
+ f_socket_t socket = f_socket_t_initialize;
+
+ {
+ will_return(__wrap_bind, false);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_okay);
+ }
+ }
{
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_inet4_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_inet4_e;
+ socket.form = f_socket_address_form_inet4_e;
+
+ will_return(__wrap_bind, false);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_okay);
+ }
+
+ {
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_inet6_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_inet6_e;
+ socket.form = f_socket_address_form_inet6_e;
+
+ will_return(__wrap_bind, false);
+
+ const f_status_t status = f_socket_bind(&socket);
+
+ assert_int_equal(status, F_okay);
+ }
+
+ {
+ f_socket_t socket = f_socket_t_initialize;
+ socket.domain = f_socket_protocol_family_local_e;
+ socket.protocol = f_socket_protocol_tcp_e;
+ socket.type = f_socket_address_family_local_e;
+ socket.form = f_socket_address_form_local_e;
+
will_return(__wrap_bind, false);
const f_status_t status = f_socket_bind(&socket);
+++ /dev/null
-#include "test-socket.h"
-#include "test-socket-bind_inet4.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void test__f_socket_bind_inet4__fails(void **state) {
-
- int errnos[] = {
- EACCES,
- EADDRINUSE,
- EADDRNOTAVAIL,
- EFAULT,
- EINVAL,
- ELOOP,
- ENAMETOOLONG,
- ENOENT,
- ENOMEM,
- ENOTDIR,
- ENOTSOCK,
- mock_errno_generic,
- };
-
- f_status_t statuss[] = {
- F_access_denied,
- F_busy_address,
- F_available_not_address,
- F_buffer,
- F_parameter,
- F_loop,
- F_string_too_large,
- F_file_found_not,
- F_memory_not,
- F_directory_found_not,
- F_socket_not,
- F_failure,
- };
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_inet4_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_inet4_e;
-
- for (uint8_t i = 0; i < 12; ++i) {
-
- will_return(__wrap_bind, true);
- will_return(__wrap_bind, errnos[i]);
-
- const f_status_t status = f_socket_bind_inet4(&socket);
-
- assert_int_equal(status, F_status_set_error(statuss[i]));
- } // for
- }
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_inet4_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_inet4_e;
-
- for (uint8_t i = 0; i < 12; ++i) {
-
- will_return(__wrap_bind, true);
- will_return(__wrap_bind, errnos[i]);
-
- const f_status_t status = f_socket_bind_inet4(&socket);
-
- assert_int_equal(status, F_status_set_error(statuss[i]));
- } // for
- }
-}
-
-void test__f_socket_bind_inet4__parameter_checking(void **state) {
-
- {
- const f_status_t status = f_socket_bind_inet4(0);
-
- assert_int_equal(status, F_status_set_error(F_parameter));
- }
-}
-
-void test__f_socket_bind_inet4__works(void **state) {
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_inet4_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_inet4_e;
-
- will_return(__wrap_bind, false);
-
- const f_status_t status = f_socket_bind_inet4(&socket);
-
- assert_int_equal(status, F_okay);
- }
-}
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
+++ /dev/null
-/**
- * FLL - Level 0
- *
- * Project: Socket
- * API Version: 0.7
- * Licenses: lgpl-2.1-or-later
- *
- * Test the socket project.
- */
-#ifndef _TEST__F_socket_bind_inet4_h
-#define _TEST__F_socket_bind_inet4_h
-
-/**
- * Test that function fails.
- *
- * @see f_socket_bind_inet4()
- */
-extern void test__f_socket_bind_inet4__fails(void **state);
-
-/**
- * Test that parameter checking works as expected.
- *
- * @see f_socket_bind_inet4()
- */
-extern void test__f_socket_bind_inet4__parameter_checking(void **state);
-
-/**
- * Test that function works.
- *
- * @see f_socket_bind_inet4()
- */
-extern void test__f_socket_bind_inet4__works(void **state);
-
-#endif // _TEST__F_socket_bind_inet4_h
+++ /dev/null
-#include "test-socket.h"
-#include "test-socket-bind_inet6.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void test__f_socket_bind_inet6__fails(void **state) {
-
- int errnos[] = {
- EACCES,
- EADDRINUSE,
- EADDRNOTAVAIL,
- EFAULT,
- EINVAL,
- ELOOP,
- ENAMETOOLONG,
- ENOENT,
- ENOMEM,
- ENOTDIR,
- ENOTSOCK,
- mock_errno_generic,
- };
-
- f_status_t statuss[] = {
- F_access_denied,
- F_busy_address,
- F_available_not_address,
- F_buffer,
- F_parameter,
- F_loop,
- F_string_too_large,
- F_file_found_not,
- F_memory_not,
- F_directory_found_not,
- F_socket_not,
- F_failure,
- };
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_inet6_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_inet6_e;
-
- for (uint8_t i = 0; i < 12; ++i) {
-
- will_return(__wrap_bind, true);
- will_return(__wrap_bind, errnos[i]);
-
- const f_status_t status = f_socket_bind_inet6(&socket);
-
- assert_int_equal(status, F_status_set_error(statuss[i]));
- } // for
- }
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_inet6_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_inet6_e;
-
- for (uint8_t i = 0; i < 12; ++i) {
-
- will_return(__wrap_bind, true);
- will_return(__wrap_bind, errnos[i]);
-
- const f_status_t status = f_socket_bind_inet6(&socket);
-
- assert_int_equal(status, F_status_set_error(statuss[i]));
- } // for
- }
-}
-
-void test__f_socket_bind_inet6__parameter_checking(void **state) {
-
- {
- const f_status_t status = f_socket_bind_inet6(0);
-
- assert_int_equal(status, F_status_set_error(F_parameter));
- }
-}
-
-void test__f_socket_bind_inet6__works(void **state) {
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_inet6_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_inet6_e;
-
- will_return(__wrap_bind, false);
-
- const f_status_t status = f_socket_bind_inet6(&socket);
-
- assert_int_equal(status, F_okay);
- }
-}
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
+++ /dev/null
-/**
- * FLL - Level 0
- *
- * Project: Socket
- * API Version: 0.7
- * Licenses: lgpl-2.1-or-later
- *
- * Test the socket project.
- */
-#ifndef _TEST__F_socket_bind_inet6_h
-#define _TEST__F_socket_bind_inet6_h
-
-/**
- * Test that function fails.
- *
- * @see f_socket_bind_inet6()
- */
-extern void test__f_socket_bind_inet6__fails(void **state);
-
-/**
- * Test that parameter checking works as expected.
- *
- * @see f_socket_bind_inet6()
- */
-extern void test__f_socket_bind_inet6__parameter_checking(void **state);
-
-/**
- * Test that function works.
- *
- * @see f_socket_bind_inet6()
- */
-extern void test__f_socket_bind_inet6__works(void **state);
-
-#endif // _TEST__F_socket_bind_inet6_h
+++ /dev/null
-#include "test-socket.h"
-#include "test-socket-bind_local.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void test__f_socket_bind_local__fails(void **state) {
-
- int errnos[] = {
- EACCES,
- EADDRINUSE,
- EADDRNOTAVAIL,
- EFAULT,
- EINVAL,
- ELOOP,
- ENAMETOOLONG,
- ENOENT,
- ENOMEM,
- ENOTDIR,
- ENOTSOCK,
- mock_errno_generic,
- };
-
- f_status_t statuss[] = {
- F_access_denied,
- F_busy_address,
- F_available_not_address,
- F_buffer,
- F_parameter,
- F_loop,
- F_string_too_large,
- F_file_found_not,
- F_memory_not,
- F_directory_found_not,
- F_socket_not,
- F_failure,
- };
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_local_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_local_e;
-
- for (uint8_t i = 0; i < 12; ++i) {
-
- will_return(__wrap_bind, true);
- will_return(__wrap_bind, errnos[i]);
-
- const f_status_t status = f_socket_bind_local(&socket);
-
- assert_int_equal(status, F_status_set_error(statuss[i]));
- } // for
- }
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_local_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_local_e;
-
- for (uint8_t i = 0; i < 12; ++i) {
-
- will_return(__wrap_bind, true);
- will_return(__wrap_bind, errnos[i]);
-
- const f_status_t status = f_socket_bind_local(&socket);
-
- assert_int_equal(status, F_status_set_error(statuss[i]));
- } // for
- }
-}
-
-void test__f_socket_bind_local__parameter_checking(void **state) {
-
- {
- const f_status_t status = f_socket_bind_local(0);
-
- assert_int_equal(status, F_status_set_error(F_parameter));
- }
-}
-
-void test__f_socket_bind_local__works(void **state) {
-
- {
- f_socket_t socket = f_socket_t_initialize;
- socket.domain = f_socket_protocol_family_local_e;
- socket.protocol = f_socket_protocol_tcp_e;
- socket.type = f_socket_address_family_local_e;
-
- will_return(__wrap_bind, false);
-
- const f_status_t status = f_socket_bind_local(&socket);
-
- assert_int_equal(status, F_okay);
- }
-}
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
+++ /dev/null
-/**
- * FLL - Level 0
- *
- * Project: Socket
- * API Version: 0.7
- * Licenses: lgpl-2.1-or-later
- *
- * Test the socket project.
- */
-#ifndef _TEST__F_socket_bind_local_h
-#define _TEST__F_socket_bind_local_h
-
-/**
- * Test that function fails.
- *
- * @see f_socket_bind_local()
- */
-extern void test__f_socket_bind_local__fails(void **state);
-
-/**
- * Test that parameter checking works as expected.
- *
- * @see f_socket_bind_local()
- */
-extern void test__f_socket_bind_local__parameter_checking(void **state);
-
-/**
- * Test that function works.
- *
- * @see f_socket_bind_local()
- */
-extern void test__f_socket_bind_local__works(void **state);
-
-#endif // _TEST__F_socket_bind_local_h
cmocka_unit_test(test__f_socket_bind__fails),
cmocka_unit_test(test__f_socket_bind__works),
- cmocka_unit_test(test__f_socket_bind_inet4__fails),
- cmocka_unit_test(test__f_socket_bind_inet4__works),
-
- cmocka_unit_test(test__f_socket_bind_inet6__fails),
- cmocka_unit_test(test__f_socket_bind_inet6__works),
-
- cmocka_unit_test(test__f_socket_bind_local__fails),
- cmocka_unit_test(test__f_socket_bind_local__works),
-
cmocka_unit_test(test__f_socket_connect__fails),
cmocka_unit_test(test__f_socket_connect__returns_file_descriptor),
cmocka_unit_test(test__f_socket_connect__works),
#ifndef _di_level_0_parameter_checking_
cmocka_unit_test(test__f_socket_accept__parameter_checking),
cmocka_unit_test(test__f_socket_bind__parameter_checking),
- cmocka_unit_test(test__f_socket_bind_inet4__parameter_checking),
- cmocka_unit_test(test__f_socket_bind_inet6__parameter_checking),
- cmocka_unit_test(test__f_socket_bind_local__parameter_checking),
cmocka_unit_test(test__f_socket_create__parameter_checking),
cmocka_unit_test(test__f_socket_create_pair__parameter_checking),
cmocka_unit_test(test__f_socket_disconnect__parameter_checking),
#include "test-socket-addressss_delete_callback.h"
#include "test-socket-addressss_destroy_callback.h"
#include "test-socket-bind.h"
-#include "test-socket-bind_inet4.h"
-#include "test-socket-bind_inet6.h"
-#include "test-socket-bind_local.h"
#include "test-socket-connect.h"
#include "test-socket-create.h"
#include "test-socket-create_pair.h"