Home | History | Annotate | Line # | Download | only in libsa
tftp.c revision 1.6
      1  1.6       cgd /*	$NetBSD: tftp.c,v 1.6 1999/03/31 01:50:26 cgd Exp $	 */
      2  1.1  drochner 
      3  1.1  drochner /*
      4  1.1  drochner  * Copyright (c) 1996
      5  1.1  drochner  *	Matthias Drochner.  All rights reserved.
      6  1.1  drochner  *
      7  1.1  drochner  * Redistribution and use in source and binary forms, with or without
      8  1.1  drochner  * modification, are permitted provided that the following conditions
      9  1.1  drochner  * are met:
     10  1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     11  1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     12  1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  drochner  *    documentation and/or other materials provided with the distribution.
     15  1.1  drochner  * 3. All advertising materials mentioning features or use of this software
     16  1.1  drochner  *    must display the following acknowledgement:
     17  1.1  drochner  *	This product includes software developed for the NetBSD Project
     18  1.1  drochner  *	by Matthias Drochner.
     19  1.1  drochner  * 4. The name of the author may not be used to endorse or promote products
     20  1.1  drochner  *    derived from this software without specific prior written permission.
     21  1.1  drochner  *
     22  1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1  drochner  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  drochner  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  drochner  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1  drochner  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.1  drochner  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.1  drochner  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.1  drochner  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.1  drochner  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.1  drochner  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  drochner  *
     33  1.1  drochner  */
     34  1.1  drochner 
     35  1.1  drochner /*
     36  1.1  drochner  * Simple TFTP implementation for libsa.
     37  1.1  drochner  * Assumes:
     38  1.1  drochner  *  - socket descriptor (int) at open_file->f_devdata
     39  1.1  drochner  *  - server host IP in global servip
     40  1.1  drochner  * Restrictions:
     41  1.1  drochner  *  - read only
     42  1.1  drochner  *  - lseek only with SEEK_SET or SEEK_CUR
     43  1.1  drochner  *  - no big time differences between transfers (<tftp timeout)
     44  1.1  drochner  */
     45  1.1  drochner 
     46  1.6       cgd /*
     47  1.6       cgd  * XXX Does not currently implement:
     48  1.6       cgd  * XXX
     49  1.6       cgd  * XXX LIBSA_NO_FS_CLOSE
     50  1.6       cgd  * XXX LIBSA_NO_FS_SEEK
     51  1.6       cgd  * XXX LIBSA_NO_FS_WRITE
     52  1.6       cgd  * XXX LIBSA_NO_FS_SYMLINK (does this even make sense?)
     53  1.6       cgd  * XXX LIBSA_FS_SINGLECOMPONENT (does this even make sense?)
     54  1.6       cgd  */
     55  1.6       cgd 
     56  1.1  drochner #include <sys/types.h>
     57  1.1  drochner #include <sys/stat.h>
     58  1.1  drochner #include <netinet/in.h>
     59  1.1  drochner #include <netinet/udp.h>
     60  1.1  drochner #include <netinet/in_systm.h>
     61  1.2      ross #include <lib/libkern/libkern.h>
     62  1.1  drochner #ifdef _STANDALONE
     63  1.1  drochner #include "/usr/include/arpa/tftp.h" /* XXX */
     64  1.1  drochner #else
     65  1.1  drochner #include <arpa/tftp.h>
     66  1.1  drochner #endif
     67  1.1  drochner 
     68  1.1  drochner #include "stand.h"
     69  1.1  drochner #include "net.h"
     70  1.1  drochner #include "netif.h"
     71  1.1  drochner 
     72  1.1  drochner #include "tftp.h"
     73  1.1  drochner 
     74  1.1  drochner extern struct in_addr servip;
     75  1.1  drochner 
     76  1.1  drochner static int      tftpport = 2000;
     77  1.1  drochner 
     78  1.1  drochner #define RSPACE 520		/* max data packet, rounded up */
     79  1.1  drochner 
     80  1.1  drochner struct tftp_handle {
     81  1.1  drochner 	struct iodesc  *iodesc;
     82  1.1  drochner 	int             currblock;	/* contents of lastdata */
     83  1.1  drochner 	int             islastblock;	/* flag */
     84  1.1  drochner 	int             validsize;
     85  1.1  drochner 	int             off;
     86  1.1  drochner 	char           *path;	/* saved for re-requests */
     87  1.1  drochner 	struct {
     88  1.1  drochner 		u_char header[HEADER_SIZE];
     89  1.1  drochner 		struct tftphdr t;
     90  1.1  drochner 		u_char space[RSPACE];
     91  1.1  drochner 	} lastdata;
     92  1.1  drochner };
     93  1.1  drochner 
     94  1.1  drochner static int tftperrors[8] = {
     95  1.1  drochner 	0,			/* ??? */
     96  1.1  drochner 	ENOENT,
     97  1.1  drochner 	EPERM,
     98  1.1  drochner 	ENOSPC,
     99  1.1  drochner 	EINVAL,			/* ??? */
    100  1.1  drochner 	EINVAL,			/* ??? */
    101  1.1  drochner 	EEXIST,
    102  1.1  drochner 	EINVAL			/* ??? */
    103  1.1  drochner };
    104  1.3  christos 
    105  1.4       kim static ssize_t recvtftp __P((struct iodesc *, void *, size_t, time_t));
    106  1.3  christos static int tftp_makereq __P((struct tftp_handle *));
    107  1.3  christos static int tftp_getnextblock __P((struct tftp_handle *));
    108  1.3  christos #ifndef TFTP_NOTERMINATE
    109  1.3  christos static void tftp_terminate __P((struct tftp_handle *));
    110  1.3  christos #endif
    111  1.1  drochner 
    112  1.1  drochner static ssize_t
    113  1.1  drochner recvtftp(d, pkt, len, tleft)
    114  1.1  drochner 	register struct iodesc *d;
    115  1.1  drochner 	register void  *pkt;
    116  1.4       kim 	register size_t len;
    117  1.1  drochner 	time_t          tleft;
    118  1.1  drochner {
    119  1.1  drochner 	struct tftphdr *t;
    120  1.1  drochner 
    121  1.1  drochner 	errno = 0;
    122  1.1  drochner 
    123  1.1  drochner 	len = readudp(d, pkt, len, tleft);
    124  1.1  drochner 
    125  1.1  drochner 	if (len < 4)
    126  1.1  drochner 		return (-1);
    127  1.1  drochner 
    128  1.1  drochner 	t = (struct tftphdr *) pkt;
    129  1.1  drochner 	switch (ntohs(t->th_opcode)) {
    130  1.1  drochner 	case DATA: {
    131  1.1  drochner 		int got;
    132  1.1  drochner 
    133  1.1  drochner 		if (htons(t->th_block) != d->xid) {
    134  1.1  drochner 			/*
    135  1.1  drochner 			 * Expected block?
    136  1.1  drochner 			 */
    137  1.1  drochner 			return (-1);
    138  1.1  drochner 		}
    139  1.1  drochner 		if (d->xid == 1) {
    140  1.1  drochner 			/*
    141  1.1  drochner 			 * First data packet from new port.
    142  1.1  drochner 			 */
    143  1.1  drochner 			register struct udphdr *uh;
    144  1.1  drochner 			uh = (struct udphdr *) pkt - 1;
    145  1.1  drochner 			d->destport = uh->uh_sport;
    146  1.1  drochner 		} /* else check uh_sport has not changed??? */
    147  1.1  drochner 		got = len - (t->th_data - (char *) t);
    148  1.1  drochner 		return got;
    149  1.1  drochner 	}
    150  1.1  drochner 	case ERROR:
    151  1.1  drochner 		if ((unsigned) ntohs(t->th_code) >= 8) {
    152  1.1  drochner 			printf("illegal tftp error %d\n", ntohs(t->th_code));
    153  1.1  drochner 			errno = EIO;
    154  1.1  drochner 		} else {
    155  1.1  drochner #ifdef DEBUG
    156  1.1  drochner 			printf("tftp-error %d\n", ntohs(t->th_code));
    157  1.1  drochner #endif
    158  1.1  drochner 			errno = tftperrors[ntohs(t->th_code)];
    159  1.1  drochner 		}
    160  1.1  drochner 		return (-1);
    161  1.1  drochner 	default:
    162  1.1  drochner #ifdef DEBUG
    163  1.1  drochner 		printf("tftp type %d not handled\n", ntohs(t->th_opcode));
    164  1.1  drochner #endif
    165  1.1  drochner 		return (-1);
    166  1.1  drochner 	}
    167  1.1  drochner }
    168  1.1  drochner 
    169  1.1  drochner /* send request, expect first block (or error) */
    170  1.1  drochner static int
    171  1.1  drochner tftp_makereq(h)
    172  1.1  drochner 	struct tftp_handle *h;
    173  1.1  drochner {
    174  1.1  drochner 	struct {
    175  1.1  drochner 		u_char header[HEADER_SIZE];
    176  1.1  drochner 		struct tftphdr  t;
    177  1.1  drochner 		u_char space[FNAME_SIZE + 6];
    178  1.1  drochner 	} wbuf;
    179  1.1  drochner 	char           *wtail;
    180  1.1  drochner 	int             l;
    181  1.1  drochner 	ssize_t         res;
    182  1.1  drochner 	struct tftphdr *t;
    183  1.1  drochner 
    184  1.1  drochner 	wbuf.t.th_opcode = htons((u_short) RRQ);
    185  1.1  drochner 	wtail = wbuf.t.th_stuff;
    186  1.1  drochner 	l = strlen(h->path);
    187  1.1  drochner 	bcopy(h->path, wtail, l + 1);
    188  1.1  drochner 	wtail += l + 1;
    189  1.1  drochner 	bcopy("octet", wtail, 6);
    190  1.1  drochner 	wtail += 6;
    191  1.1  drochner 
    192  1.1  drochner 	t = &h->lastdata.t;
    193  1.1  drochner 
    194  1.1  drochner 	/* h->iodesc->myport = htons(--tftpport); */
    195  1.1  drochner 	h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
    196  1.1  drochner 	h->iodesc->destport = htons(IPPORT_TFTP);
    197  1.1  drochner 	h->iodesc->xid = 1;	/* expected block */
    198  1.1  drochner 
    199  1.1  drochner 	res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
    200  1.1  drochner 		       recvtftp, t, sizeof(*t) + RSPACE);
    201  1.1  drochner 
    202  1.1  drochner 	if (res == -1)
    203  1.1  drochner 		return (errno);
    204  1.1  drochner 
    205  1.1  drochner 	h->currblock = 1;
    206  1.1  drochner 	h->validsize = res;
    207  1.1  drochner 	h->islastblock = 0;
    208  1.1  drochner 	if (res < SEGSIZE)
    209  1.1  drochner 		h->islastblock = 1;	/* very short file */
    210  1.1  drochner 	return (0);
    211  1.1  drochner }
    212  1.1  drochner 
    213  1.1  drochner /* ack block, expect next */
    214  1.1  drochner static int
    215  1.1  drochner tftp_getnextblock(h)
    216  1.1  drochner 	struct tftp_handle *h;
    217  1.1  drochner {
    218  1.1  drochner 	struct {
    219  1.1  drochner 		u_char header[HEADER_SIZE];
    220  1.1  drochner 		struct tftphdr t;
    221  1.1  drochner 	} wbuf;
    222  1.1  drochner 	char           *wtail;
    223  1.1  drochner 	int             res;
    224  1.1  drochner 	struct tftphdr *t;
    225  1.1  drochner 
    226  1.1  drochner 	wbuf.t.th_opcode = htons((u_short) ACK);
    227  1.1  drochner 	wbuf.t.th_block = htons((u_short) h->currblock);
    228  1.1  drochner 	wtail = (char *) &wbuf.t.th_data;
    229  1.1  drochner 
    230  1.1  drochner 	t = &h->lastdata.t;
    231  1.1  drochner 
    232  1.1  drochner 	h->iodesc->xid = h->currblock + 1;	/* expected block */
    233  1.1  drochner 
    234  1.1  drochner 	res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
    235  1.1  drochner 		       recvtftp, t, sizeof(*t) + RSPACE);
    236  1.1  drochner 
    237  1.1  drochner 	if (res == -1)		/* 0 is OK! */
    238  1.1  drochner 		return (errno);
    239  1.1  drochner 
    240  1.1  drochner 	h->currblock++;
    241  1.1  drochner 	h->validsize = res;
    242  1.1  drochner 	if (res < SEGSIZE)
    243  1.1  drochner 		h->islastblock = 1;	/* EOF */
    244  1.1  drochner 	return (0);
    245  1.1  drochner }
    246  1.1  drochner 
    247  1.1  drochner #ifndef TFTP_NOTERMINATE
    248  1.1  drochner static void
    249  1.1  drochner tftp_terminate(h)
    250  1.1  drochner 	struct tftp_handle *h;
    251  1.1  drochner {
    252  1.1  drochner 	struct {
    253  1.1  drochner 		u_char header[HEADER_SIZE];
    254  1.1  drochner 		struct tftphdr t;
    255  1.1  drochner 	} wbuf;
    256  1.1  drochner 	char           *wtail;
    257  1.1  drochner 
    258  1.1  drochner 	if (h->islastblock) {
    259  1.1  drochner 		wbuf.t.th_opcode = htons((u_short) ACK);
    260  1.1  drochner 		wbuf.t.th_block = htons((u_short) h->currblock);
    261  1.1  drochner 	} else {
    262  1.1  drochner 		wbuf.t.th_opcode = htons((u_short) ERROR);
    263  1.1  drochner 		wbuf.t.th_code = htons((u_short) ENOSPACE); /* ??? */
    264  1.1  drochner 	}
    265  1.1  drochner 	wtail = (char *) &wbuf.t.th_data;
    266  1.1  drochner 
    267  1.1  drochner 	(void) sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
    268  1.1  drochner }
    269  1.1  drochner #endif
    270  1.1  drochner 
    271  1.1  drochner int
    272  1.1  drochner tftp_open(path, f)
    273  1.1  drochner 	char           *path;
    274  1.1  drochner 	struct open_file *f;
    275  1.1  drochner {
    276  1.1  drochner 	struct tftp_handle *tftpfile;
    277  1.1  drochner 	struct iodesc  *io;
    278  1.1  drochner 	int             res;
    279  1.1  drochner 
    280  1.1  drochner 	tftpfile = (struct tftp_handle *) alloc(sizeof(*tftpfile));
    281  1.1  drochner 	if (!tftpfile)
    282  1.1  drochner 		return (ENOMEM);
    283  1.1  drochner 
    284  1.1  drochner 	tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
    285  1.1  drochner 	io->destip = servip;
    286  1.1  drochner 	tftpfile->off = 0;
    287  1.1  drochner 	tftpfile->path = path;	/* XXXXXXX we hope it's static */
    288  1.1  drochner 
    289  1.4       kim 	res = tftp_makereq(tftpfile);
    290  1.1  drochner 
    291  1.1  drochner 	if (res) {
    292  1.1  drochner 		free(tftpfile, sizeof(*tftpfile));
    293  1.1  drochner 		return (res);
    294  1.1  drochner 	}
    295  1.1  drochner 	f->f_fsdata = (void *) tftpfile;
    296  1.1  drochner 	return (0);
    297  1.1  drochner }
    298  1.1  drochner 
    299  1.1  drochner int
    300  1.1  drochner tftp_read(f, addr, size, resid)
    301  1.1  drochner 	struct open_file *f;
    302  1.1  drochner 	void           *addr;
    303  1.1  drochner 	size_t          size;
    304  1.1  drochner 	size_t         *resid;	/* out */
    305  1.1  drochner {
    306  1.1  drochner 	struct tftp_handle *tftpfile;
    307  1.6       cgd #if !defined(LIBSA_NO_TWIDDLE)
    308  1.1  drochner 	static int      tc = 0;
    309  1.6       cgd #endif
    310  1.1  drochner 	tftpfile = (struct tftp_handle *) f->f_fsdata;
    311  1.1  drochner 
    312  1.1  drochner 	while (size > 0) {
    313  1.1  drochner 		int needblock, count;
    314  1.1  drochner 
    315  1.6       cgd #if !defined(LIBSA_NO_TWIDDLE)
    316  1.1  drochner 		if (!(tc++ % 16))
    317  1.1  drochner 			twiddle();
    318  1.6       cgd #endif
    319  1.1  drochner 
    320  1.1  drochner 		needblock = tftpfile->off / SEGSIZE + 1;
    321  1.1  drochner 
    322  1.1  drochner 		if (tftpfile->currblock > needblock) {	/* seek backwards */
    323  1.1  drochner #ifndef TFTP_NOTERMINATE
    324  1.1  drochner 			tftp_terminate(tftpfile);
    325  1.1  drochner #endif
    326  1.1  drochner 			tftp_makereq(tftpfile);	/* no error check, it worked
    327  1.1  drochner 						 * for open */
    328  1.1  drochner 		}
    329  1.1  drochner 
    330  1.1  drochner 		while (tftpfile->currblock < needblock) {
    331  1.1  drochner 			int res;
    332  1.1  drochner 
    333  1.1  drochner 			res = tftp_getnextblock(tftpfile);
    334  1.1  drochner 			if (res) {	/* no answer */
    335  1.1  drochner #ifdef DEBUG
    336  1.1  drochner 				printf("tftp: read error (block %d->%d)\n",
    337  1.1  drochner 				       tftpfile->currblock, needblock);
    338  1.1  drochner #endif
    339  1.1  drochner 				return (res);
    340  1.1  drochner 			}
    341  1.1  drochner 			if (tftpfile->islastblock)
    342  1.1  drochner 				break;
    343  1.1  drochner 		}
    344  1.1  drochner 
    345  1.1  drochner 		if (tftpfile->currblock == needblock) {
    346  1.1  drochner 			int offinblock, inbuffer;
    347  1.1  drochner 
    348  1.1  drochner 			offinblock = tftpfile->off % SEGSIZE;
    349  1.1  drochner 
    350  1.1  drochner 			inbuffer = tftpfile->validsize - offinblock;
    351  1.1  drochner 			if (inbuffer < 0) {
    352  1.1  drochner #ifdef DEBUG
    353  1.1  drochner 				printf("tftp: invalid offset %d\n",
    354  1.1  drochner 				    tftpfile->off);
    355  1.1  drochner #endif
    356  1.1  drochner 				return (EINVAL);
    357  1.1  drochner 			}
    358  1.1  drochner 			count = (size < inbuffer ? size : inbuffer);
    359  1.1  drochner 			bcopy(tftpfile->lastdata.t.th_data + offinblock,
    360  1.1  drochner 			    addr, count);
    361  1.1  drochner 
    362  1.5       dbj 			addr = (caddr_t)addr + count;
    363  1.1  drochner 			tftpfile->off += count;
    364  1.1  drochner 			size -= count;
    365  1.1  drochner 
    366  1.1  drochner 			if ((tftpfile->islastblock) && (count == inbuffer))
    367  1.1  drochner 				break;	/* EOF */
    368  1.1  drochner 		} else {
    369  1.1  drochner #ifdef DEBUG
    370  1.1  drochner 			printf("tftp: block %d not found\n", needblock);
    371  1.1  drochner #endif
    372  1.1  drochner 			return (EINVAL);
    373  1.1  drochner 		}
    374  1.1  drochner 
    375  1.1  drochner 	}
    376  1.1  drochner 
    377  1.1  drochner 	if (resid)
    378  1.1  drochner 		*resid = size;
    379  1.1  drochner 	return (0);
    380  1.1  drochner }
    381  1.1  drochner 
    382  1.1  drochner int
    383  1.1  drochner tftp_close(f)
    384  1.1  drochner 	struct open_file *f;
    385  1.1  drochner {
    386  1.1  drochner 	struct tftp_handle *tftpfile;
    387  1.1  drochner 	tftpfile = (struct tftp_handle *) f->f_fsdata;
    388  1.1  drochner 
    389  1.1  drochner #ifdef TFTP_NOTERMINATE
    390  1.1  drochner 	/* let it time out ... */
    391  1.1  drochner #else
    392  1.1  drochner 	tftp_terminate(tftpfile);
    393  1.1  drochner #endif
    394  1.1  drochner 
    395  1.1  drochner 	free(tftpfile, sizeof(*tftpfile));
    396  1.1  drochner 	return (0);
    397  1.1  drochner }
    398  1.1  drochner 
    399  1.1  drochner int
    400  1.1  drochner tftp_write(f, start, size, resid)
    401  1.1  drochner 	struct open_file *f;
    402  1.1  drochner 	void           *start;
    403  1.1  drochner 	size_t          size;
    404  1.1  drochner 	size_t         *resid;	/* out */
    405  1.1  drochner {
    406  1.1  drochner 	return (EROFS);
    407  1.1  drochner }
    408  1.1  drochner 
    409  1.1  drochner int
    410  1.1  drochner tftp_stat(f, sb)
    411  1.1  drochner 	struct open_file *f;
    412  1.1  drochner 	struct stat    *sb;
    413  1.1  drochner {
    414  1.1  drochner 	struct tftp_handle *tftpfile;
    415  1.1  drochner 	tftpfile = (struct tftp_handle *) f->f_fsdata;
    416  1.1  drochner 
    417  1.1  drochner 	sb->st_mode = 0444;
    418  1.1  drochner 	sb->st_nlink = 1;
    419  1.1  drochner 	sb->st_uid = 0;
    420  1.1  drochner 	sb->st_gid = 0;
    421  1.1  drochner 	sb->st_size = -1;
    422  1.1  drochner 	return (0);
    423  1.1  drochner }
    424  1.1  drochner 
    425  1.1  drochner off_t
    426  1.1  drochner tftp_seek(f, offset, where)
    427  1.1  drochner 	struct open_file *f;
    428  1.1  drochner 	off_t           offset;
    429  1.1  drochner 	int             where;
    430  1.1  drochner {
    431  1.1  drochner 	struct tftp_handle *tftpfile;
    432  1.1  drochner 	tftpfile = (struct tftp_handle *) f->f_fsdata;
    433  1.1  drochner 
    434  1.1  drochner 	switch (where) {
    435  1.1  drochner 	case SEEK_SET:
    436  1.1  drochner 		tftpfile->off = offset;
    437  1.1  drochner 		break;
    438  1.1  drochner 	case SEEK_CUR:
    439  1.1  drochner 		tftpfile->off += offset;
    440  1.1  drochner 		break;
    441  1.1  drochner 	default:
    442  1.1  drochner 		errno = EOFFSET;
    443  1.1  drochner 		return (-1);
    444  1.1  drochner 	}
    445  1.1  drochner 	return (tftpfile->off);
    446  1.1  drochner }
    447