home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / svc_udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-30  |  14.2 KB  |  572 lines

  1. /* @(#)svc_udp.c    2.2 88/07/29 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * svc_udp.c,
  36.  * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
  37.  * achieving execute-at-most-once semantics.)
  38.  *
  39.  * Copyright (C) 1984, Sun Microsystems, Inc.
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <rpc/rpc.h>
  44. #include <sys/socket.h>
  45. #include <errno.h>
  46.  
  47. #include <unistd.h>
  48. #include <strings.h>
  49.  
  50. #ifdef __STDC__
  51. #include <stdlib.h>
  52. #endif
  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. #if NLS
  76. #include "nl_types.h"
  77. #endif
  78.  
  79. /*
  80.  * kept in xprt->xp_p2
  81.  */
  82. struct svcudp_data {
  83.     u_int   su_iosz;    /* byte size of send.recv buffer */
  84.     u_long    su_xid;        /* transaction id */
  85.     XDR    su_xdrs;    /* XDR handle */
  86.     char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
  87.     char *     su_cache;    /* cached data, NULL if no cache */
  88. };
  89. #define    su_data(xprt)    ((struct svcudp_data *)(xprt->xp_p2))
  90.  
  91. static void cache_set(SVCXPRT *, u_long);
  92. static int cache_get (SVCXPRT *, struct rpc_msg *, char **, u_long *);
  93.  
  94. /*
  95.  * Usage:
  96.  *    xprt = svcudp_create(sock);
  97.  *
  98.  * If sock<0 then a socket is created, else sock is used.
  99.  * If the socket, sock is not bound to a port then svcudp_create
  100.  * binds it to an arbitrary port.  In any (successful) case,
  101.  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  102.  * associated port number.
  103.  * Once *xprt is initialized, it is registered as a transporter;
  104.  * see (svc.h, xprt_register).
  105.  * The routines returns NULL if a problem occurred.
  106.  */
  107. SVCXPRT *
  108. svcudp_bufcreate(sock, sendsz, recvsz)
  109.     register int sock;
  110.     u_int sendsz, recvsz;
  111. {
  112.     bool_t madesock = FALSE;
  113.     register SVCXPRT *xprt;
  114.     register struct svcudp_data *su;
  115.     struct sockaddr_in addr;
  116.     int len = sizeof(struct sockaddr_in);
  117.  
  118. #if NLS
  119.     libc_nls_init();
  120. #endif
  121.     if (sock == RPC_ANYSOCK) {
  122.         if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  123. #if NLS
  124.             perror(catgets(_libc_cat, RpcMiscSet,
  125.                    RpcMiscUdpSocketCreateProblem,
  126.                    "svcudp_create: socket creation problem"));
  127. #else
  128.             perror("svcudp_create: socket creation problem");
  129. #endif
  130.             return ((SVCXPRT *)NULL);
  131.         }
  132.         madesock = TRUE;
  133.     }
  134.     bzero((char *)&addr, sizeof (addr));
  135.     addr.sin_family = AF_INET;
  136.     if (bindresvport(sock, &addr)) {
  137.         addr.sin_port = 0;
  138.         (void)bind(sock, (struct sockaddr *)&addr, len);
  139.     }
  140.     if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
  141. #if NLS
  142.         perror(catgets(_libc_cat, RpcMiscSet,
  143.                 RpcMiscUdpCantGetName,
  144.                 "svcudp_create - cannot getsockname"));
  145. #else
  146.         perror("svcudp_create - cannot getsockname");
  147. #endif
  148.         if (madesock)
  149.             (void)close(sock);
  150.         return ((SVCXPRT *)NULL);
  151.     }
  152.     xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
  153.     if (xprt == NULL) {
  154. #if NLS
  155.         (void)fprintf(stderr, "svcudp_create: %s\n",
  156.                               catgets(_libc_cat, RpcMiscSet,
  157.                                       RpcMiscOutOfMemory, "out of memory"));
  158. #else
  159.         (void)fprintf(stderr, "svcudp_create: out of memory\n");
  160. #endif
  161.         return (NULL);
  162.     }
  163.     su = (struct svcudp_data *)mem_alloc(sizeof(*su));
  164.     if (su == NULL) {
  165. #if NLS
  166.         (void)fprintf(stderr, "svcudp_create: %s\n",
  167.                               catgets(_libc_cat, RpcMiscSet,
  168.                                       RpcMiscOutOfMemory, "out of memory"));
  169. #else
  170.         (void)fprintf(stderr, "svcudp_create: out of memory\n");
  171. #endif
  172.         return (NULL);
  173.     }
  174.     su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
  175.     if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
  176. #if NLS
  177.         (void)fprintf(stderr, "svcudp_create: %s\n",
  178.                               catgets(_libc_cat, RpcMiscSet,
  179.                                       RpcMiscOutOfMemory, "out of memory"));
  180. #else
  181.  
  182.         (void)fprintf(stderr, "svcudp_create: out of memory\n");
  183. #endif
  184.         return (NULL);
  185.     }
  186.     xdrmem_create(
  187.         &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
  188.     su->su_cache = NULL;
  189.     xprt->xp_p2 = (caddr_t)su;
  190.     xprt->xp_verf.oa_base = su->su_verfbody;
  191.     xprt->xp_ops = &svcudp_op;
  192.     xprt->xp_port = ntohs(addr.sin_port);
  193.     xprt->xp_sock = sock;
  194.     xprt_register(xprt);
  195.     return (xprt);
  196. }
  197.  
  198. SVCXPRT *
  199. svcudp_create(sock)
  200.     int sock;
  201. {
  202.  
  203.     return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
  204. }
  205.  
  206. static enum xprt_stat
  207. svcudp_stat(xprt)
  208.     SVCXPRT *xprt;
  209. {
  210.  
  211.     return (XPRT_IDLE); 
  212. }
  213.  
  214. static bool_t
  215. svcudp_recv(xprt, msg)
  216.     register SVCXPRT *xprt;
  217.     struct rpc_msg *msg;
  218. {
  219.     register struct svcudp_data *su = su_data(xprt);
  220.     register XDR *xdrs = &(su->su_xdrs);
  221.     register int rlen;
  222.     char *reply;
  223.     u_long replylen;
  224.  
  225.     again:
  226.     xprt->xp_addrlen = sizeof(struct sockaddr_in);
  227.     rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
  228.         0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
  229.     if (rlen == -1 && errno == EINTR)
  230.         goto again;
  231.     if (rlen < (int) (4*sizeof(u_long)))
  232.         return (FALSE);
  233.     xdrs->x_op = XDR_DECODE;
  234.     XDR_SETPOS(xdrs, 0);
  235.     if (! xdr_callmsg(xdrs, msg))
  236.         return (FALSE);
  237.     su->su_xid = msg->rm_xid;
  238.     if (su->su_cache != NULL) {
  239.         if (cache_get(xprt, msg, &reply, &replylen)) {
  240.             (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
  241.               (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
  242.             return (TRUE);
  243.         }
  244.     }
  245.     return (TRUE);
  246. }
  247.  
  248. static bool_t
  249. svcudp_reply(xprt, msg)
  250.     register SVCXPRT *xprt; 
  251.     struct rpc_msg *msg; 
  252. {
  253.     register struct svcudp_data *su = su_data(xprt);
  254.     register XDR *xdrs = &(su->su_xdrs);
  255.     register int slen;
  256.     register bool_t stat = FALSE;
  257.  
  258.     xdrs->x_op = XDR_ENCODE;
  259.     XDR_SETPOS(xdrs, 0);
  260.     msg->rm_xid = su->su_xid;
  261.     if (xdr_replymsg(xdrs, msg)) {
  262.         slen = (int)XDR_GETPOS(xdrs);
  263.         if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
  264.             (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
  265.             == slen) {
  266.             stat = TRUE;
  267.             if (su->su_cache && slen >= 0) {
  268.                 cache_set(xprt, (u_long) slen);
  269.             }
  270.         }
  271.     }
  272.     return (stat);
  273. }
  274.  
  275. static bool_t
  276. svcudp_getargs(xprt, xdr_args, args_ptr)
  277.     SVCXPRT *xprt;
  278.     xdrproc_t xdr_args;
  279.     caddr_t args_ptr;
  280. {
  281.  
  282.     return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
  283. }
  284.  
  285. static bool_t
  286. svcudp_freeargs(xprt, xdr_args, args_ptr)
  287.     SVCXPRT *xprt;
  288.     xdrproc_t xdr_args;
  289.     caddr_t args_ptr;
  290. {
  291.     register XDR *xdrs = &(su_data(xprt)->su_xdrs);
  292.  
  293.     xdrs->x_op = XDR_FREE;
  294.     return ((*xdr_args)(xdrs, args_ptr));
  295. }
  296.  
  297. static void
  298. svcudp_destroy(xprt)
  299.     register SVCXPRT *xprt;
  300. {
  301.     register struct svcudp_data *su = su_data(xprt);
  302.  
  303.     xprt_unregister(xprt);
  304.     (void)close(xprt->xp_sock);
  305.     XDR_DESTROY(&(su->su_xdrs));
  306.     mem_free(rpc_buffer(xprt), su->su_iosz);
  307.     mem_free((caddr_t)su, sizeof(struct svcudp_data));
  308.     mem_free((caddr_t)xprt, sizeof(SVCXPRT));
  309. }
  310.  
  311.  
  312. /***********this could be a separate file*********************/
  313.  
  314. /*
  315.  * Fifo cache for udp server
  316.  * Copies pointers to reply buffers into fifo cache
  317.  * Buffers are sent again if retransmissions are detected.
  318.  */
  319.  
  320. #define SPARSENESS 4    /* 75% sparse */
  321.  
  322. #define CACHE_PERROR(msg)    \
  323.     (void) fprintf(stderr,"%s\n", msg)
  324.  
  325. #define ALLOC(type, size)    \
  326.     (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
  327.  
  328. #define BZERO(addr, type, size)     \
  329.     bzero((char *) addr, sizeof(type) * (int) (size)) 
  330.  
  331. /*
  332.  * An entry in the cache
  333.  */
  334. typedef struct cache_node *cache_ptr;
  335. struct cache_node {
  336.     /*
  337.      * Index into cache is xid, proc, vers, prog and address
  338.      */
  339.     u_long cache_xid;
  340.     u_long cache_proc;
  341.     u_long cache_vers;
  342.     u_long cache_prog;
  343.     struct sockaddr_in cache_addr;
  344.     /*
  345.      * The cached reply and length
  346.      */
  347.     char * cache_reply;
  348.     u_long cache_replylen;
  349.     /*
  350.       * Next node on the list, if there is a collision
  351.      */
  352.     cache_ptr cache_next;    
  353. };
  354.  
  355.  
  356.  
  357. /*
  358.  * The entire cache
  359.  */
  360. struct udp_cache {
  361.     u_long uc_size;        /* size of cache */
  362.     cache_ptr *uc_entries;    /* hash table of entries in cache */
  363.     cache_ptr *uc_fifo;    /* fifo list of entries in cache */
  364.     u_long uc_nextvictim;    /* points to next victim in fifo list */
  365.     u_long uc_prog;        /* saved program number */
  366.     u_long uc_vers;        /* saved version number */
  367.     u_long uc_proc;        /* saved procedure number */
  368.     struct sockaddr_in uc_addr; /* saved caller's address */
  369. };
  370.  
  371.  
  372. /*
  373.  * the hashing function
  374.  */
  375. #define CACHE_LOC(transp, xid)    \
  376.  (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))    
  377.  
  378.  
  379. /*
  380.  * Enable use of the cache. 
  381.  * Note: there is no disable.
  382.  */
  383. int svcudp_enablecache(transp, size)
  384.     SVCXPRT *transp;
  385.     u_long size;
  386. {
  387.     struct svcudp_data *su = su_data(transp);
  388.     struct udp_cache *uc;
  389.  
  390. #if NLS
  391.     libc_nls_init();
  392. #endif
  393.     if (su->su_cache != NULL) {
  394. #if NLS
  395.         CACHE_PERROR(catgets(_libc_cat, RpcMiscSet,
  396.                      RpcMiscCacheAlreadyEnabled,
  397.                      "enablecache: cache already enabled"));
  398. #else
  399.         CACHE_PERROR("enablecache: cache already enabled");
  400. #endif
  401.         return(0);    
  402.     }
  403.     uc = ALLOC(struct udp_cache, 1);
  404.     if (uc == NULL) {
  405. #if NLS
  406.         CACHE_PERROR(catgets(_libc_cat, RpcMiscSet,
  407.                      RpcMiscCantAllocateCache,
  408.                      "enablecache: could not allocate cache"));
  409. #else
  410.         CACHE_PERROR("enablecache: could not allocate cache");
  411. #endif
  412.         return(0);
  413.     }
  414.     uc->uc_size = size;
  415.     uc->uc_nextvictim = 0;
  416.     uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
  417.     if (uc->uc_entries == NULL) {
  418. #if NLS
  419.         CACHE_PERROR(catgets(_libc_cat, RpcMiscSet,
  420.                      RpcMiscCantAllocateCacheData,
  421.                      "enablecache: could not allocate cache data"));
  422. #else
  423.         CACHE_PERROR("enablecache: could not allocate cache data");
  424. #endif
  425.         return(0);
  426.     }
  427.     BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
  428.     uc->uc_fifo = ALLOC(cache_ptr, size);
  429.     if (uc->uc_fifo == NULL) {
  430. #if NLS
  431.         CACHE_PERROR(catgets(_libc_cat, RpcMiscSet,
  432.                      RpcMiscCantAllocateCacheFifo,
  433.                      "enablecache: could not allocate cache fifo"));
  434. #else
  435.         CACHE_PERROR("enablecache: could not allocate cache fifo");
  436. #endif
  437.         return(0);
  438.     }
  439.     BZERO(uc->uc_fifo, cache_ptr, size);
  440.     su->su_cache = (char *) uc;
  441.     return(1);
  442. }
  443.  
  444.  
  445. /*
  446.  * Set an entry in the cache
  447.  */
  448. static void
  449. cache_set(xprt, replylen)
  450.     SVCXPRT *xprt;
  451.     u_long replylen;    
  452. {
  453.     register cache_ptr victim;    
  454.     register cache_ptr *vicp;
  455.     register struct svcudp_data *su = su_data(xprt);
  456.     struct udp_cache *uc = (struct udp_cache *) su->su_cache;
  457.     u_int loc;
  458.     char *newbuf;
  459.  
  460. #if NLS
  461.     libc_nls_init();
  462. #endif
  463.  
  464.     /*
  465.       * Find space for the new entry, either by
  466.      * reusing an old entry, or by mallocing a new one
  467.      */
  468.     victim = uc->uc_fifo[uc->uc_nextvictim];
  469.     if (victim != NULL) {
  470.         loc = CACHE_LOC(xprt, victim->cache_xid);
  471.         for (vicp = &uc->uc_entries[loc]; 
  472.           *vicp != NULL && *vicp != victim; 
  473.           vicp = &(*vicp)->cache_next) 
  474.                 ;
  475.         if (*vicp == NULL) {
  476. #if NLS
  477.         CACHE_PERROR(catgets(_libc_cat, RpcMiscSet,
  478.                      RpcMiscVictimNotFound,
  479.                      "cache_set: victim not found"));
  480. #else
  481.             CACHE_PERROR("cache_set: victim not found");
  482. #endif
  483.             return;
  484.         }
  485.         *vicp = victim->cache_next;    /* remote from cache */
  486.         newbuf = victim->cache_reply;
  487.     } else {
  488.         victim = ALLOC(struct cache_node, 1);
  489.         if (victim == NULL) {
  490. #if NLS
  491.         CACHE_PERROR(catgets(_libc_cat, RpcMiscSet,
  492.                      RpcMiscVictimAllocFailed,
  493.                      "cache_set: victim alloc failed"));
  494. #else
  495.             CACHE_PERROR("cache_set: victim alloc failed");
  496. #endif
  497.             return;
  498.         }
  499.         newbuf = mem_alloc(su->su_iosz);
  500.         if (newbuf == NULL) {
  501. #if NLS
  502.         CACHE_PERROR(catgets(_libc_cat, RpcMiscSet,
  503.                      RpcMiscCantAllocateRpcBuffer,
  504.                      "cache_set: could not allocate new rpc_buffer"));
  505. #else
  506.             CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
  507. #endif
  508.             return;
  509.         }
  510.     }
  511.  
  512.     /*
  513.      * Store it away
  514.      */
  515.     victim->cache_replylen = replylen;
  516.     victim->cache_reply = rpc_buffer(xprt);
  517.     rpc_buffer(xprt) = newbuf;
  518.     xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE);
  519.     victim->cache_xid = su->su_xid;
  520.     victim->cache_proc = uc->uc_proc;
  521.     victim->cache_vers = uc->uc_vers;
  522.     victim->cache_prog = uc->uc_prog;
  523.     victim->cache_addr = uc->uc_addr;
  524.     loc = CACHE_LOC(xprt, victim->cache_xid);
  525.     victim->cache_next = uc->uc_entries[loc];    
  526.     uc->uc_entries[loc] = victim;
  527.     uc->uc_fifo[uc->uc_nextvictim++] = victim;
  528.     uc->uc_nextvictim %= uc->uc_size;
  529. }
  530.  
  531. /*
  532.  * Try to get an entry from the cache
  533.  * return 1 if found, 0 if not found
  534.  */
  535. static int
  536. cache_get(xprt, msg, replyp, replylenp)
  537.     SVCXPRT *xprt;
  538.     struct rpc_msg *msg;
  539.     char **replyp;
  540.     u_long *replylenp;
  541. {
  542.     u_int loc;
  543.     register cache_ptr ent;
  544.     register struct svcudp_data *su = su_data(xprt);
  545.     register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
  546.  
  547. #    define EQADDR(a1, a2)    (bcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
  548.  
  549.     loc = CACHE_LOC(xprt, su->su_xid);
  550.     for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
  551.         if (ent->cache_xid == su->su_xid &&
  552.           ent->cache_proc == uc->uc_proc &&
  553.           ent->cache_vers == uc->uc_vers &&
  554.           ent->cache_prog == uc->uc_prog &&
  555.           EQADDR(ent->cache_addr, uc->uc_addr)) {
  556.             *replyp = ent->cache_reply;
  557.             *replylenp = ent->cache_replylen;
  558.             return(1);
  559.         }
  560.     }
  561.     /*
  562.      * Failed to find entry
  563.      * Remember a few things so we can do a set later
  564.      */
  565.     uc->uc_proc = msg->rm_call.cb_proc;
  566.     uc->uc_vers = msg->rm_call.cb_vers;
  567.     uc->uc_prog = msg->rm_call.cb_prog;
  568.     uc->uc_addr = xprt->xp_raddr;
  569.     return(0);
  570. }
  571.  
  572.