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