clnt_generic.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3. * unrestricted use provided that this legend is included on all tape
  4. * media and as a part of the software program in whole or part. Users
  5. * may copy or modify Sun RPC without charge, but are not authorized
  6. * to license or distribute it to anyone else except as part of a product or
  7. * program developed by the user.
  8. *
  9. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12. *
  13. * Sun RPC is provided with no support and without any obligation on the
  14. * part of Sun Microsystems, Inc. to assist in its use, correction,
  15. * modification or enhancement.
  16. *
  17. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19. * OR ANY PART THEREOF.
  20. *
  21. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22. * or profits or other special, indirect and consequential damages, even if
  23. * Sun has been advised of the possibility of such damages.
  24. *
  25. * Sun Microsystems, Inc.
  26. * 2550 Garcia Avenue
  27. * Mountain View, California 94043
  28. */
  29. /*
  30. * Copyright (C) 1987, Sun Microsystems, Inc.
  31. */
  32. #include <alloca.h>
  33. #include <errno.h>
  34. #include <string.h>
  35. #include <rpc/rpc.h>
  36. #include <sys/socket.h>
  37. #include <netdb.h>
  38. /*
  39. * Generic client creation: takes (hostname, program-number, protocol) and
  40. * returns client handle. Default options are set, which the user can
  41. * change using the rpc equivalent of ioctl()'s.
  42. */
  43. CLIENT *
  44. clnt_create (const char *hostname, u_long prog, u_long vers,
  45. const char *proto)
  46. {
  47. struct hostent hostbuf, *h;
  48. size_t hstbuflen;
  49. char *hsttmpbuf;
  50. struct protoent protobuf, *p;
  51. size_t prtbuflen;
  52. char *prttmpbuf;
  53. struct sockaddr_in sin;
  54. struct sockaddr_un sun;
  55. int sock;
  56. struct timeval tv;
  57. CLIENT *client;
  58. int herr;
  59. if (strcmp (proto, "unix") == 0)
  60. {
  61. memset ((char *)&sun, 0, sizeof (sun));
  62. sun.sun_family = AF_UNIX;
  63. strcpy (sun.sun_path, hostname);
  64. sock = RPC_ANYSOCK;
  65. client = clntunix_create (&sun, prog, vers, &sock, 0, 0);
  66. if (client == NULL)
  67. return NULL;
  68. #if 0
  69. /* This is not wanted. This would disable the user from having
  70. a timeout in the clnt_call() call. Only a call to cnlt_control()
  71. by the user should set the timeout value. */
  72. tv.tv_sec = 25;
  73. tv.tv_usec = 0;
  74. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  75. #endif
  76. return client;
  77. }
  78. hstbuflen = 1024;
  79. hsttmpbuf = alloca (hstbuflen);
  80. while (gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen,
  81. &h, &herr) != 0
  82. || h == NULL)
  83. if (herr != NETDB_INTERNAL || errno != ERANGE)
  84. {
  85. get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST;
  86. return NULL;
  87. }
  88. else
  89. {
  90. /* Enlarge the buffer. */
  91. hstbuflen *= 2;
  92. hsttmpbuf = alloca (hstbuflen);
  93. }
  94. if (h->h_addrtype != AF_INET)
  95. {
  96. /*
  97. * Only support INET for now
  98. */
  99. struct rpc_createerr *ce = &get_rpc_createerr ();
  100. ce->cf_stat = RPC_SYSTEMERROR;
  101. ce->cf_error.re_errno = EAFNOSUPPORT;
  102. return NULL;
  103. }
  104. sin.sin_family = h->h_addrtype;
  105. sin.sin_port = 0;
  106. memset (sin.sin_zero, 0, sizeof (sin.sin_zero));
  107. memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length);
  108. prtbuflen = 1024;
  109. prttmpbuf = alloca (prtbuflen);
  110. while (getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
  111. || p == NULL)
  112. if (errno != ERANGE)
  113. {
  114. struct rpc_createerr *ce = &get_rpc_createerr ();
  115. ce->cf_stat = RPC_UNKNOWNPROTO;
  116. ce->cf_error.re_errno = EPFNOSUPPORT;
  117. return NULL;
  118. }
  119. else
  120. {
  121. /* Enlarge the buffer. */
  122. prtbuflen *= 2;
  123. prttmpbuf = alloca (prtbuflen);
  124. }
  125. sock = RPC_ANYSOCK;
  126. switch (p->p_proto)
  127. {
  128. case IPPROTO_UDP:
  129. tv.tv_sec = 5;
  130. tv.tv_usec = 0;
  131. client = clntudp_create (&sin, prog, vers, tv, &sock);
  132. if (client == NULL)
  133. {
  134. return NULL;
  135. }
  136. #if 0
  137. /* This is not wanted. This would disable the user from having
  138. a timeout in the clnt_call() call. Only a call to cnlt_control()
  139. by the user should set the timeout value. */
  140. tv.tv_sec = 25;
  141. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  142. #endif
  143. break;
  144. case IPPROTO_TCP:
  145. client = clnttcp_create (&sin, prog, vers, &sock, 0, 0);
  146. if (client == NULL)
  147. {
  148. return NULL;
  149. }
  150. #if 0
  151. /* This is not wanted. This would disable the user from having
  152. a timeout in the clnt_call() call. Only a call to cnlt_control()
  153. by the user should set the timeout value. */
  154. tv.tv_sec = 25;
  155. tv.tv_usec = 0;
  156. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  157. #endif
  158. break;
  159. default:
  160. {
  161. struct rpc_createerr *ce = &get_rpc_createerr ();
  162. ce->cf_stat = RPC_SYSTEMERROR;
  163. ce->cf_error.re_errno = EPFNOSUPPORT;
  164. }
  165. return (NULL);
  166. }
  167. return client;
  168. }