svc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * svc.h, Server-side remote procedure call interface.
  3. *
  4. * Copyright (C) 1984, Sun Microsystems, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. * * Neither the name of Sun Microsystems, 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
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifndef _RPC_SVC_H
  34. #define _RPC_SVC_H 1
  35. #include <features.h>
  36. #include <rpc/rpc_msg.h>
  37. __BEGIN_DECLS
  38. /*
  39. * This interface must manage two items concerning remote procedure calling:
  40. *
  41. * 1) An arbitrary number of transport connections upon which rpc requests
  42. * are received. The two most notable transports are TCP and UDP; they are
  43. * created and registered by routines in svc_tcp.c and svc_udp.c, respectively;
  44. * they in turn call xprt_register and xprt_unregister.
  45. *
  46. * 2) An arbitrary number of locally registered services. Services are
  47. * described by the following four data: program number, version number,
  48. * "service dispatch" function, a transport handle, and a boolean that
  49. * indicates whether or not the exported program should be registered with a
  50. * local binder service; if true the program's number and version and the
  51. * port number from the transport handle are registered with the binder.
  52. * These data are registered with the rpc svc system via svc_register.
  53. *
  54. * A service's dispatch function is called whenever an rpc request comes in
  55. * on a transport. The request's program and version numbers must match
  56. * those of the registered service. The dispatch function is passed two
  57. * parameters, struct svc_req * and SVCXPRT *, defined below.
  58. */
  59. enum xprt_stat {
  60. XPRT_DIED,
  61. XPRT_MOREREQS,
  62. XPRT_IDLE
  63. };
  64. /*
  65. * Server side transport handle
  66. */
  67. typedef struct SVCXPRT SVCXPRT;
  68. struct SVCXPRT {
  69. int xp_sock;
  70. u_short xp_port; /* associated port number */
  71. const struct xp_ops {
  72. bool_t (*xp_recv) (SVCXPRT *__xprt, struct rpc_msg *__msg);
  73. /* receive incoming requests */
  74. enum xprt_stat (*xp_stat) (SVCXPRT *__xprt);
  75. /* get transport status */
  76. bool_t (*xp_getargs) (SVCXPRT *__xprt, xdrproc_t __xdr_args,
  77. caddr_t __args_ptr); /* get arguments */
  78. bool_t (*xp_reply) (SVCXPRT *__xprt, struct rpc_msg *__msg);
  79. /* send reply */
  80. bool_t (*xp_freeargs) (SVCXPRT *__xprt, xdrproc_t __xdr_args,
  81. caddr_t __args_ptr);
  82. /* free mem allocated for args */
  83. void (*xp_destroy) (SVCXPRT *__xprt);
  84. /* destroy this struct */
  85. } *xp_ops;
  86. int xp_addrlen; /* length of remote address */
  87. struct sockaddr_in xp_raddr; /* remote address */
  88. struct opaque_auth xp_verf; /* raw response verifier */
  89. caddr_t xp_p1; /* private */
  90. caddr_t xp_p2; /* private */
  91. char xp_pad [256]; /* padding, internal use */
  92. };
  93. /*
  94. * Approved way of getting address of caller
  95. */
  96. #define svc_getcaller(x) (&(x)->xp_raddr)
  97. /*
  98. * Operations defined on an SVCXPRT handle
  99. *
  100. * SVCXPRT *xprt;
  101. * struct rpc_msg *msg;
  102. * xdrproc_t xargs;
  103. * caddr_t argsp;
  104. */
  105. #define SVC_RECV(xprt, msg) \
  106. (*(xprt)->xp_ops->xp_recv)((xprt), (msg))
  107. #define svc_recv(xprt, msg) \
  108. (*(xprt)->xp_ops->xp_recv)((xprt), (msg))
  109. #define SVC_STAT(xprt) \
  110. (*(xprt)->xp_ops->xp_stat)(xprt)
  111. #define svc_stat(xprt) \
  112. (*(xprt)->xp_ops->xp_stat)(xprt)
  113. #define SVC_GETARGS(xprt, xargs, argsp) \
  114. (*(xprt)->xp_ops->xp_getargs)((xprt), (xargs), (argsp))
  115. #define svc_getargs(xprt, xargs, argsp) \
  116. (*(xprt)->xp_ops->xp_getargs)((xprt), (xargs), (argsp))
  117. #define SVC_REPLY(xprt, msg) \
  118. (*(xprt)->xp_ops->xp_reply) ((xprt), (msg))
  119. #define svc_reply(xprt, msg) \
  120. (*(xprt)->xp_ops->xp_reply) ((xprt), (msg))
  121. #define SVC_FREEARGS(xprt, xargs, argsp) \
  122. (*(xprt)->xp_ops->xp_freeargs)((xprt), (xargs), (argsp))
  123. #define svc_freeargs(xprt, xargs, argsp) \
  124. (*(xprt)->xp_ops->xp_freeargs)((xprt), (xargs), (argsp))
  125. #define SVC_DESTROY(xprt) \
  126. (*(xprt)->xp_ops->xp_destroy)(xprt)
  127. #define svc_destroy(xprt) \
  128. (*(xprt)->xp_ops->xp_destroy)(xprt)
  129. /*
  130. * Service request
  131. */
  132. struct svc_req {
  133. rpcprog_t rq_prog; /* service program number */
  134. rpcvers_t rq_vers; /* service protocol version */
  135. rpcproc_t rq_proc; /* the desired procedure */
  136. struct opaque_auth rq_cred; /* raw creds from the wire */
  137. caddr_t rq_clntcred; /* read only cooked cred */
  138. SVCXPRT *rq_xprt; /* associated transport */
  139. };
  140. #ifndef __DISPATCH_FN_T
  141. #define __DISPATCH_FN_T
  142. typedef void (*__dispatch_fn_t) (struct svc_req*, SVCXPRT*);
  143. #endif
  144. /*
  145. * Service registration
  146. *
  147. * svc_register(xprt, prog, vers, dispatch, protocol)
  148. * SVCXPRT *xprt;
  149. * rpcprog_t prog;
  150. * rpcvers_t vers;
  151. * void (*dispatch)(struct svc_req*, SVCXPRT*);
  152. * rpcprot_t protocol; like TCP or UDP, zero means do not register
  153. */
  154. extern bool_t svc_register (SVCXPRT *__xprt, rpcprog_t __prog,
  155. rpcvers_t __vers, __dispatch_fn_t __dispatch,
  156. rpcprot_t __protocol) __THROW;
  157. /*
  158. * Service un-registration
  159. *
  160. * svc_unregister(prog, vers)
  161. * rpcprog_t prog;
  162. * rpcvers_t vers;
  163. */
  164. extern void svc_unregister (rpcprog_t __prog, rpcvers_t __vers) __THROW;
  165. /*
  166. * Transport registration.
  167. *
  168. * xprt_register(xprt)
  169. * SVCXPRT *xprt;
  170. */
  171. extern void xprt_register (SVCXPRT *__xprt) __THROW;
  172. /*
  173. * Transport un-register
  174. *
  175. * xprt_unregister(xprt)
  176. * SVCXPRT *xprt;
  177. */
  178. extern void xprt_unregister (SVCXPRT *__xprt) __THROW;
  179. /*
  180. * When the service routine is called, it must first check to see if it
  181. * knows about the procedure; if not, it should call svcerr_noproc
  182. * and return. If so, it should deserialize its arguments via
  183. * SVC_GETARGS (defined above). If the deserialization does not work,
  184. * svcerr_decode should be called followed by a return. Successful
  185. * decoding of the arguments should be followed the execution of the
  186. * procedure's code and a call to svc_sendreply.
  187. *
  188. * Also, if the service refuses to execute the procedure due to too-
  189. * weak authentication parameters, svcerr_weakauth should be called.
  190. * Note: do not confuse access-control failure with weak authentication!
  191. *
  192. * NB: In pure implementations of rpc, the caller always waits for a reply
  193. * msg. This message is sent when svc_sendreply is called.
  194. * Therefore pure service implementations should always call
  195. * svc_sendreply even if the function logically returns void; use
  196. * xdr.h - xdr_void for the xdr routine. HOWEVER, tcp based rpc allows
  197. * for the abuse of pure rpc via batched calling or pipelining. In the
  198. * case of a batched call, svc_sendreply should NOT be called since
  199. * this would send a return message, which is what batching tries to avoid.
  200. * It is the service/protocol writer's responsibility to know which calls are
  201. * batched and which are not. Warning: responding to batch calls may
  202. * deadlock the caller and server processes!
  203. */
  204. extern bool_t svc_sendreply (SVCXPRT *__xprt, xdrproc_t __xdr_results,
  205. caddr_t __xdr_location) __THROW;
  206. extern void svcerr_decode (SVCXPRT *__xprt) __THROW;
  207. extern void svcerr_weakauth (SVCXPRT *__xprt) __THROW;
  208. extern void svcerr_noproc (SVCXPRT *__xprt) __THROW;
  209. extern void svcerr_progvers (SVCXPRT *__xprt, rpcvers_t __low_vers,
  210. rpcvers_t __high_vers) __THROW;
  211. extern void svcerr_auth (SVCXPRT *__xprt, enum auth_stat __why) __THROW;
  212. extern void svcerr_noprog (SVCXPRT *__xprt) __THROW;
  213. extern void svcerr_systemerr (SVCXPRT *__xprt) __THROW;
  214. /*
  215. * Lowest level dispatching -OR- who owns this process anyway.
  216. * Somebody has to wait for incoming requests and then call the correct
  217. * service routine. The routine svc_run does infinite waiting; i.e.,
  218. * svc_run never returns.
  219. * Since another (coexistent) package may wish to selectively wait for
  220. * incoming calls or other events outside of the rpc architecture, the
  221. * routine svc_getreq is provided. It must be passed readfds, the
  222. * "in-place" results of a select system call (see select, section 2).
  223. */
  224. /*
  225. * Global keeper of rpc service descriptors in use
  226. * dynamic; must be inspected before each call to select
  227. */
  228. extern struct pollfd *svc_pollfd;
  229. extern int svc_max_pollfd;
  230. extern fd_set svc_fdset;
  231. #define svc_fds svc_fdset.fds_bits[0] /* compatibility */
  232. /*
  233. * a small program implemented by the svc_rpc implementation itself;
  234. * also see clnt.h for protocol numbers.
  235. */
  236. extern void svc_getreq (int __rdfds) __THROW;
  237. extern void svc_getreq_common (const int __fd) __THROW;
  238. extern void svc_getreqset (fd_set *__readfds) __THROW;
  239. extern void svc_getreq_poll (struct pollfd *, const int) __THROW;
  240. extern void svc_exit (void) __THROW;
  241. extern void svc_run (void) __THROW;
  242. /*
  243. * Socket to use on svcxxx_create call to get default socket
  244. */
  245. #define RPC_ANYSOCK -1
  246. /*
  247. * These are the existing service side transport implementations
  248. */
  249. /*
  250. * Memory based rpc for testing and timing.
  251. */
  252. extern SVCXPRT *svcraw_create (void) __THROW;
  253. /*
  254. * Udp based rpc.
  255. */
  256. extern SVCXPRT *svcudp_create (int __sock) __THROW;
  257. extern SVCXPRT *svcudp_bufcreate (int __sock, u_int __sendsz, u_int __recvsz)
  258. __THROW;
  259. /*
  260. * Tcp based rpc.
  261. */
  262. extern SVCXPRT *svctcp_create (int __sock, u_int __sendsize, u_int __recvsize)
  263. __THROW;
  264. /*
  265. * FD based rpc.
  266. */
  267. extern SVCXPRT *svcfd_create (int __sock, u_int __sendsize, u_int __recvsize)
  268. __THROW;
  269. /*
  270. * Unix based rpc.
  271. */
  272. extern SVCXPRT *svcunix_create (int __sock, u_int __sendsize, u_int __recvsize,
  273. char *__path) __THROW;
  274. __END_DECLS
  275. #endif /* rpc/svc.h */