/* itf.c - IP interface registry */
 
/* Written 1995-1997 by Werner Almesberger, EPFL-LRC */
 

#include <stdlib.h>
#include <string.h>
#include <linux/atmclip.h>

#include "atmd.h"

#include "table.h"
#include "io.h"
#include "itf.h"
#include "arp.h"


#define COMPONENT "ITF"


ITF *lookup_itf(int number)
{
    ITF *itf;

    for (itf = itfs; itf; itf = itf->next)
	if (itf->number == number) break;
    return itf;
}


ITF *lookup_itf_by_ip(unsigned long ip)
{
    ITF *itf;

    for (itf = itfs; itf; itf = itf->next)
	if (!((itf->local_ip ^ ip) & itf->netmask)) break;
    return itf;
}


int itf_create(int number)
{
    diag(COMPONENT,DIAG_DEBUG,"ITF CREATE %d",number);
    return 0;
}


void itf_up(int number)
{
    ITF *itf;

    diag(COMPONENT,DIAG_DEBUG,"ITF UP %d",number);
    for (itf = itfs; itf; itf = itf->next)
	if (itf->number == number) {
	    diag(COMPONENT,DIAG_ERROR,"interface is already active");
	    return;
	}
    itf = alloc_t(ITF);
    if (ip_itf_info(number,&itf->local_ip,&itf->netmask,&itf->mtu) < 0) {
	free(itf);
	return;
    }
    itf->number = number;
    memset(&itf->qos,0,sizeof(struct atm_qos));
    itf->qos.aal = ATM_AAL5;
    itf->qos.txtp.traffic_class = ATM_UBR;
    itf->qos.txtp.max_sdu = RFC1483LLC_LEN+RFC1626_MTU;
    itf->qos.rxtp = itf->qos.txtp;
    itf->table = itf->arp_srv = NULL;
    Q_INSERT_HEAD(itfs,itf);
}


void itf_down(int number)
{
    ITF *itf;
    ENTRY *entry,*next;

    diag(COMPONENT,DIAG_DEBUG,"ITF DOWN %d",number);
    for (itf = itfs; itf; itf = itf->next)
	if (itf->number == number) break;
    if (!itf) {
	diag(COMPONENT,DIAG_ERROR,"no such interface");
	return;
    }
    for (entry = itf->table; entry; entry = next) {
	next = entry->next;
	discard_entry(entry);
    }
    Q_REMOVE(itfs,itf);
    free(itf);
}
