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