ipv6_setsockopt: Merge similarity logic

modify the assignment logic to make it easier to extend new options later

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2023-05-12 15:44:27 +08:00 committed by Xiang Xiao
parent d3b6bbe4fd
commit 62076fb7b9
1 changed files with 16 additions and 51 deletions

View File

@ -73,6 +73,11 @@ int ipv6_setsockopt(FAR struct socket *psock, int option,
ninfo("option: %d\n", option);
if (value == NULL || value_len == 0)
{
return -EINVAL;
}
net_lock();
switch (option)
{
@ -85,14 +90,7 @@ int ipv6_setsockopt(FAR struct socket *psock, int option,
FAR const struct ipv6_mreq *mrec ;
mrec = (FAR const struct ipv6_mreq *)value;
if (mrec == NULL)
{
ret = -EINVAL;
}
else
{
ret = mld_joingroup(mrec);
}
ret = mld_joingroup(mrec);
}
break;
@ -101,32 +99,17 @@ int ipv6_setsockopt(FAR struct socket *psock, int option,
FAR const struct ipv6_mreq *mrec ;
mrec = (FAR const struct ipv6_mreq *)value;
if (mrec == NULL)
{
ret = -EINVAL;
}
else
{
ret = mld_leavegroup(mrec);
}
ret = mld_leavegroup(mrec);
}
break;
case IPV6_MULTICAST_HOPS: /* Multicast hop limit */
{
FAR struct socket_conn_s *conn;
uint8_t ttl;
if (value == NULL || value_len == 0)
{
ret = -EINVAL;
break;
}
ttl = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
conn = psock->s_conn;
conn->ttl = ttl;
conn->ttl = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
ret = OK;
}
break;
@ -147,18 +130,10 @@ int ipv6_setsockopt(FAR struct socket *psock, int option,
case IPV6_UNICAST_HOPS: /* Unicast hop limit */
{
FAR struct socket_conn_s *conn;
uint8_t ttl;
if (value == NULL || value_len == 0)
{
ret = -EINVAL;
break;
}
ttl = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
conn = psock->s_conn;
conn->ttl = ttl;
conn->ttl = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
ret = OK;
}
break;
@ -166,18 +141,10 @@ int ipv6_setsockopt(FAR struct socket *psock, int option,
case IPV6_RECVPKTINFO:
case IPV6_RECVHOPLIMIT:
{
FAR struct socket_conn_s *conn;
int enable;
FAR struct socket_conn_s *conn = psock->s_conn;
int enable = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
if (value == NULL || value_len == 0)
{
ret = -EINVAL;
break;
}
enable = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
conn = psock->s_conn;
if (enable)
{
_SO_SETOPT(conn->s_options, option);
@ -194,10 +161,8 @@ int ipv6_setsockopt(FAR struct socket *psock, int option,
case IPV6_TCLASS:
{
FAR struct socket_conn_s *conn = psock->s_conn;
int tclass;
tclass = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
int tclass = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
/* According to RFC3542 6.5, the interpretation of the integer
* traffic class value is: