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. #define __FORCE_GLIBC
  31. #include <features.h>
  32. /*
  33. * svc_simple.c
  34. * Simplified front end to rpc.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. */
  38. #include <stdio.h>
  39. #include <rpc/rpc.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. static struct proglst {
  43. char *(*p_progname) ();
  44. int p_prognum;
  45. int p_procnum;
  46. xdrproc_t p_inproc, p_outproc;
  47. struct proglst *p_nxt;
  48. } *proglst;
  49. static void universal();
  50. static SVCXPRT *transp;
  51. struct proglst *pl;
  52. extern bool_t pmap_unset(u_long program, u_long version);
  53. int 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 %lu\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 universal(rqstp, transp)
  91. struct svc_req *rqstp;
  92. SVCXPRT *transp;
  93. {
  94. int prog, proc;
  95. char *outdata;
  96. char xdrbuf[UDPMSGSIZE];
  97. struct proglst *pl;
  98. /*
  99. * enforce "procnum 0 is echo" convention
  100. */
  101. if (rqstp->rq_proc == NULLPROC) {
  102. if (svc_sendreply(transp, (xdrproc_t) xdr_void, (char *) NULL) == FALSE) {
  103. (void) fprintf(stderr, "xxx\n");
  104. exit(1);
  105. }
  106. return;
  107. }
  108. prog = rqstp->rq_prog;
  109. proc = rqstp->rq_proc;
  110. for (pl = proglst; pl != NULL; pl = pl->p_nxt)
  111. if (pl->p_prognum == prog && pl->p_procnum == proc) {
  112. /* decode arguments into a CLEAN buffer */
  113. bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
  114. if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
  115. svcerr_decode(transp);
  116. return;
  117. }
  118. outdata = (*(pl->p_progname)) (xdrbuf);
  119. if (outdata == NULL && pl->p_outproc != (xdrproc_t) xdr_void)
  120. /* there was an error */
  121. return;
  122. if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
  123. (void) fprintf(stderr,
  124. "trouble replying to prog %d\n",
  125. pl->p_prognum);
  126. exit(1);
  127. }
  128. /* free the decoded arguments */
  129. (void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
  130. return;
  131. }
  132. (void) fprintf(stderr, "never registered prog %d\n", prog);
  133. exit(1);
  134. }