Cerberus Networking Problem

I’m attempting to setup my first network/internet connection to a Cerberus and either something isn’t working or I’m totally missing something. Any help would be greatly appreciated. I’m using Duke Nukem’s ThingSpeak code as a test but I don’t think that’s a problem at this point since my main problem seems to be in getting an IP address.

http://www.tinyclr.com/codeshare/entry/607

I have installed the latest ethernet firmware to the Cerberus and set the DNS address through MFDeploy to be that of my router. Running the code results in a DHCP IP address never being granted. I tried setting a static IP through MFDeploy that that didn’t work either. Then I made an interesting discovery. If I watch my current DHCP connections through my router GUI then I can see an IP address is granted to my device when I open the Network Configuration dialog within MFDeploy. The IP stays alive as long as that window is open. As soon as I close the window, I see the connection drop out of my router. Also, I’ve noticed that I can also see it granted an IP address when it is not debugging but the board is powered up.

Any ideas? Four nights ago I thought this was going to be a couple hour project… :frowning:

Here is the network code thats working for me tonight with a Cerberus on another project I’m working on.


            try
            {
                ni = NetworkInterface.GetAllNetworkInterfaces()[0];
                
                if (!ni.IsDhcpEnabled)
                    ni.EnableDhcp();

                if (ni.IPAddress != "0.0.0.0")
                {
                    Debug.Print("IP Address:" + ni.IPAddress);
                }
            }
            catch (Exception exception)
            {
                throw new ArgumentException("Could not resolve host via DNS.", exception);
            }

Haven’t used Dhcp yet, but for static IP the following code works well for Cerberus/ENC28 (actual addresses are different in your case, of course)
I think you need to set the MAC in the program. The MSDeploy setup works for EMX, but maybe not for Cerbs. I believe there was a post about this.


      // Network Parameters adjusted HERE
      static string myIP = "192.168.3.223";
      static string subnetMask = "255.255.255.0";
      static string gatewayAddress = "192.168.3.1";
      static string dnsAddresses = "194.246.96.1";
      static byte[] myMAC = new byte[] { 0x00, 0x30, 0xFB, 0x87, 0xD2, 0x3C };
      ... 
      ... 
      private static void InitializeNetwork()
      {
         Debug.Print("Initializing network...");
         NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
         if (interfaces != null && interfaces.Length > 0)
         {
            NetworkInterface networkInterface = interfaces[0];
            Boolean isDhcpWorked = false;

            Debug.Print("Setting static IP...");
            networkInterface.EnableStaticIP(myIP, subnetMask, gatewayAddress);
            networkInterface.PhysicalAddress = myMAC;
            networkInterface.EnableStaticDns(new[] { dnsAddresses });

            Debug.Print("Network ready.");
            Debug.Print(" IP Address: " + networkInterface.IPAddress);
            Debug.Print(" Subnet Mask: " + networkInterface.SubnetMask);
            Debug.Print(" Default Gateway: " + networkInterface.GatewayAddress);
            Debug.Print(" DNS Server: " + networkInterface.DnsAddresses[0]);
         }
         else
         {
            Debug.Print("No network device found.");
         }
      }

W00t! We’re making some progress. I’m able to get a static IP using Lurch’s code. However, I still can’t get a DHCP address :frowning: I had hoped that assigning the MAC at runtime would fix it also, but it doesn’t seem to make a difference. Any other ideas? I’d really rather have this work with DHCP.


        private static void InitializeNetwork_Dhcp()
        {
            try
            {
                var nis = NetworkInterface.GetAllNetworkInterfaces();
                
                if (nis.Length == 0)
                {
                    throw new IndexOutOfRangeException("No network interface was found.  Make sure you have the ethernet firmware installed and a NIC attached.");
                }
                var ni = nis[0];
                ni.EnableDhcp();
                ni.PhysicalAddress = new byte[] { 0x00, 0x30, 0xFB, 0x87, 0xD2, 0x3C };

                while (ni.IPAddress == "0.0.0.0")
                {
                    Debug.Print("Waiting for IP address...");
                    Thread.Sleep(1000);
                }
                Debug.Print("IP Address:" + ni.IPAddress);
            }
            catch (Exception exception)
            {
                throw new ArgumentException("Could not resolve host via DNS.", exception);
            }    
        }

EDIT: Using a static IP, Duke’s code is working perfectly for posting data to ThingSpeak! :smiley: They will shudder in disgust when they see what I’m using their service for!!! :slight_smile: :slight_smile: :slight_smile:

Does it work if the MAC is assigned before the Dhcp call? I don’t know enough about the Dhcp code to guess if it’s a timing problem.
AFAIK, Dhcp needs to know the MAC before it can assign the IP in order to check for a previous assignment.

I tried both ways. Didn’t make a difference. I agree that assigning the MAC should probably happen first and that’s how my first test looked.

