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