dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / tftp / tftp.c
blob9aa18222ae2696529939a56070392e6bb89e0229
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
32 * All Rights Reserved
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
36 * contributors.
39 #pragma ident "%Z%%M% %I% %E% SMI"
42 * TFTP User Program -- Protocol Machines
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <sys/param.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <stddef.h>
57 #include <inttypes.h>
59 #include "tftpcommon.h"
60 #include "tftpprivate.h"
62 static char *blksize_str(void);
63 static char *timeout_str(void);
64 static char *tsize_str(void);
65 static int blksize_handler(char *);
66 static int timeout_handler(char *);
67 static int tsize_handler(char *);
68 static int add_options(char *, char *);
69 static int process_oack(tftpbuf *, int);
70 static void nak(int);
71 static void startclock(void);
72 static void stopclock(void);
73 static void printstats(char *, off_t);
74 static int makerequest(int, char *, struct tftphdr *, char *);
75 static void tpacket(char *, struct tftphdr *, int);
77 static struct options {
78 char *opt_name;
79 char *(*opt_str)(void);
80 int (*opt_handler)(char *);
81 } options[] = {
82 { "blksize", blksize_str, blksize_handler },
83 { "timeout", timeout_str, timeout_handler },
84 { "tsize", tsize_str, tsize_handler },
85 { NULL }
88 static char optbuf[MAX_OPTVAL_LEN];
89 static boolean_t tsize_set;
91 static tftpbuf ackbuf;
92 static int timeout;
93 static off_t tsize;
94 static jmp_buf timeoutbuf;
96 int blocksize = SEGSIZE; /* Number of data bytes in a DATA packet */
98 /*ARGSUSED*/
99 static void
100 timer(int signum)
102 timeout += rexmtval;
103 if (timeout >= maxtimeout) {
104 (void) fputs("Transfer timed out.\n", stderr);
105 longjmp(toplevel, -1);
107 (void) signal(SIGALRM, timer);
108 longjmp(timeoutbuf, 1);
112 * Send the requested file.
114 void
115 tftp_sendfile(int fd, char *name, char *mode)
117 struct tftphdr *ap; /* data and ack packets */
118 struct tftphdr *dp;
119 int count = 0, size, n;
120 ushort_t block = 0;
121 off_t amount = 0;
122 struct sockaddr_in6 from;
123 socklen_t fromlen;
124 int convert; /* true if doing nl->crlf conversion */
125 FILE *file;
126 struct stat statb;
127 int errcode;
129 startclock(); /* start stat's clock */
130 dp = r_init(); /* reset fillbuf/read-ahead code */
131 ap = &ackbuf.tb_hdr;
132 file = fdopen(fd, "r");
133 convert = (strcmp(mode, "netascii") == 0);
135 tsize_set = ((tsize_opt != 0) && !convert && (fstat(fd, &statb) == 0));
136 if (tsize_set)
137 tsize = statb.st_size;
139 do {
140 (void) signal(SIGALRM, timer);
141 if (count == 0) {
142 if ((size = makerequest(WRQ, name, dp, mode)) == -1) {
143 (void) fprintf(stderr,
144 "tftp: Error: Write request packet too "
145 "big\n");
146 (void) fclose(file);
147 return;
149 size -= 4;
150 } else {
151 size = readit(file, &dp, convert);
152 if (size < 0) {
153 nak(errno + 100);
154 break;
156 dp->th_opcode = htons((ushort_t)DATA);
157 dp->th_block = htons((ushort_t)block);
159 timeout = 0;
160 (void) setjmp(timeoutbuf);
161 if (trace)
162 tpacket("sent", dp, size + 4);
163 n = sendto(f, dp, size + 4, 0,
164 (struct sockaddr *)&sin6, sizeof (sin6));
165 if (n != size + 4) {
166 perror("tftp: sendto");
167 goto abort;
169 /* Can't read-ahead first block as OACK may change blocksize */
170 if (count != 0)
171 read_ahead(file, convert);
172 (void) alarm(rexmtval);
173 for (; ; ) {
174 (void) sigrelse(SIGALRM);
175 do {
176 fromlen = (socklen_t)sizeof (from);
177 n = recvfrom(f, ackbuf.tb_data,
178 sizeof (ackbuf.tb_data), 0,
179 (struct sockaddr *)&from, &fromlen);
180 if (n < 0) {
181 perror("tftp: recvfrom");
182 goto abort;
184 } while (n < offsetof(struct tftphdr, th_data));
185 (void) sighold(SIGALRM);
186 sin6.sin6_port = from.sin6_port; /* added */
187 if (trace)
188 tpacket("received", ap, n);
189 /* should verify packet came from server */
190 ap->th_opcode = ntohs(ap->th_opcode);
191 if (ap->th_opcode == ERROR) {
192 ap->th_code = ntohs(ap->th_code);
193 (void) fprintf(stderr,
194 "Error code %d", ap->th_code);
195 if (n > offsetof(struct tftphdr, th_data))
196 (void) fprintf(stderr, ": %.*s", n -
197 offsetof(struct tftphdr, th_data),
198 ap->th_msg);
199 (void) fputc('\n', stderr);
200 goto abort;
202 if ((count == 0) && (ap->th_opcode == OACK)) {
203 errcode = process_oack(&ackbuf, n);
204 if (errcode >= 0) {
205 nak(errcode);
206 (void) fputs("Rejected OACK\n",
207 stderr);
208 goto abort;
210 break;
212 if (ap->th_opcode == ACK) {
213 ap->th_block = ntohs(ap->th_block);
214 if (ap->th_block == block) {
215 break;
218 * Never resend the current DATA packet on
219 * receipt of a duplicate ACK, doing so would
220 * cause the "Sorcerer's Apprentice Syndrome".
224 cancel_alarm();
225 if (count > 0)
226 amount += size;
227 block++;
228 count++;
229 } while (size == blocksize || count == 1);
230 abort:
231 cancel_alarm();
232 (void) fclose(file);
233 stopclock();
234 if (amount > 0)
235 printstats("Sent", amount);
239 * Receive a file.
241 void
242 tftp_recvfile(int fd, char *name, char *mode)
244 struct tftphdr *ap;
245 struct tftphdr *dp;
246 ushort_t block = 1;
247 int n, size;
248 unsigned long amount = 0;
249 struct sockaddr_in6 from;
250 socklen_t fromlen;
251 boolean_t firsttrip = B_TRUE;
252 FILE *file;
253 int convert; /* true if converting crlf -> lf */
254 int errcode;
256 startclock();
257 dp = w_init();
258 ap = &ackbuf.tb_hdr;
259 file = fdopen(fd, "w");
260 convert = (strcmp(mode, "netascii") == 0);
262 tsize_set = (tsize_opt != 0);
263 if (tsize_set)
264 tsize = 0;
266 if ((size = makerequest(RRQ, name, ap, mode)) == -1) {
267 (void) fprintf(stderr,
268 "tftp: Error: Read request packet too big\n");
269 (void) fclose(file);
270 return;
273 do {
274 (void) signal(SIGALRM, timer);
275 if (firsttrip) {
276 firsttrip = B_FALSE;
277 } else {
278 ap->th_opcode = htons((ushort_t)ACK);
279 ap->th_block = htons((ushort_t)(block));
280 size = 4;
281 block++;
284 send_oack_ack:
285 timeout = 0;
286 (void) setjmp(timeoutbuf);
287 send_ack:
288 if (trace)
289 tpacket("sent", ap, size);
290 if (sendto(f, ackbuf.tb_data, size, 0, (struct sockaddr *)&sin6,
291 sizeof (sin6)) != size) {
292 (void) alarm(0);
293 perror("tftp: sendto");
294 goto abort;
296 if (write_behind(file, convert) < 0) {
297 nak(errno + 100);
298 goto abort;
300 (void) alarm(rexmtval);
301 for (; ; ) {
302 (void) sigrelse(SIGALRM);
303 do {
304 fromlen = (socklen_t)sizeof (from);
305 n = recvfrom(f, dp, blocksize + 4, 0,
306 (struct sockaddr *)&from, &fromlen);
307 if (n < 0) {
308 perror("tftp: recvfrom");
309 goto abort;
311 } while (n < offsetof(struct tftphdr, th_data));
312 (void) sighold(SIGALRM);
313 sin6.sin6_port = from.sin6_port; /* added */
314 if (trace)
315 tpacket("received", dp, n);
316 /* should verify client address */
317 dp->th_opcode = ntohs(dp->th_opcode);
318 if (dp->th_opcode == ERROR) {
319 dp->th_code = ntohs(dp->th_code);
320 (void) fprintf(stderr, "Error code %d",
321 dp->th_code);
322 if (n > offsetof(struct tftphdr, th_data))
323 (void) fprintf(stderr, ": %.*s", n -
324 offsetof(struct tftphdr, th_data),
325 dp->th_msg);
326 (void) fputc('\n', stderr);
327 goto abort;
329 if ((block == 1) && (dp->th_opcode == OACK)) {
330 errcode = process_oack((tftpbuf *)dp, n);
331 if (errcode >= 0) {
332 cancel_alarm();
333 nak(errcode);
334 (void) fputs("Rejected OACK\n",
335 stderr);
336 (void) fclose(file);
337 return;
339 ap->th_opcode = htons((ushort_t)ACK);
340 ap->th_block = htons(0);
341 size = 4;
342 goto send_oack_ack;
344 if (dp->th_opcode == DATA) {
345 int j;
347 dp->th_block = ntohs(dp->th_block);
348 if (dp->th_block == block) {
349 break; /* have next packet */
352 * On an error, try to synchronize
353 * both sides.
355 j = synchnet(f);
356 if (j < 0) {
357 perror("tftp: recvfrom");
358 goto abort;
360 if ((j > 0) && trace) {
361 (void) printf("discarded %d packets\n",
364 if (dp->th_block == (block-1)) {
365 goto send_ack; /* resend ack */
369 cancel_alarm();
370 size = writeit(file, &dp, n - 4, convert);
371 if (size < 0) {
372 nak(errno + 100);
373 goto abort;
375 amount += size;
376 } while (size == blocksize);
378 cancel_alarm();
379 if (write_behind(file, convert) < 0) { /* flush last buffer */
380 nak(errno + 100);
381 goto abort;
383 n = fclose(file);
384 file = NULL;
385 if (n == EOF) {
386 nak(errno + 100);
387 goto abort;
390 /* ok to ack, since user has seen err msg */
391 ap->th_opcode = htons((ushort_t)ACK);
392 ap->th_block = htons((ushort_t)block);
393 if (trace)
394 tpacket("sent", ap, 4);
395 if (sendto(f, ackbuf.tb_data, 4, 0,
396 (struct sockaddr *)&sin6, sizeof (sin6)) != 4)
397 perror("tftp: sendto");
399 abort:
400 cancel_alarm();
401 if (file != NULL)
402 (void) fclose(file);
403 stopclock();
404 if (amount > 0)
405 printstats("Received", amount);
408 static int
409 makerequest(int request, char *name, struct tftphdr *tp, char *mode)
411 char *cp, *cpend;
412 int len;
414 tp->th_opcode = htons((ushort_t)request);
415 cp = (char *)&tp->th_stuff;
417 /* Maximum size of a request packet is 512 bytes (RFC 2347) */
418 cpend = (char *)tp + SEGSIZE;
420 len = strlcpy(cp, name, cpend - cp) + 1;
421 cp += len;
422 if (cp > cpend)
423 return (-1);
425 len = strlcpy(cp, mode, cpend - cp) + 1;
426 cp += len;
427 if (cp > cpend)
428 return (-1);
430 len = add_options(cp, cpend);
431 if (len == -1)
432 return (-1);
433 cp += len;
435 return (cp - (char *)tp);
439 * Return the blksize option value string to include in the request packet.
441 static char *
442 blksize_str(void)
444 blocksize = SEGSIZE;
445 if (blksize == 0)
446 return (NULL);
448 (void) snprintf(optbuf, sizeof (optbuf), "%d", blksize);
449 return (optbuf);
453 * Return the timeout option value string to include in the request packet.
455 static char *
456 timeout_str(void)
458 if (srexmtval == 0)
459 return (NULL);
461 (void) snprintf(optbuf, sizeof (optbuf), "%d", srexmtval);
462 return (optbuf);
466 * Return the tsize option value string to include in the request packet.
468 static char *
469 tsize_str(void)
471 if (tsize_set == B_FALSE)
472 return (NULL);
474 (void) snprintf(optbuf, sizeof (optbuf), OFF_T_FMT, tsize);
475 return (optbuf);
479 * Validate and action the blksize option value string from the OACK packet.
480 * Returns -1 on success or an error code on failure.
482 static int
483 blksize_handler(char *optstr)
485 char *endp;
486 int value;
488 /* Make sure the option was requested */
489 if (blksize == 0)
490 return (EOPTNEG);
491 errno = 0;
492 value = (int)strtol(optstr, &endp, 10);
493 if (errno != 0 || value < MIN_BLKSIZE || value > blksize ||
494 *endp != '\0')
495 return (EOPTNEG);
496 blocksize = value;
497 return (-1);
501 * Validate and action the timeout option value string from the OACK packet.
502 * Returns -1 on success or an error code on failure.
504 static int
505 timeout_handler(char *optstr)
507 char *endp;
508 int value;
510 /* Make sure the option was requested */
511 if (srexmtval == 0)
512 return (EOPTNEG);
513 errno = 0;
514 value = (int)strtol(optstr, &endp, 10);
515 if (errno != 0 || value != srexmtval || *endp != '\0')
516 return (EOPTNEG);
518 * Nothing to set, client and server retransmission intervals are
519 * set separately in the client.
521 return (-1);
525 * Validate and action the tsize option value string from the OACK packet.
526 * Returns -1 on success or an error code on failure.
528 static int
529 tsize_handler(char *optstr)
531 char *endp;
532 longlong_t value;
534 /* Make sure the option was requested */
535 if (tsize_set == B_FALSE)
536 return (EOPTNEG);
537 errno = 0;
538 value = strtoll(optstr, &endp, 10);
539 if (errno != 0 || value < 0 || *endp != '\0')
540 return (EOPTNEG);
541 #if _FILE_OFFSET_BITS == 32
542 if (value > MAXOFF_T)
543 return (ENOSPACE);
544 #endif
546 * Don't bother checking the tsize value we specified in a write
547 * request is echoed back in the OACK.
549 if (tsize == 0)
550 tsize = value;
551 return (-1);
555 * Add TFTP options to a request packet.
557 static int
558 add_options(char *obuf, char *obufend)
560 int i;
561 char *cp, *ostr;
563 cp = obuf;
564 for (i = 0; options[i].opt_name != NULL; i++) {
565 ostr = options[i].opt_str();
566 if (ostr != NULL) {
567 cp += strlcpy(cp, options[i].opt_name, obufend - cp)
568 + 1;
569 if (cp > obufend)
570 return (-1);
572 cp += strlcpy(cp, ostr, obufend - cp) + 1;
573 if (cp > obufend)
574 return (-1);
577 return (cp - obuf);
581 * Process OACK packet sent by server in response to options in the request
582 * packet. Returns -1 on success or an error code on failure.
584 static int
585 process_oack(tftpbuf *oackbuf, int n)
587 char *cp, *oackend, *optname, *optval;
588 struct tftphdr *oackp;
589 int i, errcode;
591 oackp = &oackbuf->tb_hdr;
592 cp = (char *)&oackp->th_stuff;
593 oackend = (char *)oackbuf + n;
595 while (cp < oackend) {
596 optname = cp;
597 if ((optval = next_field(optname, oackend)) == NULL)
598 return (EOPTNEG);
599 if ((cp = next_field(optval, oackend)) == NULL)
600 return (EOPTNEG);
601 for (i = 0; options[i].opt_name != NULL; i++) {
602 if (strcasecmp(optname, options[i].opt_name) == 0)
603 break;
605 if (options[i].opt_name == NULL)
606 return (EOPTNEG);
607 errcode = options[i].opt_handler(optval);
608 if (errcode >= 0)
609 return (errcode);
611 return (-1);
615 * Send a nak packet (error message).
616 * Error code passed in is one of the
617 * standard TFTP codes, or a UNIX errno
618 * offset by 100.
620 static void
621 nak(int error)
623 struct tftphdr *tp;
624 int length;
625 struct errmsg *pe;
627 tp = &ackbuf.tb_hdr;
628 tp->th_opcode = htons((ushort_t)ERROR);
629 tp->th_code = htons((ushort_t)error);
630 for (pe = errmsgs; pe->e_code >= 0; pe++)
631 if (pe->e_code == error)
632 break;
633 if (pe->e_code < 0) {
634 pe->e_msg = strerror(error - 100);
635 tp->th_code = EUNDEF;
637 (void) strlcpy(tp->th_msg, pe->e_msg,
638 sizeof (ackbuf) - sizeof (struct tftphdr));
639 length = strlen(pe->e_msg) + 4;
640 if (trace)
641 tpacket("sent", tp, length);
642 if (sendto(f, ackbuf.tb_data, length, 0,
643 (struct sockaddr *)&sin6, sizeof (sin6)) != length)
644 perror("nak");
647 static void
648 tpacket(char *s, struct tftphdr *tp, int n)
650 static char *opcodes[] = \
651 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
652 char *cp, *file, *mode;
653 ushort_t op = ntohs(tp->th_opcode);
654 char *tpend;
656 if (op < RRQ || op > OACK)
657 (void) printf("%s opcode=%x ", s, op);
658 else
659 (void) printf("%s %s ", s, opcodes[op]);
661 switch (op) {
662 case RRQ:
663 case WRQ:
664 tpend = (char *)tp + n;
665 n -= sizeof (tp->th_opcode);
666 file = (char *)&tp->th_stuff;
667 if ((mode = next_field(file, tpend)) == NULL) {
668 (void) printf("<file=%.*s>\n", n, file);
669 break;
671 n -= mode - file;
672 if ((cp = next_field(mode, tpend)) == NULL) {
673 (void) printf("<file=%s, mode=%.*s>\n", file, n, mode);
674 break;
676 (void) printf("<file=%s, mode=%s", file, mode);
677 n -= cp - mode;
678 if (n > 0) {
679 (void) printf(", options: ");
680 print_options(stdout, cp, n);
682 (void) puts(">");
683 break;
685 case DATA:
686 (void) printf("<block=%d, %d bytes>\n", ntohs(tp->th_block),
687 n - sizeof (tp->th_opcode) - sizeof (tp->th_block));
688 break;
690 case ACK:
691 (void) printf("<block=%d>\n", ntohs(tp->th_block));
692 break;
694 case OACK:
695 (void) printf("<options: ");
696 print_options(stdout, (char *)&tp->th_stuff,
697 n - sizeof (tp->th_opcode));
698 (void) puts(">");
699 break;
701 case ERROR:
702 (void) printf("<code=%d", ntohs(tp->th_code));
703 n = n - sizeof (tp->th_opcode) - sizeof (tp->th_code);
704 if (n > 0)
705 (void) printf(", msg=%.*s", n, tp->th_msg);
706 (void) puts(">");
707 break;
711 static hrtime_t tstart, tstop;
713 static void
714 startclock(void)
716 tstart = gethrtime();
719 static void
720 stopclock(void)
722 tstop = gethrtime();
725 static void
726 printstats(char *direction, off_t amount)
728 hrtime_t delta, tenths;
730 delta = tstop - tstart;
731 tenths = delta / (NANOSEC / 10);
732 (void) printf("%s " OFF_T_FMT " bytes in %" PRId64 ".%" PRId64
733 " seconds", direction, amount, tenths / 10, tenths % 10);
734 if (verbose)
735 (void) printf(" [%" PRId64 " bits/sec]\n",
736 ((hrtime_t)amount * 8 * NANOSEC) / delta);
737 else
738 (void) putchar('\n');