Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12747 Discussions

Allowing user to choose DHCP or static IP

Altera_Forum
Honored Contributor II
1,125 Views

Hi, 

 

I would like the users of my product to be able to choose whether they want to use DHCP or use a static IP address. The idea is to allow users to adjust the IP configuration using the front panel of the system I am developing. The IP address set-up in ecos works very well but the choice between DHCP and a static IP address is made in the ecos configuration tool and is then "hard-wired" into the project. 

 

I have written a function based on init_all_network_interfaces (See below) which takes a parameter to specifiy whether to use DHCP or a static IP address (The static IP address is currently hard-coded in this function, but I will make it adjustable by the user later). This function actually works very well, but it has one bizarre piece of behaviour. If I call it with UseDHCP=true to get a dynamic IP address and then some time later call it with UseDHCP=false to set a static IP address then the system ends up responding to both the IP address assigned by DHCP and the static IP address. I can use a web browser to communicate with the system using either the old IP address or the new one. This can't be a good thing! 

 

Is there a function I can call which will completely shut down the ethernet interface so that I can then restart it with a different IP address? I have tried this code without success: 

 

my_init_all_network_interfaces(false); dhcp_halt(); my_init_all_network_interfaces(true); 

 

Here is the function I am using: 

 

// This function replaces the one supplied by ecos: init_all_network_interfaces // This version of the function allows DHCP to be dynamically switched on or off // void my_init_all_network_interfaces(cyg_bool_t UseDHCP) {  static volatile int in_my_init_all_network_interfaces = 0;      cyg_scheduler_lock();  while ( in_my_init_all_network_interfaces )  {    // Another thread is doing this...    cyg_scheduler_unlock();    cyg_thread_delay( 10 );    cyg_scheduler_lock();  }  in_my_init_all_network_interfaces = 1;  cyg_scheduler_unlock();  if (UseDHCP)  {    // Perform a complete initialization, using BOOTP/DHCP    eth0_up = true;    eth0_dhcpstate = 0; // Says that initialization is external to dhcp    if (do_dhcp(eth0_name, &eth0_bootp_data, &eth0_dhcpstate, &eth0_lease))    {      show_bootp(eth0_name, &eth0_bootp_data);    }    else    {      diag_printf("BOOTP/DHCP failed on eth0\n");      eth0_up = false;    }  }  else  {    eth0_up = true;    build_bootp_record(&eth0_bootp_data,  eth0_name,  "192.168.205.100",  "255.255.252.0",  "192.168.204.250",  "192.168.204.250",  "192.168.204.250");    show_bootp(eth0_name, &eth0_bootp_data);  }  if (eth0_up)  {    if (!init_net(eth0_name, &eth0_bootp_data))    {      diag_printf("Network initialization failed for eth0\n");      eth0_up = false;    }  }  // Open the monitor to other threads.  in_my_init_all_network_interfaces = 0; } 

 

Mike
0 Kudos
1 Reply
Altera_Forum
Honored Contributor II
371 Views

My understanding is that your interface now has both IP addresses assigned to it, which is fine if that's what you want. You need to delete the old one with something like: 

 

strcpy(ifr.ifr_name, intf); 

if (ioctl(s, SIOCDIFADDR, &ifr)) { /* delete IF addr */ 

perror("SIOCDIFADDR1"); 

 

which I just grabbed from dhcp_prot.c 

 

Would be nice if there was an ecos config that was smart enough to fall back to static or link local ip address if dhcp failed. We all need this.
0 Kudos
Reply