File: srpc.c

package info (click to toggle)
sfs 1%3A0.8-0%2Bpre20060720.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,668 kB
  • ctags: 14,317
  • sloc: cpp: 78,358; ansic: 15,494; sh: 9,540; yacc: 786; makefile: 706; perl: 676; lex: 553; python: 146; sed: 70
file content (270 lines) | stat: -rw-r--r-- 6,648 bytes parent folder | download | duplicates (3)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* $Id: srpc.c,v 1.9 1999/10/23 14:18:23 dm Exp $ */

/*
 *
 * Copyright (C) 1999 David Mazieres (dm@uun.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#include "sfs-internal.h"

#ifdef HAVE___SETERR_REPLY
#define seterr_reply __seterr_reply
#else /* !HAVE___SETERR_REPLY */
#define seterr_reply _seterr_reply
#endif /* !HAVE___SETERR_REPLY */

#define MAXMSG 0x2400

static inline bool_t
xdr_putint (XDR *xdrs, xdrlong_t val)
{
  return xdr_putlong (xdrs, &val);
}

static inline bool_t
xdr_getint (XDR *xdrs, u_int32_t *valp)
{
  xdrlong_t val;
  if (xdr_getlong (xdrs, &val)) {
    *valp = val;
    return TRUE;
  }
  return FALSE;
}

static bool_t
xdr_auth (XDR *xdrs, struct opaque_auth *oap)
{
  u_int32_t val;
  if (!xdr_getint (xdrs, &val))
    return FALSE;
  oap->oa_flavor = val;
  if (!xdr_getint (xdrs, &val))
    return FALSE;
  oap->oa_length = val;

  if (oap->oa_length) {
    if (oap->oa_length > 400)
      return FALSE;
    xfree (oap->oa_base);
    oap->oa_base = malloc (oap->oa_length);
    if (!oap->oa_base) {
      oap->oa_length = 0;
      return FALSE;
    }
    if (!xdr_opaque (xdrs, oap->oa_base, oap->oa_length))
      return FALSE;
  }
  else if (oap->oa_base) {
    xfree (oap->oa_base);
    oap->oa_base = NULL;
  }
  return TRUE;
}

enum clnt_stat
srpc_marshal_call (XDR *xdrs, u_int32_t xid,
		   u_int32_t progno, u_int32_t versno, u_int32_t procno,
		   AUTH *auth, xdrproc_t inproc, void *in)
{
  u_int32_t *dp = (u_int32_t *) xdr_inline (xdrs, 6*4);
  if (dp) {
    *dp++ = xid;
    *dp++ = htonl (CALL);
    *dp++ = htonl (RPC_MSG_VERSION);
    *dp++ = htonl (progno);
    *dp++ = htonl (versno);
    *dp++ = htonl (procno);
  }
  else if (!xdr_putint (xdrs, xid)
	   || !xdr_putint (xdrs, CALL)
	   || !xdr_putint (xdrs, RPC_MSG_VERSION)
	   || !xdr_putint (xdrs, progno)
	   || !xdr_putint (xdrs, versno)
	   || !xdr_putint (xdrs, procno))
    return RPC_CANTENCODEARGS;

  if (!auth) {
    dp = (u_int32_t *) xdr_inline (xdrs, 4*4);
    if (dp)
      bzero (dp, 4 * 4);
    else if (!xdr_putint (xdrs, 0)
	     || !xdr_putint (xdrs, 0)
	     || !xdr_putint (xdrs, 0)
	     || !xdr_putint (xdrs, 0))
      return RPC_CANTENCODEARGS;
  }
  else if (!auth_marshall (auth, xdrs))
    return RPC_CANTENCODEARGS;

  if (!inproc (xdrs, (void *) in))
    return RPC_CANTENCODEARGS;

  return RPC_SUCCESS;
}

