1 /* $NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner Exp $ */
5 * Matthias Drochner. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Matthias Drochner.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: src/lib/libstand/tftp.c,v 1.2.6.4 2001/07/14 14:00:03 mikeh Exp $
34 * $DragonFly: src/lib/libstand/tftp.c,v 1.4 2004/10/25 19:38:45 drhodus Exp $
38 * Simple TFTP implementation for libsa.
40 * - socket descriptor (int) at open_file->f_devdata
41 * - server host IP in global servip
44 * - lseek only with SEEK_SET or SEEK_CUR
45 * - no big time differences between transfers (<tftp timeout)
48 #include <sys/param.h>
50 #include <netinet/in.h>
51 #include <netinet/udp.h>
52 #include <netinet/in_systm.h>
53 #include <arpa/tftp.h>
63 static int tftp_open(const char *path, struct open_file *f);
64 static int tftp_close(struct open_file *f);
65 static int tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid);
66 static int tftp_write(struct open_file *f, void *buf, size_t size, size_t *resid);
67 static off_t tftp_seek(struct open_file *f, off_t offset, int where);
68 static int tftp_stat(struct open_file *f, struct stat *sb);
70 struct fs_ops tftp_fsops = {
81 extern struct in_addr servip;
83 static int tftpport = 2000;
85 #define RSPACE 520 /* max data packet, rounded up */
88 struct iodesc *iodesc;
89 int currblock; /* contents of lastdata */
90 int islastblock; /* flag */
93 char *path; /* saved for re-requests */
95 u_char header[HEADER_SIZE];
101 static int tftperrors[8] = {
113 recvtftp(d, pkt, len, tleft)
123 len = readudp(d, pkt, len, tleft);
128 t = (struct tftphdr *) pkt;
129 switch (ntohs(t->th_opcode)) {
133 if (htons(t->th_block) != d->xid) {
141 * First data packet from new port.
144 uh = (struct udphdr *) pkt - 1;
145 d->destport = uh->uh_sport;
146 } /* else check uh_sport has not changed??? */
147 got = len - (t->th_data - (char *) t);
151 if ((unsigned) ntohs(t->th_code) >= 8) {
152 printf("illegal tftp error %d\n", ntohs(t->th_code));
156 printf("tftp-error %d\n", ntohs(t->th_code));
158 errno = tftperrors[ntohs(t->th_code)];
163 printf("tftp type %d not handled\n", ntohs(t->th_opcode));
169 /* send request, expect first block (or error) */
172 struct tftp_handle *h;
175 u_char header[HEADER_SIZE];
177 u_char space[FNAME_SIZE + 6];
184 wbuf.t.th_opcode = htons((u_short) RRQ);
185 wtail = wbuf.t.th_stuff;
187 bcopy(h->path, wtail, l + 1);
189 bcopy("octet", wtail, 6);
194 /* h->iodesc->myport = htons(--tftpport); */
195 h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
196 h->iodesc->destport = htons(IPPORT_TFTP);
197 h->iodesc->xid = 1; /* expected block */
199 res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
200 recvtftp, t, sizeof(*t) + RSPACE);
209 h->islastblock = 1; /* very short file */
213 /* ack block, expect next */
216 struct tftp_handle *h;
219 u_char header[HEADER_SIZE];
226 wbuf.t.th_opcode = htons((u_short) ACK);
227 wtail = (char *) &wbuf.t.th_block;
228 wbuf.t.th_block = htons((u_short) h->currblock);
233 h->iodesc->xid = h->currblock + 1; /* expected block */
235 res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
236 recvtftp, t, sizeof(*t) + RSPACE);
238 if (res == -1) /* 0 is OK! */
244 h->islastblock = 1; /* EOF */
253 struct tftp_handle *tftpfile;
257 tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile));
261 tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
267 tftpfile->path = strdup(path);
268 if (tftpfile->path == NULL) {
273 res = tftp_makereq(tftpfile, path);
276 free(tftpfile->path);
280 f->f_fsdata = (void *) tftpfile;
285 tftp_read(f, addr, size, resid)
289 size_t *resid; /* out */
291 struct tftp_handle *tftpfile;
293 tftpfile = (struct tftp_handle *) f->f_fsdata;
296 int needblock, count;
301 needblock = tftpfile->off / SEGSIZE + 1;
303 if (tftpfile->currblock > needblock) /* seek backwards */
304 tftp_makereq(tftpfile); /* no error check, it worked
307 while (tftpfile->currblock < needblock) {
310 res = tftp_getnextblock(tftpfile);
311 if (res) { /* no answer */
313 printf("tftp: read error\n");
317 if (tftpfile->islastblock)
321 if (tftpfile->currblock == needblock) {
322 int offinblock, inbuffer;
324 offinblock = tftpfile->off % SEGSIZE;
326 inbuffer = tftpfile->validsize - offinblock;
329 printf("tftp: invalid offset %d\n",
334 count = (size < inbuffer ? size : inbuffer);
335 bcopy(tftpfile->lastdata.t.th_data + offinblock,
339 tftpfile->off += count;
342 if ((tftpfile->islastblock) && (count == inbuffer))
346 printf("tftp: block %d not found\n", needblock);
362 struct tftp_handle *tftpfile;
363 tftpfile = (struct tftp_handle *) f->f_fsdata;
365 /* let it time out ... */
368 free(tftpfile->path);
375 tftp_write(f, start, size, resid)
379 size_t *resid; /* out */
389 struct tftp_handle *tftpfile;
390 tftpfile = (struct tftp_handle *) f->f_fsdata;
392 sb->st_mode = 0444 | S_IFREG;
401 tftp_seek(f, offset, where)
406 struct tftp_handle *tftpfile;
407 tftpfile = (struct tftp_handle *) f->f_fsdata;
411 tftpfile->off = offset;
414 tftpfile->off += offset;
420 return (tftpfile->off);