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

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

commit 3ca4324a88ae762d581b4e74ad5b68b46dce04ed
Author: Daniel P. Carvalho <[email protected]>
AuthorDate: Sun Oct 18 18:11:27 2026 -0300

    netutils/netlib: add bridge helpers.
    
    Add netlib_add_bridge(), netlib_del_bridge(), netlib_bridge_addif() and
    netlib_bridge_delif(), built when CONFIG_NET_BRIDGE is set.  They wrap the
    SIOCBRADDBR, SIOCBRDELBR, SIOCBRADDIF and SIOCBRDELIF ioctls so that
    applications do not have to open a socket and fill struct ifreq by
    themselves.  They return 0 on success or a negated errno value on failure.
    
    Assisted-by: Claude Code:claude-sonnet-5-5
    Signed-off-by: Daniel P. Carvalho <[email protected]>
---
 include/netutils/netlib.h       |   7 ++
 netutils/netlib/CMakeLists.txt  |   4 +
 netutils/netlib/Makefile        |   4 +
 netutils/netlib/netlib_bridge.c | 183 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 198 insertions(+)

diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h
index d76c93e96..49a99061e 100644
--- a/include/netutils/netlib.h
+++ b/include/netutils/netlib.h
@@ -382,6 +382,13 @@ int netlib_add_vlan(FAR const char *ifname, int vlanid, 
int prio);
 int netlib_del_vlan(FAR const char *vlanif);
 #endif
 
+#ifdef CONFIG_NET_BRIDGE
+int netlib_add_bridge(FAR const char *bridge);
+int netlib_del_bridge(FAR const char *bridge);
+int netlib_bridge_addif(FAR const char *bridge, FAR const char *ifname);
+int netlib_bridge_delif(FAR const char *bridge, FAR const char *ifname);
+#endif
+
 #ifdef CONFIG_NET_ARP
 /* ARP Table Support */
 
diff --git a/netutils/netlib/CMakeLists.txt b/netutils/netlib/CMakeLists.txt
index 0a6735121..27c03f88f 100644
--- a/netutils/netlib/CMakeLists.txt
+++ b/netutils/netlib/CMakeLists.txt
@@ -123,6 +123,10 @@ if(CONFIG_NETUTILS_NETLIB)
     list(APPEND SRCS netlib_addvlan.c netlib_delvlan.c)
   endif()
 
+  if(CONFIG_NET_BRIDGE)
+    list(APPEND SRCS netlib_bridge.c)
+  endif()
+
   list(APPEND SRCS netlib_setmtu.c)
 
   if(CONFIG_WIRELESS_IEEE802154)
diff --git a/netutils/netlib/Makefile b/netutils/netlib/Makefile
index 5867d9190..f606d50d7 100644
--- a/netutils/netlib/Makefile
+++ b/netutils/netlib/Makefile
@@ -128,6 +128,10 @@ ifeq ($(CONFIG_NET_VLAN),y)
 CSRCS += netlib_addvlan.c netlib_delvlan.c
 endif
 
+ifeq ($(CONFIG_NET_BRIDGE),y)
+CSRCS += netlib_bridge.c
+endif
+
 CSRCS += netlib_setmtu.c
 
 ifeq ($(CONFIG_WIRELESS_IEEE802154),y)
diff --git a/netutils/netlib/netlib_bridge.c b/netutils/netlib/netlib_bridge.c
new file mode 100644
index 000000000..e0eda86cd
--- /dev/null
+++ b/netutils/netlib/netlib_bridge.c
@@ -0,0 +1,183 @@
+/****************************************************************************
+ * apps/netutils/netlib/netlib_bridge.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <net/if.h>
+
+#include <nuttx/net/ioctl.h>
+#include <nuttx/net/netconfig.h>
+
+#include "netutils/netlib.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netlib_bridge_cmd
+ *
+ * Description:
+ *   Issue a bridge ioctl that takes a bridge name only (add/delete bridge)
+ *   or a bridge name and an interface (add/delete port).
+ *
+ ****************************************************************************/
+
+static int netlib_bridge_cmd(int cmd, FAR const char *bridge,
+                             FAR const char *ifname)
+{
+  struct ifreq req;
+  int sockfd;
+  int ret;
+
+  if (bridge == NULL)
+    {
+      return -EINVAL;
+    }
+
+  sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
+  if (sockfd < 0)
+    {
+      return -errno;
+    }
+
+  if (ifname == NULL)
+    {
+      ret = ioctl(sockfd, cmd, (unsigned long)(uintptr_t)bridge);
+      if (ret < 0)
+        {
+          ret = -errno;
+        }
+    }
+  else
+    {
+      memset(&req, 0, sizeof(req));
+      strlcpy(req.ifr_name, bridge, IFNAMSIZ);
+      req.ifr_ifindex = if_nametoindex(ifname);
+      if (req.ifr_ifindex == 0)
+        {
+          ret = -errno;
+        }
+      else
+        {
+          ret = ioctl(sockfd, cmd, (unsigned long)(uintptr_t)&req);
+          if (ret < 0)
+            {
+              ret = -errno;
+            }
+        }
+    }
+
+  close(sockfd);
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netlib_add_bridge
+ *
+ * Description:
+ *   Create a bridge.
+ *
+ * Parameters:
+ *   bridge - The name of the bridge to create
+ *
+ * Return:
+ *   0 on success; a negated errno value on failure
+ *
+ ****************************************************************************/
+
+int netlib_add_bridge(FAR const char *bridge)
+{
+  return netlib_bridge_cmd(SIOCBRADDBR, bridge, NULL);
+}
+
+/****************************************************************************
+ * Name: netlib_del_bridge
+ *
+ * Description:
+ *   Delete a bridge.
+ *
+ * Parameters:
+ *   bridge - The name of the bridge to delete
+ *
+ * Return:
+ *   0 on success; a negated errno value on failure
+ *
+ ****************************************************************************/
+
+int netlib_del_bridge(FAR const char *bridge)
+{
+  return netlib_bridge_cmd(SIOCBRDELBR, bridge, NULL);
+}
+
+/****************************************************************************
+ * Name: netlib_bridge_addif
+ *
+ * Description:
+ *   Add a network device to a bridge.
+ *
+ * Parameters:
+ *   bridge - The name of the bridge
+ *   ifname - The name of the network device to add
+ *
+ * Return:
+ *   0 on success; a negated errno value on failure
+ *
+ ****************************************************************************/
+
+int netlib_bridge_addif(FAR const char *bridge, FAR const char *ifname)
+{
+  return ifname == NULL ? -EINVAL :
+         netlib_bridge_cmd(SIOCBRADDIF, bridge, ifname);
+}
+
+/****************************************************************************
+ * Name: netlib_bridge_delif
+ *
+ * Description:
+ *   Remove a network device from a bridge.
+ *
+ * Parameters:
+ *   bridge - The name of the bridge
+ *   ifname - The name of the network device to remove
+ *
+ * Return:
+ *   0 on success; a negated errno value on failure
+ *
+ ****************************************************************************/
+
+int netlib_bridge_delif(FAR const char *bridge, FAR const char *ifname)
+{
+  return ifname == NULL ? -EINVAL :
+         netlib_bridge_cmd(SIOCBRDELIF, bridge, ifname);
+}

Reply via email to