home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / rpc2 / part08 / rpc / rpclib / svc_udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  6.4 KB  |  252 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 <stdio.h>
  42. #include "types.h"
  43. #include <netinet/in.h>
  44. #include <sys/socket.h>
  45. #include <errno.h>
  46. #include "xdr.h"
  47. #include "auth.h"
  48. #include "clnt.h"
  49. #include "rpc_msg.h"
  50. #include "svc.h"
  51.  
  52. char *mem_alloc();
  53.  
  54. #define rpc_buffer(xprt) ((xprt)->xp_p1)
  55. #define MAX(a, b)     ((a > b) ? a : b)
  56.  
  57. static bool_t        svcudp_recv();
  58. static bool_t        svcudp_reply();
  59. static enum xprt_stat    svcudp_stat();
  60. static bool_t        svcudp_getargs();
  61. static bool_t        svcudp_freeargs();
  62. static void        svcudp_destroy();
  63.  
  64. static struct xp_ops svcudp_op = {
  65.     svcudp_recv,
  66.     svcudp_stat,
  67.     svcudp_getargs,
  68.     svcudp_reply,
  69.     svcudp_freeargs,
  70.     svcudp_destroy
  71. };
  72.  
  73. extern int errno;
  74.  
  75. /*
  76.  * kept in xprt->xp_p2
  77.  */
  78. struct svcudp_data {
  79.     u_int   su_iosz;    /* byte size of send.recv buffer */
  80.     u_long    su_xid;        /* transaction id */
  81.     XDR    su_xdrs;    /* XDR handle */
  82.     char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
  83. };
  84. #define    su_data(xprt)    ((struct svcudp_data *)(xprt->xp_p2))
  85.  
  86. /*
  87.  * Usage:
  88.  *    xprt = svcudp_create(sock);
  89.  *
  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.     int len = sizeof(struct sockaddr_in);
  109.  
  110.     if (sock == RPC_ANYSOCK) {
  111.         if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  112.             perror("svcudp_create: socket creation problem");
  113.             return ((SVCXPRT *)NULL);
  114.         }
  115.         madesock = TRUE;
  116.     }
  117.     addr.sin_addr.s_addr = 0;
  118.     addr.sin_family = AF_INET;
  119.     addr.sin_port = 0;
  120.     (void)bind(sock, (struct sockaddr *)&addr, len);
  121.     if (getsockname(sock, (caddr_t)&addr, &len) != 0) {
  122.         perror("svcudp_create - cannot getsockname");
  123.         if (madesock)
  124.             (void)close(sock);
  125.         return ((SVCXPRT *)NULL);
  126.     }
  127.     xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
  128.     if (xprt == NULL) {
  129.         fprintf(stderr, "svcudp_create: out of memory\n");
  130.         return (NULL);
  131.     }
  132.     su = (struct svcudp_data *)mem_alloc(sizeof(*su));
  133.     if (su == NULL) {
  134.         fprintf(stderr, "svcudp_create: out of memory\n");
  135.         return (NULL);
  136.     }
  137.     su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
  138.     if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
  139.         fprintf(stderr, "svcudp_create: out of memory\n");
  140.         return (NULL);
  141.     }
  142.     xdrmem_create(
  143.         &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
  144.     xprt->xp_p2 = (caddr_t)su;
  145.     xprt->xp_verf.oa_base = su->su_verfbody;
  146.     xprt->xp_ops = &svcudp_op;
  147.     xprt->xp_port = ntohs(addr.sin_port);
  148.     xprt->xp_sock = sock;
  149.     xprt_register(xprt);
  150.     return (xprt);
  151. }
  152.  
  153. SVCXPRT *
  154. svcudp_create(sock, sendsz, recvsz)
  155.     int sock;
  156. {
  157.  
  158.     return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
  159. }
  160.  
  161. static enum xprt_stat
  162. svcudp_stat(xprt)
  163.     SVCXPRT *xprt;
  164. {
  165.  
  166.     return (XPRT_IDLE); 
  167. }
  168.  
  169. static bool_t
  170. svcudp_recv(xprt, msg)
  171.     register SVCXPRT *xprt;
  172.     struct rpc_msg *msg;
  173. {
  174.     register struct svcudp_data *su = su_data(xprt);
  175.     register XDR *xdrs = &(su->su_xdrs);
  176.     register int rlen;
  177.  
  178.     again:
  179.     xprt->xp_addrlen = sizeof(struct sockaddr_in);
  180.     rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), su->su_iosz,
  181.         0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
  182.     if (rlen == -1 && errno == EINTR)
  183.         goto again;
  184.     if (rlen < 4*sizeof(u_long))
  185.         return (FALSE);
  186.     xdrs->x_op = XDR_DECODE;
  187.     XDR_SETPOS(xdrs, 0);
  188.     if (! xdr_callmsg(xdrs, msg))
  189.         return (FALSE);
  190.     su->su_xid = msg->rm_xid;
  191.     return (TRUE);
  192. }
  193.  
  194. static bool_t
  195. svcudp_reply(xprt, msg)
  196.     register SVCXPRT *xprt; 
  197.     struct rpc_msg *msg; 
  198. {
  199.     register struct svcudp_data *su = su_data(xprt);
  200.     register XDR *xdrs = &(su->su_xdrs);
  201.     register int slen;
  202.     register bool_t stat = FALSE;
  203.  
  204.     xdrs->x_op = XDR_ENCODE;
  205.     XDR_SETPOS(xdrs, 0);
  206.     msg->rm_xid = su->su_xid;
  207.     if (xdr_replymsg(xdrs, msg)) {
  208.         slen = (int)XDR_GETPOS(xdrs);
  209.         if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
  210.             (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
  211.             == slen)
  212.             stat = TRUE;
  213.     }
  214.     return (stat);
  215. }
  216.  
  217. static bool_t
  218. svcudp_getargs(xprt, xdr_args, args_ptr)
  219.     SVCXPRT *xprt;
  220.     xdrproc_t xdr_args;
  221.     caddr_t args_ptr;
  222. {
  223.  
  224.     return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
  225. }
  226.  
  227. static bool_t
  228. svcudp_freeargs(xprt, xdr_args, args_ptr)
  229.     SVCXPRT *xprt;
  230.     xdrproc_t xdr_args;
  231.     caddr_t args_ptr;
  232. {
  233.     register XDR *xdrs = &(su_data(xprt)->su_xdrs);
  234.  
  235.     xdrs->x_op = XDR_FREE;
  236.     return ((*xdr_args)(xdrs, args_ptr));
  237. }
  238.  
  239. static void
  240. svcudp_destroy(xprt)
  241.     register SVCXPRT *xprt;
  242. {
  243.     register struct svcudp_data *su = su_data(xprt);
  244.  
  245.     xprt_unregister(xprt);
  246.     (void)close(xprt->xp_sock);
  247.     XDR_DESTROY(&(su->su_xdrs));
  248.     mem_free(rpc_buffer(xprt), su->su_iosz);
  249.     mem_free((caddr_t)su, sizeof(struct svcudp_data));
  250.     mem_free((caddr_t)xprt, sizeof(SVCXPRT));
  251. }
  252.