home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / svc_simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-01  |  5.2 KB  |  202 lines

  1. /* @(#)svc_simple.c    2.2 88/08/01 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /* 
  35.  * svc_simple.c
  36.  * Simplified front end to rpc.
  37.  *
  38.  * Copyright (C) 1984, Sun Microsystems, Inc.
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include <rpc/rpc.h>
  43. #include <rpc/pmap_clnt.h>
  44. #include <sys/socket.h>
  45. #include <netdb.h>
  46. #include <strings.h>
  47.  
  48. #ifdef __STDC__
  49. #include <stdlib.h>
  50. #endif
  51.  
  52. #if NLS
  53. #include "nl_types.h"
  54. #endif
  55.  
  56. static struct proglst {
  57.     char *(*p_progname)();
  58.     int  p_prognum;
  59.     int  p_procnum;
  60.     xdrproc_t p_inproc, p_outproc;
  61.     struct proglst *p_nxt;
  62. } *proglst;
  63. static void universal();
  64. static SVCXPRT *transp;
  65.  
  66. int registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
  67.     u_long prognum;
  68.     u_long versnum;
  69.     u_long procnum;
  70.     char *(*progname)();
  71.     xdrproc_t inproc, outproc;
  72. {
  73.     struct proglst *pl;
  74.  
  75. #if NLS
  76.     libc_nls_init();
  77. #endif
  78.     if (procnum == NULLPROC) {
  79.  
  80. #if NLS
  81.         (void) fprintf(stderr,
  82.                catgets(_libc_cat, RpcMiscSet, RpcMiscCantReassignProc,
  83.                 "can't reassign procedure number %d\n"), NULLPROC);
  84. #else
  85.         (void) fprintf(stderr,
  86.             "can't reassign procedure number %d\n", NULLPROC);
  87. #endif
  88.         return (-1);
  89.     }
  90.     if (transp == 0) {
  91.         transp = svcudp_create(RPC_ANYSOCK);
  92.         if (transp == NULL) {
  93. #if NLS
  94.             (void) fprintf(stderr, "%s\n",
  95.                    catgets(_libc_cat, RpcMiscSet,
  96.                        RpcMiscCantCreateServer,
  97.                        "couldn't create an rpc server"));
  98. #else
  99.             (void) fprintf(stderr, "couldn't create an rpc server\n");
  100. #endif
  101.             return (-1);
  102.         }
  103.     }
  104.     (void) pmap_unset((u_long)prognum, (u_long)versnum);
  105.     if (!svc_register(transp, (u_long)prognum, (u_long)versnum, 
  106.         universal, IPPROTO_UDP)) {
  107. #if NLS
  108.             (void) fprintf(stderr, catgets(_libc_cat, RpcMiscSet,
  109.                        RpcMiscCantRegisterProg,
  110.                        "couldn't register prog %d vers %d\n"),
  111.                 prognum, versnum);
  112. #else
  113.             (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
  114.             prognum, versnum);
  115. #endif
  116.         return (-1);
  117.     }
  118.     pl = (struct proglst *)malloc(sizeof(struct proglst));
  119.     if (pl == NULL) {
  120. #if NLS
  121.         (void) fprintf(stderr, "registerrpc: %s\n",
  122.                    catgets(_libc_cat, RpcMiscSet,
  123.                        RpcMiscOutOfMemory, "out of memory"));
  124. #else
  125.         (void) fprintf(stderr, "registerrpc: out of memory\n");
  126. #endif
  127.         return (-1);
  128.     }
  129.     pl->p_progname = progname;
  130.     pl->p_prognum = prognum;
  131.     pl->p_procnum = procnum;
  132.     pl->p_inproc = inproc;
  133.     pl->p_outproc = outproc;
  134.     pl->p_nxt = proglst;
  135.     proglst = pl;
  136.     return (0);
  137. }
  138.  
  139. static void
  140. universal(rqstp, transp)
  141.     struct svc_req *rqstp;
  142.     SVCXPRT *transp;
  143. {
  144.     int prog, proc;
  145.     char *outdata;
  146.     char xdrbuf[UDPMSGSIZE];
  147.     struct proglst *pl;
  148.  
  149. #if NLS
  150.     libc_nls_init();
  151. #endif
  152.     /* 
  153.      * enforce "procnum 0 is echo" convention
  154.      */
  155.     if (rqstp->rq_proc == NULLPROC) {
  156.         if (svc_sendreply(transp, xdr_void, (char *)NULL) == FALSE) {
  157.             (void) fprintf(stderr, "xxx\n");
  158.             exit(1);
  159.         }
  160.         return;
  161.     }
  162.     prog = rqstp->rq_prog;
  163.     proc = rqstp->rq_proc;
  164.     for (pl = proglst; pl != NULL; pl = pl->p_nxt)
  165.         if (pl->p_prognum == prog && pl->p_procnum == proc) {
  166.             /* decode arguments into a CLEAN buffer */
  167.             bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
  168.             if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
  169.                 svcerr_decode(transp);
  170.                 return;
  171.             }
  172.             outdata = (*(pl->p_progname))(xdrbuf);
  173.             if (outdata == NULL && pl->p_outproc != xdr_void)
  174.                 /* there was an error */
  175.                 return;
  176.             if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
  177.                 (void) fprintf(stderr,
  178. #if NLS
  179.                     catgets(_libc_cat, RpcMiscSet,
  180.                                       RpcMiscTroubleReplying,
  181.                       "trouble replying to prog %d\n"),
  182. #else
  183.                     "trouble replying to prog %d\n",
  184. #endif
  185.                     pl->p_prognum);
  186.                 exit(1);
  187.             }
  188.             /* free the decoded arguments */
  189.             (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
  190.             return;
  191.         }
  192. #if NLS
  193.     (void) fprintf(stderr, catgets(_libc_cat, RpcMiscSet,
  194.                                       RpcMiscNeverRegisteredProg,
  195.                       "never registered prog %d\n"), prog);
  196. #else
  197.     (void) fprintf(stderr, "never registered prog %d\n", prog);
  198. #endif
  199.     exit(1);
  200. }
  201.  
  202.