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