home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp-sdk / src / rpclib / svc_simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  3.0 KB  |  122 lines

  1. /*
  2.  *      $Id: svc_simple.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *      Simplified front end to rpc.
  5.  *
  6.  *      Copyright © 1994 AmiTCP/IP Group,
  7.  *                       Network Solutions Development Inc.
  8.  *                       All rights reserved. 
  9.  */
  10.  
  11. /* @(#)svc_simple.c    2.2 88/08/01 4.0 RPCSRC */
  12. #if !defined(lint) && defined(SCCSIDS)
  13. static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
  14. #endif
  15.  
  16. /* 
  17.  * Copyright (C) 1984, Sun Microsystems, Inc.
  18.  */
  19.  
  20. #include <sys/param.h>
  21. #include <stdio.h>
  22. #include <rpc/rpc.h>
  23. #include <rpc/pmap_clnt.h>
  24. #include <sys/socket.h>
  25. #include <netdb.h>
  26.  
  27. static struct proglst {
  28.     char *(*p_progname)(char *);
  29.     int  p_prognum;
  30.     int  p_procnum;
  31.     xdrproc_t p_inproc, p_outproc;
  32.     struct proglst *p_nxt;
  33. } *proglst;
  34. static void universal(struct svc_req *rqstp, SVCXPRT *transp);
  35. static SVCXPRT *transp;
  36. struct proglst *pl;
  37.  
  38. registerrpc(u_long prognum, u_long versnum, u_long procnum, 
  39.         char *(*progname)(char *), xdrproc_t inproc, xdrproc_t outproc)
  40. {
  41.     
  42.     if (procnum == NULLPROC) {
  43.         (void) fprintf(stderr,
  44.             "can't reassign procedure number %lu\n", NULLPROC);
  45.         return (-1);
  46.     }
  47.     if (transp == 0) {
  48.         transp = svcudp_create(RPC_ANYSOCK);
  49.         if (transp == NULL) {
  50.             (void) fprintf(stderr, "couldn't create an rpc server\n");
  51.             return (-1);
  52.         }
  53.     }
  54.     (void) pmap_unset((u_long)prognum, (u_long)versnum);
  55.     if (!svc_register(transp, (u_long)prognum, (u_long)versnum, 
  56.         universal, IPPROTO_UDP)) {
  57.             (void) fprintf(stderr, "couldn't register prog %lu vers %lu\n",
  58.             prognum, versnum);
  59.         return (-1);
  60.     }
  61.     pl = (struct proglst *)mem_alloc(sizeof(struct proglst));
  62.     if (pl == NULL) {
  63.         (void) fprintf(stderr, "registerrpc: out of memory\n");
  64.         return (-1);
  65.     }
  66.     pl->p_progname = progname;
  67.     pl->p_prognum = prognum;
  68.     pl->p_procnum = procnum;
  69.     pl->p_inproc = inproc;
  70.     pl->p_outproc = outproc;
  71.     pl->p_nxt = proglst;
  72.     proglst = pl;
  73.     return (0);
  74. }
  75.  
  76. static void
  77. universal(struct svc_req *rqstp, SVCXPRT *transp)
  78. {
  79.     int prog, proc;
  80.     char *outdata;
  81.     char xdrbuf[UDPMSGSIZE];
  82.     struct proglst *pl;
  83.  
  84.     /* 
  85.      * enforce "procnum 0 is echo" convention
  86.      */
  87.     if (rqstp->rq_proc == NULLPROC) {
  88.         if (svc_sendreply(transp, xdr_void, (char *)NULL) == FALSE) {
  89.             (void) fprintf(stderr, "xxx\n");
  90.             exit(1);
  91.         }
  92.         return;
  93.     }
  94.     prog = rqstp->rq_prog;
  95.     proc = rqstp->rq_proc;
  96.     for (pl = proglst; pl != NULL; pl = pl->p_nxt)
  97.         if (pl->p_prognum == prog && pl->p_procnum == proc) {
  98.             /* decode arguments into a CLEAN buffer */
  99.             bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
  100.             if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
  101.                 svcerr_decode(transp);
  102.                 return;
  103.             }
  104.             outdata = (*(pl->p_progname))(xdrbuf);
  105.             if (outdata == NULL && pl->p_outproc != xdr_void)
  106.                 /* there was an error */
  107.                 return;
  108.             if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
  109.                 (void) fprintf(stderr,
  110.                     "trouble replying to prog %lu\n",
  111.                     pl->p_prognum);
  112.                 exit(1);
  113.             }
  114.             /* free the decoded arguments */
  115.             (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
  116.             return;
  117.         }
  118.     (void) fprintf(stderr, "never registered prog %lu\n", prog);
  119.     exit(1);
  120. }
  121.  
  122.