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