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