clnt_generic.c 5.0 KB

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