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