Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sdk:set-default-route-based-on-current-ssid [2014/11/26 17:26] (current)
Line 1: Line 1:
 +====== Set fake default route dynamically based on SSID ======
 +This script checks every 5 seconds which interface should be used as default gateway. As the default gateway is managed by the link-manager,​ a fake default gateway is configured by this script. \\
 +Conditions could be any, here it depend on which WLAN SSID the router is connected.
 +<code c set-route.are>​
 +/* DESC: set route to server depending on active WWAN / WLAN network
 + * Copyright (C) 2014 NetModule AG
 + *
 + */
 + 
 +nb_syslog("​starting"​);​
 + 
 +void restartIPsec(){ ​
 +    nb_config_set("​ipsec.status=0"​);​
 +    sleep(6); // lower valus result in disabled ipsec admin status
 +    nb_config_set("​ipsec.status=1"​);​
 +}
 +void addRoute(int id, string interface, string destination,​ string netmask, string gateway){ ​
 +    route = strcat("​route."​ ,id, "​.metric=",​ 0);
 +    route = strcat(route,​ " route."​ ,id, "​.interface=",​ interface);
 +    route = strcat(route,​ " route."​ ,id, "​.target=",​ destination);​
 +    route = strcat(route,​ " route."​ ,id, "​.netmask=",​ netmask);
 +    route = strcat(route,​ " route."​ ,id, "​.gateway=",​ gateway);
 +    nb_config_set(route);​
 +}   
  
 +
 +lastWanInterface = "";​
 +while (1) {
 +    wanInterface = "";​
 +    status = nb_status("​wan"​);​
 +    wwan_status = status.WANLINK1_STATE;​
 +    wlan_status = status.WANLINK2_STATE;​
 +    wlan_network = status.WANLINK2_NETWORK;​
 +    SERVER = nb_config_get("​ipsec.0.remote.serverIp"​);​
 +    nb_syslog("​WLAN is %s with %s", wlan_status,​ wlan_network);​
 +    nb_syslog("​WWAN is %s", wwan_status);​
 +    nb_syslog("​routing traffic over %s", lastWanInterface); ​
 +
 +    if (wlan_status == "​up"​ && wlan_network == "​kis"​) { 
 +        // take wlan link for IPsec
 +        wanInterface = "​wlan0"; ​  
 +    } else if (wwan_status == "​up"​ || wlan_status == "​up"​ && wlan_network == "​video"​) { 
 +        // take mobile link for IPsec
 +        wanInterface = "​wwan0";​
 +    }
 +    if (wanInterface != ""​ && wanInterface != lastWanInterface) {
 +        if (wanInterface == "​wlan0"​) {
 +            addRoute(0, wanInterface,​ SERVER, "​255.255.255.255",​ struct_get(status,​ "​WANLINK2_GATEWAY"​));​
 +            addRoute(1, wanInterface,​ "​0.0.0.0",​ "​128.0.0.0",​ struct_get(status,​ "​WANLINK2_GATEWAY"​));​
 +            addRoute(2, wanInterface,​ "​128.0.0.0",​ "​128.0.0.0",​ struct_get(status,​ "​WANLINK2_GATEWAY"​));​
 +            nb_syslog("​routing traffic over wlan"​); ​
 +        } else if (wanInterface == "​wwan0"​) {    ​
 +            addRoute(0, wanInterface,​ SERVER, "​255.255.255.255",​ struct_get(status,​ "​WANLINK1_GATEWAY"​)); ​
 +            addRoute(1, wanInterface,​ "​0.0.0.0",​ "​128.0.0.0",​ struct_get(status,​ "​WANLINK1_GATEWAY"​));​
 +            addRoute(2, wanInterface,​ "​128.0.0.0",​ "​128.0.0.0",​ struct_get(status,​ "​WANLINK1_GATEWAY"​));​
 +            nb_syslog("​routing traffic over mobile"​); ​
 +        }
 +        restartIPsec();​
 +        lastWanInterface = wanInterface;​
 +    }
 +    sleep(5);
 +}
 +nb_syslog("​exit"​);​
 +exit(0);
 +</​code>​