#include <sys/param.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_ether.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#ifdef _STANDALONE
#include <lib/libkern/libkern.h>
#else
#include <string.h>
#endif
#include "stand.h"
#include "net.h"
struct ether_arp {
struct arphdr ea_hdr;
u_int8_t arp_sha[ETHER_ADDR_LEN];
u_int8_t arp_spa[4];
u_int8_t arp_tha[ETHER_ADDR_LEN];
u_int8_t arp_tpa[4];
};
#define arp_hrd ea_hdr.ar_hrd
#define arp_pro ea_hdr.ar_pro
#define arp_hln ea_hdr.ar_hln
#define arp_pln ea_hdr.ar_pln
#define arp_op ea_hdr.ar_op
static ssize_t rarpsend(struct iodesc *, void *, size_t);
static ssize_t rarprecv(struct iodesc *, void *, size_t, saseconds_t);
int
rarp_getipaddress(int sock)
{
struct iodesc *d;
struct ether_arp *ap;
struct {
u_char header[ETHERNET_HEADER_SIZE];
struct {
struct ether_arp arp;
u_char pad[18];
} data;
} wbuf;
struct {
u_char header[ETHERNET_HEADER_SIZE];
struct {
struct ether_arp arp;
u_char pad[24];
} data;
} rbuf;
#ifdef RARP_DEBUG
if (debug)
printf("%s: socket=%d\n", __func__, sock);
#endif
if (!(d = socktodesc(sock))) {
printf("%s: bad socket. %d\n", __func__, sock);
return -1;
}
#ifdef RARP_DEBUG
if (debug)
printf("%s: d=%p\n", __func__, d);
#endif
(void)memset(&wbuf.data, 0, sizeof(wbuf.data));
ap = &wbuf.data.arp;
ap->arp_hrd = htons(ARPHRD_ETHER);
ap->arp_pro = htons(ETHERTYPE_IP);
ap->arp_hln = sizeof(ap->arp_sha);
ap->arp_pln = sizeof(ap->arp_spa);
ap->arp_op = htons(ARPOP_REVREQUEST);
(void)memcpy(ap->arp_sha, d->myea, ETHER_ADDR_LEN);
(void)memcpy(ap->arp_tha, d->myea, ETHER_ADDR_LEN);
if (sendrecv(d,
rarpsend, &wbuf.data, sizeof(wbuf.data),
rarprecv, &rbuf.data, sizeof(rbuf.data)) < 0)
{
printf("No response for RARP request\n");
return -1;
}
ap = &rbuf.data.arp;
(void)memcpy(&myip, ap->arp_tpa, sizeof(myip));
#if 0
(void)memcpy(&rootip, ap->arp_spa, sizeof(rootip));
#endif
if (IN_CLASSA(myip.s_addr))
netmask = IN_CLASSA_NET;
else if (IN_CLASSB(myip.s_addr))
netmask = IN_CLASSB_NET;
else
netmask = IN_CLASSC_NET;
d->myip = myip;
return 0;
}
static ssize_t
rarpsend(struct iodesc *d, void *pkt, size_t len)
{
#ifdef RARP_DEBUG
if (debug)
printf("%s: called\n", __func__);
#endif
return sendether(d, pkt, len, bcea, ETHERTYPE_REVARP);
}
static ssize_t
rarprecv(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
{
ssize_t n;
struct ether_arp *ap;
u_int16_t etype;
#ifdef RARP_DEBUG
if (debug)
printf("%s: ", __func__);
#endif
n = readether(d, pkt, len, tleft, &etype);
errno = 0;
if (n == -1 || (size_t)n < sizeof(struct ether_arp)) {
#ifdef RARP_DEBUG
if (debug)
printf("bad len=%d\n", (int)n);
#endif
return -1;
}
if (etype != ETHERTYPE_REVARP) {
#ifdef RARP_DEBUG
if (debug)
printf("bad type=0x%x\n", etype);
#endif
return -1;
}
ap = (struct ether_arp *)pkt;
if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
ap->arp_pro != htons(ETHERTYPE_IP) ||
ap->arp_hln != sizeof(ap->arp_sha) ||
ap->arp_pln != sizeof(ap->arp_spa) )
{
#ifdef RARP_DEBUG
if (debug)
printf("bad hrd/pro/hln/pln\n");
#endif
return -1;
}
if (ap->arp_op != htons(ARPOP_REVREPLY)) {
#ifdef RARP_DEBUG
if (debug)
printf("bad op=0x%x\n", ntohs(ap->arp_op));
#endif
return -1;
}
if (memcmp(ap->arp_tha, d->myea, ETHER_ADDR_LEN)) {
#ifdef RARP_DEBUG
if (debug)
printf("unwanted address\n");
#endif
return -1;
}
#ifdef RARP_DEBUG
if (debug)
printf("got it\n");
#endif
return n;
}