Skip to content

Commit

Permalink
Fix nasa#864, Refactor duplicate addrlen swtiches into an inline fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
thnkslprpt committed May 10, 2023
1 parent 5036fed commit aa8459d
Showing 1 changed file with 31 additions and 44 deletions.
75 changes: 31 additions & 44 deletions src/os/portable/os-impl-bsd-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit aa8459d

Please sign in to comment.