Initial import from FreeBSD RELENG_4:
[games.git] / lib / libstand / tftp.c
1 /*      $NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner 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  * $FreeBSD: src/lib/libstand/tftp.c,v 1.2.6.4 2001/07/14 14:00:03 mikeh Exp $
34  */
35
36 /*
37  * Simple TFTP implementation for libsa.
38  * Assumes:
39  *  - socket descriptor (int) at open_file->f_devdata
40  *  - server host IP in global servip
41  * Restrictions:
42  *  - read only
43  *  - lseek only with SEEK_SET or SEEK_CUR
44  *  - no big time differences between transfers (<tftp timeout)
45  */
46
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <netinet/in.h>
50 #include <netinet/udp.h>
51 #include <netinet/in_systm.h>
52 #include <arpa/tftp.h>
53
54 #include <string.h>
55
56 #include "stand.h"
57 #include "net.h"
58 #include "netif.h"
59
60 #include "tftp.h"
61
62 static int      tftp_open(const char *path, struct open_file *f);
63 static int      tftp_close(struct open_file *f);
64 static int      tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid);
65 static int      tftp_write(struct open_file *f, void *buf, size_t size, size_t *resid);
66 static off_t    tftp_seek(struct open_file *f, off_t offset, int where);
67 static int      tftp_stat(struct open_file *f, struct stat *sb);
68
69 struct fs_ops tftp_fsops = {
70         "tftp",
71         tftp_open,
72         tftp_close,
73         tftp_read,
74         tftp_write,
75         tftp_seek,
76         tftp_stat,
77         null_readdir
78 };
79
80 extern struct in_addr servip;
81
82 static int      tftpport = 2000;
83
84 #define RSPACE 520              /* max data packet, rounded up */
85
86 struct tftp_handle {
87         struct iodesc  *iodesc;
88         int             currblock;      /* contents of lastdata */
89         int             islastblock;    /* flag */
90         int             validsize;
91         int             off;
92         char           *path;   /* saved for re-requests */
93         struct {
94                 u_char header[HEADER_SIZE];
95                 struct tftphdr t;
96                 u_char space[RSPACE];
97         } lastdata;
98 };
99
100 static int tftperrors[8] = {
101         0,                      /* ??? */
102         ENOENT,
103         EPERM,
104         ENOSPC,
105         EINVAL,                 /* ??? */
106         EINVAL,                 /* ??? */
107         EEXIST,
108         EINVAL                  /* ??? */
109 };
110
111 static ssize_t 
112 recvtftp(d, pkt, len, tleft)
113         register struct iodesc *d;
114         register void  *pkt;
115         register ssize_t len;
116         time_t          tleft;
117 {
118         struct tftphdr *t;
119
120         errno = 0;
121
122         len = readudp(d, pkt, len, tleft);
123
124         if (len < 4)
125                 return (-1);
126
127         t = (struct tftphdr *) pkt;
128         switch (ntohs(t->th_opcode)) {
129         case DATA: {
130                 int got;
131
132                 if (htons(t->th_block) != d->xid) {
133                         /*
134                          * Expected block?
135                          */
136                         return (-1);
137                 }
138                 if (d->xid == 1) {
139                         /*
140                          * First data packet from new port.
141                          */
142                         register struct udphdr *uh;
143                         uh = (struct udphdr *) pkt - 1;
144                         d->destport = uh->uh_sport;
145                 } /* else check uh_sport has not changed??? */
146                 got = len - (t->th_data - (char *) t);
147                 return got;
148         }
149         case ERROR:
150                 if ((unsigned) ntohs(t->th_code) >= 8) {
151                         printf("illegal tftp error %d\n", ntohs(t->th_code));
152                         errno = EIO;
153                 } else {
154 #ifdef DEBUG
155                         printf("tftp-error %d\n", ntohs(t->th_code));
156 #endif
157                         errno = tftperrors[ntohs(t->th_code)];
158                 }
159                 return (-1);
160         default:
161 #ifdef DEBUG
162                 printf("tftp type %d not handled\n", ntohs(t->th_opcode));
163 #endif
164                 return (-1);
165         }
166 }
167
168 /* send request, expect first block (or error) */
169 static int 
170 tftp_makereq(h)
171         struct tftp_handle *h;
172 {
173         struct {
174                 u_char header[HEADER_SIZE];
175                 struct tftphdr  t;
176                 u_char space[FNAME_SIZE + 6];
177         } wbuf;
178         char           *wtail;
179         int             l;
180         ssize_t         res;
181         struct tftphdr *t;
182
183         wbuf.t.th_opcode = htons((u_short) RRQ);
184         wtail = wbuf.t.th_stuff;
185         l = strlen(h->path);
186         bcopy(h->path, wtail, l + 1);
187         wtail += l + 1;
188         bcopy("octet", wtail, 6);
189         wtail += 6;
190
191         t = &h->lastdata.t;
192
193         /* h->iodesc->myport = htons(--tftpport); */
194         h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
195         h->iodesc->destport = htons(IPPORT_TFTP);
196         h->iodesc->xid = 1;     /* expected block */
197
198         res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
199                        recvtftp, t, sizeof(*t) + RSPACE);
200
201         if (res == -1)
202                 return (errno);
203
204         h->currblock = 1;
205         h->validsize = res;
206         h->islastblock = 0;
207         if (res < SEGSIZE)
208                 h->islastblock = 1;     /* very short file */
209         return (0);
210 }
211
212 /* ack block, expect next */
213 static int 
214 tftp_getnextblock(h)
215         struct tftp_handle *h;
216 {
217         struct {
218                 u_char header[HEADER_SIZE];
219                 struct tftphdr t;
220         } wbuf;
221         char           *wtail;
222         int             res;
223         struct tftphdr *t;
224
225         wbuf.t.th_opcode = htons((u_short) ACK);
226         wtail = (char *) &wbuf.t.th_block;
227         wbuf.t.th_block = htons((u_short) h->currblock);
228         wtail += 2;
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 static int 
248 tftp_open(path, f)
249         const char *path;
250         struct open_file *f;
251 {
252         struct tftp_handle *tftpfile;
253         struct iodesc  *io;
254         int             res;
255
256         tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile));
257         if (!tftpfile)
258                 return (ENOMEM);
259
260         tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
261         if (io == NULL)
262                 return (EINVAL);
263
264         io->destip = servip;
265         tftpfile->off = 0;
266         tftpfile->path = strdup(path);
267         if (tftpfile->path == NULL) {
268             free(tftpfile);
269             return(ENOMEM);
270         }
271
272         res = tftp_makereq(tftpfile, path);
273
274         if (res) {
275                 free(tftpfile->path);
276                 free(tftpfile);
277                 return (res);
278         }
279         f->f_fsdata = (void *) tftpfile;
280         return (0);
281 }
282
283 static int 
284 tftp_read(f, addr, size, resid)
285         struct open_file *f;
286         void           *addr;
287         size_t          size;
288         size_t         *resid;  /* out */
289 {
290         struct tftp_handle *tftpfile;
291         static int      tc = 0;
292         tftpfile = (struct tftp_handle *) f->f_fsdata;
293
294         while (size > 0) {
295                 int needblock, count;
296
297                 if (!(tc++ % 16))
298                         twiddle();
299
300                 needblock = tftpfile->off / SEGSIZE + 1;
301
302                 if (tftpfile->currblock > needblock)    /* seek backwards */
303                         tftp_makereq(tftpfile); /* no error check, it worked
304                                                  * for open */
305
306                 while (tftpfile->currblock < needblock) {
307                         int res;
308
309                         res = tftp_getnextblock(tftpfile);
310                         if (res) {      /* no answer */
311 #ifdef DEBUG
312                                 printf("tftp: read error\n");
313 #endif
314                                 return (res);
315                         }
316                         if (tftpfile->islastblock)
317                                 break;
318                 }
319
320                 if (tftpfile->currblock == needblock) {
321                         int offinblock, inbuffer;
322
323                         offinblock = tftpfile->off % SEGSIZE;
324
325                         inbuffer = tftpfile->validsize - offinblock;
326                         if (inbuffer < 0) {
327 #ifdef DEBUG
328                                 printf("tftp: invalid offset %d\n",
329                                     tftpfile->off);
330 #endif
331                                 return (EINVAL);
332                         }
333                         count = (size < inbuffer ? size : inbuffer);
334                         bcopy(tftpfile->lastdata.t.th_data + offinblock,
335                             addr, count);
336
337                         addr += count;
338                         tftpfile->off += count;
339                         size -= count;
340
341                         if ((tftpfile->islastblock) && (count == inbuffer))
342                                 break;  /* EOF */
343                 } else {
344 #ifdef DEBUG
345                         printf("tftp: block %d not found\n", needblock);
346 #endif
347                         return (EINVAL);
348                 }
349
350         }
351
352         if (resid)
353                 *resid = size;
354         return (0);
355 }
356
357 static int 
358 tftp_close(f)
359         struct open_file *f;
360 {
361         struct tftp_handle *tftpfile;
362         tftpfile = (struct tftp_handle *) f->f_fsdata;
363
364         /* let it time out ... */
365
366         if (tftpfile) {
367                 free(tftpfile->path);
368                 free(tftpfile);
369         }
370         return (0);
371 }
372
373 static int 
374 tftp_write(f, start, size, resid)
375         struct open_file *f;
376         void           *start;
377         size_t          size;
378         size_t         *resid;  /* out */
379 {
380         return (EROFS);
381 }
382
383 static int 
384 tftp_stat(f, sb)
385         struct open_file *f;
386         struct stat    *sb;
387 {
388         struct tftp_handle *tftpfile;
389         tftpfile = (struct tftp_handle *) f->f_fsdata;
390
391         sb->st_mode = 0444 | S_IFREG;
392         sb->st_nlink = 1;
393         sb->st_uid = 0;
394         sb->st_gid = 0;
395         sb->st_size = -1;
396         return (0);
397 }
398
399 static off_t 
400 tftp_seek(f, offset, where)
401         struct open_file *f;
402         off_t           offset;
403         int             where;
404 {
405         struct tftp_handle *tftpfile;
406         tftpfile = (struct tftp_handle *) f->f_fsdata;
407
408         switch (where) {
409         case SEEK_SET:
410                 tftpfile->off = offset;
411                 break;
412         case SEEK_CUR:
413                 tftpfile->off += offset;
414                 break;
415         default:
416                 errno = EOFFSET;
417                 return (-1);
418         }
419         return (tftpfile->off);
420 }