source: rtems-libbsd/services/librpc/src/rpc/svc.c @ 20ec9e6

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 20ec9e6 was 20ec9e6, checked in by Joel Sherrill <joel.sherrill@…>, on 08/03/12 at 19:19:49

librpc: Initial addition

This does not currently compile. There is an include file
issue and int32_t is not defined even though stdint.h is included
before its use.

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