fix print format for garbage packets (from mogul@stanford)
[unix-history] / usr / src / usr.bin / tftp / tftp.c
index 4fe9c10..e68357a 100644 (file)
@@ -1,4 +1,6 @@
-/*     tftp.c  4.3     82/11/14        */
+#ifndef lint
+static char sccsid[] = "@(#)tftp.c     4.9 (Berkeley) %G%";
+#endif
 
 /*
  * TFTP User Program -- Protocol Machines
 
 /*
  * TFTP User Program -- Protocol Machines
@@ -7,33 +9,37 @@
 #include <sys/socket.h>
 
 #include <netinet/in.h>
 #include <sys/socket.h>
 
 #include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/tftp.h>
 
 #include <signal.h>
 #include <stdio.h>
 #include <errno.h>
 #include <setjmp.h>
 
 #include <signal.h>
 #include <stdio.h>
 #include <errno.h>
 #include <setjmp.h>
-
-#include "tftp.h"
+#include <netdb.h>
 
 extern int errno;
 extern struct sockaddr_in sin;
 extern char mode[];
 int    f;
 int    trace;
 
 extern int errno;
 extern struct sockaddr_in sin;
 extern char mode[];
 int    f;
 int    trace;
-int    verbose;
 int    connected;
 char   buf[BUFSIZ];
 int    connected;
 char   buf[BUFSIZ];
+int    rexmtval;
+int    maxtimeout;
 int    timeout;
 jmp_buf        toplevel;
 int    timeout;
 jmp_buf        toplevel;
+jmp_buf        timeoutbuf;
 
 timer()
 {
 
 timer()
 {
-       timeout += TIMEOUT;
-       if (timeout >= MAXTIMEOUT) {
+
+       timeout += rexmtval;
+       if (timeout >= maxtimeout) {
                printf("Transfer timed out.\n");
                longjmp(toplevel, -1);
        }
                printf("Transfer timed out.\n");
                longjmp(toplevel, -1);
        }
-       alarm(TIMEOUT);
+       longjmp(timeoutbuf, 1);
 }
 
 /*
 }
 
 /*
@@ -45,70 +51,81 @@ sendfile(fd, name)
 {
        register struct tftphdr *tp = (struct tftphdr *)buf;
        register int block = 0, size, n, amount = 0;
 {
        register struct tftphdr *tp = (struct tftphdr *)buf;
        register int block = 0, size, n, amount = 0;
-       struct sockaddr_in from;
+       struct sockaddr_in from, to;
        time_t start = time(0), delta;
        time_t start = time(0), delta;
-       int fromlen;
+       int fromlen, aborted = 0;
 
 
-       size = makerequest(WRQ, name) - 4;
-       timeout = 0;
-       sigset(SIGALRM, timer);
+       to = sin;
+       signal(SIGALRM, timer);
        do {
        do {
-               if (block != 0) {
+               if (block == 0)
+                       size = makerequest(WRQ, name) - 4;
+               else {
                        size = read(fd, tp->th_data, SEGSIZE);
                        if (size < 0) {
                        size = read(fd, tp->th_data, SEGSIZE);
                        if (size < 0) {
-                               nak(errno + 100);
+                               nak(&to, errno + 100);
                                break;
                        }
                        tp->th_opcode = htons((u_short)DATA);
                        tp->th_block = htons((u_short)block);
                }
                timeout = 0;
                                break;
                        }
                        tp->th_opcode = htons((u_short)DATA);
                        tp->th_block = htons((u_short)block);
                }
                timeout = 0;
-               alarm(TIMEOUT);
-rexmt:
+               (void) setjmp(timeoutbuf);
                if (trace)
                if (trace)
-                       tpacket("sent", tp, size + 4);
-               n = sendto(f, buf, size + 4, (caddr_t)&sin, sizeof (sin), 0);
+                       tpacket("sent", &to, tp, size + 4);
+               n = sendto(f, buf, size + 4, 0, (caddr_t)&to, sizeof (to));
                if (n != size + 4) {
                if (n != size + 4) {
-                       perror("send");
-                       break;
+                       perror("tftp: sendto");
+                       aborted = 1;
+                       goto done;
                }
                }
+               do {
 again:
 again:
-               fromlen = sizeof (from);
-               n = recvfrom(f, buf, sizeof (buf), (caddr_t)&from, &fromlen, 0);
-               if (n <= 0) {
-                       if (n == 0)
-                               goto again;
-                       if (errno == EINTR)
-                               goto rexmt;
+                       alarm(rexmtval);
+                       do {
+                               fromlen = sizeof (from);
+                               n = recvfrom(f, buf, sizeof (buf), 0,
+                                   (caddr_t)&from, &fromlen);
+                       } while (n <= 0);
                        alarm(0);
                        alarm(0);
-                       perror("receive");
-                       break;
-               }
-               alarm(0);
-               if (trace)
-                       tpacket("received", tp, n);
-               /* should verify packet came from server */
-#if vax || pdp11
-               tp->th_opcode = ntohs(tp->th_opcode);
-               tp->th_block = ntohs(tp->th_block);
-#endif
-               if (tp->th_opcode == ERROR) {
-                       printf("Error code %d: %s\n", tp->th_code,
-                               tp->th_msg);
-                       break;
-               }
-               if (tp->th_opcode != ACK || block != tp->th_block)
-                       goto again;
+                       if (n < 0) {
+                               perror("tftp: recvfrom");
+                               aborted = 1;
+                               goto done;
+                       }
+                       if (to.sin_addr.s_addr != from.sin_addr.s_addr) {
+                               tpacket("discarded (wrong host)", &from, tp, n);
+                               goto again;
+                       }
+                       if (to.sin_port = sin.sin_port)
+                               to.sin_port = from.sin_port;
+                       if (to.sin_port != from.sin_port) {
+                               tpacket("discarded (wrong port)", &from, tp, n);
+                               goto again;
+                       }
+                       if (trace)
+                               tpacket("received", &from, tp, n);
+                       /* should verify packet came from server */
+                       tp->th_opcode = ntohs(tp->th_opcode);
+                       tp->th_block = ntohs(tp->th_block);
+                       if (tp->th_opcode == ERROR) {
+                               printf("Error code %d: %s\n", tp->th_code,
+                                       tp->th_msg);
+                               aborted = 1;
+                               goto done;
+                       }
+               } while (tp->th_opcode != ACK && block != tp->th_block);
                if (block > 0)
                        amount += size;
                block++;
        } while (size == SEGSIZE || block == 1);
                if (block > 0)
                        amount += size;
                block++;
        } while (size == SEGSIZE || block == 1);
