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

4.104.114.84.95
Last change on this file since b2b143f4 was b2b143f4, checked in by Joel Sherrill <joel.sherrill@…>, on 03/05/04 at 17:58:51

2004-03-05 Joel Sherrill <joel@…>

  • libblock/src/bdbuf.c, libblock/src/ramdisk.c, libcsupport/src/newlibc.c, libcsupport/src/sync.c, libmisc/cpuuse/cpuuse.c, libmisc/monitor/mon-symbols.c, libmisc/shell/cmds.c, libmisc/shell/shell.c, libnetworking/kern/kern_sysctl.c, libnetworking/lib/ftpfs.c, libnetworking/lib/tftpDriver.c, libnetworking/libc/gethostbydns.c, libnetworking/libc/gethostbyht.c, libnetworking/libc/gethostnamadr.c, libnetworking/libc/getnetbyht.c, libnetworking/libc/getnetnamadr.c, libnetworking/libc/inet_addr.c, libnetworking/libc/linkaddr.c, libnetworking/libc/map_v4v6.c, libnetworking/libc/ns_print.c, libnetworking/libc/ns_ttl.c, libnetworking/libc/nsap_addr.c, libnetworking/libc/rcmd.c, libnetworking/libc/res_debug.c, libnetworking/libc/res_mkupdate.c, libnetworking/libc/res_query.c, libnetworking/libc/res_send.c, libnetworking/libc/res_update.c, libnetworking/net/radix.c, libnetworking/rtems/mkrootfs.c, librpc/src/rpc/clnt_perror.c, librpc/src/rpc/rtems_rpc.c, librpc/src/rpc/svc.c, sapi/include/confdefs.h, score/macros/rtems/score/chain.inl, score/src/objectidtoname.c:
  • Property mode set to 100644
