drop the use of getprotobyname_r()
[project/librpc-uclibc.git] / rpc_prot.c
1 /* @(#)rpc_prot.c       2.3 88/08/07 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if 0
31 static char sccsid[] = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * rpc_prot.c
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  *
39  * This set of routines implements the rpc message definition,
40  * its serializer and some common rpc utility routines.
41  * The routines are meant for various implementations of rpc -
42  * they are NOT for the rpc client or rpc service implementations!
43  * Because authentication stuff is easy and is part of rpc, the opaque
44  * routines are also in this program.
45  */
46
47 #define __FORCE_GLIBC
48 #include <features.h>
49
50 #include <sys/param.h>
51
52 #include <rpc/rpc.h>
53
54
55 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
56
57 /*
58  * XDR an opaque authentication struct
59  * (see auth.h)
60  */
61 bool_t
62 xdr_opaque_auth (XDR *xdrs, struct opaque_auth *ap)
63 {
64
65   if (xdr_enum (xdrs, &(ap->oa_flavor)))
66     return xdr_bytes (xdrs, &ap->oa_base,
67                       &ap->oa_length, MAX_AUTH_BYTES);
68   return FALSE;
69 }
70 libc_hidden_def(xdr_opaque_auth)
71
72 /*
73  * XDR a DES block
74  */
75 bool_t
76 xdr_des_block (XDR *xdrs, des_block *blkp)
77 {
78   return xdr_opaque (xdrs, (caddr_t) blkp, sizeof (des_block));
79 }
80
81 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
82
83 /*
84  * XDR the MSG_ACCEPTED part of a reply message union
85  */
86 bool_t
87 xdr_accepted_reply (XDR *xdrs, struct accepted_reply *ar)
88 {
89   /* personalized union, rather than calling xdr_union */
90   if (!xdr_opaque_auth (xdrs, &(ar->ar_verf)))
91     return FALSE;
92   if (!xdr_enum (xdrs, (enum_t *) & (ar->ar_stat)))
93     return FALSE;
94   switch (ar->ar_stat)
95     {
96     case SUCCESS:
97       return ((*(ar->ar_results.proc)) (xdrs, ar->ar_results.where));
98     case PROG_MISMATCH:
99       if (!xdr_u_long (xdrs, &(ar->ar_vers.low)))
100         return FALSE;
101       return (xdr_u_long (xdrs, &(ar->ar_vers.high)));
102     default:
103       return TRUE;
104     }
105   return TRUE;          /* TRUE => open ended set of problems */
106 }
107 libc_hidden_def(xdr_accepted_reply)
108
109 /*
110  * XDR the MSG_DENIED part of a reply message union
111  */
112 bool_t
113 xdr_rejected_reply (XDR *xdrs, struct rejected_reply *rr)
114 {
115   /* personalized union, rather than calling xdr_union */
116   if (!xdr_enum (xdrs, (enum_t *) & (rr->rj_stat)))
117     return FALSE;
118   switch (rr->rj_stat)
119     {
120     case RPC_MISMATCH:
121       if (!xdr_u_long (xdrs, &(rr->rj_vers.low)))
122         return FALSE;
123       return xdr_u_long (xdrs, &(rr->rj_vers.high));
124
125     case AUTH_ERROR:
126       return xdr_enum (xdrs, (enum_t *) & (rr->rj_why));
127     }
128   return FALSE;
129 }
130 libc_hidden_def(xdr_rejected_reply)
131
132 static const struct xdr_discrim reply_dscrm[3] =
133 {
134   {(int) MSG_ACCEPTED, (xdrproc_t) xdr_accepted_reply},
135   {(int) MSG_DENIED, (xdrproc_t) xdr_rejected_reply},
136   {__dontcare__, NULL_xdrproc_t}};
137
138 /*
139  * XDR a reply message
140  */
141 bool_t
142 xdr_replymsg (XDR *xdrs, struct rpc_msg *rmsg)
143 {
144   if (xdr_u_long (xdrs, &(rmsg->rm_xid)) &&
145       xdr_enum (xdrs, (enum_t *) & (rmsg->rm_direction)) &&
146       (rmsg->rm_direction == REPLY))
147     return xdr_union (xdrs, (enum_t *) & (rmsg->rm_reply.rp_stat),
148                       (caddr_t) & (rmsg->rm_reply.ru), reply_dscrm,
149                       NULL_xdrproc_t);
150   return FALSE;
151 }
152 libc_hidden_def(xdr_replymsg)
153
154
155 /*
156  * Serializes the "static part" of a call message header.
157  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
158  * The rm_xid is not really static, but the user can easily munge on the fly.
159  */
160 bool_t
161 xdr_callhdr (XDR *xdrs, struct rpc_msg *cmsg)
162 {
163
164   cmsg->rm_direction = CALL;
165   cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
166   if (
167        (xdrs->x_op == XDR_ENCODE) &&
168        xdr_u_long (xdrs, &(cmsg->rm_xid)) &&
169        xdr_enum (xdrs, (enum_t *) & (cmsg->rm_direction)) &&
170        xdr_u_long (xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
171        xdr_u_long (xdrs, &(cmsg->rm_call.cb_prog)))
172     return xdr_u_long (xdrs, &(cmsg->rm_call.cb_vers));
173   return FALSE;
174 }
175 libc_hidden_def(xdr_callhdr)
176
177 /* ************************** Client utility routine ************* */
178
179 static void
180 accepted (enum accept_stat acpt_stat,
181           struct rpc_err *error)
182 {
183   switch (acpt_stat)
184     {
185
186     case PROG_UNAVAIL:
187       error->re_status = RPC_PROGUNAVAIL;
188       return;
189
190     case PROG_MISMATCH:
191       error->re_status = RPC_PROGVERSMISMATCH;
192       return;
193
194     case PROC_UNAVAIL:
195       error->re_status = RPC_PROCUNAVAIL;
196       return;
197
198     case GARBAGE_ARGS:
199       error->re_status = RPC_CANTDECODEARGS;
200       return;
201
202     case SYSTEM_ERR:
203       error->re_status = RPC_SYSTEMERROR;
204       return;
205
206     case SUCCESS:
207       error->re_status = RPC_SUCCESS;
208       return;
209     }
210   /* something's wrong, but we don't know what ... */
211   error->re_status = RPC_FAILED;
212   error->re_lb.s1 = (long) MSG_ACCEPTED;
213   error->re_lb.s2 = (long) acpt_stat;
214 }
215
216 static void
217 rejected (enum reject_stat rjct_stat,
218           struct rpc_err *error)
219 {
220   switch (rjct_stat)
221     {
222     case RPC_MISMATCH:
223       error->re_status = RPC_VERSMISMATCH;
224       return;
225     case AUTH_ERROR:
226       error->re_status = RPC_AUTHERROR;
227       return;
228     default:
229       /* something's wrong, but we don't know what ... */
230       error->re_status = RPC_FAILED;
231       error->re_lb.s1 = (long) MSG_DENIED;
232       error->re_lb.s2 = (long) rjct_stat;
233       return;
234     }
235 }
236
237 /*
238  * given a reply message, fills in the error
239  */
240 void
241 _seterr_reply (struct rpc_msg *msg,
242                struct rpc_err *error)
243 {
244   /* optimized for normal, SUCCESSful case */
245   switch (msg->rm_reply.rp_stat)
246     {
247     case MSG_ACCEPTED:
248       if (msg->acpted_rply.ar_stat == SUCCESS)
249         {
250           error->re_status = RPC_SUCCESS;
251           return;
252         };
253       accepted (msg->acpted_rply.ar_stat, error);
254       break;
255
256     case MSG_DENIED:
257       rejected (msg->rjcted_rply.rj_stat, error);
258       break;
259
260     default:
261       error->re_status = RPC_FAILED;
262       error->re_lb.s1 = (long) (msg->rm_reply.rp_stat);
263       break;
264     }
265   switch (error->re_status)
266     {
267
268     case RPC_VERSMISMATCH:
269       error->re_vers.low = msg->rjcted_rply.rj_vers.low;
270       error->re_vers.high = msg->rjcted_rply.rj_vers.high;
271       break;
272
273     case RPC_AUTHERROR:
274       error->re_why = msg->rjcted_rply.rj_why;
275       break;
276
277     case RPC_PROGVERSMISMATCH:
278       error->re_vers.low = msg->acpted_rply.ar_vers.low;
279       error->re_vers.high = msg->acpted_rply.ar_vers.high;
280       break;
281     default:
282       break;
283     }
284 }
285 libc_hidden_def(_seterr_reply)