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 5*294f5787Sas198278 * Common Development and Distribution License (the "License"). 6*294f5787Sas198278 * 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 /* 22*294f5787Sas198278 * 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 */ 277c478bd9Sstevel@tonic-gate /* 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 #pragma ident "%Z%%M% %I% %E% SMI" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * TFTP User Program -- Protocol Machines 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate #include <sys/types.h> 457c478bd9Sstevel@tonic-gate #include <sys/socket.h> 467c478bd9Sstevel@tonic-gate #include <sys/time.h> 477c478bd9Sstevel@tonic-gate #include <sys/stat.h> 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #include <signal.h> 507c478bd9Sstevel@tonic-gate #include <stdio.h> 517c478bd9Sstevel@tonic-gate #include <stdlib.h> 527c478bd9Sstevel@tonic-gate #include <unistd.h> 537c478bd9Sstevel@tonic-gate #include <errno.h> 547c478bd9Sstevel@tonic-gate #include <string.h> 557c478bd9Sstevel@tonic-gate #include <stddef.h> 567c478bd9Sstevel@tonic-gate #include <inttypes.h> 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #include "tftpcommon.h" 597c478bd9Sstevel@tonic-gate #include "tftpprivate.h" 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate static char *blksize_str(void); 627c478bd9Sstevel@tonic-gate static char *timeout_str(void); 637c478bd9Sstevel@tonic-gate static char *tsize_str(void); 647c478bd9Sstevel@tonic-gate static int blksize_handler(char *); 657c478bd9Sstevel@tonic-gate static int timeout_handler(char *); 667c478bd9Sstevel@tonic-gate static int tsize_handler(char *); 677c478bd9Sstevel@tonic-gate static int add_options(char *, char *); 687c478bd9Sstevel@tonic-gate static int process_oack(tftpbuf *, int); 697c478bd9Sstevel@tonic-gate static void nak(int); 707c478bd9Sstevel@tonic-gate static void startclock(void); 717c478bd9Sstevel@tonic-gate static void stopclock(void); 727c478bd9Sstevel@tonic-gate static void printstats(char *, off_t); 737c478bd9Sstevel@tonic-gate static int makerequest(int, char *, struct tftphdr *, char *); 747c478bd9Sstevel@tonic-gate static void tpacket(char *, struct tftphdr *, int); 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate static struct options { 777c478bd9Sstevel@tonic-gate char *opt_name; 787c478bd9Sstevel@tonic-gate char *(*opt_str)(void); 797c478bd9Sstevel@tonic-gate int (*opt_handler)(char *); 807c478bd9Sstevel@tonic-gate } options[] = { 817c478bd9Sstevel@tonic-gate { "blksize", blksize_str, blksize_handler }, 827c478bd9Sstevel@tonic-gate { "timeout", timeout_str, timeout_handler }, 837c478bd9Sstevel@tonic-gate { "tsize", tsize_str, tsize_handler }, 847c478bd9Sstevel@tonic-gate { NULL } 857c478bd9Sstevel@tonic-gate }; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate static char optbuf[MAX_OPTVAL_LEN]; 887c478bd9Sstevel@tonic-gate static boolean_t tsize_set; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate static tftpbuf ackbuf; 917c478bd9Sstevel@tonic-gate static int timeout; 927c478bd9Sstevel@tonic-gate static off_t tsize; 937c478bd9Sstevel@tonic-gate static jmp_buf timeoutbuf; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate int blocksize = SEGSIZE; /* Number of data bytes in a DATA packet */ 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 987c478bd9Sstevel@tonic-gate static void 997c478bd9Sstevel@tonic-gate timer(int signum) 1007c478bd9Sstevel@tonic-gate { 1017c478bd9Sstevel@tonic-gate timeout += rexmtval; 1027c478bd9Sstevel@tonic-gate if (timeout >= maxtimeout) { 1037c478bd9Sstevel@tonic-gate (void) fputs("Transfer timed out.\n", stderr); 1047c478bd9Sstevel@tonic-gate longjmp(toplevel, -1); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, timer); 1077c478bd9Sstevel@tonic-gate longjmp(timeoutbuf, 1); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate /* 1117c478bd9Sstevel@tonic-gate * Send the requested file. 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate void 1147c478bd9Sstevel@tonic-gate tftp_sendfile(int fd, char *name, char *mode) 1157c478bd9Sstevel@tonic-gate { 1167c478bd9Sstevel@tonic-gate struct tftphdr *ap; /* data and ack packets */ 1177c478bd9Sstevel@tonic-gate struct tftphdr *dp; 118*294f5787Sas198278 int count = 0, size, n; 119*294f5787Sas198278 ushort_t block = 0; 1207c478bd9Sstevel@tonic-gate 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); 140*294f5787Sas198278 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 */ 169*294f5787Sas198278 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, 1937c478bd9Sstevel@tonic-gate "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 } 201*294f5787Sas198278 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(); 224*294f5787Sas198278 if (count > 0) 2257c478bd9Sstevel@tonic-gate amount += size; 2267c478bd9Sstevel@tonic-gate block++; 227*294f5787Sas198278 count++; 228*294f5787Sas198278 } 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 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*294f5787Sas198278 ushort_t block = 1; 246*294f5787Sas198278 int n, size; 2477c478bd9Sstevel@tonic-gate unsigned long amount = 0; 2487c478bd9Sstevel@tonic-gate struct sockaddr_in6 from; 2497c478bd9Sstevel@tonic-gate socklen_t fromlen; 2507c478bd9Sstevel@tonic-gate boolean_t firsttrip = B_TRUE; 2517c478bd9Sstevel@tonic-gate FILE *file; 2527c478bd9Sstevel@tonic-gate int convert; /* true if converting crlf -> lf */ 2537c478bd9Sstevel@tonic-gate int errcode; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate startclock(); 2567c478bd9Sstevel@tonic-gate dp = w_init(); 2577c478bd9Sstevel@tonic-gate ap = &ackbuf.tb_hdr; 2587c478bd9Sstevel@tonic-gate file = fdopen(fd, "w"); 2597c478bd9Sstevel@tonic-gate convert = (strcmp(mode, "netascii") == 0); 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate tsize_set = (tsize_opt != 0); 2627c478bd9Sstevel@tonic-gate if (tsize_set) 2637c478bd9Sstevel@tonic-gate tsize = 0; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate if ((size = makerequest(RRQ, name, ap, mode)) == -1) { 2667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2677c478bd9Sstevel@tonic-gate "tftp: Error: Read request packet too big\n"); 2687c478bd9Sstevel@tonic-gate (void) fclose(file); 2697c478bd9Sstevel@tonic-gate return; 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate do { 2737c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, timer); 2747c478bd9Sstevel@tonic-gate if (firsttrip) { 2757c478bd9Sstevel@tonic-gate firsttrip = B_FALSE; 2767c478bd9Sstevel@tonic-gate } else { 2777c478bd9Sstevel@tonic-gate ap->th_opcode = htons((ushort_t)ACK); 2787c478bd9Sstevel@tonic-gate ap->th_block = htons((ushort_t)(block)); 2797c478bd9Sstevel@tonic-gate size = 4; 2807c478bd9Sstevel@tonic-gate block++; 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate send_oack_ack: 2847c478bd9Sstevel@tonic-gate timeout = 0; 2857c478bd9Sstevel@tonic-gate (void) setjmp(timeoutbuf); 2867c478bd9Sstevel@tonic-gate send_ack: 2877c478bd9Sstevel@tonic-gate if (trace) 2887c478bd9Sstevel@tonic-gate tpacket("sent", ap, size); 2897c478bd9Sstevel@tonic-gate if (sendto(f, ackbuf.tb_data, size, 0, (struct sockaddr *)&sin6, 2907c478bd9Sstevel@tonic-gate sizeof (sin6)) != size) { 2917c478bd9Sstevel@tonic-gate (void) alarm(0); 2927c478bd9Sstevel@tonic-gate perror("tftp: sendto"); 2937c478bd9Sstevel@tonic-gate goto abort; 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate if (write_behind(file, convert) < 0) { 2967c478bd9Sstevel@tonic-gate nak(errno + 100); 2977c478bd9Sstevel@tonic-gate goto abort; 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate (void) alarm(rexmtval); 3007c478bd9Sstevel@tonic-gate for (; ; ) { 3017c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM); 3027c478bd9Sstevel@tonic-gate do { 3037c478bd9Sstevel@tonic-gate fromlen = (socklen_t)sizeof (from); 3047c478bd9Sstevel@tonic-gate n = recvfrom(f, dp, blocksize + 4, 0, 3057c478bd9Sstevel@tonic-gate (struct sockaddr *)&from, &fromlen); 3067c478bd9Sstevel@tonic-gate if (n < 0) { 3077c478bd9Sstevel@tonic-gate perror("tftp: recvfrom"); 3087c478bd9Sstevel@tonic-gate goto abort; 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate } while (n < offsetof(struct tftphdr, th_data)); 3117c478bd9Sstevel@tonic-gate (void) sighold(SIGALRM); 3127c478bd9Sstevel@tonic-gate sin6.sin6_port = from.sin6_port; /* added */ 3137c478bd9Sstevel@tonic-gate if (trace) 3147c478bd9Sstevel@tonic-gate tpacket("received", dp, n); 3157c478bd9Sstevel@tonic-gate /* should verify client address */ 3167c478bd9Sstevel@tonic-gate dp->th_opcode = ntohs(dp->th_opcode); 3177c478bd9Sstevel@tonic-gate if (dp->th_opcode == ERROR) { 3187c478bd9Sstevel@tonic-gate dp->th_code = ntohs(dp->th_code); 3197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Error code %d", 3207c478bd9Sstevel@tonic-gate dp->th_code); 3217c478bd9Sstevel@tonic-gate if (n > offsetof(struct tftphdr, th_data)) 3227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %.*s", n - 3237c478bd9Sstevel@tonic-gate offsetof(struct tftphdr, th_data), 3247c478bd9Sstevel@tonic-gate dp->th_msg); 3257c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr); 3267c478bd9Sstevel@tonic-gate goto abort; 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate if ((block == 1) && (dp->th_opcode == OACK)) { 3297c478bd9Sstevel@tonic-gate errcode = process_oack((tftpbuf *)dp, n); 3307c478bd9Sstevel@tonic-gate if (errcode >= 0) { 3317c478bd9Sstevel@tonic-gate cancel_alarm(); 3327c478bd9Sstevel@tonic-gate nak(errcode); 3337c478bd9Sstevel@tonic-gate (void) fputs("Rejected OACK\n", 3347c478bd9Sstevel@tonic-gate stderr); 3357c478bd9Sstevel@tonic-gate (void) fclose(file); 3367c478bd9Sstevel@tonic-gate return; 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate ap->th_opcode = htons((ushort_t)ACK); 3397c478bd9Sstevel@tonic-gate ap->th_block = htons(0); 3407c478bd9Sstevel@tonic-gate size = 4; 3417c478bd9Sstevel@tonic-gate goto send_oack_ack; 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate if (dp->th_opcode == DATA) { 3447c478bd9Sstevel@tonic-gate int j; 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate dp->th_block = ntohs(dp->th_block); 3477c478bd9Sstevel@tonic-gate if (dp->th_block == block) { 3487c478bd9Sstevel@tonic-gate break; /* have next packet */ 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate /* 3517c478bd9Sstevel@tonic-gate * On an error, try to synchronize 3527c478bd9Sstevel@tonic-gate * both sides. 3537c478bd9Sstevel@tonic-gate */ 3547c478bd9Sstevel@tonic-gate j = synchnet(f); 3557c478bd9Sstevel@tonic-gate if (j < 0) { 3567c478bd9Sstevel@tonic-gate perror("tftp: recvfrom"); 3577c478bd9Sstevel@tonic-gate goto abort; 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate if ((j > 0) && trace) { 3607c478bd9Sstevel@tonic-gate (void) printf("discarded %d packets\n", 3617c478bd9Sstevel@tonic-gate j); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate if (dp->th_block == (block-1)) { 3647c478bd9Sstevel@tonic-gate goto send_ack; /* resend ack */ 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate cancel_alarm(); 3697c478bd9Sstevel@tonic-gate size = writeit(file, &dp, n - 4, convert); 3707c478bd9Sstevel@tonic-gate if (size < 0) { 3717c478bd9Sstevel@tonic-gate nak(errno + 100); 3727c478bd9Sstevel@tonic-gate goto abort; 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate amount += size; 3757c478bd9Sstevel@tonic-gate } while (size == blocksize); 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate cancel_alarm(); 3787c478bd9Sstevel@tonic-gate if (write_behind(file, convert) < 0) { /* flush last buffer */ 3797c478bd9Sstevel@tonic-gate nak(errno + 100); 3807c478bd9Sstevel@tonic-gate goto abort; 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate n = fclose(file); 3837c478bd9Sstevel@tonic-gate file = NULL; 3847c478bd9Sstevel@tonic-gate if (n == EOF) { 3857c478bd9Sstevel@tonic-gate nak(errno + 100); 3867c478bd9Sstevel@tonic-gate goto abort; 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate /* ok to ack, since user has seen err msg */ 3907c478bd9Sstevel@tonic-gate ap->th_opcode = htons((ushort_t)ACK); 3917c478bd9Sstevel@tonic-gate ap->th_block = htons((ushort_t)block); 3927c478bd9Sstevel@tonic-gate if (trace) 3937c478bd9Sstevel@tonic-gate tpacket("sent", ap, 4); 3947c478bd9Sstevel@tonic-gate if (sendto(f, ackbuf.tb_data, 4, 0, 3957c478bd9Sstevel@tonic-gate (struct sockaddr *)&sin6, sizeof (sin6)) != 4) 3967c478bd9Sstevel@tonic-gate perror("tftp: sendto"); 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate abort: 3997c478bd9Sstevel@tonic-gate cancel_alarm(); 4007c478bd9Sstevel@tonic-gate if (file != NULL) 4017c478bd9Sstevel@tonic-gate (void) fclose(file); 4027c478bd9Sstevel@tonic-gate stopclock(); 4037c478bd9Sstevel@tonic-gate if (amount > 0) 4047c478bd9Sstevel@tonic-gate printstats("Received", amount); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate static int 4087c478bd9Sstevel@tonic-gate makerequest(int request, char *name, struct tftphdr *tp, char *mode) 4097c478bd9Sstevel@tonic-gate { 4107c478bd9Sstevel@tonic-gate char *cp, *cpend; 4117c478bd9Sstevel@tonic-gate int len; 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate tp->th_opcode = htons((ushort_t)request); 4147c478bd9Sstevel@tonic-gate cp = (char *)&tp->th_stuff; 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate /* Maximum size of a request packet is 512 bytes (RFC 2347) */ 4177c478bd9Sstevel@tonic-gate cpend = (char *)tp + SEGSIZE; 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate len = strlcpy(cp, name, cpend - cp) + 1; 4207c478bd9Sstevel@tonic-gate cp += len; 4217c478bd9Sstevel@tonic-gate if (cp > cpend) 4227c478bd9Sstevel@tonic-gate return (-1); 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate len = strlcpy(cp, mode, cpend - cp) + 1; 4257c478bd9Sstevel@tonic-gate cp += len; 4267c478bd9Sstevel@tonic-gate if (cp > cpend) 4277c478bd9Sstevel@tonic-gate return (-1); 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate len = add_options(cp, cpend); 4307c478bd9Sstevel@tonic-gate if (len == -1) 4317c478bd9Sstevel@tonic-gate return (-1); 4327c478bd9Sstevel@tonic-gate cp += len; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate return (cp - (char *)tp); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate /* 4387c478bd9Sstevel@tonic-gate * Return the blksize option value string to include in the request packet. 4397c478bd9Sstevel@tonic-gate */ 4407c478bd9Sstevel@tonic-gate static char * 4417c478bd9Sstevel@tonic-gate blksize_str(void) 4427c478bd9Sstevel@tonic-gate { 4437c478bd9Sstevel@tonic-gate blocksize = SEGSIZE; 4447c478bd9Sstevel@tonic-gate if (blksize == 0) 4457c478bd9Sstevel@tonic-gate return (NULL); 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate (void) snprintf(optbuf, sizeof (optbuf), "%d", blksize); 4487c478bd9Sstevel@tonic-gate return (optbuf); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate /* 4527c478bd9Sstevel@tonic-gate * Return the timeout option value string to include in the request packet. 4537c478bd9Sstevel@tonic-gate */ 4547c478bd9Sstevel@tonic-gate static char * 4557c478bd9Sstevel@tonic-gate timeout_str(void) 4567c478bd9Sstevel@tonic-gate { 4577c478bd9Sstevel@tonic-gate if (srexmtval == 0) 4587c478bd9Sstevel@tonic-gate return (NULL); 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate (void) snprintf(optbuf, sizeof (optbuf), "%d", srexmtval); 4617c478bd9Sstevel@tonic-gate return (optbuf); 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate /* 4657c478bd9Sstevel@tonic-gate * Return the tsize option value string to include in the request packet. 4667c478bd9Sstevel@tonic-gate */ 4677c478bd9Sstevel@tonic-gate static char * 4687c478bd9Sstevel@tonic-gate tsize_str(void) 4697c478bd9Sstevel@tonic-gate { 4707c478bd9Sstevel@tonic-gate if (tsize_set == B_FALSE) 4717c478bd9Sstevel@tonic-gate return (NULL); 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate (void) snprintf(optbuf, sizeof (optbuf), OFF_T_FMT, tsize); 4747c478bd9Sstevel@tonic-gate return (optbuf); 4757c478bd9Sstevel@tonic-gate } 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate /* 4787c478bd9Sstevel@tonic-gate * Validate and action the blksize option value string from the OACK packet. 4797c478bd9Sstevel@tonic-gate * Returns -1 on success or an error code on failure. 4807c478bd9Sstevel@tonic-gate */ 4817c478bd9Sstevel@tonic-gate static int 4827c478bd9Sstevel@tonic-gate blksize_handler(char *optstr) 4837c478bd9Sstevel@tonic-gate { 4847c478bd9Sstevel@tonic-gate char *endp; 4857c478bd9Sstevel@tonic-gate int value; 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate /* Make sure the option was requested */ 4887c478bd9Sstevel@tonic-gate if (blksize == 0) 4897c478bd9Sstevel@tonic-gate return (EOPTNEG); 4907c478bd9Sstevel@tonic-gate errno = 0; 4917c478bd9Sstevel@tonic-gate value = (int)strtol(optstr, &endp, 10); 4927c478bd9Sstevel@tonic-gate if (errno != 0 || value < MIN_BLKSIZE || value > blksize || 4937c478bd9Sstevel@tonic-gate *endp != '\0') 4947c478bd9Sstevel@tonic-gate return (EOPTNEG); 4957c478bd9Sstevel@tonic-gate blocksize = value; 4967c478bd9Sstevel@tonic-gate return (-1); 4977c478bd9Sstevel@tonic-gate } 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate /* 5007c478bd9Sstevel@tonic-gate * Validate and action the timeout option value string from the OACK packet. 5017c478bd9Sstevel@tonic-gate * Returns -1 on success or an error code on failure. 5027c478bd9Sstevel@tonic-gate */ 5037c478bd9Sstevel@tonic-gate static int 5047c478bd9Sstevel@tonic-gate timeout_handler(char *optstr) 5057c478bd9Sstevel@tonic-gate { 5067c478bd9Sstevel@tonic-gate char *endp; 5077c478bd9Sstevel@tonic-gate int value; 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate /* Make sure the option was requested */ 5107c478bd9Sstevel@tonic-gate if (srexmtval == 0) 5117c478bd9Sstevel@tonic-gate return (EOPTNEG); 5127c478bd9Sstevel@tonic-gate errno = 0; 5137c478bd9Sstevel@tonic-gate value = (int)strtol(optstr, &endp, 10); 5147c478bd9Sstevel@tonic-gate if (errno != 0 || value != srexmtval || *endp != '\0') 5157c478bd9Sstevel@tonic-gate return (EOPTNEG); 5167c478bd9Sstevel@tonic-gate /* 5177c478bd9Sstevel@tonic-gate * Nothing to set, client and server retransmission intervals are 5187c478bd9Sstevel@tonic-gate * set separately in the client. 5197c478bd9Sstevel@tonic-gate */ 5207c478bd9Sstevel@tonic-gate return (-1); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate /* 5247c478bd9Sstevel@tonic-gate * Validate and action the tsize option value string from the OACK packet. 5257c478bd9Sstevel@tonic-gate * Returns -1 on success or an error code on failure. 5267c478bd9Sstevel@tonic-gate */ 5277c478bd9Sstevel@tonic-gate static int 5287c478bd9Sstevel@tonic-gate tsize_handler(char *optstr) 5297c478bd9Sstevel@tonic-gate { 5307c478bd9Sstevel@tonic-gate char *endp; 5317c478bd9Sstevel@tonic-gate longlong_t value; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate /* Make sure the option was requested */ 5347c478bd9Sstevel@tonic-gate if (tsize_set == B_FALSE) 5357c478bd9Sstevel@tonic-gate return (EOPTNEG); 5367c478bd9Sstevel@tonic-gate errno = 0; 5377c478bd9Sstevel@tonic-gate value = strtoll(optstr, &endp, 10); 5387c478bd9Sstevel@tonic-gate if (errno != 0 || value < 0 || *endp != '\0') 5397c478bd9Sstevel@tonic-gate return (EOPTNEG); 5407c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 32 5417c478bd9Sstevel@tonic-gate if (value > MAXOFF_T) 5427c478bd9Sstevel@tonic-gate return (ENOSPACE); 5437c478bd9Sstevel@tonic-gate #endif 5447c478bd9Sstevel@tonic-gate /* 5457c478bd9Sstevel@tonic-gate * Don't bother checking the tsize value we specified in a write 5467c478bd9Sstevel@tonic-gate * request is echoed back in the OACK. 5477c478bd9Sstevel@tonic-gate */ 5487c478bd9Sstevel@tonic-gate if (tsize == 0) 5497c478bd9Sstevel@tonic-gate tsize = value; 5507c478bd9Sstevel@tonic-gate return (-1); 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate /* 5547c478bd9Sstevel@tonic-gate * Add TFTP options to a request packet. 5557c478bd9Sstevel@tonic-gate */ 5567c478bd9Sstevel@tonic-gate static int 5577c478bd9Sstevel@tonic-gate add_options(char *obuf, char *obufend) 5587c478bd9Sstevel@tonic-gate { 5597c478bd9Sstevel@tonic-gate int i; 5607c478bd9Sstevel@tonic-gate char *cp, *ostr; 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate cp = obuf; 5637c478bd9Sstevel@tonic-gate for (i = 0; options[i].opt_name != NULL; i++) { 5647c478bd9Sstevel@tonic-gate ostr = options[i].opt_str(); 5657c478bd9Sstevel@tonic-gate if (ostr != NULL) { 5667c478bd9Sstevel@tonic-gate cp += strlcpy(cp, options[i].opt_name, obufend - cp) 5677c478bd9Sstevel@tonic-gate + 1; 5687c478bd9Sstevel@tonic-gate if (cp > obufend) 5697c478bd9Sstevel@tonic-gate return (-1); 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate cp += strlcpy(cp, ostr, obufend - cp) + 1; 5727c478bd9Sstevel@tonic-gate if (cp > obufend) 5737c478bd9Sstevel@tonic-gate return (-1); 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate } 5767c478bd9Sstevel@tonic-gate return (cp - obuf); 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate /* 5807c478bd9Sstevel@tonic-gate * Process OACK packet sent by server in response to options in the request 5817c478bd9Sstevel@tonic-gate * packet. Returns -1 on success or an error code on failure. 5827c478bd9Sstevel@tonic-gate */ 5837c478bd9Sstevel@tonic-gate static int 5847c478bd9Sstevel@tonic-gate process_oack(tftpbuf *oackbuf, int n) 5857c478bd9Sstevel@tonic-gate { 5867c478bd9Sstevel@tonic-gate char *cp, *oackend, *optname, *optval; 5877c478bd9Sstevel@tonic-gate struct tftphdr *oackp; 5887c478bd9Sstevel@tonic-gate int i, errcode; 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate oackp = &oackbuf->tb_hdr; 5917c478bd9Sstevel@tonic-gate cp = (char *)&oackp->th_stuff; 5927c478bd9Sstevel@tonic-gate oackend = (char *)oackbuf + n; 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate while (cp < oackend) { 5957c478bd9Sstevel@tonic-gate optname = cp; 5967c478bd9Sstevel@tonic-gate if ((optval = next_field(optname, oackend)) == NULL) 5977c478bd9Sstevel@tonic-gate return (EOPTNEG); 5987c478bd9Sstevel@tonic-gate if ((cp = next_field(optval, oackend)) == NULL) 5997c478bd9Sstevel@tonic-gate return (EOPTNEG); 6007c478bd9Sstevel@tonic-gate for (i = 0; options[i].opt_name != NULL; i++) { 6017c478bd9Sstevel@tonic-gate if (strcasecmp(optname, options[i].opt_name) == 0) 6027c478bd9Sstevel@tonic-gate break; 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate if (options[i].opt_name == NULL) 6057c478bd9Sstevel@tonic-gate return (EOPTNEG); 6067c478bd9Sstevel@tonic-gate errcode = options[i].opt_handler(optval); 6077c478bd9Sstevel@tonic-gate if (errcode >= 0) 6087c478bd9Sstevel@tonic-gate return (errcode); 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate return (-1); 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate /* 6147c478bd9Sstevel@tonic-gate * Send a nak packet (error message). 6157c478bd9Sstevel@tonic-gate * Error code passed in is one of the 6167c478bd9Sstevel@tonic-gate * standard TFTP codes, or a UNIX errno 6177c478bd9Sstevel@tonic-gate * offset by 100. 6187c478bd9Sstevel@tonic-gate */ 6197c478bd9Sstevel@tonic-gate static void 6207c478bd9Sstevel@tonic-gate nak(int error) 6217c478bd9Sstevel@tonic-gate { 6227c478bd9Sstevel@tonic-gate struct tftphdr *tp; 6237c478bd9Sstevel@tonic-gate int length; 6247c478bd9Sstevel@tonic-gate struct errmsg *pe; 6257c478bd9Sstevel@tonic-gate 6267c478bd9Sstevel@tonic-gate tp = &ackbuf.tb_hdr; 6277c478bd9Sstevel@tonic-gate tp->th_opcode = htons((ushort_t)ERROR); 6287c478bd9Sstevel@tonic-gate tp->th_code = htons((ushort_t)error); 6297c478bd9Sstevel@tonic-gate for (pe = errmsgs; pe->e_code >= 0; pe++) 6307c478bd9Sstevel@tonic-gate if (pe->e_code == error) 6317c478bd9Sstevel@tonic-gate break; 6327c478bd9Sstevel@tonic-gate if (pe->e_code < 0) { 6337c478bd9Sstevel@tonic-gate pe->e_msg = strerror(error - 100); 6347c478bd9Sstevel@tonic-gate tp->th_code = EUNDEF; 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate (void) strlcpy(tp->th_msg, pe->e_msg, 6377c478bd9Sstevel@tonic-gate sizeof (ackbuf) - sizeof (struct tftphdr)); 6387c478bd9Sstevel@tonic-gate length = strlen(pe->e_msg) + 4; 6397c478bd9Sstevel@tonic-gate if (trace) 6407c478bd9Sstevel@tonic-gate tpacket("sent", tp, length); 6417c478bd9Sstevel@tonic-gate if (sendto(f, ackbuf.tb_data, length, 0, 6427c478bd9Sstevel@tonic-gate (struct sockaddr *)&sin6, sizeof (sin6)) != length) 6437c478bd9Sstevel@tonic-gate perror("nak"); 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate static void 6477c478bd9Sstevel@tonic-gate tpacket(char *s, struct tftphdr *tp, int n) 6487c478bd9Sstevel@tonic-gate { 6497c478bd9Sstevel@tonic-gate static char *opcodes[] = \ 6507c478bd9Sstevel@tonic-gate { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" }; 6517c478bd9Sstevel@tonic-gate char *cp, *file, *mode; 6527c478bd9Sstevel@tonic-gate ushort_t op = ntohs(tp->th_opcode); 6537c478bd9Sstevel@tonic-gate char *tpend; 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate if (op < RRQ || op > OACK) 6567c478bd9Sstevel@tonic-gate (void) printf("%s opcode=%x ", s, op); 6577c478bd9Sstevel@tonic-gate else 6587c478bd9Sstevel@tonic-gate (void) printf("%s %s ", s, opcodes[op]); 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate switch (op) { 6617c478bd9Sstevel@tonic-gate case RRQ: 6627c478bd9Sstevel@tonic-gate case WRQ: 6637c478bd9Sstevel@tonic-gate tpend = (char *)tp + n; 6647c478bd9Sstevel@tonic-gate n -= sizeof (tp->th_opcode); 6657c478bd9Sstevel@tonic-gate file = (char *)&tp->th_stuff; 6667c478bd9Sstevel@tonic-gate if ((mode = next_field(file, tpend)) == NULL) { 6677c478bd9Sstevel@tonic-gate (void) printf("<file=%.*s>\n", n, file); 6687c478bd9Sstevel@tonic-gate break; 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate n -= mode - file; 6717c478bd9Sstevel@tonic-gate if ((cp = next_field(mode, tpend)) == NULL) { 6727c478bd9Sstevel@tonic-gate (void) printf("<file=%s, mode=%.*s>\n", file, n, mode); 6737c478bd9Sstevel@tonic-gate break; 6747c478bd9Sstevel@tonic-gate } 6757c478bd9Sstevel@tonic-gate (void) printf("<file=%s, mode=%s", file, mode); 6767c478bd9Sstevel@tonic-gate n -= cp - mode; 6777c478bd9Sstevel@tonic-gate if (n > 0) { 6787c478bd9Sstevel@tonic-gate (void) printf(", options: "); 6797c478bd9Sstevel@tonic-gate print_options(stdout, cp, n); 6807c478bd9Sstevel@tonic-gate } 6817c478bd9Sstevel@tonic-gate (void) puts(">"); 6827c478bd9Sstevel@tonic-gate break; 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate case DATA: 6857c478bd9Sstevel@tonic-gate (void) printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), 6867c478bd9Sstevel@tonic-gate n - sizeof (tp->th_opcode) - sizeof (tp->th_block)); 6877c478bd9Sstevel@tonic-gate break; 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate case ACK: 6907c478bd9Sstevel@tonic-gate (void) printf("<block=%d>\n", ntohs(tp->th_block)); 6917c478bd9Sstevel@tonic-gate break; 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate case OACK: 6947c478bd9Sstevel@tonic-gate (void) printf("<options: "); 6957c478bd9Sstevel@tonic-gate print_options(stdout, (char *)&tp->th_stuff, 6967c478bd9Sstevel@tonic-gate n - sizeof (tp->th_opcode)); 6977c478bd9Sstevel@tonic-gate (void) puts(">"); 6987c478bd9Sstevel@tonic-gate break; 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate case ERROR: 7017c478bd9Sstevel@tonic-gate (void) printf("<code=%d", ntohs(tp->th_code)); 7027c478bd9Sstevel@tonic-gate n = n - sizeof (tp->th_opcode) - sizeof (tp->th_code); 7037c478bd9Sstevel@tonic-gate if (n > 0) 7047c478bd9Sstevel@tonic-gate (void) printf(", msg=%.*s", n, tp->th_msg); 7057c478bd9Sstevel@tonic-gate (void) puts(">"); 7067c478bd9Sstevel@tonic-gate break; 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate static hrtime_t tstart, tstop; 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate static void 7137c478bd9Sstevel@tonic-gate startclock(void) 7147c478bd9Sstevel@tonic-gate { 7157c478bd9Sstevel@tonic-gate tstart = gethrtime(); 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate static void 7197c478bd9Sstevel@tonic-gate stopclock(void) 7207c478bd9Sstevel@tonic-gate { 7217c478bd9Sstevel@tonic-gate tstop = gethrtime(); 7227c478bd9Sstevel@tonic-gate } 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate static void 7257c478bd9Sstevel@tonic-gate printstats(char *direction, off_t amount) 7267c478bd9Sstevel@tonic-gate { 7277c478bd9Sstevel@tonic-gate hrtime_t delta, tenths; 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate delta = tstop - tstart; 7307c478bd9Sstevel@tonic-gate tenths = delta / (NANOSEC / 10); 7317c478bd9Sstevel@tonic-gate (void) printf("%s " OFF_T_FMT " bytes in %" PRId64 ".%" PRId64 7327c478bd9Sstevel@tonic-gate " seconds", direction, amount, tenths / 10, tenths % 10); 7337c478bd9Sstevel@tonic-gate if (verbose) 7347c478bd9Sstevel@tonic-gate (void) printf(" [%" PRId64 " bits/sec]\n", 7357c478bd9Sstevel@tonic-gate ((hrtime_t)amount * 8 * NANOSEC) / delta); 7367c478bd9Sstevel@tonic-gate else 7377c478bd9Sstevel@tonic-gate (void) putchar('\n'); 7387c478bd9Sstevel@tonic-gate } 739