home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / print-tftp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-19  |  3.8 KB  |  150 lines

  1. /*
  2.  * Copyright (c) 1988-1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * Format and print trivial file transfer protocol packets.
  22.  */
  23. #ifndef lint
  24. static char rcsid[] =
  25.     "@(#) $Header: print-tftp.c,v 1.13 91/04/19 10:46:57 mccanne Exp $ (LBL)";
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include <sys/param.h>
  30. #include <sys/types.h>
  31. #include <arpa/tftp.h>
  32.  
  33. #include "interface.h"
  34. #include <strings.h>
  35. #include <ctype.h>
  36.  
  37. struct int2str {
  38.     int code;
  39.     char *str;
  40. };
  41.  
  42. /* op code to string mapping */
  43. static struct int2str op2str[] = {
  44.     RRQ, "RRQ",            /* read request */
  45.     WRQ, "WRQ",            /* write request */
  46.     DATA, "DATA",            /* data packet */
  47.     ACK, "ACK",            /* acknowledgement */
  48.     ERROR, "ERROR",            /* error code */
  49.     0, 0
  50. };
  51.  
  52. /* error code to string mapping */
  53. static struct int2str err2str[] = {
  54.     EUNDEF, "EUNDEF",        /* not defined */
  55.     ENOTFOUND, "ENOTFOUND",        /* file not found */
  56.     EACCESS, "EACCESS",        /* access violation */
  57.     ENOSPACE, "ENOSPACE",        /* disk full or allocation exceeded *?
  58.     EBADOP, "EBADOP",        /* illegal TFTP operation */
  59.     EBADID, "EBADID",        /* unknown transfer ID */
  60.     EEXISTS, "EEXISTS",        /* file already exists */
  61.     ENOUSER, "ENOUSER",        /* no such user */
  62.     0, 0
  63. };
  64.  
  65. /*
  66.  * Print trivial file transfer program requests
  67.  */
  68. void
  69. tftp_print(tp, length)
  70.     register struct tftphdr *tp;
  71.     int length;
  72. {
  73.     register struct int2str *ts;
  74.     register u_char *ep;
  75. #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
  76.     static char tstr[] = " [|tftp]";
  77.  
  78.     /* 'ep' points to the end of avaible data. */
  79.     ep = (u_char *)snapend;
  80.  
  81.     /* Print length */
  82.     printf(" %d", length);
  83.  
  84.     /* Print tftp request type */
  85.     TCHECK(tp->th_opcode, sizeof(tp->th_opcode));
  86.     NTOHS(tp->th_opcode);
  87.     putchar(' ');
  88.     for (ts = op2str; ts->str; ++ts)
  89.         if (ts->code == tp->th_opcode) {
  90.             fputs(ts->str, stdout);
  91.             break;
  92.         }
  93.     if (ts->str == 0) {
  94.         /* Bail if bogus opcode */
  95.         printf("tftp-#%d", tp->th_opcode);
  96.         return;
  97.     }
  98.  
  99.     switch (tp->th_opcode) {
  100.  
  101.     case RRQ:
  102.     case WRQ:
  103.         putchar(' ');
  104.         if (printfn((u_char *)tp->th_stuff, ep)) {
  105.             fputs(&tstr[1], stdout);
  106.             return;
  107.         }
  108.         break;
  109.  
  110.     case DATA:
  111.         TCHECK(tp->th_block, sizeof(tp->th_block));
  112.         NTOHS(tp->th_block);
  113.         printf(" block %d", tp->th_block);
  114.         break;
  115.  
  116.     case ACK:
  117.         break;
  118.  
  119.     case ERROR:
  120.         /* Print error code string */
  121.         TCHECK(tp->th_code, sizeof(tp->th_code));
  122.         NTOHS(tp->th_code);
  123.         putchar(' ');
  124.         for (ts = err2str; ts->str; ++ts)
  125.             if (ts->code == tp->th_code) {
  126.                 fputs(ts->str, stdout);
  127.                 break;
  128.             }
  129.         if (ts->str == 0)
  130.             printf("tftp-err-#%d", tp->th_code);
  131.  
  132.         /* Print error message string */
  133.         putchar(' ');
  134.         if (printfn((u_char *)tp->th_data, ep)) {
  135.             fputs(&tstr[1], stdout);
  136.             return;
  137.         }
  138.         break;
  139.  
  140.     default:
  141.         /* We shouldn't get here */
  142.         printf("(unknown #%d)", tp->th_opcode);
  143.         break;
  144.     }
  145.     return;
  146. trunc:
  147.     fputs(tstr, stdout);
  148. #undef TCHECK
  149. }
  150.