EnableDHCP ist blocking

I wrote a simple programm, where I’m checking if DHCP is enabled. If DHCP isn’t enabled I’m using EnableDHCP. If this function is used, my programm stopps working - not always but very very often. What could be the reason for that? I’ve a try/catch block around it, but it doesn’t help…

Your question is missing one or more of these details:[ul]
The name of the product.
The code you’re trying to run.
Details on your setup.[/ul]
(Generated by QuickReply)
Please tell yus what board you’re running…

Sorry, I forgot it :frowning:
I’m working with the chipworkx (version 4.1.8.0) and the developement kit.

My code is very simple - only for testing:


NetworkInterface[]  networks = NetworkInterface.GetAllNetworkInterfaces();
if (networks != null)
 {
    if (networks.Length >= 1)
    {
        if (!networks[0].IsDhcpEnabled)
        {
            networks[0].EnableDhcp();
        }
    }
}

This is everything I’m doing while starting. Then I have a loop, where I’m checking 2 buttons from the dev kit. If Button LEFT (PA25) is pressed, I’m checking doing the following to enable DHCP:


NetworkInterface[]  networks = NetworkInterface.GetAllNetworkInterfaces();
if (networks != null)
 {
    if (networks.Length >= 1)
    {
        if (!networks[0].IsDhcpEnabled)
        {
            networks[0].EnableDhcp();
        }
    }
}

If Button RIGHT (PA16) is pressed, I’m checking doing the following:


NetworkInterface[]  networks = NetworkInterface.GetAllNetworkInterfaces();
if (networks != null)
 {
    if (networks.Length >= 1)
    {
        if (networks[0].IsDhcpEnabled)
        {
            networks[0].EnableStaticIP(networks[0].IPAddress, networks[0].SubnetMask, networks[0].GatewayAddress);// This function is blocking
            networks[0].EnableStaticDns(networks[0].DnsAddresses);
        }
    }
}

From the beginning I’m toggling a Led, to see if the chipworkx is working.

During runtime I’ve no problem and everything works fine. But after restarting the chipworkx (hardreset by breaking power), sometimes it won’t work again. There are two different kinds of “not working”:

  1. Nothing is starting - Led isn’t blinking and also the Leds on the Ethernet connector will not shine or blink. After more than 30 seconds (sometimes never) ethernet connector ist starting blinking but code doesn’t start - because my Led won’t blink.
  2. Ethernet connector is starting blinking immediately after restart, but but code doesn’t start - because my Led won’t blink.

In both cases I have to restart the module for several times. Most time second restart is succesful, but not always.
All other code samples I’ve tried till now made absolutely no problems.
Does somebody has an idea, what I’m making wrong?

Why not use the dispkay to show some debug info that may help?

I’m using the display, but in case 2 the output ends when using EnableDHCP(). In case 1 output won’t start.

@ LionHeart,

Some points to check :

  1. Did you look at the MAC Adress assigned to your module ? sometimes it can be erased to 00-00-00… which can causes problem for DHCP assignment,
  2. What type of DHCP Server are your using ? The one provided by your box ? a real DHCP server ? Try to use the same code with a DHCP Server tool that can be download from the net, and check if the problem keeps going…

I had such a problem with my CHIPworkx and finally demonstrate that this blocking situation was occuring when using my Orange BOX DHCP server, but went well when using other DHCPs. However, I can not tell you if the problem is in the implementation cases of the DHCP client on Microframework side, or even on the DHCP Server side on the box…

  1. Looking to your second code sample, does it mean you’re trying to assign the DHCP given address as a static addess ? this can onl be done if on your DHCP server, the address caution is set as unchangeable !

@ louis, good pickup.

@ Lionheart, did you mean to say something like

  if ( ! networks[0].IsDhcpEnabled)

?

@ LuisCpro
thank you for the tipps.

  1. MAC address is still the same like on the sticker on the chipworkx
  2. I’m using the DHCP function of my router (Draytek 2950i)
  3. yes, this means that I’m trying to set the DHCP address static. What do your mean by “address caution is set unchangeable”?

Did you have the same situation, that sometimes the chipworkx didn’t start. Also the ethernet port will not start. It seems that the module is completely dead.

@ Brett
yes, I would like to say this. Why?

