This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 48b0e48c net/tcp: set/get TCP_KEEPINTVL/IDLE value as BSD style
48b0e48c is described below

commit 48b0e48cd46ad93fc55b28a612b3943cd492e1de
Author: chao.an <[email protected]>
AuthorDate: Tue May 11 20:20:05 2021 +0800

    net/tcp: set/get TCP_KEEPINTVL/IDLE value as BSD style
    
    Signed-off-by: chao.an <[email protected]>
---
 net/tcp/tcp_getsockopt.c | 102 ++++++++++++++++++-----------------------
 net/tcp/tcp_setsockopt.c | 117 ++++++++++++++++++-----------------------------
 2 files changed, 89 insertions(+), 130 deletions(-)

diff --git a/net/tcp/tcp_getsockopt.c b/net/tcp/tcp_getsockopt.c
index f8dec29..88a2da1 100644
--- a/net/tcp/tcp_getsockopt.c
+++ b/net/tcp/tcp_getsockopt.c
@@ -144,65 +144,51 @@ int tcp_getsockopt(FAR struct socket *psock, int option,
         break;
 
       case TCP_KEEPIDLE:  /* Start keepalives after this IDLE period */
-        if (*value_len < sizeof(struct timeval))
-          {
-            /* REVISIT: POSIX says that we should truncate the value if it
-             * is larger than value_len.   That just doesn't make sense
-             * to me in this case.
-             */
-
-            ret = -EINVAL;
-          }
-        else
-          {
-            FAR struct timeval *tv = (FAR struct timeval *)value;
-
-            if (tv == NULL)
-              {
-                ret        = -EINVAL;
-              }
-            else
-              {
-                /* Convert the KeepIdle time from deciseconds to struct
-                 * timeval.
-                 */
-
-                net_dsec2timeval(conn->keepidle, tv);
-                *value_len = sizeof(struct timeval);
-                ret        = OK;
-              }
-          }
-        break;
-
       case TCP_KEEPINTVL: /* Interval between keepalives */
-        if (*value_len < sizeof(struct timeval))
-          {
-            /* REVISIT: POSIX says that we should truncate the value if it
-             * is larger than value_len.   That just doesn't make sense
-             * to me in this case.
-             */
-
-            ret = -EINVAL;
-          }
-        else
-          {
-            FAR struct timeval *tv = (FAR struct timeval *)value;
-
-            if (tv == NULL)
-              {
-                ret        = -EINVAL;
-              }
-            else
-              {
-                /* Convert the KeepIdle time from deciseconds to struct
-                 * timeval.
-                 */
-
-                net_dsec2timeval(conn->keepintvl, tv);
-                *value_len = sizeof(struct timeval);
-                ret        = OK;
-              }
-          }
+        {
+          int dsecs;
+
+          if (option == TCP_KEEPIDLE)
+            {
+              dsecs = conn->keepidle;
+            }
+          else
+            {
+              dsecs = conn->keepintvl;
+            }
+
+          if (value == NULL)
+            {
+              ret = -EINVAL;
+            }
+          else if (*value_len == sizeof(struct timeval))
+            {
+              FAR struct timeval *tv = (FAR struct timeval *)value;
+
+              /* Convert the KeepIdle time from deciseconds to struct
+               * timeval.
+               */
+
+              net_dsec2timeval(dsecs, tv);
+              *value_len      = sizeof(struct timeval);
+              ret             = OK;
+            }
+          else if (*value_len == sizeof(int))
+            {
+              FAR int *pdsecs = (FAR int *)value;
+              *pdsecs         = dsecs;
+              ret             = OK;
+            }
+          else
+            {
+              /* REVISIT: POSIX says that we should truncate the value if it
+               * is larger than value_len.   That just doesn't make sense
+               * to me in this case.
+               */
+
+              ret = -EINVAL;
+            }
+        }
         break;
 
       case TCP_KEEPCNT:   /* Number of keepalives before death */
diff --git a/net/tcp/tcp_setsockopt.c b/net/tcp/tcp_setsockopt.c
index 5404c80..3ddba92 100644
--- a/net/tcp/tcp_setsockopt.c
+++ b/net/tcp/tcp_setsockopt.c
@@ -138,79 +138,52 @@ int tcp_setsockopt(FAR struct socket *psock, int option,
         break;
 
       case TCP_KEEPIDLE:  /* Start keepalives after this IDLE period */
-        if (value_len != sizeof(struct timeval))
-          {
-            ret = -EDOM;
-          }
-        else
-          {
-            FAR struct timeval *tv = (FAR struct timeval *)value;
-
-            if (tv == NULL)
-              {
-                ret = -EINVAL;
-              }
-            else
-              {
-                unsigned int dsecs;
-
-               /* Get the IDLE time value.  Any microsecond remainder will
-                * be forced to the next larger, whole decisecond value.
-                */
-
-               dsecs = (socktimeo_t)net_timeval2dsec(tv, TV2DS_CEIL);
-               if (dsecs > UINT16_MAX)
-                  {
-                    nwarn("WARNING: TCP_KEEPIDLE value out of range: %u\n",
-                          dsecs);
-                    ret = -EDOM;
-                  }
-                else
-                  {
-                    conn->keepidle = (uint16_t)dsecs;
-                    conn->keeptime = clock_systime_ticks();   /* Reset start 
time */
-                    ret = OK;
-                  }
-              }
-          }
-        break;
-
       case TCP_KEEPINTVL: /* Interval between keepalives */
-        if (value_len != sizeof(struct timeval))
-          {
-            ret = -EDOM;
-          }
-        else
-          {
-            FAR struct timeval *tv = (FAR struct timeval *)value;
-
-            if (tv == NULL)
-              {
-                ret = -EINVAL;
-              }
-            else
-              {
-                unsigned int dsecs;
-
-               /* Get the IDLE time value.  Any microsecond remainder will
-                * be forced to the next larger, whole decisecond value.
-                */
-
-               dsecs = (socktimeo_t)net_timeval2dsec(tv, TV2DS_CEIL);
-               if (dsecs > UINT16_MAX)
-                  {
-                    nwarn("WARNING: TCP_KEEPINTVL value out of range: %u\n",
-                          dsecs);
-                    ret = -EDOM;
-                  }
-                else
-                  {
-                    conn->keepintvl = (uint16_t)dsecs;
-                    conn->keeptime  = clock_systime_ticks();   /* Reset start 
time */
-                    ret = OK;
-                  }
-              }
-          }
+        {
+          unsigned int dsecs;
+
+          if (value == NULL)
+            {
+              return -EINVAL;
+            }
+          else if (value_len == sizeof(struct timeval))
+            {
+              FAR struct timeval *tv = (FAR struct timeval *)value;
+
+              /* Get the IDLE time value.  Any microsecond remainder will
+               * be forced to the next larger, whole decisecond value.
+               */
+
+              dsecs = (socktimeo_t)net_timeval2dsec(tv, TV2DS_CEIL);
+            }
+          else if (value_len == sizeof(int))
+            {
+              dsecs = *(FAR int *)value;
+            }
+          else
+            {
+              return -EDOM;
+            }
+
+          if (dsecs > UINT16_MAX)
+             {
+               nwarn("WARNING: value out of range: %u\n", dsecs);
+               return -EDOM;
+             }
+
+          if (option == TCP_KEEPIDLE)
+            {
+              conn->keepidle = (uint16_t)dsecs;
+            }
+          else
+            {
+              conn->keepintvl = (uint16_t)dsecs;
+            }
+
+          conn->keeptime  = clock_systime_ticks();   /* Reset start time */
+
+          ret = OK;
+        }
         break;
 
       case TCP_KEEPCNT:   /* Number of keepalives before death */

Reply via email to