source: rtems/cpukit/librpc/src/rpc/svc.c @ 624a038

4.104.114.95
Last change on this file since 624a038 was 624a038, checked in by Ralf Corsepius <ralf.corsepius@…>, on 08/01/08 at 07:32:47

Add missing prototypes.

  • Property mode set to 100644
File size: 11.5 KB
Line 
1/*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part.  Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
8 *
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12 *
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
16 *
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
20 *
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
24 *
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California  94043
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31/*static char *sccsid = "from: @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";*/
32/*static char *sccsid = "from: @(#)svc.c        2.4 88/08/11 4.0 RPCSRC";*/
33static char *rcsid = "$FreeBSD: src/lib/libc/rpc/svc.c,v 1.14 1999/08/28 00:00:48 peter Exp $";
34#endif
35
36/*
37 * svc.c, Server-side remote procedure call interface.
38 *
39 * There are two sets of procedures here.  The xprt routines are
40 * for handling transport handles.  The svc routines handle the
41 * list of service routines.
42 *
43 * Copyright (C) 1984, Sun Microsystems, Inc.
44 */
45
46#include <string.h>
47#include <stdlib.h>
48#include <sys/errno.h>
49#include <rpc/rpc.h>
50#include <rpc/pmap_clnt.h>
51
52#define xports (rtems_rpc_task_variables->svc_xports)
53#define xportssize (rtems_rpc_task_variables->svc_xportssize)
54
55#define NULL_SVC ((struct svc_callout *)0)
56#define RQCRED_SIZE     400             /* this size is excessive */
57
58#define max(a, b) (a > b ? a : b)
59
60/*
61 * The services list
62 * Each entry represents a set of procedures (an rpc program).
63 * The dispatch routine takes request structs and runs the
64 * apropriate procedure.
65 */
66struct svc_callout {
67        struct svc_callout *sc_next;
68        u_long              sc_prog;
69        u_long              sc_vers;
70        void                (*sc_dispatch)(struct svc_req *r, SVCXPRT *xprt);
71};
72#define svc_head (rtems_rpc_task_variables->svc_svc_head)
73
74static struct svc_callout *svc_find(u_long prog, u_long vers,
75  struct svc_callout **prev);
76
77/* ***************  SVCXPRT related stuff **************** */
78
79/*
80 * Activate a transport handle.
81 */
82void
83xprt_register(
84        SVCXPRT *xprt )
85{
86        register int sock = xprt->xp_sock;
87
88        if (sock + 1 > __svc_fdsetsize) {
89                int bytes = sizeof (fd_set);
90                fd_set *fds;
91
92                fds = (fd_set *)malloc(bytes);
93                memset(fds, 0, bytes);
94                if (__svc_fdset) {
95                        memcpy(fds, __svc_fdset, bytes);
96                        free(__svc_fdset);
97                }
98                __svc_fdset = fds;
99                __svc_fdsetsize = bytes * NBBY;
100        }
101
102        if (sock < FD_SETSIZE)
103                FD_SET(sock, &svc_fdset);
104        FD_SET(sock, __svc_fdset);
105
106        if (xports == NULL || sock + 1 > xportssize) {
107                SVCXPRT **xp;
108                int size = FD_SETSIZE;
109
110                if (sock + 1 > size)
111                        size = sock + 1;
112                xp = (SVCXPRT **)mem_alloc(size * sizeof(SVCXPRT *));
113                memset(xp, 0, size * sizeof(SVCXPRT *));
114                if (xports) {
115                        memcpy(xp, xports, xportssize * sizeof(SVCXPRT *));
116                        free(xports);
117                }
118                xportssize = size;
119                xports = xp;
120        }
121        xports[sock] = xprt;
122        svc_maxfd = max(svc_maxfd, sock);
123}
124
125/*
126 * De-activate a transport handle.
127 */
128void
129xprt_unregister(
130        SVCXPRT *xprt )
131{
132        register int sock = xprt->xp_sock;
133
134        if (xports[sock] == xprt) {
135                xports[sock] = (SVCXPRT *)0;
136                if (sock < FD_SETSIZE)
137                        FD_CLR(sock, &svc_fdset);
138                FD_CLR(sock, __svc_fdset);
139                if (sock == svc_maxfd) {
140                        for (svc_maxfd--; svc_maxfd >= 0; svc_maxfd--)
141                                if (xports[svc_maxfd])
142                                        break;
143                }
144                /*
145                 * XXX could use svc_maxfd as a hint to
146                 * decrease the size of __svc_fdset
147                 */
148        }
149}
150
151
152/* ********************** CALLOUT list related stuff ************* */
153
154/*
155 * Add a service program to the callout list.
156 * The dispatch routine will be called when a rpc request for this
157 * program number comes in.
158 */
159bool_t
160svc_register(
161        SVCXPRT *xprt,
162        u_long prog,
163        u_long vers,
164        void (*dispatch)(struct svc_req *r, SVCXPRT *xprt),
165        int protocol )
166{
167        struct svc_callout *prev;
168        register struct svc_callout *s;
169
170        if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
171                if (s->sc_dispatch == dispatch)
172                        goto pmap_it;  /* he is registering another xptr */
173                return (FALSE);
174        }
175        s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
176        if (s == (struct svc_callout *)0) {
177                return (FALSE);
178        }
179        s->sc_prog = prog;
180        s->sc_vers = vers;
181        s->sc_dispatch = dispatch;
182        s->sc_next = svc_head;
183        svc_head = s;
184pmap_it:
185        /* now register the information with the local binder service */
186        if (protocol) {
187                return (pmap_set(prog, vers, protocol, xprt->xp_port));
188        }
189        return (TRUE);
190}
191
192/*
193 * Remove a service program from the callout list.
194 */
195void
196svc_unregister(
197        u_long prog,
198        u_long vers )
199{
200        struct svc_callout *prev;
201        register struct svc_callout *s;
202
203        if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
204                return;
205        if (prev == NULL_SVC) {
206                svc_head = s->sc_next;
207        } else {
208                prev->sc_next = s->sc_next;
209        }
210        s->sc_next = NULL_SVC;
211        mem_free((char *) s, (u_int) sizeof(struct svc_callout));
212        /* now unregister the information with the local binder service */
213        (void)pmap_unset(prog, vers);
214}
215
216/*
217 * Search the callout list for a program number, return the callout
218 * struct.
219 */
220static struct svc_callout *
221svc_find(
222        u_long prog,
223        u_long vers,
224        struct svc_callout **prev )
225{
226        register struct svc_callout *s, *p;
227
228        p = NULL_SVC;
229        for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
230                if ((s->sc_prog == prog) && (s->sc_vers == vers))
231                        goto done;
232                p = s;
233        }
234done:
235        *prev = p;
236        return (s);
237}
238
239/* ******************* REPLY GENERATION ROUTINES  ************ */
240
241/*
242 * Send a reply to an rpc request
243 */
244bool_t
245svc_sendreply(
246        register SVCXPRT *xprt,
247        xdrproc_t xdr_results,
248        void *xdr_location )
249{
250        struct rpc_msg rply;
251
252        rply.rm_direction = REPLY;
253        rply.rm_reply.rp_stat = MSG_ACCEPTED;
254        rply.acpted_rply.ar_verf = xprt->xp_verf;
255        rply.acpted_rply.ar_stat = SUCCESS;
256        rply.acpted_rply.ar_results.where = xdr_location;
257        rply.acpted_rply.ar_results.proc = xdr_results;
258        return (SVC_REPLY(xprt, &rply));
259}
260
261/*
262 * No procedure error reply
263 */
264void
265svcerr_noproc(
266        register SVCXPRT *xprt )
267{
268        struct rpc_msg rply;
269
270        rply.rm_direction = REPLY;
271        rply.rm_reply.rp_stat = MSG_ACCEPTED;
272        rply.acpted_rply.ar_verf = xprt->xp_verf;
273        rply.acpted_rply.ar_stat = PROC_UNAVAIL;
274        SVC_REPLY(xprt, &rply);
275}
276
277/*
278 * Can't decode args error reply
279 */
280void
281svcerr_decode(
282        register SVCXPRT *xprt )
283{
284        struct rpc_msg rply;
285
286        rply.rm_direction = REPLY;
287        rply.rm_reply.rp_stat = MSG_ACCEPTED;
288        rply.acpted_rply.ar_verf = xprt->xp_verf;
289        rply.acpted_rply.ar_stat = GARBAGE_ARGS;
290        SVC_REPLY(xprt, &rply);
291}
292
293/*
294 * Some system error
295 */
296void
297svcerr_systemerr(
298        register SVCXPRT *xprt )
299{
300        struct rpc_msg rply;
301
302        rply.rm_direction = REPLY;
303        rply.rm_reply.rp_stat = MSG_ACCEPTED;
304        rply.acpted_rply.ar_verf = xprt->xp_verf;
305        rply.acpted_rply.ar_stat = SYSTEM_ERR;
306        SVC_REPLY(xprt, &rply);
307}
308
309/*
310 * Authentication error reply
311 */
312void
313svcerr_auth(
314        SVCXPRT *xprt,
315        enum auth_stat why )
316{
317        struct rpc_msg rply;
318
319        rply.rm_direction = REPLY;
320        rply.rm_reply.rp_stat = MSG_DENIED;
321        rply.rjcted_rply.rj_stat = AUTH_ERROR;
322        rply.rjcted_rply.rj_why = why;
323        SVC_REPLY(xprt, &rply);
324}
325
326/*
327 * Auth too weak error reply
328 */
329void
330svcerr_weakauth(
331        SVCXPRT *xprt )
332{
333
334        svcerr_auth(xprt, AUTH_TOOWEAK);
335}
336
337/*
338 * Program unavailable error reply
339 */
340void
341svcerr_noprog(
342        register SVCXPRT *xprt )
343{
344        struct rpc_msg rply;
345
346        rply.rm_direction = REPLY;
347        rply.rm_reply.rp_stat = MSG_ACCEPTED;
348        rply.acpted_rply.ar_verf = xprt->xp_verf;
349        rply.acpted_rply.ar_stat = PROG_UNAVAIL;
350        SVC_REPLY(xprt, &rply);
351}
352
353/*
354 * Program version mismatch error reply
355 */
356void
357svcerr_progvers(
358        register SVCXPRT *xprt,
359        rpcvers_t low_vers,
360        rpcvers_t high_vers )
361{
362        struct rpc_msg rply;
363
364        rply.rm_direction = REPLY;
365        rply.rm_reply.rp_stat = MSG_ACCEPTED;
366        rply.acpted_rply.ar_verf = xprt->xp_verf;
367        rply.acpted_rply.ar_stat = PROG_MISMATCH;
368        rply.acpted_rply.ar_vers.low = low_vers;
369        rply.acpted_rply.ar_vers.high = high_vers;
370        SVC_REPLY(xprt, &rply);
371}
372
373/* ******************* SERVER INPUT STUFF ******************* */
374
375/*
376 * Get server side input from some transport.
377 *
378 * Statement of authentication parameters management:
379 * This function owns and manages all authentication parameters, specifically
380 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
381 * the "cooked" credentials (rqst->rq_clntcred).
382 * However, this function does not know the structure of the cooked
383 * credentials, so it make the following assumptions:
384 *   a) the structure is contiguous (no pointers), and
385 *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
386 * In all events, all three parameters are freed upon exit from this routine.
387 * The storage is trivially management on the call stack in user land, but
388 * is mallocated in kernel land.
389 */
390
391void
392svc_getreq(
393        int rdfds )
394{
395        fd_set readfds;
396
397        FD_ZERO(&readfds);
398        readfds.fds_bits[0] = rdfds;
399        svc_getreqset(&readfds);
400}
401
402void
403svc_getreqset(
404        fd_set *readfds )
405{
406        svc_getreqset2(readfds, FD_SETSIZE);
407}
408
409void
410svc_getreqset2(
411        fd_set *readfds,
412        int width )
413{
414        enum xprt_stat stat;
415        struct rpc_msg msg;
416        int prog_found;
417        u_long low_vers;
418        u_long high_vers;
419        struct svc_req r;
420        register SVCXPRT *xprt;
421        register int bit;
422        register int sock;
423        register fd_mask mask, *maskp;
424        char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
425        msg.rm_call.cb_cred.oa_base = cred_area;
426        msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
427        r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
428
429
430        maskp = readfds->fds_bits;
431        for (sock = 0; sock < width; sock += NFDBITS) {
432            for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
433                /* sock has input waiting */
434                xprt = xports[sock + bit - 1];
435                if (xprt == NULL)
436                        /* But do we control sock? */
437                        continue;
438                /* now receive msgs from xprtprt (support batch calls) */
439                do {
440                        if (SVC_RECV(xprt, &msg)) {
441
442                                /* now find the exported program and call it */
443                                register struct svc_callout *s;
444                                enum auth_stat why;
445
446                                r.rq_xprt = xprt;
447                                r.rq_prog = msg.rm_call.cb_prog;
448                                r.rq_vers = msg.rm_call.cb_vers;
449                                r.rq_proc = msg.rm_call.cb_proc;
450                                r.rq_cred = msg.rm_call.cb_cred;
451                                /* first authenticate the message */
452                                if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
453                                        svcerr_auth(xprt, why);
454                                        goto call_done;
455                                }
456                                /* now match message with a registered service*/
457                                prog_found = FALSE;
458                                low_vers = (u_long) - 1;
459                                high_vers = 0;
460                                for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
461                                        if (s->sc_prog == r.rq_prog) {
462                                                if (s->sc_vers == r.rq_vers) {
463                                                        (*s->sc_dispatch)(&r, xprt);
464                                                        goto call_done;
465                                                }  /* found correct version */
466                                                prog_found = TRUE;
467                                                if (s->sc_vers < low_vers)
468                                                        low_vers = s->sc_vers;
469                                                if (s->sc_vers > high_vers)
470                                                        high_vers = s->sc_vers;
471                                        }   /* found correct program */
472                                }
473                                /*
474                                 * if we got here, the program or version
475                                 * is not served ...
476                                 */
477                                if (prog_found)
478                                        svcerr_progvers(xprt,
479                                        low_vers, high_vers);
480                                else
481                                         svcerr_noprog(xprt);
482                                /* Fall through to ... */
483                        }
484                call_done:
485                        if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
486                                SVC_DESTROY(xprt);
487                                break;
488                        }
489                } while (stat == XPRT_MOREREQS);
490            }
491        }
492}
Note: See TracBrowser for help on using the repository browser.