xiaoxiang781216 commented on code in PR #3876:
URL: https://github.com/apache/nuttx-apps/pull/3876#discussion_r4313599871


##########
netutils/wifibridge/wifibridge.c:
##########
@@ -0,0 +1,351 @@
+/****************************************************************************
+ * apps/netutils/wifibridge/wifibridge.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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/socket.h>
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <net/if.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include "netutils/netlib.h"
+#include "netutils/wifibridge.h"
+#include "wireless/wapi.h"
+
+#ifdef CONFIG_NETUTILS_WIFIBRIDGE_DHCPD
+#  include "netutils/dhcpd.h"
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: wifibridge_setup_softap
+ *
+ * Description:
+ *   Put the Wi-Fi interface in master mode with the requested security,
+ *   channel and SSID.
+ *
+ ****************************************************************************/
+
+static int wifibridge_setup_softap(int sock,
+                                   FAR const struct wifibridge_config_s *cfg)
+{
+  size_t passlen = strnlen(cfg->passphrase, 64);
+  int version;
+  int ret;
+
+  if (passlen < 8 || passlen > 63 || cfg->channel < 1 || cfg->channel > 13)
+    {
+      return -EINVAL;
+    }
+
+  version = cfg->security == WIFIBRIDGE_SEC_WPA3 ?
+            IW_AUTH_WPA_VERSION_WPA3 : IW_AUTH_WPA_VERSION_WPA2;
+
+  ret = wapi_set_mode(sock, cfg->wlanif, WAPI_MODE_MASTER);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = wpa_driver_wext_set_auth_param(sock, cfg->wlanif,
+                                       IW_AUTH_WPA_VERSION, version);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = wpa_driver_wext_set_auth_param(sock, cfg->wlanif,
+                                       IW_AUTH_CIPHER_PAIRWISE,
+                                       IW_AUTH_CIPHER_CCMP);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = wpa_driver_wext_set_key_ext(sock, cfg->wlanif, WPA_ALG_CCMP,
+                                    cfg->passphrase, passlen);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = wapi_set_freq(sock, cfg->wlanif, cfg->channel, WAPI_FREQ_FIXED);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return wapi_set_essid(sock, cfg->wlanif, cfg->ssid, WAPI_ESSID_ON);
+}
+
+/****************************************************************************
+ * Name: wifibridge_teardown
+ *
+ * Description:
+ *   Undo the steps of wifibridge_start() in reverse order.  Each step is
+ *   tried even if an earlier one failed, since this also cleans up a
+ *   partially started bridge.
+ *
+ ****************************************************************************/
+
+static int wifibridge_teardown(int sock,
+                               FAR const struct wifibridge_config_s *cfg)
+{
+  int ret = OK;
+
+#ifdef CONFIG_NETUTILS_WIFIBRIDGE_DHCPD
+  dhcpd_stop();
+#endif
+
+  netlib_ifdown(cfg->bridge);
+
+  netlib_bridge_delif(cfg->bridge, cfg->wlanif);
+  netlib_bridge_delif(cfg->bridge, cfg->ethif);
+
+  if (netlib_del_bridge(cfg->bridge) < 0)
+    {
+      ret = -errno;
+    }
+
+  /* Bringing the interface down does not stop the AP, an ESSID set with the
+   * off flag does.
+   */
+
+  wapi_set_essid(sock, cfg->wlanif, "", WAPI_ESSID_OFF);
+  wapi_set_ifdown(sock, cfg->wlanif);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: wifibridge_isup
+ ****************************************************************************/
+
+static bool wifibridge_isup(int sock, FAR const char *ifname)
+{
+  int is_up;
+
+  return wapi_get_ifup(sock, ifname, &is_up) >= 0 && is_up != 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: wifibridge_start
+ ****************************************************************************/
+
+int wifibridge_start(FAR const struct wifibridge_config_s *config)
+{
+  struct in_addr none;
+  struct in_addr addr;
+  int sock;
+  int ret;
+
+  if (config == NULL || config->bridge == NULL || config->ethif == NULL ||
+      config->wlanif == NULL || config->ssid == NULL ||
+      config->passphrase == NULL)
+    {
+      return -EINVAL;
+    }
+
+  if (if_nametoindex(config->bridge) != 0)
+    {
+      return -EEXIST;

Review Comment:
   sorroy, my mistake.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to