#include <sys/param.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <netinet/in_systm.h>
#include <arpa/tftp.h>
#include <string.h>
#include "stand.h"
#include "net.h"
#include "netif.h"
#include "tftp.h"
static int tftp_open(const char *path, struct open_file *f);
static int tftp_close(struct open_file *f);
static int tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid);
static int tftp_write(struct open_file *f, void *buf, size_t size, size_t *resid);
static off_t tftp_seek(struct open_file *f, off_t offset, int where);
static int tftp_stat(struct open_file *f, struct stat *sb);
struct fs_ops tftp_fsops = {
"tftp",
tftp_open,
tftp_close,
tftp_read,
tftp_write,
tftp_seek,
tftp_stat,
null_readdir
};
extern struct in_addr servip;
static int tftpport = 2000;
#define RSPACE 520
struct tftp_handle {
struct iodesc *iodesc;
int currblock;
int islastblock;
int validsize;
int off;
char *path;
struct {
u_char header[HEADER_SIZE];
struct tftphdr t;
u_char space[RSPACE];
} __packed __aligned(4) lastdata;
};
static int tftperrors[8] = {
0,
ENOENT,
EPERM,
ENOSPC,
EINVAL,
EINVAL,
EEXIST,
EINVAL
};
static ssize_t
recvtftp(struct iodesc *d, void *pkt, size_t max_len, time_t tleft)
{
struct tftphdr *t;
ssize_t len;
ssize_t tmp_len;
errno = 0;
bzero(pkt, max_len);
if (d->xid == 1) {
len = -1;
while ((tmp_len = readudp(d, pkt, max_len, tleft)) > 0) {
len = tmp_len;
t = (struct tftphdr *)pkt;
if (ntohs(t->th_opcode) == DATA)
break;
}
} else {
len = readudp(d, pkt, max_len, tleft);
}
if ((int)len < (int)sizeof(*t))
return (-1);
t = (struct tftphdr *)pkt;
errno = 0;
switch (ntohs(t->th_opcode)) {
case DATA: {
int got;
if (htons(t->th_block) != (uint16_t)d->xid) {
return (-1);
}
if (d->xid == 1) {
struct udphdr *uh;
struct ip *ip;
uh = (struct udphdr *) pkt - 1;
ip = (struct ip *)uh - 1;
d->destport = uh->uh_sport;
d->destip = ip->ip_src;
}
got = len - (t->th_data - (char *)t);
return got;
}
case ERROR:
if ((unsigned) ntohs(t->th_code) >= 8) {
printf("illegal tftp error %d\n", ntohs(t->th_code));
errno = EIO;
} else {
#ifdef DEBUG
printf("tftp-error %d\n", ntohs(t->th_code));
#endif
errno = tftperrors[ntohs(t->th_code)];
}
return (-1);
default:
#ifdef DEBUG
printf("tftp type %d not handled\n", ntohs(t->th_opcode));
#endif
return (-1);
}
}
static int
tftp_makereq(struct tftp_handle *h)
{
struct {
u_char header[HEADER_SIZE];
struct tftphdr t;
u_char space[FNAME_SIZE + 6];
} __packed __aligned(4) wbuf;
char *wtail;
int l;
ssize_t res;
struct tftphdr *t;
wbuf.t.th_opcode = htons((u_short) RRQ);
wtail = wbuf.t.th_stuff;
l = strlen(h->path);
bcopy(h->path, wtail, l + 1);
wtail += l + 1;
bcopy("octet", wtail, 6);
wtail += 6;
t = &h->lastdata.t;
h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
h->iodesc->destport = htons(IPPORT_TFTP);
h->iodesc->xid = 1;
res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
recvtftp, t, sizeof(*t) + RSPACE);
if (res == -1)
return (errno);
h->currblock = 1;
h->validsize = res;
h->islastblock = 0;
if (res < SEGSIZE)
h->islastblock = 1;
return (0);
}
static int
tftp_getnextblock(struct tftp_handle *h)
{
struct {
u_char header[HEADER_SIZE];
struct tftphdr t;
} __packed __aligned(4) wbuf;
char *wtail;
int res;
struct tftphdr *t;
wbuf.t.th_opcode = htons((u_short) ACK);
wtail = (char *) &wbuf.t.th_block;
wbuf.t.th_block = htons((u_short) h->currblock);
wtail += 2;
t = &h->lastdata.t;
h->iodesc->xid = h->currblock + 1;
res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
recvtftp, t, sizeof(*t) + RSPACE);
if (res == -1)
return (errno);
h->currblock++;
h->validsize = res;
if (res < SEGSIZE)
h->islastblock = 1;
return (0);
}
static int
tftp_open(const char *path, struct open_file *f)
{
struct tftp_handle *tftpfile;
struct iodesc *io;
int res;
#ifndef __i386__
if (strcmp(f->f_dev->dv_name, "net") != 0)
return (EINVAL);
#endif
tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile));
if (!tftpfile)
return (ENOMEM);
tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
if (io == NULL) {
free(tftpfile);
return (EINVAL);
}
io->destip = servip;
tftpfile->off = 0;
tftpfile->path = strdup(path);
if (tftpfile->path == NULL) {
free(tftpfile);
return(ENOMEM);
}
res = tftp_makereq(tftpfile);
if (res) {
free(tftpfile->path);
free(tftpfile);
return (res);
}
f->f_fsdata = (void *) tftpfile;
return (0);
}
static int
tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid)
{
struct tftp_handle *tftpfile;
static int tc = 0;
tftpfile = (struct tftp_handle *) f->f_fsdata;
while (size > 0) {
int needblock, count;
if (!(tc++ % 256))
twiddle();
needblock = tftpfile->off / SEGSIZE + 1;
if (tftpfile->currblock > needblock)
tftp_makereq(tftpfile);
while (tftpfile->currblock < needblock) {
int res;
res = tftp_getnextblock(tftpfile);
if (res) {
#ifdef DEBUG
printf("tftp: read error\n");
#endif
return (res);
}
if (tftpfile->islastblock)
break;
}
if (tftpfile->currblock == needblock) {
int offinblock, inbuffer;
offinblock = tftpfile->off % SEGSIZE;
inbuffer = tftpfile->validsize - offinblock;
if (inbuffer < 0) {
#ifdef DEBUG
printf("tftp: invalid offset %d\n",
tftpfile->off);
#endif
return (EINVAL);
}
count = (size < inbuffer ? size : inbuffer);
bcopy(tftpfile->lastdata.t.th_data + offinblock,
addr, count);
addr = (char *)addr + count;
tftpfile->off += count;
size -= count;
if ((tftpfile->islastblock) && (count == inbuffer))
break;
} else {
#ifdef DEBUG
printf("tftp: block %d not found\n", needblock);
#endif
return (EINVAL);
}
}
if (resid)
*resid = size;
return (0);
}
static int
tftp_close(struct open_file *f)
{
struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata;
f->f_fsdata = NULL;
if (tftpfile) {
free(tftpfile->path);
free(tftpfile);
f->f_fsdata = NULL;
}
return (0);
}
static int
tftp_write(struct open_file *f, void *start, size_t size, size_t *resid)
{
return (EROFS);
}
static int
tftp_stat(struct open_file *f, struct stat *sb)
{
sb->st_mode = 0444 | S_IFREG;
sb->st_nlink = 1;
sb->st_uid = 0;
sb->st_gid = 0;
sb->st_size = -1;
return (0);
}
static off_t
tftp_seek(struct open_file *f, off_t offset, int where)
{
struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata;
switch (where) {
case SEEK_SET:
tftpfile->off = offset;
break;
case SEEK_CUR:
tftpfile->off += offset;
break;
default:
errno = EOFFSET;
return (-1);
}
return (tftpfile->off);
}