home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / lan / soss.arj / RPC / SVC_UDP.C < prev    next >
C/C++ Source or Header  |  1991-02-22  |  7KB  |  245 lines

  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. #ifndef lint
  30. static char sccsid[] = "@(#)svc_udp.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  31. #endif
  32.  
  33. /*
  34.  * svc_udp.c,
  35.  * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
  36.  * achieving execute-at-most-once semantics.)
  37.  *
  38.  * Copyright (C) 1984, Sun Microsystems, Inc.
  39.  */
  40.  
  41. #include "sock.h"
  42. #include <stdio.h>
  43. #include <malloc.h>
  44. #include "types.h"
  45. #include "in.h"
  46. #include <errno.h>
  47. #include "xdr.h"
  48. #include "auth.h"
  49. #include "clnt.h"
  50. #include "rpc_msg.h"
  51. #include "svc.h"
  52.  
  53.  
  54.  
  55. #define rpc_buffer(xprt) ((xprt)->xp_p1)
  56. #define MAX(a, b)     ((a > b) ? a : b)
  57.  
  58. static bool_t        svcudp_recv();
  59. static bool_t        svcudp_reply();
  60. static enum xprt_stat    svcudp_stat();
  61. static bool_t        svcudp_getargs();
  62. static bool_t        svcudp_freeargs();
  63. static void        svcudp_destroy();
  64.  
  65. static struct xp_ops svcudp_op = {
  66.     svcudp_recv,
  67.     svcudp_stat,
  68.     svcudp_getargs,
  69.     svcudp_reply,
  70.     svcudp_freeargs,
  71.     svcudp_destroy
  72. };
  73.  
  74. extern int errno;
  75.  
  76. /*
  77.  * kept in xprt->xp_p2
  78.  */
  79. struct svcudp_data {
  80.     u_int   su_iosz;    /* byte size of send.recv buffer */
  81.     u_long    su_xid;        /* transaction id */
  82.     XDR    su_xdrs;    /* XDR handle */
  83.     char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
  84. };
  85. #define    su_data(xprt)    ((struct svcudp_data *)(xprt->xp_p2))
  86.  
  87. /*
  88.  * Usage:
  89.  *    xprt = svcudp_create(sock);
  90.  * * If sock<0 then a socket is created, else sock is used.
  91.  * If the socket, sock is not bound to a port then svcudp_create
  92.  * binds it to an arbitrary port.  In any (successful) case,
  93.  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  94.  * associated port number.
  95.  * Once *xprt is initialized, it is registered as a transporter;
  96.  * see (svc.h, xprt_register).
  97.  * The routines returns NULL if a problem occurred.
  98.  */
  99. SVCXPRT *
  100. svcudp_bufcreate(sock, sendsz, recvsz)
  101.     register int sock;
  102.     u_int sendsz, recvsz;
  103. {
  104.     bool_t madesock = FALSE;
  105.     register SVCXPRT *xprt;
  106.     register struct svcudp_data *su;
  107.     struct sockaddr_in addr;
  108.  
  109.     if (sock_getsockaddr(sock, &addr) == -1) {
  110.         (void) fprintf(stderr, "svcudp: cannot get socket addr\n");
  111.         (void) sock_close(sock);
  112.         return NULL;
  113.     }
  114.     xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
  115.     if (xprt == NULL) {
  116.         fprintf(stderr, "svcudp_create: out of memory\n");
  117.         (void) sock_close(sock);
  118.         return (NULL);
  119.     }
  120.     su = (struct svcudp_data *)mem_alloc(sizeof(*su));
  121.     if (su == NULL) {
  122.         fprintf(stderr, "svcudp_create: out of memory\n");
  123.         (void) sock_close(sock);
  124.         return (NULL);
  125.     }
  126.     su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
  127.     if ((rpc_buffer(xprt) = _fmalloc(su->su_iosz)) == NULL) {
  128.         fprintf(stderr, "svcudp_create: out of memory\n");
  129.         return (NULL);
  130.     }
  131.     xdrmem_create(
  132.         &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
  133.     xprt->xp_p2 = (caddr_t)su;
  134.     xprt->xp_verf.oa_base = su->su_verfbody;
  135.     xprt->xp_ops = &svcudp_op;
  136.     xprt->xp_port = ntohs(addr.sin_port);
  137.     xprt->xp_sock = sock;
  138.     xprt_register(xprt);
  139.     return (xprt);
  140. }
  141.  
  142. SVCXPRT *
  143. svcudp_create(sock, sz)    /* modification - create big size if sz == 1 */
  144.     int sock;
  145.     int sz;
  146. {
  147.     if (sz == 1)
  148.         return svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE);
  149.     else
  150.         return svcudp_bufcreate(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
  151. }
  152.  
  153. static enum xprt_stat
  154. svcudp_stat(xprt)
  155.     SVCXPRT *xprt;
  156. {
  157.  
  158.     return (XPRT_IDLE); 
  159. }
  160.  
  161. static bool_t
  162. svcudp_recv(xprt, msg)
  163.     register SVCXPRT *xprt;
  164.     struct rpc_msg *msg;
  165. {
  166.     struct svcudp_data *su = su_data(xprt);
  167.     XDR *xdrs = &(su->su_xdrs);
  168.     int rlen;
  169.  
  170.     again:
  171.     xprt->xp_addrlen = sizeof(struct sockaddr_in);
  172.     rlen = sock_recv(xprt->xp_sock, rpc_buffer(xprt), su->su_iosz,
  173.                  (struct sockaddr_in *) &(xprt->xp_raddr) );
  174.     if (rlen == -1 && errno == EINTR)
  175.         goto again;
  176.     if (rlen < 4*sizeof(u_long))
  177.         return (FALSE);
  178.     xdrs->x_op = XDR_DECODE;
  179.     XDR_SETPOS(xdrs, 0);
  180.     if (! xdr_callmsg(xdrs, msg))
  181.         return (FALSE);
  182.     su->su_xid = msg->rm_xid;
  183.     return (TRUE);
  184. }
  185.  
  186. static bool_t
  187. svcudp_reply(xprt, msg)
  188.     SVCXPRT *xprt; 
  189.     struct rpc_msg *msg; 
  190. {
  191.     register struct svcudp_data *su = su_data(xprt);
  192.     register XDR *xdrs = &(su->su_xdrs);
  193.     register int slen;
  194.     register bool_t stat = FALSE;
  195.  
  196.     xdrs->x_op = XDR_ENCODE;
  197.     XDR_SETPOS(xdrs, 0);
  198.     msg->rm_xid = su->su_xid;
  199.     if (xdr_replymsg(xdrs, msg)) {
  200.         slen = (int)XDR_GETPOS(xdrs);
  201.         if (sock_send(xprt->xp_sock, &(xprt->xp_raddr),
  202.                 rpc_buffer(xprt), slen) == slen)
  203.             stat = TRUE;
  204.     }
  205.     return (stat);
  206. }
  207.  
  208. static bool_t
  209. svcudp_getargs(xprt, xdr_args, args_ptr)
  210.     SVCXPRT *xprt;
  211.     xdrproc_t xdr_args;
  212.     caddr_t args_ptr;
  213. {
  214.  
  215.     return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
  216. }
  217.  
  218. static bool_t
  219. svcudp_freeargs(xprt, xdr_args, args_ptr)
  220.     SVCXPRT *xprt;
  221.     xdrproc_t xdr_args;
  222.     caddr_t args_ptr;
  223. {
  224.     register XDR *xdrs = &(su_data(xprt)->su_xdrs);
  225.  
  226.     xdrs->x_op = XDR_FREE;
  227.     (void)((*xdr_args)(xdrs, args_ptr));
  228.     free(args_ptr);
  229.     return(TRUE);
  230. }
  231.  
  232. static void
  233. svcudp_destroy(xprt)
  234.     register SVCXPRT *xprt;
  235. {
  236.     register struct svcudp_data *su = su_data(xprt);
  237.  
  238.     xprt_unregister(xprt);
  239.     (void)close(xprt->xp_sock);
  240.     XDR_DESTROY(&(su->su_xdrs));
  241.     _ffree(rpc_buffer(xprt));
  242.     mem_free((caddr_t)su, sizeof(struct svcudp_data));
  243.     mem_free((caddr_t)xprt, sizeof(SVCXPRT));
  244. }
  245.