You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/10/27 14:28:07 UTC

(nuttx) branch master updated: simwifi: host wlan0 obtains ip and set dns for wlan0 in the defwan wlan0

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/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 41762eb187 simwifi: host wlan0 obtains ip and set dns for wlan0 in the defwan wlan0
41762eb187 is described below

commit 41762eb187bd50e1a77ba606fb8bcfd1c94fd936
Author: liqinhui <li...@xiaomi.com>
AuthorDate: Thu Oct 19 15:53:14 2023 +0800

    simwifi: host wlan0 obtains ip and set dns for wlan0 in the defwan wlan0
    
    Signed-off-by: liqinhui <li...@xiaomi.com>
---
 tools/simwifi/sim_wifi.sh   |  8 ++++++--
 tools/simwifi/udhcpc.script | 30 ++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/tools/simwifi/sim_wifi.sh b/tools/simwifi/sim_wifi.sh
index 6858b158f7..5b7c785ece 100755
--- a/tools/simwifi/sim_wifi.sh
+++ b/tools/simwifi/sim_wifi.sh
@@ -33,7 +33,7 @@ HOSTAPD_CONF_FILE="$RUN_DIR/hostapd.conf"
 DNSMASQ_PID="$RUN_DIR/dnsmasq.pid"
 DNSMASQ_CONF="$RUN_DIR/dnsmasq.conf"
 UDHCPC_PID="$RUN_DIR/udhcpc.pid"
-UDHCPC_SCRIPT="$RUN_DIR/udhcpc.script"
+UDHCPC_SCRIPT="/var/udhcpc.script"
 
 LOG_FILE="$RUN_DIR/simwifi.log"
 STATE_FILE="$RUN_DIR/simwifi.state"
@@ -151,6 +151,7 @@ init_env()
 
   cp -fr $CUR_DIR/dnsmasq.conf $DNSMASQ_CONF
   cp -fr $CUR_DIR/udhcpc.script $UDHCPC_SCRIPT
+  chmod +x $UDHCPC_SCRIPT
 }
 
 # Rename the interface name
@@ -337,6 +338,8 @@ del_gw_wlan()
     ip route del $router
     sw_dbg 1 "del the default router on $wlan_if"
   }
+
+  ifconfig $wlan_if 0.0.0.0
 }
 
 stop_sta()
@@ -512,7 +515,7 @@ init()
   echo "mode:$2" >> $DEFCONF_FILE
   [ "$2" = "hwsim" ] &&  modprobe  mac80211_hwsim
 
-  set_state SW_INIT
+  set_state SW_INIT  "" $NUTTX_BR_IF $1
 }
 
 clean()
@@ -523,6 +526,7 @@ clean()
   [ "$cur_mode" = "hwsim" ] &&  modprobe -r mac80211_hwsim
 
   rm -fr $RUN_DIR
+  rm -f $UDHCPC_SCRIPT
 }
 
 usage()
diff --git a/tools/simwifi/udhcpc.script b/tools/simwifi/udhcpc.script
index 06343e06e4..fc98ffae7e 100644
--- a/tools/simwifi/udhcpc.script
+++ b/tools/simwifi/udhcpc.script
@@ -10,6 +10,8 @@ log() {
   logger -t "udhcpc[$PPID]" -p daemon.$1 "$interface: $2"
 }
 
+SIMWIFILOG_FILE=/var/run/simwifi/simwifi.log
+DEFCONF_FILE=/var/run/simwifi/simwifi.conf
 case $1 in
   bound|renew)
 
@@ -19,6 +21,34 @@ case $1 in
   busybox ifconfig $interface ${mtu:+mtu $mtu} \
       $ip netmask $subnet ${broadcast:+broadcast $broadcast}
 
+  # defwan equals to wlanx, should add router and DEF_NS
+
+  defwan=$(cat $DEFCONF_FILE | grep defwan | awk -F':' '{print $2}')
+  [ ".$defwan" = ".wlan0" ] && {
+    # get current ("old") routes (after setting new IP)
+    crouter=$(busybox ip -4 route show dev $interface |
+              busybox awk '$1 == "default" { print $3; }')
+    router="${router%% *}" # linux kernel supports only one (default) route
+    if [ ".$router" != ".$crouter" ]; then
+        # reset just default routes
+        busybox ip -4 route flush exact 0.0.0.0/0 dev $interface
+    fi
+
+    if [ -n "$router" ]; then
+        # special case for /32 subnets: use onlink keyword
+        [ ".$subnet" = .255.255.255.255 ] \
+            && onlink=onlink || onlink=
+        busybox ip -4 route add default via $router dev $interface $onlink
+        log info "udhcpc add router $router on $interfac"
+    fi
+
+    DEF_NS="nameserver 8.8.8.8"
+    RESOLV_CONF_FILE=${RESOLV_CONF:-/etc/resolv.conf}
+    [ -f $RESOLV_CONF_FILE -a -z "$(cat $RESOLV_CONF_FILE | grep 8.8.8.8)" ] && {
+        echo "$DEF_NS" >> "$RESOLV_CONF_FILE"
+    }
+  }
+
   log info "$1: IP=$ip/$subnet router=$router domain=\"$domain\" dns=\"$dns\" lease=$lease"
   ;;