Commit 296bbc27 authored by Jondy Zhao's avatar Jondy Zhao

Use ip script to add/remove Openvpn tap driver in the Cygwin.

parent 5f4fa13d
...@@ -291,13 +291,20 @@ function prefix_to_netmask() ...@@ -291,13 +291,20 @@ function prefix_to_netmask()
echo $result echo $result
} }
#
# Parameter:
# ifname: interface name or guid name
#
# If ifname is guid name, return the corresponding connection name,
# otherwise return the original ifname.
#
function format_interface_name() function format_interface_name()
{ {
if [[ "$1" == "" ]]; then if [[ "$1" == "" ]]; then
return 1 return 1
fi fi
local guid=$1 local guid="$1"
if ! [[ "${guid:0:1}" == "{" ]] ; then if ! [[ "${guid:0:1}" == "{" ]] ; then
echo $1 echo $1
else else
...@@ -306,6 +313,157 @@ function format_interface_name() ...@@ -306,6 +313,157 @@ function format_interface_name()
fi fi
} }
#
# Parameter:
# ifname: connection name
#
# Add a TAP-WINDOWS driver of Openvpn, then rename the connection name
# as ifname.
#
function install_tap_driver()
{
local FILENAME="/etc/openvpn/driver/OemWin2k.inf"
local DEVFILE=$(cygpath -w $FILENAME)
local DEVCON=$(which devcon.exe)
local HWID=tap0901
local CHECKSCRIPT=$(cygpath -m /etc/openvpn/check_driver_signing_dialog.vbs)
local CSCRIPT=$(which cscript)
local GETSCRIPT=$(cygpath -m /etc/openvpn/get_last_connection.vbs)
if [[ ! -f $FILENAME ]] ; then
echo "Error: no TAP-WINDOWS driver inf file found"
return 1
fi
if [[ ! -x $DEVCON ]] ; then
echo "Error: no devcon.exe found"
return 1
fi
if [[ ! -x $CSCRIPT ]] ; then
echo "Error: no cscript.exe found"
return 1
fi
if ! [[ -f $CHECKSCRIPT ]] ; then
cat <<EOF > $CHECKSCRIPT
Set oShell = CreateObject("WScript.Shell")
Do
If oShell.AppActivate("Hardware Installation") Then
WScript.Sleep 1000
oShell.SendKeys "%C"
WScript.Sleep 2000
End If
WScript.Sleep 1000
Loop While True
EOF
fi
# install driver
$CSCRIPT $CHECKSCRIPT > /dev/null &
local sid=$!
$DEVCON install $DEVFILE $HWID
kill $sid
# rename the connection name
if [[ ! "$1" == "" ]] ; then
if [[ ! -f $GETSCRIPT ]] ; then
cat <<EOF > $GETSCRIPT
strComputer = "."
strPrefix = "Local Area Connection"
Set objWMIService = GetObject("winmgmts:\\\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT NetConnectionID FROM Win32_NetworkAdapter WHERE " & _
"NetConnectionID Like '" & strPrefix & "%'", _
"WQL", 48)
strLastConnectionName = ""
i = -1
k = Len(strPrefix)
For Each objItem In colItems
s = Right(objItem.NetConnectionID, Len(objItem.NetConnectionID) - k)
If s = "" Then
j = 0
Else
j = Int(s)
End If
If j > i Then
strLastConnectionName = objItem.NetConnectionID
i = j
End If
Next
WScript.StdOut.Write(strLastConnectionName)
WScript.Quit(0)
EOF
fi
local OLDNAME=$($CSCRIPT //Nologo $GETSCRIPT)
if (( $? == 0 )) ; then
netsh interface set interface name="$OLDNAME" newname="$1"
fi
fi
}
#
# Parameter:
# ifname: connection name
#
# Remove a TAP-WINDOWS driver of Openvpn which connection name equals
# ifname.
#
function uninstall_tap_driver()
{
local DEVCON=$(which devcon.exe)
local CSCRIPT=$(which cscript.exe)
local GETSCRIPT=$(cygpath -m /etc/openvpn/get_pnpid_connection.vbs)
if [[ "$1" == "" ]] ; then
echo "Error: missing connection name"
return 1
fi
local IFNAME=$1
if [[ ! -x $DEVCON ]] ; then
echo "Error: no devcon.exe found"
return 1
fi
if [[ ! -x $CSCRIPT ]] ; then
echo "Error: no cscript.exe found"
return 1
fi
if [[ ! -f $GETSCRIPT ]] ; then
cat <<EOF > $GETSCRIPT
If WScript.Arguments.Count < 1 Then
WScript.Echo "Error: missing paramter connection name"
WScript.Quit(1)
End If
strComputer = "."
strDriverName = "TAP-Windows Adapter V9"
strConnectionName = WScript.Arguments(0)
Set objWMIService = GetObject("winmgmts:\\\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery(_
"SELECT PNPDeviceID FROM Win32_NetworkAdapter WHERE Name='" _
& strDriverName & "' AND NetConnectionID='" & strConnectionName & "'", _
"WQL", 48)
strDeviceID = ""
For Each objItem In colItems
strDeviceID = objItem.PNPDeviceID
Next
WScript.StdOut.Write(strDeviceId)
WScript.Quit(0)
EOF
fi
local PNPDEVICEID=$($CSCRIPT //Nologo "$GETSCRIPT" "$IFNAME")
if (( $? == 0 )) ; then
$DEVCON remove =Net "@$PNPDEVICEID"
return $?
fi
}
orig_cmd="$0 $*" orig_cmd="$0 $*"
opt_family= opt_family=
opt_statistics=0 opt_statistics=0
...@@ -338,12 +496,8 @@ while [[ "$1" != "" ]] && [[ "$object" == "" ]] ; do ...@@ -338,12 +496,8 @@ while [[ "$1" != "" ]] && [[ "$object" == "" ]] ; do
-s | -stats | -statistics) -s | -stats | -statistics)
let opt_statistics+=1 let opt_statistics+=1
;; ;;
tuntap)
echo $orig_cmd
exit 0
;;
link | addr | addrlabel | route | rule | neigh | tunnel | \ link | addr | addrlabel | route | rule | neigh | tunnel | \
maddr | mroute | monitor) maddr | mroute | monitor | tuntap)
object=$1 object=$1
;; ;;
*) echo Warning: unsupport options "$1" *) echo Warning: unsupport options "$1"
...@@ -402,7 +556,7 @@ elif [[ $object == "addr" ]] ; then ...@@ -402,7 +556,7 @@ elif [[ $object == "addr" ]] ; then
while [[ "$1" != "" ]] ; do while [[ "$1" != "" ]] ; do
case $1 in case $1 in
dev) dev)
dev=\"$(format_interface_name $2)\" dev=$(format_interface_name "$2")
shift shift
;; ;;
*) echo Warning: unsupport parameter "$1" *) echo Warning: unsupport parameter "$1"
...@@ -411,7 +565,7 @@ elif [[ $object == "addr" ]] ; then ...@@ -411,7 +565,7 @@ elif [[ $object == "addr" ]] ; then
shift shift
done done
ipcmd="$ipcmd $dev $address" ipcmd="$ipcmd \"$dev\" $address"
elif [[ $object == "addrlabel" ]] ; then elif [[ $object == "addrlabel" ]] ; then
...@@ -520,7 +674,7 @@ elif [[ $object == "route" ]] ; then ...@@ -520,7 +674,7 @@ elif [[ $object == "route" ]] ; then
while [[ "$1" != "" ]] ; do while [[ "$1" != "" ]] ; do
case $1 in case $1 in
dev) dev)
interface=\"$(format_interface_name $2)\" interface=$(format_interface_name "$2")
shift shift
;; ;;
proto) proto)
...@@ -565,8 +719,37 @@ elif [[ $object == "route" ]] ; then ...@@ -565,8 +719,37 @@ elif [[ $object == "route" ]] ; then
fi fi
fi fi
ipcmd="$ipcmd $prefix $interface $nexthop" ipcmd="$ipcmd $prefix \"$interface\" $nexthop"
elif [[ $object == "tuntap" ]] ; then
while [[ "$1" != "" ]] ; do
case $1 in
dev)
dev=$(format_interface_name "$2")
shift
;;
mode)
mode=$2
shift
;;
*) echo Warning: unsupport parameter "$1"
esac
shift
done
if [[ "$command" == "add" ]] ; then
install_tap_driver "$dev"
exit $?
elif [[ "$command" == "del" ]] ; then
uninstall_tap_driver "$dev"
exit $?
else
echo $orig_cmd
echo "Error: unsupported command \"$command\" for tuntap"
exit 1
fi
# elif [[ $object == "rule" ]] ; then # elif [[ $object == "rule" ]] ; then
# echo "Error: unsupported ip object \"$object\" in the Cygwin" # echo "Error: unsupported ip object \"$object\" in the Cygwin"
# exit 1 # exit 1
...@@ -593,4 +776,3 @@ fi ...@@ -593,4 +776,3 @@ fi
echo "Mapped to: $ipcmd" echo "Mapped to: $ipcmd"
$ipcmd $ipcmd
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment