ACC SHELL

Path : /usr/share/YaST2/clients/
File Upload :
Current File : //usr/share/YaST2/clients/routing.ycp

/**
 * File:	clients/routing.ycp
 * Package:	Network configuration
 * Summary:	Routing client
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: routing.ycp 51079 2008-09-12 09:35:53Z kmachalkova $
 *
 * Main file for routing configuration.
 * Uses all other files.
 */

{

textdomain "network";

/* The main () */
y2milestone("----------------------------------------");
y2milestone("Routing module started");

import "Label";
import "Routing";
import "Wizard";

import "CommandLine";
import "String";
import "RichText";
import "NetworkService";

/**
 * Return a modification status
 * @return true if data was modified
 */
define boolean Modified() {
    return Routing::Modified();
}

include "network/services/routing.ycp";

/**
 * Main Routing GUI
 */
any RoutingGUI() {
    Routing::Read();

    Wizard::CreateDialog();
    Wizard::SetDesktopIcon("routing");
    Wizard::SetNextButton(`next, Label::FinishButton() );

    /* main ui function */
    any ret = RoutingMainDialog();
    y2debug("ret == %1", ret);

    if(ret == `next && Routing::Modified()) {
	Routing::Write();
	NetworkService::StartStop ();
    }

    UI::CloseDialog();
    return ret;
}

string PrintableRoutingTable( list <map> items )
{
    list <list <string> > table_items = [];
    foreach (map route, items, {
	table_items = add( table_items, 
		[
		  route["destination"]:"",
		  route["gateway"]:"",
		  route["netmask"]:"-",
		  route["device"]:"-",
		  route["extrapara"]:""
		]
	);

    });

    string headline = String::UnderlinedHeader( _("Routing Table"), 0 );
    string table = String::TextTable(
	[
	    _("Destination"),
	    _("Gateway"),
	    _("Netmask"),
	    _("Device"),
	    _("Options")
	],
	table_items,
	$[] );

    return headline + "\n" + table;

}
/**
 * Handler for action "list"
 * @param options action options
 */
define boolean ListHandler( map <string, string> options ) {

    CommandLine::Print( PrintableRoutingTable( Routing::Routes ) );
    CommandLine::Print("");

    return true;
}

define boolean ShowHandler( map <string, string> options ) {

    list <map> routes = filter( map route, Routing::Routes, {
	    return (route["destination"]:"" == options["dest"]:"");
	});

    if ( routes != [] && routes != nil)
    {
        CommandLine::Print( PrintableRoutingTable(routes) );
        CommandLine::Print("");
    }
    else
    {
	CommandLine::Error( sformat(_("No entry for destination '%1' in routing table"), options["dest"]:"") );
	return false;
    }

    return true;
}

define boolean IPFWHandler( map <string, string> options ) {
    CommandLine::Print( String::UnderlinedHeader(_("IP Forwarding:"), 0) );

    if (options["show"]:nil != nil)
    {
	CommandLine::Print("");
	if ( Routing::Forward )
	   CommandLine::Print(_("IP forwarding is enabled"));
	else
	   CommandLine::Print(_("IP forwarding is disabled"));
	CommandLine::Print("");
    }
    else if (options["on"]:nil != nil)
    {
	CommandLine::Print("");
	CommandLine::Print(_("Enabling IP forwarding..."));
	CommandLine::Print("");
	Routing::Forward = true;
    }
    else if (options["off"]:nil != nil)
    {
	CommandLine::Print("");
	CommandLine::Print(_("Disabling IP forwarding..."));
	CommandLine::Print("");
	Routing::Forward = false;
    }
    return true;
}

define boolean AddEditHandler( symbol addedit, map <string, string> options ) {
    list <map> routes = Routing::Routes; 
    string destination = options["dest"]: ""; 
    string gateway = options["gateway"]: ""; 
    string netmask = options["netmask"]: "-";
    string device = options["dev"]: "-";
    string extrapara = options["options"]: "";

    if (addedit == `add)
    {
        if ( destination == "" || gateway == "" ) {
	    CommandLine::Error( _("At least destination and gateway IP addresses must be specified"));
	    return false;
        }

        CommandLine::Print( sformat( _("Adding '%1' destination to routing table ..."), destination) );
        routes = add( routes, 
	      $[
	        "destination" : destination,
	        "gateway" : gateway,
	        "netmask" : netmask,
	        "device" : device,
	        "extrapara" : extrapara,
	      ] );
    }
    else if ( addedit == `edit)
    {
        if ( destination == "") {
	    CommandLine::Error( _("Destination IP address must be specified"));
	    return false;
	}
	if ( size( options) < 2 ) {
	    CommandLine::Error( _("At least one of the following parameters (gateway, netmask, device, options) must be specified"));
	    return false;
	}

	boolean found = false;
	routes = maplist( map m, routes, {
	    if ( m["destination"]:nil == destination )
	    {
		m["gateway"] = gateway;
		m["netmask"] = netmask;
		m["device"] = device;
		m["extrapara"] = extrapara;
		found = true;
	    }
	    return m;
	});

	if (found) {
            CommandLine::Print( sformat( _("Updating '%1' destination in routing table ..."), destination) );
	}
	else {
	    CommandLine::Error( sformat(_("No entry for destination '%1' in routing table"), destination) );
	    return false;
	}
    }

    Routing::Routes = routes;
    return true;
}

define boolean AddHandler( map <string, string> options ) {
    AddEditHandler( `add, options );
    return true;
}
define boolean EditHandler( map <string, string> options ) {
    AddEditHandler( `edit, options );
    return true;
}
define boolean DeleteHandler( map <string, string> options ) {

	boolean found = false;
	Routing::Routes = maplist( map m, Routing::Routes, {
	    if ( m["destination"]:nil == options["dest"]:nil )
		found = true;
	    else
	       return m;
	});

	if (found) {
            CommandLine::Print( sformat( _("Deleting '%1' destination from routing table ..."), options["dest"]:"") );
	    return true;
	}
	else {
	    CommandLine::Error( sformat(_("No entry for destination '%1' in routing table"), options["dest"]:"") );
	    return false;
	}

    return true;
}

/**
 * Command line definition
 */
map cmdline = $[
    /* Commandline help title */
    "help"	: _("Routing Configuration"),
    "id"	: "routing",
    "guihandler": RoutingGUI,
    "initialize": Routing::Read,
    "finish"	: Routing::Write, // FIXME
    "actions"	: $[
	"list" : $[
	    "help"	: _("Show complete routing table"),
	    "handler"	: ListHandler, 
	],
	"show" : $[
	    "help"	: _("Show routing table entry for selected destination"),
	    "handler"	: ShowHandler,
	    "example"	: "show dest=10.10.1.0"
	],
	"ip-forwarding" : $[
	    "help"	: _("IP forwarding settings"),
	    "handler"   : IPFWHandler,
	    "example"	: [
			    "ip-forwarding show",
			    "ip-forwarding on",
			]
	],
	"add" : $[
	    "help"	: _("Add new route"),
	    "handler"	: AddHandler,
	    "example"   : "add dest=10.10.1.0 gateway=10.10.1.1 netmask=255.255.255.0"
	],
	"edit" : $[
	    "help"	: _("Edit an existing route"),
	    "handler"	: EditHandler,
	    "example"   : "edit dest=10.10.1.0 gateway=10.10.1.1 netmask=255.255.255.0"
	],
	"delete" : $[
	    "help"	: _("Delete an existing route"),
	    "handler"	: DeleteHandler,
	    "example"	: "delete dest=10.10.1.0"
	],
    ],
    "options" : $[
	"dest" : $[
	    "type" : "string",
	    "help" : _("Destination IP address"),
	],
	"gateway" : $[
	    "type" : "string",
	    "help" : _("Gateway IP address"),
	],
	"netmask" : $[
	    "type" : "string",
	    "help" : _("Subnet mask")
	],
	"dev" : $[
	    "type" : "string",
	    "help" : _("Network device")
	],
	"options" : $[
	    "type" : "string",
	    "help" : _("Additional options")
	],
	"show" : $[
	    "help" : _("Show current settings")
	],
	"on" : $[
	    "help" : _("Enable IP forwarding")
	],
	"off" : $[
	    "help" : _("Disable IP forwarding")
	],
    ],
    "mappings" : $[
	"show" : [ "dest" ],
	"ip-forwarding" : [ "show", "on", "off" ],
	"add" : [ "dest", "gateway", "netmask", "dev", "options" ],
	"edit" : [ "dest", "gateway", "netmask", "dev", "options" ],
	"delete" : [ "dest" ]
    ]
];

any ret = CommandLine::Run(cmdline);
y2debug("ret=%1", ret);

/* Finish */
y2milestone("Routing module finished");
y2milestone("----------------------------------------");
return ret;

/* EOF */
}

ACC SHELL 2018