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