File: rpc.c

package info (click to toggle)
dsniff 2.4b1+debian-22
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,808 kB
  • sloc: ansic: 10,785; sh: 152; makefile: 116
file content (133 lines) | stat: -rw-r--r-- 2,317 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * rpc.c
 *
 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
 *
 * $Id: rpc.c,v 1.8 2001/03/15 08:33:04 dugsong Exp $
 */

#include "config.h"

#include <sys/types.h>
#include <rpc/rpc.h>

#include <stdio.h>
#include <string.h>

#include "decode.h"
#include "rpc.h"

#define XIDMAPSIZE	64

static struct xid_map	xid_maps[XIDMAPSIZE];

static int		xid_map_next = 0;
static int		xid_map_hint = 0;

/* xid_map adapted from tcpdump's print-nfs.c */

void
xid_map_enter(u_int32_t xid, u_int32_t prog, u_int32_t vers,
	      u_int32_t proc, void *data)
{
	struct xid_map *mp;
	
	mp = &xid_maps[xid_map_next];
	
	if (++xid_map_next >= XIDMAPSIZE)
		xid_map_next = 0;
	
	mp->xid = xid;
	mp->prog = prog;
	mp->vers = vers;
	mp->proc = proc;
	mp->data = data;
}

struct xid_map *
xid_map_find(int xid)
{
	struct xid_map *mp;
	int i;
	
	/* Start searching from where we last left off. */
	i = xid_map_hint;
	do {
		mp = &xid_maps[i];
		if (mp->xid == xid) {
			/* match */
			xid_map_hint = i;
			return (mp);
		}
		if (++i >= XIDMAPSIZE)
			i = 0;
	} while (i != xid_map_hint);
	
	return (NULL);
}

int
rpc_decode(u_char *buf, int len, struct rpc_msg *msg)
{
	XDR xdrs;
	u_int32_t fraghdr;
	u_char *p, *tmp;
	int stat, tmplen;

	if (len < 20)
		return (0);
	
	p = buf + 4;

	/* If not recognizably RPC, try TCP record defragmentation */
	if (pntohl(p) != CALL && pntohl(p) != REPLY) {
		tmp = buf;
		tmplen = 0;
		
		for (;;) {
			fraghdr = pntohl(tmp);
			
			if (FRAGLEN(fraghdr) + 4 > len)
				return (0);
			
			len -= 4;
			memmove(tmp, tmp + 4, len);
			tmplen += FRAGLEN(fraghdr);
			
			if (LASTFRAG(fraghdr))
				break;
			
			tmp += FRAGLEN(fraghdr);
			len -= FRAGLEN(fraghdr);
			
			if (len < 4)
				return (0);
		}
		len = tmplen;
	}
	/* Decode RPC message. */
	memset(msg, 0, sizeof(*msg));
	
	if (ntohl(((struct rpc_msg *)buf)->rm_direction) == CALL) {
		xdrmem_create(&xdrs, buf, len, XDR_DECODE);
		
		if (!xdr_callmsg(&xdrs, msg)) {
			xdr_destroy(&xdrs);
			return (0);
		}
	}
	else if (ntohl(((struct rpc_msg *)buf)->rm_direction) == REPLY) {
		msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
		xdrmem_create(&xdrs, buf, len, XDR_DECODE);
		
		if (!xdr_replymsg(&xdrs, msg)) {
			xdr_destroy(&xdrs);
			return (0);
		}
	}
	stat = xdr_getpos(&xdrs);
	xdr_destroy(&xdrs);
	
	return (stat);
}