19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 346dfc2060SDavid Malone #if 0 359b50d902SRodney W. Grimes #ifndef lint 366dfc2060SDavid Malone static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; 376dfc2060SDavid Malone #endif /* not lint */ 38fd129a02SPhilippe Charnier #endif 399b50d902SRodney W. Grimes 406dfc2060SDavid Malone #include <sys/cdefs.h> 416dfc2060SDavid Malone __FBSDID("$FreeBSD$"); 426dfc2060SDavid Malone 439b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes /* 469b50d902SRodney W. Grimes * TFTP User Program -- Protocol Machines 479b50d902SRodney W. Grimes */ 489b50d902SRodney W. Grimes #include <sys/types.h> 499b50d902SRodney W. Grimes #include <sys/socket.h> 509b50d902SRodney W. Grimes #include <sys/time.h> 519b50d902SRodney W. Grimes 529b50d902SRodney W. Grimes #include <netinet/in.h> 539b50d902SRodney W. Grimes 546dfc2060SDavid Malone #include <arpa/inet.h> 559b50d902SRodney W. Grimes #include <arpa/tftp.h> 569b50d902SRodney W. Grimes 57fd129a02SPhilippe Charnier #include <err.h> 589b50d902SRodney W. Grimes #include <errno.h> 599b50d902SRodney W. Grimes #include <setjmp.h> 609b50d902SRodney W. Grimes #include <signal.h> 619b50d902SRodney W. Grimes #include <stdio.h> 625b116430SJohn Birrell #include <string.h> 639b50d902SRodney W. Grimes #include <unistd.h> 649b50d902SRodney W. Grimes 659b50d902SRodney W. Grimes #include "extern.h" 669b50d902SRodney W. Grimes #include "tftpsubs.h" 679b50d902SRodney W. Grimes 689b50d902SRodney W. Grimes extern struct sockaddr_in peeraddr; /* filled in by main */ 699b50d902SRodney W. Grimes extern int f; /* the opened socket */ 709b50d902SRodney W. Grimes extern int trace; 719b50d902SRodney W. Grimes extern int verbose; 729b50d902SRodney W. Grimes extern int rexmtval; 739b50d902SRodney W. Grimes extern int maxtimeout; 749b50d902SRodney W. Grimes 759b50d902SRodney W. Grimes #define PKTSIZE SEGSIZE+4 769b50d902SRodney W. Grimes char ackbuf[PKTSIZE]; 779b50d902SRodney W. Grimes int timeout; 789b50d902SRodney W. Grimes jmp_buf toplevel; 799b50d902SRodney W. Grimes jmp_buf timeoutbuf; 809b50d902SRodney W. Grimes 813f330d7dSWarner Losh static void nak(int); 823f330d7dSWarner Losh static int makerequest(int, const char *, struct tftphdr *, const char *); 833f330d7dSWarner Losh static void printstats(const char *, unsigned long); 843f330d7dSWarner Losh static void startclock(void); 853f330d7dSWarner Losh static void stopclock(void); 863f330d7dSWarner Losh static void timer(int); 873f330d7dSWarner Losh static void tpacket(const char *, struct tftphdr *, int); 889b50d902SRodney W. Grimes 899b50d902SRodney W. Grimes /* 909b50d902SRodney W. Grimes * Send the requested file. 919b50d902SRodney W. Grimes */ 929b50d902SRodney W. Grimes void 938692ad46SDavid Greenman xmitfile(fd, name, mode) 949b50d902SRodney W. Grimes int fd; 959b50d902SRodney W. Grimes char *name; 969b50d902SRodney W. Grimes char *mode; 979b50d902SRodney W. Grimes { 988049f797SMark Murray struct tftphdr *ap; /* data and ack packets */ 998049f797SMark Murray struct tftphdr *dp; 1008049f797SMark Murray int n; 10167034ac6SJeroen Ruigrok van der Werven volatile unsigned short block; 10267034ac6SJeroen Ruigrok van der Werven volatile int size, convert; 1039b50d902SRodney W. Grimes volatile unsigned long amount; 1049b50d902SRodney W. Grimes struct sockaddr_in from; 1059b50d902SRodney W. Grimes int fromlen; 1069b50d902SRodney W. Grimes FILE *file; 1079b50d902SRodney W. Grimes 1089b50d902SRodney W. Grimes startclock(); /* start stat's clock */ 1099b50d902SRodney W. Grimes dp = r_init(); /* reset fillbuf/read-ahead code */ 1109b50d902SRodney W. Grimes ap = (struct tftphdr *)ackbuf; 1119b50d902SRodney W. Grimes file = fdopen(fd, "r"); 1129b50d902SRodney W. Grimes convert = !strcmp(mode, "netascii"); 1139b50d902SRodney W. Grimes block = 0; 1149b50d902SRodney W. Grimes amount = 0; 1159b50d902SRodney W. Grimes 1169b50d902SRodney W. Grimes signal(SIGALRM, timer); 1179b50d902SRodney W. Grimes do { 1189b50d902SRodney W. Grimes if (block == 0) 1199b50d902SRodney W. Grimes size = makerequest(WRQ, name, dp, mode) - 4; 1209b50d902SRodney W. Grimes else { 1219b50d902SRodney W. Grimes /* size = read(fd, dp->th_data, SEGSIZE); */ 1229b50d902SRodney W. Grimes size = readit(file, &dp, convert); 1239b50d902SRodney W. Grimes if (size < 0) { 1249b50d902SRodney W. Grimes nak(errno + 100); 1259b50d902SRodney W. Grimes break; 1269b50d902SRodney W. Grimes } 1279b50d902SRodney W. Grimes dp->th_opcode = htons((u_short)DATA); 1289b50d902SRodney W. Grimes dp->th_block = htons((u_short)block); 1299b50d902SRodney W. Grimes } 1309b50d902SRodney W. Grimes timeout = 0; 1319b50d902SRodney W. Grimes (void) setjmp(timeoutbuf); 1329b50d902SRodney W. Grimes send_data: 1339b50d902SRodney W. Grimes if (trace) 1349b50d902SRodney W. Grimes tpacket("sent", dp, size + 4); 1359b50d902SRodney W. Grimes n = sendto(f, dp, size + 4, 0, 1369b50d902SRodney W. Grimes (struct sockaddr *)&peeraddr, sizeof(peeraddr)); 1379b50d902SRodney W. Grimes if (n != size + 4) { 138fd129a02SPhilippe Charnier warn("sendto"); 1399b50d902SRodney W. Grimes goto abort; 1409b50d902SRodney W. Grimes } 1419b50d902SRodney W. Grimes read_ahead(file, convert); 1429b50d902SRodney W. Grimes for ( ; ; ) { 1439b50d902SRodney W. Grimes alarm(rexmtval); 1449b50d902SRodney W. Grimes do { 1459b50d902SRodney W. Grimes fromlen = sizeof(from); 1469b50d902SRodney W. Grimes n = recvfrom(f, ackbuf, sizeof(ackbuf), 0, 1479b50d902SRodney W. Grimes (struct sockaddr *)&from, &fromlen); 1489b50d902SRodney W. Grimes } while (n <= 0); 1499b50d902SRodney W. Grimes alarm(0); 1509b50d902SRodney W. Grimes if (n < 0) { 151fd129a02SPhilippe Charnier warn("recvfrom"); 1529b50d902SRodney W. Grimes goto abort; 1539b50d902SRodney W. Grimes } 1549b50d902SRodney W. Grimes peeraddr.sin_port = from.sin_port; /* added */ 1559b50d902SRodney W. Grimes if (trace) 1569b50d902SRodney W. Grimes tpacket("received", ap, n); 1579b50d902SRodney W. Grimes /* should verify packet came from server */ 1589b50d902SRodney W. Grimes ap->th_opcode = ntohs(ap->th_opcode); 1599b50d902SRodney W. Grimes ap->th_block = ntohs(ap->th_block); 1609b50d902SRodney W. Grimes if (ap->th_opcode == ERROR) { 1619b50d902SRodney W. Grimes printf("Error code %d: %s\n", ap->th_code, 1629b50d902SRodney W. Grimes ap->th_msg); 1639b50d902SRodney W. Grimes goto abort; 1649b50d902SRodney W. Grimes } 1659b50d902SRodney W. Grimes if (ap->th_opcode == ACK) { 1669b50d902SRodney W. Grimes int j; 1679b50d902SRodney W. Grimes 1689b50d902SRodney W. Grimes if (ap->th_block == block) { 1699b50d902SRodney W. Grimes break; 1709b50d902SRodney W. Grimes } 1719b50d902SRodney W. Grimes /* On an error, try to synchronize 1729b50d902SRodney W. Grimes * both sides. 1739b50d902SRodney W. Grimes */ 1749b50d902SRodney W. Grimes j = synchnet(f); 1759b50d902SRodney W. Grimes if (j && trace) { 1769b50d902SRodney W. Grimes printf("discarded %d packets\n", 1779b50d902SRodney W. Grimes j); 1789b50d902SRodney W. Grimes } 1799b50d902SRodney W. Grimes if (ap->th_block == (block-1)) { 1809b50d902SRodney W. Grimes goto send_data; 1819b50d902SRodney W. Grimes } 1829b50d902SRodney W. Grimes } 1839b50d902SRodney W. Grimes } 1849b50d902SRodney W. Grimes if (block > 0) 1859b50d902SRodney W. Grimes amount += size; 1869b50d902SRodney W. Grimes block++; 1879b50d902SRodney W. Grimes } while (size == SEGSIZE || block == 1); 1889b50d902SRodney W. Grimes abort: 1899b50d902SRodney W. Grimes fclose(file); 1909b50d902SRodney W. Grimes stopclock(); 1919b50d902SRodney W. Grimes if (amount > 0) 1929b50d902SRodney W. Grimes printstats("Sent", amount); 1939b50d902SRodney W. Grimes } 1949b50d902SRodney W. Grimes 1959b50d902SRodney W. Grimes /* 1969b50d902SRodney W. Grimes * Receive a file. 1979b50d902SRodney W. Grimes */ 1989b50d902SRodney W. Grimes void 1999b50d902SRodney W. Grimes recvfile(fd, name, mode) 2009b50d902SRodney W. Grimes int fd; 2019b50d902SRodney W. Grimes char *name; 2029b50d902SRodney W. Grimes char *mode; 2039b50d902SRodney W. Grimes { 2048049f797SMark Murray struct tftphdr *ap; 2058049f797SMark Murray struct tftphdr *dp; 2068049f797SMark Murray int n; 20767034ac6SJeroen Ruigrok van der Werven volatile unsigned short block; 20867034ac6SJeroen Ruigrok van der Werven volatile int size, firsttrip; 2099b50d902SRodney W. Grimes volatile unsigned long amount; 2109b50d902SRodney W. Grimes struct sockaddr_in from; 2119b50d902SRodney W. Grimes int fromlen; 2129b50d902SRodney W. Grimes FILE *file; 2139b50d902SRodney W. Grimes volatile int convert; /* true if converting crlf -> lf */ 2149b50d902SRodney W. Grimes 2159b50d902SRodney W. Grimes startclock(); 2169b50d902SRodney W. Grimes dp = w_init(); 2179b50d902SRodney W. Grimes ap = (struct tftphdr *)ackbuf; 2189b50d902SRodney W. Grimes file = fdopen(fd, "w"); 2199b50d902SRodney W. Grimes convert = !strcmp(mode, "netascii"); 2209b50d902SRodney W. Grimes block = 1; 2219b50d902SRodney W. Grimes firsttrip = 1; 2229b50d902SRodney W. Grimes amount = 0; 2239b50d902SRodney W. Grimes 2249b50d902SRodney W. Grimes signal(SIGALRM, timer); 2259b50d902SRodney W. Grimes do { 2269b50d902SRodney W. Grimes if (firsttrip) { 2279b50d902SRodney W. Grimes size = makerequest(RRQ, name, ap, mode); 2289b50d902SRodney W. Grimes firsttrip = 0; 2299b50d902SRodney W. Grimes } else { 2309b50d902SRodney W. Grimes ap->th_opcode = htons((u_short)ACK); 2319b50d902SRodney W. Grimes ap->th_block = htons((u_short)(block)); 2329b50d902SRodney W. Grimes size = 4; 2339b50d902SRodney W. Grimes block++; 2349b50d902SRodney W. Grimes } 2359b50d902SRodney W. Grimes timeout = 0; 2369b50d902SRodney W. Grimes (void) setjmp(timeoutbuf); 2379b50d902SRodney W. Grimes send_ack: 2389b50d902SRodney W. Grimes if (trace) 2399b50d902SRodney W. Grimes tpacket("sent", ap, size); 2409b50d902SRodney W. Grimes if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr, 2419b50d902SRodney W. Grimes sizeof(peeraddr)) != size) { 2429b50d902SRodney W. Grimes alarm(0); 243fd129a02SPhilippe Charnier warn("sendto"); 2449b50d902SRodney W. Grimes goto abort; 2459b50d902SRodney W. Grimes } 2469b50d902SRodney W. Grimes write_behind(file, convert); 2479b50d902SRodney W. Grimes for ( ; ; ) { 2489b50d902SRodney W. Grimes alarm(rexmtval); 2499b50d902SRodney W. Grimes do { 2509b50d902SRodney W. Grimes fromlen = sizeof(from); 2519b50d902SRodney W. Grimes n = recvfrom(f, dp, PKTSIZE, 0, 2529b50d902SRodney W. Grimes (struct sockaddr *)&from, &fromlen); 2539b50d902SRodney W. Grimes } while (n <= 0); 2549b50d902SRodney W. Grimes alarm(0); 2559b50d902SRodney W. Grimes if (n < 0) { 256fd129a02SPhilippe Charnier warn("recvfrom"); 2579b50d902SRodney W. Grimes goto abort; 2589b50d902SRodney W. Grimes } 2599b50d902SRodney W. Grimes peeraddr.sin_port = from.sin_port; /* added */ 2609b50d902SRodney W. Grimes if (trace) 2619b50d902SRodney W. Grimes tpacket("received", dp, n); 2629b50d902SRodney W. Grimes /* should verify client address */ 2639b50d902SRodney W. Grimes dp->th_opcode = ntohs(dp->th_opcode); 2649b50d902SRodney W. Grimes dp->th_block = ntohs(dp->th_block); 2659b50d902SRodney W. Grimes if (dp->th_opcode == ERROR) { 2669b50d902SRodney W. Grimes printf("Error code %d: %s\n", dp->th_code, 2679b50d902SRodney W. Grimes dp->th_msg); 2689b50d902SRodney W. Grimes goto abort; 2699b50d902SRodney W. Grimes } 2709b50d902SRodney W. Grimes if (dp->th_opcode == DATA) { 2719b50d902SRodney W. Grimes int j; 2729b50d902SRodney W. Grimes 2739b50d902SRodney W. Grimes if (dp->th_block == block) { 2749b50d902SRodney W. Grimes break; /* have next packet */ 2759b50d902SRodney W. Grimes } 2769b50d902SRodney W. Grimes /* On an error, try to synchronize 2779b50d902SRodney W. Grimes * both sides. 2789b50d902SRodney W. Grimes */ 2799b50d902SRodney W. Grimes j = synchnet(f); 2809b50d902SRodney W. Grimes if (j && trace) { 2819b50d902SRodney W. Grimes printf("discarded %d packets\n", j); 2829b50d902SRodney W. Grimes } 2839b50d902SRodney W. Grimes if (dp->th_block == (block-1)) { 2849b50d902SRodney W. Grimes goto send_ack; /* resend ack */ 2859b50d902SRodney W. Grimes } 2869b50d902SRodney W. Grimes } 2879b50d902SRodney W. Grimes } 2889b50d902SRodney W. Grimes /* size = write(fd, dp->th_data, n - 4); */ 2899b50d902SRodney W. Grimes size = writeit(file, &dp, n - 4, convert); 2909b50d902SRodney W. Grimes if (size < 0) { 2919b50d902SRodney W. Grimes nak(errno + 100); 2929b50d902SRodney W. Grimes break; 2939b50d902SRodney W. Grimes } 2949b50d902SRodney W. Grimes amount += size; 2959b50d902SRodney W. Grimes } while (size == SEGSIZE); 2969b50d902SRodney W. Grimes abort: /* ok to ack, since user */ 2979b50d902SRodney W. Grimes ap->th_opcode = htons((u_short)ACK); /* has seen err msg */ 2989b50d902SRodney W. Grimes ap->th_block = htons((u_short)block); 2999b50d902SRodney W. Grimes (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr, 3009b50d902SRodney W. Grimes sizeof(peeraddr)); 3019b50d902SRodney W. Grimes write_behind(file, convert); /* flush last buffer */ 3029b50d902SRodney W. Grimes fclose(file); 3039b50d902SRodney W. Grimes stopclock(); 3049b50d902SRodney W. Grimes if (amount > 0) 3059b50d902SRodney W. Grimes printstats("Received", amount); 3069b50d902SRodney W. Grimes } 3079b50d902SRodney W. Grimes 3089b50d902SRodney W. Grimes static int 3099b50d902SRodney W. Grimes makerequest(request, name, tp, mode) 3109b50d902SRodney W. Grimes int request; 3119b50d902SRodney W. Grimes const char *name; 3129b50d902SRodney W. Grimes struct tftphdr *tp; 3139b50d902SRodney W. Grimes const char *mode; 3149b50d902SRodney W. Grimes { 3158049f797SMark Murray char *cp; 3169b50d902SRodney W. Grimes 3179b50d902SRodney W. Grimes tp->th_opcode = htons((u_short)request); 3189b50d902SRodney W. Grimes cp = tp->th_stuff; 3199b50d902SRodney W. Grimes strcpy(cp, name); 3209b50d902SRodney W. Grimes cp += strlen(name); 3219b50d902SRodney W. Grimes *cp++ = '\0'; 3229b50d902SRodney W. Grimes strcpy(cp, mode); 3239b50d902SRodney W. Grimes cp += strlen(mode); 3249b50d902SRodney W. Grimes *cp++ = '\0'; 3259b50d902SRodney W. Grimes return (cp - (char *)tp); 3269b50d902SRodney W. Grimes } 3279b50d902SRodney W. Grimes 3289b50d902SRodney W. Grimes struct errmsg { 3299b50d902SRodney W. Grimes int e_code; 3308049f797SMark Murray const char *e_msg; 3319b50d902SRodney W. Grimes } errmsgs[] = { 3329b50d902SRodney W. Grimes { EUNDEF, "Undefined error code" }, 3339b50d902SRodney W. Grimes { ENOTFOUND, "File not found" }, 3349b50d902SRodney W. Grimes { EACCESS, "Access violation" }, 3359b50d902SRodney W. Grimes { ENOSPACE, "Disk full or allocation exceeded" }, 3369b50d902SRodney W. Grimes { EBADOP, "Illegal TFTP operation" }, 3379b50d902SRodney W. Grimes { EBADID, "Unknown transfer ID" }, 3389b50d902SRodney W. Grimes { EEXISTS, "File already exists" }, 3399b50d902SRodney W. Grimes { ENOUSER, "No such user" }, 3409b50d902SRodney W. Grimes { -1, 0 } 3419b50d902SRodney W. Grimes }; 3429b50d902SRodney W. Grimes 3439b50d902SRodney W. Grimes /* 3449b50d902SRodney W. Grimes * Send a nak packet (error message). 3459b50d902SRodney W. Grimes * Error code passed in is one of the 3469b50d902SRodney W. Grimes * standard TFTP codes, or a UNIX errno 3479b50d902SRodney W. Grimes * offset by 100. 3489b50d902SRodney W. Grimes */ 3499b50d902SRodney W. Grimes static void 3509b50d902SRodney W. Grimes nak(error) 3519b50d902SRodney W. Grimes int error; 3529b50d902SRodney W. Grimes { 3538049f797SMark Murray struct errmsg *pe; 3548049f797SMark Murray struct tftphdr *tp; 3559b50d902SRodney W. Grimes int length; 3569b50d902SRodney W. Grimes 3579b50d902SRodney W. Grimes tp = (struct tftphdr *)ackbuf; 3589b50d902SRodney W. Grimes tp->th_opcode = htons((u_short)ERROR); 3599b50d902SRodney W. Grimes tp->th_code = htons((u_short)error); 3609b50d902SRodney W. Grimes for (pe = errmsgs; pe->e_code >= 0; pe++) 3619b50d902SRodney W. Grimes if (pe->e_code == error) 3629b50d902SRodney W. Grimes break; 3639b50d902SRodney W. Grimes if (pe->e_code < 0) { 3649b50d902SRodney W. Grimes pe->e_msg = strerror(error - 100); 3659b50d902SRodney W. Grimes tp->th_code = EUNDEF; 3669b50d902SRodney W. Grimes } 3679b50d902SRodney W. Grimes strcpy(tp->th_msg, pe->e_msg); 3689b50d902SRodney W. Grimes length = strlen(pe->e_msg) + 4; 3699b50d902SRodney W. Grimes if (trace) 3709b50d902SRodney W. Grimes tpacket("sent", tp, length); 3719b50d902SRodney W. Grimes if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&peeraddr, 3729b50d902SRodney W. Grimes sizeof(peeraddr)) != length) 373fd129a02SPhilippe Charnier warn("nak"); 3749b50d902SRodney W. Grimes } 3759b50d902SRodney W. Grimes 3769b50d902SRodney W. Grimes static void 3779b50d902SRodney W. Grimes tpacket(s, tp, n) 3789b50d902SRodney W. Grimes const char *s; 3799b50d902SRodney W. Grimes struct tftphdr *tp; 3809b50d902SRodney W. Grimes int n; 3819b50d902SRodney W. Grimes { 3828049f797SMark Murray static const char *opcodes[] = 3839b50d902SRodney W. Grimes { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" }; 3848049f797SMark Murray char *cp, *file; 3859b50d902SRodney W. Grimes u_short op = ntohs(tp->th_opcode); 3869b50d902SRodney W. Grimes 3879b50d902SRodney W. Grimes if (op < RRQ || op > ERROR) 3889b50d902SRodney W. Grimes printf("%s opcode=%x ", s, op); 3899b50d902SRodney W. Grimes else 3909b50d902SRodney W. Grimes printf("%s %s ", s, opcodes[op]); 3919b50d902SRodney W. Grimes switch (op) { 3929b50d902SRodney W. Grimes 3939b50d902SRodney W. Grimes case RRQ: 3949b50d902SRodney W. Grimes case WRQ: 3959b50d902SRodney W. Grimes n -= 2; 3969b50d902SRodney W. Grimes file = cp = tp->th_stuff; 3979b50d902SRodney W. Grimes cp = index(cp, '\0'); 3989b50d902SRodney W. Grimes printf("<file=%s, mode=%s>\n", file, cp + 1); 3999b50d902SRodney W. Grimes break; 4009b50d902SRodney W. Grimes 4019b50d902SRodney W. Grimes case DATA: 4029b50d902SRodney W. Grimes printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4); 4039b50d902SRodney W. Grimes break; 4049b50d902SRodney W. Grimes 4059b50d902SRodney W. Grimes case ACK: 4069b50d902SRodney W. Grimes printf("<block=%d>\n", ntohs(tp->th_block)); 4079b50d902SRodney W. Grimes break; 4089b50d902SRodney W. Grimes 4099b50d902SRodney W. Grimes case ERROR: 4109b50d902SRodney W. Grimes printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg); 4119b50d902SRodney W. Grimes break; 4129b50d902SRodney W. Grimes } 4139b50d902SRodney W. Grimes } 4149b50d902SRodney W. Grimes 4159b50d902SRodney W. Grimes struct timeval tstart; 4169b50d902SRodney W. Grimes struct timeval tstop; 4179b50d902SRodney W. Grimes 4189b50d902SRodney W. Grimes static void 4199b50d902SRodney W. Grimes startclock() 4209b50d902SRodney W. Grimes { 4219b50d902SRodney W. Grimes 4229b50d902SRodney W. Grimes (void)gettimeofday(&tstart, NULL); 4239b50d902SRodney W. Grimes } 4249b50d902SRodney W. Grimes 4259b50d902SRodney W. Grimes static void 4269b50d902SRodney W. Grimes stopclock() 4279b50d902SRodney W. Grimes { 4289b50d902SRodney W. Grimes 4299b50d902SRodney W. Grimes (void)gettimeofday(&tstop, NULL); 4309b50d902SRodney W. Grimes } 4319b50d902SRodney W. Grimes 4329b50d902SRodney W. Grimes static void 4339b50d902SRodney W. Grimes printstats(direction, amount) 4349b50d902SRodney W. Grimes const char *direction; 4359b50d902SRodney W. Grimes unsigned long amount; 4369b50d902SRodney W. Grimes { 4379b50d902SRodney W. Grimes double delta; 4389b50d902SRodney W. Grimes /* compute delta in 1/10's second units */ 4399b50d902SRodney W. Grimes delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) - 4409b50d902SRodney W. Grimes ((tstart.tv_sec*10.)+(tstart.tv_usec/100000)); 4419b50d902SRodney W. Grimes delta = delta/10.; /* back to seconds */ 4428049f797SMark Murray printf("%s %ld bytes in %.1f seconds", direction, amount, delta); 4439b50d902SRodney W. Grimes if (verbose) 4449b50d902SRodney W. Grimes printf(" [%.0f bits/sec]", (amount*8.)/delta); 4459b50d902SRodney W. Grimes putchar('\n'); 4469b50d902SRodney W. Grimes } 4479b50d902SRodney W. Grimes 4489b50d902SRodney W. Grimes static void 4499b50d902SRodney W. Grimes timer(sig) 4508049f797SMark Murray int sig __unused; 4519b50d902SRodney W. Grimes { 4529b50d902SRodney W. Grimes 4539b50d902SRodney W. Grimes timeout += rexmtval; 4549b50d902SRodney W. Grimes if (timeout >= maxtimeout) { 4559b50d902SRodney W. Grimes printf("Transfer timed out.\n"); 4569b50d902SRodney W. Grimes longjmp(toplevel, -1); 4579b50d902SRodney W. Grimes } 4589b50d902SRodney W. Grimes longjmp(timeoutbuf, 1); 4599b50d902SRodney W. Grimes } 460