17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5294f5787Sas * Common Development and Distribution License (the "License").
6294f5787Sas * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22294f5787Sas * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27*cb76cc66SToomas Soome /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate * The Regents of the University of California
327c478bd9Sstevel@tonic-gate * All Rights Reserved
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate * contributors.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate * TFTP User Program -- Protocol Machines
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/socket.h>
447c478bd9Sstevel@tonic-gate #include <sys/time.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
50*cb76cc66SToomas Soome #include <stdbool.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate #include <errno.h>
537c478bd9Sstevel@tonic-gate #include <string.h>
547c478bd9Sstevel@tonic-gate #include <stddef.h>
557c478bd9Sstevel@tonic-gate #include <inttypes.h>
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #include "tftpcommon.h"
587c478bd9Sstevel@tonic-gate #include "tftpprivate.h"
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate static char *blksize_str(void);
617c478bd9Sstevel@tonic-gate static char *timeout_str(void);
627c478bd9Sstevel@tonic-gate static char *tsize_str(void);
637c478bd9Sstevel@tonic-gate static int blksize_handler(char *);
647c478bd9Sstevel@tonic-gate static int timeout_handler(char *);
657c478bd9Sstevel@tonic-gate static int tsize_handler(char *);
667c478bd9Sstevel@tonic-gate static int add_options(char *, char *);
677c478bd9Sstevel@tonic-gate static int process_oack(tftpbuf *, int);
687c478bd9Sstevel@tonic-gate static void nak(int);
697c478bd9Sstevel@tonic-gate static void startclock(void);
707c478bd9Sstevel@tonic-gate static void stopclock(void);
717c478bd9Sstevel@tonic-gate static void printstats(char *, off_t);
727c478bd9Sstevel@tonic-gate static int makerequest(int, char *, struct tftphdr *, char *);
737c478bd9Sstevel@tonic-gate static void tpacket(char *, struct tftphdr *, int);
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate static struct options {
767c478bd9Sstevel@tonic-gate char *opt_name;
777c478bd9Sstevel@tonic-gate char *(*opt_str)(void);
787c478bd9Sstevel@tonic-gate int (*opt_handler)(char *);
797c478bd9Sstevel@tonic-gate } options[] = {
807c478bd9Sstevel@tonic-gate { "blksize", blksize_str, blksize_handler },
817c478bd9Sstevel@tonic-gate { "timeout", timeout_str, timeout_handler },
827c478bd9Sstevel@tonic-gate { "tsize", tsize_str, tsize_handler },
837c478bd9Sstevel@tonic-gate { NULL }
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate
86*cb76cc66SToomas Soome static char optbuf[MAX_OPTVAL_LEN];
87*cb76cc66SToomas Soome static bool tsize_set;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate static tftpbuf ackbuf;
907c478bd9Sstevel@tonic-gate static int timeout;
917c478bd9Sstevel@tonic-gate static off_t tsize;
927c478bd9Sstevel@tonic-gate static jmp_buf timeoutbuf;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate int blocksize = SEGSIZE; /* Number of data bytes in a DATA packet */
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
977c478bd9Sstevel@tonic-gate static void
timer(int signum)987c478bd9Sstevel@tonic-gate timer(int signum)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate timeout += rexmtval;
1017c478bd9Sstevel@tonic-gate if (timeout >= maxtimeout) {
1027c478bd9Sstevel@tonic-gate (void) fputs("Transfer timed out.\n", stderr);
1037c478bd9Sstevel@tonic-gate longjmp(toplevel, -1);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, timer);
1067c478bd9Sstevel@tonic-gate longjmp(timeoutbuf, 1);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * Send the requested file.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate void
tftp_sendfile(int fd,char * name,char * mode)1137c478bd9Sstevel@tonic-gate tftp_sendfile(int fd, char *name, char *mode)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate struct tftphdr *ap; /* data and ack packets */
1167c478bd9Sstevel@tonic-gate struct tftphdr *dp;
117*cb76cc66SToomas Soome int n;
118*cb76cc66SToomas Soome volatile int count = 0, size;
119*cb76cc66SToomas Soome volatile ushort_t block = 0;
120*cb76cc66SToomas Soome volatile off_t amount = 0;
1217c478bd9Sstevel@tonic-gate struct sockaddr_in6 from;
1227c478bd9Sstevel@tonic-gate socklen_t fromlen;
1237c478bd9Sstevel@tonic-gate int convert; /* true if doing nl->crlf conversion */
1247c478bd9Sstevel@tonic-gate FILE *file;
1257c478bd9Sstevel@tonic-gate struct stat statb;
1267c478bd9Sstevel@tonic-gate int errcode;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate startclock(); /* start stat's clock */
1297c478bd9Sstevel@tonic-gate dp = r_init(); /* reset fillbuf/read-ahead code */
1307c478bd9Sstevel@tonic-gate ap = &ackbuf.tb_hdr;
1317c478bd9Sstevel@tonic-gate file = fdopen(fd, "r");
1327c478bd9Sstevel@tonic-gate convert = (strcmp(mode, "netascii") == 0);
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate tsize_set = ((tsize_opt != 0) && !convert && (fstat(fd, &statb) == 0));
1357c478bd9Sstevel@tonic-gate if (tsize_set)
1367c478bd9Sstevel@tonic-gate tsize = statb.st_size;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate do {
1397c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, timer);
140294f5787Sas if (count == 0) {
1417c478bd9Sstevel@tonic-gate if ((size = makerequest(WRQ, name, dp, mode)) == -1) {
1427c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1437c478bd9Sstevel@tonic-gate "tftp: Error: Write request packet too "
1447c478bd9Sstevel@tonic-gate "big\n");
1457c478bd9Sstevel@tonic-gate (void) fclose(file);
1467c478bd9Sstevel@tonic-gate return;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate size -= 4;
1497c478bd9Sstevel@tonic-gate } else {
1507c478bd9Sstevel@tonic-gate size = readit(file, &dp, convert);
1517c478bd9Sstevel@tonic-gate if (size < 0) {
1527c478bd9Sstevel@tonic-gate nak(errno + 100);
1537c478bd9Sstevel@tonic-gate break;
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate dp->th_opcode = htons((ushort_t)DATA);
1567c478bd9Sstevel@tonic-gate dp->th_block = htons((ushort_t)block);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate timeout = 0;
1597c478bd9Sstevel@tonic-gate (void) setjmp(timeoutbuf);
1607c478bd9Sstevel@tonic-gate if (trace)
1617c478bd9Sstevel@tonic-gate tpacket("sent", dp, size + 4);
1627c478bd9Sstevel@tonic-gate n = sendto(f, dp, size + 4, 0,
1637c478bd9Sstevel@tonic-gate (struct sockaddr *)&sin6, sizeof (sin6));
1647c478bd9Sstevel@tonic-gate if (n != size + 4) {
1657c478bd9Sstevel@tonic-gate perror("tftp: sendto");
1667c478bd9Sstevel@tonic-gate goto abort;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate /* Can't read-ahead first block as OACK may change blocksize */
169294f5787Sas if (count != 0)
1707c478bd9Sstevel@tonic-gate read_ahead(file, convert);
1717c478bd9Sstevel@tonic-gate (void) alarm(rexmtval);
1727c478bd9Sstevel@tonic-gate for (; ; ) {
1737c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
1747c478bd9Sstevel@tonic-gate do {
1757c478bd9Sstevel@tonic-gate fromlen = (socklen_t)sizeof (from);
1767c478bd9Sstevel@tonic-gate n = recvfrom(f, ackbuf.tb_data,
1777c478bd9Sstevel@tonic-gate sizeof (ackbuf.tb_data), 0,
1787c478bd9Sstevel@tonic-gate (struct sockaddr *)&from, &fromlen);
1797c478bd9Sstevel@tonic-gate if (n < 0) {
1807c478bd9Sstevel@tonic-gate perror("tftp: recvfrom");
1817c478bd9Sstevel@tonic-gate goto abort;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate } while (n < offsetof(struct tftphdr, th_data));
1847c478bd9Sstevel@tonic-gate (void) sighold(SIGALRM);
1857c478bd9Sstevel@tonic-gate sin6.sin6_port = from.sin6_port; /* added */
1867c478bd9Sstevel@tonic-gate if (trace)
1877c478bd9Sstevel@tonic-gate tpacket("received", ap, n);
1887c478bd9Sstevel@tonic-gate /* should verify packet came from server */
1897c478bd9Sstevel@tonic-gate ap->th_opcode = ntohs(ap->th_opcode);
1907c478bd9Sstevel@tonic-gate if (ap->th_opcode == ERROR) {
1917c478bd9Sstevel@tonic-gate ap->th_code = ntohs(ap->th_code);
1927c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
193*cb76cc66SToomas Soome "Error code %d", ap->th_code);
1947c478bd9Sstevel@tonic-gate if (n > offsetof(struct tftphdr, th_data))
1957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %.*s", n -
1967c478bd9Sstevel@tonic-gate offsetof(struct tftphdr, th_data),
1977c478bd9Sstevel@tonic-gate ap->th_msg);
1987c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
1997c478bd9Sstevel@tonic-gate goto abort;
2007c478bd9Sstevel@tonic-gate }
201294f5787Sas if ((count == 0) && (ap->th_opcode == OACK)) {
2027c478bd9Sstevel@tonic-gate errcode = process_oack(&ackbuf, n);
2037c478bd9Sstevel@tonic-gate if (errcode >= 0) {
2047c478bd9Sstevel@tonic-gate nak(errcode);
2057c478bd9Sstevel@tonic-gate (void) fputs("Rejected OACK\n",
2067c478bd9Sstevel@tonic-gate stderr);
2077c478bd9Sstevel@tonic-gate goto abort;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate break;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate if (ap->th_opcode == ACK) {
2127c478bd9Sstevel@tonic-gate ap->th_block = ntohs(ap->th_block);
2137c478bd9Sstevel@tonic-gate if (ap->th_block == block) {
2147c478bd9Sstevel@tonic-gate break;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate * Never resend the current DATA packet on
2187c478bd9Sstevel@tonic-gate * receipt of a duplicate ACK, doing so would
2197c478bd9Sstevel@tonic-gate * cause the "Sorcerer's Apprentice Syndrome".
2207c478bd9Sstevel@tonic-gate */
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate cancel_alarm();
224294f5787Sas if (count > 0)
2257c478bd9Sstevel@tonic-gate amount += size;
2267c478bd9Sstevel@tonic-gate block++;
227294f5787Sas count++;
228294f5787Sas } while (size == blocksize || count == 1);
2297c478bd9Sstevel@tonic-gate abort:
2307c478bd9Sstevel@tonic-gate cancel_alarm();
2317c478bd9Sstevel@tonic-gate (void) fclose(file);
2327c478bd9Sstevel@tonic-gate stopclock();
2337c478bd9Sstevel@tonic-gate if (amount > 0)
2347c478bd9Sstevel@tonic-gate printstats("Sent", amount);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * Receive a file.
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate void
tftp_recvfile(int fd,char * name,char * mode)2417c478bd9Sstevel@tonic-gate tftp_recvfile(int fd, char *name, char *mode)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate struct tftphdr *ap;
2447c478bd9Sstevel@tonic-gate struct tftphdr *dp;
245*cb76cc66SToomas Soome volatile ushort_t block = 1;
246*cb76cc66SToomas Soome int n;
247*cb76cc66SToomas Soome volatile int size;
248*cb76cc66SToomas Soome volatile unsigned long amount = 0;
2497c478bd9Sstevel@tonic-gate struct sockaddr_in6 from;
2507c478bd9Sstevel@tonic-gate socklen_t fromlen;
251*cb76cc66SToomas Soome volatile bool firsttrip = true;
2527c478bd9Sstevel@tonic-gate FILE *file;
2537c478bd9Sstevel@tonic-gate int convert; /* true if converting crlf -> lf */
2547c478bd9Sstevel@tonic-gate int errcode;
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate startclock();
2577c478bd9Sstevel@tonic-gate dp = w_init();
2587c478bd9Sstevel@tonic-gate ap = &ackbuf.tb_hdr;
2597c478bd9Sstevel@tonic-gate file = fdopen(fd, "w");
2607c478bd9Sstevel@tonic-gate convert = (strcmp(mode, "netascii") == 0);
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate tsize_set = (tsize_opt != 0);
2637c478bd9Sstevel@tonic-gate if (tsize_set)
2647c478bd9Sstevel@tonic-gate tsize = 0;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate if ((size = makerequest(RRQ, name, ap, mode)) == -1) {
2677c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2687c478bd9Sstevel@tonic-gate "tftp: Error: Read request packet too big\n");
2697c478bd9Sstevel@tonic-gate (void) fclose(file);
2707c478bd9Sstevel@tonic-gate return;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate do {
2747c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, timer);
2757c478bd9Sstevel@tonic-gate if (firsttrip) {
276*cb76cc66SToomas Soome firsttrip = false;
2777c478bd9Sstevel@tonic-gate } else {
2787c478bd9Sstevel@tonic-gate ap->th_opcode = htons((ushort_t)ACK);
2797c478bd9Sstevel@tonic-gate ap->th_block = htons((ushort_t)(block));
2807c478bd9Sstevel@tonic-gate size = 4;
2817c478bd9Sstevel@tonic-gate block++;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate send_oack_ack:
2857c478bd9Sstevel@tonic-gate timeout = 0;
2867c478bd9Sstevel@tonic-gate (void) setjmp(timeoutbuf);
2877c478bd9Sstevel@tonic-gate send_ack:
2887c478bd9Sstevel@tonic-gate if (trace)
2897c478bd9Sstevel@tonic-gate tpacket("sent", ap, size);
2907c478bd9Sstevel@tonic-gate if (sendto(f, ackbuf.tb_data, size, 0, (struct sockaddr *)&sin6,
2917c478bd9Sstevel@tonic-gate sizeof (sin6)) != size) {
2927c478bd9Sstevel@tonic-gate (void) alarm(0);
2937c478bd9Sstevel@tonic-gate perror("tftp: sendto");
2947c478bd9Sstevel@tonic-gate goto abort;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate if (write_behind(file, convert) < 0) {
2977c478bd9Sstevel@tonic-gate nak(errno + 100);
2987c478bd9Sstevel@tonic-gate goto abort;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate (void) alarm(rexmtval);
3017c478bd9Sstevel@tonic-gate for (; ; ) {
3027c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
3037c478bd9Sstevel@tonic-gate do {
3047c478bd9Sstevel@tonic-gate fromlen = (socklen_t)sizeof (from);
3057c478bd9Sstevel@tonic-gate n = recvfrom(f, dp, blocksize + 4, 0,
3067c478bd9Sstevel@tonic-gate (struct sockaddr *)&from, &fromlen);
3077c478bd9Sstevel@tonic-gate if (n < 0) {
3087c478bd9Sstevel@tonic-gate perror("tftp: recvfrom");
3097c478bd9Sstevel@tonic-gate goto abort;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate } while (n < offsetof(struct tftphdr, th_data));
3127c478bd9Sstevel@tonic-gate (void) sighold(SIGALRM);
3137c478bd9Sstevel@tonic-gate sin6.sin6_port = from.sin6_port; /* added */
3147c478bd9Sstevel@tonic-gate if (trace)
3157c478bd9Sstevel@tonic-gate tpacket("received", dp, n);
3167c478bd9Sstevel@tonic-gate /* should verify client address */
3177c478bd9Sstevel@tonic-gate dp->th_opcode = ntohs(dp->th_opcode);
3187c478bd9Sstevel@tonic-gate if (dp->th_opcode == ERROR) {
3197c478bd9Sstevel@tonic-gate dp->th_code = ntohs(dp->th_code);
3207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Error code %d",
3217c478bd9Sstevel@tonic-gate dp->th_code);
3227c478bd9Sstevel@tonic-gate if (n > offsetof(struct tftphdr, th_data))
3237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %.*s", n -
3247c478bd9Sstevel@tonic-gate offsetof(struct tftphdr, th_data),
3257c478bd9Sstevel@tonic-gate dp->th_msg);
3267c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
3277c478bd9Sstevel@tonic-gate goto abort;
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate if ((block == 1) && (dp->th_opcode == OACK)) {
3307c478bd9Sstevel@tonic-gate errcode = process_oack((tftpbuf *)dp, n);
3317c478bd9Sstevel@tonic-gate if (errcode >= 0) {
3327c478bd9Sstevel@tonic-gate cancel_alarm();
3337c478bd9Sstevel@tonic-gate nak(errcode);
3347c478bd9Sstevel@tonic-gate (void) fputs("Rejected OACK\n",
3357c478bd9Sstevel@tonic-gate stderr);
3367c478bd9Sstevel@tonic-gate (void) fclose(file);
3377c478bd9Sstevel@tonic-gate return;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate ap->th_opcode = htons((ushort_t)ACK);
3407c478bd9Sstevel@tonic-gate ap->th_block = htons(0);
3417c478bd9Sstevel@tonic-gate size = 4;
3427c478bd9Sstevel@tonic-gate goto send_oack_ack;
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate if (dp->th_opcode == DATA) {
3457c478bd9Sstevel@tonic-gate int j;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate dp->th_block = ntohs(dp->th_block);
3487c478bd9Sstevel@tonic-gate if (dp->th_block == block) {
3497c478bd9Sstevel@tonic-gate break; /* have next packet */
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate * On an error, try to synchronize
3537c478bd9Sstevel@tonic-gate * both sides.
3547c478bd9Sstevel@tonic-gate */
3557c478bd9Sstevel@tonic-gate j = synchnet(f);
3567c478bd9Sstevel@tonic-gate if (j < 0) {
3577c478bd9Sstevel@tonic-gate perror("tftp: recvfrom");
3587c478bd9Sstevel@tonic-gate goto abort;
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate if ((j > 0) && trace) {
3617c478bd9Sstevel@tonic-gate (void) printf("discarded %d packets\n",
3627c478bd9Sstevel@tonic-gate j);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate if (dp->th_block == (block-1)) {
3657c478bd9Sstevel@tonic-gate goto send_ack; /* resend ack */
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate cancel_alarm();
3707c478bd9Sstevel@tonic-gate size = writeit(file, &dp, n - 4, convert);
3717c478bd9Sstevel@tonic-gate if (size < 0) {
3727c478bd9Sstevel@tonic-gate nak(errno + 100);
3737c478bd9Sstevel@tonic-gate goto abort;
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate amount += size;
3767c478bd9Sstevel@tonic-gate } while (size == blocksize);
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate cancel_alarm();
3797c478bd9Sstevel@tonic-gate if (write_behind(file, convert) < 0) { /* flush last buffer */
3807c478bd9Sstevel@tonic-gate nak(errno + 100);
3817c478bd9Sstevel@tonic-gate goto abort;
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate n = fclose(file);
3847c478bd9Sstevel@tonic-gate file = NULL;
3857c478bd9Sstevel@tonic-gate if (n == EOF) {
3867c478bd9Sstevel@tonic-gate nak(errno + 100);
3877c478bd9Sstevel@tonic-gate goto abort;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate /* ok to ack, since user has seen err msg */
3917c478bd9Sstevel@tonic-gate ap->th_opcode = htons((ushort_t)ACK);
3927c478bd9Sstevel@tonic-gate ap->th_block = htons((ushort_t)block);
3937c478bd9Sstevel@tonic-gate if (trace)
3947c478bd9Sstevel@tonic-gate tpacket("sent", ap, 4);
3957c478bd9Sstevel@tonic-gate if (sendto(f, ackbuf.tb_data, 4, 0,
3967c478bd9Sstevel@tonic-gate (struct sockaddr *)&sin6, sizeof (sin6)) != 4)
3977c478bd9Sstevel@tonic-gate perror("tftp: sendto");
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate abort:
4007c478bd9Sstevel@tonic-gate cancel_alarm();
4017c478bd9Sstevel@tonic-gate if (file != NULL)
4027c478bd9Sstevel@tonic-gate (void) fclose(file);
4037c478bd9Sstevel@tonic-gate stopclock();
4047c478bd9Sstevel@tonic-gate if (amount > 0)
4057c478bd9Sstevel@tonic-gate printstats("Received", amount);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate static int
makerequest(int request,char * name,struct tftphdr * tp,char * mode)4097c478bd9Sstevel@tonic-gate makerequest(int request, char *name, struct tftphdr *tp, char *mode)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate char *cp, *cpend;
4127c478bd9Sstevel@tonic-gate int len;
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate tp->th_opcode = htons((ushort_t)request);
4157c478bd9Sstevel@tonic-gate cp = (char *)&tp->th_stuff;
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate /* Maximum size of a request packet is 512 bytes (RFC 2347) */
4187c478bd9Sstevel@tonic-gate cpend = (char *)tp + SEGSIZE;
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate len = strlcpy(cp, name, cpend - cp) + 1;
4217c478bd9Sstevel@tonic-gate cp += len;
4227c478bd9Sstevel@tonic-gate if (cp > cpend)
4237c478bd9Sstevel@tonic-gate return (-1);
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate len = strlcpy(cp, mode, cpend - cp) + 1;
4267c478bd9Sstevel@tonic-gate cp += len;
4277c478bd9Sstevel@tonic-gate if (cp > cpend)
4287c478bd9Sstevel@tonic-gate return (-1);
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate len = add_options(cp, cpend);
4317c478bd9Sstevel@tonic-gate if (len == -1)
4327c478bd9Sstevel@tonic-gate return (-1);
4337c478bd9Sstevel@tonic-gate cp += len;
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate return (cp - (char *)tp);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate * Return the blksize option value string to include in the request packet.
4407c478bd9Sstevel@tonic-gate */
4417c478bd9Sstevel@tonic-gate static char *
blksize_str(void)4427c478bd9Sstevel@tonic-gate blksize_str(void)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate blocksize = SEGSIZE;
4457c478bd9Sstevel@tonic-gate if (blksize == 0)
4467c478bd9Sstevel@tonic-gate return (NULL);
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate (void) snprintf(optbuf, sizeof (optbuf), "%d", blksize);
4497c478bd9Sstevel@tonic-gate return (optbuf);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate * Return the timeout option value string to include in the request packet.
4547c478bd9Sstevel@tonic-gate */
4557c478bd9Sstevel@tonic-gate static char *
timeout_str(void)4567c478bd9Sstevel@tonic-gate timeout_str(void)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate if (srexmtval == 0)
4597c478bd9Sstevel@tonic-gate return (NULL);
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate (void) snprintf(optbuf, sizeof (optbuf), "%d", srexmtval);
4627c478bd9Sstevel@tonic-gate return (optbuf);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate * Return the tsize option value string to include in the request packet.
4677c478bd9Sstevel@tonic-gate */
4687c478bd9Sstevel@tonic-gate static char *
tsize_str(void)4697c478bd9Sstevel@tonic-gate tsize_str(void)
4707c478bd9Sstevel@tonic-gate {
471*cb76cc66SToomas Soome if (tsize_set == false)
4727c478bd9Sstevel@tonic-gate return (NULL);
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate (void) snprintf(optbuf, sizeof (optbuf), OFF_T_FMT, tsize);
4757c478bd9Sstevel@tonic-gate return (optbuf);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate * Validate and action the blksize option value string from the OACK packet.
4807c478bd9Sstevel@tonic-gate * Returns -1 on success or an error code on failure.
4817c478bd9Sstevel@tonic-gate */
4827c478bd9Sstevel@tonic-gate static int
blksize_handler(char * optstr)4837c478bd9Sstevel@tonic-gate blksize_handler(char *optstr)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate char *endp;
4867c478bd9Sstevel@tonic-gate int value;
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate /* Make sure the option was requested */
4897c478bd9Sstevel@tonic-gate if (blksize == 0)
4907c478bd9Sstevel@tonic-gate return (EOPTNEG);
4917c478bd9Sstevel@tonic-gate errno = 0;
4927c478bd9Sstevel@tonic-gate value = (int)strtol(optstr, &endp, 10);
4937c478bd9Sstevel@tonic-gate if (errno != 0 || value < MIN_BLKSIZE || value > blksize ||
4947c478bd9Sstevel@tonic-gate *endp != '\0')
4957c478bd9Sstevel@tonic-gate return (EOPTNEG);
4967c478bd9Sstevel@tonic-gate blocksize = value;
4977c478bd9Sstevel@tonic-gate return (-1);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate * Validate and action the timeout option value string from the OACK packet.
5027c478bd9Sstevel@tonic-gate * Returns -1 on success or an error code on failure.
5037c478bd9Sstevel@tonic-gate */
5047c478bd9Sstevel@tonic-gate static int
timeout_handler(char * optstr)5057c478bd9Sstevel@tonic-gate timeout_handler(char *optstr)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate char *endp;
5087c478bd9Sstevel@tonic-gate int value;
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate /* Make sure the option was requested */
5117c478bd9Sstevel@tonic-gate if (srexmtval == 0)
5127c478bd9Sstevel@tonic-gate return (EOPTNEG);
5137c478bd9Sstevel@tonic-gate errno = 0;
5147c478bd9Sstevel@tonic-gate value = (int)strtol(optstr, &endp, 10);
5157c478bd9Sstevel@tonic-gate if (errno != 0 || value != srexmtval || *endp != '\0')
5167c478bd9Sstevel@tonic-gate return (EOPTNEG);
5177c478bd9Sstevel@tonic-gate /*
5187c478bd9Sstevel@tonic-gate * Nothing to set, client and server retransmission intervals are
5197c478bd9Sstevel@tonic-gate * set separately in the client.
5207c478bd9Sstevel@tonic-gate */
5217c478bd9Sstevel@tonic-gate return (-1);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate * Validate and action the tsize option value string from the OACK packet.
5267c478bd9Sstevel@tonic-gate * Returns -1 on success or an error code on failure.
5277c478bd9Sstevel@tonic-gate */
5287c478bd9Sstevel@tonic-gate static int
tsize_handler(char * optstr)5297c478bd9Sstevel@tonic-gate tsize_handler(char *optstr)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate char *endp;
5327c478bd9Sstevel@tonic-gate longlong_t value;
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate /* Make sure the option was requested */
535*cb76cc66SToomas Soome if (tsize_set == false)
5367c478bd9Sstevel@tonic-gate return (EOPTNEG);
5377c478bd9Sstevel@tonic-gate errno = 0;
5387c478bd9Sstevel@tonic-gate value = strtoll(optstr, &endp, 10);
5397c478bd9Sstevel@tonic-gate if (errno != 0 || value < 0 || *endp != '\0')
5407c478bd9Sstevel@tonic-gate return (EOPTNEG);
5417c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 32
5427c478bd9Sstevel@tonic-gate if (value > MAXOFF_T)
5437c478bd9Sstevel@tonic-gate return (ENOSPACE);
5447c478bd9Sstevel@tonic-gate #endif
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate * Don't bother checking the tsize value we specified in a write
5477c478bd9Sstevel@tonic-gate * request is echoed back in the OACK.
5487c478bd9Sstevel@tonic-gate */
5497c478bd9Sstevel@tonic-gate if (tsize == 0)
5507c478bd9Sstevel@tonic-gate tsize = value;
5517c478bd9Sstevel@tonic-gate return (-1);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate * Add TFTP options to a request packet.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate static int
add_options(char * obuf,char * obufend)5587c478bd9Sstevel@tonic-gate add_options(char *obuf, char *obufend)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate int i;
5617c478bd9Sstevel@tonic-gate char *cp, *ostr;
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate cp = obuf;
5647c478bd9Sstevel@tonic-gate for (i = 0; options[i].opt_name != NULL; i++) {
5657c478bd9Sstevel@tonic-gate ostr = options[i].opt_str();
5667c478bd9Sstevel@tonic-gate if (ostr != NULL) {
5677c478bd9Sstevel@tonic-gate cp += strlcpy(cp, options[i].opt_name, obufend - cp)
5687c478bd9Sstevel@tonic-gate + 1;
5697c478bd9Sstevel@tonic-gate if (cp > obufend)
5707c478bd9Sstevel@tonic-gate return (-1);
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate cp += strlcpy(cp, ostr, obufend - cp) + 1;
5737c478bd9Sstevel@tonic-gate if (cp > obufend)
5747c478bd9Sstevel@tonic-gate return (-1);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate return (cp - obuf);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate * Process OACK packet sent by server in response to options in the request
5827c478bd9Sstevel@tonic-gate * packet. Returns -1 on success or an error code on failure.
5837c478bd9Sstevel@tonic-gate */
5847c478bd9Sstevel@tonic-gate static int
process_oack(tftpbuf * oackbuf,int n)5857c478bd9Sstevel@tonic-gate process_oack(tftpbuf *oackbuf, int n)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate char *cp, *oackend, *optname, *optval;
5887c478bd9Sstevel@tonic-gate struct tftphdr *oackp;
5897c478bd9Sstevel@tonic-gate int i, errcode;
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate oackp = &oackbuf->tb_hdr;
5927c478bd9Sstevel@tonic-gate cp = (char *)&oackp->th_stuff;
5937c478bd9Sstevel@tonic-gate oackend = (char *)oackbuf + n;
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate while (cp < oackend) {
5967c478bd9Sstevel@tonic-gate optname = cp;
5977c478bd9Sstevel@tonic-gate if ((optval = next_field(optname, oackend)) == NULL)
5987c478bd9Sstevel@tonic-gate return (EOPTNEG);
5997c478bd9Sstevel@tonic-gate if ((cp = next_field(optval, oackend)) == NULL)
6007c478bd9Sstevel@tonic-gate return (EOPTNEG);
6017c478bd9Sstevel@tonic-gate for (i = 0; options[i].opt_name != NULL; i++) {
6027c478bd9Sstevel@tonic-gate if (strcasecmp(optname, options[i].opt_name) == 0)
6037c478bd9Sstevel@tonic-gate break;
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate if (options[i].opt_name == NULL)
6067c478bd9Sstevel@tonic-gate return (EOPTNEG);
6077c478bd9Sstevel@tonic-gate errcode = options[i].opt_handler(optval);
6087c478bd9Sstevel@tonic-gate if (errcode >= 0)
6097c478bd9Sstevel@tonic-gate return (errcode);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate return (-1);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate * Send a nak packet (error message).
6167c478bd9Sstevel@tonic-gate * Error code passed in is one of the
6177c478bd9Sstevel@tonic-gate * standard TFTP codes, or a UNIX errno
6187c478bd9Sstevel@tonic-gate * offset by 100.
6197c478bd9Sstevel@tonic-gate */
6207c478bd9Sstevel@tonic-gate static void
nak(int error)6217c478bd9Sstevel@tonic-gate nak(int error)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate struct tftphdr *tp;
6247c478bd9Sstevel@tonic-gate int length;
6257c478bd9Sstevel@tonic-gate struct errmsg *pe;
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate tp = &ackbuf.tb_hdr;
6287c478bd9Sstevel@tonic-gate tp->th_opcode = htons((ushort_t)ERROR);
6297c478bd9Sstevel@tonic-gate tp->th_code = htons((ushort_t)error);
6307c478bd9Sstevel@tonic-gate for (pe = errmsgs; pe->e_code >= 0; pe++)
6317c478bd9Sstevel@tonic-gate if (pe->e_code == error)
6327c478bd9Sstevel@tonic-gate break;
6337c478bd9Sstevel@tonic-gate if (pe->e_code < 0) {
6347c478bd9Sstevel@tonic-gate pe->e_msg = strerror(error - 100);
6357c478bd9Sstevel@tonic-gate tp->th_code = EUNDEF;
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate (void) strlcpy(tp->th_msg, pe->e_msg,
6387c478bd9Sstevel@tonic-gate sizeof (ackbuf) - sizeof (struct tftphdr));
6397c478bd9Sstevel@tonic-gate length = strlen(pe->e_msg) + 4;
6407c478bd9Sstevel@tonic-gate if (trace)
6417c478bd9Sstevel@tonic-gate tpacket("sent", tp, length);
6427c478bd9Sstevel@tonic-gate if (sendto(f, ackbuf.tb_data, length, 0,
6437c478bd9Sstevel@tonic-gate (struct sockaddr *)&sin6, sizeof (sin6)) != length)
6447c478bd9Sstevel@tonic-gate perror("nak");
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate static void
tpacket(char * s,struct tftphdr * tp,int n)6487c478bd9Sstevel@tonic-gate tpacket(char *s, struct tftphdr *tp, int n)
6497c478bd9Sstevel@tonic-gate {
650*cb76cc66SToomas Soome static char *opcodes[] = {
651*cb76cc66SToomas Soome "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK"
652*cb76cc66SToomas Soome };
6537c478bd9Sstevel@tonic-gate char *cp, *file, *mode;
6547c478bd9Sstevel@tonic-gate ushort_t op = ntohs(tp->th_opcode);
6557c478bd9Sstevel@tonic-gate char *tpend;
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate if (op < RRQ || op > OACK)
6587c478bd9Sstevel@tonic-gate (void) printf("%s opcode=%x ", s, op);
6597c478bd9Sstevel@tonic-gate else
6607c478bd9Sstevel@tonic-gate (void) printf("%s %s ", s, opcodes[op]);
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate switch (op) {
6637c478bd9Sstevel@tonic-gate case RRQ:
6647c478bd9Sstevel@tonic-gate case WRQ:
6657c478bd9Sstevel@tonic-gate tpend = (char *)tp + n;
6667c478bd9Sstevel@tonic-gate n -= sizeof (tp->th_opcode);
6677c478bd9Sstevel@tonic-gate file = (char *)&tp->th_stuff;
6687c478bd9Sstevel@tonic-gate if ((mode = next_field(file, tpend)) == NULL) {
6697c478bd9Sstevel@tonic-gate (void) printf("<file=%.*s>\n", n, file);
6707c478bd9Sstevel@tonic-gate break;
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate n -= mode - file;
6737c478bd9Sstevel@tonic-gate if ((cp = next_field(mode, tpend)) == NULL) {
6747c478bd9Sstevel@tonic-gate (void) printf("<file=%s, mode=%.*s>\n", file, n, mode);
6757c478bd9Sstevel@tonic-gate break;
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate (void) printf("<file=%s, mode=%s", file, mode);
6787c478bd9Sstevel@tonic-gate n -= cp - mode;
6797c478bd9Sstevel@tonic-gate if (n > 0) {
6807c478bd9Sstevel@tonic-gate (void) printf(", options: ");
6817c478bd9Sstevel@tonic-gate print_options(stdout, cp, n);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate (void) puts(">");
6847c478bd9Sstevel@tonic-gate break;
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate case DATA:
6877c478bd9Sstevel@tonic-gate (void) printf("<block=%d, %d bytes>\n", ntohs(tp->th_block),
6887c478bd9Sstevel@tonic-gate n - sizeof (tp->th_opcode) - sizeof (tp->th_block));
6897c478bd9Sstevel@tonic-gate break;
6907c478bd9Sstevel@tonic-gate
6917c478bd9Sstevel@tonic-gate case ACK:
6927c478bd9Sstevel@tonic-gate (void) printf("<block=%d>\n", ntohs(tp->th_block));
6937c478bd9Sstevel@tonic-gate break;
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate case OACK:
6967c478bd9Sstevel@tonic-gate (void) printf("<options: ");
6977c478bd9Sstevel@tonic-gate print_options(stdout, (char *)&tp->th_stuff,
6987c478bd9Sstevel@tonic-gate n - sizeof (tp->th_opcode));
6997c478bd9Sstevel@tonic-gate (void) puts(">");
7007c478bd9Sstevel@tonic-gate break;
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate case ERROR:
7037c478bd9Sstevel@tonic-gate (void) printf("<code=%d", ntohs(tp->th_code));
7047c478bd9Sstevel@tonic-gate n = n - sizeof (tp->th_opcode) - sizeof (tp->th_code);
7057c478bd9Sstevel@tonic-gate if (n > 0)
7067c478bd9Sstevel@tonic-gate (void) printf(", msg=%.*s", n, tp->th_msg);
7077c478bd9Sstevel@tonic-gate (void) puts(">");
7087c478bd9Sstevel@tonic-gate break;
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate static hrtime_t tstart, tstop;
7137c478bd9Sstevel@tonic-gate
7147c478bd9Sstevel@tonic-gate static void
startclock(void)7157c478bd9Sstevel@tonic-gate startclock(void)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate tstart = gethrtime();
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate static void
stopclock(void)7217c478bd9Sstevel@tonic-gate stopclock(void)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate tstop = gethrtime();
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate static void
printstats(char * direction,off_t amount)7277c478bd9Sstevel@tonic-gate printstats(char *direction, off_t amount)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate hrtime_t delta, tenths;
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate delta = tstop - tstart;
7327c478bd9Sstevel@tonic-gate tenths = delta / (NANOSEC / 10);
7337c478bd9Sstevel@tonic-gate (void) printf("%s " OFF_T_FMT " bytes in %" PRId64 ".%" PRId64
7347c478bd9Sstevel@tonic-gate " seconds", direction, amount, tenths / 10, tenths % 10);
7357c478bd9Sstevel@tonic-gate if (verbose)
7367c478bd9Sstevel@tonic-gate (void) printf(" [%" PRId64 " bits/sec]\n",
7377c478bd9Sstevel@tonic-gate ((hrtime_t)amount * 8 * NANOSEC) / delta);
7387c478bd9Sstevel@tonic-gate else
7397c478bd9Sstevel@tonic-gate (void) putchar('\n');
7407c478bd9Sstevel@tonic-gate }
741