netPortForwardSet (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

All

Firmware version:

1.74.00


This sets a port forward rule. It has the same effect as setting the port forwarding rules via Device: Network: Port Forward Rules in the RTCU IDE.

When a new rule is set, new connections will be forwarded immediately.

A rule can be removed by setting the src_port to 0.

 

 

Input:

index : INT (1..32)

The index of the rule to set.

 

dst_addr : DINT

This is the internal IP address where the communication is forwarded to.

 

dst_port : DINT

This is the internal port where the communication is forwarded to

 

src_port : DINT (default 0)

This is the external port where the communication is forwarded from.

If this is set to zero the rule will be removed.

 

tcp : BOOL

This determines if TCP communication is forwarded.

 

udp : BOOL

This determines if UDP communication is forwarded.

 

 

Returns:

1

- Success.

0

- Not supported.

-1

- Illegal index.

-2

- Illegal parameter.

 

 

Declaration:

FUNCTION netPortForwardSet;
VAR_INPUT
  index      : INT;
  dst_addr   : DINT;
  dst_port   : DINT;
  src_port   : DINT := 0;
  tcp        : BOOL;
  udp        : BOOL;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR
  _rule_get_   : netPortForwardGet;
END_VAR;
 
FUNCTION pad_string : STRING;
VAR_INPUT
  str      : STRING;
  len      : INT;
END_VAR;
 
  pad_string := str;
  WHILE strLen(str := pad_string) < len DO
    pad_string := pad_string + " ";
  END_WHILE;
 
END_FUNCTION;
 
FUNCTION show_rule
VAR
  str   : STRING;
END_VAR;
 
  // Add index
  IF _rule_get_.index < 10 THEN
    str := pad_string(str := "", len := 3);
  ELSE
    str := pad_string(str := "", len := 2);
  END_IF;
  str := str + intToStr(v := _rule_get_.index);
  str := str + ": ";
 
  IF _rule_get_.status = 1 THEN
    // Add port
    str := str + dintToStr(v := _rule_get_.src_port);
    str := pad_string(str := str, len := 12);
    str := str + "=> ";
 
    // Add dest
    str := str + sockIPToName(ip := _rule_get_.dst_addr);
    str := str + ":";
    str := str + dintToStr(v := _rule_get_.dst_port);
    str := pad_string(str := str, len := 38);
 
    // Add protocol
    IF _rule_get_.tcp THEN str := str + "[TCP]"; END_IF;
    IF _rule_get_.udp THEN str := str + "[UDP]"; END_IF;
 
  ELSE
    str := str + "<NO RULE>";
 
  END_IF;
 
  // Show
  DebugMsg(message := str);
 
END_FUNCTION;
 
FUNCTION show_rules
VAR
  i : INT;
END_VAR;
 
  DebugMsg(message := "------------------------------------------------------------");
  DebugMsg(message := " Get port forwarding rules...");
 
  // Iterate
  FOR i := 1 TO 32 DO
    _rule_get_(index := i);
    show_rule();
 
  END_FOR;
 
END_FUNCTION;
 
PROGRAM example;
VAR
  rc : INT;
END_VAR;
 
  // Set port forward
  rc := netPortForwardSet(
                          index    := 1,
                          dst_addr := sockIPFromName(str := "192.168.1.153"),
                          dst_port := 5020,
                          src_port := 2000,
                          tcp      := TRUE,
                          udp      := FALSE
                         );
  DebugFmt(message := "netPortForwardSet = \1", v1 := rc);
 
  // Show rules
  show_rules();
 
  // Clear port forward
  rc := netPortForwardSet(index := 1, src_port := 0);
  DebugFmt(message := "netPortForwardSet = \1", v1 := rc);
 
  // Show rules
  show_rules();
 
BEGIN
END;
END_PROGRAM;