#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/in_systm.h>
#include <string.h>
#include "stand.h"
#include "net.h"
#define ARP_NUM 8
struct arp_list {
struct in_addr addr;
u_char ea[6];
} arp_list[ARP_NUM] = {
{ {0xffffffff}, BA }
};
int arp_num = 1;
static ssize_t arpsend(struct iodesc *, void *, size_t);
static ssize_t arprecv(struct iodesc *, void **, void **, time_t, void *);
u_char *
arpwhohas(struct iodesc *d, struct in_addr addr)
{
int i;
struct ether_arp *ah;
struct arp_list *al;
void *pkt;
struct {
struct ether_header eh;
struct {
struct ether_arp arp;
u_char pad[18];
} data;
} wbuf;
for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
if (addr.s_addr == al->addr.s_addr)
return (al->ea);
if (arp_num > ARP_NUM - 1) {
arp_num = 1;
printf("arpwhohas: overflowed arp_list!\n");
}
#ifdef ARP_DEBUG
if (debug)
printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
#endif
bzero((char*)&wbuf.data, sizeof(wbuf.data));
ah = &wbuf.data.arp;
ah->arp_hrd = htons(ARPHRD_ETHER);
ah->arp_pro = htons(ETHERTYPE_IP);
ah->arp_hln = sizeof(ah->arp_sha);
ah->arp_pln = sizeof(ah->arp_spa);
ah->arp_op = htons(ARPOP_REQUEST);
MACPY(d->myea, ah->arp_sha);
bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
al->addr = addr;
pkt = NULL;
ah = NULL;
i = sendrecv(d,
arpsend, &wbuf.data, sizeof(wbuf.data),
arprecv, &pkt, (void **)&ah, NULL);
if (i == -1) {
panic("arp: no response for %s",
inet_ntoa(addr));
}
#ifdef ARP_DEBUG
if (debug) {
struct ether_header *eh;
eh = (struct ether_header *)((uintptr_t)pkt + ETHER_ALIGN);
printf("arp: response from %s\n",
ether_sprintf(eh->ether_shost));
printf("arp: cacheing %s --> %s\n",
inet_ntoa(addr), ether_sprintf(ah->arp_sha));
}
#endif
MACPY(ah->arp_sha, al->ea);
++arp_num;
free(pkt);
return (al->ea);
}
static ssize_t
arpsend(struct iodesc *d, void *pkt, size_t len)
{
#ifdef ARP_DEBUG
if (debug)
printf("arpsend: called\n");
#endif
return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
}
static ssize_t
arprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft, void *extra)
{
ssize_t n;
struct ether_arp *ah;
uint16_t etype;
void *ptr;
#ifdef ARP_DEBUG
if (debug)
printf("arprecv: ");
#endif
ptr = NULL;
n = readether(d, &ptr, (void **)&ah, tleft, &etype);
errno = 0;
if (n == -1 || n < sizeof(struct ether_arp)) {
#ifdef ARP_DEBUG
if (debug)
printf("bad len=%zd\n", n);
#endif
free(ptr);
return (-1);
}
if (etype != ETHERTYPE_ARP) {
#ifdef ARP_DEBUG
if (debug)
printf("not arp type=%d\n", etype);
#endif
free(ptr);
return (-1);
}
if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
ah->arp_pro != htons(ETHERTYPE_IP) ||
ah->arp_hln != sizeof(ah->arp_sha) ||
ah->arp_pln != sizeof(ah->arp_spa) )
{
#ifdef ARP_DEBUG
if (debug)
printf("bad hrd/pro/hln/pln\n");
#endif
free(ptr);
return (-1);
}
if (ah->arp_op == htons(ARPOP_REQUEST)) {
#ifdef ARP_DEBUG
if (debug)
printf("is request\n");
#endif
arp_reply(d, ah);
free(ptr);
return (-1);
}
if (ah->arp_op != htons(ARPOP_REPLY)) {
#ifdef ARP_DEBUG
if (debug)
printf("not ARP reply\n");
#endif
free(ptr);
return (-1);
}
if (bcmp(&arp_list[arp_num].addr,
ah->arp_spa, sizeof(ah->arp_spa)))
{
#ifdef ARP_DEBUG
if (debug)
printf("unwanted address\n");
#endif
free(ptr);
return (-1);
}
#ifdef ARP_DEBUG
if (debug)
printf("got it\n");
#endif
*pkt = ptr;
*payload = ah;
return (n);
}
void
arp_reply(struct iodesc *d, void *pkt)
{
struct ether_arp *arp = pkt;
if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
arp->arp_pro != htons(ETHERTYPE_IP) ||
arp->arp_hln != sizeof(arp->arp_sha) ||
arp->arp_pln != sizeof(arp->arp_spa) )
{
#ifdef ARP_DEBUG
if (debug)
printf("arp_reply: bad hrd/pro/hln/pln\n");
#endif
return;
}
if (arp->arp_op != htons(ARPOP_REQUEST)) {
#ifdef ARP_DEBUG
if (debug)
printf("arp_reply: not request!\n");
#endif
return;
}
if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
return;
#ifdef ARP_DEBUG
if (debug) {
printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
}
#endif
arp->arp_op = htons(ARPOP_REPLY);
bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
bcopy(d->myea, arp->arp_sha, sizeof(arp->arp_sha));
bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
(void) sendether(d, pkt, sizeof(*arp) + 18,
arp->arp_tha, ETHERTYPE_ARP);
}