]> The Tcpdump Group git mirrors - tcpdump/blob - print-tftp.c
Merge branch 'master' of git+ssh://bpf.tcpdump.org/tcpdump/master/git/tcpdump
[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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <tcpdump-stdinc.h>
29
30 #include <string.h>
31
32 #include "interface.h"
33 #include "extract.h"
34
35 /*
36 * Trivial File Transfer Protocol (IEN-133)
37 */
38
39 /*
40 * Packet types.
41 */
42 #define RRQ 01 /* read request */
43 #define WRQ 02 /* write request */
44 #define DATA 03 /* data packet */
45 #define ACK 04 /* acknowledgement */
46 #define TFTP_ERROR 05 /* error code */
47 #define OACK 06 /* option acknowledgement */
48
49 struct tftphdr {
50 unsigned short th_opcode; /* packet type */
51 union {
52 unsigned short tu_block; /* block # */
53 unsigned short tu_code; /* error code */
54 char tu_stuff[1]; /* request packet stuff */
55 } th_u;
56 char th_data[1]; /* data or error string */
57 };
58
59 #define th_block th_u.tu_block
60 #define th_code th_u.tu_code
61 #define th_stuff th_u.tu_stuff
62 #define th_msg th_data
63
64 /*
65 * Error codes.
66 */
67 #define EUNDEF 0 /* not defined */
68 #define ENOTFOUND 1 /* file not found */
69 #define EACCESS 2 /* access violation */
70 #define ENOSPACE 3 /* disk full or allocation exceeded */
71 #define EBADOP 4 /* illegal TFTP operation */
72 #define EBADID 5 /* unknown transfer ID */
73 #define EEXISTS 6 /* file already exists */
74 #define ENOUSER 7 /* no such user */
75
76 static const char tstr[] = " [|tftp]";
77
78 /* op code to string mapping */
79 static const struct tok op2str[] = {
80 { RRQ, "RRQ" }, /* read request */
81 { WRQ, "WRQ" }, /* write request */
82 { DATA, "DATA" }, /* data packet */
83 { ACK, "ACK" }, /* acknowledgement */
84 { TFTP_ERROR, "ERROR" }, /* error code */
85 { OACK, "OACK" }, /* option acknowledgement */
86 { 0, NULL }
87 };
88
89 /* error code to string mapping */
90 static const struct tok err2str[] = {
91 { EUNDEF, "EUNDEF" }, /* not defined */
92 { ENOTFOUND, "ENOTFOUND" }, /* file not found */
93 { EACCESS, "EACCESS" }, /* access violation */
94 { ENOSPACE, "ENOSPACE" }, /* disk full or allocation exceeded */
95 { EBADOP, "EBADOP" }, /* illegal TFTP operation */
96 { EBADID, "EBADID" }, /* unknown transfer ID */
97 { EEXISTS, "EEXISTS" }, /* file already exists */
98 { ENOUSER, "ENOUSER" }, /* no such user */
99 { 0, NULL }
100 };
101
102 /*
103 * Print trivial file transfer program requests
104 */
105 void
106 tftp_print(netdissect_options *ndo,
107 register const u_char *bp, u_int length)
108 {
109 register const struct tftphdr *tp;
110 register const char *cp;
111 register const u_char *p;
112 register int opcode, i;
113
114 tp = (const struct tftphdr *)bp;
115
116 /* Print length */
117 ND_PRINT((ndo, " %d", length));
118
119 /* Print tftp request type */
120 ND_TCHECK(tp->th_opcode);
121 opcode = EXTRACT_16BITS(&tp->th_opcode);
122 cp = tok2str(op2str, "tftp-#%d", opcode);
123 ND_PRINT((ndo, " %s", cp));
124 /* Bail if bogus opcode */
125 if (*cp == 't')
126 return;
127
128 switch (opcode) {
129
130 case RRQ:
131 case WRQ:
132 case OACK:
133 p = (u_char *)tp->th_stuff;
134 ND_PRINT((ndo, " "));
135 /* Print filename or first option */
136 if (opcode != OACK)
137 ND_PRINT((ndo, "\""));
138 i = fn_print(ndo, p, ndo->ndo_snapend);
139 if (opcode != OACK)
140 ND_PRINT((ndo, "\""));
141
142 /* Print the mode (RRQ and WRQ only) and any options */
143 while ((p = (const u_char *)strchr((const char *)p, '\0')) != NULL) {
144 if (length <= (u_int)(p - (const u_char *)&tp->th_block))
145 break;
146 p++;
147 if (*p != '\0') {
148 ND_PRINT((ndo, " "));
149 fn_print(ndo, p, ndo->ndo_snapend);
150 }
151 }
152
153 if (i)
154 goto trunc;
155 break;
156
157 case ACK:
158 case DATA:
159 ND_TCHECK(tp->th_block);
160 ND_PRINT((ndo, " block %d", EXTRACT_16BITS(&tp->th_block)));
161 break;
162
163 case TFTP_ERROR:
164 /* Print error code string */
165 ND_TCHECK(tp->th_code);
166 ND_PRINT((ndo, " %s \"", tok2str(err2str, "tftp-err-#%d \"",
167 EXTRACT_16BITS(&tp->th_code))));
168 /* Print error message string */
169 i = fn_print(ndo, (const u_char *)tp->th_data, ndo->ndo_snapend);
170 ND_PRINT((ndo, "\""));
171 if (i)
172 goto trunc;
173 break;
174
175 default:
176 /* We shouldn't get here */
177 ND_PRINT((ndo, "(unknown #%d)", opcode));
178 break;
179 }
180 return;
181 trunc:
182 ND_PRINT((ndo, "%s", tstr));
183 return;
184 }