/* Copyright 1985 by the Massachusetts Institute of Technology */
#include <notice.h>

/* ARP support for internet */

/* Written by David Bridgham (Nov 1985)
 */

#include <types.h>
#include <sys.h>
#include "../src/const.h"
#include "../src/param.h"
#include "../src/defs.h"
#include "../src/macs.h"
#include "../src/net.h"
#include "../src/ext.h"

#include "../in/in.h"
#include "../in/inext.h"
#include "../in/inrte.h"
#include "arp.h"

/* Returns ~0 if this ARP packet should be replied to, that is if the
 * address being requested is one of my addresses..  Else returns 0.
 */
arp_ip_myaddp(netp, iaddr)
net *netp;
reg inaddr *iaddr;
{
    reg inia *inina;

    inina = iniatlst[netp->n_net];
    while (inina != NULL) {
	if (iaddr->i_long == mkina(inina->inia_addr)->i_long)
	  return ~0;
	inina = inina->inia_link;
    }
    return 0;
}

/* Returns ~0 if this ARP packet should be replied to.  Else returns 0.
 * Use this routine instead of the previous one if you want ARP routing.
 * This routine returns TRUE if I know a route to the address requested
 * and that route does not go back out on the net that the packet came
 * in on or the address is one of mine.
 */
arprte_ip_myaddp(netp, iaddr)
net *netp;
reg inaddr *iaddr;
{
    reg iprte *ipr;

    /* If one of my addresses return TRUE. */
    if (arp_ip_myaddp(netp, iaddr))
      return ~0;
    /* If no route to address return FALSE. */
    if ((ipr = ipr_lookup(iaddr->i_long)) == NULL || ipr->ipr_netp == NULL)
      return 0;
    /* If route goes back onto same net return FALSE. */
    if (ipr->ipr_netp == netp)
      return 0;
    return ~0;
}


/* Returns a pointer to my internet address for this net */
unsb *arp_ip_myaddr(netp)
net *netp;
{
    return ((unsb *)(iniatlst[netp->n_net]->inia_addr));
}

/* Fills a character buffer with an internet address for printing. */
char *arp_ip_paddr(buflen, buf, iaddr)
int buflen;
char *buf;
inaddr *iaddr;
{
    char *snprintf();

    return (snprintf(buflen, buf, "%d.%d.%d.%d",
		     iaddr->i_byte.i_byteh,
		     iaddr->i_byte.i_byteu,
		     iaddr->i_byte.i_bytem,
		     iaddr->i_byte.i_bytel));
}

