SDK Script set-ipsec-route.are

set-ipsec-route.are
/* DESC: set route to IPSEC server depending on active WWAN / WLAN network
 * Copyright (C) 2014 NetModule AG, Switzerland
 *
 */
 
SERVER = nb_config_get("ipsec.0.remote.serverIp");
 
if (strlen(SERVER) == 0) {
    nb_syslog("no IPSEC server configured");
    exit(1);
}
 
 
nb_syslog("starting");
oldroute = "";
 
while (1) {
    route = "";
    status = nb_status("wan");
 
    wwan_status = struct_get(status, "WANLINK1_STATE");
    wlan_status = struct_get(status, "WANLINK2_STATE");
    wlan_network = struct_get(status, "WANLINK2_NETWORK");
 
    if (wlan_status == "up" && wlan_network == "a") {
        nb_syslog("WLAN is up with %s", wlan_network);
        route = sprintf(
                "route.0.metric=0 route.0.interface=wlan0 route.0.target=%s route.0.netmask=255.255.255.255 route.0.gateway=%s", 
                SERVER, struct_get(status, "WANLINK2_GATEWAY")
                );
 
    } else if (wwan_status == "up") {
        nb_syslog("WWAN is up");
        route = sprintf(
                "route.0.metric=0 route.0.interface=wwan0 route.0.target=%s route.0.netmask=255.255.255.255 route.0.gateway=%s", 
                SERVER, struct_get(status, "WANLINK1_GATEWAY")
                );
 
    } else if (wlan_status == "up" && wlan_network == "b") { 
        nb_syslog("WLAN is up with %s", wlan_network);
        route = sprintf(
                "route.0.metric=0 route.0.interface=wlan0 route.0.target=%s route.0.netmask=255.255.255.255 route.0.gateway=%s", 
                SERVER, struct_get(status, "WANLINK2_GATEWAY")
                );
    }
 
    if (route != "") {
        if (route == oldroute) {
            nb_syslog("route didn't change");
        } else {
            nb_syslog("setting: %s", route);
            nb_config_set(route);
            oldroute = route;
            sleep(3);
            nb_restart("ipsec");
        }
    }
 
    sleep(5);
}
 
nb_syslog("exit");
exit(0);