print-tftp.c revision 17680
117680Spst/*
217680Spst * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
317680Spst *	The Regents of the University of California.  All rights reserved.
417680Spst *
517680Spst * Redistribution and use in source and binary forms, with or without
617680Spst * modification, are permitted provided that: (1) source code distributions
717680Spst * retain the above copyright notice and this paragraph in its entirety, (2)
817680Spst * distributions including binary code include the above copyright notice and
917680Spst * this paragraph in its entirety in the documentation or other materials
1017680Spst * provided with the distribution, and (3) all advertising materials mentioning
1117680Spst * features or use of this software display the following acknowledgement:
1217680Spst * ``This product includes software developed by the University of California,
1317680Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1417680Spst * the University nor the names of its contributors may be used to endorse
1517680Spst * or promote products derived from this software without specific prior
1617680Spst * written permission.
1717680Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1817680Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1917680Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2017680Spst *
2117680Spst * Format and print trivial file transfer protocol packets.
2217680Spst */
2317680Spst
2417680Spst#ifndef lint
2517680Spststatic char rcsid[] =
2617680Spst    "@(#) $Header: print-tftp.c,v 1.27 96/07/23 14:17:28 leres Exp $ (LBL)";
2717680Spst#endif
2817680Spst
2917680Spst#include <sys/param.h>
3017680Spst#include <sys/time.h>
3117680Spst
3217680Spst#include <netinet/in.h>
3317680Spst
3417680Spst#include <arpa/tftp.h>
3517680Spst
3617680Spst#include <ctype.h>
3717680Spst#include <stdio.h>
3817680Spst#include <string.h>
3917680Spst
4017680Spst#include "interface.h"
4117680Spst#include "addrtoname.h"
4217680Spst
4317680Spst/* op code to string mapping */
4417680Spststatic struct tok op2str[] = {
4517680Spst	{ RRQ,		"RRQ" },	/* read request */
4617680Spst	{ WRQ,		"WRQ" },	/* write request */
4717680Spst	{ DATA,		"DATA" },	/* data packet */
4817680Spst	{ ACK,		"ACK" },	/* acknowledgement */
4917680Spst	{ ERROR,	"ERROR" },	/* error code */
5017680Spst	{ 0,		NULL }
5117680Spst};
5217680Spst
5317680Spst/* error code to string mapping */
5417680Spststatic struct tok err2str[] = {
5517680Spst	{ EUNDEF,	"EUNDEF" },	/* not defined */
5617680Spst	{ ENOTFOUND,	"ENOTFOUND" },	/* file not found */
5717680Spst	{ EACCESS,	"EACCESS" },	/* access violation */
5817680Spst	{ ENOSPACE,	"ENOSPACE" },	/* disk full or allocation exceeded */
5917680Spst	{ EBADOP,	"EBADOP" },	/* illegal TFTP operation */
6017680Spst	{ EBADID,	"EBADID" },	/* unknown transfer ID */
6117680Spst	{ EEXISTS,	"EEXISTS" },	/* file already exists */
6217680Spst	{ ENOUSER,	"ENOUSER" },	/* no such user */
6317680Spst	{ 0,		NULL }
6417680Spst};
6517680Spst
6617680Spst/*
6717680Spst * Print trivial file transfer program requests
6817680Spst */
6917680Spstvoid
7017680Spsttftp_print(register const u_char *bp, u_int length)
7117680Spst{
7217680Spst	register const struct tftphdr *tp;
7317680Spst	register const char *cp;
7417680Spst	register const u_char *p;
7517680Spst	register int opcode, i;
7617680Spst	static char tstr[] = " [|tftp]";
7717680Spst
7817680Spst	tp = (const struct tftphdr *)bp;
7917680Spst
8017680Spst	/* Print length */
8117680Spst	printf(" %d", length);
8217680Spst
8317680Spst	/* Print tftp request type */
8417680Spst	TCHECK(tp->th_opcode);
8517680Spst	opcode = ntohs(tp->th_opcode);
8617680Spst	cp = tok2str(op2str, "tftp-#%d", opcode);
8717680Spst	printf(" %s", cp);
8817680Spst	/* Bail if bogus opcode */
8917680Spst	if (*cp == 't')
9017680Spst		return;
9117680Spst
9217680Spst	switch (opcode) {
9317680Spst
9417680Spst	case RRQ:
9517680Spst	case WRQ:
9617680Spst		/*
9717680Spst		 * XXX Not all arpa/tftp.h's specify th_stuff as any
9817680Spst		 * array; use address of th_block instead
9917680Spst		 */
10017680Spst#ifdef notdef
10117680Spst		p = (u_char *)tp->th_stuff;
10217680Spst#else
10317680Spst		p = (u_char *)&tp->th_block;
10417680Spst#endif
10517680Spst		fputs(" \"", stdout);
10617680Spst		i = fn_print(p, snapend);
10717680Spst		putchar('"');
10817680Spst		if (i)
10917680Spst			goto trunc;
11017680Spst		break;
11117680Spst
11217680Spst	case ACK:
11317680Spst	case DATA:
11417680Spst		TCHECK(tp->th_block);
11517680Spst		printf(" block %d", ntohs(tp->th_block));
11617680Spst		break;
11717680Spst
11817680Spst	case ERROR:
11917680Spst		/* Print error code string */
12017680Spst		TCHECK(tp->th_code);
12117680Spst		printf(" %s ", tok2str(err2str, "tftp-err-#%d \"",
12217680Spst				       ntohs(tp->th_code)));
12317680Spst		/* Print error message string */
12417680Spst		i = fn_print((const u_char *)tp->th_data, snapend);
12517680Spst		putchar('"');
12617680Spst		if (i)
12717680Spst			goto trunc;
12817680Spst		break;
12917680Spst
13017680Spst	default:
13117680Spst		/* We shouldn't get here */
13217680Spst		printf("(unknown #%d)", opcode);
13317680Spst		break;
13417680Spst	}
13517680Spst	return;
13617680Spsttrunc:
13717680Spst	fputs(tstr, stdout);
13817680Spst	return;
13917680Spst}
140