tftp.c revision 1.9.2.1 1 /* $NetBSD: tftp.c,v 1.9.2.1 2000/01/23 12:10:09 he Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. 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 by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: tftp.c,v 1.9.2.1 2000/01/23 12:10:09 he Exp $");
42 #endif
43 #endif /* not lint */
44
45 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
46
47 /*
48 * TFTP User Program -- Protocol Machines
49 */
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <sys/time.h>
53
54 #include <netinet/in.h>
55
56 #include <arpa/tftp.h>
57
58 #include <err.h>
59 #include <errno.h>
60 #include <setjmp.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include "extern.h"
67 #include "tftpsubs.h"
68
69 extern struct sockaddr_in peeraddr; /* filled in by main */
70 extern int f; /* the opened socket */
71 extern int trace;
72 extern int verbose;
73 extern int rexmtval;
74 extern int maxtimeout;
75
76 #define PKTSIZE SEGSIZE+4
77 char ackbuf[PKTSIZE];
78 int timeout;
79 jmp_buf toplevel;
80 jmp_buf timeoutbuf;
81
82 static void nak __P((int));
83 static int makerequest __P((int, const char *, struct tftphdr *, const char *));
84 static void printstats __P((const char *, unsigned long));
85 static void startclock __P((void));
86 static void stopclock __P((void));
87 static void timer __P((int));
88 static void tpacket __P((const char *, struct tftphdr *, int));
89
90 /*
91 * Send the requested file.
92 */
93 void
94 sendfile(fd, name, mode)
95 int fd;
96 char *name;
97 char *mode;
98 {
99 struct tftphdr *ap; /* data and ack packets */
100 struct tftphdr *dp;
101 int n;
102 volatile int block, size, convert;
103 volatile unsigned long amount;
104 struct sockaddr_in from;
105 int fromlen;
106 FILE *file;
107
108 startclock(); /* start stat's clock */
109 dp = r_init(); /* reset fillbuf/read-ahead code */
110 ap = (struct tftphdr *)ackbuf;
111 file = fdopen(fd, "r");
112 convert = !strcmp(mode, "netascii");
113 block = 0;
114 amount = 0;
115
116 signal(SIGALRM, timer);
117 do {
118 if (block == 0)
119 size = makerequest(WRQ, name, dp, mode) - 4;
120 else {
121 /* size = read(fd, dp->th_data, SEGSIZE); */
122 size = readit(file, &dp, convert);
123 if (size < 0) {
124 nak(errno + 100);
125 break;
126 }
127 dp->th_opcode = htons((u_short)DATA);
128 dp->th_block = htons((u_short)block);
129 }
130 timeout = 0;
131 (void) setjmp(timeoutbuf);
132 send_data:
133 if (trace)
134 tpacket("sent", dp, size + 4);
135 n = sendto(f, dp, size + 4, 0,
136 (struct sockaddr *)&peeraddr, sizeof(peeraddr));
137 if (n != size + 4) {
138 warn("sendto");
139 goto abort;
140 }
141 read_ahead(file, convert);
142 for ( ; ; ) {
143 alarm(rexmtval);
144 do {
145 fromlen = sizeof(from);
146 n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
147 (struct sockaddr *)&from, &fromlen);
148 } while (n <= 0);
149 alarm(0);
150 if (n < 0) {
151 warn("recvfrom");
152 goto abort;
153 }
154 peeraddr.sin_port = from.sin_port; /* added */
155 if (trace)
156 tpacket("received", ap, n);
157 /* should verify packet came from server */
158 ap->th_opcode = ntohs(ap->th_opcode);
159 ap->th_block = ntohs(ap->th_block);
160 if (ap->th_opcode == ERROR) {
161 printf("Error code %d: %s\n", ap->th_code,
162 ap->th_msg);
163 goto abort;
164 }
165 if (ap->th_opcode == ACK) {
166 int j;
167
168 if (ap->th_block == block) {
169 break;
170 }
171 /* On an error, try to synchronize
172 * both sides.
173 */
174 j = synchnet(f);
175 if (j && trace) {
176 printf("discarded %d packets\n",
177 j);
178 }
179 if (ap->th_block == (block-1)) {
180 goto send_data;
181 }
182 }
183 }
184 if (block > 0)
185 amount += size;
186 block++;
187 } while (size == SEGSIZE || block == 1);
188 abort:
189 fclose(file);
190 stopclock();
191 if (amount > 0)
192 printstats("Sent", amount);
193 }
194
195 /*
196 * Receive a file.
197 */
198 void
199 recvfile(fd, name, mode)
200 int fd;
201 char *name;
202 char *mode;
203 {
204 struct tftphdr *ap;
205 struct tftphdr *dp;
206 int n;
207 volatile int block, size, firsttrip;
208 volatile unsigned long amount;
209 struct sockaddr_in from;
210 int fromlen;
211 FILE *file;
212 volatile int convert; /* true if converting crlf -> lf */
213
214 startclock();
215 dp = w_init();
216 ap = (struct tftphdr *)ackbuf;
217 file = fdopen(fd, "w");
218 convert = !strcmp(mode, "netascii");
219 block = 1;
220 firsttrip = 1;
221 amount = 0;
222
223 signal(SIGALRM, timer);
224 do {
225 if (firsttrip) {
226 size = makerequest(RRQ, name, ap, mode);
227 firsttrip = 0;
228 } else {
229 ap->th_opcode = htons((u_short)ACK);
230 ap->th_block = htons((u_short)(block));
231 size = 4;
232 block++;
233 }
234 timeout = 0;
235 (void) setjmp(timeoutbuf);
236 send_ack:
237 if (trace)
238 tpacket("sent", ap, size);
239 if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr,
240 sizeof(peeraddr)) != size) {
241 alarm(0);
242 warn("sendto");
243 goto abort;
244 }
245 write_behind(file, convert);
246 for ( ; ; ) {
247 alarm(rexmtval);
248 do {
249 fromlen = sizeof(from);
250 n = recvfrom(f, dp, PKTSIZE, 0,
251 (struct sockaddr *)&from, &fromlen);
252 } while (n <= 0);
253 alarm(0);
254 if (n < 0) {
255 warn("recvfrom");
256 goto abort;
257 }
258 peeraddr.sin_port = from.sin_port; /* added */
259 if (trace)
260 tpacket("received", dp, n);
261 /* should verify client address */
262 dp->th_opcode = ntohs(dp->th_opcode);
263 dp->th_block = ntohs(dp->th_block);
264 if (dp->th_opcode == ERROR) {
265 printf("Error code %d: %s\n", dp->th_code,
266 dp->th_msg);
267 goto abort;
268 }
269 if (dp->th_opcode == DATA) {
270 int j;
271
272 if (dp->th_block == block) {
273 break; /* have next packet */
274 }
275 /* On an error, try to synchronize
276 * both sides.
277 */
278 j = synchnet(f);
279 if (j && trace) {
280 printf("discarded %d packets\n", j);
281 }
282 if (dp->th_block == (block-1)) {
283 goto send_ack; /* resend ack */
284 }
285 }
286 }
287 /* size = write(fd, dp->th_data, n - 4); */
288 size = writeit(file, &dp, n - 4, convert);
289 if (size < 0) {
290 nak(errno + 100);
291 break;
292 }
293 amount += size;
294 } while (size == SEGSIZE);
295 abort: /* ok to ack, since user */
296 ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
297 ap->th_block = htons((u_short)block);
298 (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
299 sizeof(peeraddr));
300 write_behind(file, convert); /* flush last buffer */
301 fclose(file);
302 stopclock();
303 if (amount > 0)
304 printstats("Received", amount);
305 }
306
307 static int
308 makerequest(request, name, tp, mode)
309 int request;
310 const char *name;
311 struct tftphdr *tp;
312 const char *mode;
313 {
314 char *cp;
315
316 tp->th_opcode = htons((u_short)request);
317 #ifndef __SVR4
318 cp = tp->th_stuff;
319 #else
320 cp = (void *)&tp->th_stuff;
321 #endif
322 strcpy(cp, name);
323 cp += strlen(name);
324 *cp++ = '\0';
325 strcpy(cp, mode);
326 cp += strlen(mode);
327 *cp++ = '\0';
328 return (cp - (char *)tp);
329 }
330
331 const struct errmsg {
332 int e_code;
333 const char *e_msg;
334 } errmsgs[] = {
335 { EUNDEF, "Undefined error code" },
336 { ENOTFOUND, "File not found" },
337 { EACCESS, "Access violation" },
338 { ENOSPACE, "Disk full or allocation exceeded" },
339 { EBADOP, "Illegal TFTP operation" },
340 { EBADID, "Unknown transfer ID" },
341 { EEXISTS, "File already exists" },
342 { ENOUSER, "No such user" },
343 { -1, 0 }
344 };
345
346 /*
347 * Send a nak packet (error message).
348 * Error code passed in is one of the
349 * standard TFTP codes, or a UNIX errno
350 * offset by 100.
351 */
352 static void
353 nak(error)
354 int error;
355 {
356 const struct errmsg *pe;
357 struct tftphdr *tp;
358 int length;
359
360 tp = (struct tftphdr *)ackbuf;
361 tp->th_opcode = htons((u_short)ERROR);
362 for (pe = errmsgs; pe->e_code >= 0; pe++)
363 if (pe->e_code == error)
364 break;
365 if (pe->e_code < 0) {
366 tp->th_code = EUNDEF;
367 strcpy(tp->th_msg, strerror(error - 100));
368 } else {
369 tp->th_code = htons((u_short)error);
370 strcpy(tp->th_msg, pe->e_msg);
371 }
372 length = strlen(pe->e_msg) + 4;
373 if (trace)
374 tpacket("sent", tp, length);
375 if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&peeraddr,
376 sizeof(peeraddr)) != length)
377 warn("nak");
378 }
379
380 static void
381 tpacket(s, tp, n)
382 const char *s;
383 struct tftphdr *tp;
384 int n;
385 {
386 static char *opcodes[] =
387 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
388 char *cp, *file;
389 u_short op = ntohs(tp->th_opcode);
390
391 if (op < RRQ || op > ERROR)
392 printf("%s opcode=%x ", s, op);
393 else
394 printf("%s %s ", s, opcodes[op]);
395 switch (op) {
396
397 case RRQ:
398 case WRQ:
399 n -= 2;
400 #ifndef __SVR4
401 cp = tp->th_stuff;
402 #else
403 cp = (void *) &tp->th_stuff;
404 #endif
405 file = cp;
406 cp = strchr(cp, '\0');
407 printf("<file=%s, mode=%s>\n", file, cp + 1);
408 break;
409
410 case DATA:
411 printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
412 break;
413
414 case ACK:
415 printf("<block=%d>\n", ntohs(tp->th_block));
416 break;
417
418 case ERROR:
419 printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
420 break;
421 }
422 }
423
424 struct timeval tstart;
425 struct timeval tstop;
426
427 static void
428 startclock()
429 {
430
431 (void)gettimeofday(&tstart, NULL);
432 }
433
434 static void
435 stopclock()
436 {
437
438 (void)gettimeofday(&tstop, NULL);
439 }
440
441 static void
442 printstats(direction, amount)
443 const char *direction;
444 unsigned long amount;
445 {
446 double delta;
447
448 /* compute delta in 1/10's second units */
449 delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
450 ((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
451 delta = delta/10.; /* back to seconds */
452 printf("%s %ld bytes in %.1f seconds", direction, amount, delta);
453 if (verbose)
454 printf(" [%.0f bits/sec]", (amount*8.)/delta);
455 putchar('\n');
456 }
457
458 static void
459 timer(sig)
460 int sig;
461 {
462
463 timeout += rexmtval;
464 if (timeout >= maxtimeout) {
465 printf("Transfer timed out.\n");
466 longjmp(toplevel, -1);
467 }
468 longjmp(timeoutbuf, 1);
469 }
470