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

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

commit 1924162ff029dc246d01b8574d371030d816ba23
Author: p-szafonimateusz <[email protected]>
AuthorDate: Mon Nov 9 11:51:12 2026 +0100

    net/can: Move the CAN socket bind code to can.c.
    
    Move the bind code of can_raw_bind() to can_conn_bind() and the
    interface index lookup of can_raw_getsockname() to can_get_ifindex()
    in can.c.  Binding a socket to a CAN device does not depend on the CAN
    protocol, so other CAN protocols can use the same code.  No functional
    change.
    
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: p-szafonimateusz <[email protected]>
---
 net/can/can.c     | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/can/can.h     | 23 +++++++++++++++++
 net/can/can_raw.c | 42 ++----------------------------
 3 files changed, 101 insertions(+), 40 deletions(-)

diff --git a/net/can/can.c b/net/can/can.c
index 130f28b0fa8..cd564de8974 100644
--- a/net/can/can.c
+++ b/net/can/can.c
@@ -878,4 +878,80 @@ FAR struct can_conn_s *can_active(FAR struct net_driver_s 
*dev,
   return conn;
 }
 
+/****************************************************************************
+ * Name: can_get_ifindex
+ *
+ * Description:
+ *   Return the interface index of the device a CAN connection is bound to,
+ *   or 0 if it is not bound to a device.
+ *
+ * Input Parameters:
+ *   conn - The CAN connection
+ *
+ ****************************************************************************/
+
+int can_get_ifindex(FAR struct can_conn_s *conn)
+{
+  if (conn->dev != NULL)
+    {
+#ifdef CONFIG_NETDEV_IFINDEX
+      return conn->dev->d_ifindex;
+#else
+      /* Reverse mapping: extract ifindex from device name "can<N>".
+       * This mirrors the bind logic which maps ifindex -> "can<N>".
+       */
+
+      return conn->dev->d_ifname[3] - '0';
+#endif
+    }
+  else
+    {
+      return 0;
+    }
+}
+
+/****************************************************************************
+ * Name: can_conn_bind
+ *
+ * Description:
+ *   Bind a CAN connection to the network device selected by the
+ *   can_ifindex field of a struct sockaddr_can.
+ *
+ * Input Parameters:
+ *   conn    - The connection to bind
+ *   addr    - The struct sockaddr_can address
+ *   addrlen - The length of addr
+ *
+ * Returned Value:
+ *   OK on success, -ENODEV if a non-zero interface index does not match a
+ *   CAN device.
+ *
+ ****************************************************************************/
+
+int can_conn_bind(FAR struct can_conn_s *conn,
+                  FAR const struct sockaddr *addr, socklen_t addrlen)
+{
+  FAR struct sockaddr_can *canaddr;
+
+  DEBUGASSERT(addr != NULL &&
+              addrlen >= sizeof(struct sockaddr_can));
+
+  /* Save the address information in the connection structure */
+
+  canaddr = (FAR struct sockaddr_can *)addr;
+
+  /* Bind CAN device to socket */
+
+#ifdef CONFIG_NETDEV_IFINDEX
+  conn->dev = netdev_findbyindex(canaddr->can_ifindex);
+#else
+  char netdev_name[5] = "can0";
+
+  netdev_name[3] += canaddr->can_ifindex;
+  conn->dev = netdev_findbyname((const char *)&netdev_name);
+#endif
+
+  return conn->dev == NULL && canaddr->can_ifindex != 0 ? -ENODEV : OK;
+}
+
 #endif /* CONFIG_NET_CAN */
diff --git a/net/can/can.h b/net/can/can.h
index 959bbe9103c..91318a71cf9 100644
--- a/net/can/can.h
+++ b/net/can/can.h
@@ -183,6 +183,29 @@ FAR struct can_conn_s *can_alloc(void);
 
 void can_free(FAR struct can_conn_s *conn);
 
+/****************************************************************************
+ * Name: can_conn_bind
+ *
+ * Description:
+ *   Bind a CAN connection to the network device selected by the
+ *   can_ifindex field of a struct sockaddr_can.
+ *
+ ****************************************************************************/
+
+int can_conn_bind(FAR struct can_conn_s *conn,
+                  FAR const struct sockaddr *addr, socklen_t addrlen);
+
+/****************************************************************************
+ * Name: can_get_ifindex
+ *
+ * Description:
+ *   Return the interface index of the device a CAN connection is bound to,
+ *   or 0 if it is not bound to a device.
+ *
+ ****************************************************************************/
+
+int can_get_ifindex(FAR struct can_conn_s *conn);
+
 /****************************************************************************
  * Name: can_conn_next
  *
diff --git a/net/can/can_raw.c b/net/can/can_raw.c
index a88aa360f99..2cf1d5a2f1d 100644
--- a/net/can/can_raw.c
+++ b/net/can/can_raw.c
@@ -1991,29 +1991,7 @@ static void can_raw_addref(FAR struct socket *psock)
 static int can_raw_bind(FAR struct socket *psock,
                         FAR const struct sockaddr *addr, socklen_t addrlen)
 {
-  FAR struct sockaddr_can *canaddr;
-  FAR struct can_conn_s *conn;
-
-  DEBUGASSERT(addr != NULL &&
-              addrlen >= sizeof(struct sockaddr_can));
-
-  /* Save the address information in the connection structure */
-
-  canaddr = (FAR struct sockaddr_can *)addr;
-  conn    = psock->s_conn;
-
-  /* Bind CAN device to socket */
-
-#ifdef CONFIG_NETDEV_IFINDEX
-  conn->dev = netdev_findbyindex(canaddr->can_ifindex);
-#else
-  char netdev_name[5] = "can0";
-
-  netdev_name[3] += canaddr->can_ifindex;
-  conn->dev = netdev_findbyname((const char *)&netdev_name);
-#endif
-
-  return conn->dev == NULL && canaddr->can_ifindex != 0 ? -ENODEV : OK;
+  return can_conn_bind(psock->s_conn, addr, addrlen);
 }
 
 /****************************************************************************
@@ -2063,23 +2041,7 @@ static int can_raw_getsockname(FAR struct socket *psock,
   memset(canaddr, 0, sizeof(struct sockaddr_can));
 
   canaddr->can_family = AF_CAN;
-
-  if (conn->dev != NULL)
-    {
-#ifdef CONFIG_NETDEV_IFINDEX
-      canaddr->can_ifindex = conn->dev->d_ifindex;
-#else
-      /* Reverse mapping: extract ifindex from device name "can<N>".
-       * This mirrors the bind logic which maps ifindex -> "can<N>".
-       */
-
-      canaddr->can_ifindex = conn->dev->d_ifname[3] - '0';
-#endif
-    }
-  else
-    {
-      canaddr->can_ifindex = 0;
-    }
+  canaddr->can_ifindex = can_get_ifindex(conn);
 
   *addrlen = sizeof(struct sockaddr_can);
   return OK;

Reply via email to