print-tftp.c revision 1.4 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that: (1) source code distributions
7 1.1 christos * retain the above copyright notice and this paragraph in its entirety, (2)
8 1.1 christos * distributions including binary code include the above copyright notice and
9 1.1 christos * this paragraph in its entirety in the documentation or other materials
10 1.1 christos * provided with the distribution, and (3) all advertising materials mentioning
11 1.1 christos * features or use of this software display the following acknowledgement:
12 1.1 christos * ``This product includes software developed by the University of California,
13 1.1 christos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 1.1 christos * the University nor the names of its contributors may be used to endorse
15 1.1 christos * or promote products derived from this software without specific prior
16 1.1 christos * written permission.
17 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 1.1 christos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 1.1 christos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 1.1 christos *
21 1.1 christos * Format and print trivial file transfer protocol packets.
22 1.1 christos */
23 1.1 christos
24 1.2 christos #include <sys/cdefs.h>
25 1.1 christos #ifndef lint
26 1.2 christos #if 0
27 1.1 christos static const char rcsid[] _U_ =
28 1.3 christos "@(#) Header: /tcpdump/master/tcpdump/print-tftp.c,v 1.39 2008-04-11 16:47:38 gianluca Exp (LBL)";
29 1.2 christos #else
30 1.4 christos __RCSID("$NetBSD: print-tftp.c,v 1.4 2013/12/31 17:33:31 christos Exp $");
31 1.2 christos #endif
32 1.1 christos #endif
33 1.1 christos
34 1.1 christos #ifdef HAVE_CONFIG_H
35 1.1 christos #include "config.h"
36 1.1 christos #endif
37 1.1 christos
38 1.1 christos #include <tcpdump-stdinc.h>
39 1.1 christos
40 1.1 christos #ifdef SEGSIZE
41 1.1 christos #undef SEGSIZE /* SINIX sucks */
42 1.1 christos #endif
43 1.1 christos
44 1.1 christos #include <stdio.h>
45 1.1 christos #include <string.h>
46 1.1 christos
47 1.1 christos #include "interface.h"
48 1.1 christos #include "addrtoname.h"
49 1.1 christos #include "extract.h"
50 1.1 christos #include "tftp.h"
51 1.1 christos
52 1.1 christos /* op code to string mapping */
53 1.4 christos static const struct tok op2str[] = {
54 1.1 christos { RRQ, "RRQ" }, /* read request */
55 1.1 christos { WRQ, "WRQ" }, /* write request */
56 1.1 christos { DATA, "DATA" }, /* data packet */
57 1.1 christos { ACK, "ACK" }, /* acknowledgement */
58 1.1 christos { TFTP_ERROR, "ERROR" }, /* error code */
59 1.1 christos { OACK, "OACK" }, /* option acknowledgement */
60 1.1 christos { 0, NULL }
61 1.1 christos };
62 1.1 christos
63 1.1 christos /* error code to string mapping */
64 1.4 christos static const struct tok err2str[] = {
65 1.1 christos { EUNDEF, "EUNDEF" }, /* not defined */
66 1.1 christos { ENOTFOUND, "ENOTFOUND" }, /* file not found */
67 1.1 christos { EACCESS, "EACCESS" }, /* access violation */
68 1.1 christos { ENOSPACE, "ENOSPACE" }, /* disk full or allocation exceeded */
69 1.1 christos { EBADOP, "EBADOP" }, /* illegal TFTP operation */
70 1.1 christos { EBADID, "EBADID" }, /* unknown transfer ID */
71 1.1 christos { EEXISTS, "EEXISTS" }, /* file already exists */
72 1.1 christos { ENOUSER, "ENOUSER" }, /* no such user */
73 1.1 christos { 0, NULL }
74 1.1 christos };
75 1.1 christos
76 1.1 christos /*
77 1.1 christos * Print trivial file transfer program requests
78 1.1 christos */
79 1.1 christos void
80 1.1 christos tftp_print(register const u_char *bp, u_int length)
81 1.1 christos {
82 1.1 christos register const struct tftphdr *tp;
83 1.1 christos register const char *cp;
84 1.1 christos register const u_char *p;
85 1.1 christos register int opcode, i;
86 1.1 christos static char tstr[] = " [|tftp]";
87 1.1 christos
88 1.1 christos tp = (const struct tftphdr *)bp;
89 1.1 christos
90 1.1 christos /* Print length */
91 1.1 christos printf(" %d", length);
92 1.1 christos
93 1.1 christos /* Print tftp request type */
94 1.1 christos TCHECK(tp->th_opcode);
95 1.1 christos opcode = EXTRACT_16BITS(&tp->th_opcode);
96 1.1 christos cp = tok2str(op2str, "tftp-#%d", opcode);
97 1.1 christos printf(" %s", cp);
98 1.1 christos /* Bail if bogus opcode */
99 1.1 christos if (*cp == 't')
100 1.1 christos return;
101 1.1 christos
102 1.1 christos switch (opcode) {
103 1.1 christos
104 1.1 christos case RRQ:
105 1.1 christos case WRQ:
106 1.1 christos case OACK:
107 1.1 christos p = (u_char *)tp->th_stuff;
108 1.1 christos putchar(' ');
109 1.1 christos /* Print filename or first option */
110 1.1 christos if (opcode != OACK)
111 1.1 christos putchar('"');
112 1.1 christos i = fn_print(p, snapend);
113 1.1 christos if (opcode != OACK)
114 1.1 christos putchar('"');
115 1.1 christos
116 1.1 christos /* Print the mode (RRQ and WRQ only) and any options */
117 1.1 christos while ((p = (const u_char *)strchr((const char *)p, '\0')) != NULL) {
118 1.1 christos if (length <= (u_int)(p - (const u_char *)&tp->th_block))
119 1.1 christos break;
120 1.1 christos p++;
121 1.1 christos if (*p != '\0') {
122 1.1 christos putchar(' ');
123 1.1 christos fn_print(p, snapend);
124 1.1 christos }
125 1.1 christos }
126 1.1 christos
127 1.1 christos if (i)
128 1.1 christos goto trunc;
129 1.1 christos break;
130 1.1 christos
131 1.1 christos case ACK:
132 1.1 christos case DATA:
133 1.1 christos TCHECK(tp->th_block);
134 1.1 christos printf(" block %d", EXTRACT_16BITS(&tp->th_block));
135 1.1 christos break;
136 1.1 christos
137 1.1 christos case TFTP_ERROR:
138 1.1 christos /* Print error code string */
139 1.1 christos TCHECK(tp->th_code);
140 1.1 christos printf(" %s \"", tok2str(err2str, "tftp-err-#%d \"",
141 1.1 christos EXTRACT_16BITS(&tp->th_code)));
142 1.1 christos /* Print error message string */
143 1.1 christos i = fn_print((const u_char *)tp->th_data, snapend);
144 1.1 christos putchar('"');
145 1.1 christos if (i)
146 1.1 christos goto trunc;
147 1.1 christos break;
148 1.1 christos
149 1.1 christos default:
150 1.1 christos /* We shouldn't get here */
151 1.1 christos printf("(unknown #%d)", opcode);
152 1.1 christos break;
153 1.1 christos }
154 1.1 christos return;
155 1.1 christos trunc:
156 1.1 christos fputs(tstr, stdout);
157 1.1 christos return;
158 1.1 christos }
159