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