/*
 *            Copyright (c) 1998-2003 by NETsilicon Inc.
 *
 *  This software is copyrighted by and is the sole property of 
 *  NETsilicon.  All rights, title, ownership, or other interests
 *  in the software remain the property of NETsilicon.  This
 *  software may only be used in accordance with the corresponding
 *  license agreement.  Any unauthorized use, duplication, transmission,  
 *  distribution, or disclosure of this software is expressly forbidden.
 *
 *  This Copyright notice may not be removed or modified without prior
 *  written consent of NETsilicon.
 *
 *  NETsilicon, reserves the right to modify this software
 *  without notice.
 *
 *  NETsilicon
 *  411 Waverley Oaks Road                  USA 781.647.1234
 *  Suite 304                               http://www.netsilicon.com
 *  Waltham, MA 02452                       AmericaSales@netsilicon.com
 *
 *************************************************************************
 *
 *  Description
 *  ====================================================================
 *  This file contains the definitions for network interface configuration.
 *
 */

#ifndef naifconfig_h

#define naifconfig_h

#include "npttypes.h"
#include "std.h"

#ifdef __cplusplus
extern "C"
{
#endif


#define NETOS_MAX_IF_NAMELEN     (MAX_DEV_NAME + 4)

/**
 * A structure of this type stores the configuration for IP address conflict detection  
 * for the network device. IP address conflict detection is used for the initial IP address 
 * assignement using BOOTP, RARP, Ping ARP, and static IP address protocols.
 *
 * @param enabled               IP address conflict detection is enabled for this device
 * @param num_probes            Number of ARP probes, sent to detect IP address conflict
 * @param probe_interval_ms     An interval in milliseconds between ARP probes
 * @param num_announcements     Number of ARP address announcements, sent on Ethenet, after 
 *                              the IP address has been configured
 * @param announce_interval_ms  An interval in milliseconds between ARP address announcements
 *
 * @since 6.2
 * @external
 * @category Interface_Config
 */
typedef struct
{
    BOOLEAN enabled;
    int     num_probes;
    int     probe_interval_ms;
    int     num_announcements;
    int     announce_interval_ms;

} NAAddrConflictConfigType;

/**
 * This type defines actions that should be taken if IP address conflict occurs during
 * initial IP address configuration. This action is returned by an IP address conflict 
 * callback of NAAddrConflictCallbackType type.
 * 
 * The protocol that caused IP address conflict is 
 * automatically stopped by ACE.
 *
 * @param NAStopOnAddrConflict          Stop ACE on the virtual interface
 * @param NAContinueOnAddrConflict      Continue running ACE without changes. This chice is 
 *                                      recommended if another protocol, that expects to receive
 *                                      IP address is already running
 * @param NAUpdateOnAddrConflict        Stop and restart ACE on the virtual interface
 * @param NAGetNetworkAddrOnConflict    Stop ACE. Disable ACE_PROT_STATIC, 
 *                                      enable network protocols, that are enabled in the
 *                                      board parameters record, and restart ACE.
 * @param NAGetStaticAddrOnConflict     Add ACE_PROT_STATIC protocol with the startup delay 0
 * @param NAAddProtocolOnConflict       Add any protocol with the startup delay 0
 *
 * @since 6.2
 * @external
 * @category Interface_Config
 */
typedef enum 
{ 
    NAStopOnAddrConflict, 
    NAContinueOnAddrConflict, 
    NAUpdateOnAddrConflict, 
    NAGetNetworkAddrOnConflict, 
    NAGetStaticAddrOnConflict,
    NAAddProtocolOnConflict
} NAAddrConflictActionType;


/**
 * A structure of this type is used as an argument by an IP address conflict callback 
 * of NAAddrConflictCallbackType type.
 *
 * @param protocol          Protocol that caused IP address conflict
 * @param ip_configured     TRUE - IP address is already configured on this virtual 
 *                          interface. This can occur if some of the protocols had   
 *                          ACE_NEVER_SHUTDOWN type
 * @param num_conflicts     Number of IP address conflicts while trying to configure 
 *                          this virtual interface.
 * @param add_protocol      The callback assignes this field if it returns NAAddProtocolOnConflict
 *
 * @since 6.2
 * @external
 * @category Interface_Config
 */
typedef struct
{
   int      protocol;
   BOOLEAN  ip_configured;
   int      num_conflicts;
   int      add_protocol;
    
} NAAddrConflictCallbackDataType;

/**
 * A function of this type is called, if registered, if an IP address conflict occurs during 
 * 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    Virtual 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
 */
typedef NAAddrConflictActionType (* NAAddrConflictCallbackType)(const char * ifname, WORD32 ipaddr, NAAddrConflictCallbackDataType * cb_data); 


/* Register IP address conflict callback of NAAddrConflictCallbackType type. */
void NARegisterAddrConflictCallback(NAAddrConflictCallbackType addr_cinflict_fn);

/* 
 * Blocking function to detect IP address conflict during initial IP address configuration.
 * Used by ACE.
 */
int NADetectAddressConflict(const char * ifname, WORD32 probe_addr, unsigned int probe_count, unsigned int interval_ms);

/* 
 * Non-blocking function to send ARP address announecements after IP address is configured. 
 * Used by ACE.
 */
int NASendAddressAnnouncement(const char * ifname,  WORD32 addr, unsigned int num_announcements, unsigned int interval_ms);

/* 
 * Non-blocking function to send ARP address probes. Intended for on-going IP address conflict detection
 */
int NASendAddressProbe(char * ifname,  WORD32 addr, unsigned int num_probes, unsigned int interval_ms);

/* 
 * Get configuration data for IP address conflict detection for the network interface 
 * Used by ACE.
 */
int NAGetAddrConflictData(char const * ifname, NAAddrConflictConfigType * data);

/**
 * A function of this type is called, if registered, when ARP protocol detects that another network device  
 * uses our, already configured IP address. This function must not block.
 *
 * @param devname       network device name, for example, eth0
 * @param ipaddr        Conflict IP address, in use by another network device
 * @param phys_addr     Physical address of another network device, using our Ip address     
 * @param phys_addr_len Size in bytes of phys_addr
 *
 * @since 6.2
 * @external
 * @category Interface_Config
 */
typedef void (* NADuplicateAddrCallbackType)(const char * devname, WORD32 ipaddr, char * phys_addr, int phys_addr_len);

/* Register a duplicate IP address callback of NADuplicateAddrCallbackType type. */
int NARegisterDuplicateAddrCallback(const char * ifname, NADuplicateAddrCallbackType dup_addr_fn);



BOOLEAN NAIfconfigIsDeviceUp(const char * ifname);
void    NAIfconfigDeviceFromInterface(char * devname, const char * ifname);
int     NAIfconfigBringDeviceUp(const char * ifname);
int     NAIfconfigSetIpAddress(const char * ifname, WORD32 ipaddr, WORD32 netmask, int mtu);


#ifdef __cplusplus
}
#endif
#endif

