117680Spst/*
217680Spst * Copyright (c) 1992, 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.
2056896Sfenner *
2156896Sfenner * $FreeBSD$
2217680Spst */
2317680Spst
2417680Spst#ifndef lint
25127675Sbmsstatic const char rcsid[] _U_ =
26190207Srpaulo    "@(#) $Header: /tcpdump/master/tcpdump/print-sunrpc.c,v 1.47 2005-04-27 21:43:48 guy Exp $ (LBL)";
2717680Spst#endif
2817680Spst
2956896Sfenner#ifdef HAVE_CONFIG_H
3056896Sfenner#include "config.h"
3156896Sfenner#endif
3256896Sfenner
33214478Srpaulo/*
34214478Srpaulo * At least on HP-UX:
35214478Srpaulo *
36214478Srpaulo *	1) getrpcbynumber() is declared in <netdb.h>, not any of the RPC
37214478Srpaulo *	   header files
38214478Srpaulo *
39214478Srpaulo * and
40214478Srpaulo *
41214478Srpaulo *	2) if _XOPEN_SOURCE_EXTENDED is defined, <netdb.h> doesn't declare
42214478Srpaulo *	   it
43214478Srpaulo *
44214478Srpaulo * so we undefine it.
45214478Srpaulo */
46214478Srpaulo#undef _XOPEN_SOURCE_EXTENDED
47214478Srpaulo
48127675Sbms#include <tcpdump-stdinc.h>
4917680Spst
50235530Sdelphij#if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
5117680Spst#include <rpc/rpc.h>
5217680Spst#ifdef HAVE_RPC_RPCENT_H
5317680Spst#include <rpc/rpcent.h>
54146778Ssam#endif /* HAVE_RPC_RPCENT_H */
55235530Sdelphij#endif /* defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H) */
5617680Spst
5717680Spst#include <stdio.h>
5817680Spst#include <string.h>
5917680Spst
6017680Spst#include "interface.h"
6117680Spst#include "addrtoname.h"
62127675Sbms#include "extract.h"
6317680Spst
6475118Sfenner#include "ip.h"
6575118Sfenner#ifdef INET6
6675118Sfenner#include "ip6.h"
6775118Sfenner#endif
6875118Sfenner
69146778Ssam#include "rpc_auth.h"
70146778Ssam#include "rpc_msg.h"
71147904Ssam#include "pmap_prot.h"
72146778Ssam
7317680Spststatic struct tok proc2str[] = {
74147904Ssam	{ SUNRPC_PMAPPROC_NULL,		"null" },
75147904Ssam	{ SUNRPC_PMAPPROC_SET,		"set" },
76147904Ssam	{ SUNRPC_PMAPPROC_UNSET,	"unset" },
77147904Ssam	{ SUNRPC_PMAPPROC_GETPORT,	"getport" },
78147904Ssam	{ SUNRPC_PMAPPROC_DUMP,		"dump" },
79147904Ssam	{ SUNRPC_PMAPPROC_CALLIT,	"call" },
80147904Ssam	{ 0,				NULL }
8117680Spst};
8217680Spst
8317680Spst/* Forwards */
8417680Spststatic char *progstr(u_int32_t);
8517680Spst
8617680Spstvoid
8717680Spstsunrpcrequest_print(register const u_char *bp, register u_int length,
8817680Spst		    register const u_char *bp2)
8917680Spst{
90146778Ssam	register const struct sunrpc_msg *rp;
9117680Spst	register const struct ip *ip;
9275118Sfenner#ifdef INET6
9375118Sfenner	register const struct ip6_hdr *ip6;
9475118Sfenner#endif
9517680Spst	u_int32_t x;
9675118Sfenner	char srcid[20], dstid[20];	/*fits 32bit*/
9717680Spst
98146778Ssam	rp = (struct sunrpc_msg *)bp;
9917680Spst
10075118Sfenner	if (!nflag) {
10175118Sfenner		snprintf(srcid, sizeof(srcid), "0x%x",
102127675Sbms		    EXTRACT_32BITS(&rp->rm_xid));
10375118Sfenner		strlcpy(dstid, "sunrpc", sizeof(dstid));
10475118Sfenner	} else {
10575118Sfenner		snprintf(srcid, sizeof(srcid), "0x%x",
106127675Sbms		    EXTRACT_32BITS(&rp->rm_xid));
107147904Ssam		snprintf(dstid, sizeof(dstid), "0x%x", SUNRPC_PMAPPORT);
10875118Sfenner	}
10975118Sfenner
11075118Sfenner	switch (IP_V((struct ip *)bp2)) {
11175118Sfenner	case 4:
11275118Sfenner		ip = (struct ip *)bp2;
11375118Sfenner		printf("%s.%s > %s.%s: %d",
11475118Sfenner		    ipaddr_string(&ip->ip_src), srcid,
11575118Sfenner		    ipaddr_string(&ip->ip_dst), dstid, length);
11675118Sfenner		break;
11775118Sfenner#ifdef INET6
11875118Sfenner	case 6:
11975118Sfenner		ip6 = (struct ip6_hdr *)bp2;
12075118Sfenner		printf("%s.%s > %s.%s: %d",
12175118Sfenner		    ip6addr_string(&ip6->ip6_src), srcid,
12275118Sfenner		    ip6addr_string(&ip6->ip6_dst), dstid, length);
12375118Sfenner		break;
12475118Sfenner#endif
12575118Sfenner	default:
12675118Sfenner		printf("%s.%s > %s.%s: %d", "?", srcid, "?", dstid, length);
12775118Sfenner		break;
12875118Sfenner	}
12975118Sfenner
13017680Spst	printf(" %s", tok2str(proc2str, " proc #%u",
131127675Sbms	    EXTRACT_32BITS(&rp->rm_call.cb_proc)));
132127675Sbms	x = EXTRACT_32BITS(&rp->rm_call.cb_rpcvers);
13317680Spst	if (x != 2)
13417680Spst		printf(" [rpcver %u]", x);
13517680Spst
136127675Sbms	switch (EXTRACT_32BITS(&rp->rm_call.cb_proc)) {
13717680Spst
138147904Ssam	case SUNRPC_PMAPPROC_SET:
139147904Ssam	case SUNRPC_PMAPPROC_UNSET:
140147904Ssam	case SUNRPC_PMAPPROC_GETPORT:
141147904Ssam	case SUNRPC_PMAPPROC_CALLIT:
142127675Sbms		x = EXTRACT_32BITS(&rp->rm_call.cb_prog);
14317680Spst		if (!nflag)
14417680Spst			printf(" %s", progstr(x));
14517680Spst		else
14617680Spst			printf(" %u", x);
147127675Sbms		printf(".%u", EXTRACT_32BITS(&rp->rm_call.cb_vers));
14817680Spst		break;
14917680Spst	}
15017680Spst}
15117680Spst
15217680Spststatic char *
15317680Spstprogstr(prog)
15417680Spst	u_int32_t prog;
15517680Spst{
156235530Sdelphij#if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
15717680Spst	register struct rpcent *rp;
158127675Sbms#endif
15917680Spst	static char buf[32];
160127675Sbms	static u_int32_t lastprog = 0;
16117680Spst
16217680Spst	if (lastprog != 0 && prog == lastprog)
16317680Spst		return (buf);
164235530Sdelphij#if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
16517680Spst	rp = getrpcbynumber(prog);
16617680Spst	if (rp == NULL)
167146778Ssam#endif
16875118Sfenner		(void) snprintf(buf, sizeof(buf), "#%u", prog);
169235530Sdelphij#if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
17075118Sfenner	else
17175118Sfenner		strlcpy(buf, rp->r_name, sizeof(buf));
172127675Sbms#endif
17317680Spst	return (buf);
17417680Spst}
175