EthenetIP

Discussion to talk about software related topics only.
Post Reply
roland.ames

EthenetIP

Post by roland.ames »

I have been using this 'variable' to tell me what IP address the netburner module is accessible on. If EthernetIP == 0, there is no ethernet available, otherwise it is equal to the IP address. This was all working fine until AutoIP was introduced, now I find that if the IP address being used is generated by the AutoIP function, EthernetIP remains at zero.

Is there a simple way to find which IP address is being used, regardless of whether it is static, DHCP'd or AutoIP'd, I just want some function that will return the IP address being used (as it appears in IPSetup or AutoUpdate)


this is my initialisation, I do not time out on getting DHCP'd on powerup, I want to handle situation where the ethernet is not necessarily connected at powerup, but should be able to be connected / disconnected to either a network (DHCP) or a laptop (AutoIP) at any time

Code: Select all

    InitializeStack();
    if (EthernetIP == 0)
    {
        DhcpObject * pDhcpObj = new DhcpObject();
        pDhcpObj->StartDHCP();
    }
    OSChangePrio(MY_MAIN_PRIO);
    EnableAutoUpdate();
    EnableSmartTraps();
    EnableTaskMonitor();



I have come up with a function that I think will do what I want

Code: Select all

IPADDR GetCurrentIP(void)
{
	// search through interface blocks in sequence until a non-zero IP is found, return the non-zero IP, or return 0 if cannot find any non-zero IP
	IPADDR ip=0;
	int i=0;
	while( !ip && (i=GetnextInterface(i)) )		// GetFirstInterface() is the same as GetnextInterface(0)
	{
		ip = InterfaceIP(i);
	}
	return ip;
}
this is not intended to be useful in all systems, my system has just the single ethernet connection, but the ip address could be static, dhcp or autoip. I will be testing soon.
sulliwk06
Posts: 118
Joined: Tue Sep 17, 2013 7:14 am

Re: EthenetIP

Post by sulliwk06 »

I actually use multihome addressing so I can use both DHCP and static addresses. This is how I look at all of my IP addresses:

Code: Select all

   numInterfaces = AddInterface( AsciiToIp( Cur_Settings.staticIP ), AsciiToIp( Cur_Settings.staticMask ), 0 );
   iprintf("\r\nTotal number of interface addresses: %d\r\n\r\n",numInterfaces);
   int lastInterface;
   for(int i = 0; i < numInterfaces; i++)
   {
	   int thisInterface;
	   if(i == 0)
	   {
		   thisInterface = GetFirstInterface();
	   }
	   else
	   {
		   thisInterface = GetnextInterface(lastInterface);
	   }
	   lastInterface = thisInterface;

	   iprintf("---------------------------\r\nAddress #%d\r\n",i+1);
	   InterfaceBlock *thisIb;
	   thisIb = GetInterFaceBlock( thisInterface ); // Get interface data
	   iprintf( "IP Address: " ); ShowIP( thisIb->netIP ); iprintf( "\r\n" );
	   iprintf( "Mask      : "); ShowIP(thisIb->netIpMask);iprintf("\r\n");
   }
roland.ames

Re: EthenetIP

Post by roland.ames »

I am not familiar with the multi-home features. I am using just the single physical interface, no wifi or ethernet2.

I wanted a function that return the ip address as it appears in the IPSetup window.

I have tested the following code and it works for me, but I have not even considered the situation with wifi or a second LAN

Code: Select all

char IPMode;

IPADDR GetCurrentIP(void)
{
	InterfaceBlock * ifp = GetInterFaceBlock();
	if (ifp->dhcpClient)
	{
		if ( ifp->dhcpClient->ValidDhcpLease() )
		{
			IPMode = 'D';							// for debugging indicates DHCP
			return ifp->dhcpClient->DhcpClientIP;
		}
		InterfaceBlock * cifp = ifp->pChild;
		if ( (cifp) && (cifp->AutoIPC) && (cifp->AutoIPC->getState() == AUTO_IP_CONFIGURED) )
		{
			IPMode = 'A';							// for debugging indicates AUTOIP
			return cifp->netIP;
		}
	}
	else
	{
		IPMode = 'S';								// for debugging indicates STATIC
		return ifp->netIP;
	}
	IPMode = 'N';									// for debugging indicates NO VALID IP
	return 0;
}
Post Reply