Forum Discussion
Altera_Forum
Honored Contributor
15 years agoI have done a small test, 7 changes static->static, and it seems to be stable.
Even when doing 4 times DHCP->static->DHCP, there is no problem. It might be neccessary to wait until DHCP has finished (which is not the case when DHC_second returns). Depending on the network the DHCP response takes a moment. A newly initiated connection might still use the old IP. When the response arrives after the new IP was assigned, it will not be seen, and the connection times out. Also, reading the code again, dhc_second() does not evalutate the NF_DHCPC flag. Also, there is a note that dhc_second() should be called every once in a while to update leases. Therefore I think it's neccessany to disable the DHCPClient via the state variable:
if(dhcp_actv)
{
p_net -> n_flags |= NF_DHCPC;
dhc_state_init(iface, FALSE); /* Put DHCPClient in INIT-REBOOT state */
......
}
else
{
p_net -> n_flags &= ~NF_DHCPC;
dhc_set_state(iface, 0); /* set interface state to DHCS_UNUSED */
......
}
So my change_ip() looks like this now:
typedef struct {
alt_u8 ipaddr_in;
alt_u8 netmask_in;
alt_u8 gw_in;
} NetInfo;
int change_ip (NetInfo new_ip, int dhcp_actv)
{
// alt_iniche_dev *p_dev;
NET p_net;
ip_addr ipaddr, netmask, gw;
int iface = 0; // number of interface to use
p_net = nets;
// Get the new ip address
printf(" Get new ip address:\n");
if(dhcp_actv)
{
p_net -> n_flags |= NF_DHCPC;
dhc_state_init(iface, FALSE); /* Put DHCPClient in INIT-REBOOT state */
}
else
{
p_net -> n_flags &= ~NF_DHCPC;
dhc_set_state(iface, 0); /* put DHCPClient in DHCS_UNUSED state */
IP4_ADDR( ipaddr, new_ip.ipaddr_in, new_ip.ipaddr_in, new_ip.ipaddr_in, new_ip.ipaddr_in );
IP4_ADDR( netmask, new_ip.netmask_in, new_ip.netmask_in, new_ip.netmask_in, new_ip.netmask_in );
IP4_ADDR( gw, new_ip.gw_in, new_ip.gw_in, new_ip.gw_in, new_ip.gw_in );
p_net -> n_ipaddr = ipaddr;
p_net -> snmask = netmask;
p_net -> n_defgw = gw;
}
// dhc_setup(); // fails, because port is in use?
int e = dhc_second(); /* To send the DISCOVER/REQUEST pkt */
if (e)
printf("DHCP returned error %i.\n", e);
return 0;
}