svc_simple.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * svc_simple.c
  35. * Simplified front end to rpc.
  36. *
  37. * Copyright (C) 1984, Sun Microsystems, Inc.
  38. */
  39. #include <stdio.h>
  40. #include <rpc/rpc.h>
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43. static struct proglst {
  44. char *(*p_progname)();
  45. int p_prognum;
  46. int p_procnum;
  47. xdrproc_t p_inproc, p_outproc;
  48. struct proglst *p_nxt;
  49. } *proglst;
  50. static void universal();
  51. static SVCXPRT *transp;
  52. struct proglst *pl;
  53. registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
  54. char *(*progname)();
  55. xdrproc_t inproc, outproc;
  56. {
  57. if (procnum == NULLPROC) {
  58. (void) fprintf(stderr,
  59. "can't reassign procedure number %d\n", NULLPROC);
  60. return (-1);
  61. }
  62. if (transp == 0) {
  63. transp = svcudp_create(RPC_ANYSOCK);
  64. if (transp == NULL) {
  65. (void) fprintf(stderr, "couldn't create an rpc server\n");
  66. return (-1);
  67. }
  68. }
  69. (void) pmap_unset((u_long)prognum, (u_long)versnum);
  70. if (!svc_register(transp, (u_long)prognum, (u_long)versnum,
  71. universal, IPPROTO_UDP)) {
  72. (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
  73. prognum, versnum);
  74. return (-1);
  75. }
  76. pl = (struct proglst *)malloc(sizeof(struct proglst));
  77. if (pl == NULL) {
  78. (void) fprintf(stderr, "registerrpc: out of memory\n");
  79. return (-1);
  80. }
  81. pl->p_progname = progname;
  82. pl->p_prognum = prognum;
  83. pl->p_procnum = procnum;
  84. pl->p_inproc = inproc;
  85. pl->p_outproc = outproc;
  86. pl->p_nxt = proglst;
  87. proglst = pl;
  88. return (0);
  89. }
  90. static void
  91. 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. }