-       alarm(0);
-       (void) close(fd);
-       if (amount > 0) {
+       if (!aborted && amount > 0) {
                delta = time(0) - start;
                printf("Sent %d bytes in %d seconds.\n", amount, delta);
        }
                delta = time(0) - start;
                printf("Sent %d bytes in %d seconds.\n", amount, delta);
        }
+done:
+       (void) close(fd);
+       return (aborted);
 }
 
 /*
 }
 
 /*
@@ -120,71 +137,87 @@ recvfile(fd, name)
 {
        register struct tftphdr *tp = (struct tftphdr *)buf;
        register int block = 1, n, size, amount = 0;
 {
        register struct tftphdr *tp = (struct tftphdr *)buf;
        register int block = 1, n, size, amount = 0;
-       struct sockaddr_in from;
+       struct sockaddr_in from, to;
        time_t start = time(0), delta;
        time_t start = time(0), delta;
-       int fromlen;
+       int fromlen, firsttrip = 1, aborted = 0;
 
 
-       size = makerequest(RRQ, name);
-       timeout = 0;
-       sigset(SIGALRM, timer);
-       alarm(TIMEOUT);
-       goto rexmt;
+       to = sin;
+       signal(SIGALRM, timer);
        do {
        do {
+               if (firsttrip) {
+                       size = makerequest(RRQ, name);
+                       firsttrip = 0;
+               } else {
+                       tp->th_opcode = htons((u_short)ACK);
+                       tp->th_block = htons((u_short)(block));
+                       size = 4;
+                       block++;
+               }
                timeout = 0;
                timeout = 0;
-               alarm(TIMEOUT);
-               tp->th_opcode = htons((u_short)ACK);
-               tp->th_block = htons((u_short)(block));
-               size = 4;
-               block++;
-rexmt:
+               (void) setjmp(timeoutbuf);
                if (trace)
                if (trace)
-                       tpacket("sent", tp, size);
-               if (sendto(f, buf, size, (caddr_t)&sin, sizeof (sin), 0) != size) {
-                       perror("send");
-                       break;
+                       tpacket("sent", &to, tp, size);
+               if (sendto(f, buf, size, 0, (caddr_t)&to,
+                   sizeof (to)) != size) {
+                       alarm(0);
+                       perror("tftp: sendto");
+                       aborted = 1;
+                       goto done;
                }
                }
+               do {
 again:
 again:
-               n = recvfrom(f, buf, sizeof (buf), (caddr_t)&from, &fromlen, 0);
-               if (n <= 0) {
-                       if (n == 0)
-                               goto again;
-                       if (errno == EINTR)
-                               goto rexmt;
+                       alarm(rexmtval);
+                       do {
+                               fromlen = sizeof (from);
+                               n = recvfrom(f, buf, sizeof (buf), 0,
+                                   (caddr_t)&from, &fromlen);
+                       } while (n <= 0);
                        alarm(0);
                        alarm(0);
-                       perror("receive");
-                       break;
-               }
-               alarm(0);
-               if (trace)
-                       tpacket("received", tp, n);
-               /* should verify client address */
-#if vax || pdp11
-               tp->th_opcode = ntohs(tp->th_opcode);
-               tp->th_block = ntohs(tp->th_block);
-#endif
-               if (tp->th_opcode == ERROR) {
-                       printf("Error code %d: %s\n", tp->th_code,
-                               tp->th_msg);
-                       break;
-               }
-               if (tp->th_opcode != DATA || block != tp->th_block)
-                       goto again;
+                       if (n < 0) {
+                               perror("tftp: recvfrom");
+                               aborted = 1;
+                               goto done;
+                       }
+                       if (to.sin_addr.s_addr != from.sin_addr.s_addr) {
+                               tpacket("discarded (wrong host)", &from, tp, n);
+                               goto again;
+                       }
+                       if (to.sin_port = sin.sin_port)
+                               to.sin_port = from.sin_port;
+                       if (to.sin_port != from.sin_port) {
+                               tpacket("discarded (wrong port)", &from, tp, n);
+                               goto again;
+                       }
+                       if (trace)
+                               tpacket("received", &from, tp, n);
+                       tp->th_opcode = ntohs(tp->th_opcode);
+                       tp->th_block = ntohs(tp->th_block);
+                       if (tp->th_opcode == ERROR) {
+                               printf("Error code %d: %s\n", tp->th_code,
+                                       tp->th_msg);
+                               aborted = 1;
+                               goto done;
+                       }
+               } while (tp->th_opcode != DATA && tp->th_block != block);
                size = write(fd, tp->th_data, n - 4);
                if (size < 0) {
                size = write(fd, tp->th_data, n - 4);
                if (size < 0) {
-                       nak(errno + 100);
-                       break;
+                       perror("tftp: write");
+                       nak(&to, errno + 100);
+                       aborted = 1;
+                       goto done;
                }
                amount += size;
        } while (size == SEGSIZE);
                }
                amount += size;
        } while (size == SEGSIZE);
