tftp.c revision 1.1 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.1 cgd static char sccsid[] = "@(#)tftp.c 5.10 (Berkeley) 3/1/91";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
39 1.1 cgd
40 1.1 cgd /*
41 1.1 cgd * TFTP User Program -- Protocol Machines
42 1.1 cgd */
43 1.1 cgd #include <sys/types.h>
44 1.1 cgd #include <sys/socket.h>
45 1.1 cgd #include <sys/time.h>
46 1.1 cgd
47 1.1 cgd #include <netinet/in.h>
48 1.1 cgd
49 1.1 cgd #include <arpa/tftp.h>
50 1.1 cgd
51 1.1 cgd #include <signal.h>
52 1.1 cgd #include <stdio.h>
53 1.1 cgd #include <errno.h>
54 1.1 cgd #include <setjmp.h>
55 1.1 cgd
56 1.1 cgd extern int errno;
57 1.1 cgd
58 1.1 cgd extern struct sockaddr_in sin; /* filled in by main */
59 1.1 cgd extern int f; /* the opened socket */
60 1.1 cgd extern int trace;
61 1.1 cgd extern int verbose;
62 1.1 cgd extern int rexmtval;
63 1.1 cgd extern int maxtimeout;
64 1.1 cgd
65 1.1 cgd #define PKTSIZE SEGSIZE+4
66 1.1 cgd char ackbuf[PKTSIZE];
67 1.1 cgd int timeout;
68 1.1 cgd jmp_buf toplevel;
69 1.1 cgd jmp_buf timeoutbuf;
70 1.1 cgd
71 1.1 cgd void
72 1.1 cgd timer()
73 1.1 cgd {
74 1.1 cgd timeout += rexmtval;
75 1.1 cgd if (timeout >= maxtimeout) {
76 1.1 cgd printf("Transfer timed out.\n");
77 1.1 cgd longjmp(toplevel, -1);
78 1.1 cgd }
79 1.1 cgd longjmp(timeoutbuf, 1);
80 1.1 cgd }
81 1.1 cgd
82 1.1 cgd /*
83 1.1 cgd * Send the requested file.
84 1.1 cgd */
85 1.1 cgd sendfile(fd, name, mode)
86 1.1 cgd int fd;
87 1.1 cgd char *name;
88 1.1 cgd char *mode;
89 1.1 cgd {
90 1.1 cgd register struct tftphdr *ap; /* data and ack packets */
91 1.1 cgd struct tftphdr *r_init(), *dp;
92 1.1 cgd register int block = 0, size, n;
93 1.1 cgd register unsigned long amount = 0;
94 1.1 cgd struct sockaddr_in from;
95 1.1 cgd int fromlen;
96 1.1 cgd int convert; /* true if doing nl->crlf conversion */
97 1.1 cgd FILE *file;
98 1.1 cgd
99 1.1 cgd startclock(); /* start stat's clock */
100 1.1 cgd dp = r_init(); /* reset fillbuf/read-ahead code */
101 1.1 cgd ap = (struct tftphdr *)ackbuf;
102 1.1 cgd file = fdopen(fd, "r");
103 1.1 cgd convert = !strcmp(mode, "netascii");
104 1.1 cgd
105 1.1 cgd signal(SIGALRM, timer);
106 1.1 cgd do {
107 1.1 cgd if (block == 0)
108 1.1 cgd size = makerequest(WRQ, name, dp, mode) - 4;
109 1.1 cgd else {
110 1.1 cgd /* size = read(fd, dp->th_data, SEGSIZE); */
111 1.1 cgd size = readit(file, &dp, convert);
112 1.1 cgd if (size < 0) {
113 1.1 cgd nak(errno + 100);
114 1.1 cgd break;
115 1.1 cgd }
116 1.1 cgd dp->th_opcode = htons((u_short)DATA);
117 1.1 cgd dp->th_block = htons((u_short)block);
118 1.1 cgd }
119 1.1 cgd timeout = 0;
120 1.1 cgd (void) setjmp(timeoutbuf);
121 1.1 cgd send_data:
122 1.1 cgd if (trace)
123 1.1 cgd tpacket("sent", dp, size + 4);
124 1.1 cgd n = sendto(f, dp, size + 4, 0,
125 1.1 cgd (struct sockaddr *)&sin, sizeof (sin));
126 1.1 cgd if (n != size + 4) {
127 1.1 cgd perror("tftp: sendto");
128 1.1 cgd goto abort;
129 1.1 cgd }
130 1.1 cgd read_ahead(file, convert);
131 1.1 cgd for ( ; ; ) {
132 1.1 cgd alarm(rexmtval);
133 1.1 cgd do {
134 1.1 cgd fromlen = sizeof (from);
135 1.1 cgd n = recvfrom(f, ackbuf, sizeof (ackbuf), 0,
136 1.1 cgd (struct sockaddr *)&from, &fromlen);
137 1.1 cgd } while (n <= 0);
138 1.1 cgd alarm(0);
139 1.1 cgd if (n < 0) {
140 1.1 cgd perror("tftp: recvfrom");
141 1.1 cgd goto abort;
142 1.1 cgd }
143 1.1 cgd sin.sin_port = from.sin_port; /* added */
144 1.1 cgd if (trace)
145 1.1 cgd tpacket("received", ap, n);
146 1.1 cgd /* should verify packet came from server */
147 1.1 cgd ap->th_opcode = ntohs(ap->th_opcode);
148 1.1 cgd ap->th_block = ntohs(ap->th_block);
149 1.1 cgd if (ap->th_opcode == ERROR) {
150 1.1 cgd printf("Error code %d: %s\n", ap->th_code,
151 1.1 cgd ap->th_msg);
152 1.1 cgd goto abort;
153 1.1 cgd }
154 1.1 cgd if (ap->th_opcode == ACK) {
155 1.1 cgd int j;
156 1.1 cgd
157 1.1 cgd if (ap->th_block == block) {
158 1.1 cgd break;
159 1.1 cgd }
160 1.1 cgd /* On an error, try to synchronize
161 1.1 cgd * both sides.
162 1.1 cgd */
163 1.1 cgd j = synchnet(f);
164 1.1 cgd if (j && trace) {
165 1.1 cgd printf("discarded %d packets\n",
166 1.1 cgd j);
167 1.1 cgd }
168 1.1 cgd if (ap->th_block == (block-1)) {
169 1.1 cgd goto send_data;
170 1.1 cgd }
171 1.1 cgd }
172 1.1 cgd }
173 1.1 cgd if (block > 0)
174 1.1 cgd amount += size;
175 1.1 cgd block++;
176 1.1 cgd } while (size == SEGSIZE || block == 1);
177 1.1 cgd abort:
178 1.1 cgd fclose(file);
179 1.1 cgd stopclock();
180 1.1 cgd if (amount > 0)
181 1.1 cgd printstats("Sent", amount);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd /*
185 1.1 cgd * Receive a file.
186 1.1 cgd */
187 1.1 cgd recvfile(fd, name, mode)
188 1.1 cgd int fd;
189 1.1 cgd char *name;
190 1.1 cgd char *mode;
191 1.1 cgd {
192 1.1 cgd register struct tftphdr *ap;
193 1.1 cgd struct tftphdr *dp, *w_init();
194 1.1 cgd register int block = 1, n, size;
195 1.1 cgd unsigned long amount = 0;
196 1.1 cgd struct sockaddr_in from;
197 1.1 cgd int fromlen, firsttrip = 1;
198 1.1 cgd FILE *file;
199 1.1 cgd int convert; /* true if converting crlf -> lf */
200 1.1 cgd
201 1.1 cgd startclock();
202 1.1 cgd dp = w_init();
203 1.1 cgd ap = (struct tftphdr *)ackbuf;
204 1.1 cgd file = fdopen(fd, "w");
205 1.1 cgd convert = !strcmp(mode, "netascii");
206 1.1 cgd
207 1.1 cgd signal(SIGALRM, timer);
208 1.1 cgd do {
209 1.1 cgd if (firsttrip) {
210 1.1 cgd size = makerequest(RRQ, name, ap, mode);
211 1.1 cgd firsttrip = 0;
212 1.1 cgd } else {
213 1.1 cgd ap->th_opcode = htons((u_short)ACK);
214 1.1 cgd ap->th_block = htons((u_short)(block));
215 1.1 cgd size = 4;
216 1.1 cgd block++;
217 1.1 cgd }
218 1.1 cgd timeout = 0;
219 1.1 cgd (void) setjmp(timeoutbuf);
220 1.1 cgd send_ack:
221 1.1 cgd if (trace)
222 1.1 cgd tpacket("sent", ap, size);
223 1.1 cgd if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&sin,
224 1.1 cgd sizeof (sin)) != size) {
225 1.1 cgd alarm(0);
226 1.1 cgd perror("tftp: sendto");
227 1.1 cgd goto abort;
228 1.1 cgd }
229 1.1 cgd write_behind(file, convert);
230 1.1 cgd for ( ; ; ) {
231 1.1 cgd alarm(rexmtval);
232 1.1 cgd do {
233 1.1 cgd fromlen = sizeof (from);
234 1.1 cgd n = recvfrom(f, dp, PKTSIZE, 0,
235 1.1 cgd (struct sockaddr *)&from, &fromlen);
236 1.1 cgd } while (n <= 0);
237 1.1 cgd alarm(0);
238 1.1 cgd if (n < 0) {
239 1.1 cgd perror("tftp: recvfrom");
240 1.1 cgd goto abort;
241 1.1 cgd }
242 1.1 cgd sin.sin_port = from.sin_port; /* added */
243 1.1 cgd if (trace)
244 1.1 cgd tpacket("received", dp, n);
245 1.1 cgd /* should verify client address */
246 1.1 cgd dp->th_opcode = ntohs(dp->th_opcode);
247 1.1 cgd dp->th_block = ntohs(dp->th_block);
248 1.1 cgd if (dp->th_opcode == ERROR) {
249 1.1 cgd printf("Error code %d: %s\n", dp->th_code,
250 1.1 cgd dp->th_msg);
251 1.1 cgd goto abort;
252 1.1 cgd }
253 1.1 cgd if (dp->th_opcode == DATA) {
254 1.1 cgd int j;
255 1.1 cgd
256 1.1 cgd if (dp->th_block == block) {
257 1.1 cgd break; /* have next packet */
258 1.1 cgd }
259 1.1 cgd /* On an error, try to synchronize
260 1.1 cgd * both sides.
261 1.1 cgd */
262 1.1 cgd j = synchnet(f);
263 1.1 cgd if (j && trace) {
264 1.1 cgd printf("discarded %d packets\n", j);
265 1.1 cgd }
266 1.1 cgd if (dp->th_block == (block-1)) {
267 1.1 cgd goto send_ack; /* resend ack */
268 1.1 cgd }
269 1.1 cgd }
270 1.1 cgd }
271 1.1 cgd /* size = write(fd, dp->th_data, n - 4); */
272 1.1 cgd size = writeit(file, &dp, n - 4, convert);
273 1.1 cgd if (size < 0) {
274 1.1 cgd nak(errno + 100);
275 1.1 cgd break;
276 1.1 cgd }
277 1.1 cgd amount += size;
278 1.1 cgd } while (size == SEGSIZE);
279 1.1 cgd abort: /* ok to ack, since user */
280 1.1 cgd ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
281 1.1 cgd ap->th_block = htons((u_short)block);
282 1.1 cgd (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&sin, sizeof (sin));
283 1.1 cgd write_behind(file, convert); /* flush last buffer */
284 1.1 cgd fclose(file);
285 1.1 cgd stopclock();
286 1.1 cgd if (amount > 0)
287 1.1 cgd printstats("Received", amount);
288 1.1 cgd }
289 1.1 cgd
290 1.1 cgd makerequest(request, name, tp, mode)
291 1.1 cgd int request;
292 1.1 cgd char *name, *mode;
293 1.1 cgd struct tftphdr *tp;
294 1.1 cgd {
295 1.1 cgd register char *cp;
296 1.1 cgd
297 1.1 cgd tp->th_opcode = htons((u_short)request);
298 1.1 cgd cp = tp->th_stuff;
299 1.1 cgd strcpy(cp, name);
300 1.1 cgd cp += strlen(name);
301 1.1 cgd *cp++ = '\0';
302 1.1 cgd strcpy(cp, mode);
303 1.1 cgd cp += strlen(mode);
304 1.1 cgd *cp++ = '\0';
305 1.1 cgd return (cp - (char *)tp);
306 1.1 cgd }
307 1.1 cgd
308 1.1 cgd struct errmsg {
309 1.1 cgd int e_code;
310 1.1 cgd char *e_msg;
311 1.1 cgd } errmsgs[] = {
312 1.1 cgd { EUNDEF, "Undefined error code" },
313 1.1 cgd { ENOTFOUND, "File not found" },
314 1.1 cgd { EACCESS, "Access violation" },
315 1.1 cgd { ENOSPACE, "Disk full or allocation exceeded" },
316 1.1 cgd { EBADOP, "Illegal TFTP operation" },
317 1.1 cgd { EBADID, "Unknown transfer ID" },
318 1.1 cgd { EEXISTS, "File already exists" },
319 1.1 cgd { ENOUSER, "No such user" },
320 1.1 cgd { -1, 0 }
321 1.1 cgd };
322 1.1 cgd
323 1.1 cgd /*
324 1.1 cgd * Send a nak packet (error message).
325 1.1 cgd * Error code passed in is one of the
326 1.1 cgd * standard TFTP codes, or a UNIX errno
327 1.1 cgd * offset by 100.
328 1.1 cgd */
329 1.1 cgd nak(error)
330 1.1 cgd int error;
331 1.1 cgd {
332 1.1 cgd register struct errmsg *pe;
333 1.1 cgd register struct tftphdr *tp;
334 1.1 cgd int length;
335 1.1 cgd char *strerror();
336 1.1 cgd
337 1.1 cgd tp = (struct tftphdr *)ackbuf;
338 1.1 cgd tp->th_opcode = htons((u_short)ERROR);
339 1.1 cgd tp->th_code = htons((u_short)error);
340 1.1 cgd for (pe = errmsgs; pe->e_code >= 0; pe++)
341 1.1 cgd if (pe->e_code == error)
342 1.1 cgd break;
343 1.1 cgd if (pe->e_code < 0) {
344 1.1 cgd pe->e_msg = strerror(error - 100);
345 1.1 cgd tp->th_code = EUNDEF;
346 1.1 cgd }
347 1.1 cgd strcpy(tp->th_msg, pe->e_msg);
348 1.1 cgd length = strlen(pe->e_msg) + 4;
349 1.1 cgd if (trace)
350 1.1 cgd tpacket("sent", tp, length);
351 1.1 cgd if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&sin,
352 1.1 cgd sizeof (sin)) != length)
353 1.1 cgd perror("nak");
354 1.1 cgd }
355 1.1 cgd
356 1.1 cgd tpacket(s, tp, n)
357 1.1 cgd char *s;
358 1.1 cgd struct tftphdr *tp;
359 1.1 cgd int n;
360 1.1 cgd {
361 1.1 cgd static char *opcodes[] =
362 1.1 cgd { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
363 1.1 cgd register char *cp, *file;
364 1.1 cgd u_short op = ntohs(tp->th_opcode);
365 1.1 cgd char *index();
366 1.1 cgd
367 1.1 cgd if (op < RRQ || op > ERROR)
368 1.1 cgd printf("%s opcode=%x ", s, op);
369 1.1 cgd else
370 1.1 cgd printf("%s %s ", s, opcodes[op]);
371 1.1 cgd switch (op) {
372 1.1 cgd
373 1.1 cgd case RRQ:
374 1.1 cgd case WRQ:
375 1.1 cgd n -= 2;
376 1.1 cgd file = cp = tp->th_stuff;
377 1.1 cgd cp = index(cp, '\0');
378 1.1 cgd printf("<file=%s, mode=%s>\n", file, cp + 1);
379 1.1 cgd break;
380 1.1 cgd
381 1.1 cgd case DATA:
382 1.1 cgd printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
383 1.1 cgd break;
384 1.1 cgd
385 1.1 cgd case ACK:
386 1.1 cgd printf("<block=%d>\n", ntohs(tp->th_block));
387 1.1 cgd break;
388 1.1 cgd
389 1.1 cgd case ERROR:
390 1.1 cgd printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
391 1.1 cgd break;
392 1.1 cgd }
393 1.1 cgd }
394 1.1 cgd
395 1.1 cgd struct timeval tstart;
396 1.1 cgd struct timeval tstop;
397 1.1 cgd struct timezone zone;
398 1.1 cgd
399 1.1 cgd startclock() {
400 1.1 cgd gettimeofday(&tstart, &zone);
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd stopclock() {
404 1.1 cgd gettimeofday(&tstop, &zone);
405 1.1 cgd }
406 1.1 cgd
407 1.1 cgd printstats(direction, amount)
408 1.1 cgd char *direction;
409 1.1 cgd unsigned long amount;
410 1.1 cgd {
411 1.1 cgd double delta;
412 1.1 cgd /* compute delta in 1/10's second units */
413 1.1 cgd delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
414 1.1 cgd ((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
415 1.1 cgd delta = delta/10.; /* back to seconds */
416 1.1 cgd printf("%s %d bytes in %.1f seconds", direction, amount, delta);
417 1.1 cgd if (verbose)
418 1.1 cgd printf(" [%.0f bits/sec]", (amount*8.)/delta);
419 1.1 cgd putchar('\n');
420 1.1 cgd }
421 1.1 cgd
422