Modifying Network Configuration Settings at Run Time

network config

Most applications at boot will get a DHCP or static IP address and not dynamically change between them. Once network configuration settings have been assigned, typically they remain until the device is reset. But sometimes the network configuration settings need to be configured and modified at run time. This article describes several ways to manipulate the network configuration at run time.

Flash vs RAM Network Settings

Before modifying the network configuration, it is important to know the difference between reading and writing the configuration in RAM or in Flash. The NetBurner device contains runtime and stored (Flash) system configuration parameters. Interface Blocks are used at runtime, and Configuration Records are stored in Flash. At boot time, data from the Configuration Records are copied to Interface Blocks.

There are 3 Configuration Records as defined in netinterfaces.h:

 #define CONFIG_IF_ID_ETHERNET  (0)
 #define CONFIG_IF_ID_WIFI      (1)
 #define CONFIG_IF_ID_ETHERNET2 (2)

The order of these configuration records is fixed, regardless of the Interface Block number. For example, if you have only a WiFi interface, you still use Configuration Record 1. Interface Blocks are numbered 1, 2, 3. There is no correlation between this numbering. Interface Block 3 could reference Configuration Record 1, depending on the order of interface registration calls by your application.

Modifying Run Time Network Configurations

When run time network configurations are modified, changes are stored in RAM. This is useful if you only need to temporarily change the network configuration settings. When the device loses power or is restarted, the settings stored in flash will be loaded. Here is some example code that modifies the run time network configuration.

void ChangeRuntimeIPSettings(int InterfaceNumber, IPADDR IpAddr, IPADDR IpMask,
                             IPADDR IpGate, IPADDR IpDNS)
{
    iprintf("rnChanging IP runtime settings for interface %d:rn",
            InterfaceNumber);

    // Display current values
    InterfaceBlock *ib = GetInterFaceBlock(InterfaceNumber);
    iprintf("Old Settings:rn");
    printNetworkConfig(ib->netIP,ib->netIpMask,ib->netIpGate,ib->netDNS);
    iprintf("   Interface Name: %srn", ib->InterfaceName);

    // Change to new values
    ib->netIP = IpAddr;
    ib->netIpMask = IpMask;
    ib->netIpGate = IpGate;
    ib->netDNS = IpDNS;

    // Display new values. At this point, you can communicate with the
    // device using the new ip address and mask.
    iprintf("New Settings:rn");
    printNetworkConfig(ib->netIP,ib->netIpMask,ib->netIpGate,ib->netDNS);
    iprintf("   Interface Name: %srn", ib->InterfaceName);
}

This function is called with new network configuration settings. Initially, the function creates a pointer to the runtime configuration InterfaceBlock. Once this pointer is valid, changing the runtime network settings is as easy as modifying the InterfaceBlock structure. In this example, we are only interested in netIP, netIpMask, netIpGate, and netDNS. Additional settings are available, and studying the InterfaceBlock declaration in /nburn/include/netinterface.h will provide additional insight.

Modifying Flash Network Configurations

When configuration records are modified, changes are stored in Flash. When changes are made in flash, they will be applied to the device on next boot. After a reset, changes to the network configuration are applied and saved through power loss. If you want the device to immediately update its network configuration, you should either simultaneously modify the run time settings, or trigger a software reset after flash settings are updated. Here is example code that modifies the Flash network configuration.

void ChangeFlashIPSettings(int RecordNumber, IPADDR IpAddr, IPADDR IpMask,
                           IPADDR IpGate, IPADDR IpDNS)
{
    iprintf("rnChanging Flash settings using RawGetConfig() for Record %d:rn",
            RecordNumber);

    // Get pointer to Configuration Record
    ConfigRecord *cr = RawGetConfig(RecordNumber);

    // Display current Flash values
    iprintf("Old Settings:rn");
    printNetworkConfig(cr->ip_Addr,cr->ip_Mask,cr->ip_GateWay,cr->ip_DNS_server);
    // create new config record and copy data
    ConfigRecord NewRec;
    memcpy( &NewRec, cr, sizeof( NewRec ) );

    // Change parameters
    NewRec.ip_Addr = IpAddr;
    NewRec.ip_Mask = IpMask;
    NewRec.ip_GateWay = IpGate;
    NewRec.ip_DNS_server = IpDNS;

    // Write new values to Flash system configuration sector
    UpdateConfigRecord_Num(&NewRec, RecordNumber);

    // Display current Flash values
    iprintf("New Settings:rn");
    printNetworkConfig(cr->ip_Addr,cr->ip_Mask,cr->ip_GateWay,cr->ip_DNS_server);
    // You do not need to reboot if you change both the runtime
    // and flash values.
    ForceReboot();
}

This function is called with new network configuration settings. The function creates a pointer to a ConfigRecord object. New values are set at ip_Addr, ip_Mask, ip_GateWay, and ip_DNS_server. Unlike the run time configuration process, you must call UpdateConfigRecord_Num() to save the new ConfigRecord in to Flash. Finally, call ForceReboot() to cause the device to reset and load the new flash settings. Alternatively, you could call ChangeRuntimeIPSettings(), which was mentioned in the previous section, to update the run time settings without rebooting the device.

Full Source for Example Application

To see the above code in action, a full source example is provided at GitHub. To build and load this example, clone the source code located at https://github.com/NetBurner/ChangeIP

Share this post

Subscribe to our Newsletter

Get monthly updates from our Learn Blog with the latest in IoT and Embedded technology news, trends, tutorial and best practices. Or just opt in for product change notifications.

Leave a Reply
Click to access the login or register cheese