]> The Tcpdump Group git mirrors - tcpdump/blob - print-tftp.c
whitespace changes
[tcpdump] / print-tftp.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. 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
24 #ifndef lint
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-tftp.c,v 1.39 2008-04-11 16:47:38 gianluca Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #ifdef SEGSIZE
36 #undef SEGSIZE /* SINIX sucks */
37 #endif
38
39 #include <stdio.h>
40 #include <string.h>
41
42 #include "interface.h"
43 #include "addrtoname.h"
44 #include "extract.h"
45 #include "tftp.h"
46
47 static const char tstr[] = " [|tftp]";
48
49 /* op code to string mapping */
50 static const struct tok op2str[] = {
51 { RRQ, "RRQ" }, /* read request */
52 { WRQ, "WRQ" }, /* write request */
53 { DATA, "DATA" }, /* data packet */
54 { ACK, "ACK" }, /* acknowledgement */
55 { TFTP_ERROR, "ERROR" }, /* error code */
56 { OACK, "OACK" }, /* option acknowledgement */
57 { 0, NULL }
58 };
59
60 /* error code to string mapping */
61 static const struct tok err2str[] = {
62 { EUNDEF, "EUNDEF" }, /* not defined */
63 { ENOTFOUND, "ENOTFOUND" }, /* file not found */
64 { EACCESS, "EACCESS" }, /* access violation */
65 { ENOSPACE, "ENOSPACE" }, /* disk full or allocation exceeded */
66 { EBADOP, "EBADOP" }, /* illegal TFTP operation */
67 { EBADID, "EBADID" }, /* unknown transfer ID */
68 { EEXISTS, "EEXISTS" }, /* file already exists */
69 { ENOUSER, "ENOUSER" }, /* no such user */
70 { 0, NULL }
71 };
72
73 /*
74 * Print trivial file transfer program requests
75 */
76 void
77 tftp_print(register const u_char *bp, u_int length)
78 {
79 register const struct tftphdr *tp;
80 register const char *cp;
81 register const u_char *p;
82 register int opcode, i;
83
84 tp = (const struct tftphdr *)bp;
85
86 /* Print length */
87 printf(" %d", length);
88
89 /* Print tftp request type */
90 TCHECK(tp->th_opcode);
91 opcode = EXTRACT_16BITS(&tp->th_opcode);
92 cp = tok2str(op2str, "tftp-#%d", opcode);
93 printf(" %s", cp);
94 /* Bail if bogus opcode */
95 if (*cp == 't')
96 return;
97
98 switch (opcode) {
99
100 case RRQ:
101 case WRQ:
102 case OACK:
103 p = (u_char *)tp->th_stuff;
104 putchar(' ');
105 /* Print filename or first option */
106 if (opcode != OACK)
107 putchar('"');
108 i = fn_print(p, snapend);
109 if (opcode != OACK)
110 putchar('"');
111
112 /* Print the mode (RRQ and WRQ only) and any options */
113 while ((p = (const u_char *)strchr((const char *)p, '\0')) != NULL) {
114 if (length <= (u_int)(p - (const u_char *)&tp->th_block))
115 break;
116 p++;
117 if (*p != '\0') {
118 putchar(' ');
119 fn_print(p, snapend);
120 }
121 }
122
123 if (i)
124 goto trunc;
125 break;
126
127 case ACK:
128 case DATA:
129 TCHECK(tp->th_block);
130 printf(" block %d", EXTRACT_16BITS(&tp->th_block));
131 break;
132
133 case TFTP_ERROR:
134 /* Print error code string */
135 TCHECK(tp->th_code);
136 printf(" %s \"", tok2str(err2str, "tftp-err-#%d \"",
137 EXTRACT_16BITS(&tp->th_code)));
138 /* Print error message string */
139 i = fn_print((const u_char *)tp->th_data, snapend);
140 putchar('"');
141 if (i)
142 goto trunc;
143 break;
144
145 default:
146 /* We shouldn't get here */
147 printf("(unknown #%d)", opcode);
148 break;
149 }
150 return;
151 trunc:
152 fputs(tstr, stdout);
153 return;
154 }