1 /**	$MirOS: src/include/rpc/svc.h,v 1.2 2005/03/06 19:13:41 tg Exp $ */
2 /*	$OpenBSD: svc.h,v 1.9 2004/01/22 21:48:02 espie Exp $	*/
3 /*	$NetBSD: svc.h,v 1.9 1995/04/29 05:28:01 cgd Exp $	*/
4 
5 /*
6  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
7  * unrestricted use provided that this legend is included on all tape
8  * media and as a part of the software program in whole or part.  Users
9  * may copy or modify Sun RPC without charge, but are not authorized
10  * to license or distribute it to anyone else except as part of a product or
11  * program developed by the user.
12  *
13  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
14  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
15  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16  *
17  * Sun RPC is provided with no support and without any obligation on the
18  * part of Sun Microsystems, Inc. to assist in its use, correction,
19  * modification or enhancement.
20  *
21  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
22  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
23  * OR ANY PART THEREOF.
24  *
25  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
26  * or profits or other special, indirect and consequential damages, even if
27  * Sun has been advised of the possibility of such damages.
28  *
29  * Sun Microsystems, Inc.
30  * 2550 Garcia Avenue
31  * Mountain View, California  94043
32  *
33  *	from: @(#)svc.h 1.20 88/02/08 SMI
34  *	@(#)svc.h	2.2 88/07/29 4.0 RPCSRC
35  */
36 
37 /*
38  * svc.h, Server-side remote procedure call interface.
39  *
40  * Copyright (C) 1984, Sun Microsystems, Inc.
41  */
42 
43 #ifndef _RPC_SVC_H
44 #define _RPC_SVC_H
45 #include <sys/cdefs.h>
46 #include <sys/poll.h>
47 
48 /*
49  * This interface must manage two items concerning remote procedure calling:
50  *
51  * 1) An arbitrary number of transport connections upon which rpc requests
52  * are received.  The two most notable transports are TCP and UDP;  they are
53  * created and registered by routines in svc_tcp.c and svc_udp.c, respectively;
54  * they in turn call xprt_register and xprt_unregister.
55  *
56  * 2) An arbitrary number of locally registered services.  Services are
57  * described by the following four data: program number, version number,
58  * "service dispatch" function, a transport handle, and a boolean that
59  * indicates whether or not the exported program should be registered with a
60  * local binder service;  if true the program's number and version and the
61  * port number from the transport handle are registered with the binder.
62  * These data are registered with the rpc svc system via svc_register.
63  *
64  * A service's dispatch function is called whenever an rpc request comes in
65  * on a transport.  The request's program and version numbers must match
66  * those of the registered service.  The dispatch function is passed two
67  * parameters, struct svc_req * and SVCXPRT *, defined below.
68  */
69 
70 enum xprt_stat {
71 	XPRT_DIED,
72 	XPRT_MOREREQS,
73 	XPRT_IDLE
74 };
75 
76 /*
77  * Server side transport handle
78  */
79 typedef struct __rpc_svcxprt {
80 	int		xp_sock;
81 	unsigned short	xp_port;	 /* associated port number */
82 	struct xp_ops {
83 		/* receive incomming requests */
84 		bool_t	(*xp_recv)(struct __rpc_svcxprt *,
85 			    struct rpc_msg *);
86 		/* get transport status */
87 		enum xprt_stat (*xp_stat)(struct __rpc_svcxprt *);
88 		/* get arguments */
89 		bool_t	(*xp_getargs)(struct __rpc_svcxprt *, xdrproc_t,
90 			    caddr_t);
91 		/* send reply */
92 		bool_t	(*xp_reply)(struct __rpc_svcxprt *, struct rpc_msg *);
93 		/* free mem allocated for args */
94 		bool_t	(*xp_freeargs)(struct __rpc_svcxprt *, xdrproc_t,
95 			    caddr_t);
96 		/* destroy this struct */
97 		void	(*xp_destroy)(struct __rpc_svcxprt *);
98 	} *xp_ops;
99 	socklen_t	xp_addrlen;	 /* length of remote address */
100 	struct sockaddr_in xp_raddr;	 /* remote address */
101 	struct opaque_auth xp_verf;	 /* raw response verifier */
102 	caddr_t		xp_p1;		 /* private */
103 	caddr_t		xp_p2;		 /* private */
104 } SVCXPRT;
105 
106 /*
107  *  Approved way of getting address of caller
108  */
109 #define svc_getcaller(x) (&(x)->xp_raddr)
110 
111 /*
112  * Operations defined on an SVCXPRT handle
113  *
114  * SVCXPRT		*xprt;
115  * struct rpc_msg	*msg;
116  * xdrproc_t		 xargs;
117  * caddr_t		 argsp;
118  */
119 #define SVC_RECV(xprt, msg)				\
120 	(*(xprt)->xp_ops->xp_recv)((xprt), (msg))
121 #define svc_recv(xprt, msg)				\
122 	(*(xprt)->xp_ops->xp_recv)((xprt), (msg))
123 
124 #define SVC_STAT(xprt)					\
125 	(*(xprt)->xp_ops->xp_stat)(xprt)
126 #define svc_stat(xprt)					\
127 	(*(xprt)->xp_ops->xp_stat)(xprt)
128 
129 #define SVC_GETARGS(xprt, xargs, argsp)			\
130 	(*(xprt)->xp_ops->xp_getargs)((xprt), (xargs), (argsp))
131 #define svc_getargs(xprt, xargs, argsp)			\
132 	(*(xprt)->xp_ops->xp_getargs)((xprt), (xargs), (argsp))
133 
134 #define SVC_REPLY(xprt, msg)				\
135 	(*(xprt)->xp_ops->xp_reply) ((xprt), (msg))
136 #define svc_reply(xprt, msg)				\
137 	(*(xprt)->xp_ops->xp_reply) ((xprt), (msg))
138 
139 #define SVC_FREEARGS(xprt, xargs, argsp)		\
140 	(*(xprt)->xp_ops->xp_freeargs)((xprt), (xargs), (argsp))
141 #define svc_freeargs(xprt, xargs, argsp)		\
142 	(*(xprt)->xp_ops->xp_freeargs)((xprt), (xargs), (argsp))
143 
144 #define SVC_DESTROY(xprt)				\
145 	(*(xprt)->xp_ops->xp_destroy)(xprt)
146 #define svc_destroy(xprt)				\
147 	(*(xprt)->xp_ops->xp_destroy)(xprt)
148 
149 
150 /*
151  * Service request
152  */
153 struct svc_req {
154 	u_int32_t	rq_prog;	/* service program number */
155 	u_int32_t	rq_vers;	/* service protocol version */
156 	u_int32_t	rq_proc;	/* the desired procedure */
157 	struct opaque_auth rq_cred;	/* raw creds from the wire */
158 	caddr_t		rq_clntcred;	/* read only cooked cred */
159 	SVCXPRT	*rq_xprt;		/* associated transport */
160 };
161 
162 
163 /*
164  * Service registration
165  *
166  * svc_register(xprt, prog, vers, dispatch, protocol)
167  *	SVCXPRT *xprt;
168  *	unsigned long prog;
169  *	unsigned long vers;
170  *	void (*dispatch)(struct svc_req *, SVCXPRT *);
171  *	int protocol;    like TCP or UDP, zero means do not register
172  */
173 __BEGIN_DECLS
174 extern bool_t	svc_register(SVCXPRT *, unsigned long, unsigned long,
175 		    void (*)(struct svc_req *, SVCXPRT *), int);
176 __END_DECLS
177 
178 /*
179  * Service un-registration
180  *
181  * svc_unregister(prog, vers)
182  *	unsigned long prog;
183  *	unsigned long vers;
184  */
185 __BEGIN_DECLS
186 extern void	svc_unregister(unsigned long, unsigned long);
187 __END_DECLS
188 
189 /*
190  * Transport registration.
191  *
192  * xprt_register(xprt)
193  *	SVCXPRT *xprt;
194  */
195 __BEGIN_DECLS
196 extern void	xprt_register(SVCXPRT *);
197 extern int	__xprt_register(SVCXPRT *);
198 __END_DECLS
199 
200 /*
201  * Transport un-register
202  *
203  * xprt_unregister(xprt)
204  *	SVCXPRT *xprt;
205  */
206 __BEGIN_DECLS
207 extern void	xprt_unregister(SVCXPRT *);
208 __END_DECLS
209 
210 
211 
212 
213 /*
214  * When the service routine is called, it must first check to see if it
215  * knows about the procedure;  if not, it should call svcerr_noproc
216  * and return.  If so, it should deserialize its arguments via
217  * SVC_GETARGS (defined above).  If the deserialization does not work,
218  * svcerr_decode should be called followed by a return.  Successful
219  * decoding of the arguments should be followed the execution of the
220  * procedure's code and a call to svc_sendreply.
221  *
222  * Also, if the service refuses to execute the procedure due to too-
223  * weak authentication parameters, svcerr_weakauth should be called.
224  * Note: do not confuse access-control failure with weak authentication!
225  *
226  * NB: In pure implementations of rpc, the caller always waits for a reply
227  * msg.  This message is sent when svc_sendreply is called.
228  * Therefore pure service implementations should always call
229  * svc_sendreply even if the function logically returns void;  use
230  * xdr.h - xdr_void for the xdr routine.  HOWEVER, tcp based rpc allows
231  * for the abuse of pure rpc via batched calling or pipelining.  In the
232  * case of a batched call, svc_sendreply should NOT be called since
233  * this would send a return message, which is what batching tries to avoid.
234  * It is the service/protocol writer's responsibility to know which calls are
235  * batched and which are not.  Warning: responding to batch calls may
236  * deadlock the caller and server processes!
237  */
238 
239 __BEGIN_DECLS
240 extern bool_t	svc_sendreply(SVCXPRT *, xdrproc_t, char *);
241 extern void	svcerr_decode(SVCXPRT *);
242 extern void	svcerr_weakauth(SVCXPRT *);
243 extern void	svcerr_noproc(SVCXPRT *);
244 extern void	svcerr_progvers(SVCXPRT *, unsigned long, unsigned long);
245 extern void	svcerr_auth(SVCXPRT *, enum auth_stat);
246 extern void	svcerr_noprog(SVCXPRT *);
247 extern void	svcerr_systemerr(SVCXPRT *);
248 __END_DECLS
249 
250 /*
251  * Lowest level dispatching -OR- who owns this process anyway.
252  * Somebody has to wait for incoming requests and then call the correct
253  * service routine.  The routine svc_run does infinite waiting; i.e.,
254  * svc_run never returns.
255  * Since another (co-existant) package may wish to selectively wait for
256  * incoming calls or other events outside of the rpc architecture, the
257  * routine svc_getreq is provided.  It must be passed readfds, the
258  * "in-place" results of a select system call (see select, section 2).
259  */
260 
261 /*
262  * Global keeper of rpc service descriptors in use
263  * dynamic; must be inspected before each call to select
264  */
265 extern fd_set svc_fdset;
266 #define svc_fds svc_fdset.fds_bits[0]	/* compatibility */
267 extern struct pollfd *svc_pollfd;
268 extern int svc_max_pollfd;
269 extern int svc_maxfd;			/* non-standard */
270 
271 /*
272  * a small program implemented by the svc_rpc implementation itself;
273  * also see clnt.h for protocol numbers.
274  */
275 extern void rpctest_service();				/* XXX relic? */
276 
277 __BEGIN_DECLS
278 extern void	svc_getreq(int);
279 extern void	svc_getreq_common(int);
280 extern void	svc_getreq_poll(struct pollfd *, const int);
281 extern void	svc_getreqset(fd_set *);
282 extern void	svc_getreqset2(fd_set *, int);
283 extern void	svc_run(void);
284 __END_DECLS
285 
286 /*
287  * Socket to use on svcxxx_create call to get default socket
288  */
289 #define	RPC_ANYSOCK	-1
290 
291 /*
292  * These are the existing service side transport implementations
293  */
294 
295 /*
296  * Memory based rpc for testing and timing.
297  */
298 __BEGIN_DECLS
299 extern SVCXPRT *svcraw_create(void);
300 __END_DECLS
301 
302 
303 /*
304  * Udp based rpc.
305  */
306 __BEGIN_DECLS
307 extern SVCXPRT *svcudp_create(int);
308 extern SVCXPRT *svcudp_bufcreate(int, unsigned int, unsigned int);
309 extern SVCXPRT *svcudp_bufcreate_withport(int, unsigned int, unsigned int,
310     unsigned short);
311 __END_DECLS
312 
313 
314 /*
315  * Tcp based rpc.
316  */
317 __BEGIN_DECLS
318 extern SVCXPRT *svctcp_create(int, unsigned int, unsigned int);
319 extern SVCXPRT *svctcp_create_withport(int, unsigned int, unsigned int,
320     unsigned short);
321 __END_DECLS
322 
323 /*
324  * Fd based rpc.
325  */
326 __BEGIN_DECLS
327 extern SVCXPRT *svcfd_create(int, unsigned int, unsigned int);
328 __END_DECLS
329 
330 #endif /* !_RPC_SVC_H */
331