Back to index

glibc  2.9
svc_simple.c
Go to the documentation of this file.
00001 /* @(#)svc_simple.c  2.2 88/08/01 4.0 RPCSRC */
00002 /*
00003  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00004  * unrestricted use provided that this legend is included on all tape
00005  * media and as a part of the software program in whole or part.  Users
00006  * may copy or modify Sun RPC without charge, but are not authorized
00007  * to license or distribute it to anyone else except as part of a product or
00008  * program developed by the user.
00009  *
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  *
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  *
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  *
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  *
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 #if !defined(lint) && defined(SCCSIDS)
00031 static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
00032 #endif
00033 
00034 /*
00035  * svc_simple.c
00036  * Simplified front end to rpc.
00037  *
00038  * Copyright (C) 1984, Sun Microsystems, Inc.
00039  */
00040 
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <libintl.h>
00044 #include <unistd.h>
00045 #include <rpc/rpc.h>
00046 #include <rpc/pmap_clnt.h>
00047 #include <sys/socket.h>
00048 #include <netdb.h>
00049 
00050 #ifdef USE_IN_LIBIO
00051 # include <wchar.h>
00052 # include <libio/iolibio.h>
00053 #endif
00054 
00055 struct proglst_
00056   {
00057     char *(*p_progname) (char *);
00058     int p_prognum;
00059     int p_procnum;
00060     xdrproc_t p_inproc, p_outproc;
00061     struct proglst_ *p_nxt;
00062   };
00063 #ifdef _RPC_THREAD_SAFE_
00064 #define proglst RPC_THREAD_VARIABLE(svcsimple_proglst_s)
00065 #else
00066 static struct proglst_ *proglst;
00067 #endif
00068 
00069 
00070 static void universal (struct svc_req *rqstp, SVCXPRT *transp_s);
00071 #ifdef _RPC_THREAD_SAFE_
00072 #define transp RPC_THREAD_VARIABLE(svcsimple_transp_s)
00073 #else
00074 static SVCXPRT *transp;
00075 #endif
00076 
00077 int
00078 registerrpc (u_long prognum, u_long versnum, u_long procnum,
00079             char *(*progname) (char *), xdrproc_t inproc, xdrproc_t outproc)
00080 {
00081   struct proglst_ *pl;
00082   char *buf;
00083 
00084   if (procnum == NULLPROC)
00085     {
00086 
00087       if (__asprintf (&buf, _("can't reassign procedure number %ld\n"),
00088                     NULLPROC) < 0)
00089        buf = NULL;
00090       goto err_out;
00091     }
00092   if (transp == 0)
00093     {
00094       transp = INTUSE(svcudp_create) (RPC_ANYSOCK);
00095       if (transp == NULL)
00096        {
00097          buf = strdup (_("couldn't create an rpc server\n"));
00098          goto err_out;
00099        }
00100     }
00101   (void) pmap_unset ((u_long) prognum, (u_long) versnum);
00102   if (!svc_register (transp, (u_long) prognum, (u_long) versnum,
00103                    universal, IPPROTO_UDP))
00104     {
00105       if (__asprintf (&buf, _("couldn't register prog %ld vers %ld\n"),
00106                     prognum, versnum) < 0)
00107        buf = NULL;
00108       goto err_out;
00109     }
00110   pl = (struct proglst_ *) malloc (sizeof (struct proglst_));
00111   if (pl == NULL)
00112     {
00113       buf = strdup (_("registerrpc: out of memory\n"));
00114       goto err_out;
00115     }
00116   pl->p_progname = progname;
00117   pl->p_prognum = prognum;
00118   pl->p_procnum = procnum;
00119   pl->p_inproc = inproc;
00120   pl->p_outproc = outproc;
00121   pl->p_nxt = proglst;
00122   proglst = pl;
00123   return 0;
00124 
00125  err_out:
00126   if (buf == NULL)
00127     return -1;
00128   (void) __fxprintf (NULL, "%s", buf);
00129   free (buf);
00130   return -1;
00131 }
00132 
00133 static void
00134 universal (struct svc_req *rqstp, SVCXPRT *transp_l)
00135 {
00136   int prog, proc;
00137   char *outdata;
00138   char xdrbuf[UDPMSGSIZE];
00139   struct proglst_ *pl;
00140   char *buf = NULL;
00141 
00142   /*
00143    * enforce "procnum 0 is echo" convention
00144    */
00145   if (rqstp->rq_proc == NULLPROC)
00146     {
00147       if (INTUSE(svc_sendreply) (transp_l, (xdrproc_t)INTUSE(xdr_void),
00148                              (char *) NULL) == FALSE)
00149        {
00150          __write (STDERR_FILENO, "xxx\n", 4);
00151          exit (1);
00152        }
00153       return;
00154     }
00155   prog = rqstp->rq_prog;
00156   proc = rqstp->rq_proc;
00157   for (pl = proglst; pl != NULL; pl = pl->p_nxt)
00158     if (pl->p_prognum == prog && pl->p_procnum == proc)
00159       {
00160        /* decode arguments into a CLEAN buffer */
00161        __bzero (xdrbuf, sizeof (xdrbuf)); /* required ! */
00162        if (!svc_getargs (transp_l, pl->p_inproc, xdrbuf))
00163          {
00164            INTUSE(svcerr_decode) (transp_l);
00165            return;
00166          }
00167        outdata = (*(pl->p_progname)) (xdrbuf);
00168        if (outdata == NULL && pl->p_outproc != (xdrproc_t)INTUSE(xdr_void))
00169          /* there was an error */
00170          return;
00171        if (!INTUSE(svc_sendreply) (transp_l, pl->p_outproc, outdata))
00172          {
00173            if (__asprintf (&buf, _("trouble replying to prog %d\n"),
00174                          pl->p_prognum) < 0)
00175              buf = NULL;
00176            goto err_out2;
00177          }
00178        /* free the decoded arguments */
00179        (void) svc_freeargs (transp_l, pl->p_inproc, xdrbuf);
00180        return;
00181       }
00182   if (__asprintf (&buf, _("never registered prog %d\n"), prog) < 0)
00183     buf = NULL;
00184  err_out2:
00185   if (buf == NULL)
00186     exit (1);
00187   __fxprintf (NULL, "%s", buf);
00188   free (buf);
00189   exit (1);
00190 }