]> The Tcpdump Group git mirrors - tcpdump/blob - print-tftp.c
Use the new GET_ macros instead of the EXTRACT_ ones
[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
22 /* \summary: Trivial File Transfer Protocol (TFTP) printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include <string.h>
31
32 #include "netdissect.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 /*
50 * Error codes.
51 */
52 #define EUNDEF 0 /* not defined */
53 #define ENOTFOUND 1 /* file not found */
54 #define EACCESS 2 /* access violation */
55 #define ENOSPACE 3 /* disk full or allocation exceeded */
56 #define EBADOP 4 /* illegal TFTP operation */
57 #define EBADID 5 /* unknown transfer ID */
58 #define EEXISTS 6 /* file already exists */
59 #define ENOUSER 7 /* no such user */
60
61
62 /* op code to string mapping */
63 static const struct tok op2str[] = {
64 { RRQ, "RRQ" }, /* read request */
65 { WRQ, "WRQ" }, /* write request */
66 { DATA, "DATA" }, /* data packet */
67 { ACK, "ACK" }, /* acknowledgement */
68 { TFTP_ERROR, "ERROR" }, /* error code */
69 { OACK, "OACK" }, /* option acknowledgement */
70 { 0, NULL }
71 };
72
73 /* error code to string mapping */
74 static const struct tok err2str[] = {
75 { EUNDEF, "EUNDEF" }, /* not defined */
76 { ENOTFOUND, "ENOTFOUND" }, /* file not found */
77 { EACCESS, "EACCESS" }, /* access violation */
78 { ENOSPACE, "ENOSPACE" }, /* disk full or allocation exceeded */
79 { EBADOP, "EBADOP" }, /* illegal TFTP operation */
80 { EBADID, "EBADID" }, /* unknown transfer ID */
81 { EEXISTS, "EEXISTS" }, /* file already exists */
82 { ENOUSER, "ENOUSER" }, /* no such user */
83 { 0, NULL }
84 };
85
86 /*
87 * Print trivial file transfer program requests
88 */
89 void
90 tftp_print(netdissect_options *ndo,
91 const u_char *bp, u_int length)
92 {
93 const char *cp;
94 u_int opcode;
95 u_int ui;
96
97 ndo->ndo_protocol = "tftp";
98
99 /* Print protocol */
100 ND_PRINT("TFTP");
101 /* Print length */
102 ND_PRINT(", length %u", length);
103
104 /* Print tftp request type */
105 if (length < 2)
106 goto trunc;
107 ND_TCHECK_2(bp);
108 opcode = GET_BE_U_2(bp);
109 cp = tok2str(op2str, "tftp-#%u", opcode);
110 ND_PRINT(", %s", cp);
111 /* Bail if bogus opcode */
112 if (*cp == 't')
113 return;
114 bp += 2;
115 length -= 2;
116
117 switch (opcode) {
118
119 case RRQ:
120 case WRQ:
121 if (length == 0)
122 goto trunc;
123 ND_PRINT(" ");
124 /* Print filename */
125 ND_PRINT("\"");
126 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
127 ND_PRINT("\"");
128 if (ui == 0)
129 goto trunc;
130 bp += ui;
131 length -= ui;
132
133 /* Print the mode - RRQ and WRQ only */
134 if (length == 0)
135 goto trunc; /* no mode */
136 ND_PRINT(" ");
137 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
138 if (ui == 0)
139 goto trunc;
140 bp += ui;
141 length -= ui;
142
143 /* Print options, if any */
144 while (length != 0) {
145 ND_TCHECK_1(bp);
146 if (GET_U_1(bp) != '\0')
147 ND_PRINT(" ");
148 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
149 if (ui == 0)
150 goto trunc;
151 bp += ui;
152 length -= ui;
153 }
154 break;
155
156 case OACK:
157 /* Print options */
158 while (length != 0) {
159 ND_TCHECK_1(bp);
160 if (GET_U_1(bp) != '\0')
161 ND_PRINT(" ");
162 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
163 if (ui == 0)
164 goto trunc;
165 bp += ui;
166 length -= ui;
167 }
168 break;
169
170 case ACK:
171 case DATA:
172 if (length < 2)
173 goto trunc; /* no block number */
174 ND_TCHECK_2(bp);
175 ND_PRINT(" block %u", GET_BE_U_2(bp));
176 break;
177
178 case TFTP_ERROR:
179 /* Print error code string */
180 if (length < 2)
181 goto trunc; /* no error code */
182 ND_TCHECK_2(bp);
183 ND_PRINT(" %s", tok2str(err2str, "tftp-err-#%u \"",
184 GET_BE_U_2(bp)));
185 bp += 2;
186 length -= 2;
187 /* Print error message string */
188 if (length == 0)
189 goto trunc; /* no error message */
190 ND_PRINT(" \"");
191 ui = nd_printztn(ndo, bp, length, ndo->ndo_snapend);
192 ND_PRINT("\"");
193 if (ui == 0)
194 goto trunc;
195 break;
196
197 default:
198 /* We shouldn't get here */
199 ND_PRINT("(unknown #%u)", opcode);
200 break;
201 }
202 return;
203 trunc:
204 nd_print_trunc(ndo);
205 return;
206 }