This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

getting the DHCP IP address

Other Parts Discussed in Thread: SYSBIOS

Hi,

i'm working on DK129 using the TI-RTOS, and my problem that if the board take an IP address on the startup from the DHCP server then i unplugged the ethernet cable and if the board take another IP (diffrent than the old one) and i checked the ip address using the following function, the IP isn't changing, so is there another function that i should call to get the actual IP address?

                                if (CfgGetImmediate(0, CFGTAG_IPNET, objstrInterfaceRequest.index, 1,\
                                        sizeof(objstrIPNETConfiguration), (UINT8 *)&objstrIPNETConfiguration) \
                                        == sizeof(objstrIPNETConfiguration))
                                {
                                    /* Yes the device was configured and we got the IP address/Mask */
                                    NtIPN2Str(objstrIPNETConfiguration.IPAddr, s8IPString);
                                    UARTprintf ("IP Address     : %s\n", s8IPString);
                                    NtIPN2Str (objstrIPNETConfiguration.IPMask, s8IPString);
                                    UARTprintf ("Net Mask       : %s\n", s8IPString);
                                }

Environment:

***** CCS     : 6.0.0.00190
***** TIRTOS  : tirtos_tivac_2.01.00.03
***** SYSBIOS : 6.40.03.39
***** TIVAWARE: 2.1.0.12573c
***** NDK     : 2.23.01.01
***** UIA     : 2.00.01.34

Thanks,

Mohamed Fawzy

  • Hi Mohamed,

    I'm wondering if there's duplicate entries for the IP address. Can you try calling the CfgGetEntryCnt() API to see what it returns?

    int numberOfInstances = CfgGetEntryCnt(0, CFGTAG_IPNET, 1);

    What's the value for numberOfInstances?

    You might also try configuring the 'network IP address hook' into your application.  This hook is called whenever an IP address (IPv4) is added or removed from the system.  The IP that's passed to this hook function is the actual IP address that the system will have bound.

    To do this, you can add the following line of code to your app's *.cfg file:

    Global.networkIPAddrHook = '&mynetworkIPAddrHook';

    And the following function to your app's C file:

    void mynetworkIPAddrHook(IPN IPAddr, uint IfIdx, uint fAdd)
    {
        IPN IPTmp;

        System_printf("mynetworkIPAddrHook: enter\n");

        IPTmp = ntohl(IPAddr);
        System_printf("mynetworkIPAddrHook:\tIf-%d:%d.%d.%d.%d\n", IfIdx,
                (UINT8)(IPTmp>>24)&0xFF, (UINT8)(IPTmp>>16)&0xFF,
                (UINT8)(IPTmp>>8)&0xFF, (UINT8)IPTmp&0xFF);

        System_flush();

    }

    What IP do you see being printed?

    You can also add a break point into this hook function to inspect what's going on, too.

    Steve

  • Hi Steve,

    Thank you for your reply and sorry for being late.

    actually, it seems that i miss something in my first post but the actual problem is, if the board started up with IP taken using the DHCP server so there is no way to be changed even if the DHCP server assign the board IP to another node in the network, here were my steps:

    1- connect the DK to the network.

    2- the Global.networkIPAddrHook function print the received IP which is 10.1.0.212.

    3- check my DHCP server (my linksys X3000 router), i found that my board available on the DHCP list and its MAC.

    4- disconnect the DK

    5- change the router configuration to assign the IP 10.1.0.212 to one of the PC on the network.

    6- checking the PC after a while found it is already took the 10.1.0.212 IP address.

    7- connect the DK once again to the network.

    8- the DK didn't took another IP even the hook function for the IP change didn't call at all beside the board keep the old IP which is 10.1.0.212 which made a conflict with the PC.

    So please advise, why the board didn't change its IP address?

    Best Regards,

    Mohamed Fawzy

  • Mohamed Fawzy said:
    8- the DK didn't took another IP even the hook function for the IP change didn't call at all beside the board keep the old IP which is 10.1.0.212 which made a conflict with the PC.

    So please advise, why the board didn't change its IP address?

    Relative to the NDK, the lease it acquired from the router/DHCP server is still valid, so the stack does not have any way of knowing that someone has changed the router's DHCP address pool and assigned a duplicate address in the network.  The NDK will keep that IP address bound until the lease expires, at which point it will send communication out to the DHCP server to request lease renewal.  I would expect it to get a different IP address since the original IP is now taken.

  • how long is the lease time? and is it a configurable parameter?

  • This is controlled by the DHCP server (router in your case).  I believe it's usually 24 hours, but that is probably implementation dependent.

    When a DHCP client requests an IP address inititally, a DHCP server responds with an offer, which includes an IP address from its free pool, as well as the lease time, expressing the amount of time that the IP address is valid for.

    There's a good description of this on Microsoft's technet website:

    "All DHCP servers that receive a DHCPDiscover message and have a valid IP address to offer the DHCP client respond with a DHCPOffer message [...] The DHCPOffer message contains an IP address and matching subnet mask, a DHCP server identifier (the IP address of the offering DHCP server), and a lease duration."

    Since you typically don't have control over the server, one way you could try to handle such a scenario is to force a lease renewal by removing the IP address from the system in this situation.  You can see an example of how to do this in the IP address remove command that's part of the Telnet console.  See the function ConRemoveIPAddress in the file "ti/ndk/tools/console/conipaddr.c" for an example of this.  Similarly, see the code of ConConfigureNIMUObject for how to add an IP address back.

    Steve

    Edit:

    Actually it may be better to re-add DHCP client to your configuration after removing the IP address.  This should force the DHCP lease process to start over.  Something like this:

        {
            CI_SERVICE_DHCPC dhcpc;

            // Specify DHCP Service on IF-1
            bzero( &dhcpc, sizeof(dhcpc) );
            dhcpc.cisargs.Mode   = CIS_FLG_IFIDXVALID;
            dhcpc.cisargs.IfIdx  = 1;
            dhcpc.cisargs.pCbSrv = &ServiceReport;
            dhcpc.param.pOptions = DHCP_OPTIONS;
            dhcpc.param.len = 2;
            CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0,
                         sizeof(dhcpc), (UINT8 *)&dhcpc, 0 );
        }