#include "sboot.h"
#include "etherfun.h"
void
do_rev_arp(void)
{
int i;
for (i = 0; i < 6; i++) {
eh->ether_dhost[i] = 0xff;
}
memcpy(eh->ether_shost, myea, 6);
eh->ether_type = ETYPE_RARP;
rarp->ar_hrd = 1;
rarp->ar_pro = PTYPE_IP;
rarp->ar_hln = 6;
rarp->ar_pln = 4;
rarp->ar_op = OPCODE_RARP;
memcpy(rarp->arp_sha, myea, sizeof(myea));
memcpy(rarp->arp_tha, myea, sizeof(myea));
for (i = 0; i < 4; i++) {
rarp->arp_spa[i] = rarp->arp_tpa[i] = 0x00;
}
le_put(buf, 76);
}
int
get_rev_arp(void)
{
le_get(buf, sizeof(buf), 6);
if (eh->ether_type == ETYPE_RARP && rarp->ar_op == OPCODE_REPLY) {
memcpy(myip, rarp->arp_tpa, sizeof(rarp->arp_tpa));
memcpy(servip, rarp->arp_spa, sizeof(rarp->arp_spa));
memcpy(servea, rarp->arp_sha, sizeof(rarp->arp_sha));
return 1;
}
return 0;
}
int
rev_arp(void)
{
int tries = 0;
while (tries < 5) {
do_rev_arp();
if (get_rev_arp()) {
return 1;
}
tries++;
}
return 0;
}
const char HEXDIGITS[16] = "0123456789ABCDEF";
void
do_send_tftp(int mesgtype)
{
u_long res, iptmp, lcv;
u_char *tot;
if (mesgtype == 0) {
tot = tftp_r + (sizeof(MSG) - 1);
myport = (u_short)time();
if (myport < 1000)
myport += 1000;
servport = FTP_PORT;
} else {
tot = (u_char *)tftp_a + 4;
}
memcpy (eh->ether_dhost, servea, sizeof(servea));
memcpy (eh->ether_shost, myea, sizeof(myea));
eh->ether_type = ETYPE_IP;
iph->ip_v = IP_VERSION;
iph->ip_hl = IP_HLEN;
iph->ip_tos = 0;
iph->ip_id = 0;
iph->ip_off = IP_DF;
iph->ip_ttl = 3;
iph->ip_p = IPP_UDP;
memcpy(iph->ip_src, myip, sizeof(myip));
memcpy(iph->ip_dst, servip, sizeof(servip));
iph->ip_sum = 0;
iph->ip_len = tot - (u_char *)iph;
res = oc_cksum(iph, sizeof(struct ip), 0);
iph->ip_sum = 0xffff & ~res;
udph->uh_sport = myport;
udph->uh_dport = servport;
udph->uh_sum = 0;
if (mesgtype) {
tftp_a->op_code = FTPOP_ACKN;
tftp_a->block = (u_short)(mesgtype);
} else {
memcpy (&iptmp, myip, sizeof(iptmp));
memcpy(tftp_r, MSG, (sizeof(MSG)-1));
for (lcv = 9; lcv >= 2; lcv--) {
tftp_r[lcv] = HEXDIGITS[iptmp & 0xF];
iptmp = iptmp >> 4;
}
}
udph->uh_ulen = tot - (u_char *)udph;
le_put(buf, tot - buf);
}
int
do_get_file(void)
{
int fail = 0, oldlen;
char *loadat = (char *)LOAD_ADDR;
last_ack = 0;
do_send_tftp(READ);
for (;;) {
if (le_get(buf, sizeof(buf), 5) == 0) {
if (last_ack) {
do_send_tftp(last_ack);
} else {
do_send_tftp(READ);
}
fail++;
if (fail > 5) {
printf("\n");
return 1;
}
} else {
printf("%x \r", tftp->info.block*512);
if ((eh->ether_type != ETYPE_IP) ||
(iph->ip_p != IPP_UDP)) {
fail++;
continue;
}
if (servport == FTP_PORT) servport = udph->uh_sport;
if (tftp->info.op_code == FTPOP_ERR) {
printf("TFTP: Download error %d: %s\n",
tftp->info.block, tftp->data);
return 1;
}
if (tftp->info.block != last_ack + 1) {
if (tftp->info.block < last_ack +1) {
do_send_tftp(tftp->info.block);
} else {
do_send_tftp(last_ack);
}
fail++;
} else {
fail = 0;
last_ack++;
oldlen = udph->uh_ulen;
do_send_tftp( last_ack );
#if 0
printf("memcpy %x %x %d\n", loadat,
&tftp->data, oldlen - 12);
#endif
memcpy(loadat, &tftp->data, oldlen - 12);
loadat += oldlen - 12;
if (oldlen < (8 + 4 + 512)) {
printf("\n");
return 0;
}
}
}
}
printf("\n");
return 0;
}