svc_simple.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* @(#)svc_simple.c 2.2 88/08/01 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[] =
  32. "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
  33. #endif
  34. /*
  35. * svc_simple.c
  36. * Simplified front end to rpc.
  37. *
  38. * Copyright (C) 1984, Sun Microsystems, Inc.
  39. */
  40. #include <stdio.h>
  41. #include <rpc/rpc.h>
  42. #include <sys/socket.h>
  43. #include <netdb.h>
  44. static struct proglst {
  45. char *(*p_progname) ();
  46. int p_prognum;
  47. int p_procnum;
  48. xdrproc_t p_inproc, p_outproc;
  49. struct proglst *p_nxt;
  50. } *proglst;
  51. static void universal();
  52. static SVCXPRT *transp;
  53. struct proglst *pl;
  54. registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
  55. char *(*progname) ();
  56. xdrproc_t inproc, outproc;
  57. {
  58. if (procnum == NULLPROC) {
  59. (void) fprintf(stderr,
  60. "can't reassign procedure number %d\n", NULLPROC);
  61. return (-1);
  62. }
  63. if (transp == 0) {
  64. transp = svcudp_create(RPC_ANYSOCK);
  65. if (transp == NULL) {
  66. (void) fprintf(stderr, "couldn't create an rpc server\n");
  67. return (-1);
  68. }
  69. }
  70. (void) pmap_unset((u_long) prognum, (u_long) versnum);
  71. if (!svc_register(transp, (u_long) prognum, (u_long) versnum,
  72. universal, IPPROTO_UDP)) {
  73. (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
  74. prognum, versnum);
  75. return (-1);
  76. }
  77. pl = (struct proglst *) malloc(sizeof(struct proglst));
  78. if (pl == NULL) {
  79. (void) fprintf(stderr, "registerrpc: out of memory\n");
  80. return (-1);
  81. }
  82. pl->p_progname = progname;
  83. pl->p_prognum = prognum;
  84. pl->p_procnum = procnum;
  85. pl->p_inproc = inproc;
  86. pl->p_outproc = outproc;
  87. pl->p_nxt = proglst;
  88. proglst = pl;
  89. return (0);
  90. }
  91. static void universal(rqstp, transp)
  92. struct svc_req *rqstp;
  93. SVCXPRT *transp;
  94. {
  95. int prog, proc;
  96. char *outdata;
  97. char xdrbuf[UDPMSGSIZE];
  98. struct proglst *pl;
  99. /*
  100. * enforce "procnum 0 is echo" convention
  101. */
  102. if (rqstp->rq_proc == NULLPROC) {
  103. if (svc_sendreply(transp, xdr_void, (char *) NULL) == FALSE) {
  104. (void) fprintf(stderr, "xxx\n");
  105. exit(1);
  106. }
  107. return;
  108. }
  109. prog = rqstp->rq_prog;
  110. proc = rqstp->rq_proc;
  111. for (pl = proglst; pl != NULL; pl = pl->p_nxt)
  112. if (pl->p_prognum == prog && pl->p_procnum == proc) {
  113. /* decode arguments into a CLEAN buffer */
  114. bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
  115. if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
  116. svcerr_decode(transp);
  117. return;
  118. }
  119. outdata = (*(pl->p_progname)) (xdrbuf);
  120. if (outdata == NULL && pl->p_outproc != xdr_void)
  121. /* there was an error */
  122. return;
  123. if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
  124. (void) fprintf(stderr,
  125. "trouble replying to prog %d\n",
  126. pl->p_prognum);
  127. exit(1);
  128. }
  129. /* free the decoded arguments */
  130. (void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
  131. return;
  132. }
  133. (void) fprintf(stderr, "never registered prog %d\n", prog);
  134. exit(1);
  135. }