Remove long since obsolete ID tags
[tftp-hpa.git] / tftp / tftp.c
blob2d26b4be70db1b95b1713f38ab549bda4759dd4b
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "common/tftpsubs.h"
37 * TFTP User Program -- Protocol Machines
39 #include "extern.h"
41 extern struct sockaddr_in peeraddr; /* filled in by main */
42 extern int f; /* the opened socket */
43 extern int trace;
44 extern int verbose;
45 extern int rexmtval;
46 extern int maxtimeout;
48 #define PKTSIZE SEGSIZE+4
49 char ackbuf[PKTSIZE];
50 int timeout;
51 sigjmp_buf toplevel;
52 sigjmp_buf timeoutbuf;
54 static void nak(int, const char *);
55 static int makerequest(int, const char *, struct tftphdr *, const char *);
56 static void printstats(const char *, unsigned long);
57 static void startclock(void);
58 static void stopclock(void);
59 static void timer(int);
60 static void tpacket(const char *, struct tftphdr *, int);
63 * Send the requested file.
65 void tftp_sendfile(int fd, const char *name, const char *mode)
67 struct tftphdr *ap; /* data and ack packets */
68 struct tftphdr *dp;
69 int n;
70 volatile int is_request;
71 volatile u_short block;
72 volatile int size, convert;
73 volatile off_t amount;
74 struct sockaddr_in from;
75 socklen_t fromlen;
76 FILE *file;
77 u_short ap_opcode, ap_block;
79 startclock(); /* start stat's clock */
80 dp = r_init(); /* reset fillbuf/read-ahead code */
81 ap = (struct tftphdr *)ackbuf;
82 convert = !strcmp(mode, "netascii");
83 file = fdopen(fd, convert ? "rt" : "rb");
84 block = 0;
85 is_request = 1; /* First packet is the actual WRQ */
86 amount = 0;
88 bsd_signal(SIGALRM, timer);
89 do {
90 if (is_request) {
91 size = makerequest(WRQ, name, dp, mode) - 4;
92 } else {
93 /* size = read(fd, dp->th_data, SEGSIZE); */
94 size = readit(file, &dp, convert);
95 if (size < 0) {
96 nak(errno + 100, NULL);
97 break;
99 dp->th_opcode = htons((u_short) DATA);
100 dp->th_block = htons((u_short) block);
102 timeout = 0;
103 (void)sigsetjmp(timeoutbuf, 1);
105 if (trace)
106 tpacket("sent", dp, size + 4);
107 n = sendto(f, dp, size + 4, 0,
108 (struct sockaddr *)&peeraddr, sizeof(peeraddr));
109 if (n != size + 4) {
110 perror("tftp: sendto");
111 goto abort;
113 read_ahead(file, convert);
114 for (;;) {
115 alarm(rexmtval);
116 do {
117 fromlen = sizeof(from);
118 n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
119 (struct sockaddr *)&from, &fromlen);
120 } while (n <= 0);
121 alarm(0);
122 if (n < 0) {
123 perror("tftp: recvfrom");
124 goto abort;
126 peeraddr.sin_port = from.sin_port; /* added */
127 if (trace)
128 tpacket("received", ap, n);
129 /* should verify packet came from server */
130 ap_opcode = ntohs((u_short) ap->th_opcode);
131 ap_block = ntohs((u_short) ap->th_block);
132 if (ap_opcode == ERROR) {
133 printf("Error code %d: %s\n", ap_block, ap->th_msg);
134 goto abort;
136 if (ap_opcode == ACK) {
137 int j;
139 if (ap_block == block) {
140 break;
142 /* On an error, try to synchronize
143 * both sides.
145 j = synchnet(f);
146 if (j && trace) {
147 printf("discarded %d packets\n", j);
150 * RFC1129/RFC1350: We MUST NOT re-send the DATA
151 * packet in response to an invalid ACK. Doing so
152 * would cause the Sorcerer's Apprentice bug.
156 if (!is_request)
157 amount += size;
158 is_request = 0;
159 block++;
160 } while (size == SEGSIZE || block == 1);
161 abort:
162 fclose(file);
163 stopclock();
164 if (amount > 0)
165 printstats("Sent", amount);
169 * Receive a file.
171 void tftp_recvfile(int fd, const char *name, const char *mode)
173 struct tftphdr *ap;
174 struct tftphdr *dp;
175 int n;
176 volatile u_short block;
177 volatile int size, firsttrip;
178 volatile unsigned long amount;
179 struct sockaddr_in from;
180 socklen_t fromlen;
181 FILE *file;
182 volatile int convert; /* true if converting crlf -> lf */
183 u_short dp_opcode, dp_block;
185 startclock();
186 dp = w_init();
187 ap = (struct tftphdr *)ackbuf;
188 convert = !strcmp(mode, "netascii");
189 file = fdopen(fd, convert ? "wt" : "wb");
190 block = 1;
191 firsttrip = 1;
192 amount = 0;
194 bsd_signal(SIGALRM, timer);
195 do {
196 if (firsttrip) {
197 size = makerequest(RRQ, name, ap, mode);
198 firsttrip = 0;
199 } else {
200 ap->th_opcode = htons((u_short) ACK);
201 ap->th_block = htons((u_short) block);
202 size = 4;
203 block++;
205 timeout = 0;
206 (void)sigsetjmp(timeoutbuf, 1);
207 send_ack:
208 if (trace)
209 tpacket("sent", ap, size);
210 if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr,
211 sizeof(peeraddr)) != size) {
212 alarm(0);
213 perror("tftp: sendto");
214 goto abort;
216 write_behind(file, convert);
217 for (;;) {
218 alarm(rexmtval);
219 do {
220 fromlen = sizeof(from);
221 n = recvfrom(f, dp, PKTSIZE, 0,
222 (struct sockaddr *)&from, &fromlen);
223 } while (n <= 0);
224 alarm(0);
225 if (n < 0) {
226 perror("tftp: recvfrom");
227 goto abort;
229 peeraddr.sin_port = from.sin_port; /* added */
230 if (trace)
231 tpacket("received", dp, n);
232 /* should verify client address */
233 dp_opcode = ntohs((u_short) dp->th_opcode);
234 dp_block = ntohs((u_short) dp->th_block);
235 if (dp_opcode == ERROR) {
236 printf("Error code %d: %s\n", dp_block, dp->th_msg);
237 goto abort;
239 if (dp_opcode == DATA) {
240 int j;
242 if (dp_block == block) {
243 break; /* have next packet */
245 /* On an error, try to synchronize
246 * both sides.
248 j = synchnet(f);
249 if (j && trace) {
250 printf("discarded %d packets\n", j);
252 if (dp_block == (block - 1)) {
253 goto send_ack; /* resend ack */
257 /* size = write(fd, dp->th_data, n - 4); */
258 size = writeit(file, &dp, n - 4, convert);
259 if (size < 0) {
260 nak(errno + 100, NULL);
261 break;
263 amount += size;
264 } while (size == SEGSIZE);
265 abort: /* ok to ack, since user */
266 ap->th_opcode = htons((u_short) ACK); /* has seen err msg */
267 ap->th_block = htons((u_short) block);
268 (void)sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
269 sizeof(peeraddr));
270 write_behind(file, convert); /* flush last buffer */
271 fclose(file);
272 stopclock();
273 if (amount > 0)
274 printstats("Received", amount);
277 static int
278 makerequest(int request, const char *name,
279 struct tftphdr *tp, const char *mode)
281 char *cp;
283 tp->th_opcode = htons((u_short) request);
284 cp = (char *)&(tp->th_stuff);
285 strcpy(cp, name);
286 cp += strlen(name);
287 *cp++ = '\0';
288 strcpy(cp, mode);
289 cp += strlen(mode);
290 *cp++ = '\0';
291 return (cp - (char *)tp);
294 static const char *const errmsgs[] = {
295 "Undefined error code", /* 0 - EUNDEF */
296 "File not found", /* 1 - ENOTFOUND */
297 "Access denied", /* 2 - EACCESS */
298 "Disk full or allocation exceeded", /* 3 - ENOSPACE */
299 "Illegal TFTP operation", /* 4 - EBADOP */
300 "Unknown transfer ID", /* 5 - EBADID */
301 "File already exists", /* 6 - EEXISTS */
302 "No such user", /* 7 - ENOUSER */
303 "Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
306 #define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
309 * Send a nak packet (error message).
310 * Error code passed in is one of the
311 * standard TFTP codes, or a UNIX errno
312 * offset by 100.
314 static void nak(int error, const char *msg)
316 struct tftphdr *tp;
317 int length;
319 tp = (struct tftphdr *)ackbuf;
320 tp->th_opcode = htons((u_short) ERROR);
321 tp->th_code = htons((u_short) error);
323 if (error >= 100) {
324 /* This is a Unix errno+100 */
325 if (!msg)
326 msg = strerror(error - 100);
327 error = EUNDEF;
328 } else {
329 if ((unsigned)error >= ERR_CNT)
330 error = EUNDEF;
332 if (!msg)
333 msg = errmsgs[error];
336 tp->th_code = htons((u_short) error);
338 length = strlen(msg) + 1;
339 memcpy(tp->th_msg, msg, length);
340 length += 4; /* Add space for header */
342 if (trace)
343 tpacket("sent", tp, length);
344 if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&peeraddr,
345 sizeof(peeraddr)) != length)
346 perror("nak");
349 static void tpacket(const char *s, struct tftphdr *tp, int n)
351 static const char *opcodes[] =
352 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
353 char *cp, *file;
354 u_short op = ntohs((u_short) tp->th_opcode);
356 if (op < RRQ || op > ERROR)
357 printf("%s opcode=%x ", s, op);
358 else
359 printf("%s %s ", s, opcodes[op]);
360 switch (op) {
362 case RRQ:
363 case WRQ:
364 n -= 2;
365 file = cp = (char *)&(tp->th_stuff);
366 cp = strchr(cp, '\0');
367 printf("<file=%s, mode=%s>\n", file, cp + 1);
368 break;
370 case DATA:
371 printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
372 break;
374 case ACK:
375 printf("<block=%d>\n", ntohs(tp->th_block));
376 break;
378 case ERROR:
379 printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
380 break;
384 struct timeval tstart;
385 struct timeval tstop;
387 static void startclock(void)
389 (void)gettimeofday(&tstart, NULL);
392 static void stopclock(void)
395 (void)gettimeofday(&tstop, NULL);
398 static void printstats(const char *direction, unsigned long amount)
400 double delta;
402 delta = (tstop.tv_sec + (tstop.tv_usec / 100000.0)) -
403 (tstart.tv_sec + (tstart.tv_usec / 100000.0));
404 if (verbose) {
405 printf("%s %lu bytes in %.1f seconds", direction, amount, delta);
406 printf(" [%.0f bit/s]", (amount * 8.) / delta);
407 putchar('\n');
411 static void timer(int sig)
413 int save_errno = errno;
415 (void)sig; /* Shut up unused warning */
417 timeout += rexmtval;
418 if (timeout >= maxtimeout) {
419 printf("Transfer timed out.\n");
420 errno = save_errno;
421 siglongjmp(toplevel, -1);
423 errno = save_errno;
424 siglongjmp(timeoutbuf, 1);