enum clnt_stat
srpc_marshal_reply (XDR *xdrs, struct rpc_msg *m)
{
  u_int32_t buf[3];
  u_int32_t *dp = (u_int32_t *) xdr_inline (xdrs, 3*4);
  struct rpc_err re;

#define GETINT(r)				\
  do {						\
    u_int32_t val;				\
    if (!xdr_getint (xdrs, &val))		\
      return RPC_CANTDECODERES;			\
    (r) = val;					\
  } while (0)

  if (!dp) {
    if (!xdr_getbytes (xdrs, (char *) buf, sizeof (buf)))
      return RPC_CANTDECODERES;
    dp = buf;
  }

  m->rm_xid = dp[0];
  m->rm_direction = ntohl (dp[1]);
  if (m->rm_direction != REPLY)
    return RPC_CANTDECODERES;
  m->rm_reply.rp_stat = ntohl (dp[2]);

  switch (m->rm_reply.rp_stat) {
  case MSG_ACCEPTED:
    if (!xdr_auth (xdrs, &m->acpted_rply.ar_verf))
      return RPC_CANTDECODERES;
    GETINT (m->acpted_rply.ar_stat);
    switch ((int) m->acpted_rply.ar_stat) {
    case SUCCESS:
      if (!m->acpted_rply.ar_results.proc (xdrs,
					   m->acpted_rply.ar_results.where))
	return RPC_CANTDECODERES;
      break;
    case PROG_MISMATCH:
      GETINT (m->acpted_rply.ar_vers.low);
      GETINT (m->acpted_rply.ar_vers.high);
      break;
    }
    break;

  case MSG_DENIED:
    GETINT (m->rjcted_rply.rj_stat);
    switch (m->rjcted_rply.rj_stat) {
    case RPC_MISMATCH:
      GETINT (m->rjcted_rply.rj_vers.low);
      GETINT (m->rjcted_rply.rj_vers.high);
      break;
    case AUTH_ERROR:
      GETINT (m->rjcted_rply.rj_why);
      break;
    default:
      return RPC_CANTDECODERES;
    }
    break;

  default:
    return RPC_CANTDECODERES;
  }

  seterr_reply (m, &re);
  return re.re_status;

#undef GETINT
}

static ssize_t
readpkt (int fd, void *buf, size_t bufsize)
{
  size_t pos = 0;

  do {
    ssize_t n = read (fd, buf + pos, bufsize - pos);
    if (n <= 0) {
      perror ("srpc");
      return -1;
    }
    pos += n;
    if (pos - n < 4 && pos >= 4) {
      u_char *bp = buf;
      u_int32_t msgsize = bp[0] << 24 | bp[1] << 16 | bp[2] << 8 | bp[3];
      if (!(msgsize & 0x80000000))
	return -1;
      msgsize &= 0x7fffffff;
      if (msgsize + 4 > bufsize || pos > msgsize + 4)
	return -1;
      bufsize = msgsize + 4;
    }
  } while (pos < bufsize);

  return bufsize - 4;
}

static int xidctr;
enum clnt_stat
srpc_callraw (int fd, u_int32_t prog, u_int32_t vers, u_int32_t proc,
	      xdrproc_t inproc, void *in, xdrproc_t outproc, void *out,
	      AUTH *auth)
{
  u_int32_t buf[(MAXMSG+7)/4];
  XDR x;
  u_int32_t xid = ++xidctr;
  enum clnt_stat err;
  size_t msgsize;
  ssize_t n;
  struct rpc_msg msg;

  xdrmem_create (&x, (char *) &buf[1], MAXMSG, XDR_ENCODE);
  err = srpc_marshal_call (&x, xid, prog, vers, proc, auth, inproc, in);
  xdr_destroy (&x);
  if (err)
    return err;

  msgsize = xdr_getpos (&x);
  buf[0] = htonl (0x80000000|msgsize);
  if ((size_t) write (fd, buf, msgsize + 4) != msgsize + 4)
    return RPC_CANTSEND;

  n = readpkt (fd, buf, sizeof (buf));
  if (n <= 0)
    return RPC_CANTRECV;
  msgsize = n;

  xdrmem_create (&x, (char *) &buf[1], msgsize, XDR_DECODE);
  bzero (&msg, sizeof (msg));
  msg.acpted_rply.ar_results.where = (char *) out;
  msg.acpted_rply.ar_results.proc = outproc;
  err = srpc_marshal_reply (&x, &msg);
  if (msg.rm_direction == REPLY && msg.rm_reply.rp_stat == MSG_ACCEPTED
      && msg.acpted_rply.ar_verf.oa_base)
    free (msg.acpted_rply.ar_verf.oa_base);
  xdr_destroy (&x);

  return err;
}

enum clnt_stat
srpc_call (const struct rpc_program *rpp, int fd, u_int32_t proc,
	   void *in, void *out)
{
  assert (proc < rpp->nproc);
  return srpc_callraw (fd, rpp->progno, rpp->versno, proc,
		       rpp->tbl[proc].xdr_arg, in,
		       rpp->tbl[proc].xdr_res, out, NULL);
}