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

  1. /*
  2.  *  pmap.c --
  3.  *      Portmapper program for PC NFS file server.
  4.  *      Service runs on top of the net daemon (netd.c).
  5.  *      This is a partial implementation of the UNIX port mapper
  6.  *      portmap(8c).
  7.  *
  8.  *  Author:
  9.  *      See-Mong Tan, 6/12/88
  10.  *      portmapper bug fixed 3/23/89
  11.  */
  12.  
  13. #include "common.h"
  14.  
  15. /*
  16.  *  bool_t pmap_init() --
  17.  *      Portmapper initializer.  Called by the net daemon.  Creates
  18.  *      and registers it's own transport handler and dispatch routine.
  19.  *      Returns TRUE if successful, or FALSE if an error occurred.
  20.  */
  21. bool_t pmap_init()
  22. {
  23.     SVCXPRT *transp;
  24.     struct sockaddr_in addr;
  25.     int sock;
  26.  
  27.     addr.sin_family = AF_INET;
  28.     addr.sin_port = htons(PMAPPORT);
  29.     addr.sin_addr.s_addr = INADDR_ANY;
  30.     if ((sock = sock_create(SOCK_DGRAM, IPPROTO_UDP, &addr)) < 0) {
  31.         (void) fprintf(stderr, "pmap: cannot create UDP socket\n");
  32.         return FALSE;
  33.     }
  34.     if ((transp = svcudp_create(sock, 0)) == (SVCXPRT *) NULL)
  35.         return FALSE;
  36.  
  37.     if (! svc_register(transp, PMAPPROG, PMAPVERS, 
  38.                 pmap_dispatch, IPPROTO_UDP)) {
  39.         (void) fprintf(stderr, "pmap_init: cannot register handle\n");
  40.         sock_close(sock);
  41.         return FALSE;
  42.     }
  43.     return TRUE;
  44. }
  45.         
  46. /*
  47.  *  bool_t pmap_dispatch(struct svc_req *rqstp, SVCXPRT *transp) --
  48.  *      Dispatch routine for portmapper.  This is only a partial
  49.  *      implementation, in particular, PMAPPROC_SET and PMAPPROC_UNSET
  50.  *      are not supported.
  51.  */
  52. bool_t pmap_dispatch(rqstp, transp)
  53.     struct svc_req *rqstp;
  54.     SVCXPRT *transp;
  55. {
  56.     int NOTSUPP = 0;     /* error not supported */
  57.     struct pmap reg;
  58.     struct sockaddr_in addr;
  59.     int port;
  60.  
  61.     switch((int) rqstp->rq_proc) {
  62.  
  63.     case PMAPPROC_NULL:   /* NULL procedure call */
  64.         if (NFS_VERBOSE)
  65.             (void) printf(">>> PMMAPPROC_NULL\n");
  66.         if (! svc_sendreply(transp, xdr_void, NULL))
  67.             (void) fprintf(stderr, "pmap: cannot send reply\n");
  68.         break;
  69.  
  70.     case PMAPPROC_SET:   /* these reqeusts are not supported */
  71.     case PMAPPROC_UNSET:
  72.         if (NFS_VERBOSE)
  73.             (void) printf("pmap: PMAPPROC_SET/UNSET\n");
  74.         if (! svc_sendreply(transp, xdr_int, &NOTSUPP))
  75.             (void) fprintf(stderr, "pmap: cannot send reply\n");
  76.         break;
  77.  
  78.     case PMAPPROC_GETPORT:  /* get a port */
  79.         if (NFS_VERBOSE)
  80.             (void) printf("\n>>> PMAPPROC_GETPORT\t"); 
  81.         if (! svc_getargs(transp, xdr_pmap, ®)) {
  82.             svcerr_decode(transp);
  83.             break;
  84.         }
  85.         if (NFS_VERBOSE)
  86.             (void) printf("--> prog = %ld, vers = %ld:\t", 
  87.                       reg.pm_prog, reg.pm_vers);
  88.         if (reg.pm_prog == NFS_PROGRAM && reg.pm_vers == NFS_VERSION) {
  89.             port = NFS_PORT;
  90.             if (NFS_VERBOSE)
  91.                 (void) printf(" --> NFS_PORT\n");
  92.             if (! svc_sendreply(transp, xdr_int, (caddr_t)&port)) {
  93.                 (void) fprintf(stderr, 
  94.                         "pmap: cannot send reply\n");
  95.                 break;
  96.             }
  97.             break;
  98.         }
  99.         if (reg.pm_prog== MOUNTPROG && reg.pm_vers== MOUNTVERS) {
  100.             port = MOUNTPORT;
  101.             if (NFS_VERBOSE)
  102.                 (void) printf("--> MOUNTPORT\n");
  103.             if (! svc_sendreply(transp, xdr_int, (caddr_t)&port)) {
  104.                 (void) fprintf(stderr, "pmap: cannot reply\n");
  105.                 break;
  106.             }
  107.             break;
  108.         }
  109.         /* everything else is not supported */
  110.         port = 0;
  111.         if (NFS_VERBOSE)
  112.             (void) printf("--> NO PORT REGISTERED\n");
  113.         if (! svc_sendreply(transp, xdr_int, (caddr_t)&port))
  114.             (void) fprintf(stderr, "cannot reply\n");
  115.         break;
  116.  
  117.         /* The fix  for the portmapper bug discovered by
  118.          * Stephen Nahm follows
  119.          */
  120.     case PMAPPROC_CALLIT:
  121.         if (NFS_VERBOSE)
  122.             (void) printf(">>> PMAPPROC_DUMP/PMAPPROC_CALLIT\n");
  123.         break;    /* should break out and not reply */
  124.         
  125.     case PMAPPROC_DUMP:
  126.         /* FALLTHROUGH */
  127.     default:
  128.         svcerr_noproc(transp);
  129.     }
  130. }
  131.  
  132. /*
  133.  *  bool_t xdr_pmap(XDR *xdrs, struct pmap *regs) --
  134.  *      XDR a pmap request.
  135.  */
  136. bool_t xdr_pmap(xdrs, regs)
  137.     XDR *xdrs;
  138.     struct pmap *regs;
  139. {
  140.  
  141.     if (xdr_u_long(xdrs, ®s->pm_prog) && 
  142.         xdr_u_long(xdrs, ®s->pm_vers) && 
  143.         xdr_u_long(xdrs, ®s->pm_prot))
  144.         return (xdr_u_long(xdrs, ®s->pm_port));
  145.     return (FALSE);
  146. }
  147.