]> git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-tftp.c
9db577548338064bfa25aa8792a139180fd921ac
[apple/network_cmds.git] / tcpdump.tproj / print-tftp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that: (1) source code distributions
30 * retain the above copyright notice and this paragraph in its entirety, (2)
31 * distributions including binary code include the above copyright notice and
32 * this paragraph in its entirety in the documentation or other materials
33 * provided with the distribution, and (3) all advertising materials mentioning
34 * features or use of this software display the following acknowledgement:
35 * ``This product includes software developed by the University of California,
36 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
37 * the University nor the names of its contributors may be used to endorse
38 * or promote products derived from this software without specific prior
39 * written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
43 *
44 * Format and print trivial file transfer protocol packets.
45 */
46
47 #ifndef lint
48 static const char rcsid[] =
49 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-tftp.c,v 1.1.1.1 1999/05/02 03:58:34 wsanchez Exp $ (LBL)";
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/time.h>
54
55 #include <netinet/in.h>
56
57 #include <arpa/tftp.h>
58
59 #include <ctype.h>
60 #include <stdio.h>
61 #include <string.h>
62
63 #include "interface.h"
64 #include "addrtoname.h"
65
66 /* op code to string mapping */
67 static struct tok op2str[] = {
68 { RRQ, "RRQ" }, /* read request */
69 { WRQ, "WRQ" }, /* write request */
70 { DATA, "DATA" }, /* data packet */
71 { ACK, "ACK" }, /* acknowledgement */
72 { ERROR, "ERROR" }, /* error code */
73 { 0, NULL }
74 };
75
76 /* error code to string mapping */
77 static struct tok err2str[] = {
78 { EUNDEF, "EUNDEF" }, /* not defined */
79 { ENOTFOUND, "ENOTFOUND" }, /* file not found */
80 { EACCESS, "EACCESS" }, /* access violation */
81 { ENOSPACE, "ENOSPACE" }, /* disk full or allocation exceeded */
82 { EBADOP, "EBADOP" }, /* illegal TFTP operation */
83 { EBADID, "EBADID" }, /* unknown transfer ID */
84 { EEXISTS, "EEXISTS" }, /* file already exists */
85 { ENOUSER, "ENOUSER" }, /* no such user */
86 { 0, NULL }
87 };
88
89 /*
90 * Print trivial file transfer program requests
91 */
92 void
93 tftp_print(register const u_char *bp, u_int length)
94 {
95 register const struct tftphdr *tp;
96 register const char *cp;
97 register const u_char *p;
98 register int opcode, i;
99 static char tstr[] = " [|tftp]";
100
101 tp = (const struct tftphdr *)bp;
102
103 /* Print length */
104 printf(" %d", length);
105
106 /* Print tftp request type */
107 TCHECK(tp->th_opcode);
108 opcode = ntohs(tp->th_opcode);
109 cp = tok2str(op2str, "tftp-#%d", opcode);
110 printf(" %s", cp);
111 /* Bail if bogus opcode */
112 if (*cp == 't')
113 return;
114
115 switch (opcode) {
116
117 case RRQ:
118 case WRQ:
119 /*
120 * XXX Not all arpa/tftp.h's specify th_stuff as any
121 * array; use address of th_block instead
122 */
123 #ifdef notdef
124 p = (u_char *)tp->th_stuff;
125 #else
126 p = (u_char *)&tp->th_block;
127 #endif
128 fputs(" \"", stdout);
129 i = fn_print(p, snapend);
130 putchar('"');
131 if (i)
132 goto trunc;
133 break;
134
135 case ACK:
136 case DATA:
137 TCHECK(tp->th_block);
138 printf(" block %d", ntohs(tp->th_block));
139 break;
140
141 case ERROR:
142 /* Print error code string */
143 TCHECK(tp->th_code);
144 printf(" %s ", tok2str(err2str, "tftp-err-#%d \"",
145 ntohs(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;
152
153 default:
154 /* We shouldn't get here */
155 printf("(unknown #%d)", opcode);
156 break;
157 }
158 return;
159 trunc:
160 fputs(tstr, stdout);
161 return;
162 }