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 $
37 * Simple TFTP implementation for libsa.
39 * - socket descriptor (int) at open_file->f_devdata
40 * - server host IP in global servip
43 * - lseek only with SEEK_SET or SEEK_CUR
44 * - no big time differences between transfers (<tftp timeout)
47 #include <sys/param.h>
49 #include <netinet/in.h>
50 #include <netinet/udp.h>
51 #include <netinet/ip.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];
98 } __packed __aligned(4) lastdata;
101 static int tftperrors[8] = {
113 recvtftp(struct iodesc *d, void *pkt, size_t max_len, time_t tleft)
120 * Note: errno of 0 with -1 return means udp poll failed or
121 * packet was not for us.
123 * We may end up broadcasting the initial TFTP request. Take the
124 * first DATA result and save any ERROR result in case we do not
131 while ((tmp_len = readudp(d, pkt, max_len, tleft)) > 0) {
133 t = (struct tftphdr *)pkt;
134 if (ntohs(t->th_opcode) == DATA)
138 len = readudp(d, pkt, max_len, tleft);
140 if ((int)len < (int)sizeof(*t))
142 t = (struct tftphdr *)pkt;
145 switch (ntohs(t->th_opcode)) {
149 if (htons(t->th_block) != d->xid) {
157 * First data packet from new port. Set destip in
158 * case we got replies from multiple hosts, so only
159 * one host is selected.
164 uh = (struct udphdr *) pkt - 1;
165 ip = (struct ip *)uh - 1;
166 d->destport = uh->uh_sport;
167 d->destip = ip->ip_src;
168 } /* else check uh_sport has not changed??? */
169 got = len - (t->th_data - (char *)t);
173 if ((unsigned) ntohs(t->th_code) >= 8) {
174 printf("illegal tftp error %d\n", ntohs(t->th_code));
178 printf("tftp-error %d\n", ntohs(t->th_code));
180 errno = tftperrors[ntohs(t->th_code)];
185 printf("tftp type %d not handled\n", ntohs(t->th_opcode));
191 /* send request, expect first block (or error) */
193 tftp_makereq(struct tftp_handle *h)
196 u_char header[HEADER_SIZE];
198 u_char space[FNAME_SIZE + 6];
199 } __packed __aligned(4) wbuf;
205 wbuf.t.th_opcode = htons((u_short) RRQ);
206 wtail = wbuf.t.th_stuff;
208 bcopy(h->path, wtail, l + 1);
210 bcopy("octet", wtail, 6);
215 /* h->iodesc->myport = htons(--tftpport); */
216 h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
217 h->iodesc->destport = htons(IPPORT_TFTP);
218 h->iodesc->xid = 1; /* expected block */
220 res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
221 recvtftp, t, sizeof(*t) + RSPACE);
230 h->islastblock = 1; /* very short file */
234 /* ack block, expect next */
236 tftp_getnextblock(struct tftp_handle *h)
239 u_char header[HEADER_SIZE];
241 } __packed __aligned(4) wbuf;
249 wbuf.t.th_opcode = htons((u_short) ACK);
250 wtail = (char *) &wbuf.t.th_block;
251 wbuf.t.th_block = htons((u_short) h->currblock);
256 h->iodesc->xid = h->currblock + 1; /* expected block */
258 res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
259 recvtftp, t, sizeof(*t) + RSPACE);
261 if (res == -1) /* 0 is OK! */
267 h->islastblock = 1; /* EOF */
272 tftp_open(const char *path, struct open_file *f)
274 struct tftp_handle *tftpfile;
278 tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile));
282 tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
290 tftpfile->path = strdup(path);
291 if (tftpfile->path == NULL) {
296 res = tftp_makereq(tftpfile);
299 free(tftpfile->path);
303 f->f_fsdata = (void *) tftpfile;
312 tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid)
314 struct tftp_handle *tftpfile;
316 tftpfile = (struct tftp_handle *) f->f_fsdata;
319 int needblock, count;
324 needblock = tftpfile->off / SEGSIZE + 1;
326 if (tftpfile->currblock > needblock) /* seek backwards */
327 tftp_makereq(tftpfile); /* no error check, it worked
330 while (tftpfile->currblock < needblock) {
333 res = tftp_getnextblock(tftpfile);
334 if (res) { /* no answer */
336 printf("tftp: read error\n");
340 if (tftpfile->islastblock)
344 if (tftpfile->currblock == needblock) {
345 int offinblock, inbuffer;
347 offinblock = tftpfile->off % SEGSIZE;
349 inbuffer = tftpfile->validsize - offinblock;
352 printf("tftp: invalid offset %d\n",
357 count = (size < inbuffer ? size : inbuffer);
358 bcopy(tftpfile->lastdata.t.th_data + offinblock,
361 addr = (char *)addr + count;
362 tftpfile->off += count;
365 if ((tftpfile->islastblock) && (count == inbuffer))
369 printf("tftp: block %d not found\n", needblock);
382 tftp_close(struct open_file *f)
384 struct tftp_handle *tftpfile;
385 tftpfile = (struct tftp_handle *) f->f_fsdata;
387 /* let it time out ... */
390 free(tftpfile->path);
402 tftp_write(struct open_file *f, void *start, size_t size, size_t *resid)
408 tftp_stat(struct open_file *f, struct stat *sb)
410 sb->st_mode = 0444 | S_IFREG;
419 tftp_seek(struct open_file *f, off_t offset, int where)
421 struct tftp_handle *tftpfile;
422 tftpfile = (struct tftp_handle *) f->f_fsdata;
426 tftpfile->off = offset;
429 tftpfile->off += offset;
435 return (tftpfile->off);