Merge from vendor branch TCPDUMP:
[dragonfly.git] / contrib / amd / libamu / misc_rpc.c
1 /*
2  * Copyright (c) 1997-1999 Erez Zadok
3  * Copyright (c) 1990 Jan-Simon Pendry
4  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgment:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      %W% (Berkeley) %G%
40  *
41  * $Id: misc_rpc.c,v 1.2 1999/01/10 21:54:37 ezk Exp $
42  *
43  */
44
45 /*
46  * Additions to Sun RPC.
47  */
48
49 #ifdef HAVE_CONFIG_H
50 # include <config.h>
51 #endif /* HAVE_CONFIG_H */
52 #include <am_defs.h>
53 #include <amu.h>
54
55 /*
56  * Some systems renamed _seterr_reply to __seterr_reply (with two
57  * leading underscores)
58  */
59 #if !defined(HAVE__SETERR_REPLY) && defined(HAVE___SETERR_REPLY)
60 # define _seterr_reply  __seterr_reply
61 #endif /* !defined(HAVE__SETERR_REPLY) && defined(HAVE___SETERR_REPLY) */
62
63
64 void
65 rpc_msg_init(struct rpc_msg *mp, u_long prog, u_long vers, u_long proc)
66 {
67   /*
68    * Initialize the message
69    */
70   memset((voidp) mp, 0, sizeof(*mp));
71   mp->rm_xid = 0;
72   mp->rm_direction = CALL;
73   mp->rm_call.cb_rpcvers = RPC_MSG_VERSION;
74   mp->rm_call.cb_prog = prog;
75   mp->rm_call.cb_vers = vers;
76   mp->rm_call.cb_proc = proc;
77 }
78
79
80 /*
81  * Field reply to call to mountd
82  */
83 int
84 pickup_rpc_reply(voidp pkt, int len, voidp where, XDRPROC_T_TYPE where_xdr)
85 {
86   XDR reply_xdr;
87   int ok;
88   struct rpc_err err;
89   struct rpc_msg reply_msg;
90   int error = 0;
91
92   /* memset((voidp) &err, 0, sizeof(err)); */
93   memset((voidp) &reply_msg, 0, sizeof(reply_msg));
94   memset((voidp) &reply_xdr, 0, sizeof(reply_xdr));
95
96   reply_msg.acpted_rply.ar_results.where = (caddr_t) where;
97   reply_msg.acpted_rply.ar_results.proc = where_xdr;
98
99   xdrmem_create(&reply_xdr, pkt, len, XDR_DECODE);
100
101   ok = xdr_replymsg(&reply_xdr, &reply_msg);
102   if (!ok) {
103     error = EIO;
104     goto drop;
105   }
106   _seterr_reply(&reply_msg, &err);
107   if (err.re_status != RPC_SUCCESS) {
108     error = EIO;
109     goto drop;
110   }
111
112 drop:
113   if (reply_msg.rm_reply.rp_stat == MSG_ACCEPTED &&
114       reply_msg.acpted_rply.ar_verf.oa_base) {
115     reply_xdr.x_op = XDR_FREE;
116     (void) xdr_opaque_auth(&reply_xdr,
117                            &reply_msg.acpted_rply.ar_verf);
118   }
119   xdr_destroy(&reply_xdr);
120
121   return error;
122 }
123
124
125 int
126 make_rpc_packet(char *buf, int buflen, u_long proc, struct rpc_msg *mp, voidp arg, XDRPROC_T_TYPE arg_xdr, AUTH *auth)
127 {
128   XDR msg_xdr;
129   int len;
130
131   xdrmem_create(&msg_xdr, buf, buflen, XDR_ENCODE);
132
133   /*
134    * Basic protocol header
135    */
136   if (!xdr_callhdr(&msg_xdr, mp))
137     return -EIO;
138
139   /*
140    * Called procedure number
141    */
142   if (!xdr_enum(&msg_xdr, (enum_t *) & proc))
143     return -EIO;
144
145   /*
146    * Authorization
147    */
148   if (!AUTH_MARSHALL(auth, &msg_xdr))
149     return -EIO;
150
151   /*
152    * Arguments
153    */
154   if (!(*arg_xdr) (&msg_xdr, arg))
155     return -EIO;
156
157   /*
158    * Determine length
159    */
160   len = xdr_getpos(&msg_xdr);
161
162   /*
163    * Throw away xdr
164    */
165   xdr_destroy(&msg_xdr);
166
167   return len;
168 }