Reference Manual

HOW TO control Dial-Up connections via Winsock.

HOW TO use fallback connections for non-stop Internet connectivity.

Introduction

Most current versions of Microsoft® operating systems have no easy way of scripting Dial-Up Networking connections so that features such as fallback connections and Dynamic DNS updates can be implemented. While the full version of NAT32 supports such features (and more), this document describes how to control Dial-Up Networking connections in the (free) Winsock version of NAT32.

Dialing

The following NAT32 command sequence can be used to dial a connection, display its IP address, and then register that IP address at a dynamic DNS registry:

dial MyVPN; dialcfg; wddns nat32.dyndns.org

The above command sequence dials the connection called MyVPN, then executes command dialcfg to display the assigned IP address, and then executes a script called wddns to register the name nat32vpn.dyndns.org at the Dynamic DNS website. The following screen output is produced:

Dialing...
[MyVPN] connected
137.92.11.80
DynDNS: good 137.92.11.80

The needed wddns Tcl script is shown below:

#!tcl

#
# Update a DYNDNS.ORG entry via WINSOCK
#
# Usage: wddns fqdn
#
# Note: usercode and password must be set below
#

# Check args

if {$argc != 1} {
    error "Usage: wddns fqdn"
}

# Get arg

set fqdn     [lindex $argv 0]
set ip       [exec dialcfg]
set usercode "sample"
set password "password"

# Send the needed GET request

set request "http://$usercode:$password@members.dyndns.org/nic/update?hostname=$fqdn&myip=$ip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
set response [exec "url \"$request\""]
echo "DynDNS: $response"

Fallback Connections

Most Internet connection technologies are normally quite reliable, but in cases where 100% uptime is required, it may be useful to have NAT32 automatically switch to another connection whenever your main Internet connection goes down. Conversely, when the main connection is working again, NAT32 should automatically switch back to using that connection.

Many DSL service providers have a Dialup Modem service that can be used when the DSL service is down. Some users may even have two or more DSL or Cable Modem connections, and one of those connections can be designated as a fallback connection that is to be used only if and when the main Internet connection is down.

Checking Internet Connectivity

NAT32 can check Internet connectivity on a specified interface with the command:

wchecki ix [ttl [ip]]

Argument ix stands for the MSMIB index of the interface to be checked, and can be determined by running the msmib command.

The wchecki command checks connectivity by sending a UDP packet with an IP TTL value of ttl (default 2) to the host specified by ip (default 128.10.2.1) via the interface with the index ix.

To ensure that the packet leaves via the specified interface, a host-specific route pointing to the gateway on the specified interface is first added to the Windows Routing Table.

A raw socket is used to receive the expected ICMP error message from the gateway that is ttl hops away. When that message is received, it means that the ISP's network is up and running and the received ICMP Type and Code fields are printed. If the ISP's network is down, no ICMP packet will be forthcoming and the receive will time out.

The added routing table entry is then deleted and the command returns to its caller.

Dialing a Fallback Connection

The following Tcl script can be used to check connectivity of interface ix and dial a fallback connection if the main connection is down. When connectivity for interface ix is restored, the script will hangup the fallback connection.

#!tcl

#
# wchecki - check a specified interface for Internet connectivity.
#           Dial a specified connection if no connectivity is available.
#           Hangup the specified connection when connectivity is restored.
#
#           WINSOCK version
#

if {$argc != 2} {
    error "Usage: wchecki ix connection"
}

set ix [lindex $argv 0]
set connection [lindex $argv 1]

set status [exec "wchecki $ix"]

if {$status == "ICMP Type 11 Code 0"} {
    set connected [exec dialcfg]
    if {$connected != "0.0.0.0"} {
        set status [exec "hangup $connection"]
        echo $status
    }
} else {
    set status [exec "dial $connection"]
    echo $status
}

Copy the above code to file wchecki.tcl in your NAT32 directory.

Switching DSL Connections

While Windows will change gateways if more than one Internet interface is available, it only does so if it detects problems with the current gateway. That means that once a change has been made, things will stay that way until problems are encountered with that gateway.

Some users may want to use a particular connection whenever possible and only switch to an alternate connection for the duration of an outage. Once the outage has been resolved, the system should switch back to the original connection.

NAT32 supports this via the Tcl script wcheckb.tcl shown below:

#!tcl

#
# wcheckb - check the main interface for Internet connectivity.
#           Switch to the backup interface if the main one is down.
#           Switch to the main interface when connectivity is restored.
#
# NOTE: The following external variables MUST be valid:
#
#       router1 = main   gateway IP
#       router2 = backup gateway IP
#       ix1     = main   interface index (MSMIB)
#       ix2     = backup interface index (MSMIB)
#

set ix_main $ix1
set ix_back $ix2

set gw_main $router1
set gw_back $router2

set ip_main [exec "wgetip $ix_main"]
set ip_back [exec "wgetip $ix_back"]

# Consistency checks

if {$ix_main == 0} {
    error "ERROR: ix1 is 0"
}

if {$ix_back == 0} {
    error "ERROR: ix2 is 0"
}

if {$gw_main == ""} {
    error "ERROR: router1 is invalid"
}

if {$gw_back == ""} {
    error "ERROR: router2 is invalid"
}

set status [exec "wchecki $ix_main"]

if {$status == "ICMP Type 11 Code 0"} {
    set status [exec "winroute delete 0.0.0.0 0.0.0.0 $gw_back"]
    set status [exec "winroute add 0.0.0.0 0.0.0.0 $gw_main $ip_main"]
} else {
    set status [exec "winroute delete 0.0.0.0 0.0.0.0 $gw_main"]
    set status [exec "winroute add 0.0.0.0 0.0.0.0 $gw_back $ip_back"]
}

The above script uses the following NAT32 variables:

    router1 0.0.0.0     // IP of the main external router
    router2 0.0.0.0     // IP of a backup external router

    ix1                 // MSMIB router1 interface index
    ix2                 // MSMIB router2 interface index

While NAT32 does attempt to set these variables when it starts, you should set them exactly as needed by adding the following commands to file wuser.txt:

wgetgw ix | set router1
set ix1 ix
wgetgw ix | set router2
set ix2 ix

In the above commands, ix stands for the desired MSMIB index that you can determine by running the msmib command and examining its output. You need do this only once, as an interface index does generally not change once the hardware has been configured.

In FULL Mode, NAT32 supports load balancing for multiple Internet interfaces, and the best way to switch connections is as described here.

Running Scripts with CRON

To make NAT32 run a script at regular intervals, add lines like the following to your crontab file:

* * * * * wchecki.tcl 2 MyConnection

The above command will check interface 2 every minute and dial MyConnection if the TIMEOUT response is received. Be sure to specify the correct interface number for your system. Command msmib can be used to display all Windows interface settings.

* * * * * wcheckb.tcl

The above command will check the main gateway every minute and switch to the backup gateway if the TIMEOUT response is received. Again, be sure to use the correct interface numbers for your system.

Both scripts will revert to the original connection once it is working again.

The CRON daemon is best started with a command such as the following in your wuser.txt file:

cron on wcron

The above command will start CRON and instruct it to obey the rules in the crontab file wcron.

SEE ALSO

Cron, Dial, Hangup, Tcl
[Back]