xref: /freebsd/include/rpc/clnt.h (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*	$NetBSD: clnt.h,v 1.14 2000/06/02 22:57:55 fvdl Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2010, Oracle America, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of the "Oracle America, Inc." nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  *	from: @(#)clnt.h 1.31 94/04/29 SMI
33  *	from: @(#)clnt.h	2.1 88/07/29 4.0 RPCSRC
34  * $FreeBSD$
35  */
36 
37 /*
38  * clnt.h - Client side remote procedure call interface.
39  */
40 
41 #ifndef _RPC_CLNT_H_
42 #define _RPC_CLNT_H_
43 #include <rpc/clnt_stat.h>
44 #include <sys/cdefs.h>
45 #include <netconfig.h>
46 #include <sys/un.h>
47 
48 /*
49  * Well-known IPV6 RPC broadcast address.
50  */
51 #define RPCB_MULTICAST_ADDR "ff02::202"
52 
53 /*
54  * the following errors are in general unrecoverable.  The caller
55  * should give up rather than retry.
56  */
57 #define IS_UNRECOVERABLE_RPC(s) (((s) == RPC_AUTHERROR) || \
58 	((s) == RPC_CANTENCODEARGS) || \
59 	((s) == RPC_CANTDECODERES) || \
60 	((s) == RPC_VERSMISMATCH) || \
61 	((s) == RPC_PROCUNAVAIL) || \
62 	((s) == RPC_PROGUNAVAIL) || \
63 	((s) == RPC_PROGVERSMISMATCH) || \
64 	((s) == RPC_CANTDECODEARGS))
65 
66 /*
67  * Error info.
68  */
69 struct rpc_err {
70 	enum clnt_stat re_status;
71 	union {
72 		int RE_errno;		/* related system error */
73 		enum auth_stat RE_why;	/* why the auth error occurred */
74 		struct {
75 			rpcvers_t low;	/* lowest version supported */
76 			rpcvers_t high;	/* highest version supported */
77 		} RE_vers;
78 		struct {		/* maybe meaningful if RPC_FAILED */
79 			int32_t s1;
80 			int32_t s2;
81 		} RE_lb;		/* life boot & debugging only */
82 	} ru;
83 #define	re_errno	ru.RE_errno
84 #define	re_why		ru.RE_why
85 #define	re_vers		ru.RE_vers
86 #define	re_lb		ru.RE_lb
87 };
88 
89 
90 /*
91  * Client rpc handle.
92  * Created by individual implementations
93  * Client is responsible for initializing auth, see e.g. auth_none.c.
94  */
95 typedef struct __rpc_client {
96 	AUTH	*cl_auth;			/* authenticator */
97 	struct clnt_ops {
98 		/* call remote procedure */
99 		enum clnt_stat	(*cl_call)(struct __rpc_client *,
100 				    rpcproc_t, xdrproc_t, void *, xdrproc_t,
101 				        void *, struct timeval);
102 		/* abort a call */
103 		void		(*cl_abort)(struct __rpc_client *);
104 		/* get specific error code */
105 		void		(*cl_geterr)(struct __rpc_client *,
106 					struct rpc_err *);
107 		/* frees results */
108 		bool_t		(*cl_freeres)(struct __rpc_client *,
109 					xdrproc_t, void *);
110 		/* destroy this structure */
111 		void		(*cl_destroy)(struct __rpc_client *);
112 		/* the ioctl() of rpc */
113 		bool_t          (*cl_control)(struct __rpc_client *, u_int,
114 				    void *);
115 	} *cl_ops;
116 	void 			*cl_private;	/* private stuff */
117 	char			*cl_netid;	/* network token */
118 	char			*cl_tp;		/* device name */
119 } CLIENT;
120 
121 
122 /*
123  * Timers used for the pseudo-transport protocol when using datagrams
124  */
125 struct rpc_timers {
126 	u_short		rt_srtt;	/* smoothed round-trip time */
127 	u_short		rt_deviate;	/* estimated deviation */
128 	u_long		rt_rtxcur;	/* current (backed-off) rto */
129 };
130 
131 /*
132  * Feedback values used for possible congestion and rate control
133  */
134 #define FEEDBACK_REXMIT1	1	/* first retransmit */
135 #define FEEDBACK_OK		2	/* no retransmits */
136 
137 /* Used to set version of portmapper used in broadcast */
138 
139 #define CLCR_SET_LOWVERS	3
140 #define CLCR_GET_LOWVERS	4
141 
142 #define RPCSMALLMSGSIZE 400	/* a more reasonable packet size */
143 
144 /*
145  * client side rpc interface ops
146  *
147  * Parameter types are:
148  *
149  */
150 
151 /*
152  * enum clnt_stat
153  * CLNT_CALL(rh, proc, xargs, argsp, xres, resp, timeout)
154  * 	CLIENT *rh;
155  *	rpcproc_t proc;
156  *	xdrproc_t xargs;
157  *	void *argsp;
158  *	xdrproc_t xres;
159  *	void *resp;
160  *	struct timeval timeout;
161  */
162 #define	CLNT_CALL(rh, proc, xargs, argsp, xres, resp, secs) \
163 	((*(rh)->cl_ops->cl_call)(rh, proc, xargs, \
164 		argsp, xres, resp, secs))
165 #define	clnt_call(rh, proc, xargs, argsp, xres, resp, secs) \
166 	((*(rh)->cl_ops->cl_call)(rh, proc, xargs, \
167 		argsp, xres, resp, secs))
168 
169 /*
170  * void
171  * CLNT_ABORT(rh);
172  * 	CLIENT *rh;
173  */
174 #define	CLNT_ABORT(rh)	((*(rh)->cl_ops->cl_abort)(rh))
175 #define	clnt_abort(rh)	((*(rh)->cl_ops->cl_abort)(rh))
176 
177 /*
178  * struct rpc_err
179  * CLNT_GETERR(rh);
180  * 	CLIENT *rh;
181  */
182 #define	CLNT_GETERR(rh,errp)	((*(rh)->cl_ops->cl_geterr)(rh, errp))
183 #define	clnt_geterr(rh,errp)	((*(rh)->cl_ops->cl_geterr)(rh, errp))
184 
185 
186 /*
187  * bool_t
188  * CLNT_FREERES(rh, xres, resp);
189  * 	CLIENT *rh;
190  *	xdrproc_t xres;
191  *	void *resp;
192  */
193 #define	CLNT_FREERES(rh,xres,resp) ((*(rh)->cl_ops->cl_freeres)(rh,xres,resp))
194 #define	clnt_freeres(rh,xres,resp) ((*(rh)->cl_ops->cl_freeres)(rh,xres,resp))
195 
196 /*
197  * bool_t
198  * CLNT_CONTROL(cl, request, info)
199  *      CLIENT *cl;
200  *      u_int request;
201  *      char *info;
202  */
203 #define	CLNT_CONTROL(cl,rq,in) ((*(cl)->cl_ops->cl_control)(cl,rq,in))
204 #define	clnt_control(cl,rq,in) ((*(cl)->cl_ops->cl_control)(cl,rq,in))
205 
206 /*
207  * control operations that apply to both udp and tcp transports
208  */
209 #define CLSET_TIMEOUT		1	/* set timeout (timeval) */
210 #define CLGET_TIMEOUT		2	/* get timeout (timeval) */
211 #define CLGET_SERVER_ADDR	3	/* get server's address (sockaddr) */
212 #define CLGET_FD		6	/* get connections file descriptor */
213 #define CLGET_SVC_ADDR		7	/* get server's address (netbuf) */
214 #define CLSET_FD_CLOSE		8	/* close fd while clnt_destroy */
215 #define CLSET_FD_NCLOSE		9	/* Do not close fd while clnt_destroy */
216 #define CLGET_XID 		10	/* Get xid */
217 #define CLSET_XID		11	/* Set xid */
218 #define CLGET_VERS		12	/* Get version number */
219 #define CLSET_VERS		13	/* Set version number */
220 #define CLGET_PROG		14	/* Get program number */
221 #define CLSET_PROG		15	/* Set program number */
222 #define CLSET_SVC_ADDR		16	/* get server's address (netbuf) */
223 #define CLSET_PUSH_TIMOD	17	/* push timod if not already present */
224 #define CLSET_POP_TIMOD		18	/* pop timod */
225 /*
226  * Connectionless only control operations
227  */
228 #define CLSET_RETRY_TIMEOUT 4   /* set retry timeout (timeval) */
229 #define CLGET_RETRY_TIMEOUT 5   /* get retry timeout (timeval) */
230 #define CLSET_ASYNC		19
231 #define CLSET_CONNECT		20	/* Use connect() for UDP. (int) */
232 
233 /*
234  * void
235  * CLNT_DESTROY(rh);
236  * 	CLIENT *rh;
237  */
238 #define	CLNT_DESTROY(rh)	((*(rh)->cl_ops->cl_destroy)(rh))
239 #define	clnt_destroy(rh)	((*(rh)->cl_ops->cl_destroy)(rh))
240 
241 
242 /*
243  * RPCTEST is a test program which is accessible on every rpc
244  * transport/port.  It is used for testing, performance evaluation,
245  * and network administration.
246  */
247 
248 #define RPCTEST_PROGRAM		((rpcprog_t)1)
249 #define RPCTEST_VERSION		((rpcvers_t)1)
250 #define RPCTEST_NULL_PROC	((rpcproc_t)2)
251 #define RPCTEST_NULL_BATCH_PROC	((rpcproc_t)3)
252 
253 /*
254  * By convention, procedure 0 takes null arguments and returns them
255  */
256 
257 #define NULLPROC ((rpcproc_t)0)
258 
259 /*
260  * Below are the client handle creation routines for the various
261  * implementations of client side rpc.  They can return NULL if a
262  * creation failure occurs.
263  */
264 
265 /*
266  * Generic client creation routine. Supported protocols are those that
267  * belong to the nettype namespace (/etc/netconfig).
268  */
269 __BEGIN_DECLS
270 extern CLIENT *clnt_create(const char *, const rpcprog_t, const rpcvers_t,
271 			   const char *);
272 /*
273  *
274  * 	const char *hostname;			-- hostname
275  *	const rpcprog_t prog;			-- program number
276  *	const rpcvers_t vers;			-- version number
277  *	const char *nettype;			-- network type
278  */
279 
280  /*
281  * Generic client creation routine. Just like clnt_create(), except
282  * it takes an additional timeout parameter.
283  */
284 extern CLIENT * clnt_create_timed(const char *, const rpcprog_t,
285 	const rpcvers_t, const char *, const struct timeval *);
286 /*
287  *
288  *	const char *hostname;			-- hostname
289  *	const rpcprog_t prog;			-- program number
290  *	const rpcvers_t vers;			-- version number
291  *	const char *nettype;			-- network type
292  *	const struct timeval *tp;		-- timeout
293  */
294 
295 /*
296  * Generic client creation routine. Supported protocols are which belong
297  * to the nettype name space.
298  */
299 extern CLIENT *clnt_create_vers(const char *, const rpcprog_t, rpcvers_t *,
300 				const rpcvers_t, const rpcvers_t,
301 				const char *);
302 /*
303  *	const char *host;		-- hostname
304  *	const rpcprog_t prog;		-- program number
305  *	rpcvers_t *vers_out;		-- servers highest available version
306  *	const rpcvers_t vers_low;	-- low version number
307  *	const rpcvers_t vers_high;	-- high version number
308  *	const char *nettype;		-- network type
309  */
310 
311 /*
312  * Generic client creation routine. Supported protocols are which belong
313  * to the nettype name space.
314  */
315 extern CLIENT * clnt_create_vers_timed(const char *, const rpcprog_t,
316 	rpcvers_t *, const rpcvers_t, const rpcvers_t, const char *,
317 	const struct timeval *);
318 /*
319  *	const char *host;		-- hostname
320  *	const rpcprog_t prog;		-- program number
321  *	rpcvers_t *vers_out;		-- servers highest available version
322  *	const rpcvers_t vers_low;	-- low version number
323  *	const rpcvers_t vers_high;	-- high version number
324  *	const char *nettype;		-- network type
325  *	const struct timeval *tp	-- timeout
326  */
327 
328 /*
329  * Generic client creation routine. It takes a netconfig structure
330  * instead of nettype
331  */
332 extern CLIENT *clnt_tp_create(const char *, const rpcprog_t,
333 			      const rpcvers_t, const struct netconfig *);
334 /*
335  *	const char *hostname;			-- hostname
336  *	const rpcprog_t prog;			-- program number
337  *	const rpcvers_t vers;			-- version number
338  *	const struct netconfig *netconf; 	-- network config structure
339  */
340 
341 /*
342  * Generic client creation routine. Just like clnt_tp_create(), except
343  * it takes an additional timeout parameter.
344  */
345 extern CLIENT * clnt_tp_create_timed(const char *, const rpcprog_t,
346 	const rpcvers_t, const struct netconfig *, const struct timeval *);
347 /*
348  *	const char *hostname;			-- hostname
349  *	const rpcprog_t prog;			-- program number
350  *	const rpcvers_t vers;			-- version number
351  *	const struct netconfig *netconf; 	-- network config structure
352  *	const struct timeval *tp		-- timeout
353  */
354 
355 /*
356  * Generic TLI create routine. Only provided for compatibility.
357  */
358 
359 extern CLIENT *clnt_tli_create(const int, const struct netconfig *,
360 			       struct netbuf *, const rpcprog_t,
361 			       const rpcvers_t, const u_int, const u_int);
362 /*
363  *	const register int fd;		-- fd
364  *	const struct netconfig *nconf;	-- netconfig structure
365  *	struct netbuf *svcaddr;		-- servers address
366  *	const u_long prog;			-- program number
367  *	const u_long vers;			-- version number
368  *	const u_int sendsz;			-- send size
369  *	const u_int recvsz;			-- recv size
370  */
371 
372 /*
373  * Low level clnt create routine for connectionful transports, e.g. tcp.
374  */
375 extern CLIENT *clnt_vc_create(const int, const struct netbuf *,
376 			      const rpcprog_t, const rpcvers_t,
377 			      u_int, u_int);
378 /*
379  * Added for compatibility to old rpc 4.0. Obsoleted by clnt_vc_create().
380  */
381 extern CLIENT *clntunix_create(struct sockaddr_un *,
382 			       u_long, u_long, int *, u_int, u_int);
383 /*
384  *	const int fd;				-- open file descriptor
385  *	const struct netbuf *svcaddr;		-- servers address
386  *	const rpcprog_t prog;			-- program number
387  *	const rpcvers_t vers;			-- version number
388  *	const u_int sendsz;			-- buffer recv size
389  *	const u_int recvsz;			-- buffer send size
390  */
391 
392 /*
393  * Low level clnt create routine for connectionless transports, e.g. udp.
394  */
395 extern CLIENT *clnt_dg_create(const int, const struct netbuf *,
396 			      const rpcprog_t, const rpcvers_t,
397 			      const u_int, const u_int);
398 /*
399  *	const int fd;				-- open file descriptor
400  *	const struct netbuf *svcaddr;		-- servers address
401  *	const rpcprog_t program;		-- program number
402  *	const rpcvers_t version;		-- version number
403  *	const u_int sendsz;			-- buffer recv size
404  *	const u_int recvsz;			-- buffer send size
405  */
406 
407 /*
408  * Memory based rpc (for speed check and testing)
409  * CLIENT *
410  * clnt_raw_create(prog, vers)
411  *	u_long prog;
412  *	u_long vers;
413  */
414 extern CLIENT *clnt_raw_create(rpcprog_t, rpcvers_t);
415 
416 __END_DECLS
417 
418 
419 /*
420  * Print why creation failed
421  */
422 __BEGIN_DECLS
423 extern void clnt_pcreateerror(const char *);			/* stderr */
424 extern char *clnt_spcreateerror(const char *);			/* string */
425 __END_DECLS
426 
427 /*
428  * Like clnt_perror(), but is more verbose in its output
429  */
430 __BEGIN_DECLS
431 extern void clnt_perrno(enum clnt_stat);		/* stderr */
432 extern char *clnt_sperrno(enum clnt_stat);		/* string */
433 __END_DECLS
434 
435 /*
436  * Print an English error message, given the client error code
437  */
438 __BEGIN_DECLS
439 extern void clnt_perror(CLIENT *, const char *);	 	/* stderr */
440 extern char *clnt_sperror(CLIENT *, const char *);		/* string */
441 __END_DECLS
442 
443 
444 /*
445  * If a creation fails, the following allows the user to figure out why.
446  */
447 struct rpc_createerr {
448 	enum clnt_stat cf_stat;
449 	struct rpc_err cf_error; /* useful when cf_stat == RPC_PMAPFAILURE */
450 };
451 
452 __BEGIN_DECLS
453 extern struct rpc_createerr	*__rpc_createerr(void);
454 __END_DECLS
455 #define rpc_createerr		(*(__rpc_createerr()))
456 
457 /*
458  * The simplified interface:
459  * enum clnt_stat
460  * rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
461  *	const char *host;
462  *	const rpcprog_t prognum;
463  *	const rpcvers_t versnum;
464  *	const rpcproc_t procnum;
465  *	const xdrproc_t inproc, outproc;
466  *	const char *in;
467  *	char *out;
468  *	const char *nettype;
469  */
470 __BEGIN_DECLS
471 extern enum clnt_stat rpc_call(const char *, const rpcprog_t,
472 			       const rpcvers_t, const rpcproc_t,
473 			       const xdrproc_t, const char *,
474 			       const xdrproc_t, char *, const char *);
475 __END_DECLS
476 
477 /*
478  * RPC broadcast interface
479  * The call is broadcasted to all locally connected nets.
480  *
481  * extern enum clnt_stat
482  * rpc_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp,
483  *			eachresult, nettype)
484  *	const rpcprog_t		prog;		-- program number
485  *	const rpcvers_t		vers;		-- version number
486  *	const rpcproc_t		proc;		-- procedure number
487  *	const xdrproc_t	xargs;		-- xdr routine for args
488  *	caddr_t		argsp;		-- pointer to args
489  *	const xdrproc_t	xresults;	-- xdr routine for results
490  *	caddr_t		resultsp;	-- pointer to results
491  *	const resultproc_t	eachresult;	-- call with each result
492  *	const char		*nettype;	-- Transport type
493  *
494  * For each valid response received, the procedure eachresult is called.
495  * Its form is:
496  *		done = eachresult(resp, raddr, nconf)
497  *			bool_t done;
498  *			caddr_t resp;
499  *			struct netbuf *raddr;
500  *			struct netconfig *nconf;
501  * where resp points to the results of the call and raddr is the
502  * address if the responder to the broadcast.  nconf is the transport
503  * on which the response was received.
504  *
505  * extern enum clnt_stat
506  * rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp,
507  *			eachresult, inittime, waittime, nettype)
508  *	const rpcprog_t		prog;		-- program number
509  *	const rpcvers_t		vers;		-- version number
510  *	const rpcproc_t		proc;		-- procedure number
511  *	const xdrproc_t	xargs;		-- xdr routine for args
512  *	caddr_t		argsp;		-- pointer to args
513  *	const xdrproc_t	xresults;	-- xdr routine for results
514  *	caddr_t		resultsp;	-- pointer to results
515  *	const resultproc_t	eachresult;	-- call with each result
516  *	const int 		inittime;	-- how long to wait initially
517  *	const int 		waittime;	-- maximum time to wait
518  *	const char		*nettype;	-- Transport type
519  */
520 
521 typedef bool_t (*resultproc_t)(caddr_t, ...);
522 
523 __BEGIN_DECLS
524 extern enum clnt_stat rpc_broadcast(const rpcprog_t, const rpcvers_t,
525 				    const rpcproc_t, const xdrproc_t,
526 				    caddr_t, const xdrproc_t, caddr_t,
527 				    const resultproc_t, const char *);
528 extern enum clnt_stat rpc_broadcast_exp(const rpcprog_t, const rpcvers_t,
529 					const rpcproc_t, const xdrproc_t,
530 					caddr_t, const xdrproc_t, caddr_t,
531 					const resultproc_t, const int,
532 					const int, const char *);
533 __END_DECLS
534 
535 /* For backward compatibility */
536 #include <rpc/clnt_soc.h>
537 
538 #endif /* !_RPC_CLNT_H_ */
539