netSetDHCPParam (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

1.50.00


This sets the DHCP parameters for a network interface. It has the same effect as setting the parameters via Device: Network: Network Settings.

The DHCP parameters for the network interface are non-volatile and therefore need only to be set once. Parameters take effect after a device resets (by using, for example, the boardReset function), or immediately if the applynow parameter is set.

The parameters set by netSetDHCPParam can be read by using netGetDHCPParam.

 

 

Input:

Iface : SINT

The network interface. (See Network)

 

Enable : BOOL (default FALSE)

Control whether the DHCP server will listen for DHCP requests from the network interface.

 

Applynow : BOOL (default FALSE)

If TRUE, the DHCP server will be restarted, so the parameters are applied. Otherwise the device must be reset for the parameters to be applied.

 

Time : INT (1..24, default 12)

The lease time in hours.

This is the amount of time a connected client has the IP address, before it needs to be renewed.

 

First : DINT

The first IP address in the range of dynamic IP addresses.

 

Last : DINT

The last IP address in the range of dynamic IP addresses.

 

DNS_type : SINT (default 1)

1

- Use the device as DNS server. Requests will be forwarded to the DNS servers from the NAT gateway interface.

2

- Use static DNS servers from the DNS1 and DNS2 parameters below.

 

DNS1 : DINT

The IP address of the primary DNS server.

 

DNS2 : DINT

The IP address of the secondary DNS server.

 

 

Returns:

1

- Success.

0

- Not supported.

-1

- Illegal network interface.

-2

- The network interface is not supported.

-3

- DHCP server can not be enabled if the network interface uses dynamic address

-4

- Illegal lease time.

-5

- The dynamic IP address range is not in the correct subnet.

-6

- The dynamic IP address range is in the wrong order.

-7

- The dynamic IP address range is too small. (10 or more addresses are required)

-8

- The dynamic IP address range includes the interface IP address

-9

- Illegal dns_type selected.

-10

- DNS server IP address is missing.

 

 

Declaration:

FUNCTION netSetDHCPParam;
VAR_INPUT
   Iface      : SINT;
   Enable     : BOOL := FALSE;
   Aplynow    : BOOL := FALSE;
   Time       : INT := 12;
   First      : DINT;
   Last       : DINT;
   DNS_type   : SINT := 1;
   DNS1       : DINT;
   DNS2       : DINT;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR
   ap   : netGetAPParam;
   dhcp : netGetDHCPParam;
   lan  : netGetLANParam;
   nat  : netGetNATParam;
   wpa  : netWLANSecurityWPA;
END_VAR;
 
FUNCTION booltostr : STRING;
VAR_INPUT
   v : BOOL;
END_VAR;
IF v THEN booltostr := "Yes"; ELSE booltostr := "No"; END_IF;
END_FUNCTION
 
FUNCTION setup_lan;
 
   // Configure LAN
   netSetLANParam(iface := 2DHCP := TRUEAutoDNS := TRUE);
 
   // Configure NAT
   netSetNATParam(iface := 2forward := FALSEgateway := TRUE);
 
END_FUNCTION;
 
FUNCTION setup_ap;
 
   // Configure Access Point
   wpa.phrase               := "pass phrase";
   netSetAPParam(
                 ssid       := "RTCU network",
                 sec_config := ADDR(wpa),
                 hidden     := FALSE,
                 chnl       := 1,
                 Address    := sockIPFromName(str := "192.168.1.1"),
                 Subnet     := sockIPFromName(str := "255.255.255.0")
                );
 
   // Configure NAT
   netSetNATParam(iface := 5forward := TRUEgateway := FALSE);
 
   // Configure DHCP
   netSetDHCPParam(
                   iface    := 5,
                   enable   := ON,
                   time     := 24,
                   first    := sockIPFromName(str := "192.168.1.100"),
                   last     := sockIPFromName(str := "192.168.1.200"),
                   dns_type := 1,
                   applynow := TRUE
                  );
 
END_FUNCTION;
 
FUNCTION show_lan;
 
   // Show
   DebugMsg(message := "===== LAN =====");
 
   // Show LAN setup
   lan(iface := 2);
   DebugMsg(message := " Network=      IPv4");
   DebugMsg(message := "   DHCP=           " + booltostr(v := lan.dhcp));
   DebugMsg(message := "   IP=             " + sockIPToName(ip := lan.ip));
   DebugMsg(message := "   Subnet=         " + sockIPToName(ip := lan.SubnetMask));
   DebugMsg(message := "   Gw=             " + sockIPToName(ip := lan.Gateway));
   DebugMsg(message := "   Auto DNS=       " + booltostr(v := lan.AutoDNS));
   DebugMsg(message := "   DNS=            " + sockIPToName(ip := lan.DNS1));
   DebugMsg(message := "   DNS=            " + sockIPToName(ip := lan.DNS2));
 
   // Show NAT setup
   nat(iface := 2);
   DebugMsg(message := " NAT");
   DebugMsg(message := "   forward=        " + booltostr(v := nat.forward));
   DebugMsg(message := "   gateway=        " + booltostr(v := nat.gateway));
 
END_FUNCTION;
 
FUNCTION show_ap;
 
   // Show
   DebugMsg(message := "===== Access Point =====");
 
   // Show AP setup
   ap();
   DebugMsg(message := " SSID=         " + ap.ssid);
   IF ap.security = 0 THEN
      DebugMsg(message := " Security=     None");
   ELSIF ap.security = 1 THEN
      DebugFmt(message := " Security=     WPA/WPA2 PSK");
      netGetAPSecurity(sec_config := ADDR(wpa));
      DebugMsg(message := "   phrase=         " + wpa.phrase);
   ELSE
      DebugFmt(message := " Security=     Unknown (\1)", v1 := ap.security);
   END_IF;
   DebugMsg(message := " Network=      IPv4");
   DebugMsg(message := "   Address=        " + sockIPToName(ip := ap.Address));
   DebugMsg(message := "   Subnet=         " + sockIPToName(ip := ap.Subnet));
 
   // Show NAT setup
   nat(iface := 5);
   DebugMsg(message := " NAT");
   DebugMsg(message := "   forward=        " + booltostr(v := nat.forward));
   DebugMsg(message := "   gateway=        " + booltostr(v := nat.gateway));
 
   // Show DHCP setup
   dhcp(iface := 5);
   DebugMsg(message := " DHCP");
   DebugMsg(message := "   enable=         " + booltostr(v := dhcp.enable));
   DebugFmt(message := "   lease time=     \1 Hours", v1 := dhcp.time);
   DebugMsg(message := "   range, start=   " + sockIPToName(ip := dhcp.first));
   DebugMsg(message := "   range, end=     " + sockIPToName(ip := dhcp.last));
   IF dhcp.dns_type = 1 THEN
      DebugMsg(message := "   DNS type=       Self/Auto");
   ELSIF dhcp.dns_type = 2 THEN
      DebugMsg(message := "   DNS type=       Static");
   ELSE
      DebugFmt(message := "   DNS type=       Unknown (\1)", v1 := dhcp.dns_type);
   END_IF;
   DebugMsg(message := "   DNS=            " + sockIPToName(ip := dhcp.DNS1));
   DebugMsg(message := "   DNS=            " + sockIPToName(ip := dhcp.DNS2));
 
END_FUNCTION;
 
PROGRAM example;
 
// Setup
setup_lan();
setup_ap();
 
// Show
show_lan();
show_ap();
 
// Start
netOpen(iface := 2);
netOpen(iface := 5);
 
BEGIN
  // ...
END;
END_PROGRAM;