dsniff  2.4b2
About: A collection of tools for network auditing
  Fossies Dox: dsniff-2.4b2.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

rpc.c
Go to the documentation of this file.
1 /*
2  * rpc.c
3  *
4  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
5  *
6  * $Id: rpc.c,v 1.8 2001/03/15 08:33:04 dugsong Exp $
7  */
8 
9 #include "config.h"
10 
11 #include <sys/types.h>
12 #include <rpc/rpc.h>
13 
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include "decode.h"
18 #include "rpc.h"
19 
20 #define XIDMAPSIZE 64
21 
22 static struct xid_map xid_maps[XIDMAPSIZE];
23 
24 static int xid_map_next = 0;
25 static int xid_map_hint = 0;
26 
27 /* xid_map adapted from tcpdump's print-nfs.c */
28 
29 void
30 xid_map_enter(u_int32_t xid, u_int32_t prog, u_int32_t vers,
31  u_int32_t proc, void *data)
32 {
33  struct xid_map *mp;
34 
35  mp = &xid_maps[xid_map_next];
36 
37  if (++xid_map_next >= XIDMAPSIZE)
38  xid_map_next = 0;
39 
40  mp->xid = xid;
41  mp->prog = prog;
42  mp->vers = vers;
43  mp->proc = proc;
44  mp->data = data;
45 }
46 
47 struct xid_map *
49 {
50  struct xid_map *mp;
51  int i;
52 
53  /* Start searching from where we last left off. */
54  i = xid_map_hint;
55  do {
56  mp = &xid_maps[i];
57  if (mp->xid == xid) {
58  /* match */
59  xid_map_hint = i;
60  return (mp);
61  }
62  if (++i >= XIDMAPSIZE)
63  i = 0;
64  } while (i != xid_map_hint);
65 
66  return (NULL);
67 }
68 
69 int
70 rpc_decode(u_char *buf, int len, struct rpc_msg *msg)
71 {
72  XDR xdrs;
73  u_int32_t fraghdr;
74  u_char *p, *tmp;
75  int stat, tmplen;
76 
77  if (len < 20)
78  return (0);
79 
80  p = buf + 4;
81 
82  /* If not recognizably RPC, try TCP record defragmentation */
83  if (pntohl(p) != CALL && pntohl(p) != REPLY) {
84  tmp = buf;
85  tmplen = 0;
86 
87  for (;;) {
88  fraghdr = pntohl(tmp);
89 
90  if (FRAGLEN(fraghdr) + 4 > len)
91  return (0);
92 
93  len -= 4;
94  memmove(tmp, tmp + 4, len);
95  tmplen += FRAGLEN(fraghdr);
96 
97  if (LASTFRAG(fraghdr))
98  break;
99 
100  tmp += FRAGLEN(fraghdr);
101  len -= FRAGLEN(fraghdr);
102 
103  if (len < 4)
104  return (0);
105  }
106  len = tmplen;
107  }
108  /* Decode RPC message. */
109  memset(msg, 0, sizeof(*msg));
110 
111  if (ntohl(((struct rpc_msg *)buf)->rm_direction) == CALL) {
112  xdrmem_create(&xdrs, buf, len, XDR_DECODE);
113 
114  if (!xdr_callmsg(&xdrs, msg)) {
115  xdr_destroy(&xdrs);
116  return (0);
117  }
118  }
119  else if (ntohl(((struct rpc_msg *)buf)->rm_direction) == REPLY) {
120  msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
121  xdrmem_create(&xdrs, buf, len, XDR_DECODE);
122 
123  if (!xdr_replymsg(&xdrs, msg)) {
124  xdr_destroy(&xdrs);
125  return (0);
126  }
127  }
128  stat = xdr_getpos(&xdrs);
129  xdr_destroy(&xdrs);
130 
131  return (stat);
132 }
133 
xid_map
Definition: rpc.h:17
xid_map::vers
u_int32_t vers
Definition: rpc.h:20
xid_map_enter
void xid_map_enter(u_int32_t xid, u_int32_t prog, u_int32_t vers, u_int32_t proc, void *data)
Definition: rpc.c:30
buf
static u_char buf[BUFSIZ]
Definition: filenamesnarf.c:29
xid_map_find
struct xid_map * xid_map_find(int xid)
Definition: rpc.c:48
pntohl
#define pntohl(p)
Definition: decode.h:37
decode.h
xid_maps
static struct xid_map xid_maps[64]
Definition: rpc.c:22
xid_map::prog
u_int32_t prog
Definition: rpc.h:19
rpc.h
buf
Definition: buf.h:14
xid_map::data
void * data
Definition: rpc.h:22
LASTFRAG
#define LASTFRAG(x)
Definition: rpc.h:15
xid_map::proc
u_int32_t proc
Definition: rpc.h:21
FRAGLEN
#define FRAGLEN(x)
Definition: rpc.h:14
config.h
xid_map_hint
static int xid_map_hint
Definition: rpc.c:25
XIDMAPSIZE
#define XIDMAPSIZE
Definition: rpc.c:20
xid_map::xid
u_int32_t xid
Definition: rpc.h:18
xid_map_next
static int xid_map_next
Definition: rpc.c:24
rpc_decode
int rpc_decode(u_char *buf, int len, struct rpc_msg *msg)
Definition: rpc.c:70