home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / amd / part06 / misc_rpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-10  |  8.2 KB  |  315 lines

  1. /*
  2.  * $Id: misc_rpc.c,v 5.1.1.1 90/01/11 17:08:49 jsp Exp Locker: jsp $
  3.  *
  4.  * Copyright (c) 1990 Jan-Simon Pendry
  5.  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
  6.  * Copyright (c) 1990 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * This code is derived from software contributed to Berkeley by
  10.  * Jan-Simon Pendry at Imperial College, London.
  11.  *
  12.  * Redistribution and use in source and binary forms are permitted
  13.  * provided that the above copyright notice and this paragraph are
  14.  * duplicated in all such forms and that any documentation,
  15.  * advertising materials, and other materials related to such
  16.  * distribution and use acknowledge that the software was developed
  17.  * by Imperial College of Science, Technology and Medicine, London, UK.
  18.  * The names of the College and University may not be used to endorse
  19.  * or promote products derived from this software without specific
  20.  * prior written permission.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  *
  25.  *    %W% (Berkeley) %G%
  26.  */
  27.  
  28. /*
  29.  * Additions to Sun RPC.
  30.  */
  31.  
  32. #include "am.h"
  33.  
  34. void rpc_msg_init P((struct rpc_msg *mp, u_long prog, u_long vers, u_long proc));
  35. void rpc_msg_init(mp, prog, vers, proc)
  36. struct rpc_msg *mp;
  37. unsigned long prog, vers, proc;
  38. {
  39.     /*
  40.      * Initialise the message
  41.      */
  42.     bzero((voidp) mp, sizeof(*mp));
  43.     mp->rm_xid = 0;
  44.     mp->rm_direction = CALL;
  45.     mp->rm_call.cb_rpcvers = RPC_MSG_VERSION;
  46.     mp->rm_call.cb_prog = prog;
  47.     mp->rm_call.cb_vers = vers;
  48.     mp->rm_call.cb_proc = proc;
  49. }
  50.  
  51. /*
  52.  * Field reply to call to mountd
  53.  */
  54. int pickup_rpc_reply P((voidp pkt, int len, voidp where, xdrproc_t where_xdr));
  55. int pickup_rpc_reply(pkt, len, where, where_xdr)
  56. voidp pkt;
  57. int len;
  58. voidp where;
  59. xdrproc_t where_xdr;
  60. {
  61.     XDR reply_xdr;
  62.     int ok;
  63.     struct rpc_err err;
  64.     struct rpc_msg reply_msg;
  65.     int error = 0;
  66.  
  67.     /*bzero((voidp) &err, sizeof(err));*/
  68.     bzero((voidp) &reply_msg, sizeof(reply_msg));
  69.  
  70.     reply_msg.acpted_rply.ar_results.where = (caddr_t) where;
  71.     reply_msg.acpted_rply.ar_results.proc = where_xdr;
  72.  
  73.     xdrmem_create(&reply_xdr, pkt, len, XDR_DECODE);
  74.  
  75.     ok = xdr_replymsg(&reply_xdr, &reply_msg);
  76.     if (!ok) {
  77.         error = EIO;
  78.         goto drop;
  79.     }
  80.     _seterr_reply(&reply_msg, &err);
  81.     if (err.re_status != RPC_SUCCESS) {
  82.         error = EIO;
  83.         goto drop;
  84.     }
  85.  
  86. drop:
  87.     if (reply_msg.acpted_rply.ar_verf.oa_base) {
  88.         reply_xdr.x_op = XDR_FREE;
  89.         (void)xdr_opaque_auth(&reply_xdr,
  90.             &reply_msg.acpted_rply.ar_verf);
  91.     }
  92.     xdr_destroy(&reply_xdr);
  93.  
  94.     return error;
  95. }
  96.  
  97. int make_rpc_packet P((char *buf, int buflen, unsigned long proc,
  98.             struct rpc_msg *mp, voidp arg, xdrproc_t arg_xdr, AUTH *auth));
  99. int make_rpc_packet(buf, buflen, proc, mp, arg, arg_xdr, auth)
  100. char *buf;
  101. int buflen;
  102. unsigned long proc;
  103. struct rpc_msg *mp;
  104. voidp arg;
  105. xdrproc_t arg_xdr;
  106. AUTH *auth;
  107. {
  108.     XDR msg_xdr;
  109.     int len;
  110.  
  111.     xdrmem_create(&msg_xdr, buf, buflen, XDR_ENCODE);
  112.     /*
  113.      * Basic protocol header
  114.      */
  115.     if (!xdr_callhdr(&msg_xdr, mp))
  116.         return -EIO;
  117.     /*
  118.      * Called procedure number
  119.      */
  120.     if (!xdr_enum(&msg_xdr, &proc))
  121.         return -EIO;
  122.     /*
  123.      * Authorization
  124.      */
  125.     if (!AUTH_MARSHALL(auth, &msg_xdr))
  126.         return -EIO;
  127.     /*
  128.      * Arguments
  129.      */
  130.     if (!(*arg_xdr)(&msg_xdr, arg))
  131.         return -EIO;
  132.     /*
  133.      * Determine length
  134.      */
  135.     len = xdr_getpos(&msg_xdr);
  136.     /*
  137.      * Throw away xdr
  138.      */
  139.     xdr_destroy(&msg_xdr);
  140.     return len;
  141. }
  142.  
  143.  
  144. #ifdef MISC_RPC
  145. /*
  146.  * Early RPC seems to be missing these..
  147.  * Extracted from the RPC 3.9 sources as indicated
  148.  */
  149.  
  150. /* @(#)xdr_reference.c    1.1 87/11/04 3.9 RPCSRC */
  151. /*
  152.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  153.  * unrestricted use provided that this legend is included on all tape
  154.  * media and as a part of the software program in whole or part.  Users
  155.  * may copy or modify Sun RPC without charge, but are not authorized
  156.  * to license or distribute it to anyone else except as part of a product or
  157.  * program developed by the user.
  158.  * 
  159.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  160.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  161.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  162.  * 
  163.  * Sun RPC is provided with no support and without any obligation on the
  164.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  165.  * modification or enhancement.
  166.  * 
  167.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  168.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  169.  * OR ANY PART THEREOF.
  170.  * 
  171.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  172.  * or profits or other special, indirect and consequential damages, even if
  173.  * Sun has been advised of the possibility of such damages.
  174.  * 
  175.  * Sun Microsystems, Inc.
  176.  * 2550 Garcia Avenue
  177.  * Mountain View, California  94043
  178.  */
  179.  
  180.  
  181. /*
  182.  * xdr_pointer():
  183.  *
  184.  * XDR a pointer to a possibly recursive data structure. This
  185.  * differs with xdr_reference in that it can serialize/deserialiaze
  186.  * trees correctly.
  187.  *
  188.  *  What's sent is actually a union:
  189.  *
  190.  *  union object_pointer switch (boolean b) {
  191.  *  case TRUE: object_data data;
  192.  *  case FALSE: void nothing;
  193.  *  }
  194.  *
  195.  * > objpp: Pointer to the pointer to the object.
  196.  * > obj_size: size of the object.
  197.  * > xdr_obj: routine to XDR an object.
  198.  *
  199.  */
  200. bool_t
  201. xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
  202.     register XDR *xdrs;
  203.     char **objpp;
  204.     u_int obj_size;
  205.     xdrproc_t xdr_obj;
  206. {
  207.  
  208.     bool_t more_data;
  209.  
  210.     more_data = (*objpp != NULL);
  211.     if (! xdr_bool(xdrs,&more_data)) {
  212.         return (FALSE);
  213.     }
  214.     if (! more_data) {
  215.         *objpp = NULL;
  216.         return (TRUE);
  217.     }
  218.     return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
  219. }
  220.  
  221. /* @(#)clnt_perror.c    1.1 87/11/04 3.9 RPCSRC */
  222. /*
  223.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  224.  * unrestricted use provided that this legend is included on all tape
  225.  * media and as a part of the software program in whole or part.  Users
  226.  * may copy or modify Sun RPC without charge, but are not authorized
  227.  * to license or distribute it to anyone else except as part of a product or
  228.  * program developed by the user.
  229.  * 
  230.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  231.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  232.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  233.  * 
  234.  * Sun RPC is provided with no support and without any obligation on the
  235.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  236.  * modification or enhancement.
  237.  * 
  238.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  239.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  240.  * OR ANY PART THEREOF.
  241.  * 
  242.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  243.  * or profits or other special, indirect and consequential damages, even if
  244.  * Sun has been advised of the possibility of such damages.
  245.  * 
  246.  * Sun Microsystems, Inc.
  247.  * 2550 Garcia Avenue
  248.  * Mountain View, California  94043
  249.  */
  250.  
  251. struct rpc_errtab {
  252.     enum clnt_stat status;
  253.     char *message;
  254. };
  255.  
  256. static struct rpc_errtab  rpc_errlist[] = {
  257.     { RPC_SUCCESS, 
  258.         "RPC: Success" }, 
  259.     { RPC_CANTENCODEARGS, 
  260.         "RPC: Can't encode arguments" },
  261.     { RPC_CANTDECODERES, 
  262.         "RPC: Can't decode result" },
  263.     { RPC_CANTSEND, 
  264.         "RPC: Unable to send" },
  265.     { RPC_CANTRECV, 
  266.         "RPC: Unable to receive" },
  267.     { RPC_TIMEDOUT, 
  268.         "RPC: Timed out" },
  269.     { RPC_VERSMISMATCH, 
  270.         "RPC: Incompatible versions of RPC" },
  271.     { RPC_AUTHERROR, 
  272.         "RPC: Authentication error" },
  273.     { RPC_PROGUNAVAIL, 
  274.         "RPC: Program unavailable" },
  275.     { RPC_PROGVERSMISMATCH, 
  276.         "RPC: Program/version mismatch" },
  277.     { RPC_PROCUNAVAIL, 
  278.         "RPC: Procedure unavailable" },
  279.     { RPC_CANTDECODEARGS, 
  280.         "RPC: Server can't decode arguments" },
  281.     { RPC_SYSTEMERROR, 
  282.         "RPC: Remote system error" },
  283.     { RPC_UNKNOWNHOST, 
  284.         "RPC: Unknown host" },
  285. /*    { RPC_UNKNOWNPROTO,
  286.         "RPC: Unknown protocol" },*/
  287.     { RPC_PMAPFAILURE, 
  288.         "RPC: Port mapper failure" },
  289.     { RPC_PROGNOTREGISTERED, 
  290.         "RPC: Program not registered"},
  291.     { RPC_FAILED, 
  292.         "RPC: Failed (unspecified error)"}
  293. };
  294.  
  295.  
  296. /*
  297.  * This interface for use by clntrpc
  298.  */
  299. char *
  300. clnt_sperrno(stat)
  301.     enum clnt_stat stat;
  302. {
  303.     int i;
  304.  
  305.     for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {
  306.         if (rpc_errlist[i].status == stat) {
  307.             return (rpc_errlist[i].message);
  308.         }
  309.     }
  310.     return ("RPC: (unknown error code)");
  311. }
  312.  
  313. #endif /* MISC_RPC */
  314.  
  315.