clnt_generic.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #define __FORCE_GLIBC
  33. #include <features.h>
  34. #include <alloca.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <rpc/rpc.h>
  38. #include <sys/socket.h>
  39. #include <netdb.h>
  40. libc_hidden_proto(memcpy)
  41. libc_hidden_proto(memset)
  42. libc_hidden_proto(strcmp)
  43. libc_hidden_proto(strcpy)
  44. libc_hidden_proto(clnttcp_create)
  45. libc_hidden_proto(clntudp_create)
  46. libc_hidden_proto(clntunix_create)
  47. libc_hidden_proto(getprotobyname_r)
  48. libc_hidden_proto(gethostbyname_r)
  49. libc_hidden_proto(__rpc_thread_createerr)
  50. /*
  51. * Generic client creation: takes (hostname, program-number, protocol) and
  52. * returns client handle. Default options are set, which the user can
  53. * change using the rpc equivalent of ioctl()'s.
  54. */
  55. CLIENT *
  56. clnt_create (const char *hostname, u_long prog, u_long vers,
  57. const char *proto)
  58. {
  59. struct hostent hostbuf, *h;
  60. size_t hstbuflen;
  61. char *hsttmpbuf;
  62. struct protoent protobuf, *p;
  63. size_t prtbuflen;
  64. char *prttmpbuf;
  65. struct sockaddr_in sin;
  66. struct sockaddr_un sun;
  67. int sock;
  68. struct timeval tv;
  69. CLIENT *client;
  70. int herr;
  71. if (strcmp (proto, "unix") == 0)
  72. {
  73. memset ((char *)&sun, 0, sizeof (sun));
  74. sun.sun_family = AF_UNIX;
  75. strcpy (sun.sun_path, hostname);
  76. sock = RPC_ANYSOCK;
  77. client = clntunix_create (&sun, prog, vers, &sock, 0, 0);
  78. if (client == NULL)
  79. return NULL;
  80. #if 0
  81. /* This is not wanted. This would disable the user from having
  82. a timeout in the clnt_call() call. Only a call to cnlt_control()
  83. by the user should set the timeout value. */
  84. tv.tv_sec = 25;
  85. tv.tv_usec = 0;
  86. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  87. #endif
  88. return client;
  89. }
  90. hstbuflen = 1024;
  91. hsttmpbuf = alloca (hstbuflen);
  92. while (gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen,
  93. &h, &herr) != 0
  94. || h == NULL)
  95. if (herr != NETDB_INTERNAL || errno != ERANGE)
  96. {
  97. get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST;
  98. return NULL;
  99. }
  100. else
  101. {
  102. /* Enlarge the buffer. */
  103. hstbuflen *= 2;
  104. hsttmpbuf = alloca (hstbuflen);
  105. }
  106. if (h->h_addrtype != AF_INET)
  107. {
  108. /*
  109. * Only support INET for now
  110. */
  111. struct rpc_createerr *ce = &get_rpc_createerr ();
  112. ce->cf_stat = RPC_SYSTEMERROR;
  113. ce->cf_error.re_errno = EAFNOSUPPORT;
  114. return NULL;
  115. }
  116. sin.sin_family = h->h_addrtype;
  117. sin.sin_port = 0;
  118. memset (sin.sin_zero, 0, sizeof (sin.sin_zero));
  119. memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length);
  120. prtbuflen = 1024;
  121. prttmpbuf = alloca (prtbuflen);
  122. while (getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
  123. || p == NULL)
  124. if (errno != ERANGE)
  125. {
  126. struct rpc_createerr *ce = &get_rpc_createerr ();
  127. ce->cf_stat = RPC_UNKNOWNPROTO;
  128. ce->cf_error.re_errno = EPFNOSUPPORT;
  129. return NULL;
  130. }
  131. else
  132. {
  133. /* Enlarge the buffer. */
  134. prtbuflen *= 2;
  135. prttmpbuf = alloca (prtbuflen);
  136. }
  137. sock = RPC_ANYSOCK;
  138. switch (p->p_proto)
  139. {
  140. case IPPROTO_UDP:
  141. tv.tv_sec = 5;
  142. tv.tv_usec = 0;
  143. client = clntudp_create (&sin, prog, vers, tv, &sock);
  144. if (client == NULL)
  145. {
  146. return NULL;
  147. }
  148. #if 0
  149. /* This is not wanted. This would disable the user from having
  150. a timeout in the clnt_call() call. Only a call to cnlt_control()
  151. by the user should set the timeout value. */
  152. tv.tv_sec = 25;
  153. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  154. #endif
  155. break;
  156. case IPPROTO_TCP:
  157. client = clnttcp_create (&sin, prog, vers, &sock, 0, 0);
  158. if (client == NULL)
  159. {
  160. return NULL;
  161. }
  162. #if 0
  163. /* This is not wanted. This would disable the user from having
  164. a timeout in the clnt_call() call. Only a call to cnlt_control()
  165. by the user should set the timeout value. */
  166. tv.tv_sec = 25;
  167. tv.tv_usec = 0;
  168. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  169. #endif
  170. break;
  171. default:
  172. {
  173. struct rpc_createerr *ce = &get_rpc_createerr ();
  174. ce->cf_stat = RPC_SYSTEMERROR;
  175. ce->cf_error.re_errno = EPFNOSUPPORT;
  176. }
  177. return (NULL);
  178. }
  179. return client;
  180. }