/*
 *
 *     Module Name: naaddrconflict.c
 *         Version: 1.00
 *   Original Date: 07/27/04    
 *          Author: Contact NetSilicon support for questions on this code
 *        Language: Ansi C
 * Compile Options:
 * Compile defines:
 *       Libraries:
 *    Link Options:
 *
 *    Entry Points: customizeReadPowerOnButtons
 *
 *  Copyright (c) Digi International Inc., All Rights Reserved
 *  2004
 *  
 *  This software contains proprietary and confidential information of Digi
 *  International Inc.  By accepting transfer of this copy, Recipient agrees
 *  to retain this software in confidence, to prevent disclosure to others,
 *  and to make no use of this software other than that for which it was
 *  delivered.  This is an unpublished copyrighted work of Digi International
 *  Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 *  prohibited.
 *  
 *  Restricted Rights Legend
 * 
 *  Use, duplication, or disclosure by the Government is subject to
 *  restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 *  Technical Data and Computer Software clause at DFARS 252.227-7031 or
 *  subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 *  Restricted Rights at 48 CFR 52.227-19, as applicable.
 * 
 *  Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 * Description.
 * =======================================================================
 * NAAddrConflict() is a template implementation of the application-level IP address 
 * conflict callback
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "npttypes.h"
#include "narmapi.h"
#include "bsp.h"
#include "naifconfig.h"
#include "ace_params.h"


#ifdef BSP_ENABLE_ADDR_CONFLICT_DETECTION

static void printAddrConflict(const char * ifname, WORD32 ipaddr, int protocol);

/**
 * This is a template implementation of the application-level IP address conflict callback.
 *
 * This routine is registered by the customizeStartAce to be called by ACE-level callback when
 * the IP address conflict occurs during the initial IP address configuration.
 *
 * This callback function will be called if the calflict IP address is received by BOOTP, RARP, Ping ARP, 
 * or static IP address assignement protocol. The callback function can also be called if the same conflict 
 * IP address was sent by DHCP server 2nd time, after the DHCP client has sent DHCP_DECLINE message.
 * 
 * @param ifname    Interface name, for example, eth0 or eth0:0
 * @param ipaddr    Conflict IP address, already in use by another network device
 * @param cb_data   Additional data  
 *
 * @return action   Action to be performed, after this callback returns
 *
 * @since 6.2
 * @external
 * @category Interface_Config
 */
NAAddrConflictActionType NAAddrConflict(const char * ifname, WORD32 ipaddr, NAAddrConflictCallbackDataType * cb_data)
{
    NAAddrConflictActionType result;

    printAddrConflict(ifname, ipaddr, cb_data->protocol);

    if (cb_data->ip_configured == FALSE)
    {
        if (cb_data->num_conflicts >= BSP_MAX_ADDR_CONFLICTS)
        {
            result = NAStopOnAddrConflict;
            printf("Exceeded Maxinum number of IP address conflicts.\nStopping ACE on %s\n", ifname);
	    }
        else
        {
            result = NAAddProtocolOnConflict;
	        cb_data->add_protocol = ACE_PROT_AUTOIP;
        }
    }
    else
    {
        result = NAContinueOnAddrConflict;
    }

    return result;
}


static void printAddrConflict(const char *ifname, WORD32 ipaddr, int protocol)
{
    char ip_addr_str[20];

    NAInet_toa(ipaddr, ip_addr_str);
    NA_DBPF("IP address conflict on %s: %s, protocol: %d\n", ifname, ip_addr_str, protocol);
    printf("IP address conflict on %s: %s, protocol: %d\n", ifname, ip_addr_str, protocol);
}
#else

NAAddrConflictActionType NAAddrConflict(const char *ifname, WORD32 ipaddr,  NAAddrConflictCallbackDataType *cb_data)
{
    return NAContinueOnAddrConflict;
}

#endif


