Apply some code cleanups that apparently fix Solaris 7 gcc problems.
[tftp-hpa.git] / tftp / tftp.c
blob0e615e528ef70c01634f2e62956471c36098bd0f
1 /* $Id$ */
3 /* $OpenBSD: tftp.c,v 1.4 1997/08/06 06:43:45 deraadt Exp $ */
4 /* $NetBSD: tftp.c,v 1.5 1995/04/29 05:55:25 cgd Exp $ */
6 /*
7 * Copyright (c) 1983, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include "tftpsubs.h"
41 #ifndef lint
42 /* static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; */
43 /* static char rcsid[] = "$OpenBSD: tftp.c,v 1.4 1997/08/06 06:43:45 deraadt Exp $"; */
44 static const char *rcsid UNUSED =
45 "tftp-hpa $Id$";
46 #endif /* not lint */
48 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
51 * TFTP User Program -- Protocol Machines
53 #include "extern.h"
55 extern struct sockaddr_in peeraddr; /* filled in by main */
56 extern int f; /* the opened socket */
57 extern int trace;
58 extern int verbose;
59 extern int rexmtval;
60 extern int maxtimeout;
62 #define PKTSIZE SEGSIZE+4
63 char ackbuf[PKTSIZE];
64 int timeout;
65 sigjmp_buf toplevel;
66 sigjmp_buf timeoutbuf;
68 static void nak(int, const char *);
69 static int makerequest(int, const char *, struct tftphdr *, const char *);
70 static void printstats(const char *, unsigned long);
71 static void startclock(void);
72 static void stopclock(void);
73 static void timer(int);
74 static void tpacket(const char *, struct tftphdr *, int);
77 * Send the requested file.
79 void
80 tftp_sendfile(int fd, const char *name, const char *mode)
82 struct tftphdr *ap; /* data and ack packets */
83 struct tftphdr *dp;
84 int n;
85 volatile int is_request;
86 volatile u_short block;
87 volatile int size, convert;
88 volatile off_t amount;
89 struct sockaddr_in from;
90 int fromlen;
91 FILE *file;
92 u_short ap_opcode, ap_block;
94 startclock(); /* start stat's clock */
95 dp = r_init(); /* reset fillbuf/read-ahead code */
96 ap = (struct tftphdr *)ackbuf;
97 convert = !strcmp(mode, "netascii");
98 file = fdopen(fd, convert ? "rt" : "rb");
99 block = 0;
100 is_request = 1; /* First packet is the actual WRQ */
101 amount = 0;
103 bsd_signal(SIGALRM, timer);
104 do {
105 if (is_request) {
106 size = makerequest(WRQ, name, dp, mode) - 4;
107 } else {
108 /* size = read(fd, dp->th_data, SEGSIZE); */
109 size = readit(file, &dp, convert);
110 if (size < 0) {
111 nak(errno + 100, NULL);
112 break;
114 dp->th_opcode = htons((u_short)DATA);
115 dp->th_block = htons((u_short)block);
117 timeout = 0;
118 (void) sigsetjmp(timeoutbuf,1);
120 if (trace)
121 tpacket("sent", dp, size + 4);
122 n = sendto(f, dp, size + 4, 0,
123 (struct sockaddr *)&peeraddr, sizeof(peeraddr));
124 if (n != size + 4) {
125 perror("tftp: sendto");
126 goto abort;
128 read_ahead(file, convert);
129 for ( ; ; ) {
130 alarm(rexmtval);
131 do {
132 fromlen = sizeof(from);
133 n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
134 (struct sockaddr *)&from, &fromlen);
135 } while (n <= 0);
136 alarm(0);
137 if (n < 0) {
138 perror("tftp: recvfrom");
139 goto abort;
141 peeraddr.sin_port = from.sin_port; /* added */
142 if (trace)
143 tpacket("received", ap, n);
144 /* should verify packet came from server */
145 ap_opcode = ntohs((u_short)ap->th_opcode);
146 ap_block = ntohs((u_short)ap->th_block);
147 if (ap_opcode == ERROR) {
148 printf("Error code %d: %s\n", ap_block,
149 ap->th_msg);
150 goto abort;
152 if (ap_opcode == ACK) {
153 int j;
155 if (ap_block == block) {
156 break;
158 /* On an error, try to synchronize
159 * both sides.
161 j = synchnet(f);
162 if (j && trace) {
163 printf("discarded %d packets\n",
167 * RFC1129/RFC1350: We MUST NOT re-send the DATA
168 * packet in response to an invalid ACK. Doing so
169 * would cause the Sorcerer's Apprentice bug.
173 if ( !is_request )
174 amount += size;
175 is_request = 0;
176 block++;
177 } while (size == SEGSIZE || block == 1);
178 abort:
179 fclose(file);
180 stopclock();
181 if (amount > 0)
182 printstats("Sent", amount);
186 * Receive a file.
188 void
189 tftp_recvfile(int fd, const char *name, const char *mode)
191 struct tftphdr *ap;
192 struct tftphdr *dp;
193 int n;
194 volatile u_short block;
195 volatile int size, firsttrip;
196 volatile unsigned long amount;
197 struct sockaddr_in from;
198 int fromlen;
199 FILE *file;
200 volatile int convert; /* true if converting crlf -> lf */
201 u_short dp_opcode, dp_block;
203 startclock();
204 dp = w_init();
205 ap = (struct tftphdr *)ackbuf;
206 convert = !strcmp(mode, "netascii");
207 file = fdopen(fd, convert ?"wt":"wb");
208 block = 1;
209 firsttrip = 1;
210 amount = 0;
212 bsd_signal(SIGALRM, timer);
213 do {
214 if (firsttrip) {
215 size = makerequest(RRQ, name, ap, mode);
216 firsttrip = 0;
217 } else {
218 ap->th_opcode = htons((u_short)ACK);
219 ap->th_block = htons((u_short)block);
220 size = 4;
221 block++;
223 timeout = 0;
224 (void) sigsetjmp(timeoutbuf,1);
225 send_ack:
226 if (trace)
227 tpacket("sent", ap, size);
228 if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr,
229 sizeof(peeraddr)) != size) {
230 alarm(0);
231 perror("tftp: sendto");
232 goto abort;
234 write_behind(file, convert);
235 for ( ; ; ) {
236 alarm(rexmtval);
237 do {
238 fromlen = sizeof(from);
239 n = recvfrom(f, dp, PKTSIZE, 0,
240 (struct sockaddr *)&from, &fromlen);
241 } while (n <= 0);
242 alarm(0);
243 if (n < 0) {
244 perror("tftp: recvfrom");
245 goto abort;
247 peeraddr.sin_port = from.sin_port; /* added */
248 if (trace)
249 tpacket("received", dp, n);
250 /* should verify client address */
251 dp_opcode = ntohs((u_short)dp->th_opcode);
252 dp_block = ntohs((u_short)dp->th_block);
253 if (dp_opcode == ERROR) {
254 printf("Error code %d: %s\n", dp_block, dp->th_msg);
255 goto abort;
257 if (dp_opcode == DATA) {
258 int j;
260 if (dp_block == block) {
261 break; /* have next packet */
263 /* On an error, try to synchronize
264 * both sides.
266 j = synchnet(f);
267 if (j && trace) {
268 printf("discarded %d packets\n", j);
270 if (dp_block == (block-1)) {
271 goto send_ack; /* resend ack */
275 /* size = write(fd, dp->th_data, n - 4); */
276 size = writeit(file, &dp, n - 4, convert);
277 if (size < 0) {
278 nak(errno + 100, NULL);
279 break;
281 amount += size;
282 } while (size == SEGSIZE);
283 abort: /* ok to ack, since user */
284 ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
285 ap->th_block = htons((u_short)block);
286 (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
287 sizeof(peeraddr));
288 write_behind(file, convert); /* flush last buffer */
289 fclose(file);
290 stopclock();
291 if (amount > 0)
292 printstats("Received", amount);
295 static int
296 makerequest(int request, const char *name,
297 struct tftphdr *tp, const char *mode)
299 char *cp;
301 tp->th_opcode = htons((u_short)request);
302 cp = (char *) &(tp->th_stuff);
303 strcpy(cp, name);
304 cp += strlen(name);
305 *cp++ = '\0';
306 strcpy(cp, mode);
307 cp += strlen(mode);
308 *cp++ = '\0';
309 return (cp - (char *)tp);
312 static const char * const errmsgs[] =
314 "Undefined error code", /* 0 - EUNDEF */
315 "File not found", /* 1 - ENOTFOUND */
316 "Access denied", /* 2 - EACCESS */
317 "Disk full or allocation exceeded", /* 3 - ENOSPACE */
318 "Illegal TFTP operation", /* 4 - EBADOP */
319 "Unknown transfer ID", /* 5 - EBADID */
320 "File already exists", /* 6 - EEXISTS */
321 "No such user", /* 7 - ENOUSER */
322 "Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
324 #define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
327 * Send a nak packet (error message).
328 * Error code passed in is one of the
329 * standard TFTP codes, or a UNIX errno
330 * offset by 100.
332 static void
333 nak(int error, const char *msg)
335 struct tftphdr *tp;
336 int length;
338 tp = (struct tftphdr *)ackbuf;
339 tp->th_opcode = htons((u_short)ERROR);
340 tp->th_code = htons((u_short)error);
342 if ( error >= 100 ) {
343 /* This is a Unix errno+100 */
344 if ( !msg )
345 msg = strerror(error - 100);
346 error = EUNDEF;
347 } else {
348 if ( (unsigned)error >= ERR_CNT )
349 error = EUNDEF;
351 if ( !msg )
352 msg = errmsgs[error];
355 tp->th_code = htons((u_short)error);
357 length = strlen(msg)+1;
358 memcpy(tp->th_msg, msg, length);
359 length += 4; /* Add space for header */
361 if (trace)
362 tpacket("sent", tp, length);
363 if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&peeraddr,
364 sizeof(peeraddr)) != length)
365 perror("nak");
368 static void
369 tpacket(const char *s, struct tftphdr *tp, int n)
371 static const char *opcodes[] =
372 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
373 char *cp, *file;
374 u_short op = ntohs((u_short)tp->th_opcode);
376 if (op < RRQ || op > ERROR)
377 printf("%s opcode=%x ", s, op);
378 else
379 printf("%s %s ", s, opcodes[op]);
380 switch (op) {
382 case RRQ:
383 case WRQ:
384 n -= 2;
385 file = cp = (char *) &(tp->th_stuff);
386 cp = strchr(cp, '\0');
387 printf("<file=%s, mode=%s>\n", file, cp + 1);
388 break;
390 case DATA:
391 printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
392 break;
394 case ACK:
395 printf("<block=%d>\n", ntohs(tp->th_block));
396 break;
398 case ERROR:
399 printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
400 break;
404 struct timeval tstart;
405 struct timeval tstop;
407 static void
408 startclock(void)
410 (void)gettimeofday(&tstart, NULL);
413 static void
414 stopclock(void)
417 (void)gettimeofday(&tstop, NULL);
420 static void
421 printstats(const char *direction, unsigned long amount)
423 double delta;
424 /* compute delta in 1/10's second units */
425 delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
426 ((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
427 delta = delta/10.; /* back to seconds */
428 printf("%s %lu bytes in %.1f seconds", direction, amount, delta);
429 if (verbose)
430 printf(" [%.0f bits/sec]", (amount*8.)/delta);
431 putchar('\n');
434 static void
435 timer(int sig)
437 int save_errno = errno;
439 (void)sig; /* Shut up unused warning */
441 timeout += rexmtval;
442 if (timeout >= maxtimeout) {
443 printf("Transfer timed out.\n");
444 errno = save_errno;
445 siglongjmp(toplevel, -1);
447 errno = save_errno;
448 siglongjmp(timeoutbuf, 1);