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