hmmm … just changed the code on my Cerbuino Bee and the Dns worked (using this change).
Not clean code, but working …


            //Debug.Print("Setting static IP...");
            //networkInterface.EnableStaticIP(myIP, subnetMask, gatewayAddress);
            networkInterface.PhysicalAddress = myMAC;
            //networkInterface.EnableStaticDns(new[] { dnsAddresses });

            
            networkInterface.EnableDhcp();
            Debug.Print("Waiting for DHCP lease.");

            IPAddress ip = null;

            while (true)
            {
               ip = IPAddress.GetDefaultLocalAddress();
               if (ip != IPAddress.Any) break;

               Thread.Sleep(1000);
            }
            Debug.Print("Client IP address:" + ip.ToString());

Another thing to check would be if your router disallows the assignment. Firewall rule etc.
In any case, thanks for prodding me - now I don’t need the fixed IP

Weird… I still can’t get a DHCP address :frowning:

the full procedure, just in case something is missing



      static NetworkInterface networkInterface;
      
      // Network Parameters adjusted HERE
      static byte[] myMAC = new byte[] { 0x00, 0x30, 0xBD, 0xF7, 0xDF, 0x7C };
     ....

      private static void InitializeNetwork()
      {
         Debug.Print("Initializing network...");
         NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
         if (interfaces != null && interfaces.Length > 0)
         {
            networkInterface = interfaces[0];
            Boolean isDhcpWorked = false;

            //Debug.Print("Setting static IP...");
            //networkInterface.EnableStaticIP(myIP, subnetMask, gatewayAddress);
            networkInterface.PhysicalAddress = myMAC;
            //networkInterface.EnableStaticDns(new[] { dnsAddresses });

            // Change to use Dhcp            
            networkInterface.EnableDhcp();
            Debug.Print("Waiting for DHCP lease.");

            IPAddress ip = null;

            while (true)
            {
               ip = IPAddress.GetDefaultLocalAddress();
               if (ip != IPAddress.Any) break;

               Thread.Sleep(1000);
            }
            Debug.Print("Client IP address:" + ip.ToString());


            Debug.Print("Network ready.");
            Debug.Print(" IP Address: " + networkInterface.IPAddress);
            Debug.Print(" Subnet Mask: " + networkInterface.SubnetMask);
            Debug.Print(" Default Gateway: " + networkInterface.GatewayAddress);
            Debug.Print(" DNS Server: " + networkInterface.DnsAddresses[0]);
         }
         else
         {
            Debug.Print("No network device found.");
         }
      }

Strange indeed, if fixed IP works. Must be some rule violation. Or the Firewall is refusing to lease for some reason. Or doesn’t have any free IPs to lease?
I can’t help there much. Sorry.

Can you try some other MAC, just in case the one you used is active somewhere else?
I usually just pick the MACs off old ethernet cards I have lying around.
How many addresses does the router have to lease? I set mine to allow it to use 101…199 for Dhcp.

Actually, it appears that DHCP is granting an IP address just fine if I look at my logs in the router. However, the ni.IPAddress property is never populated with the IP that it is given.

If I try to ping the DHCP IP, check out the response back in the pic. I am pinging the Cerberus, but I get an “unreachable” response back from my PCs IP. This feels like a Visual Studio proxy problem… I’m using Windows 8 Pro & VS2010 Ultimate.

And your ni (like my variable networkInterface) is declared static somewhere ?
Very strange.

225 is on a boundary of a subnet. Can you set your DHCP server to give the Cerberus an address say .237 ??

That did it! So, does this mean that I have DHCP setup wrong on my router? I’ve got it setup to give a number between .100 and .254. These were just random (apparently bad) choices due to my limited knowledge of the insides of networking. Would .226 - .254 be better? What if I end up needing more?

What’s your subnet mask? If you set it as 255.255.255.0 you will be fine. But I suspect its not that at the moment.

Yes, it is 255.255.255.0. However, there’s something stranger going on… It appears that it will only work if I reserve a number in my router. I tried changing the range to .235 - .255 and it still would not register an IP on the Cerberus. But if I then reserve .235 the same code works just fine. It seems there’s something the NetworkInterface code isn’t understanding/handling properly.

can you show an IPCONFIG from your PC?

How did you choose your MAC address? Can you grab a new on e from macvendorlookup.com and try it again, some routers don’t like handing out to MACs that they see as “invalid”?

not discounting that dhcp in 4.2 may have bugs, but what I found interesting in Ian’s testing was that by setting a reserved IP for the mac adder, the response worked.

Does anyone have a network hub and can do a packet capture to see what is going on? If you can get me a Netmon capture I’ll take a look

*** I accidentally overwrote this post instead of quoting it :frowning: If it’s possible to revert it back to it’s original content, please do so. ***

@ Gus, is this a problem with GHI code or is this something we need to report to Microsoft?