From aa8459dd13359af1f10dad07f14904855723cf95 Mon Sep 17 00:00:00 2001 From: Avi Weiss Date: Wed, 10 May 2023 13:54:36 +1000 Subject: [PATCH] Fix #864, Refactor duplicate `addrlen` swtiches into an inline function --- src/os/portable/os-impl-bsd-sockets.c | 75 +++++++++++---------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/src/os/portable/os-impl-bsd-sockets.c b/src/os/portable/os-impl-bsd-sockets.c index f02be9ed1..986297531 100644 --- a/src/os/portable/os-impl-bsd-sockets.c +++ b/src/os/portable/os-impl-bsd-sockets.c @@ -210,24 +210,14 @@ int32 OS_SocketOpen_Impl(const OS_object_token_t *token) /*---------------------------------------------------------------- * - * Purpose: Implemented per internal OSAL API - * See prototype for argument/return detail + * Internal helper routine only, not part of API. * *-----------------------------------------------------------------*/ -int32 OS_SocketBind_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Addr) +static inline socklen_t OS_GetAddrLen(sa_family_t sa_family) { - int os_result; - socklen_t addrlen; - const struct sockaddr * sa; - OS_impl_file_internal_record_t *impl; - OS_stream_internal_record_t * stream; - - impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token); - stream = OS_OBJECT_TABLE_GET(OS_stream_table, *token); + socklen_t addrlen; - sa = (const struct sockaddr *)&Addr->AddrData; - - switch (sa->sa_family) + switch (sa_family) { case AF_INET: addrlen = sizeof(struct sockaddr_in); @@ -242,6 +232,29 @@ int32 OS_SocketBind_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Ad break; } + return addrlen; +} + +/*---------------------------------------------------------------- + * + * Purpose: Implemented per internal OSAL API + * See prototype for argument/return detail + * + *-----------------------------------------------------------------*/ +int32 OS_SocketBind_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Addr) +{ + int os_result; + socklen_t addrlen; + const struct sockaddr * sa; + OS_impl_file_internal_record_t *impl; + OS_stream_internal_record_t * stream; + + impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token); + stream = OS_OBJECT_TABLE_GET(OS_stream_table, *token); + + sa = (const struct sockaddr *)&Addr->AddrData; + addrlen = OS_GetAddrLen(sa->sa_family); + if (addrlen == 0) { return OS_ERR_BAD_ADDRESS; @@ -285,21 +298,8 @@ int32 OS_SocketConnect_Impl(const OS_object_token_t *token, const OS_SockAddr_t impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token); - sa = (const struct sockaddr *)&Addr->AddrData; - switch (sa->sa_family) - { - case AF_INET: - slen = sizeof(struct sockaddr_in); - break; -#ifdef OS_NETWORK_SUPPORTS_IPV6 - case AF_INET6: - slen = sizeof(struct sockaddr_in6); - break; -#endif - default: - slen = 0; - break; - } + sa = (const struct sockaddr *)&Addr->AddrData; + slen = OS_GetAddrLen(sa->sa_family); if (slen != Addr->ActualLength) { @@ -564,21 +564,8 @@ int32 OS_SocketSendTo_Impl(const OS_object_token_t *token, const void *buffer, s impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token); - sa = (const struct sockaddr *)&RemoteAddr->AddrData; - switch (sa->sa_family) - { - case AF_INET: - addrlen = sizeof(struct sockaddr_in); - break; -#ifdef OS_NETWORK_SUPPORTS_IPV6 - case AF_INET6: - addrlen = sizeof(struct sockaddr_in6); - break; -#endif - default: - addrlen = 0; - break; - } + sa = (const struct sockaddr *)&RemoteAddr->AddrData; + addrlen = OS_GetAddrLen(sa->sa_family); if (addrlen != RemoteAddr->ActualLength) {