print-tftp.c revision 39297
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#ifndef lint
25static const char rcsid[] =
26    "@(#) $Header: print-tftp.c,v 1.30 97/06/13 12:57:12 leres Exp $ (LBL)";
27#endif
28
29#include <sys/param.h>
30#include <sys/time.h>
31
32#include <netinet/in.h>
33
34#ifdef SEGSIZE
35#undef SEGSIZE					/* SINIX sucks */
36#endif
37#include <arpa/tftp.h>
38
39#include <ctype.h>
40#include <stdio.h>
41#include <string.h>
42
43#include "interface.h"
44#include "addrtoname.h"
45
46/* op code to string mapping */
47static struct tok op2str[] = {
48	{ RRQ,		"RRQ" },	/* read request */
49	{ WRQ,		"WRQ" },	/* write request */
50	{ DATA,		"DATA" },	/* data packet */
51	{ ACK,		"ACK" },	/* acknowledgement */
52	{ ERROR,	"ERROR" },	/* error code */
53	{ 0,		NULL }
54};
55
56/* error code to string mapping */
57static struct tok err2str[] = {
58	{ EUNDEF,	"EUNDEF" },	/* not defined */
59	{ ENOTFOUND,	"ENOTFOUND" },	/* file not found */
60	{ EACCESS,	"EACCESS" },	/* access violation */
61	{ ENOSPACE,	"ENOSPACE" },	/* disk full or allocation exceeded */
62	{ EBADOP,	"EBADOP" },	/* illegal TFTP operation */
63	{ EBADID,	"EBADID" },	/* unknown transfer ID */
64	{ EEXISTS,	"EEXISTS" },	/* file already exists */
65	{ ENOUSER,	"ENOUSER" },	/* no such user */
66	{ 0,		NULL }
67};
68
69/*
70 * Print trivial file transfer program requests
71 */
72void
73tftp_print(register const u_char *bp, u_int length)
74{
75	register const struct tftphdr *tp;
76	register const char *cp;
77	register const u_char *p;
78	register int opcode, i;
79	static char tstr[] = " [|tftp]";
80
81	tp = (const struct tftphdr *)bp;
82
83	/* Print length */
84	printf(" %d", length);
85
86	/* Print tftp request type */
87	TCHECK(tp->th_opcode);
88	opcode = ntohs(tp->th_opcode);
89	cp = tok2str(op2str, "tftp-#%d", opcode);
90	printf(" %s", cp);
91	/* Bail if bogus opcode */
92	if (*cp == 't')
93		return;
94
95	switch (opcode) {
96
97	case RRQ:
98	case WRQ:
99		/*
100		 * XXX Not all arpa/tftp.h's specify th_stuff as any
101		 * array; use address of th_block instead
102		 */
103#ifdef notdef
104		p = (u_char *)tp->th_stuff;
105#else
106		p = (u_char *)&tp->th_block;
107#endif
108		fputs(" \"", stdout);
109		i = fn_print(p, snapend);
110		putchar('"');
111		if (i)
112			goto trunc;
113		break;
114
115	case ACK:
116	case DATA:
117		TCHECK(tp->th_block);
118		printf(" block %d", ntohs(tp->th_block));
119		break;
120
121	case ERROR:
122		/* Print error code string */
123		TCHECK(tp->th_code);
124		printf(" %s ", tok2str(err2str, "tftp-err-#%d \"",
125				       ntohs(tp->th_code)));
126		/* Print error message string */
127		i = fn_print((const u_char *)tp->th_data, snapend);
128		putchar('"');
129		if (i)
130			goto trunc;
131		break;
132
133	default:
134		/* We shouldn't get here */
135		printf("(unknown #%d)", opcode);
136		break;
137	}
138	return;
139trunc:
140	fputs(tstr, stdout);
141	return;
142}
143