In many cases, in DHCP server, you can set that DHCP Lease is set as permanent, which mean that this IP address is reserved to tha associated MAC Address for ever. If you do not set that, it is possible that at the same time you’re fixing the address of your present lease, your dhcp server signal that the lease has ended and re-assign your address to someone else (in general it does not apply if you do not leave the subnet, but in your explanation, you set your IP address at boot up, which means you leaved the network for few seconds, and this is sufficient to make such a situation arrive.

However, it is not recommended to assign as static, addresses that are in the DHCP table, unlees you made this address reserved to your MAC…

Effectviely I have the same problem as your with the OrangeBOX, and not sometimes but ALWAYS !!! :-(. The boot sequence stop (looks dead) on the EnableDHCP instruction, which is a synchronized call, and does not have a timeout implementation…I think the problem is that my box is waiting for an information in the demand packet, which is probably not mandatory in the RFC2131 MF implementation, but requested by the box…

In my project, this is not blocking because I do not use DHCP, and always set to static… As far as it can go right with other DHCP servers solutions, I choose to think that it will probably not be a CHIPWORKX problem, but a BOX one…

Just try with a soft DHCP server and tell me, it would be interesting to know if you have the same problem as mine !

Hi @ Lionheart,

This is what I don’t really understand in what you’re trying to do.

You’re checking that DHCP is enabled on your network, and then you’re enabling a static IP address. Why?

The way DHCP works is that you don’t need to do anything except enable DHCP. It is quite possible that the IP stack is not working when you enable a Static IP in this way, when DHCP is enabled, in this code.

If Button RIGHT (PA16) is pressed, I'm checking doing the following:
 NetworkInterface[]  networks = NetworkInterface.GetAllNetworkInterfaces();
if (networks != null)
 {
    if (networks.Length >= 1)
    {
        if (networks[0].IsDhcpEnabled)
        {
            networks[0].EnableStaticIP(networks[0].IPAddress, networks[0].SubnetMask, networks[0].GatewayAddress);// This function is blocking
            networks[0].EnableStaticDns(networks[0].DnsAddresses);
        }
    }
}

Perhaps you want to Disable DHCP before you enable a Static IP address, but you’ll have to record the current IP and gateway and DNS addresses so that you can re-use them, as I suspect disabling DHCP will invalidate them.

As Louis says, as a standard action you should NOT use an address in this way, that a DHCP server could issue to a different device and then you’d have an address conflict. Once you give up a DHCP address, the DHCP server is within it’s right to issue it to the next requesting device (although in practice it’s unlikely to do that within the lease time, at least)

Hi Brett, thank you for your answer.
I will have a dip-switch on my custom-board for the chipworkx. One switch will be responsible for the network configuration. So my customers are able to set DHCP manuall - this means switching on DHCP. If he would like to us static network configuration, he can switch off DHCP and enter static network parameters via software. If he will forget to enter static network parameters, we will save the last network parameters from DHCP automatically.

I’m aware of the fact that this could lead to address conflicts, but for correct network settings the customer is responsible. The most important thing is, that my board isn’t stop working, also if there are problems with the network.

At the moment it always stopps working on the first restart after changing the network settings (from static to dhcp and from dhcp to static). A second restart will correct it and it works. Is there a way to avoid such blocking?

@ Lionheart,

As it is quite always done, the best is to set a factory default IP address that is hardcoded in the module, and given to your customer. This way, he can simply connect the module to its notebook with a crossed ethernet cable and configure the module as needed, before plugging it to the final network.

In this case, no need for dip switch and ensure that your customer will understand that all the module are set with the same address when received.

For example, on my project, there is an embedded website on the chipwork, that has a page for ethernet configuration. By default, the address is hardcoded and shown to the user that can change it, or event pass thru the DHCP solution.

Also this practice is a best because, as Brett said, in the case of static address use, you also often need the give the subnet, gateway, and maybe DNS server address to make things happened correctly ! a dip switch wont give you that !

@ LouisCpro
Thank you for your answer.

If I understand you correct, you are also offering DHCP to your customers. Only the way is different. Your customers can activate DHCP over an internal website, my customers can activate it via dip-switch.

What I’m not understanding is, that we are both doing the same. Also if I’m doing the changing of the network parameters via website, I have to ask if DHCP is enabled and if not, enable DHCP. This will maybe lead to a blocking.

Right,

but as I said few posts before, I have the blocking situation only when in my Office, and only when using my Orange BOX, so I guess this is probably the same for you…Have you tried to use another dhcp server ?

Regarding to your dip switch, what about when the user do not choose DHCP ? How does it set the subnet mask and the IP address ?

I tried it with 3 router - 2 are blocking, 1 is ok.
Sometimes I have the problem, that my module is starting about 30 seconds delay. Using such a chipworkx module in the dev kits shows the output of the Tinybooter on the display. Normally Tinybooter output disapears after 1 second. But if the module is “broken” I have to wait about 30 seconds or press the keys UP, Select and DOWN to enter the normal mode. Disabling DHCP (if enabled) via MFDeploy or completing network parameters (if at least one is not correct) will fix the problem.
Strange… because I think I’m not doing something really wrong. I’m of the opinion, that even if networks parameters are wrong or no router is available, the chipworkx module should run…
Does somebody has an idea, what’s my error?

Taking a step back, here’s how I would approach the “swap”.

Record the current IP parameters from DHCP.
disable DHCP.
enable fixed IP based on the saved IP parameters.

That is what I would deem is “safe”

@ Brett
thanks, I will try this. But how should I disable DHCP? With releaseDHCPLease?

What disturbs me most, is the fact, that the chipworkx sometimes won’t start again, causing to wrong network parameters or other network troubles.

great point, there’s not really a “disable” method :frowning:

I would release the IP address, yes, and then try enabling.

edit: having said that, this is really not a common way of doing this. Perhaps you can think about releasing the IP address, setting an appropriate EWR value and then forcing reboot, and in your app you always only enable DHCP if that has been set previously, otherwise you set fixed IP?

It’s getting better and better :slight_smile: Thanks!

As already mentioned sometimes the board remains in tinybooter mode after starting. After 30 - 40 seconds it jumps automatically to the normal mode and my application starts running. If I don’t want to wait so long, I can force the normal mode by pressing the buttons UP, SELECT and DOWN on the DevKit.

Now I can reproduce this behaviour. On one of my PCs MFDeploy 2.0 was installed. Updating the network information always succeeded. But if the network configuration has been opened and no update was neccessary, clicking “Cancel” irritates the chipworkx module and then it remains vor about 30 sec in Tinybooter Mode until I was updating (click UPDATE in network configuration) the network.

With the correct version of MFDeploy (4.1) everything works fine.

Effectively, one thing that you say, makes me think you should erase and reinstall all the bootloader and tiniybooter again from scrash.
I guess probably something was wrong during your previous installation. The combination of UP/SELECT/DOWN is normaly the begin of the procedure to erase the memory…

If I’m right, EnableSTATIC is the way to make EnableDHCP to stop…

FYY : Here’s a piece of code on what I do. The Save() method get fields from the web page, and stores values, the Apply() set the network interface.



      public void Save()
        {
            try
            {
                if (this.UseDhcp)
                {
                    NetworkInterface.GetAllNetworkInterfaces()[0].EnableDhcp();
                }
                else
                {
                    NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticIP(this.ipaddress, this.subnetmask, this.gateway);

                    string[] dnss = new string[2];

                    dnss[0] = this.dnsserver1;
                    dnss[1] = this.dnsserver2;

                    NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticDns(dnss);
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Apply the IP configuration
        /// </summary>
        /// <returns></returns>
        public void Apply()
        {
            try
            {
                // Search interface
                var interfaces = NetworkInterface.GetAllNetworkInterfaces();

                // Get the DNS Definition
                if (this.dnsserver1.Length == 0)
                    throw new Exception("No DNS specified. At least one is required for me !!!");
                else
                {
                    // How many dns ?
                    int index = 0;
                    if (this.dnsserver1.Length > 0) index++;
                    if (this.dnsserver2.Length > 0) index++;

                    // instanciate the list
                    string[] dnsaddresses = new string[index];

                    dnsaddresses[0] = this.dnsserver1.Trim();
                    if (index == 2) dnsaddresses[1] = this.dnsserver2.Trim();

                    interfaces[0].EnableStaticDns(dnsaddresses);
                }

                // Get DHCP/STATIC state
                if (this.usedhcp)
                {
                    if (!interfaces[0].IsDhcpEnabled) interfaces[0].EnableDhcp();
                    else interfaces[0].RenewDhcpLease();
                }
                else
                {
                    interfaces[0].EnableStaticIP(this.ipaddress, this.subnetmask, this.gateway);
                }                
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

A you will see, in DHCP case, I always invoke a Lease renewal …

Hope this will help