-       alarm(0);
+done:
        tp->th_opcode = htons((u_short)ACK);
        tp->th_block = htons((u_short)block);
        tp->th_opcode = htons((u_short)ACK);
        tp->th_block = htons((u_short)block);
-       (void) sendto(f, buf, 4, &sin, sizeof (sin), 0);
+       (void) sendto(f, buf, 4, 0, &to, sizeof (to));
        (void) close(fd);
        (void) close(fd);
-       if (amount > 0) {
+       if (!aborted && amount > 0) {
                delta = time(0) - start;
                printf("Received %d bytes in %d seconds.\n", amount, delta);
        }
                delta = time(0) - start;
                printf("Received %d bytes in %d seconds.\n", amount, delta);
        }
+       return (aborted);
 }
 
 makerequest(request, name)
 }
 
 makerequest(request, name)
@@ -228,7 +261,8 @@ struct errmsg {
  * standard TFTP codes, or a UNIX errno
  * offset by 100.
  */
  * standard TFTP codes, or a UNIX errno
  * offset by 100.
  */
-nak(error)
+nak(to, error)
+       struct sockaddr_in *to;
        int error;
 {
        register struct tftphdr *tp;
        int error;
 {
        register struct tftphdr *tp;
@@ -247,12 +281,13 @@ nak(error)
        strcpy(tp->th_msg, pe->e_msg);
        length = strlen(pe->e_msg) + 4;
        if (trace)
        strcpy(tp->th_msg, pe->e_msg);
        length = strlen(pe->e_msg) + 4;
        if (trace)
-               tpacket("sent", tp, length);
-       if (send(f, &sin, buf, length) != length)
-               perror("nak");
+               tpacket("sent", to, tp, length);
+       if (sendto(f, buf, length, 0, to, sizeof (*to)) != length)
+               perror("tftp: nak");
 }
 
 }
 
-tpacket(s, tp, n)
+tpacket(s, sin, tp, n)
+       struct sockaddr_in *sin;
        struct tftphdr *tp;
        int n;
 {
        struct tftphdr *tp;
        int n;
 {
@@ -262,10 +297,19 @@ tpacket(s, tp, n)
        u_short op = ntohs(tp->th_opcode);
        char *index();
 
        u_short op = ntohs(tp->th_opcode);
        char *index();
 
+       printf("%s ", s);
+       if (sin) {
+               struct hostent *hp = gethostbyaddr(&sin->sin_addr,
+                    sizeof (sin->sin_addr), AF_INET);
+
+               printf("%s.%d ",
+                   hp == 0 ? inet_ntoa(sin->sin_addr) : hp->h_name,
+                   ntohs(sin->sin_port));
+       }
        if (op < RRQ || op > ERROR)
        if (op < RRQ || op > ERROR)
-               printf("%s opcode=%x ", s, op);
+               printf("opcode=%x ", op);
        else
        else
-               printf("%s %s ", s, opcodes[op]);
+               printf("%s ", opcodes[op]);
        switch (op) {
 
        case RRQ:
        switch (op) {
 
        case RRQ:
@@ -287,5 +331,9 @@ tpacket(s, tp, n)
        case ERROR:
                printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
                break;
        case ERROR:
                printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
                break;
+
+       default:
+               putchar('\n');
+               break;
        }
 }
        }
 }