File size: 11.7 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/* Since we compile with strict ANSI we need to undef it to get
47 * prototypes for extensions
48 */
49#undef __STRICT_ANSI__
50
51#include <string.h>
52#include <stdlib.h>
53#include <sys/errno.h>
54#include <rpc/rpc.h>
55#include <rpc/pmap_clnt.h>
56
57#define xports ((SVCXPRT **) rtems_rpc_task_variables->svc_xports)
58#define xportssize (rtems_rpc_task_variables->svc_xportssize)
59
60#define NULL_SVC ((struct svc_callout *)0)
61#define RQCRED_SIZE     400             /* this size is excessive */
62
63#define max(a, b) (a > b ? a : b)
64
65/*
66 * The services list
67 * Each entry represents a set of procedures (an rpc program).
68 * The dispatch routine takes request structs and runs the
69 * apropriate procedure.
70 */
71struct svc_callout {
72        struct svc_callout *sc_next;
73        u_long              sc_prog;
74        u_long              sc_vers;
75        void                (*sc_dispatch)();
76};
77#define svc_head (struct svc_callout *)((rtems_rpc_task_variables)->svc_svc_head)
78
79static struct svc_callout *svc_find();
80
81/* ***************  SVCXPRT related stuff **************** */
82
83/*
84 * Activate a transport handle.
85 */
86void
87xprt_register(xprt)
88        SVCXPRT *xprt;
89{
90        register int sock = xprt->xp_sock;
91
92        if (sock + 1 > __svc_fdsetsize) {
93                int bytes = sizeof (fd_set);
94                fd_set *fds;
95
96                fds = (fd_set *)malloc(bytes);
97                memset(fds, 0, bytes);
98                if (__svc_fdset) {
99                        memcpy(fds, __svc_fdset, bytes);
100                        free(__svc_fdset);
101                }
102                __svc_fdset = fds;
103                __svc_fdsetsize = bytes * NBBY;
104        }
105
106        if (sock < FD_SETSIZE)
107                FD_SET(sock, &svc_fdset);
108        FD_SET(sock, __svc_fdset);
109
110        if (xports == NULL || sock + 1 > xportssize) {
111                SVCXPRT **xp;
112                int size = FD_SETSIZE;
113
114                if (sock + 1 > size)
115                        size = sock + 1;
116                xp = (SVCXPRT **)mem_alloc(size * sizeof(SVCXPRT *));
117                memset(xp, 0, size * sizeof(SVCXPRT *));
118                if (xports) {
119                        memcpy(xp, xports, xportssize * sizeof(SVCXPRT *));
120                        free(xports);
121                }
122                xportssize = size;
123                xports = xp;
124        }
125        xports[sock] = xprt;
126        svc_maxfd = max(svc_maxfd, sock);
127}
128
129/*
130 * De-activate a transport handle.
131 */
132void
133xprt_unregister(xprt)
134        SVCXPRT *xprt;
135{
136        register int sock = xprt->xp_sock;
137
138        if (xports[sock] == xprt) {
139                xports[sock] = (SVCXPRT *)0;
140                if (sock < FD_SETSIZE)
141                        FD_CLR(sock, &svc_fdset);
142                FD_CLR(sock, __svc_fdset);
143                if (sock == svc_maxfd) {
144                        for (svc_maxfd--; svc_maxfd >= 0; svc_maxfd--)
145                                if (xports[svc_maxfd])
146                                        break;
147                }
148                /*
149                 * XXX could use svc_maxfd as a hint to
150                 * decrease the size of __svc_fdset
151                 */
152        }
153}
154
155
156/* ********************** CALLOUT list related stuff ************* */
157
158/*
159 * Add a service program to the callout list.
160 * The dispatch routine will be called when a rpc request for this
161 * program number comes in.
162 */
163bool_t
164svc_register(xprt, prog, vers, dispatch, protocol)
165        SVCXPRT *xprt;
166        u_long prog;
167        u_long vers;
168        void (*dispatch)();
169        int protocol;
170{
171        struct svc_callout *prev;
172        register struct svc_callout *s;
173
174        if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
175                if (s->sc_dispatch == dispatch)
176                        goto pmap_it;  /* he is registering another xptr */
177                return (FALSE);
178        }
179        s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
180        if (s == (struct svc_callout *)0) {
181                return (FALSE);
182        }
183        s->sc_prog = prog;
184        s->sc_vers = vers;
185        s->sc_dispatch = dispatch;
186        s->sc_next = svc_head;
187        svc_head = s;
188pmap_it:
189        /* now register the information with the local binder service */
190        if (protocol) {
191                return (pmap_set(prog, vers, protocol, xprt->xp_port));
192        }
193        return (TRUE);
194}
195
196/*
197 * Remove a service program from the callout list.
198 */
199void
200svc_unregister(prog, vers)
201        u_long prog;
202        u_long vers;
203{
204        struct svc_callout *prev;
205        register struct svc_callout *s;
206
207        if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
208                return;
209        if (prev == NULL_SVC) {
210                svc_head = s->sc_next;
211        } else {
212                prev->sc_next = s->sc_next;
213        }
214        s->sc_next = NULL_SVC;
215        mem_free((char *) s, (u_int) sizeof(struct svc_callout));
216        /* now unregister the information with the local binder service */
217        (void)pmap_unset(prog, vers);
218}
219
220/*
221 * Search the callout list for a program number, return the callout
222 * struct.
223 */
224static struct svc_callout *
225svc_find(prog, vers, prev)
226        u_long prog;
227        u_long vers;
228        struct svc_callout **prev;
229{
230        register struct svc_callout *s, *p;
231
232        p = NULL_SVC;
233        for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
234                if ((s->sc_prog == prog) && (s->sc_vers == vers))
235                        goto done;
236                p = s;
237        }
238done:
239        *prev = p;
240        return (s);
241}
242
243/* ******************* REPLY GENERATION ROUTINES  ************ */
244
245/*
246 * Send a reply to an rpc request
247 */
248bool_t
249svc_sendreply(xprt, xdr_results, xdr_location)
250        register SVCXPRT *xprt;
251        xdrproc_t xdr_results;
252        caddr_t xdr_location;
253{
254        struct rpc_msg rply;
255
256        rply.rm_direction = REPLY;
257        rply.rm_reply.rp_stat = MSG_ACCEPTED;
258        rply.acpted_rply.ar_verf = xprt->xp_verf;
259        rply.acpted_rply.ar_stat = SUCCESS;
260        rply.acpted_rply.ar_results.where = xdr_location;
261        rply.acpted_rply.ar_results.proc = xdr_results;
262        return (SVC_REPLY(xprt, &rply));
263}
264
265/*
266 * No procedure error reply
267 */
268void
269svcerr_noproc(xprt)
270        register SVCXPRT *xprt;
271{
272        struct rpc_msg rply;
273
274        rply.rm_direction = REPLY;
275        rply.rm_reply.rp_stat = MSG_ACCEPTED;
276        rply.acpted_rply.ar_verf = xprt->xp_verf;
277        rply.acpted_rply.ar_stat = PROC_UNAVAIL;
278        SVC_REPLY(xprt, &rply);
279}
280
281/*
282 * Can't decode args error reply
283 */
284void
285svcerr_decode(xprt)
286        register SVCXPRT *xprt;
287{
288        struct rpc_msg rply;
289
290        rply.rm_direction = REPLY;
291        rply.rm_reply.rp_stat = MSG_ACCEPTED;
292        rply.acpted_rply.ar_verf = xprt->xp_verf;
293        rply.acpted_rply.ar_stat = GARBAGE_ARGS;
294        SVC_REPLY(xprt, &rply);
295}
296
297/*
298 * Some system error
299 */
300void
301svcerr_systemerr(xprt)
302        register SVCXPRT *xprt;
303{
304        struct rpc_msg rply;
305
306        rply.rm_direction = REPLY;
307        rply.rm_reply.rp_stat = MSG_ACCEPTED;
308        rply.acpted_rply.ar_verf = xprt->xp_verf;
309        rply.acpted_rply.ar_stat = SYSTEM_ERR;
310        SVC_REPLY(xprt, &rply);
311}
312
313/*
314 * Authentication error reply
315 */
316void
317svcerr_auth(xprt, why)
318        SVCXPRT *xprt;
319        enum auth_stat why;
320{
321        struct rpc_msg rply;
322
323        rply.rm_direction = REPLY;
324        rply.rm_reply.rp_stat = MSG_DENIED;
325        rply.rjcted_rply.rj_stat = AUTH_ERROR;
326        rply.rjcted_rply.rj_why = why;
327        SVC_REPLY(xprt, &rply);
328}
329
330/*
331 * Auth too weak error reply
332 */
333void
334svcerr_weakauth(xprt)
335        SVCXPRT *xprt;
336{
337
338        svcerr_auth(xprt, AUTH_TOOWEAK);
339}
340
341/*
342 * Program unavailable error reply
343 */
344void
345svcerr_noprog(xprt)
346        register SVCXPRT *xprt;
347{
348        struct rpc_msg rply;
349
350        rply.rm_direction = REPLY;
351        rply.rm_reply.rp_stat = MSG_ACCEPTED;
352        rply.acpted_rply.ar_verf = xprt->xp_verf;
353        rply.acpted_rply.ar_stat = PROG_UNAVAIL;
354        SVC_REPLY(xprt, &rply);
355}
356
357/*
358 * Program version mismatch error reply
359 */
360void
361svcerr_progvers(xprt, low_vers, high_vers)
362        register SVCXPRT *xprt;
363        u_long low_vers;
364        u_long high_vers;
365{
366        struct rpc_msg rply;
367
368        rply.rm_direction = REPLY;
369        rply.rm_reply.rp_stat = MSG_ACCEPTED;
370        rply.acpted_rply.ar_verf = xprt->xp_verf;
371        rply.acpted_rply.ar_stat = PROG_MISMATCH;
372        rply.acpted_rply.ar_vers.low = low_vers;
373        rply.acpted_rply.ar_vers.high = high_vers;
374        SVC_REPLY(xprt, &rply);
375}
376
377/* ******************* SERVER INPUT STUFF ******************* */
378
379/*
380 * Get server side input from some transport.
381 *
382 * Statement of authentication parameters management:
383 * This function owns and manages all authentication parameters, specifically
384 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
385 * the "cooked" credentials (rqst->rq_clntcred).
386 * However, this function does not know the structure of the cooked
387 * credentials, so it make the following assumptions:
388 *   a) the structure is contiguous (no pointers), and
389 *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
390 * In all events, all three parameters are freed upon exit from this routine.
391 * The storage is trivially management on the call stack in user land, but
392 * is mallocated in kernel land.
393 */
394
395void
396svc_getreq(rdfds)
397        int rdfds;
398{
399        fd_set readfds;
400
401        FD_ZERO(&readfds);
402        readfds.fds_bits[0] = rdfds;
403        svc_getreqset(&readfds);
404}
405
406void
407svc_getreqset(readfds)
408        fd_set *readfds;
409{
410        svc_getreqset2(readfds, FD_SETSIZE);
411}
412
413void
414svc_getreqset2(readfds, width)
415        fd_set *readfds;
416        int width;
417{
418        enum xprt_stat stat;
419        struct rpc_msg msg;
420        int prog_found;
421        u_long low_vers;
422        u_long high_vers;
423        struct svc_req r;
424        register SVCXPRT *xprt;
425        register int bit;
426        register int sock;
427        register fd_mask mask, *maskp;
428        char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
429        msg.rm_call.cb_cred.oa_base = cred_area;
430        msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
431        r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
432
433
434        maskp = readfds->fds_bits;
435        for (sock = 0; sock < width; sock += NFDBITS) {
436            for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
437                /* sock has input waiting */
438                xprt = xports[sock + bit - 1];
439                if (xprt == NULL)
440                        /* But do we control sock? */
441                        continue;
442                /* now receive msgs from xprtprt (support batch calls) */
443                do {
444                        if (SVC_RECV(xprt, &msg)) {
445
446                                /* now find the exported program and call it */
447                                register struct svc_callout *s;
448                                enum auth_stat why;
449
450                                r.rq_xprt = xprt;
451                                r.rq_prog = msg.rm_call.cb_prog;
452                                r.rq_vers = msg.rm_call.cb_vers;
453                                r.rq_proc = msg.rm_call.cb_proc;
454                                r.rq_cred = msg.rm_call.cb_cred;
455                                /* first authenticate the message */
456                                if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
457                                        svcerr_auth(xprt, why);
458                                        goto call_done;
459                                }
460                                /* now match message with a registered service*/
461                                prog_found = FALSE;
462                                low_vers = (u_long) - 1;
463                                high_vers = 0;
464                                for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
465                                        if (s->sc_prog == r.rq_prog) {
466                                                if (s->sc_vers == r.rq_vers) {
467                                                        (*s->sc_dispatch)(&r, xprt);
468                                                        goto call_done;
469                                                }  /* found correct version */
470                                                prog_found = TRUE;
471                                                if (s->sc_vers < low_vers)
472                                                        low_vers = s->sc_vers;
473                                                if (s->sc_vers > high_vers)
474                                                        high_vers = s->sc_vers;
475                                        }   /* found correct program */
476                                }
477                                /*
478                                 * if we got here, the program or version
479                                 * is not served ...
480                                 */
481                                if (prog_found)
482                                        svcerr_progvers(xprt,
483                                        low_vers, high_vers);
484                                else
485                                         svcerr_noprog(xprt);
486                                /* Fall through to ... */
487                        }
488                call_done:
489                        if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
490                                SVC_DESTROY(xprt);
491                                break;
492                        }
493                } while (stat == XPRT_MOREREQS);
494            }
495        }
496}
Note: See TracBrowser for help on using the repository browser.