[En-Nut-Discussion] Attempting to fix network config API.

Thiago A. Corrêa thiago.correa at gmail.com
Wed Aug 20 04:11:57 CEST 2014


Hi Henrik,

    I didn't attempt to transition from DHCP to static configuration
without reboot, nice to know it's even possible. :)

    In our code, I have used for years the snippet bellow. As you
know, to build that, I had to look up the implementation of the Nut
functions to figure which magic parameter and global structs to set in
order to get the behavior I was looking for. Obviously this turns out
to be a barrier for new developers as they can't figure it out from
either samples, or documentation how to do that.

    Any suggestion for names? I wouldn't like to append numbers to the
API names. It would be best if we could reuse the current names (break
current behavior). Just occurred to me to make it a configure option,
to select old/new behavior.

   Currently, my code to setup network is this:

void StartNetwork(void)
{
    u_long ip_addr = inet_addr(DEFAULT_IP_ADDRESS);
    u_long ip_mask = inet_addr(DEFAULT_IP_MASK);
    u_long ip_gate = inet_addr(DEFAULT_GATEWAY);
    u_char mac[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x00};
    INFO("Setting up eth0...");
    INFO("MAC address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1],
mac[2], mac[3], mac[4], mac[5]);

    if ( settings.useDHCP )
    {
        INFO("Using DHCP...");
        confnet.cdn_cip_addr = 0;
        confnet.cdn_ip_addr = 0;
        confnet.cdn_gateway = 0;
        if( NutDhcpIfConfig("eth0", 0, 60000) &&
NutDhcpIfConfig("eth0", mac, 60000) )
        {
            // Falha em obter IP por DHCP
            ERROR("Failed to obtain address from DHCP server");
            ERROR("Using default IP address");
            strncpy( confos.hostname, DEFAULT_HOSTNAME,
sizeof(confos.hostname) );
            confos.hostname[sizeof(confos.hostname) - 1] = 0;
            goto useFixedIP;
        }
        else
            return;
    }
    else
    {
        if ( !NutNetLoadConfig("eth0") )
        {
            ip_addr = confnet.cdn_cip_addr;
            ip_mask = confnet.cdn_ip_mask;
            ip_gate = confnet.cdn_gateway;
            memcpy(mac, confnet.cdn_mac, 6);
            INFO("Loading Network settings from EEPROM");
        }
        else
        {
            INFO("Loading default network settings");
            strncpy( confos.hostname, DEFAULT_HOSTNAME,
sizeof(confos.hostname) );
            confos.hostname[sizeof(confos.hostname) - 1] = 0;
        }
    }
useFixedIP:
    confnet.cdn_cip_addr = ip_addr;
    confnet.cdn_ip_mask = ip_mask;
    confnet.cdn_gateway = ip_gate;
    NutNetIfConfig2("eth0", mac, ip_addr, ip_mask, ip_gate);
    NutDnsConfig2( 0, 0, settings.dns1, settings.dns2 );
    INFO("Gateway: %s", inet_ntoa(ip_gate));
}

    With a NutNetLoadConfig that loads up hard-coded defaults I could
reduce it to the snippet below (work in progress) and move the default
settings to the .conf file and into the library.

void StartNetwork(void)
{
    NutNetLoadConfig("eth0");
    if ( settings.useDHCP )
    {
        INFO("Using DHCP...");
        confnet.cdn_cip_addr = 0;
        confnet.cdn_ip_addr = 0;
        confnet.cdn_gateway = 0;
        if( NutDhcpIfConfig("eth0", confnet.cdn_mac, 60000) )
        {
        }
        else
            return;
    }
    NutNetLoadConfig("eth0");
    NutNetIfConfig2("eth0", confnet.cdn_mac, confnet.cdn_cip_addr,
confnet.cdn_ip_mask, confnet.cdn_gateway);
    NutDnsConfig2( 0, 0, settings.dns1, settings.dns2 );
    INFO("Gateway: %s", inet_ntoa(ip_gate));
}

Kind Regards,
     Thiago A. Correa


More information about the En-Nut-Discussion mailing list