AutoIP Configuration

Jump to: navigation, search

Table of Contents >> Optional Protocols


The AUTO IP configuration APIs allow the user to configure the interface with an AUTO IP address, without having to pick a specific IP address. The IP address will automatically be selected in the AUTO IP v4 address range, i.e. IP addresses between 169.254.1.0, and 169.254.254.255. To configure an interface with an AUTO IP address:


  1. Starting the configuration: The user needs to call tfOpenInterface(), using a zero IP address, zero IP netmask, and setting the TM_DEV_IP_USER_BOOT flag in the flags parameter, as shown in the example below. Note that if the interface had already been opened on multi home index 0, and not closed, this step can be omitted.

  2. Selecting an IP address and starting the collision detection: Next the user needs to call tfAutoIPPickIpAddress() to pick an AUTO IP address, then call tfUseCollisionDetection(), passing that IP address, and a call back function, information that will be stored in the Treck stack. Next the user needs to call tfUserStartArpSend() so that the Treck stack can start sending ARP probes, to check for collisions on the IP address. tfConfigAutoIp(), provided as an example in 'examples\txautoip.c', and described below does all this.

  3. Finishing the configuration or trying another IP address: The call back function will be called if either a collision has been detected (non zero errorCode), or when the timeout for sending the probes has expired with no collision (zero errorCode). If no collision has been detected, then the user should call tfFinishOpenInterface() with the selected AUTO IP address, if tfOpenInterface() with the TM_DEV_IP_USER_BOOT flag had been called earlier (mhome index 0 configuration, as shown in step 1), otherwise tfConfigInterface() should be called instead. If a collision has been detected, then the user should cancel the collision detection (by calling tfCancelCollisionDetection() on the current IP address), and try another IP address, by calling tfConfigAutoIp() or similar function again. The tfAutoIpFinish() call back function provided as an example in 'examples\txautoip.c', and shown below does all this. It also cancels the collision detection if no collision occurred.

  4. Monitoring the network for collision after finishing the configuration: After the interface has been successfully configured, the user should still monitor the network for collision. This could be done by not canceling the collision detection in the call back function, and modifying the call back function to handle collision detection, when the interface has been configured.


Enabling AUTO IP

You must define TM_USE_AUTO_IP in <trsystem.h> to enable the AUTO IP code.


Example

The following code sample can be found in 'examples\txautoip.c':

 
...
    errorCode = tfOpenInterface(interfaceHandle,
                               0,
                               0,
                               TM_DEV_IP_USER_BOOT,
                               1,
                               0);
    if (errorCode == TM_ENOERROR)
    {
        errorCode = tfConfigAutoIp(interfaceHandle, 0);
    }
...
 
int tfConfigAutoIp(ttUserInterface interfaceHandle,
                   int             mhomeIndex)
{
    ttUserGenericUnion autoIpParam;
    ttUserIpAddress    ipAddress;
    int                errorCode;
 
    do
    {
/* Pick a random AUTO IP address */
        ipAddress = tfAutoIPPickIpAddress();
        if (ipAddress != (ttUserIpAddress)0)
        {
            autoIpParam.genIntParm = mhomeIndex;
/* Register the call back function for that IP address with the stack*/
            errorCode = tfUseCollisionDetection(ipAddress,
                                                tfAutoIpFinish,
                                                autoIpParam);
        }
        else
        {
            errorCode = TM_ENOENT;
        }
    } while (errorCode == TM_EADDRINUSE);
 
    if (errorCode == TM_ENOERROR)
    {
/* Selected AUTO IP address is not in the ARP cache */
/* Start sending ARP probes on the interface */
/* We use the default probe interval (2s), and number of probes (4) */
        errorCode = tfUserStartArpSend (interfaceHandle, ipAddress, 0, 0, 0);
    }
    return errorCode;
}
 
int tfAutoIpFinish(ttUserInterface    interfaceHandle,
                   ttUserIpAddress    ipAddress,
                   int                errorCode,
                   ttUserGenericUnion autoIpParam)
{
    int mhomeIndex;
 
/* Cancel the collision detection check on that IP address */
    (void)tfCancelCollisionDetection(ipAddress);
    mhomeIndex = autoIpParam.genIntParm;
    if (errorCode == TM_ENOERROR)
    {
/* No collision occurred. Finish configuring the interface */
        tlConfigured = 1;
        if (mhomeIndex == 0)
        {
/* User used tfOpenInterface with TM_DEV_IP_USER_BOOT on mhome index 0 */
         errorCode = tfFinishOpenInterface(interfaceHandle,
                                           ipAddress,
                                           TM_IP_LOCAL_NETMASK);
            if (errorCode == TM_ENOERROR)
            {
                printf("tfFinishOpenInterface with 0x%x\n", ipAddress);
            }
            else
            {
                printf("tfFinishOpenInterface with 0x%x failed `%s'\n",
                       ipAddress,
                       tfStrError(errorCode));
            }
        }
        else
        {
/* User had already opened the interface on another mhome */
            errorCode = tfConfigInterface(interfaceHandle,
                                          ipAddress,
                                          TM_IP_LOCAL_NETMASK,
                                          0, /* not used. */
                                          1, /*  has to be one */
                                          (unsigned char)mhomeIndex);
 
            if (errorCode == TM_ENOERROR)
            {
                printf("tfConfigInterface with 0x%x\n", ipAddress);
            }
            else
            {
                printf("tfConfigInterface with 0x%x failed `%s'\n",
                       ipAddress,
                       tfStrError(errorCode));
            }
        }
    }
    else
    {
/* A collision occurred on the IP address, try another IP address */
        tfConfigAutoIp(interfaceHandle, mhomeIndex);
    }
    return 0;
}
 

Function Calls


Table of Contents >> Optional Protocols