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 $
34 * $DragonFly: src/lib/libstand/tftp.c,v 1.5 2005/12/11 03:27:26 swildner Exp $
38 * Simple TFTP implementation for libsa.
40 * - socket descriptor (int) at open_file->f_devdata
41 * - server host IP in global servip
44 * - lseek only with SEEK_SET or SEEK_CUR
45 * - no big time differences between transfers (<tftp timeout)
48 #include <sys/param.h>
50 #include <netinet/in.h>
51 #include <netinet/udp.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
];
101 static int tftperrors
[8] = {
113 recvtftp(struct iodesc
*d
, void *pkt
, size_t len
, time_t tleft
)
119 len
= readudp(d
, pkt
, len
, tleft
);
124 t
= (struct tftphdr
*) pkt
;
125 switch (ntohs(t
->th_opcode
)) {
129 if (htons(t
->th_block
) != d
->xid
) {
137 * First data packet from new port.
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
);
147 if ((unsigned) ntohs(t
->th_code
) >= 8) {
148 printf("illegal tftp error %d\n", ntohs(t
->th_code
));
152 printf("tftp-error %d\n", ntohs(t
->th_code
));
154 errno
= tftperrors
[ntohs(t
->th_code
)];
159 printf("tftp type %d not handled\n", ntohs(t
->th_opcode
));
165 /* send request, expect first block (or error) */
167 tftp_makereq(struct tftp_handle
*h
)
170 u_char header
[HEADER_SIZE
];
172 u_char space
[FNAME_SIZE
+ 6];
179 wbuf
.t
.th_opcode
= htons((u_short
) RRQ
);
180 wtail
= wbuf
.t
.th_stuff
;
182 bcopy(h
->path
, wtail
, l
+ 1);
184 bcopy("octet", wtail
, 6);
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 */
194 res
= sendrecv(h
->iodesc
, sendudp
, &wbuf
.t
, wtail
- (char *) &wbuf
.t
,
195 recvtftp
, t
, sizeof(*t
) + RSPACE
);
204 h
->islastblock
= 1; /* very short file */
208 /* ack block, expect next */
210 tftp_getnextblock(struct tftp_handle
*h
)
213 u_char header
[HEADER_SIZE
];
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
);
227 h
->iodesc
->xid
= h
->currblock
+ 1; /* expected block */
229 res
= sendrecv(h
->iodesc
, sendudp
, &wbuf
.t
, wtail
- (char *) &wbuf
.t
,
230 recvtftp
, t
, sizeof(*t
) + RSPACE
);
232 if (res
== -1) /* 0 is OK! */
238 h
->islastblock
= 1; /* EOF */
243 tftp_open(const char *path
, struct open_file
*f
)
245 struct tftp_handle
*tftpfile
;
249 tftpfile
= (struct tftp_handle
*) malloc(sizeof(*tftpfile
));
253 tftpfile
->iodesc
= io
= socktodesc(*(int *) (f
->f_devdata
));
259 tftpfile
->path
= strdup(path
);
260 if (tftpfile
->path
== NULL
) {
265 res
= tftp_makereq(tftpfile
);
268 free(tftpfile
->path
);
272 f
->f_fsdata
= (void *) tftpfile
;
281 tftp_read(struct open_file
*f
, void *addr
, size_t size
, size_t *resid
)
283 struct tftp_handle
*tftpfile
;
285 tftpfile
= (struct tftp_handle
*) f
->f_fsdata
;
288 int needblock
, count
;
293 needblock
= tftpfile
->off
/ SEGSIZE
+ 1;
295 if (tftpfile
->currblock
> needblock
) /* seek backwards */
296 tftp_makereq(tftpfile
); /* no error check, it worked
299 while (tftpfile
->currblock
< needblock
) {
302 res
= tftp_getnextblock(tftpfile
);
303 if (res
) { /* no answer */
305 printf("tftp: read error\n");
309 if (tftpfile
->islastblock
)
313 if (tftpfile
->currblock
== needblock
) {
314 int offinblock
, inbuffer
;
316 offinblock
= tftpfile
->off
% SEGSIZE
;
318 inbuffer
= tftpfile
->validsize
- offinblock
;
321 printf("tftp: invalid offset %d\n",
326 count
= (size
< inbuffer
? size
: inbuffer
);
327 bcopy(tftpfile
->lastdata
.t
.th_data
+ offinblock
,
331 tftpfile
->off
+= count
;
334 if ((tftpfile
->islastblock
) && (count
== inbuffer
))
338 printf("tftp: block %d not found\n", needblock
);
351 tftp_close(struct open_file
*f
)
353 struct tftp_handle
*tftpfile
;
354 tftpfile
= (struct tftp_handle
*) f
->f_fsdata
;
356 /* let it time out ... */
359 free(tftpfile
->path
);
370 tftp_write(struct open_file
*f
, void *start
, size_t size
, size_t *resid
)
376 tftp_stat(struct open_file
*f
, struct stat
*sb
)
378 struct tftp_handle
*tftpfile
;
379 tftpfile
= (struct tftp_handle
*) f
->f_fsdata
;
381 sb
->st_mode
= 0444 | S_IFREG
;
390 tftp_seek(struct open_file
*f
, off_t offset
, int where
)
392 struct tftp_handle
*tftpfile
;
393 tftpfile
= (struct tftp_handle
*) f
->f_fsdata
;
397 tftpfile
->off
= offset
;
400 tftpfile
->off
+= offset
;
406 return (tftpfile
->off
);