1/*
2 * Copyright (c) 1992, 1993, 1994, 1995, 1996
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 * $FreeBSD$
22 */
23
24#ifndef lint
25static const char rcsid[] _U_ =
26    "@(#) $Header: /tcpdump/master/tcpdump/print-sunrpc.c,v 1.47 2005-04-27 21:43:48 guy Exp $ (LBL)";
27#endif
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33/*
34 * At least on HP-UX:
35 *
36 *	1) getrpcbynumber() is declared in <netdb.h>, not any of the RPC
37 *	   header files
38 *
39 * and
40 *
41 *	2) if _XOPEN_SOURCE_EXTENDED is defined, <netdb.h> doesn't declare
42 *	   it
43 *
44 * so we undefine it.
45 */
46#undef _XOPEN_SOURCE_EXTENDED
47
48#include <tcpdump-stdinc.h>
49
50#if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
51#include <rpc/rpc.h>
52#ifdef HAVE_RPC_RPCENT_H
53#include <rpc/rpcent.h>
54#endif /* HAVE_RPC_RPCENT_H */
55#endif /* defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H) */
56
57#include <stdio.h>
58#include <string.h>
59
60#include "interface.h"
61#include "addrtoname.h"
62#include "extract.h"
63
64#include "ip.h"
65#ifdef INET6
66#include "ip6.h"
67#endif
68
69#include "rpc_auth.h"
70#include "rpc_msg.h"
71#include "pmap_prot.h"
72
73static struct tok proc2str[] = {
74	{ SUNRPC_PMAPPROC_NULL,		"null" },
75	{ SUNRPC_PMAPPROC_SET,		"set" },
76	{ SUNRPC_PMAPPROC_UNSET,	"unset" },
77	{ SUNRPC_PMAPPROC_GETPORT,	"getport" },
78	{ SUNRPC_PMAPPROC_DUMP,		"dump" },
79	{ SUNRPC_PMAPPROC_CALLIT,	"call" },
80	{ 0,				NULL }
81};
82
83/* Forwards */
84static char *progstr(u_int32_t);
85
86void
87sunrpcrequest_print(register const u_char *bp, register u_int length,
88		    register const u_char *bp2)
89{
90	register const struct sunrpc_msg *rp;
91	register const struct ip *ip;
92#ifdef INET6
93	register const struct ip6_hdr *ip6;
94#endif
95	u_int32_t x;
96	char srcid[20], dstid[20];	/*fits 32bit*/
97
98	rp = (struct sunrpc_msg *)bp;
99
100	if (!nflag) {
101		snprintf(srcid, sizeof(srcid), "0x%x",
102		    EXTRACT_32BITS(&rp->rm_xid));
103		strlcpy(dstid, "sunrpc", sizeof(dstid));
104	} else {
105		snprintf(srcid, sizeof(srcid), "0x%x",
106		    EXTRACT_32BITS(&rp->rm_xid));
107		snprintf(dstid, sizeof(dstid), "0x%x", SUNRPC_PMAPPORT);
108	}
109
110	switch (IP_V((struct ip *)bp2)) {
111	case 4:
112		ip = (struct ip *)bp2;
113		printf("%s.%s > %s.%s: %d",
114		    ipaddr_string(&ip->ip_src), srcid,
115		    ipaddr_string(&ip->ip_dst), dstid, length);
116		break;
117#ifdef INET6
118	case 6:
119		ip6 = (struct ip6_hdr *)bp2;
120		printf("%s.%s > %s.%s: %d",
121		    ip6addr_string(&ip6->ip6_src), srcid,
122		    ip6addr_string(&ip6->ip6_dst), dstid, length);
123		break;
124#endif
125	default:
126		printf("%s.%s > %s.%s: %d", "?", srcid, "?", dstid, length);
127		break;
128	}
129
130	printf(" %s", tok2str(proc2str, " proc #%u",
131	    EXTRACT_32BITS(&rp->rm_call.cb_proc)));
132	x = EXTRACT_32BITS(&rp->rm_call.cb_rpcvers);
133	if (x != 2)
134		printf(" [rpcver %u]", x);
135
136	switch (EXTRACT_32BITS(&rp->rm_call.cb_proc)) {
137
138	case SUNRPC_PMAPPROC_SET:
139	case SUNRPC_PMAPPROC_UNSET:
140	case SUNRPC_PMAPPROC_GETPORT:
141	case SUNRPC_PMAPPROC_CALLIT:
142		x = EXTRACT_32BITS(&rp->rm_call.cb_prog);
143		if (!nflag)
144			printf(" %s", progstr(x));
145		else
146			printf(" %u", x);
147		printf(".%u", EXTRACT_32BITS(&rp->rm_call.cb_vers));
148		break;
149	}
150}
151
152static char *
153progstr(prog)
154	u_int32_t prog;
155{
156#if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
157	register struct rpcent *rp;
158#endif
159	static char buf[32];
160	static u_int32_t lastprog = 0;
161
162	if (lastprog != 0 && prog == lastprog)
163		return (buf);
164#if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
165	rp = getrpcbynumber(prog);
166	if (rp == NULL)
167#endif
168		(void) snprintf(buf, sizeof(buf), "#%u", prog);
169#if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
170	else
171		strlcpy(buf, rp->r_name, sizeof(buf));
172#endif
173	return (buf);
174}
175