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