NetBSD/sys/lib/libsa/tftp.c

444 lines
9.8 KiB
C
Raw Normal View History

2000-03-30 16:19:46 +04:00
/* $NetBSD: tftp.c,v 1.10 2000/03/30 12:19:49 augustss Exp $ */
/*
* Copyright (c) 1996
* Matthias Drochner. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project
* by Matthias Drochner.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* Simple TFTP implementation for libsa.
* Assumes:
* - socket descriptor (int) at open_file->f_devdata
* - server host IP in global servip
* Restrictions:
* - read only
* - lseek only with SEEK_SET or SEEK_CUR
* - no big time differences between transfers (<tftp timeout)
*/
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
/*
* XXX Does not currently implement:
* XXX
* XXX LIBSA_NO_FS_CLOSE
* XXX LIBSA_NO_FS_SEEK
* XXX LIBSA_NO_FS_WRITE
* XXX LIBSA_NO_FS_SYMLINK (does this even make sense?)
* XXX LIBSA_FS_SINGLECOMPONENT (does this even make sense?)
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <netinet/in_systm.h>
1999-11-14 00:17:56 +03:00
#include <lib/libkern/libkern.h>
#ifdef _STANDALONE
#include "/usr/include/arpa/tftp.h" /* XXX */
#else
#include <arpa/tftp.h>
#endif
#include "stand.h"
#include "net.h"
#include "netif.h"
#include "tftp.h"
extern struct in_addr servip;
static int tftpport = 2000;
#define RSPACE 520 /* max data packet, rounded up */
struct tftp_handle {
struct iodesc *iodesc;
int currblock; /* contents of lastdata */
int islastblock; /* flag */
int validsize;
int off;
char *path; /* saved for re-requests */
struct {
u_char header[HEADER_SIZE];
struct tftphdr t;
u_char space[RSPACE];
} lastdata;
};
static int tftperrors[8] = {
0, /* ??? */
ENOENT,
EPERM,
ENOSPC,
EINVAL, /* ??? */
EINVAL, /* ??? */
EEXIST,
EINVAL /* ??? */
};
static ssize_t recvtftp __P((struct iodesc *, void *, size_t, time_t));
static int tftp_makereq __P((struct tftp_handle *));
static int tftp_getnextblock __P((struct tftp_handle *));
#ifndef TFTP_NOTERMINATE
static void tftp_terminate __P((struct tftp_handle *));
#endif
static ssize_t
recvtftp(d, pkt, len, tleft)
2000-03-30 16:19:46 +04:00
struct iodesc *d;
void *pkt;
size_t len;
time_t tleft;
{
ssize_t n;
struct tftphdr *t;
errno = 0;
n = readudp(d, pkt, len, tleft);
if (n < 4)
return (-1);
t = (struct tftphdr *) pkt;
switch (ntohs(t->th_opcode)) {
case DATA:
if (htons(t->th_block) != d->xid) {
/*
* Expected block?
*/
return (-1);
}
if (d->xid == 1) {
/*
* First data packet from new port.
*/
2000-03-30 16:19:46 +04:00
struct udphdr *uh;
uh = (struct udphdr *) pkt - 1;
d->destport = uh->uh_sport;
} /* else check uh_sport has not changed??? */
return (n - (t->th_data - (char *)t));
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);
}
}
/* send request, expect first block (or error) */
static int
tftp_makereq(h)
struct tftp_handle *h;
{
struct {
u_char header[HEADER_SIZE];
struct tftphdr t;
u_char space[FNAME_SIZE + 6];
} 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); */
h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
h->iodesc->destport = htons(IPPORT_TFTP);
h->iodesc->xid = 1; /* expected block */
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; /* very short file */
return (0);
}
/* ack block, expect next */
static int
tftp_getnextblock(h)
struct tftp_handle *h;
{
struct {
u_char header[HEADER_SIZE];
struct tftphdr t;
} wbuf;
char *wtail;
int res;
struct tftphdr *t;
wbuf.t.th_opcode = htons((u_short) ACK);
wbuf.t.th_block = htons((u_short) h->currblock);
wtail = (char *) &wbuf.t.th_data;
t = &h->lastdata.t;
h->iodesc->xid = h->currblock + 1; /* expected block */
res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
recvtftp, t, sizeof(*t) + RSPACE);
if (res == -1) /* 0 is OK! */
return (errno);
h->currblock++;
h->validsize = res;
if (res < SEGSIZE)
h->islastblock = 1; /* EOF */
return (0);
}
#ifndef TFTP_NOTERMINATE
static void
tftp_terminate(h)
struct tftp_handle *h;
{
struct {
u_char header[HEADER_SIZE];
struct tftphdr t;
} wbuf;
char *wtail;
if (h->islastblock) {
wbuf.t.th_opcode = htons((u_short) ACK);
wbuf.t.th_block = htons((u_short) h->currblock);
} else {
wbuf.t.th_opcode = htons((u_short) ERROR);
wbuf.t.th_code = htons((u_short) ENOSPACE); /* ??? */
}
wtail = (char *) &wbuf.t.th_data;
(void) sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
}
#endif
int
tftp_open(path, f)
char *path;
struct open_file *f;
{
struct tftp_handle *tftpfile;
struct iodesc *io;
int res;
tftpfile = (struct tftp_handle *) alloc(sizeof(*tftpfile));
if (!tftpfile)
return (ENOMEM);
tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
io->destip = servip;
tftpfile->off = 0;
tftpfile->path = path; /* XXXXXXX we hope it's static */
res = tftp_makereq(tftpfile);
if (res) {
free(tftpfile, sizeof(*tftpfile));
return (res);
}
f->f_fsdata = (void *) tftpfile;
return (0);
}
int
tftp_read(f, addr, size, resid)
struct open_file *f;
void *addr;
size_t size;
size_t *resid; /* out */
{
struct tftp_handle *tftpfile;
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
#if !defined(LIBSA_NO_TWIDDLE)
static int tc = 0;
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
#endif
tftpfile = (struct tftp_handle *) f->f_fsdata;
while (size > 0) {
int needblock, count;
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
#if !defined(LIBSA_NO_TWIDDLE)
if (!(tc++ % 16))
twiddle();
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
#endif
needblock = tftpfile->off / SEGSIZE + 1;
if (tftpfile->currblock > needblock) { /* seek backwards */
#ifndef TFTP_NOTERMINATE
tftp_terminate(tftpfile);
#endif
tftp_makereq(tftpfile); /* no error check, it worked
* for open */
}
while (tftpfile->currblock < needblock) {
int res;
res = tftp_getnextblock(tftpfile);
if (res) { /* no answer */
#ifdef DEBUG
printf("tftp: read error (block %d->%d)\n",
tftpfile->currblock, needblock);
#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 = (caddr_t)addr + count;
tftpfile->off += count;
size -= count;
if ((tftpfile->islastblock) && (count == inbuffer))
break; /* EOF */
} else {
#ifdef DEBUG
printf("tftp: block %d not found\n", needblock);
#endif
return (EINVAL);
}
}
if (resid)
*resid = size;
return (0);
}
int
tftp_close(f)
struct open_file *f;
{
struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata;
#ifdef TFTP_NOTERMINATE
/* let it time out ... */
#else
tftp_terminate(tftpfile);
#endif
free(tftpfile, sizeof(*tftpfile));
return (0);
}
int
tftp_write(f, start, size, resid)
struct open_file *f;
void *start;
size_t size;
size_t *resid; /* out */
{
return (EROFS);
}
int
tftp_stat(f, sb)
struct open_file *f;
struct stat *sb;
{
struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata;
sb->st_mode = 0444;
sb->st_nlink = 1;
sb->st_uid = 0;
sb->st_gid = 0;
sb->st_size = -1;
return (0);
}
off_t
tftp_seek(f, offset, where)
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);
}