clnt_generic.c 5.2 KB

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