This function will get information about a route previously set by the netRouteAdd function.
Input:
index : INT (1..25)
The index of the route.
Output:
status : SINT
5
|
- The gateway is unreachable.
|
4
|
- The route is duplicated.
|
3
|
- The route is bad. Can be caused by a netmask that does not match the IP.
|
2
|
- The route is not used. Happens when the interface is not connected.
|
1
|
- The route is used.
|
0
|
- No route found.
|
-1
|
- Invalid parameter.
|
iface : SINT
The network interface to use for the route.
ip : DINT
The destination IP address.
netmask : DINT
The subnet mask.
gateway : DINT
The gateway IP address.
metric : INT
The metric of the route.
Declaration:
FUNCTION_BLOCK netRouteGet;
VAR_INPUT
index : INT;
END_VAR;
VAR_OUTPUT
status : SINT;
ip : DINT;
netmask : DINT;
gateway : DINT;
metric : INT;
iface : SINT;
END_VAR;
Example:
INCLUDE rtcu.inc
VAR
_route_ : netRouteGet;
END_VAR;
FUNCTION show_iface : STRING;
VAR_INPUT
iface : SINT;
END_VAR;
CASE iface OF
1: show_iface := "Cellular";
2: show_iface := "LAN1";
3: show_iface := "LAN2";
4: show_iface := "WLAN";
5: show_iface := "AP";
ELSE
show_iface := "<unknown>";
END_CASE;
END_FUNCTION;
FUNCTION route_list;
VAR
str : STRING;
tmp : STRING;
i : INT;
END_VAR;
DebugMsg(message := " Destination Gateway Subnet mask Metric Iface Status");
FOR i := 1 TO 100 DO
_route_(index := i);
IF _route_.status < 1 THEN EXIT; END_IF;
str := " ";
tmp := sockIPToName(ip := _route_.ip);
WHILE strLen(str := tmp) < 17 DO tmp := tmp + " "; END_WHILE;
str := str + tmp;
tmp := sockIPToName(ip := _route_.gateway);
WHILE strLen(str := tmp) < 17 DO tmp := tmp + " "; END_WHILE;
str := str + tmp;
tmp := sockIPToName(ip := _route_.netmask);
WHILE strLen(str := tmp) < 17 DO tmp := tmp + " "; END_WHILE;
str := str + tmp;
tmp := intToStr(v := _route_.metric);
WHILE strLen(str := tmp) < 8 DO tmp := tmp + " "; END_WHILE;
str := str + tmp;
tmp := show_iface(iface := _route_.iface);
WHILE strLen(str := tmp) < 7 DO tmp := tmp + " "; END_WHILE;
str := str + tmp;
str := str + intToStr(v := _route_.status);
DebugMsg(message := str);
END_FOR;
END_FUNCTION;
PROGRAM example;
VAR
rc : INT;
END_VAR;
rc := netRouteAdd(
index := 1
,iface := 2
,ip := sockIPFromName(str := "10.0.0.0")
,netmask := sockIPFromName(str := "255.0.0.0")
,gateway := sockIPFromName(str := "192.168.1.1")
,metric := 100
);
IF rc < 1 THEN
DebugFmt(message := " netRouteAdd=\1", v1 := rc);
RETURN;
END_IF;
route_list();
rc := netRouteDelete(index := 1);
IF rc < 1 THEN
DebugFmt(message := " netRouteDel=\1", v1 := rc);
RETURN;
END_IF;
BEGIN
END;
END_PROGRAM;
|